Skip to content

Commit

Permalink
enhancement: git push command use branch name from init.defaultBranch…
Browse files Browse the repository at this point in the history
… in git config
  • Loading branch information
gmlwo530 committed Oct 20, 2021
1 parent a2115d2 commit 9a1fc2f
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions internal/vcs/create-git-repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,26 @@ type initialCommands struct {
args []string
}

// getInitDefaultBranch return init.defaultBranch value in git config.
// If init.defaultBranch isn't set, getInitDefaultBranch return 'main'.
func getInitDefaultBranch() string {
cmd := exec.Command("git", "config", "--get", "init.defaultBranch")

output, err := cmd.CombinedOutput()

if err != nil {
return "main"
}

return string(output)
}

// doInitialCommit runs the git commands that initialize and do the first commit to a repository.
func doInitialCommit(ownerName string, repositoryName string) error {
remoteOrigin := fmt.Sprintf("git@github.com:%s/%s.git", ownerName, repositoryName)

initDefaultBranch := getInitDefaultBranch()

commands := []initialCommands{
{
description: "git init",
Expand All @@ -179,9 +196,9 @@ func doInitialCommit(ownerName string, repositoryName string) error {
args: []string{"remote", "add", "origin", remoteOrigin},
},
{
description: "git push -u origin master",
description: fmt.Sprintf("git push -u origin %s", initDefaultBranch),
command: "git",
args: []string{"push", "-u", "origin", "master"},
args: []string{"push", "-u", "origin", initDefaultBranch},
},
}

Expand Down

0 comments on commit 9a1fc2f

Please sign in to comment.