Skip to content

Commit 86eb576

Browse files
committed
Add commit count to output
1 parent b598e0d commit 86eb576

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

cmd/clone.go

+29-7
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) {
633633

634634
limit := limiter.NewConcurrencyLimiter(l)
635635

636-
var cloneCount, pulledCount, updateRemoteCount int
636+
var cloneCount, pulledCount, updateRemoteCount, newCommits int
637637

638638
// maps in go are not safe for concurrent use
639639
var mutex = &sync.RWMutex{}
@@ -713,7 +713,8 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) {
713713
}
714714

715715
action := "cloning"
716-
if repoExistsLocally(repo) {
716+
repoWillBePulled := repoExistsLocally(repo)
717+
if repoWillBePulled {
717718
// prevents git from asking for user for credentials, needs to be unset so creds aren't stored
718719
err := git.SetOriginWithCredentials(repo)
719720
if err != nil {
@@ -779,6 +780,14 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) {
779780
}
780781
}
781782

783+
count, _ := git.RepoCommitCount(repo)
784+
if err != nil {
785+
e := fmt.Sprintf("Problem trying to get pre pull commit count for on repo: %s", repo.URL)
786+
cloneInfos = append(cloneInfos, e)
787+
}
788+
789+
repo.Commits.CountPrePull = count
790+
782791
err = git.Clean(repo)
783792

784793
if err != nil {
@@ -803,6 +812,15 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) {
803812
return
804813
}
805814

815+
count, _ = git.RepoCommitCount(repo)
816+
if err != nil {
817+
e := fmt.Sprintf("Problem trying to get post pull commit count for on repo: %s", repo.URL)
818+
cloneInfos = append(cloneInfos, e)
819+
}
820+
821+
repo.Commits.CountPostPull = count
822+
repo.Commits.CountDiff = (repo.Commits.CountPostPull - repo.Commits.CountPrePull)
823+
newCommits = (newCommits + repo.Commits.CountDiff)
806824
action = "pulling"
807825
pulledCount++
808826
}
@@ -864,15 +882,19 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) {
864882
}
865883
}
866884

867-
colorlog.PrintSuccess("Success " + action + " " + repo.URL + " -> branch: " + repo.CloneBranch)
885+
if repoWillBePulled && repo.Commits.CountDiff > 0 {
886+
colorlog.PrintSuccess(fmt.Sprintf("Success %s %s, branch: %s, new commits: %d", action, repo.URL, repo.CloneBranch, repo.Commits.CountDiff))
887+
} else {
888+
colorlog.PrintSuccess(fmt.Sprintf("Success %s %s, branch: %s", action, repo.URL, repo.CloneBranch))
889+
}
868890
})
869891

870892
}
871893

872894
limit.WaitAndClose()
873895

874896
printRemainingMessages()
875-
printCloneStatsMessage(cloneCount, pulledCount, updateRemoteCount)
897+
printCloneStatsMessage(cloneCount, pulledCount, updateRemoteCount, newCommits)
876898

877899
if hasCollisions {
878900
fmt.Println("")
@@ -1025,13 +1047,13 @@ func pruneRepos(cloneTargets []scm.Repo) {
10251047
}
10261048
}
10271049

1028-
func printCloneStatsMessage(cloneCount, pulledCount, updateRemoteCount int) {
1050+
func printCloneStatsMessage(cloneCount, pulledCount, updateRemoteCount, newCommits int) {
10291051
if updateRemoteCount > 0 {
1030-
colorlog.PrintSuccess(fmt.Sprintf("New clones: %v, existing resources pulled: %v, remotes updated: %v", cloneCount, pulledCount, updateRemoteCount))
1052+
colorlog.PrintSuccess(fmt.Sprintf("New clones: %v, existing resources pulled: %v, total new commits: %v, remotes updated: %v", cloneCount, pulledCount, newCommits, updateRemoteCount))
10311053
return
10321054
}
10331055

1034-
colorlog.PrintSuccess(fmt.Sprintf("New clones: %v, existing resources pulled: %v", cloneCount, pulledCount))
1056+
colorlog.PrintSuccess(fmt.Sprintf("New clones: %v, existing resources pulled: %v, total new commits: %v", cloneCount, pulledCount, newCommits))
10351057
}
10361058

10371059
func interactiveYesNoPrompt(prompt string) bool {

git/git.go

+28
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"os"
66
"os/exec"
7+
"strconv"
8+
"strings"
79

810
"github.com/davecgh/go-spew/spew"
911
"github.com/gabrie30/ghorg/scm"
@@ -20,6 +22,7 @@ type Gitter interface {
2022
UpdateRemote(scm.Repo) error
2123
FetchAll(scm.Repo) error
2224
FetchCloneBranch(scm.Repo) error
25+
RepoCommitCount(scm.Repo) (int, error)
2326
}
2427

2528
type GitClient struct{}
@@ -192,3 +195,28 @@ func (g GitClient) FetchCloneBranch(repo scm.Repo) error {
192195
}
193196
return cmd.Run()
194197
}
198+
199+
func (g GitClient) RepoCommitCount(repo scm.Repo) (int, error) {
200+
args := []string{"rev-list", "--count", repo.CloneBranch}
201+
cmd := exec.Command("git", args...)
202+
cmd.Dir = repo.HostPath
203+
204+
if os.Getenv("GHORG_DEBUG") != "" {
205+
err := printDebugCmd(cmd, repo)
206+
if err != nil {
207+
return 0, err
208+
}
209+
}
210+
211+
output, err := cmd.Output()
212+
if err != nil {
213+
return 0, err
214+
}
215+
216+
count, err := strconv.Atoi(strings.TrimSpace(string(output)))
217+
if err != nil {
218+
return 0, err
219+
}
220+
221+
return count, nil
222+
}

scm/structs.go

+7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ type Repo struct {
2424
IsGitLabRootLevelSnippet bool
2525
// GitLabSnippetInfo provides additional information when the thing we are cloning is a gitlab snippet
2626
GitLabSnippetInfo GitLabSnippet
27+
Commits RepoCommits
28+
}
29+
30+
type RepoCommits struct {
31+
CountPrePull int
32+
CountPostPull int
33+
CountDiff int
2734
}
2835

2936
type GitLabSnippet struct {

0 commit comments

Comments
 (0)