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]>
This commit is contained in:
co-authored by
Kashif Khan
parent
3876bea3ef
commit
f946748dd9
@@ -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)
|
||||
|
||||
@@ -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())
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user