diff options
author | Runxi Yu <me@runxiyu.org> | 2025-03-25 02:24:52 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-03-25 02:24:52 +0800 |
commit | 958d64cc922b2688c910e339748fa28a0ff540b7 (patch) | |
tree | 7e0ebe68ce9bf31b051514682e95ee88c0dbfbec | |
parent | Revert "Add prometheus" (diff) | |
download | forge-958d64cc922b2688c910e339748fa28a0ff540b7.tar.gz forge-958d64cc922b2688c910e339748fa28a0ff540b7.tar.zst forge-958d64cc922b2688c910e339748fa28a0ff540b7.zip |
Cache commit logs on the repo index page
-rw-r--r-- | cache.go | 14 | ||||
-rw-r--r-- | git_misc.go | 54 | ||||
-rw-r--r-- | http_handle_repo_index.go | 32 | ||||
-rw-r--r-- | templates/repo_contrib_index.tmpl | 4 | ||||
-rw-r--r-- | templates/repo_contrib_one.tmpl | 2 | ||||
-rw-r--r-- | templates/repo_index.tmpl | 2 | ||||
-rw-r--r-- | templates/repo_log.tmpl | 2 |
7 files changed, 95 insertions, 15 deletions
@@ -29,3 +29,17 @@ func init() { clog.Fatal(1, "Error initializing indexPageCache: "+err.Error()) } } + +var indexCommitsDisplayCache *ristretto.Cache[[]byte, []commitDisplay] + +func init() { + var err error + indexCommitsDisplayCache, err = ristretto.NewCache(&ristretto.Config[[]byte, []commitDisplay]{ + NumCounters: 1e4, + MaxCost: 1 << 30, + BufferItems: 64, + }) + if err != nil { + clog.Fatal(1, "Error initializing indexCommitsCache: "+err.Error()) + } +} diff --git a/git_misc.go b/git_misc.go index 8b5dbf6..273c28b 100644 --- a/git_misc.go +++ b/git_misc.go @@ -165,6 +165,60 @@ func getRecentCommits(repo *git.Repository, headHash plumbing.Hash, numCommits i return recentCommits, err } +func getRecentCommitsDisplay(repo *git.Repository, headHash plumbing.Hash, numCommits int) (recentCommits []commitDisplay, err error) { + var commitIter object.CommitIter + var thisCommit *object.Commit + + commitIter, err = repo.Log(&git.LogOptions{From: headHash}) //exhaustruct:ignore + if err != nil { + return nil, err + } + recentCommits = make([]commitDisplay, 0) + defer commitIter.Close() + if numCommits < 0 { + for { + thisCommit, err = commitIter.Next() + if errors.Is(err, io.EOF) { + return recentCommits, nil + } else if err != nil { + return nil, err + } + recentCommits = append(recentCommits, commitDisplay{ + thisCommit.Hash, + thisCommit.Author, + thisCommit.Committer, + thisCommit.Message, + thisCommit.TreeHash, + }) + } + } else { + for range numCommits { + thisCommit, err = commitIter.Next() + if errors.Is(err, io.EOF) { + return recentCommits, nil + } else if err != nil { + return nil, err + } + recentCommits = append(recentCommits, commitDisplay{ + thisCommit.Hash, + thisCommit.Author, + thisCommit.Committer, + thisCommit.Message, + thisCommit.TreeHash, + }) + } + } + return recentCommits, err +} + +type commitDisplay struct { + Hash plumbing.Hash + Author object.Signature + Committer object.Signature + Message string + TreeHash plumbing.Hash +} + func fmtCommitAsPatch(commit *object.Commit) (parentCommitHash plumbing.Hash, patch *object.Patch, err error) { var parentCommit *object.Commit var commitTree *object.Tree diff --git a/http_handle_repo_index.go b/http_handle_repo_index.go index 9285e6e..21493c0 100644 --- a/http_handle_repo_index.go +++ b/http_handle_repo_index.go @@ -4,7 +4,6 @@ package main import ( - "iter" "net/http" "strings" "time" @@ -22,14 +21,12 @@ func httpHandleRepoIndex(writer http.ResponseWriter, _ *http.Request, params map var refHash plumbing.Hash var refHashSlice []byte var err error - var logOptions git.LogOptions - var commitIter object.CommitIter - var commitIterSeq iter.Seq[*object.Commit] var commitObj *object.Commit var tree *object.Tree var notes []string var branches []string var branchesIter storer.ReferenceIter + var commits []commitDisplay repo, repoName, groupPath = params["repo"].(*git.Repository), params["repo_name"].(string), params["group_path"].([]string) @@ -52,13 +49,28 @@ func httpHandleRepoIndex(writer http.ResponseWriter, _ *http.Request, params map } params["branches"] = branches - // TODO: Cache - logOptions = git.LogOptions{From: refHash} //exhaustruct:ignore - if commitIter, err = repo.Log(&logOptions); err != nil { - goto no_ref + if value, found := indexCommitsDisplayCache.Get(refHashSlice); found { + if value != nil { + commits = value + } else { + goto readme + } + } else { + start := time.Now() + commits, err = getRecentCommitsDisplay(repo, refHash, 5) + if err != nil { + commits = nil + } + cost := time.Since(start).Nanoseconds() + indexCommitsDisplayCache.Set(refHashSlice, commits, cost) + if err != nil { + goto readme + } } - commitIterSeq, params["commits_err"] = commitIterSeqErr(commitIter) - params["commits"] = iterSeqLimit(commitIterSeq, 3) + + params["commits"] = commits + +readme: if value, found := treeReadmeCache.Get(refHashSlice); found { params["files"] = value.DisplayTree diff --git a/templates/repo_contrib_index.tmpl b/templates/repo_contrib_index.tmpl index f791e4d..1848576 100644 --- a/templates/repo_contrib_index.tmpl +++ b/templates/repo_contrib_index.tmpl @@ -26,8 +26,8 @@ <tbody> {{- range .merge_requests -}} <tr> - <td class="merge_request-id">{{- .ID -}}</td> - <td class="merge_request-title"><a href="{{- .ID -}}/">{{- .Title -}}</a></td> + <td class="merge_request-id">{{- .Hash -}}</td> + <td class="merge_request-title"><a href="{{- .Hash -}}/">{{- .Title -}}</a></td> <td class="merge_request-status">{{- .Status -}}</td> </tr> {{- end -}} diff --git a/templates/repo_contrib_one.tmpl b/templates/repo_contrib_one.tmpl index b657ee8..113584a 100644 --- a/templates/repo_contrib_one.tmpl +++ b/templates/repo_contrib_one.tmpl @@ -41,7 +41,7 @@ </tr> <tr> <th scope="row">Merge base</th> - <td>{{- .merge_base.ID.String -}}</td> + <td>{{- .merge_base.Hash.String -}}</td> </tr> </tbody> </table> diff --git a/templates/repo_index.tmpl b/templates/repo_index.tmpl index ba5ad9e..39e55cf 100644 --- a/templates/repo_index.tmpl +++ b/templates/repo_index.tmpl @@ -81,7 +81,7 @@ <tbody> {{- range .commits -}} <tr> - <td class="commit-title"><a href="commit/{{- .ID -}}">{{- .Message | first_line -}}</a></td> + <td class="commit-title"><a href="commit/{{- .Hash -}}">{{- .Message | first_line -}}</a></td> <td class="commit-author"> <a class="email-name" href="mailto:{{- .Author.Email -}}">{{- .Author.Name -}}</a> </td> diff --git a/templates/repo_log.tmpl b/templates/repo_log.tmpl index ac5f554..2cb5867 100644 --- a/templates/repo_log.tmpl +++ b/templates/repo_log.tmpl @@ -27,7 +27,7 @@ <tbody> {{- range .commits -}} <tr> - <td class="commit-id"><a href="../commit/{{- .ID -}}">{{- .ID -}}</a></td> + <td class="commit-id"><a href="../commit/{{- .Hash -}}">{{- .Hash -}}</a></td> <td class="commit-title">{{- .Message | first_line -}}</td> <td class="commit-author"> <a class="email-name" href="mailto:{{- .Author.Email -}}">{{- .Author.Name -}}</a> |