Skip to content

Commit 98c7a45

Browse files
committed
Track pathnames of objects at initial creation time.
The objects might move around in the tree from commit to commit, but it's a little friendlier than nothing at all, and produces better error messages.
1 parent 203a66b commit 98c7a45

File tree

1 file changed

+44
-18
lines changed

1 file changed

+44
-18
lines changed

subtrac.go

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"gopkg.in/src-d/go-git.v4/plumbing/filemode"
99
"gopkg.in/src-d/go-git.v4/plumbing/object"
1010
"log"
11+
"sort"
12+
"strconv"
1113
"strings"
1214
)
1315

@@ -55,8 +57,17 @@ func NewCache(r *git.Repository) *Cache {
5557
}
5658

5759
func (c *Cache) String() string {
58-
var out []string
60+
var l []*Trac
5961
for _, v := range c.tracs {
62+
l = append(l, v)
63+
}
64+
65+
sort.Slice(l, func(i, j int) bool {
66+
return l[i].name < l[j].name
67+
})
68+
69+
var out []string
70+
for _, v := range l {
6071
out = append(out, v.String())
6172
}
6273
return strings.Join(out, "\n")
@@ -72,41 +83,57 @@ func (c *Cache) tracByRef(refname string) (*Trac, error) {
7283
if err != nil {
7384
return nil, err
7485
}
75-
return c.tracCommit(commit)
86+
return c.tracCommit(refname, commit)
7687
}
7788

7889
// Mercifully, git's content-addressable storage means there are never
7990
// any cycles when traversing the commit+submodule hierarchy, although the
8091
// same sub-objects may occur many times at different points in the tree.
81-
func (c *Cache) tracCommit(commit *object.Commit) (*Trac, error) {
92+
func (c *Cache) tracCommit(path string, commit *object.Commit) (*Trac, error) {
8293
trac := c.tracs[commit.Hash]
8394
if trac != nil {
8495
return trac, nil
8596
}
8697
trac = &Trac{
87-
name: "<COMMIT>",
98+
name: path,
8899
hash: commit.Hash,
89100
}
90101
tree, err := commit.Tree()
91102
if err != nil {
92-
return nil, fmt.Errorf("%.10v.Tree: %v", commit.Hash, err)
103+
return nil, fmt.Errorf("%v:%.10v: %v", path, commit.Hash, err)
93104
}
94-
_, err = c.tracTree("<ROOT>", tree)
105+
_, err = c.tracTree(path+"/", tree)
95106
if err != nil {
96-
return nil, fmt.Errorf("%.10v.addTree: %v", commit.Hash, err)
107+
return nil, err
97108
}
98-
for _, parent := range commit.ParentHashes {
109+
for i, parent := range commit.ParentHashes {
99110
pc, err := c.repo.CommitObject(parent)
100111
if err != nil {
101-
return nil, fmt.Errorf("%.10v: %v", pc.Hash, err)
112+
return nil, fmt.Errorf("%v:%.10v: %v", path, pc.Hash, err)
102113
}
103-
_, err = c.tracCommit(pc)
114+
np := commitPath(path, i+1)
115+
_, err = c.tracCommit(np, pc)
104116
}
105117
c.tracs[commit.Hash] = trac
106118
return trac, nil
107119
}
108120

109-
func (c *Cache) tracTree(name string, tree *object.Tree) (*Trac, error) {
121+
func commitPath(path string, sub int) string {
122+
if sub != 1 {
123+
return fmt.Sprintf("%s^%d", path, sub)
124+
}
125+
ix := strings.LastIndexByte(path, '~')
126+
if ix < 0 {
127+
return fmt.Sprintf("%s~1", path)
128+
}
129+
v, err := strconv.Atoi(path[ix+1:])
130+
if err != nil {
131+
return fmt.Sprintf("%s~1", path)
132+
}
133+
return fmt.Sprintf("%s~%d", path[:ix], v+1)
134+
}
135+
136+
func (c *Cache) tracTree(path string, tree *object.Tree) (*Trac, error) {
110137
trac := c.tracs[tree.Hash]
111138
if trac != nil {
112139
return trac, nil
@@ -117,18 +144,17 @@ func (c *Cache) tracTree(name string, tree *object.Tree) (*Trac, error) {
117144
} else if e.Mode == filemode.Dir {
118145
t, err := c.repo.TreeObject(e.Hash)
119146
if err != nil {
120-
return nil, fmt.Errorf("%.10v.Tree.%.10v: %v",
121-
tree.Hash, e.Hash, err)
147+
return nil, fmt.Errorf("%v:%.10v: %v",
148+
path, e.Hash, err)
122149
}
123-
_, err = c.tracTree(e.Name, t)
150+
_, err = c.tracTree(path+e.Name+"/", t)
124151
if err != nil {
125-
return nil, fmt.Errorf("%.10v.addTree: %v",
126-
t.Hash, err)
152+
return nil, err
127153
}
128154
}
129155
}
130156
trac = &Trac{
131-
name: name,
157+
name: path,
132158
hash: tree.Hash,
133159
}
134160
c.tracs[tree.Hash] = trac
@@ -145,7 +171,7 @@ func main() {
145171
refname := "junk"
146172
_, err = c.tracByRef(refname)
147173
if err != nil {
148-
fatalf("AddByRef: %v: %v\n", refname, err)
174+
fatalf("AddByRef: %v\n", err)
149175
}
150176
fmt.Printf("%v\n", c)
151177
}

0 commit comments

Comments
 (0)