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 17, 2021
1 parent a2115d2 commit b05a3bc
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions internal/vcs/create-git-repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,30 @@ type initialCommands struct {
args []string
}

// getInitDefaultBranch return init.defaultBranch value in git config.
// If init.defaultBranch isn't set, getInitDefaultBranch return 'master'.
func getInitDefaultBranch() string {
var initDefaultBranch string

cmd := exec.Command("git", "config", "--get", "init.defaultBranch")

output, err := cmd.CombinedOutput()

if err != nil {
initDefaultBranch = "master"
} else {
initDefaultBranch = string(output)
}

return initDefaultBranch
}

// 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("[email protected]:%s/%s.git", ownerName, repositoryName)

initDefaultBranch := getInitDefaultBranch()

commands := []initialCommands{
{
description: "git init",
Expand All @@ -179,9 +200,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 b05a3bc

Please sign in to comment.