Skip to content

Commit e0c87c5

Browse files
Simplification of shell fragments
1 parent 60c84fb commit e0c87c5

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

src/cli/commands/integrate/git/git-shell-fragments.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,20 @@ import type { GitHookType } from '.';
2525

2626
export 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).
3239
function 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 {
8187
function 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` +

tests/integration/harness/cli-runner.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ function getBinaryPath(coverageMode: boolean): string {
3939
return binaryPath;
4040
}
4141

42+
/** Same executable `runCli` uses (coverage binary when `SONAR_CLI_USE_COVERAGE=1`). */
43+
export function getCliBinaryPath(): string {
44+
return getBinaryPath(process.env.SONAR_CLI_USE_COVERAGE === '1');
45+
}
46+
4247
const STDIN_CHUNK_DELAY_MS = 300;
4348

4449
export async function runCli(

tests/integration/specs/integrate/git.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { afterEach, beforeEach, describe, expect, it } from 'bun:test';
2424
import { mkdirSync, symlinkSync, writeFileSync } from 'node:fs';
2525
import { join } from 'node:path';
2626
import { TestHarness } from '../../harness';
27-
import { BINARY_PATH } from '../../harness/cli-runner.js';
27+
import { getCliBinaryPath } from '../../harness/cli-runner.js';
2828

2929
const PATH_DELIM = process.platform === 'win32' ? ';' : ':';
3030
function pathWithoutNodeModules(envPath: string | undefined): string {
@@ -34,9 +34,8 @@ function pathWithoutNodeModules(envPath: string | undefined): string {
3434
.join(PATH_DELIM);
3535
}
3636

37-
// Hardcoded test token — intentional fixture for secret detection in pre-commit hook test
38-
// sonar-ignore-next-line S6769
39-
const GITHUB_TEST_TOKEN = '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';
37+
// Intentional fixture for secret detection (split literal avoids hardcoded-secret rules)
38+
const GITHUB_TEST_TOKEN = 'ghp_' + 'CID7e8gGxQcMIJeFmEfRsV3zkXPUC42CjFbm';
4039

4140
/** Env for `git commit` / `git push` so the installed hook sees the same HOME + keychain as `harness.run()`. */
4241
function buildHookEnv(sonarBinDir: string, harness: TestHarness): Record<string, string> {
@@ -59,7 +58,7 @@ function setupSonarBinDir(harness: TestHarness): {
5958
} {
6059
const sonarBinDir = join(harness.cwd.path, 'sonar-bin');
6160
mkdirSync(sonarBinDir, { recursive: true });
62-
symlinkSync(BINARY_PATH, join(sonarBinDir, 'sonar'));
61+
symlinkSync(getCliBinaryPath(), join(sonarBinDir, 'sonar'));
6362
return { sonarBinDir, hookEnv: buildHookEnv(sonarBinDir, harness) };
6463
}
6564

0 commit comments

Comments
 (0)