@@ -25,14 +25,20 @@ import type { GitHookType } from '.';
2525
2626export const HOOK_MARKER = 'Sonar secrets scan - installed by sonar integrate git' ;
2727
28+ /**
29+ * All-zero object id Git passes on pre-push stdin for ref deletion (`local_sha`) and new refs
30+ * (`remote_sha`). See githooks(5) "pre-push". SHA-1 length; SHA-256 repos use 64 hex zeros instead.
31+ */
32+ const GIT_HOOK_NULL_OID = '0000000000000000000000000000000000000000' ;
33+
2834// ─── Shared block ─────────────────────────────────────────────────────────────
2935// Used inside `while read ... done` in both native and Husky pre-push scripts.
3036// filesVar: shell variable name to assign results to.
31- // Indented 4 spaces to sit inside `while` + `if [ remote_sha = 0000... ]`.
37+ // Indented 4 spaces to sit inside `while` + `if [ remote_sha = null oid ]`.
38+ // `$EMPTY_TREE` is set once before the loop (see prePushBody).
3239function newBranchPushBlock ( filesVar : string ) : string {
3340 return (
3441 ` # New branch push — enumerate commits not yet on any remote, then diff-tree each one\n` +
35- ` EMPTY_TREE=4b825dc642cb6eb9a060e54bf8d69288fbee4904\n` +
3642 ` COMMITS=$(git rev-list "$local_sha" --not --remotes 2>/dev/null)\n` +
3743 ` if [ -n "$COMMITS" ]; then\n` +
3844 ` ${ filesVar } =$(echo "$COMMITS" | while IFS= read -r c; do\n` +
@@ -81,11 +87,13 @@ function preCommitBody(filesVar: string, binBlock: BinBlock): string {
8187function prePushBody ( filesVar : string , binBlock : BinBlock ) : string {
8288 return (
8389 `${ binBlock ( ) } \n` +
90+ `# Canonical empty tree: \`git mktree\` with no entries (correct for the repo's hash algorithm).\n` +
91+ `EMPTY_TREE=$(printf '' | git mktree)\n` +
8492 `# For each ref being pushed, scan files in the new commits\n` +
8593 `while read -r local_ref local_sha remote_ref remote_sha; do\n` +
8694 ` # Branch deletion — nothing to scan\n` +
87- ` [ "$local_sha" = '0000000000000000000000000000000000000000 ' ] && continue\n` +
88- ` if [ "$remote_sha" = '0000000000000000000000000000000000000000 ' ]; then\n` +
95+ ` [ "$local_sha" = '${ GIT_HOOK_NULL_OID } ' ] && continue\n` +
96+ ` if [ "$remote_sha" = '${ GIT_HOOK_NULL_OID } ' ]; then\n` +
8997 `${ newBranchPushBlock ( filesVar ) } \n` +
9098 ` else\n` +
9199 ` ${ filesVar } =$(git diff --name-only --diff-filter=ACMR "$remote_sha" "$local_sha")\n` +
0 commit comments