@@ -6,8 +6,7 @@ package git
6
6
7
7
import (
8
8
"fmt"
9
- "os"
10
- "path/filepath"
9
+ "path"
11
10
"sort"
12
11
"strconv"
13
12
"strings"
@@ -149,7 +148,7 @@ func (tes Entries) Sort() {
149
148
// getCommitInfoState transient state for getting commit info for entries
150
149
type getCommitInfoState struct {
151
150
entries map [string ]* TreeEntry // map from filepath to entry
152
- commits map [string ]* Commit // map from entry name to commit
151
+ commits map [string ]* Commit // map from filepath to commit
153
152
lastCommitHash string
154
153
lastCommit * Commit
155
154
treePath string
@@ -160,7 +159,10 @@ type getCommitInfoState struct {
160
159
func initGetCommitInfoState (entries Entries , headCommit * Commit , treePath string ) * getCommitInfoState {
161
160
entriesByPath := make (map [string ]* TreeEntry , len (entries ))
162
161
for _ , entry := range entries {
163
- entriesByPath [filepath .Join (treePath , entry .Name ())] = entry
162
+ entriesByPath [path .Join (treePath , entry .Name ())] = entry
163
+ }
164
+ if treePath = path .Clean (treePath ); treePath == "." {
165
+ treePath = ""
164
166
}
165
167
return & getCommitInfoState {
166
168
entries : entriesByPath ,
@@ -180,7 +182,7 @@ func (tes Entries) GetCommitsInfo(commit *Commit, treePath string) ([][]interfac
180
182
181
183
commitsInfo := make ([][]interface {}, len (tes ))
182
184
for i , entry := range tes {
183
- commit = state .commits [filepath .Join (treePath , entry .Name ())]
185
+ commit = state .commits [path .Join (treePath , entry .Name ())]
184
186
switch entry .Type {
185
187
case ObjectCommit :
186
188
subModuleURL := ""
@@ -211,22 +213,23 @@ func (state *getCommitInfoState) commit() (*Commit, error) {
211
213
return state .lastCommit , err
212
214
}
213
215
214
- func (state * getCommitInfoState ) update (path string ) error {
215
- relPath , err := filepath . Rel ( state . treePath , path )
216
- if err != nil {
217
- return nil
216
+ func (state * getCommitInfoState ) update (entryPath string ) error {
217
+ var entryNameStartIndex int
218
+ if len ( state . treePath ) > 0 {
219
+ entryNameStartIndex = len ( state . treePath ) + 1
218
220
}
219
- var entryPath string
220
- if index := strings .IndexRune (relPath , os .PathSeparator ); index >= 0 {
221
- entryPath = filepath .Join (state .treePath , relPath [:index ])
222
- } else {
223
- entryPath = path
221
+
222
+ if index := strings .IndexByte (entryPath [entryNameStartIndex :], '/' ); index >= 0 {
223
+ entryPath = entryPath [:entryNameStartIndex + index ]
224
224
}
225
+
225
226
if _ , ok := state .entries [entryPath ]; ! ok {
226
227
return nil
227
228
} else if _ , ok := state .commits [entryPath ]; ok {
228
229
return nil
229
230
}
231
+
232
+ var err error
230
233
state .commits [entryPath ], err = state .commit ()
231
234
return err
232
235
}
@@ -251,17 +254,17 @@ func getNextCommitInfos(state *getCommitInfoState) error {
251
254
state .nextCommit (lines [i ])
252
255
i ++
253
256
for ; i < len (lines ); i ++ {
254
- path := lines [i ]
255
- if path == "" {
257
+ entryPath := lines [i ]
258
+ if entryPath == "" {
256
259
break
257
260
}
258
- if path [0 ] == '"' {
259
- path , err = strconv .Unquote (path )
261
+ if entryPath [0 ] == '"' {
262
+ entryPath , err = strconv .Unquote (entryPath )
260
263
if err != nil {
261
264
return fmt .Errorf ("Unquote: %v" , err )
262
265
}
263
266
}
264
- state .update (path )
267
+ state .update (entryPath )
265
268
}
266
269
i ++ // skip blank line
267
270
if len (state .entries ) == len (state .commits ) {
@@ -284,9 +287,9 @@ func logCommand(exclusiveStartHash string, state *getCommitInfoState) *Command {
284
287
searchSize := (numRemainingEntries + 1 ) / 2
285
288
command = NewCommand ("log" , prettyLogFormat , "--name-only" ,
286
289
"-" + strconv .Itoa (searchSize ), commitHash , "--" )
287
- for path := range state .entries {
288
- if _ , ok := state .commits [path ]; ! ok {
289
- command .AddArguments (path )
290
+ for entryPath := range state .entries {
291
+ if _ , ok := state .commits [entryPath ]; ! ok {
292
+ command .AddArguments (entryPath )
290
293
}
291
294
}
292
295
} else {
0 commit comments