8
8
"gopkg.in/src-d/go-git.v4/plumbing/filemode"
9
9
"gopkg.in/src-d/go-git.v4/plumbing/object"
10
10
"log"
11
+ "sort"
12
+ "strconv"
11
13
"strings"
12
14
)
13
15
@@ -55,8 +57,17 @@ func NewCache(r *git.Repository) *Cache {
55
57
}
56
58
57
59
func (c * Cache ) String () string {
58
- var out []string
60
+ var l []* Trac
59
61
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 {
60
71
out = append (out , v .String ())
61
72
}
62
73
return strings .Join (out , "\n " )
@@ -72,41 +83,57 @@ func (c *Cache) tracByRef(refname string) (*Trac, error) {
72
83
if err != nil {
73
84
return nil , err
74
85
}
75
- return c .tracCommit (commit )
86
+ return c .tracCommit (refname , commit )
76
87
}
77
88
78
89
// Mercifully, git's content-addressable storage means there are never
79
90
// any cycles when traversing the commit+submodule hierarchy, although the
80
91
// 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 ) {
82
93
trac := c .tracs [commit .Hash ]
83
94
if trac != nil {
84
95
return trac , nil
85
96
}
86
97
trac = & Trac {
87
- name : "<COMMIT>" ,
98
+ name : path ,
88
99
hash : commit .Hash ,
89
100
}
90
101
tree , err := commit .Tree ()
91
102
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 )
93
104
}
94
- _ , err = c .tracTree ("<ROOT> " , tree )
105
+ _ , err = c .tracTree (path + "/ " , tree )
95
106
if err != nil {
96
- return nil , fmt . Errorf ( "%.10v.addTree: %v" , commit . Hash , err )
107
+ return nil , err
97
108
}
98
- for _ , parent := range commit .ParentHashes {
109
+ for i , parent := range commit .ParentHashes {
99
110
pc , err := c .repo .CommitObject (parent )
100
111
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 )
102
113
}
103
- _ , err = c .tracCommit (pc )
114
+ np := commitPath (path , i + 1 )
115
+ _ , err = c .tracCommit (np , pc )
104
116
}
105
117
c .tracs [commit .Hash ] = trac
106
118
return trac , nil
107
119
}
108
120
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 ) {
110
137
trac := c .tracs [tree .Hash ]
111
138
if trac != nil {
112
139
return trac , nil
@@ -117,18 +144,17 @@ func (c *Cache) tracTree(name string, tree *object.Tree) (*Trac, error) {
117
144
} else if e .Mode == filemode .Dir {
118
145
t , err := c .repo .TreeObject (e .Hash )
119
146
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 )
122
149
}
123
- _ , err = c .tracTree (e .Name , t )
150
+ _ , err = c .tracTree (path + e .Name + "/" , t )
124
151
if err != nil {
125
- return nil , fmt .Errorf ("%.10v.addTree: %v" ,
126
- t .Hash , err )
152
+ return nil , err
127
153
}
128
154
}
129
155
}
130
156
trac = & Trac {
131
- name : name ,
157
+ name : path ,
132
158
hash : tree .Hash ,
133
159
}
134
160
c .tracs [tree .Hash ] = trac
@@ -145,7 +171,7 @@ func main() {
145
171
refname := "junk"
146
172
_ , err = c .tracByRef (refname )
147
173
if err != nil {
148
- fatalf ("AddByRef: %v: %v \n " , refname , err )
174
+ fatalf ("AddByRef: %v\n " , err )
149
175
}
150
176
fmt .Printf ("%v\n " , c )
151
177
}
0 commit comments