Some checks failed
cache-seeder / prune (push) Has been skipped
cache-seeder / frontend (push) Failing after 3s
cache-seeder / python (push) Failing after 3s
cache-seeder / gobuild (push) Failing after 4s
cache-seeder / lint (bindata, lint-backend) (push) Failing after 3s
giteabot backport / giteabot (push) Has been skipped
giteabot / giteabot (push) Has been skipped
cache-prune / prune (push) Has been skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Has been cancelled
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Has been cancelled
release-nightly / nightly-binary (push) Has been cancelled
release-nightly / nightly-container (push) Has been cancelled
cron-renovate / cron-renovate (push) Has been skipped
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))
|
|
}
|