From 958d64cc922b2688c910e339748fa28a0ff540b7 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Tue, 25 Mar 2025 02:24:52 +0800 Subject: Cache commit logs on the repo index page --- git_misc.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'git_misc.go') 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 -- cgit v1.2.3