Skip to content

Commit 6b81917

Browse files
Kasi-Rlafriks
authored andcommitted
Improving / Exposing Git-Trees related features for Git-Trees API. (go-gitea#123)
* Exposed TreeEntry.mode, changed EntryMode to hex. * Added reference parsing (HEAD, HEAD~1, etc) for trees * Added missing description * Added Tree.ListEntriesRecursive() - utilizes the git ls-tree -r command
1 parent 578ad8f commit 6b81917

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

repo_tree.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ func (repo *Repository) getTree(id SHA1) (*Tree, error) {
1818

1919
// GetTree find the tree object in the repository.
2020
func (repo *Repository) GetTree(idStr string) (*Tree, error) {
21+
if len(idStr) != 40 {
22+
res, err := NewCommand("rev-parse", idStr).RunInDir(repo.Path)
23+
if err != nil {
24+
return nil, err;
25+
}
26+
if len(res) > 0 {
27+
idStr = res[:len(res)-1]
28+
}
29+
}
2130
id, err := NewIDFromString(idStr)
2231
if err != nil {
2332
return nil, err

tree.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,17 @@ func (t *Tree) ListEntries() (Entries, error) {
7070
t.entries, err = parseTreeEntries(stdout, t)
7171
return t.entries, err
7272
}
73+
74+
// ListEntriesRecursive returns all entries of current tree recursively including all subtrees
75+
func (t *Tree) ListEntriesRecursive() (Entries, error) {
76+
if t.entriesParsed {
77+
return t.entries, nil
78+
}
79+
stdout, err := NewCommand("ls-tree", "-t", "-r", t.ID.String()).RunInDirBytes(t.repo.Path)
80+
81+
if err != nil {
82+
return nil, err
83+
}
84+
t.entries, err = parseTreeEntries(stdout, t)
85+
return t.entries, err
86+
}

tree_entry.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ type EntryMode int
1818
// one of these.
1919
const (
2020
// EntryModeBlob
21-
EntryModeBlob EntryMode = 0100644
21+
EntryModeBlob EntryMode = 0x0100644
2222
// EntryModeExec
23-
EntryModeExec EntryMode = 0100755
23+
EntryModeExec EntryMode = 0x0100755
2424
// EntryModeSymlink
25-
EntryModeSymlink EntryMode = 0120000
25+
EntryModeSymlink EntryMode = 0x0120000
2626
// EntryModeCommit
27-
EntryModeCommit EntryMode = 0160000
27+
EntryModeCommit EntryMode = 0x0160000
2828
// EntryModeTree
29-
EntryModeTree EntryMode = 0040000
29+
EntryModeTree EntryMode = 0x0040000
3030
)
3131

3232
// TreeEntry the leaf in the git tree
@@ -50,6 +50,11 @@ func (te *TreeEntry) Name() string {
5050
return te.name
5151
}
5252

53+
// Mode returns the mode of the entry
54+
func (te *TreeEntry) Mode() EntryMode {
55+
return te.mode
56+
}
57+
5358
// Size returns the size of the entry
5459
func (te *TreeEntry) Size() int64 {
5560
if te.IsDir() {

0 commit comments

Comments
 (0)