Skip to content

Commit

Permalink
More efficient way
Browse files Browse the repository at this point in the history
  • Loading branch information
sapk committed Mar 13, 2016
1 parent e159adc commit b03405e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
5 changes: 5 additions & 0 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,10 @@ func (t *Tree) ListEntries() (Entries, error) {
return nil, err
}
t.entries, err = parseTreeData(t, stdout)

for _, entry := range t.entries {
entry.parseLastUniqueEntry()
}

return t.entries, err
}
53 changes: 25 additions & 28 deletions tree_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,23 @@ type TreeEntry struct {

ptree *Tree

lastUniqueEntry *TreeEntry
lastUniquePath string

commited bool

size int64
sized bool
}

func (te *TreeEntry) LastUniqueEntry() *TreeEntry {
return te.lastUniqueEntry
}

func (te *TreeEntry) LastUniquePath() string {
return te.lastUniquePath
}

func (te *TreeEntry) Name() string {
return te.name
}
Expand All @@ -66,37 +77,23 @@ func (te *TreeEntry) IsSubModule() bool {
return te.mode == ENTRY_MODE_COMMIT
}

func (te *TreeEntry) JumpableName() string {
entry := te
for entry.IsJumpableDir() {
tree, _ := entry.ptree.SubTree(entry.name)
entries, _ := tree.ListEntries()
entry = entries[0]
}
return entry.name
}
func (te *TreeEntry) JumpablePath() string {
path := ""
entry := te
for entry.IsJumpableDir() {
path += entry.name + "/"
tree, _ := entry.ptree.SubTree(entry.name)
entries, _ := tree.ListEntries()
entry = entries[0]
}
return path
}
func (te *TreeEntry) parseLastUniqueEntry() *TreeEntry {
te.lastUniqueEntry = te
te.lastUniquePath = ""

func (te *TreeEntry) IsJumpableDir() bool {
if te.IsSubModule() || !te.IsDir() {
return false
if te.lastUniqueEntry.IsSubModule() || !te.lastUniqueEntry.IsDir() {
return te.lastUniqueEntry
}
tree, err := te.ptree.SubTree(te.name)
if err != nil {
return false

tree, _ := te.lastUniqueEntry.ptree.SubTree(te.lastUniqueEntry.name)
entries, _ := tree.ListEntries()
for !(te.lastUniqueEntry.IsSubModule() || !te.lastUniqueEntry.IsDir()) && len(entries) == 1 {
te.lastUniquePath += te.lastUniqueEntry.name + "/"
te.lastUniqueEntry = entries[0]
tree, _ = te.lastUniqueEntry.ptree.SubTree(te.lastUniqueEntry.name)
entries, _ = tree.ListEntries()
}
entries, err := tree.ListEntries()
return len(entries) == 1
return te.lastUniqueEntry
}

func (te *TreeEntry) IsDir() bool {
Expand Down

0 comments on commit b03405e

Please sign in to comment.