Skip to content

Commit 9a1fc2f

Browse files
committed
enhancement: git push command use branch name from init.defaultBranch in git config
1 parent a2115d2 commit 9a1fc2f

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

internal/vcs/create-git-repos.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,26 @@ type initialCommands struct {
154154
args []string
155155
}
156156

157+
// getInitDefaultBranch return init.defaultBranch value in git config.
158+
// If init.defaultBranch isn't set, getInitDefaultBranch return 'main'.
159+
func getInitDefaultBranch() string {
160+
cmd := exec.Command("git", "config", "--get", "init.defaultBranch")
161+
162+
output, err := cmd.CombinedOutput()
163+
164+
if err != nil {
165+
return "main"
166+
}
167+
168+
return string(output)
169+
}
170+
157171
// doInitialCommit runs the git commands that initialize and do the first commit to a repository.
158172
func doInitialCommit(ownerName string, repositoryName string) error {
159173
remoteOrigin := fmt.Sprintf("git@github.com:%s/%s.git", ownerName, repositoryName)
174+
175+
initDefaultBranch := getInitDefaultBranch()
176+
160177
commands := []initialCommands{
161178
{
162179
description: "git init",
@@ -179,9 +196,9 @@ func doInitialCommit(ownerName string, repositoryName string) error {
179196
args: []string{"remote", "add", "origin", remoteOrigin},
180197
},
181198
{
182-
description: "git push -u origin master",
199+
description: fmt.Sprintf("git push -u origin %s", initDefaultBranch),
183200
command: "git",
184-
args: []string{"push", "-u", "origin", "master"},
201+
args: []string{"push", "-u", "origin", initDefaultBranch},
185202
},
186203
}
187204

0 commit comments

Comments
 (0)