From b413e398d6f4e42a2270badee755cc58d8c1d742 Mon Sep 17 00:00:00 2001 From: Xianpeng Shen <3353385+shenxianpeng@users.noreply.github.com> Date: Wed, 25 Nov 2026 21:50:33 +0000 Subject: [PATCH] fix: answer git var for a squash message instead of logging it as unknown --- python/commit_check_forge.py | 5 +++++ src/__tests__/cli.test.js | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/python/commit_check_forge.py b/python/commit_check_forge.py index 5f697cd..d696d6a 100644 --- a/python/commit_check_forge.py +++ b/python/commit_check_forge.py @@ -115,6 +115,11 @@ def _git(repo: _Repository, args: list[str]) -> tuple[int, str, str]: if args[:1] == ["config"]: # No git config: identities come from the commits themselves. return 1, "", "" + if args in (["var", "GIT_AUTHOR_IDENT"], ["var", "GIT_COMMITTER_IDENT"]): + # Who a commit not made yet would be by: here, a squash merge whose + # author is whoever presses Merge, which Bitbucket does not tell + # merge checks. Answer as git does with no identity configured. + return 128, "", "fatal: unable to auto-detect email address\n" if args[:3] == ["log", "-n", "1"] and len(args) == 5 and args[3].startswith("--pretty=format:"): sha = repo.resolve(args[4]) if sha is None: diff --git a/src/__tests__/cli.test.js b/src/__tests__/cli.test.js index 37e3d13..0ed36ad 100644 --- a/src/__tests__/cli.test.js +++ b/src/__tests__/cli.test.js @@ -106,6 +106,25 @@ describe('the CLI in Pyodide', () => { expect(JSON.parse(res.stdout).checks.map((c) => [c.rule_id, c.status])).toEqual([['CC013', 'fail']]) }) + test('a squash message is checked without an author, as git with no identity would', async () => { + // Bitbucket does not tell a merge check who presses Merge, so the + // squash commit's author is unknown when the CLI asks git var. + const res = await cli.runCli({ + argv: ['.commit-check-message', '-m', '-n', '-e', '-b', '--format', 'json', '--no-banner'], + files: [ + { path: 'cchk.toml', text: '[commit]\nai_attribution = "forbid"\n' }, + { path: '.commit-check-message', text: 'bad message' } + ], + commits: [{ sha: sha(1), message: 'feat: x\n', parents: [], author }], + head: sha(1), + branch: 'feature/x' + }) + expect(res.unsupported).toEqual([]) + const status = Object.fromEntries(JSON.parse(res.stdout).checks.map((c) => [c.rule_id, c.status])) + expect(status.CC001).toBe('fail') + expect(status.CC017).not.toBe('fail') + }) + test('a git command it does not know fails as git would, and is listed', async () => { const res = await cli.runCli({ argv: ['-t', '--format', 'json', '--no-banner'], files: [], commits: [], head: null, branch: '' }) expect(res.unsupported).toEqual(['git tag --points-at HEAD'])