Add commit count to output (#440)

This commit is contained in:
gabrie30 2024-07-18 08:37:58 -07:00 committed by GitHub
parent b598e0ded3
commit 7167cd4207
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 72 additions and 6 deletions

View File

@ -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,9 +1047,14 @@ 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
}
if newCommits > 0 {
colorlog.PrintSuccess(fmt.Sprintf("New clones: %v, existing resources pulled: %v, total new commits: %v", cloneCount, pulledCount, newCommits))
return
}

View File

@ -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")

View File

@ -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
}

View File

@ -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 {