Repository navigation
Fix hardcoded main branch in gh aw trial --host-repo
#43405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
8aee340
e6ed69e
980047c
bedf6c5
18342aa
d9331c0
c586c11
b37a1d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -522,16 +522,22 @@ func commitAndPushWorkflow(tempDir, workflowName string, verbose bool) error { | |
| return fmt.Errorf("failed to commit changes: %w (output: %s)", err, string(output)) | ||
| } | ||
|
|
||
| branch, err := getCurrentBranchIn(tempDir) | ||
| if err != nil { | ||
| trialRepoLog.Printf("Failed to detect branch in %s: %v, falling back to main", tempDir, err) | ||
| branch = "main" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same silent 💡 Details and suggested fixWhen Because branch, err := getCurrentBranchIn(tempDir)
if err != nil {
return fmt.Errorf("failed to detect branch for push in %s: %w", tempDir, err)
} |
||
| } | ||
| if verbose { | ||
| fmt.Fprintln(os.Stderr, console.FormatInfoMessage("Pulling latest changes from main branch")) | ||
| fmt.Fprintln(os.Stderr, console.FormatInfoMessage("Pulling latest changes from "+branch+" branch")) | ||
| } | ||
| cmd = exec.Command("git", "pull", "origin", "main") | ||
| cmd = exec.Command("git", "pull", "origin", branch) | ||
| cmd.Dir = tempDir | ||
| if output, err := cmd.CombinedOutput(); err != nil { | ||
| return fmt.Errorf("failed to pull latest changes: %w (output: %s)", err, string(output)) | ||
| } | ||
|
Comment on lines
+531
to
537
|
||
|
|
||
| // Push to main | ||
| cmd = exec.Command("git", "push", "origin", "main") | ||
| // Push to current branch | ||
| cmd = exec.Command("git", "push", "origin", branch) | ||
| cmd.Dir = tempDir | ||
| if output, err := cmd.CombinedOutput(); err != nil { | ||
| return fmt.Errorf("failed to push changes: %w (output: %s)", err, string(output)) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,10 @@ | |
|
|
||
| package cli | ||
|
|
||
| import "testing" | ||
| import ( | ||
| "os/exec" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestTrialRepositoryURLHelpers(t *testing.T) { | ||
| tests := []struct { | ||
|
|
@@ -61,3 +64,67 @@ func TestTrialRepositoryURLHelpers(t *testing.T) { | |
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestGetCurrentBranchIn(t *testing.T) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Consider moving this test there to keep all @copilot please address this. |
||
| // initRepo creates a minimal git repo in dir with the given branch name. | ||
| initRepo := func(t *testing.T, dir, branch string) { | ||
| t.Helper() | ||
| run := func(args ...string) { | ||
| t.Helper() | ||
| cmd := exec.Command(args[0], args[1:]...) | ||
| cmd.Dir = dir | ||
| if out, err := cmd.CombinedOutput(); err != nil { | ||
| t.Fatalf("command %v failed: %v (output: %s)", args, err, out) | ||
| } | ||
| } | ||
| run("git", "init") | ||
| run("git", "config", "user.email", "test@example.com") | ||
| run("git", "config", "user.name", "Test") | ||
| run("git", "symbolic-ref", "HEAD", "refs/heads/"+branch) | ||
| run("git", "commit", "--allow-empty", "-m", "init") | ||
| } | ||
|
|
||
| t.Run("returns main for a repo using main", func(t *testing.T) { | ||
| dir := t.TempDir() | ||
| initRepo(t, dir, "main") | ||
| got, err := getCurrentBranchIn(dir) | ||
| if err != nil { | ||
| t.Fatalf("getCurrentBranchIn() unexpected error: %v", err) | ||
| } | ||
| if got != "main" { | ||
| t.Fatalf("getCurrentBranchIn() = %q, want %q", got, "main") | ||
| } | ||
| }) | ||
|
|
||
| t.Run("returns master for a repo using master", func(t *testing.T) { | ||
| dir := t.TempDir() | ||
| initRepo(t, dir, "master") | ||
| got, err := getCurrentBranchIn(dir) | ||
| if err != nil { | ||
| t.Fatalf("getCurrentBranchIn() unexpected error: %v", err) | ||
| } | ||
| if got != "master" { | ||
| t.Fatalf("getCurrentBranchIn() = %q, want %q", got, "master") | ||
| } | ||
| }) | ||
|
|
||
| t.Run("returns custom branch name", func(t *testing.T) { | ||
| dir := t.TempDir() | ||
| initRepo(t, dir, "trunk") | ||
| got, err := getCurrentBranchIn(dir) | ||
| if err != nil { | ||
| t.Fatalf("getCurrentBranchIn() unexpected error: %v", err) | ||
| } | ||
| if got != "trunk" { | ||
| t.Fatalf("getCurrentBranchIn() = %q, want %q", got, "trunk") | ||
| } | ||
| }) | ||
|
|
||
| t.Run("returns error for non-git directory", func(t *testing.T) { | ||
| dir := t.TempDir() | ||
| _, err := getCurrentBranchIn(dir) | ||
| if err == nil { | ||
| t.Fatal("getCurrentBranchIn() expected an error for non-git directory, got nil") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test covers non-git-directory error but not detached HEAD, which is the primary trigger for the 💡 Suggested additional test case
This matters because both t.Run("returns error for detached HEAD", func(t *testing.T) {
dir := t.TempDir()
initRepo(t, dir, "main")
// detach HEAD by checking out the commit SHA directly
run := func(args ...string) {
t.Helper()
cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = dir
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("command %v failed: %v (output: %s)", args, err, out)
}
}
// Get current HEAD SHA and detach
sha, _ := exec.Command("git", "-C", dir, "rev-parse", "HEAD").Output()
run("git", "checkout", strings.TrimSpace(string(sha)))
_, err := getCurrentBranchIn(dir)
if err == nil {
t.Fatal("getCurrentBranchIn() expected an error for detached HEAD, got nil")
}
}) |
||
| } | ||
| }) | ||
|
Comment on lines
+112
to
+130
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Silent
"main"fallback reintroduces the exact bug this PR is fixing when branch detection fails.💡 Details and suggested fix
If
getCurrentBranchInreturns an error — including the detached HEAD path wheregit branch --show-currentexits 0 with empty output — execution silently falls through tobranch = "main". The error is only logged at debug level; the function then proceeds togit push origin main, silently pushing to the wrong branch in non-main-default repos.The
tempDiris a fresh clone, making detached HEAD uncommon in practice, but any transient git failure (filesystem permissions, corrupt index) will also trigger this path and produce a misleadingSUCCESSlog for the user.Suggested approach: propagate the error rather than swallowing it:
If a graceful fallback is truly desired, at minimum surface it to the user via
console.FormatWarningMessagerather than a silent debug log, so operators know the wrong branch may be in use.