From 204ba12b2c82c55a1c797fe0f8fa948351b5a848 Mon Sep 17 00:00:00 2001 From: Tor Bratsberg Date: Wed, 2 Mar 2022 20:39:39 +0100 Subject: [PATCH] Feat: Show commit count in repo info --- client/main.go | 48 +++++++++++++++++++++++++++++------------------- server/main.go | 17 +++++++++++++---- 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/client/main.go b/client/main.go index fd1bec8..f41353f 100644 --- a/client/main.go +++ b/client/main.go @@ -31,10 +31,11 @@ type ClientConfig struct { } type Repo struct { - Name string - Branches []string - Remotes []string - Tags []map[string]string + Name string + Branches []string + Remotes []string + Tags []map[string]string + CommitCount int } type Parameter struct { @@ -77,33 +78,42 @@ func getConfig() (cfg *ClientConfig) { var gitorConfig ClientConfig = *getConfig() func printRepoInfo(repo Repo) { + res := "" + // Print repo name - fmt.Println(repo.Name) + res += repo.Name + "\n" + + // Print commit count + res += fmt.Sprintf("\n Commit count: %d\n", repo.CommitCount) // Print branches - fmt.Printf("\n Branches:\n") + res += "\n Branches:\n" for i := range repo.Branches { - fmt.Printf(" ") - fmt.Println(repo.Branches[i]) + res += " " + res += repo.Branches[i] + "\n" } // Print remotes - fmt.Printf("\n Remotes:\n") + res += "\n Remotes:\n" for i := range repo.Remotes { - fmt.Printf(" ") - fmt.Println(repo.Remotes[i]) + res += " " + res += repo.Remotes[i] + "\n" } // Print tags - fmt.Printf("\n Tags:\n") - for i := range repo.Tags { - fmt.Printf(" ") - fmt.Printf( - "%s: %s\n", - repo.Tags[i]["hash"], - repo.Tags[i]["name"], - ) + if len(repo.Tags) > 0 { + res += "\n Tags:\n" + for i := range repo.Tags { + res += " " + res += fmt.Sprintf( + "%s: %s\n", + repo.Tags[i]["hash"], + repo.Tags[i]["name"], + ) + } } + + fmt.Println(res) } func encodeToken() string { diff --git a/server/main.go b/server/main.go index b9b3aee..f100251 100644 --- a/server/main.go +++ b/server/main.go @@ -14,14 +14,16 @@ import ( "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/config" "github.com/go-git/go-git/v5/plumbing" + "github.com/go-git/go-git/v5/plumbing/object" "github.com/go-yaml/yaml" ) type Repo struct { - Name string - Branches []string - Remotes []string - Tags []map[string]string + Name string + Branches []string + Remotes []string + Tags []map[string]string + CommitCount int } type ServerConfig struct { @@ -172,6 +174,13 @@ func getRepository(res http.ResponseWriter, req *http.Request) { return nil }) + // Get the commit count + commits, err := repo.CommitObjects() + commits.ForEach(func(obj *object.Commit) error { + repoRes.CommitCount++ + return nil + }) + encode := json.NewEncoder(res) encode.Encode(repoRes) }