fix: silence git fetch stdout to prevent branch number parse error#1696
fix: silence git fetch stdout to prevent branch number parse error#1696fsilvaortiz wants to merge 3 commits intogithub:mainfrom
Conversation
`git fetch --all` can write status text (e.g., "Fetching origin...") to stdout, which gets captured by command substitution in check_existing_branches(). This contaminates BRANCH_NUMBER with non-numeric text, causing `$((10#$BRANCH_NUMBER))` to fail with "value too great for base". Redirect both stdout and stderr to /dev/null instead of stderr only. Closes github#1592 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…cing Adds 5 tests covering: - Valid JSON output from --json flag - FEATURE_NUM is always a zero-padded numeric string - No git fetch stdout leaks into JSON output - Repeated runs produce clean JSON without fetch artifacts - Regression guard: git fetch redirects both stdout and stderr Tests that require git are marked with skipif so they degrade gracefully in environments without git installed. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
db3da6f to
a45e9f0
Compare
There was a problem hiding this comment.
Pull request overview
Fixes scripts/bash/create-new-feature.sh --json failing when git fetch writes status text to stdout by silencing git fetch output so branch-number parsing remains purely numeric (closes #1592).
Changes:
- Redirect
git fetch --all --prunestdout + stderr to/dev/nullinsidecheck_existing_branches(). - Add pytest coverage for
--jsonoutput validity and a regression guard around the redirect behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
scripts/bash/create-new-feature.sh |
Silences git fetch stdout/stderr to prevent contaminating command-substitution output used for branch numbering. |
tests/test_create_new_feature.py |
Adds tests for JSON output validity and guards against reintroducing the old stderr-only redirect pattern. |
Comments suppressed due to low confidence (2)
tests/test_create_new_feature.py:106
test_script_does_not_leak_git_fetch_stdoutclaims stdout should contain exactly one line, but it only asserts there is one line starting with{. If extra non-JSON lines are present (e.g., fetch noise), this test can still pass. Consider assertinglen(stdout_lines) == 1(or otherwise asserting there are no non-JSON lines) to actually enforce the intended behavior.
stdout_lines = result.stdout.strip().splitlines()
# In JSON mode, stdout should contain exactly one line: the JSON object
json_lines = [line for line in stdout_lines if line.startswith("{")]
assert len(json_lines) == 1, f"Expected 1 JSON line, got {len(json_lines)}: {stdout_lines}"
tests/test_create_new_feature.py:133
- The redirect-pattern regression test is very brittle because it requires an exact string match for a specific redirection style/order. If the script later switches to an equivalent pattern (e.g.,
&>/dev/null,--quiet, different spacing, or adds flags), this test will fail even though behavior is correct. Consider using a more flexible check (e.g., regex that verifies both stdout and stderr are redirected) or a behavioral test that simulates stdout noise fromgit fetch.
def test_git_fetch_redirect_pattern_in_script(self):
"""The git fetch call should redirect both stdout and stderr to /dev/null."""
script_content = SCRIPT_PATH.read_text()
assert "git fetch --all --prune >/dev/null 2>&1" in script_content, (
"git fetch should redirect both stdout and stderr: >/dev/null 2>&1"
)
assert "git fetch --all --prune 2>/dev/null" not in script_content, (
"git fetch should NOT redirect only stderr (old pattern)"
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| def git_repo(tmp_path): | ||
| """Create a temporary git repo with a fake remote that produces stdout on fetch.""" | ||
| repo = tmp_path / "repo" | ||
| repo.mkdir() | ||
|
|
||
| # Initialize git repo | ||
| subprocess.run(["git", "init", str(repo)], capture_output=True, check=True) | ||
| subprocess.run(["git", "-C", str(repo), "config", "user.email", "test@test.com"], capture_output=True, check=True) | ||
| subprocess.run(["git", "-C", str(repo), "config", "user.name", "Test"], capture_output=True, check=True) |
There was a problem hiding this comment.
The git_repo fixture docstring says it creates “a fake remote that produces stdout on fetch”, but the fixture never configures any remotes. Either update the docstring to match what the fixture actually does, or add a local/bare remote setup so the test genuinely exercises the fetch-output scenario described in #1592.
This issue also appears in the following locations of the same file:
- line 103
- line 125
tests/test_create_new_feature.py
Outdated
| (repo / ".specify" / "templates").mkdir() | ||
|
|
||
| yield repo | ||
| shutil.rmtree(tmp_path) |
There was a problem hiding this comment.
Avoid manually deleting tmp_path in this fixture. Pytest owns tmp_path lifecycle, and shutil.rmtree(tmp_path) can introduce flaky teardown failures (e.g., if files are still open) and makes debugging failed tests harder. Rely on pytest cleanup, or only remove subpaths you created if absolutely necessary.
| shutil.rmtree(tmp_path) |
| # Create an initial commit so HEAD exists | ||
| (repo / "README.md").write_text("# Test") | ||
| subprocess.run(["git", "-C", str(repo), "add", "."], capture_output=True, check=True) | ||
| subprocess.run(["git", "-C", str(repo), "commit", "-m", "init"], capture_output=True, check=True) | ||
|
|
There was a problem hiding this comment.
This git commit can fail on developer machines if global git config enforces commit signing (e.g., commit.gpgsign=true). To make the test hermetic, disable signing for this invocation (or set a local repo config) so the fixture doesn’t depend on user/global git settings.
- Fix misleading docstring on git_repo fixture - Remove manual shutil.rmtree(tmp_path); pytest owns tmp_path lifecycle - Disable GPG signing in test git repo for hermetic execution Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Addressed all 3 Copilot suggestions in 9132749:
All 5 tests passing. 🤖 Generated with Claude Code |
Summary
Fixes
create-new-feature.sh --jsonfailing with:Root cause:
git fetch --all --prune 2>/dev/nullonly silences stderr. Whengit fetchwrites status text (e.g.,Fetching origin...) to stdout, it gets captured by command substitution incheck_existing_branches(), contaminatingBRANCH_NUMBERwith non-numeric text. The subsequent$((10#$BRANCH_NUMBER))then fails.Fix: Redirect both stdout and stderr to
/dev/null:Closes #1592
Test plan
git fetchtests/test_create_new_feature.py:test_json_output_is_valid— JSON output has correct keystest_feature_num_is_numeric— FEATURE_NUM is always zero-padded numerictest_script_does_not_leak_git_fetch_stdout— no fetch noise in stdouttest_script_does_not_leak_git_fetch_to_json— repeated runs produce clean JSONtest_git_fetch_redirect_pattern_in_script— regression guard for the redirect pattern (no git required)pytest.mark.skipiffor environments without gitAI Assistance Disclosure
This PR was drafted with assistance from Claude Code (Anthropic). The root cause analysis, fix, and tests were validated manually by the author.
🤖 Generated with Claude Code