mirror of
https://github.com/go-gitea/gitea.git
synced 2026-07-16 11:43:01 +02:00
1. Storing "ctx" in a long-living object is wrong 2. Make the commit & tree cacheable (for the future performance optimization) 3. Also fix some bad designs like `// FIXME: bad design, this field can be nil if the commit is from "last commit cache"` ref: * #33893
66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
//go:build gogit
|
|
|
|
package git
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
|
cgobject "github.com/go-git/go-git/v5/plumbing/object/commitgraph"
|
|
)
|
|
|
|
// CacheCommit will cache the commit from the gitRepository
|
|
func (c *Commit) CacheCommit(ctx context.Context, gitRepo *Repository) error {
|
|
if gitRepo.LastCommitCache == nil {
|
|
return nil
|
|
}
|
|
commitNodeIndex, _ := gitRepo.CommitNodeIndex()
|
|
|
|
index, err := commitNodeIndex.Get(plumbing.Hash(c.ID.RawValue()))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.recursiveCache(ctx, gitRepo, index, c.Tree(), "", 1)
|
|
}
|
|
|
|
func (c *Commit) recursiveCache(ctx context.Context, gitRepo *Repository, index cgobject.CommitNode, tree *Tree, treePath string, level int) error {
|
|
if level == 0 {
|
|
return nil
|
|
}
|
|
|
|
entries, err := tree.ListEntries(ctx, gitRepo)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
entryPaths := make([]string, len(entries))
|
|
entryMap := make(map[string]*TreeEntry)
|
|
for i, entry := range entries {
|
|
entryPaths[i] = entry.Name()
|
|
entryMap[entry.Name()] = entry
|
|
}
|
|
|
|
commits, err := getLastCommitForPathsByCommitNode(ctx, gitRepo, index, treePath, entryPaths)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for entry := range commits {
|
|
if entryMap[entry].IsDir() {
|
|
subTree, err := tree.SubTree(ctx, gitRepo, entry)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := c.recursiveCache(ctx, gitRepo, index, subTree, entry, level-1); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|