Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 4b6f4c8

Browse files
committed
test: git alternates: cross-platform support
1 parent 7104281 commit 4b6f4c8

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

storage/filesystem/internal/dotgit/dotgit.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -780,14 +780,18 @@ func (d *DotGit) Alternates() ([]*DotGit, error) {
780780
for scanner.Scan() {
781781
path := scanner.Text()
782782
if !filepath.IsAbs(path) {
783+
// For relative paths, we can perform an internal conversion to
784+
// slash so that they work cross-platform.
785+
slashPath := filepath.ToSlash(path)
783786
// If the path is not absolute, it must be relative to object
784787
// database (.git/objects/info).
785788
// https://www.kernel.org/pub/software/scm/git/docs/gitrepository-layout.html
786789
// Hence, derive a path relative to DotGit's root.
787790
// "../../../reponame/.git/" -> "../../reponame/.git"
788791
// Remove the first ../
789-
relpath := filepath.Join(strings.Split(path, "/")[1:]...)
790-
path = filepath.Join(d.fs.Root(), relpath)
792+
relpath := filepath.Join(strings.Split(slashPath, "/")[1:]...)
793+
normalPath := filepath.FromSlash(relpath)
794+
path = filepath.Join(d.fs.Root(), normalPath)
791795
}
792796
fs := osfs.New(filepath.Dir(path))
793797
alternates = append(alternates, New(fs))

storage/filesystem/internal/dotgit/dotgit_test.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io/ioutil"
66
"os"
77
"path/filepath"
8+
"runtime"
89
"strings"
910
"testing"
1011

@@ -635,19 +636,35 @@ func (s *SuiteDotGit) TestAlternates(c *C) {
635636
c.Assert(err, IsNil)
636637

637638
// Multiple alternates.
638-
content := []byte("/Users/username/rep1//.git/objects\n../../../rep2//.git/objects")
639+
var strContent string
640+
if runtime.GOOS == "windows" {
641+
strContent = "C:\\Users\\username\\repo1\\.git\\objects\r\n..\\..\\..\\rep2\\.git\\objects"
642+
} else {
643+
strContent = "/Users/username/rep1//.git/objects\n../../../rep2//.git/objects"
644+
}
645+
content := []byte(strContent)
639646
f.Write(content)
640647
f.Close()
641648

642649
dotgits, err := dir.Alternates()
643650
c.Assert(err, IsNil)
644-
c.Assert(dotgits[0].fs.Root(), Equals, "/Users/username/rep1/.git")
651+
if runtime.GOOS == "windows" {
652+
c.Assert(dotgits[0].fs.Root(), Equals, "C:\\Users\\username\\repo1\\.git")
653+
} else {
654+
c.Assert(dotgits[0].fs.Root(), Equals, "/Users/username/rep1/.git")
655+
}
645656

646657
// For relative path:
647658
// /some/absolute/path/to/dot-git -> /some/absolute/path
648-
pathx := strings.Split(tmp, "/")
659+
pathx := strings.Split(tmp, string(filepath.Separator))
649660
pathx = pathx[:len(pathx)-2]
650-
resolvedPath := filepath.Join(pathx...)
661+
// Use string.Join() to avoid malformed absolutepath on windows
662+
// C:Users\\User\\... instead of C:\\Users\\appveyor\\... .
663+
resolvedPath := strings.Join(pathx, string(filepath.Separator))
651664
// Append the alternate path to the resolvedPath
652-
c.Assert(dotgits[1].fs.Root(), Equals, filepath.Join("/", resolvedPath, "rep2", ".git"))
665+
expectedPath := filepath.Join(string(filepath.Separator), resolvedPath, "rep2", ".git")
666+
if runtime.GOOS == "windows" {
667+
expectedPath = filepath.Join(resolvedPath, "rep2", ".git")
668+
}
669+
c.Assert(dotgits[1].fs.Root(), Equals, expectedPath)
653670
}

0 commit comments

Comments
 (0)