From f946748dd9dfff1d8eb9b2ba20766a90147bf1f2 Mon Sep 17 00:00:00 2001 From: Rusted2361 <93772165+Rusted2361@users.noreply.github.com> Date: Mon, 19 Jan 2026 18:37:35 +0500 Subject: [PATCH] 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 <70996046+kashifkhan0771@users.noreply.github.com> --- pkg/sources/git/git.go | 10 ++++++++-- pkg/sources/git/git_test.go | 17 +++++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/pkg/sources/git/git.go b/pkg/sources/git/git.go index 3ee18e21a..2aada8b02 100644 --- a/pkg/sources/git/git.go +++ b/pkg/sources/git/git.go @@ -434,9 +434,12 @@ func normalizeFileURI(uri *url.URL) (*url.URL, error) { return nil, fmt.Errorf("failed to resolve absolute path for %q: %w", rawPath, err) } + // Convert to forward slashes (for Windows compatibility) + normalizedPath := filepath.ToSlash(absPath) + normalizedURI := &url.URL{ Scheme: "file", - Path: absPath, + Path: normalizedPath, } return normalizedURI, nil @@ -1341,7 +1344,10 @@ func PrepareRepo(ctx context.Context, uriString, clonePath string, trustLocalGit if !isRepoBare(path) { // Only copy index file for non-bare clones from working directory repos. This is used to see staged changes. // Note: To scan **un**staged changes in the future, we'd need to set core.worktree to the original path. - originalIndexPath := filepath.Join(strings.TrimPrefix(normalizedURI.String(), "file://"), gitDirName, "index") + uriPath := normalizedURI.Path + + originalIndexPath := filepath.Join(uriPath, gitDirName, "index") + clonedIndexPath := filepath.Join(path, gitDirName, "index") indexData, err := os.ReadFile(originalIndexPath) diff --git a/pkg/sources/git/git_test.go b/pkg/sources/git/git_test.go index f20ec503f..744218b83 100644 --- a/pkg/sources/git/git_test.go +++ b/pkg/sources/git/git_test.go @@ -6,6 +6,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strings" "testing" "time" @@ -861,7 +862,7 @@ func TestNormalizeFileURI(t *testing.T) { { name: "absolute file URI unchanged", input: "file:///absolute/path", - expected: "file:///absolute/path", + expected: "", }, { name: "relative file URI with current directory", @@ -908,6 +909,15 @@ func TestNormalizeFileURI(t *testing.T) { var expected string switch tt.name { + case "absolute file URI unchanged": + // On Windows, absolute paths get drive letter prepended + // On Unix, they remain as-is + if runtime.GOOS == "windows" { + expectedPath, _ := filepath.Abs("/absolute/path") + expected = "file://" + expectedPath + } else { + expected = "file:///absolute/path" + } case "relative file URI with current directory": expected = "file://" + cwd case "relative file URI with subdirectory": @@ -920,7 +930,10 @@ func TestNormalizeFileURI(t *testing.T) { default: expected = tt.expected } - + // Normalize slashes for Windows comparison + if runtime.GOOS == "windows" { + expected = filepath.ToSlash(expected) + } assert.Equal(t, expected, result.String()) }) }