From 86eb576b6fc49fddc78351e8abb79b8590905b0e Mon Sep 17 00:00:00 2001 From: Jay Gabriels Date: Wed, 17 Jul 2024 19:26:59 -0700 Subject: [PATCH 1/3] Add commit count to output --- cmd/clone.go | 36 +++++++++++++++++++++++++++++------- git/git.go | 28 ++++++++++++++++++++++++++++ scm/structs.go | 7 +++++++ 3 files changed, 64 insertions(+), 7 deletions(-) diff --git a/cmd/clone.go b/cmd/clone.go index 3af9d1ff33..c36ad3fdeb 100644 --- a/cmd/clone.go +++ b/cmd/clone.go @@ -633,7 +633,7 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) { limit := limiter.NewConcurrencyLimiter(l) - var cloneCount, pulledCount, updateRemoteCount int + var cloneCount, pulledCount, updateRemoteCount, newCommits int // maps in go are not safe for concurrent use var mutex = &sync.RWMutex{} @@ -713,7 +713,8 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) { } action := "cloning" - if repoExistsLocally(repo) { + repoWillBePulled := repoExistsLocally(repo) + if repoWillBePulled { // prevents git from asking for user for credentials, needs to be unset so creds aren't stored err := git.SetOriginWithCredentials(repo) if err != nil { @@ -779,6 +780,14 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) { } } + count, _ := git.RepoCommitCount(repo) + if err != nil { + e := fmt.Sprintf("Problem trying to get pre pull commit count for on repo: %s", repo.URL) + cloneInfos = append(cloneInfos, e) + } + + repo.Commits.CountPrePull = count + err = git.Clean(repo) if err != nil { @@ -803,6 +812,15 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) { return } + count, _ = git.RepoCommitCount(repo) + if err != nil { + e := fmt.Sprintf("Problem trying to get post pull commit count for on repo: %s", repo.URL) + cloneInfos = append(cloneInfos, e) + } + + repo.Commits.CountPostPull = count + repo.Commits.CountDiff = (repo.Commits.CountPostPull - repo.Commits.CountPrePull) + newCommits = (newCommits + repo.Commits.CountDiff) action = "pulling" pulledCount++ } @@ -864,7 +882,11 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) { } } - colorlog.PrintSuccess("Success " + action + " " + repo.URL + " -> branch: " + repo.CloneBranch) + if repoWillBePulled && repo.Commits.CountDiff > 0 { + colorlog.PrintSuccess(fmt.Sprintf("Success %s %s, branch: %s, new commits: %d", action, repo.URL, repo.CloneBranch, repo.Commits.CountDiff)) + } else { + colorlog.PrintSuccess(fmt.Sprintf("Success %s %s, branch: %s", action, repo.URL, repo.CloneBranch)) + } }) } @@ -872,7 +894,7 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) { limit.WaitAndClose() printRemainingMessages() - printCloneStatsMessage(cloneCount, pulledCount, updateRemoteCount) + printCloneStatsMessage(cloneCount, pulledCount, updateRemoteCount, newCommits) if hasCollisions { fmt.Println("") @@ -1025,13 +1047,13 @@ func pruneRepos(cloneTargets []scm.Repo) { } } -func printCloneStatsMessage(cloneCount, pulledCount, updateRemoteCount int) { +func printCloneStatsMessage(cloneCount, pulledCount, updateRemoteCount, newCommits int) { if updateRemoteCount > 0 { - colorlog.PrintSuccess(fmt.Sprintf("New clones: %v, existing resources pulled: %v, remotes updated: %v", cloneCount, pulledCount, updateRemoteCount)) + colorlog.PrintSuccess(fmt.Sprintf("New clones: %v, existing resources pulled: %v, total new commits: %v, remotes updated: %v", cloneCount, pulledCount, newCommits, updateRemoteCount)) return } - colorlog.PrintSuccess(fmt.Sprintf("New clones: %v, existing resources pulled: %v", cloneCount, pulledCount)) + colorlog.PrintSuccess(fmt.Sprintf("New clones: %v, existing resources pulled: %v, total new commits: %v", cloneCount, pulledCount, newCommits)) } func interactiveYesNoPrompt(prompt string) bool { diff --git a/git/git.go b/git/git.go index d40bd9c57d..496274cf0d 100644 --- a/git/git.go +++ b/git/git.go @@ -4,6 +4,8 @@ import ( "fmt" "os" "os/exec" + "strconv" + "strings" "github.com/davecgh/go-spew/spew" "github.com/gabrie30/ghorg/scm" @@ -20,6 +22,7 @@ type Gitter interface { UpdateRemote(scm.Repo) error FetchAll(scm.Repo) error FetchCloneBranch(scm.Repo) error + RepoCommitCount(scm.Repo) (int, error) } type GitClient struct{} @@ -192,3 +195,28 @@ func (g GitClient) FetchCloneBranch(repo scm.Repo) error { } return cmd.Run() } + +func (g GitClient) RepoCommitCount(repo scm.Repo) (int, error) { + args := []string{"rev-list", "--count", repo.CloneBranch} + cmd := exec.Command("git", args...) + cmd.Dir = repo.HostPath + + if os.Getenv("GHORG_DEBUG") != "" { + err := printDebugCmd(cmd, repo) + if err != nil { + return 0, err + } + } + + output, err := cmd.Output() + if err != nil { + return 0, err + } + + count, err := strconv.Atoi(strings.TrimSpace(string(output))) + if err != nil { + return 0, err + } + + return count, nil +} diff --git a/scm/structs.go b/scm/structs.go index 8aa32dec7a..31519012e9 100644 --- a/scm/structs.go +++ b/scm/structs.go @@ -24,6 +24,13 @@ type Repo struct { IsGitLabRootLevelSnippet bool // GitLabSnippetInfo provides additional information when the thing we are cloning is a gitlab snippet GitLabSnippetInfo GitLabSnippet + Commits RepoCommits +} + +type RepoCommits struct { + CountPrePull int + CountPostPull int + CountDiff int } type GitLabSnippet struct { From 9cfb3bc0e6bae438d38ab3ca359ef42d40f6c7aa Mon Sep 17 00:00:00 2001 From: Jay Gabriels Date: Wed, 17 Jul 2024 20:08:35 -0700 Subject: [PATCH 2/3] Update clone_test.go --- cmd/clone_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmd/clone_test.go b/cmd/clone_test.go index 929c3f8bdc..95161e5c67 100644 --- a/cmd/clone_test.go +++ b/cmd/clone_test.go @@ -101,6 +101,10 @@ func (g MockGitClient) FetchCloneBranch(repo scm.Repo) error { return nil } +func (g MockGitClient) RepoCommitCount(repo scm.Repo) (int, error) { + return 0, nil +} + func TestInitialClone(t *testing.T) { defer UnsetEnv("GHORG_")() dir, err := os.MkdirTemp("", "ghorg_test_initial") From 72f1b6a502138468ce7e81eef89346565b1bd1b2 Mon Sep 17 00:00:00 2001 From: Jay Gabriels Date: Thu, 18 Jul 2024 05:24:48 -0700 Subject: [PATCH 3/3] Update clone.go --- cmd/clone.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cmd/clone.go b/cmd/clone.go index c36ad3fdeb..e595512935 100644 --- a/cmd/clone.go +++ b/cmd/clone.go @@ -1053,7 +1053,12 @@ func printCloneStatsMessage(cloneCount, pulledCount, updateRemoteCount, newCommi return } - colorlog.PrintSuccess(fmt.Sprintf("New clones: %v, existing resources pulled: %v, total new commits: %v", cloneCount, pulledCount, newCommits)) + if newCommits > 0 { + colorlog.PrintSuccess(fmt.Sprintf("New clones: %v, existing resources pulled: %v, total new commits: %v", cloneCount, pulledCount, newCommits)) + return + } + + colorlog.PrintSuccess(fmt.Sprintf("New clones: %v, existing resources pulled: %v", cloneCount, pulledCount)) } func interactiveYesNoPrompt(prompt string) bool {