Skip to content

Commit 17352fb

Browse files
author
Chris McDonnell
committed
Add fallback to detecting main branches for fresh repositories
All other attempts to determine the main branch fail on a fresh repository right after `git init` because there are no actual commits that these branches point to. This does return `master` instead a full name like `refs/heads/master` which I don't think is an issue, but might be?
1 parent d1d2bb2 commit 17352fb

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

pkg/commands/git_commands/main_branches.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@ func (self *MainBranches) determineMainBranches(configuredMainBranches []string)
113113
NewGitCmd("rev-parse").Arg("--verify", "--quiet", ref).ToArgv(),
114114
).DontLog().Run(); err == nil {
115115
existingBranches[i] = ref
116+
return
117+
}
118+
119+
// If this is a brand new repository with no commits, then the current branch would be the default
120+
if ref, err := self.cmd.New(
121+
NewGitCmd("symbolic-ref").Arg("--short", "HEAD").ToArgv(),
122+
).DontLog().RunWithOutput(); err == nil {
123+
if strings.TrimSpace(ref) == branchName {
124+
existingBranches[i] = branchName
125+
return
126+
}
116127
}
117128
})
118129
}

0 commit comments

Comments
 (0)