mirror of
https://github.com/gogs/gogs.git
synced 2026-11-29 21:14:36 +01:00
81 lines
1.7 KiB
Go
81 lines
1.7 KiB
Go
package git
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"sync"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/trace"
|
|
)
|
|
|
|
// Tree represents a flat directory listing in Git.
|
|
type Tree struct {
|
|
id OID
|
|
parent *Tree
|
|
|
|
repo *Repository
|
|
|
|
entries Entries
|
|
entriesMu sync.Mutex
|
|
entriesSet bool
|
|
}
|
|
|
|
// Subtree returns a subtree by given subpath of the tree.
|
|
func (t *Tree) Subtree(ctx context.Context, subpath string) (_ *Tree, err error) {
|
|
ctx, span := tracer.Start(ctx, "git.Tree.Subtree", trace.WithAttributes(attribute.String("git.tree", t.id.String())))
|
|
defer func() { endSpan(span, err) }()
|
|
|
|
if len(subpath) == 0 {
|
|
return t, nil
|
|
}
|
|
|
|
paths := strings.Split(subpath, "/")
|
|
var (
|
|
g = t
|
|
p = t
|
|
e *TreeEntry
|
|
)
|
|
for _, name := range paths {
|
|
e, err = p.TreeEntry(ctx, name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// A path through a file, symlink, or submodule does not exist in the
|
|
// tree, even when Git could list the object of a submodule.
|
|
if !e.IsTree() {
|
|
return nil, ErrRevisionNotExist
|
|
}
|
|
|
|
g = &Tree{
|
|
id: e.id,
|
|
parent: p,
|
|
repo: t.repo,
|
|
}
|
|
p = g
|
|
}
|
|
return g, nil
|
|
}
|
|
|
|
// Entries returns all entries of the tree. Successful results are cached.
|
|
// Failed attempts are not cached, allowing retries with a fresh context.
|
|
func (t *Tree) Entries(ctx context.Context) (_ Entries, err error) {
|
|
ctx, span := tracer.Start(ctx, "git.Tree.Entries", trace.WithAttributes(attribute.String("git.tree", t.id.String())))
|
|
defer func() { endSpan(span, err) }()
|
|
|
|
t.entriesMu.Lock()
|
|
defer t.entriesMu.Unlock()
|
|
|
|
if t.entriesSet {
|
|
return t.entries, nil
|
|
}
|
|
|
|
tt, err := t.repo.LsTree(ctx, t.id.String())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
t.entries = tt.entries
|
|
t.entriesSet = true
|
|
return t.entries, nil
|
|
}
|