From 958d64cc922b2688c910e339748fa28a0ff540b7 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Tue, 25 Mar 2025 02:24:52 +0800 Subject: [PATCH] Cache commit logs on the repo index page --- cache.go | 14 ++++++++++++++ git_misc.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ http_handle_repo_index.go | 32 ++++++++++++++++++++++---------- templates/repo_contrib_index.tmpl | 4 ++-- templates/repo_contrib_one.tmpl | 2 +- templates/repo_index.tmpl | 2 +- templates/repo_log.tmpl | 2 +- diff --git a/cache.go b/cache.go index e74588449c38b4771cc5d9e5907e2b15a8575017..72c6458bd3170172f6902ca2c0d5bb067bbcc7fc 100644 --- a/cache.go +++ b/cache.go @@ -29,3 +29,17 @@ if err != nil { 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 8b5dbf6528a77bbf861735619d17a79a6bbfa1ba..273c28b78f057f647f3e8197f7f3eb5ca4cde54a 100644 --- a/git_misc.go +++ b/git_misc.go @@ -165,6 +165,60 @@ } 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 9285e6e5e993bd54ee4d01ffedd0b634a50f0f0b..21493c00630f281b37c1a1b4be121451aa3599d0 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 @@ var groupPath []string 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 @@ }) } 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 f791e4db965b53012a2961bec49780240f876c42..1848576e39d562647dcf327596dc386d79f99361 100644 --- a/templates/repo_contrib_index.tmpl +++ b/templates/repo_contrib_index.tmpl @@ -26,8 +26,8 @@ {{- range .merge_requests -}} - {{- .ID -}} - {{- .Title -}} + {{- .Hash -}} + {{- .Title -}} {{- .Status -}} {{- end -}} diff --git a/templates/repo_contrib_one.tmpl b/templates/repo_contrib_one.tmpl index b657ee874516f1926e23c7933e843caa80d6901c..113584aa3cb3703d8e0d3ed347de205fd677782c 100644 --- a/templates/repo_contrib_one.tmpl +++ b/templates/repo_contrib_one.tmpl @@ -41,7 +41,7 @@ {{- .mr_destination_branch -}} Merge base - {{- .merge_base.ID.String -}} + {{- .merge_base.Hash.String -}} diff --git a/templates/repo_index.tmpl b/templates/repo_index.tmpl index ba5ad9e94f51a2b0a56e6628de23fbdcbf480979..39e55cfa5fdddcc3e3c3f215c3ecc8f0a8698d55 100644 --- a/templates/repo_index.tmpl +++ b/templates/repo_index.tmpl @@ -81,7 +81,7 @@ {{- range .commits -}} - {{- .Message | first_line -}} + {{- .Message | first_line -}} {{- .Author.Name -}} diff --git a/templates/repo_log.tmpl b/templates/repo_log.tmpl index ac5f5549e14b666ecc2c1dda49101a6e1c76f7c7..2cb586713efd9fb7841f871e0e98617e64fa431e 100644 --- a/templates/repo_log.tmpl +++ b/templates/repo_log.tmpl @@ -27,7 +27,7 @@ {{- range .commits -}} - {{- .ID -}} + {{- .Hash -}} {{- .Message | first_line -}} {{- .Author.Name -}} -- 2.48.1