2020-12-17 14:00:47 +00:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-17 14:00:47 +00:00
|
|
|
|
2021-08-24 16:47:09 +00:00
|
|
|
//go:build gogit
|
2020-12-17 14:00:47 +00:00
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2021-06-06 23:44:58 +00:00
|
|
|
"context"
|
2020-12-17 14:00:47 +00:00
|
|
|
|
2023-12-13 21:02:00 +00:00
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
2020-12-17 14:00:47 +00:00
|
|
|
cgobject "github.com/go-git/go-git/v5/plumbing/object/commitgraph"
|
|
|
|
)
|
|
|
|
|
2022-07-25 15:39:42 +00:00
|
|
|
// CacheCommit will cache the commit from the gitRepository
|
|
|
|
func (c *Commit) CacheCommit(ctx context.Context) error {
|
|
|
|
if c.repo.LastCommitCache == nil {
|
2020-12-17 14:00:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-07-25 15:39:42 +00:00
|
|
|
commitNodeIndex, _ := c.repo.CommitNodeIndex()
|
2020-12-17 14:00:47 +00:00
|
|
|
|
2023-12-13 21:02:00 +00:00
|
|
|
index, err := commitNodeIndex.Get(plumbing.Hash(c.ID.RawValue()))
|
2020-12-17 14:00:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-25 15:39:42 +00:00
|
|
|
return c.recursiveCache(ctx, index, &c.Tree, "", 1)
|
2020-12-17 14:00:47 +00:00
|
|
|
}
|
|
|
|
|
2022-07-25 15:39:42 +00:00
|
|
|
func (c *Commit) recursiveCache(ctx context.Context, index cgobject.CommitNode, tree *Tree, treePath string, level int) error {
|
2020-12-17 14:00:47 +00:00
|
|
|
if level == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
entries, err := tree.ListEntries()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-07-25 15:39:42 +00:00
|
|
|
commits, err := GetLastCommitForPaths(ctx, c.repo.LastCommitCache, index, treePath, entryPaths)
|
2020-12-17 14:00:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-08 13:08:22 +00:00
|
|
|
for entry := range commits {
|
2020-12-17 14:00:47 +00:00
|
|
|
if entryMap[entry].IsDir() {
|
|
|
|
subTree, err := tree.SubTree(entry)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-06 23:44:58 +00:00
|
|
|
if err := c.recursiveCache(ctx, index, subTree, entry, level-1); err != nil {
|
2020-12-17 14:00:47 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|