Skip to content

Commit 1e1db8b

Browse files
committedJan 13, 2017
Fixing windows compatability
There have been several compatability issues with Windows due to assumptions of a posix environment.
1 parent 9a6e212 commit 1e1db8b

6 files changed

+53
-14
lines changed
 

‎git.go

+8
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,14 @@ func (s *GitRepo) ExportDir(dir string) error {
371371
dir = dir + string(os.PathSeparator)
372372
}
373373

374+
// checkout-index on some systems, such as some Windows cases, does not
375+
// create the parent directory to export into if it does not exist. Explicitly
376+
// creating it.
377+
err := os.MkdirAll(dir, 0755)
378+
if err != nil {
379+
return NewLocalError("Unable to create directory", err, "")
380+
}
381+
374382
out, err := s.RunFromDir("git", "checkout-index", "-f", "-a", "--prefix="+dir)
375383
s.log(out)
376384
if err != nil {

‎repo_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestTypeSwitch(t *testing.T) {
4646
}
4747
}()
4848

49-
repo, err := NewSvnRepo("https://github.com/Masterminds/VCSTestRepo/trunk", tempDir+"/VCSTestRepo")
49+
repo, err := NewSvnRepo("https://github.com/Masterminds/VCSTestRepo/trunk", tempDir+string(os.PathSeparator)+"VCSTestRepo")
5050
if err != nil {
5151
t.Error(err)
5252
}
@@ -55,7 +55,7 @@ func TestTypeSwitch(t *testing.T) {
5555
t.Errorf("Unable to checkout SVN repo for repo switching tests. Err was %s", err)
5656
}
5757

58-
_, err = NewRepo("https://github.com/Masterminds/VCSTestRepo", tempDir+"/VCSTestRepo")
58+
_, err = NewRepo("https://github.com/Masterminds/VCSTestRepo", tempDir+string(os.PathSeparator)+"VCSTestRepo")
5959
if err != ErrWrongVCS {
6060
t.Errorf("Not detecting repo switch from SVN to Git")
6161
}

‎svn.go

+22-7
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
"time"
1112
)
@@ -76,9 +77,13 @@ func (s *SvnRepo) Get() error {
7677
remote := s.Remote()
7778
if strings.HasPrefix(remote, "/") {
7879
remote = "file://" + remote
80+
} else if runtime.GOOS == "windows" && filepath.VolumeName(remote) != "" {
81+
remote = "file:///" + remote
7982
}
8083
out, err := s.run("svn", "checkout", remote, s.LocalPath())
8184
if err != nil {
85+
fmt.Println(string(out))
86+
fmt.Println(err.Error())
8287
return NewRemoteError("Unable to get repository", err, string(out))
8388
}
8489
return nil
@@ -183,8 +188,8 @@ func (s *SvnRepo) Date() (time.Time, error) {
183188
if err != nil {
184189
return time.Time{}, NewLocalError("Unable to retrieve revision date", err, string(out))
185190
}
186-
const longForm = "2006-01-02T15:04:05.000000Z\n"
187-
t, err := time.Parse(longForm, string(out))
191+
const longForm = "2006-01-02T15:04:05.000000Z"
192+
t, err := time.Parse(longForm, strings.TrimSpace(string(out)))
188193
if err != nil {
189194
return time.Time{}, NewLocalError("Unable to retrieve revision date", err, string(out))
190195
}
@@ -193,14 +198,24 @@ func (s *SvnRepo) Date() (time.Time, error) {
193198

194199
// CheckLocal verifies the local location is an SVN repo.
195200
func (s *SvnRepo) CheckLocal() bool {
196-
sep := fmt.Sprintf("%c", os.PathSeparator)
197-
psplit := strings.Split(s.LocalPath(), sep)
198-
for i := 0; i < len(psplit); i++ {
199-
path := fmt.Sprintf("%s%s", sep, filepath.Join(psplit[0:(len(psplit)-(i))]...))
200-
if _, err := os.Stat(filepath.Join(path, ".svn")); err == nil {
201+
pth, err := filepath.Abs(s.LocalPath())
202+
if err != nil {
203+
s.log(err.Error())
204+
return false
205+
}
206+
207+
if _, err := os.Stat(filepath.Join(pth, ".svn")); err == nil {
208+
return true
209+
}
210+
211+
oldpth := pth
212+
for oldpth != pth {
213+
pth = filepath.Dir(pth)
214+
if _, err := os.Stat(filepath.Join(pth, ".svn")); err == nil {
201215
return true
202216
}
203217
}
218+
204219
return false
205220
}
206221

‎svn_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestSvn(t *testing.T) {
2828
}
2929
}()
3030

31-
repo, err := NewSvnRepo("https://github.com/Masterminds/VCSTestRepo/trunk", tempDir+"/VCSTestRepo")
31+
repo, err := NewSvnRepo("https://github.com/Masterminds/VCSTestRepo/trunk", tempDir+string(os.PathSeparator)+"VCSTestRepo")
3232
if err != nil {
3333
t.Error(err)
3434
}
@@ -41,7 +41,7 @@ func TestSvn(t *testing.T) {
4141
if repo.Remote() != "https://github.com/Masterminds/VCSTestRepo/trunk" {
4242
t.Error("Remote not set properly")
4343
}
44-
if repo.LocalPath() != tempDir+"/VCSTestRepo" {
44+
if repo.LocalPath() != tempDir+string(os.PathSeparator)+"VCSTestRepo" {
4545
t.Error("Local disk location not set properly")
4646
}
4747

@@ -300,8 +300,8 @@ func TestSvnPing(t *testing.T) {
300300

301301
func TestSvnInit(t *testing.T) {
302302
tempDir, err := ioutil.TempDir("", "go-vcs-svn-tests")
303-
remoteDir := tempDir + "/remoteDir"
304-
localDir := tempDir + "/localDir"
303+
remoteDir := tempDir + string(os.PathSeparator) + "remoteDir"
304+
localDir := tempDir + string(os.PathSeparator) + "localDir"
305305
if err != nil {
306306
t.Error(err)
307307
}

‎vcs_local_lookup.go

+8
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@ package vcs
22

33
import (
44
"os"
5+
"runtime"
6+
"strings"
57
)
68

79
// DetectVcsFromFS detects the type from the local path.
810
// Is there a better way to do this?
911
func DetectVcsFromFS(vcsPath string) (Type, error) {
1012

13+
// There are cases under windows that a path could start with a / and it needs
14+
// to be stripped. For example, a path such as /C:\foio\bar.
15+
if runtime.GOOS == "windows" && strings.HasPrefix(vcsPath, "/") {
16+
vcsPath = strings.TrimPrefix(vcsPath, "/")
17+
}
18+
1119
// When the local directory to the package doesn't exist
1220
// it's not yet downloaded so we can't detect the type
1321
// locally.

‎vcs_remote_lookup_test.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"io/ioutil"
55
"os"
66
"os/exec"
7+
"runtime"
78
"strings"
89
"testing"
910
)
@@ -92,7 +93,14 @@ func TestVCSFileLookup(t *testing.T) {
9293
t.Error(err)
9394
}
9495

95-
pth := "file://" + tempDir
96+
// On Windows it should be file:// followed by /C:\for\bar. That / before
97+
// the drive needs to be included in testing.
98+
var pth string
99+
if runtime.GOOS == "windows" {
100+
pth = "file:///" + tempDir
101+
} else {
102+
pth = "file://" + tempDir
103+
}
96104
ty, _, err := detectVcsFromRemote(pth)
97105

98106
if err != nil {

0 commit comments

Comments
 (0)
Please sign in to comment.