Skip to content

Commit 6862035

Browse files
authoredAug 7, 2024··
fix: normalise path separators on Windows to fix sub-package tags (#165)
The git cli and the go os library return representations of the same path using different path separators, so the comparison used here returned false, and the absolute path of the repo was set as the default for the pattern arg. e.g.: > git rev-parse --show-toplevel C:/Users/Doug/Code/myrepo os.getWd() C:\\Users\\Doug\\Code\\myrepo Using filepath.Clean() on both paths before using them normalises the slashes to a single "\"
1 parent d3987e0 commit 6862035

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed
 

‎main.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"path"
7+
"path/filepath"
78
"runtime/debug"
89
"strings"
910

@@ -53,11 +54,18 @@ func defaults(flag string) string {
5354

5455
cwd, wdErr := os.Getwd()
5556
gitRoot, grErr := git.Root()
56-
if wdErr == nil && grErr == nil && cwd != gitRoot {
57-
prefix := strings.TrimPrefix(cwd, gitRoot)
58-
prefix = strings.TrimPrefix(prefix, string(os.PathSeparator))
59-
prefix = strings.TrimSuffix(prefix, string(os.PathSeparator))
60-
return path.Join(prefix, pat)
57+
58+
if wdErr == nil && grErr == nil {
59+
60+
cwd = filepath.Clean(cwd)
61+
gitRoot = filepath.Clean(gitRoot)
62+
63+
if cwd != gitRoot {
64+
prefix := strings.TrimPrefix(cwd, gitRoot)
65+
prefix = strings.TrimPrefix(prefix, string(os.PathSeparator))
66+
prefix = strings.TrimSuffix(prefix, string(os.PathSeparator))
67+
return path.Join(prefix, pat)
68+
}
6169
}
6270

6371
return def

0 commit comments

Comments
 (0)
Please sign in to comment.