mirror of
https://github.com/go-gitea/gitea.git
synced 2026-07-21 22:05:17 +02:00
before: gitrepo vs git packages after: git package fully handle all git operations by the way, use `WithRepo(repo)` instead of `WithDir(repo.Path)` to hide path details. benefits: 1. remove all unnecessary wrappers, developers no need to struggle with "which package should be used" 2. simplify code, RepositoryFacade can (will) be used everywhere, all "path" details are (will be) hidden
79 lines
1.8 KiB
Go
79 lines
1.8 KiB
Go
// Copyright 2015 The Gogs Authors. All rights reserved.
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package git
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"strings"
|
|
|
|
"gitea.dev/modules/git/gitcmd"
|
|
)
|
|
|
|
type TreeCommon struct {
|
|
ID ObjectID
|
|
}
|
|
|
|
func newTree(id ObjectID) *Tree {
|
|
return &Tree{TreeCommon: TreeCommon{ID: id}}
|
|
}
|
|
|
|
// SubTree get a subtree by the sub dir path
|
|
func (t *Tree) SubTree(ctx context.Context, gitRepo *Repository, rpath string) (*Tree, error) {
|
|
if len(rpath) == 0 {
|
|
return t, nil
|
|
}
|
|
|
|
paths := strings.Split(rpath, "/")
|
|
var (
|
|
err error
|
|
g = t
|
|
p = t
|
|
te *TreeEntry
|
|
)
|
|
for _, name := range paths {
|
|
te, err = p.GetTreeEntryByPath(ctx, gitRepo, name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
g, err = gitRepo.getTree(ctx, te.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
p = g
|
|
}
|
|
return g, nil
|
|
}
|
|
|
|
// LsTree checks if the given filenames are in the tree
|
|
func (repo *Repository) LsTree(ctx context.Context, ref string, filenames ...string) ([]string, error) {
|
|
cmd := gitcmd.NewCommand("ls-tree", "-z", "--name-only").
|
|
AddDashesAndList(append([]string{ref}, filenames...)...)
|
|
|
|
res, _, err := cmd.WithRepo(repo).RunStdBytes(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
filelist := make([]string, 0, len(filenames))
|
|
for line := range bytes.SplitSeq(res, []byte{'\000'}) {
|
|
filelist = append(filelist, string(line))
|
|
}
|
|
|
|
return filelist, err
|
|
}
|
|
|
|
// GetTreePathLatestCommit returns the latest commit of a tree path
|
|
func (repo *Repository) GetTreePathLatestCommit(ctx context.Context, refName, treePath string) (*Commit, error) {
|
|
stdout, _, err := gitcmd.NewCommand("rev-list", "-1").
|
|
AddDynamicArguments(refName).AddDashesAndList(treePath).
|
|
WithRepo(repo).
|
|
RunStdString(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return repo.GetCommit(ctx, strings.TrimSpace(stdout))
|
|
}
|