Skip to content

fix: silence git fetch stdout to prevent branch number parse error#1696

Open
fsilvaortiz wants to merge 3 commits intogithub:mainfrom
fsilvaortiz:fix/silence-git-fetch-stdout-in-create-feature
Open

fix: silence git fetch stdout to prevent branch number parse error#1696
fsilvaortiz wants to merge 3 commits intogithub:mainfrom
fsilvaortiz:fix/silence-git-fetch-stdout-in-create-feature

Conversation

@fsilvaortiz
Copy link
Contributor

@fsilvaortiz fsilvaortiz commented Feb 25, 2026

Summary

Fixes create-new-feature.sh --json failing with:

line 250: 10#Fetching: value too great for base (error token is "10#Fetching")

Root cause: git fetch --all --prune 2>/dev/null only silences stderr. When git fetch writes status text (e.g., Fetching origin...) to stdout, it gets captured by command substitution in check_existing_branches(), contaminating BRANCH_NUMBER with non-numeric text. The subsequent $((10#$BRANCH_NUMBER)) then fails.

Fix: Redirect both stdout and stderr to /dev/null:

- git fetch --all --prune 2>/dev/null || true
+ git fetch --all --prune >/dev/null 2>&1 || true

Closes #1592

Test plan

  • Verified the fix silences both stdout and stderr from git fetch
  • All tests pass (100/100 — 95 existing + 5 new)
  • Added 5 new tests in tests/test_create_new_feature.py:
    • test_json_output_is_valid — JSON output has correct keys
    • test_feature_num_is_numeric — FEATURE_NUM is always zero-padded numeric
    • test_script_does_not_leak_git_fetch_stdout — no fetch noise in stdout
    • test_script_does_not_leak_git_fetch_to_json — repeated runs produce clean JSON
    • test_git_fetch_redirect_pattern_in_script — regression guard for the redirect pattern (no git required)
  • Tests that require git are marked with pytest.mark.skipif for environments without git

AI 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

`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>
@fsilvaortiz fsilvaortiz requested a review from mnriem as a code owner February 25, 2026 23:03
…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>
@fsilvaortiz fsilvaortiz force-pushed the fix/silence-git-fetch-stdout-in-create-feature branch from db3da6f to a45e9f0 Compare February 25, 2026 23:08
@mnriem mnriem requested a review from Copilot February 27, 2026 20:51
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 --prune stdout + stderr to /dev/null inside check_existing_branches().
  • Add pytest coverage for --json output 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_stdout claims 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 asserting len(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 from git 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.

Comment on lines 25 to 33
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)
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copilot uses AI. Check for mistakes.
(repo / ".specify" / "templates").mkdir()

yield repo
shutil.rmtree(tmp_path)
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
shutil.rmtree(tmp_path)

Copilot uses AI. Check for mistakes.
Comment on lines 35 to 39
# 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)

Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
- 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>
@fsilvaortiz
Copy link
Contributor Author

Addressed all 3 Copilot suggestions in 9132749:

  1. Misleading docstring — Updated fixture docstring to accurately describe what it does
  2. Manual shutil.rmtree(tmp_path) — Removed; pytest owns tmp_path lifecycle
  3. GPG signing — Added git config commit.gpgsign false in the test repo for hermetic execution

All 5 tests passing.

🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

create-new-feature.sh --json can fail with '10#Fetching: value too great for base'

2 participants