Skip to content

get_current_branch.cjs leaks stderr when not in a git repository #19475

@samuelkahessay

Description

@samuelkahessay

What happens

get_current_branch.cjs runs git rev-parse --abbrev-ref HEAD via execSync without suppressing stderr. When called in a context where the working directory isn't a git repository (or the .git directory is inaccessible), git prints fatal: not a git repository (or any of the parent directories): .git to stderr before the function catches the error and falls back to GITHUB_REF_NAME.

The fallback works correctly — the function returns the right branch name. But the stderr output leaks into the Actions log, adding confusing noise to failure reports.

This function is called in the failure reporting path (handle_agent_failure.cjs:742), so the stderr noise appears specifically in runs that are already failing — the worst time to add confusing log output.

What should happen

The execSync call should suppress stderr so that the fallback is silent. The function already has a try/catch; the issue is purely that stderr is inherited from the parent process rather than piped.

Where in the code

All references are to main at 99b2107.

The leaky call:

  • get_current_branch.cjs:18-23:
    const branch = execSync("git rev-parse --abbrev-ref HEAD", {
      encoding: "utf8",
      cwd: cwd,
    }).trim();
    No stdio option — defaults to "pipe" for stdout but inherits stderr from the parent process.

Call site in failure path:

  • handle_agent_failure.cjs:18const { getCurrentBranch } = require("./get_current_branch.cjs");
  • handle_agent_failure.cjs:742const currentBranch = getCurrentBranch();

Evidence

Local reproduction:

  • Set GITHUB_WORKSPACE=/tmp and GITHUB_REF_NAME=main
  • Ran the function — stderr printed fatal: not a git repository (or any of the parent directories): .git
  • Return value was correctly main (from GITHUB_REF_NAME fallback)

Source-level verification (2026-03-03):

  • Confirmed execSync at line 19 has no stdio configuration
  • Node.js execSync defaults: stdout is "pipe" (captured), stderr is inherited (printed to console)
  • The try/catch at lines 18/24 catches the thrown error but stderr has already been written

Proposed fix

Add stdio: ["pipe", "pipe", "pipe"] (or stdio: "pipe") to the execSync options:

const branch = execSync("git rev-parse --abbrev-ref HEAD", {
  encoding: "utf8",
  cwd: cwd,
  stdio: ["pipe", "pipe", "pipe"],
}).trim();

This suppresses stderr while still capturing stdout. The existing try/catch handles the error path.

Impact

Frequency: Every failure-path invocation where GITHUB_WORKSPACE doesn't point to a valid git repository.
Cost: Low — purely cosmetic noise, but it appears in failure logs where clarity matters most. One-line fix.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions