Author | Jakob Wakeling <[email protected]> |
Date | 2023-12-25 10:47:47 |
Commit | d6784d5fe91e45dad537cd5ae4a85898d1e07d24 |
Parent | 83800a4f8dddc219023ac9b3d658ddfe00fc9591 |
Display commit count per branch on refs page
Diffstat
M | res/repo/refs.html | | | 2 | ++ |
M | src/goit/git.go | | | 35 | +++++++++++++++++++++++++++++++++++ |
M | src/repo/refs.go | | | 14 | +++++++++++--- |
3 files changed, 48 insertions, 3 deletions
diff --git a/res/repo/refs.html b/res/repo/refs.html index ac0d253..00f20cd 100644 --- a/res/repo/refs.html +++ b/res/repo/refs.html @@ -11,6 +11,7 @@ <td><b>Message</b></td> <td><b>Author</b></td> <td><b>Last Commit</b></td> + <td><b>Commits</b></td> </tr> </thead> <tbody> @@ -20,6 +21,7 @@ <td><a href="/{{$.Name}}/commit/{{.Hash}}">{{.Message}}</a></td> <td>{{.Author}}</td> <td>{{.LastCommit}}</td> + <td>{{.Commits}}</td> </tr> {{else}} <tr><td colspan="4">No branches</td></tr> diff --git a/src/goit/git.go b/src/goit/git.go index e75fd0e..d6b1737 100644 --- a/src/goit/git.go +++ b/src/goit/git.go @@ -324,3 +324,38 @@ func DiffStats(c *object.Commit) ([]DiffStat, error) { return stats, nil } + +type countPair struct { + hash plumbing.Hash + count uint64 +} + +var counts = map[string]countPair{} +var countsLock sync.RWMutex + +func CommitCount(repo, branch string, hash plumbing.Hash) (uint64, error) { + countsLock.RLock() + if count, ok := counts[repo+"/"+branch]; ok && count.hash == hash { + countsLock.RUnlock() + return count.count, nil + } + countsLock.RUnlock() + + c := NewGitCommand("rev-list", "--count", branch) + c.Dir = RepoPath(repo, true) + out, _, err := c.Run(nil, nil) + if err != nil { + return 0, err + } + + count, err := strconv.ParseUint(strings.TrimSpace(string(out)), 10, 64) + if err != nil { + return 0, err + } + + countsLock.Lock() + counts[repo+"/"+branch] = countPair{hash, count} + countsLock.Unlock() + + return count, nil +} diff --git a/src/repo/refs.go b/src/repo/refs.go index f1454af..f76afb9 100644 --- a/src/repo/refs.go +++ b/src/repo/refs.go @@ -35,7 +35,10 @@ func HandleRefs(w http.ResponseWriter, r *http.Request) { return } - type row struct{ Name, Message, Author, LastCommit, Hash string } + type row struct { + Name, Hash, Message, Author, LastCommit string + Commits uint64 + } data := struct { HeaderFields Title string @@ -78,9 +81,14 @@ func HandleRefs(w http.ResponseWriter, r *http.Request) { return err } + commits, err := goit.CommitCount(repo.Name, r.Name().Short(), r.Hash()) + if err != nil { + return err + } + data.Branches = append(data.Branches, row{ - Name: r.Name().Short(), Message: strings.SplitN(commit.Message, "\n", 2)[0], Author: commit.Author.Name, - LastCommit: commit.Author.When.UTC().Format(time.DateTime), Hash: r.Hash().String(), + Name: r.Name().Short(), Hash: r.Hash().String(), Message: strings.SplitN(commit.Message, "\n", 2)[0], + Author: commit.Author.Name, LastCommit: commit.Author.When.UTC().Format(time.DateTime), Commits: commits, }) return nil