fix: suppress stderr in get_current_branch.cjs when outside a git repo#19499
fix: suppress stderr in get_current_branch.cjs when outside a git repo#19499
Conversation
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Suppresses noisy git stderr output from getCurrentBranch() when invoked outside a git repository, improving clarity in failure-path logs while keeping the existing fallback behavior.
Changes:
- Pipe
stderrfor thegit rev-parse --abbrev-ref HEADexecSynccall to preventfatal: not a git repositoryfrom leaking into logs.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
| const branch = execSync("git rev-parse --abbrev-ref HEAD", { | ||
| encoding: "utf8", | ||
| cwd: cwd, | ||
| stdio: ["pipe", "pipe", "pipe"], | ||
| }).trim(); |
There was a problem hiding this comment.
Add a regression test to ensure getCurrentBranch() does not leak the underlying git failure message to stderr when cwd is not a git repo (the behavior this change is meant to fix). The repo already has get_current_branch.test.cjs, so this can be covered by spawning a node process with GITHUB_WORKSPACE set to a non-git directory and asserting the child process stderr does not contain fatal: not a git repository (while the function still returns the env fallback or throws as expected).
| const branch = execSync("git rev-parse --abbrev-ref HEAD", { | ||
| encoding: "utf8", | ||
| cwd: cwd, | ||
| stdio: ["pipe", "pipe", "pipe"], |
There was a problem hiding this comment.
For consistency with other child_process sync calls in this repo, consider using stdio: "pipe" instead of the explicit ['pipe','pipe','pipe'] array (e.g. actions/setup/js/merge_remote_agent_github_folder.cjs:191 and actions/setup/js/push_repo_memory.cjs:130 use the string form). This is functionally equivalent here and a bit easier to scan.
| stdio: ["pipe", "pipe", "pipe"], | |
| stdio: "pipe", |
get_current_branch.cjsleaksfatal: not a git repositoryto stderr on every invocation from a non-git working directory, becauseexecSyncinherits stderr by default. This noise appears specifically in failure-path logs (called fromhandle_agent_failure.cjs), where clarity matters most.Change
Added
stdio: ["pipe", "pipe", "pipe"]to theexecSynccall so stderr is captured rather than inherited:The existing try/catch already handles the error and falls back to
GITHUB_HEAD_REF/GITHUB_REF_NAMEcorrectly — this makes that fallback silent.Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.