@diffci.com/diffci 0.3.35 → 0.3.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,11 +6,17 @@
6
6
  * repository gates in CI. Instructions alone did not make DiffCI the default. A hook does: every push,
7
7
  * by an agent or a person, verifies the commits being pushed before they leave the machine.
8
8
  *
9
- * BEHAVIOUR. For each pushed ref that is the checked-out HEAD, the hook runs
10
- * `diffci verify --base <remote sha> --head <local sha>` (a new branch uses its merge base with the
11
- * remote's default branch). Failing verification (exit 1) blocks the push. When DiffCI cannot verify
12
- * (exit 2: dirty checkout, unknown base, analysis refused) it says why and lets the push through,
13
- * because required CI still runs; `DIFFCI_HOOK_STRICT=1` blocks instead. `git push --no-verify` skips it.
9
+ * BEHAVIOUR. For each pushed ref the hook runs `diffci verify --base <remote sha> --head <local sha>`
10
+ * (a new branch uses its merge base with the remote's default branch). Failing verification (exit 1)
11
+ * blocks the push. When DiffCI cannot verify (exit 2: unknown base, analysis refused) it says why and
12
+ * lets the push through, because required CI still runs; `DIFFCI_HOOK_STRICT=1` blocks instead.
13
+ * `git push --no-verify` skips it.
14
+ *
15
+ * CLEAN WORKTREE (2026-11-08). Commit-range verification refuses a dirty checkout, so a stray untracked
16
+ * file (DentalPresence had an empty one named "True") made every push unverifiable. When the checkout
17
+ * has other changes, or the pushed ref is not checked out, the hook verifies in a temporary detached
18
+ * worktree of the pushed commit, linking the checkout's node_modules only when the root lockfiles match.
19
+ * Links are removed before the worktree, so cleanup can never delete the real dependencies.
14
20
  *
15
21
  * REPORTING (2026-10-29). When DIFFCI_API_URL and DIFFCI_TOKEN are set, the hook also sends an
16
22
  * observation report for the pushed range, the same report the GitHub Action sends. A dogfood repository
@@ -9,11 +9,17 @@ import { dirname, isAbsolute, join, relative } from "node:path";
9
9
  * repository gates in CI. Instructions alone did not make DiffCI the default. A hook does: every push,
10
10
  * by an agent or a person, verifies the commits being pushed before they leave the machine.
11
11
  *
12
- * BEHAVIOUR. For each pushed ref that is the checked-out HEAD, the hook runs
13
- * `diffci verify --base <remote sha> --head <local sha>` (a new branch uses its merge base with the
14
- * remote's default branch). Failing verification (exit 1) blocks the push. When DiffCI cannot verify
15
- * (exit 2: dirty checkout, unknown base, analysis refused) it says why and lets the push through,
16
- * because required CI still runs; `DIFFCI_HOOK_STRICT=1` blocks instead. `git push --no-verify` skips it.
12
+ * BEHAVIOUR. For each pushed ref the hook runs `diffci verify --base <remote sha> --head <local sha>`
13
+ * (a new branch uses its merge base with the remote's default branch). Failing verification (exit 1)
14
+ * blocks the push. When DiffCI cannot verify (exit 2: unknown base, analysis refused) it says why and
15
+ * lets the push through, because required CI still runs; `DIFFCI_HOOK_STRICT=1` blocks instead.
16
+ * `git push --no-verify` skips it.
17
+ *
18
+ * CLEAN WORKTREE (2026-11-08). Commit-range verification refuses a dirty checkout, so a stray untracked
19
+ * file (DentalPresence had an empty one named "True") made every push unverifiable. When the checkout
20
+ * has other changes, or the pushed ref is not checked out, the hook verifies in a temporary detached
21
+ * worktree of the pushed commit, linking the checkout's node_modules only when the root lockfiles match.
22
+ * Links are removed before the worktree, so cleanup can never delete the real dependencies.
17
23
  *
18
24
  * REPORTING (2026-10-29). When DIFFCI_API_URL and DIFFCI_TOKEN are set, the hook also sends an
19
25
  * observation report for the pushed range, the same report the GitHub Action sends. A dogfood repository
@@ -29,24 +35,85 @@ ${HOOK_MARKER}
29
35
  remote="$1"
30
36
  zero=0000000000000000000000000000000000000000
31
37
  head=$(git rev-parse HEAD)
38
+ top=$(git rev-parse --show-toplevel)
32
39
  status=0
40
+ case "$(uname -s 2>/dev/null)" in MINGW*|MSYS*|CYGWIN*) windows=1 ;; *) windows=0 ;; esac
41
+
42
+ # Link a dependency directory from the checkout into the temporary worktree, and remove only the link.
43
+ link_dir() {
44
+ if [ "$windows" = "1" ]; then
45
+ # //J, not /J: Git Bash would rewrite /J as a path.
46
+ cmd //c mklink //J "$(cygpath -w "$2")" "$(cygpath -w "$1")" >/dev/null 2>&1
47
+ else
48
+ ln -s "$1" "$2" 2>/dev/null
49
+ fi
50
+ }
51
+ unlink_dir() {
52
+ if [ "$windows" = "1" ]; then cmd //c rmdir "$(cygpath -w "$1")" >/dev/null 2>&1; else rm -f "$1"; fi
53
+ }
54
+
55
+ # Verifies the pushed commit in a clean temporary worktree, so uncommitted or untracked files in this
56
+ # checkout neither block nor influence the result. Installed dependencies are reused only when the
57
+ # pushed commit's lockfiles match this checkout's; otherwise it reports that it could not verify.
58
+ verify_in_worktree() {
59
+ tmpbase="$TMPDIR"; [ -n "$tmpbase" ] || tmpbase=/tmp
60
+ tmp=$(mktemp -d "$tmpbase/diffci-push-XXXXXX") || return 2
61
+ tree="$tmp/tree"
62
+ if ! git worktree add --quiet --detach "$tree" "$2" >/dev/null 2>&1; then
63
+ rmdir "$tmp" 2>/dev/null
64
+ echo "diffci: could not create a temporary worktree" >&2
65
+ return 2
66
+ fi
67
+ linked=""
68
+ result=0
69
+ # Blob ids, not bytes: Git normalizes line endings (core.autocrlf) the same way on both sides.
70
+ for lock in package-lock.json npm-shrinkwrap.json pnpm-lock.yaml yarn.lock bun.lock bun.lockb; do
71
+ here=""; pushed=""
72
+ [ -f "$top/$lock" ] && here=$(git hash-object -- "$top/$lock" 2>/dev/null)
73
+ pushed=$(git rev-parse -q --verify "$2:$lock" 2>/dev/null)
74
+ if [ -n "$here$pushed" ]; then
75
+ if [ "$here" != "$pushed" ]; then
76
+ echo "diffci: $lock differs from the pushed commit; install dependencies for it, or push from a clean checkout" >&2
77
+ result=2
78
+ fi
79
+ fi
80
+ done
81
+ if [ "$result" -eq 0 ]; then
82
+ for dir in $(cd "$top" && find . -maxdepth 4 \\( -name .git -o -name node_modules \\) -prune -name node_modules -type d -print 2>/dev/null); do
83
+ if [ -d "$tree/$(dirname "$dir")" ] && [ ! -e "$tree/$dir" ] && link_dir "$top/$dir" "$tree/$dir"; then
84
+ linked="$linked $tree/$dir"
85
+ fi
86
+ done
87
+ (cd "$tree" && ${command} verify --base "$1" --head "$2" </dev/null)
88
+ result=$?
89
+ fi
90
+ # Links first: removing the worktree must never reach through a link into this checkout.
91
+ for link in $linked; do unlink_dir "$link"; done
92
+ git worktree remove --force "$tree" >/dev/null 2>&1 || rm -rf "$tree"
93
+ git worktree prune >/dev/null 2>&1
94
+ rmdir "$tmp" 2>/dev/null
95
+ return $result
96
+ }
97
+
33
98
  while read -r local_ref local_sha remote_ref remote_sha; do
34
99
  [ "$local_sha" = "$zero" ] && continue
35
- if [ "$local_sha" != "$head" ]; then
36
- echo "diffci: $local_ref is not checked out; not verified here" >&2
37
- continue
38
- fi
39
100
  base="$remote_sha"
40
101
  if [ "$base" = "$zero" ] || ! git cat-file -e "$base^{commit}" 2>/dev/null; then
41
- base=$(git merge-base HEAD "refs/remotes/$remote/HEAD" 2>/dev/null || true)
102
+ base=$(git merge-base "$local_sha" "refs/remotes/$remote/HEAD" 2>/dev/null || true)
42
103
  fi
43
104
  if [ -z "$base" ] || [ "$base" = "$local_sha" ]; then
44
105
  echo "diffci: no base commit for $local_ref; not verified" >&2
45
106
  continue
46
107
  fi
47
- echo "diffci: verifying $base..$local_sha before push" >&2
48
- ${command} verify --base "$base" --head "$local_sha" </dev/null
49
- code=$?
108
+ if [ "$local_sha" = "$head" ] && [ -z "$(git status --porcelain 2>/dev/null)" ]; then
109
+ echo "diffci: verifying $base..$local_sha before push" >&2
110
+ ${command} verify --base "$base" --head "$local_sha" </dev/null
111
+ code=$?
112
+ else
113
+ echo "diffci: verifying $base..$local_sha in a clean temporary worktree (this checkout has other changes or another branch)" >&2
114
+ verify_in_worktree "$base" "$local_sha"
115
+ code=$?
116
+ fi
50
117
  if [ "$code" -eq 1 ]; then
51
118
  echo "diffci: verification failed; push blocked. Fix it, or skip once with git push --no-verify" >&2
52
119
  status=1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@diffci.com/diffci",
3
- "version": "0.3.35",
3
+ "version": "0.3.36",
4
4
  "description": "Fail-closed, change-aware verification for AI coding agents with conservative affected-test selection",
5
5
  "keywords": [
6
6
  "ci",