diff --git a/CHANGELOG.md b/CHANGELOG.md index 53fe3c1..fed552a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) ### Added ### Changed - error messages for ls command +- GHORG_BRANCH if not set, will first look for the repos default branch, if no default branch is found on repo will fall back to using master as default ### Deprecated ### Removed ### Fixed diff --git a/cmd/clone.go b/cmd/clone.go index 79510a0..a39dec7 100644 --- a/cmd/clone.go +++ b/cmd/clone.go @@ -356,7 +356,7 @@ func CloneAllRepos() { limit := limiter.NewConcurrencyLimiter(l) for _, target := range cloneTargets { appName := getAppNameFromURL(target.URL) - branch := os.Getenv("GHORG_BRANCH") + branch := target.CloneBranch repo := target limit.Execute(func() { @@ -452,7 +452,7 @@ func CloneAllRepos() { } } - colorlog.PrintSuccess("Success " + repo.URL) + colorlog.PrintSuccess("Success cloning repo: " + repo.URL + " -> branch: " + branch) }) } @@ -484,10 +484,13 @@ func PrintConfigs() { colorlog.PrintInfo("* SCM : " + os.Getenv("GHORG_SCM_TYPE")) colorlog.PrintInfo("* Type : " + os.Getenv("GHORG_CLONE_TYPE")) colorlog.PrintInfo("* Protocol : " + os.Getenv("GHORG_CLONE_PROTOCOL")) - colorlog.PrintInfo("* Branch : " + os.Getenv("GHORG_BRANCH")) colorlog.PrintInfo("* Location : " + os.Getenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO")) colorlog.PrintInfo("* Concurrency : " + os.Getenv("GHORG_CONCURRENCY")) + if os.Getenv("GHORG_BRANCH") != "" { + colorlog.PrintInfo("* Branch : " + getGhorgBranch()) + } + if os.Getenv("GHORG_SCM_BASE_URL") != "" { colorlog.PrintInfo("* Base URL : " + os.Getenv("GHORG_SCM_BASE_URL")) } @@ -511,6 +514,14 @@ func PrintConfigs() { fmt.Println("") } +func getGhorgBranch() string { + if os.Getenv("GHORG_BRANCH") == "" { + return "default branch" + } + + return os.Getenv("GHORG_BRANCH") +} + func ensureTrailingSlash(path string) string { if string(path[len(path)-1]) == "/" { return path diff --git a/configs/configs.go b/configs/configs.go index 7e1da9c..f586824 100644 --- a/configs/configs.go +++ b/configs/configs.go @@ -121,8 +121,6 @@ func getOrSetDefaults(envVar string) { switch envVar { case "GHORG_ABSOLUTE_PATH_TO_CLONE_TO": os.Setenv(envVar, HomeDir()+"/Desktop/ghorg/") - case "GHORG_BRANCH": - os.Setenv(envVar, "master") case "GHORG_CLONE_PROTOCOL": os.Setenv(envVar, "https") case "GHORG_CLONE_TYPE": diff --git a/configs/configs_test.go b/configs/configs_test.go index a1a4263..345b80f 100644 --- a/configs/configs_test.go +++ b/configs/configs_test.go @@ -9,15 +9,10 @@ import ( func TestDefaultSettings(t *testing.T) { - branch := os.Getenv("GHORG_BRANCH") protocol := os.Getenv("GHORG_CLONE_PROTOCOL") scm := os.Getenv("GHORG_SCM_TYPE") cloneType := os.Getenv("GHORG_CLONE_TYPE") - if branch != "master" { - t.Errorf("Default branch should be master, got: %v", branch) - } - if protocol != "https" { t.Errorf("Default protocol should be https, got: %v", protocol) } diff --git a/sample-conf.yaml b/sample-conf.yaml index f58f57d..064230e 100644 --- a/sample-conf.yaml +++ b/sample-conf.yaml @@ -24,7 +24,6 @@ GHORG_GITLAB_TOKEN: # flag (--preserve-dir) GHORG_PRESERVE_DIRECTORY_STRUCTURE: - # +-+-+-+-+-+ +-+-+-+-+-+-+-+-+ # |G|I|T|E|A| |S|P|E|C|I|F|I|C| # +-+-+-+-+-+ +-+-+-+-+-+-+-+-+ @@ -34,7 +33,6 @@ GHORG_PRESERVE_DIRECTORY_STRUCTURE: # flag (--token, -t) eg: --token=bGVhdmUgYSBjb21tZW50IG9uIGlzc3VlIDY2 GHORG_GITEA_TOKEN: - # +-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ # |B|I|T|B|U|C|K|E|T| |S|P|E|C|I|F|I|C| # +-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ @@ -47,7 +45,6 @@ GHORG_BITBUCKET_APP_PASSWORD: # flag (--bitbucket-username) GHORG_BITBUCKET_USERNAME: - # +-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+-+ # |G|E|N|E|R|A|L| |C|O|N|F|I|G|U|R|A|T|I|O|N| # +-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -73,7 +70,8 @@ GHORG_CLONE_PROTOCOL: GHORG_ABSOLUTE_PATH_TO_CLONE_TO: # Branch ghorg resets and leaves checked out -# default: master +# default: default branch +# NOTE: if no default branch is found on the repo, will fallback to using master # flag (--branch, -b) GHORG_BRANCH: diff --git a/scm/bitbucket.go b/scm/bitbucket.go index 753cd38..04c30b1 100644 --- a/scm/bitbucket.go +++ b/scm/bitbucket.go @@ -78,6 +78,26 @@ func (_ Bitbucket) filter(resp interface{}) (repoData []Repo, err error) { } r := Repo{} + + if os.Getenv("GHORG_BRANCH") == "" { + var defaultBranch string + if clone["mainbranch"] == nil { + defaultBranch = "master" + } else { + defaultBranch = clone["mainbranch"].(map[string]interface{})["name"].(string) + } + + r.CloneBranch = defaultBranch + } else { + r.CloneBranch = os.Getenv("GHORG_BRANCH") + } + + if os.Getenv("GHORG_BRANCH") == "" { + r.CloneBranch = "master" + } else { + r.CloneBranch = os.Getenv("GHORG_BRANCH") + } + if os.Getenv("GHORG_CLONE_PROTOCOL") == "ssh" && linkType == "ssh" { r.URL = link.(string) r.CloneURL = link.(string) diff --git a/scm/gitea.go b/scm/gitea.go index 45a152f..331a381 100644 --- a/scm/gitea.go +++ b/scm/gitea.go @@ -174,6 +174,16 @@ func (c Gitea) filter(rps []*gitea.Repository) (repoData []Repo, err error) { r := Repo{} r.Path = rp.FullName + if os.Getenv("GHORG_BRANCH") == "" { + defaultBranch := rp.DefaultBranch + if defaultBranch == "" { + defaultBranch = "master" + } + r.CloneBranch = defaultBranch + } else { + r.CloneBranch = os.Getenv("GHORG_BRANCH") + } + if os.Getenv("GHORG_CLONE_PROTOCOL") == "https" { cloneURL := rp.CloneURL if rp.Private { diff --git a/scm/github.go b/scm/github.go index 79b1636..345d18b 100644 --- a/scm/github.go +++ b/scm/github.go @@ -161,6 +161,17 @@ func (c Github) filter(allRepos []*github.Repository, envTopics []string) []Repo } r := Repo{} + + if os.Getenv("GHORG_BRANCH") == "" { + defaultBranch := ghRepo.GetDefaultBranch() + if defaultBranch == "" { + defaultBranch = "master" + } + r.CloneBranch = defaultBranch + } else { + r.CloneBranch = os.Getenv("GHORG_BRANCH") + } + if os.Getenv("GHORG_CLONE_PROTOCOL") == "https" { r.CloneURL = c.addTokenToHTTPSCloneURL(*ghRepo.CloneURL, os.Getenv("GHORG_GITHUB_TOKEN")) r.URL = *ghRepo.CloneURL diff --git a/scm/gitlab.go b/scm/gitlab.go index 7f60e0b..c29b4fe 100644 --- a/scm/gitlab.go +++ b/scm/gitlab.go @@ -152,6 +152,16 @@ func (c Gitlab) filter(ps []*gitlab.Project) []Repo { r := Repo{} + if os.Getenv("GHORG_BRANCH") == "" { + defaultBranch := p.DefaultBranch + if defaultBranch == "" { + defaultBranch = "master" + } + r.CloneBranch = defaultBranch + } else { + r.CloneBranch = os.Getenv("GHORG_BRANCH") + } + r.Path = p.PathWithNamespace if os.Getenv("GHORG_CLONE_PROTOCOL") == "https" { r.CloneURL = c.addTokenToHTTPSCloneURL(p.HTTPURLToRepo, os.Getenv("GHORG_GITLAB_TOKEN")) diff --git a/scm/structs.go b/scm/structs.go index f7468bf..23e71ed 100644 --- a/scm/structs.go +++ b/scm/structs.go @@ -2,8 +2,9 @@ package scm // Repo represents an SCM repo type Repo struct { - Name string - Path string - URL string - CloneURL string + Name string + Path string + URL string + CloneURL string + CloneBranch string }