Skip to content

Commit 70024d4

Browse files
dhruv-15-03Copilot
andcommitted
fix(git-extension): limit commit_style parsing to first match in config
Address Copilot review feedback on PR #3413: grep '^commit_style:' without -m1 could concatenate values if a config file accidentally contains multiple commit_style lines (e.g. from a bad merge/manual edit), causing an unexpected fallback to 'fixed'. Limit to the first match and add a regression test covering duplicate commit_style lines. Assisted-by: GitHub Copilot (model: claude-sonnet-5, autonomous) Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 5f20fd5 commit 70024d4

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

extensions/git/scripts/bash/auto-commit.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ _commit_style="fixed"
5555

5656
if [ -f "$_config_file" ]; then
5757
# Top-level scalar key: commit_style (fixed | conventional)
58-
_style_val=$(grep '^commit_style:' "$_config_file" 2>/dev/null | sed 's/^commit_style:[[:space:]]*//' | sed 's/[[:space:]]\{1,\}#.*$//' | sed 's/[[:space:]]*$//' | sed 's/^["'\'']//' | sed 's/["'\'']*$//' | tr -d '[:space:]' | tr '[:upper:]' '[:lower:]')
58+
_style_val=$(grep -m1 '^commit_style:' "$_config_file" 2>/dev/null | sed 's/^commit_style:[[:space:]]*//' | sed 's/[[:space:]]\{1,\}#.*$//' | sed 's/[[:space:]]*$//' | sed 's/^["'\'']//' | sed 's/["'\'']*$//' | tr -d '[:space:]' | tr '[:upper:]' '[:lower:]')
5959
if [ -n "$_style_val" ]; then
6060
case "$_style_val" in
6161
fixed|conventional)

tests/extensions/git/test_git_extension.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,32 @@ def test_unknown_commit_style_defaults_to_fixed(self, tmp_path: Path):
12221222
)
12231223
assert "[Spec Kit] Add specification" in log.stdout
12241224

1225+
def test_duplicate_commit_style_lines_use_first_match(self, tmp_path: Path):
1226+
"""A config with multiple `commit_style:` lines (e.g. from a bad merge) uses only
1227+
the first match instead of concatenating values into an unrecognized style."""
1228+
project = _setup_project(tmp_path)
1229+
_write_config(project, (
1230+
"commit_style: conventional\n"
1231+
"commit_style: fixed\n"
1232+
"auto_commit:\n"
1233+
" default: false\n"
1234+
" after_specify:\n"
1235+
" enabled: true\n"
1236+
' message: "[Spec Kit] Add specification"\n'
1237+
))
1238+
(project / "new-file.txt").write_text("content")
1239+
result = _run_bash(
1240+
"auto-commit.sh", project, "after_specify", "feat: add OAuth specification"
1241+
)
1242+
assert result.returncode == 0
1243+
assert "unknown commit_style" not in result.stderr.lower()
1244+
log = subprocess.run(
1245+
["git", "log", "--oneline", "-1"],
1246+
cwd=project, capture_output=True, text=True,
1247+
)
1248+
assert "feat: add OAuth specification" in log.stdout
1249+
assert "[Spec Kit] Add specification" not in log.stdout
1250+
12251251

12261252
@pytest.mark.skipif(not HAS_PWSH, reason="pwsh not available")
12271253
class TestAutoCommitPowerShell:

0 commit comments

Comments
 (0)