Fix #6813: Allow git.GetTree to take both commit and tree names (#6816)

* Allow git.GetTree to take both commit and tree names, return full paths on entries listed through Tree.ListEntriesRecursive

Signed-off-by: Filip Navara <filip.navara@gmail.com>

* Fix the SHA returned on Git Tree APIs called with commit hash or symbolic name

Signed-off-by: Filip Navara <filip.navara@gmail.com>
This commit is contained in:
Filip Navara 2019-05-03 02:33:11 +02:00 committed by techknowlogick
parent a27d5d2b23
commit dbb0c9658c
4 changed files with 18 additions and 12 deletions

View file

@ -35,14 +35,15 @@ func (repo *Repository) GetTree(idStr string) (*Tree, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
resolvedID := id
commitObject, err := repo.gogitRepo.CommitObject(plumbing.Hash(id)) commitObject, err := repo.gogitRepo.CommitObject(plumbing.Hash(id))
if err == nil {
id = SHA1(commitObject.TreeHash)
}
treeObject, err := repo.getTree(id)
if err != nil { if err != nil {
return nil, err return nil, err
} }
treeObject, err := repo.getTree(SHA1(commitObject.TreeHash)) treeObject.ResolvedID = resolvedID
if err != nil {
return nil, err
}
treeObject.CommitID = id
return treeObject, nil return treeObject, nil
} }

View file

@ -15,9 +15,9 @@ import (
// Tree represents a flat directory listing. // Tree represents a flat directory listing.
type Tree struct { type Tree struct {
ID SHA1 ID SHA1
CommitID SHA1 ResolvedID SHA1
repo *Repository repo *Repository
gogitTree *object.Tree gogitTree *object.Tree
@ -106,7 +106,7 @@ func (t *Tree) ListEntriesRecursive() (Entries, error) {
seen := map[plumbing.Hash]bool{} seen := map[plumbing.Hash]bool{}
walker := object.NewTreeWalker(t.gogitTree, true, seen) walker := object.NewTreeWalker(t.gogitTree, true, seen)
for { for {
_, entry, err := walker.Next() fullName, entry, err := walker.Next()
if err == io.EOF { if err == io.EOF {
break break
} }
@ -121,6 +121,7 @@ func (t *Tree) ListEntriesRecursive() (Entries, error) {
ID: entry.Hash, ID: entry.Hash,
gogitTreeEntry: &entry, gogitTreeEntry: &entry,
ptree: t, ptree: t,
fullName: fullName,
} }
entries = append(entries, convertedEntry) entries = append(entries, convertedEntry)
} }

View file

@ -40,12 +40,16 @@ type TreeEntry struct {
gogitTreeEntry *object.TreeEntry gogitTreeEntry *object.TreeEntry
ptree *Tree ptree *Tree
size int64 size int64
sized bool sized bool
fullName string
} }
// Name returns the name of the entry // Name returns the name of the entry
func (te *TreeEntry) Name() string { func (te *TreeEntry) Name() string {
if te.fullName != "" {
return te.fullName
}
return te.gogitTreeEntry.Name return te.gogitTreeEntry.Name
} }

View file

@ -23,7 +23,7 @@ func GetTreeBySHA(repo *models.Repository, sha string, page, perPage int, recurs
} }
} }
tree := new(api.GitTreeResponse) tree := new(api.GitTreeResponse)
tree.SHA = gitTree.CommitID.String() tree.SHA = gitTree.ResolvedID.String()
tree.URL = repo.APIURL() + "/git/trees/" + tree.SHA tree.URL = repo.APIURL() + "/git/trees/" + tree.SHA
var entries git.Entries var entries git.Entries
if recursive { if recursive {