Skip to content
This repository was archived by the owner on Apr 12, 2019. It is now read-only.

Commit a34a13d

Browse files
ethantkoenigappleboy
authored andcommitted
Fix Windows path bug in GetCommitInfos (#67)
1 parent cd5d28e commit a34a13d

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

tree_entry.go

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ package git
66

77
import (
88
"fmt"
9-
"os"
10-
"path/filepath"
9+
"path"
1110
"sort"
1211
"strconv"
1312
"strings"
@@ -149,7 +148,7 @@ func (tes Entries) Sort() {
149148
// getCommitInfoState transient state for getting commit info for entries
150149
type getCommitInfoState struct {
151150
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
153152
lastCommitHash string
154153
lastCommit *Commit
155154
treePath string
@@ -160,7 +159,10 @@ type getCommitInfoState struct {
160159
func initGetCommitInfoState(entries Entries, headCommit *Commit, treePath string) *getCommitInfoState {
161160
entriesByPath := make(map[string]*TreeEntry, len(entries))
162161
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 = ""
164166
}
165167
return &getCommitInfoState{
166168
entries: entriesByPath,
@@ -180,7 +182,7 @@ func (tes Entries) GetCommitsInfo(commit *Commit, treePath string) ([][]interfac
180182

181183
commitsInfo := make([][]interface{}, len(tes))
182184
for i, entry := range tes {
183-
commit = state.commits[filepath.Join(treePath, entry.Name())]
185+
commit = state.commits[path.Join(treePath, entry.Name())]
184186
switch entry.Type {
185187
case ObjectCommit:
186188
subModuleURL := ""
@@ -211,22 +213,23 @@ func (state *getCommitInfoState) commit() (*Commit, error) {
211213
return state.lastCommit, err
212214
}
213215

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
218220
}
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]
224224
}
225+
225226
if _, ok := state.entries[entryPath]; !ok {
226227
return nil
227228
} else if _, ok := state.commits[entryPath]; ok {
228229
return nil
229230
}
231+
232+
var err error
230233
state.commits[entryPath], err = state.commit()
231234
return err
232235
}
@@ -251,17 +254,17 @@ func getNextCommitInfos(state *getCommitInfoState) error {
251254
state.nextCommit(lines[i])
252255
i++
253256
for ; i < len(lines); i++ {
254-
path := lines[i]
255-
if path == "" {
257+
entryPath := lines[i]
258+
if entryPath == "" {
256259
break
257260
}
258-
if path[0] == '"' {
259-
path, err = strconv.Unquote(path)
261+
if entryPath[0] == '"' {
262+
entryPath, err = strconv.Unquote(entryPath)
260263
if err != nil {
261264
return fmt.Errorf("Unquote: %v", err)
262265
}
263266
}
264-
state.update(path)
267+
state.update(entryPath)
265268
}
266269
i++ // skip blank line
267270
if len(state.entries) == len(state.commits) {
@@ -284,9 +287,9 @@ func logCommand(exclusiveStartHash string, state *getCommitInfoState) *Command {
284287
searchSize := (numRemainingEntries + 1) / 2
285288
command = NewCommand("log", prettyLogFormat, "--name-only",
286289
"-"+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)
290293
}
291294
}
292295
} else {

0 commit comments

Comments
 (0)