Skip to content

Commit f946748

Browse files
Fix/issue 4578 path normalization for unix and windows (#4614)
* Fix Windows file:// URI normalization and index path handling * add condition to trim leading slash only incase of windows paths * access path from URL object and identify platform using goos * simplified path logic * added test cases for windows powershell & bash --------- Co-authored-by: Kashif Khan <[email protected]>
1 parent 3876bea commit f946748

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

pkg/sources/git/git.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,12 @@ func normalizeFileURI(uri *url.URL) (*url.URL, error) {
434434
return nil, fmt.Errorf("failed to resolve absolute path for %q: %w", rawPath, err)
435435
}
436436

437+
// Convert to forward slashes (for Windows compatibility)
438+
normalizedPath := filepath.ToSlash(absPath)
439+
437440
normalizedURI := &url.URL{
438441
Scheme: "file",
439-
Path: absPath,
442+
Path: normalizedPath,
440443
}
441444

442445
return normalizedURI, nil
@@ -1341,7 +1344,10 @@ func PrepareRepo(ctx context.Context, uriString, clonePath string, trustLocalGit
13411344
if !isRepoBare(path) {
13421345
// Only copy index file for non-bare clones from working directory repos. This is used to see staged changes.
13431346
// Note: To scan **un**staged changes in the future, we'd need to set core.worktree to the original path.
1344-
originalIndexPath := filepath.Join(strings.TrimPrefix(normalizedURI.String(), "file://"), gitDirName, "index")
1347+
uriPath := normalizedURI.Path
1348+
1349+
originalIndexPath := filepath.Join(uriPath, gitDirName, "index")
1350+
13451351
clonedIndexPath := filepath.Join(path, gitDirName, "index")
13461352

13471353
indexData, err := os.ReadFile(originalIndexPath)

pkg/sources/git/git_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"os/exec"
88
"path/filepath"
9+
"runtime"
910
"strings"
1011
"testing"
1112
"time"
@@ -861,7 +862,7 @@ func TestNormalizeFileURI(t *testing.T) {
861862
{
862863
name: "absolute file URI unchanged",
863864
input: "file:///absolute/path",
864-
expected: "file:///absolute/path",
865+
expected: "",
865866
},
866867
{
867868
name: "relative file URI with current directory",
@@ -908,6 +909,15 @@ func TestNormalizeFileURI(t *testing.T) {
908909

909910
var expected string
910911
switch tt.name {
912+
case "absolute file URI unchanged":
913+
// On Windows, absolute paths get drive letter prepended
914+
// On Unix, they remain as-is
915+
if runtime.GOOS == "windows" {
916+
expectedPath, _ := filepath.Abs("/absolute/path")
917+
expected = "file://" + expectedPath
918+
} else {
919+
expected = "file:///absolute/path"
920+
}
911921
case "relative file URI with current directory":
912922
expected = "file://" + cwd
913923
case "relative file URI with subdirectory":
@@ -920,7 +930,10 @@ func TestNormalizeFileURI(t *testing.T) {
920930
default:
921931
expected = tt.expected
922932
}
923-
933+
// Normalize slashes for Windows comparison
934+
if runtime.GOOS == "windows" {
935+
expected = filepath.ToSlash(expected)
936+
}
924937
assert.Equal(t, expected, result.String())
925938
})
926939
}

0 commit comments

Comments
 (0)