mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-06 19:08:57 +09:00
deccd53c24
1. always use "last commit cache" 2. correctly build the cache key for any input (SafeCacheKey) 3. fix the git note "last commit cache FIXME" and avoid OOM
64 lines
1.5 KiB
Go
64 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 {
|
|
commitNodeIndex, closer := gitRepo.CommitNodeIndex()
|
|
defer closer()
|
|
|
|
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
|
|
}
|