From 9a1fc2fbae03818fc8e63ed410165bc7a35fa7f8 Mon Sep 17 00:00:00 2001 From: CHOI HEE JAE Date: Mon, 18 Oct 2021 02:51:32 +0900 Subject: [PATCH] enhancement: git push command use branch name from init.defaultBranch in git config --- internal/vcs/create-git-repos.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/internal/vcs/create-git-repos.go b/internal/vcs/create-git-repos.go index 53e549c54..ff09bb52a 100644 --- a/internal/vcs/create-git-repos.go +++ b/internal/vcs/create-git-repos.go @@ -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", @@ -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}, }, }