Skip to content

Commit

Permalink
Fixing windows compatability
Browse files Browse the repository at this point in the history
There have been several compatability issues with Windows due to
assumptions of a posix environment.
  • Loading branch information
mattfarina committed Jan 13, 2017
1 parent 9a6e212 commit 431ae8b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 15 deletions.
8 changes: 8 additions & 0 deletions git.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,14 @@ func (s *GitRepo) ExportDir(dir string) error {
dir = dir + string(os.PathSeparator)
}

// checkout-index on some systems, such as some Windows cases, does not
// create the parent directory to export into if it does not exist. Explicitly
// creating it.
err := os.MkdirAll(dir, 0755)
if err != nil {
return NewLocalError("Unable to create directory", err, "")
}

out, err := s.RunFromDir("git", "checkout-index", "-f", "-a", "--prefix="+dir)
s.log(out)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestTypeSwitch(t *testing.T) {
}
}()

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

_, err = NewRepo("https://github.com/Masterminds/VCSTestRepo", tempDir+"/VCSTestRepo")
_, err = NewRepo("https://github.com/Masterminds/VCSTestRepo", tempDir+string(os.PathSeparator)+"VCSTestRepo")
if err != ErrWrongVCS {
t.Errorf("Not detecting repo switch from SVN to Git")
}
Expand Down
29 changes: 22 additions & 7 deletions svn.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
)
Expand Down Expand Up @@ -76,9 +77,13 @@ func (s *SvnRepo) Get() error {
remote := s.Remote()
if strings.HasPrefix(remote, "/") {
remote = "file://" + remote
} else if runtime.GOOS == "windows" && filepath.VolumeName(remote) != "" {
remote = "file:///" + remote
}
out, err := s.run("svn", "checkout", remote, s.LocalPath())
if err != nil {
fmt.Println(string(out))
fmt.Println(err.Error())
return NewRemoteError("Unable to get repository", err, string(out))
}
return nil
Expand Down Expand Up @@ -183,8 +188,8 @@ func (s *SvnRepo) Date() (time.Time, error) {
if err != nil {
return time.Time{}, NewLocalError("Unable to retrieve revision date", err, string(out))
}
const longForm = "2006-01-02T15:04:05.000000Z\n"
t, err := time.Parse(longForm, string(out))
const longForm = "2006-01-02T15:04:05.000000Z"
t, err := time.Parse(longForm, strings.TrimSpace(string(out)))
if err != nil {
return time.Time{}, NewLocalError("Unable to retrieve revision date", err, string(out))
}
Expand All @@ -193,14 +198,24 @@ func (s *SvnRepo) Date() (time.Time, error) {

// CheckLocal verifies the local location is an SVN repo.
func (s *SvnRepo) CheckLocal() bool {
sep := fmt.Sprintf("%c", os.PathSeparator)
psplit := strings.Split(s.LocalPath(), sep)
for i := 0; i < len(psplit); i++ {
path := fmt.Sprintf("%s%s", sep, filepath.Join(psplit[0:(len(psplit)-(i))]...))
if _, err := os.Stat(filepath.Join(path, ".svn")); err == nil {
pth, err := filepath.Abs(s.LocalPath())
if err != nil {
s.log(err.Error())
return false
}

if _, err := os.Stat(filepath.Join(pth, ".svn")); err == nil {
return true
}

oldpth := pth
for oldpth != pth {
pth = filepath.Dir(pth)
if _, err := os.Stat(filepath.Join(pth, ".svn")); err == nil {
return true
}
}

return false
}

Expand Down
8 changes: 4 additions & 4 deletions svn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestSvn(t *testing.T) {
}
}()

repo, err := NewSvnRepo("https://github.com/Masterminds/VCSTestRepo/trunk", tempDir+"/VCSTestRepo")
repo, err := NewSvnRepo("https://github.com/Masterminds/VCSTestRepo/trunk", tempDir+string(os.PathSeparator)+"VCSTestRepo")
if err != nil {
t.Error(err)
}
Expand All @@ -41,7 +41,7 @@ func TestSvn(t *testing.T) {
if repo.Remote() != "https://github.com/Masterminds/VCSTestRepo/trunk" {
t.Error("Remote not set properly")
}
if repo.LocalPath() != tempDir+"/VCSTestRepo" {
if repo.LocalPath() != tempDir+string(os.PathSeparator)+"VCSTestRepo" {
t.Error("Local disk location not set properly")
}

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

func TestSvnInit(t *testing.T) {
tempDir, err := ioutil.TempDir("", "go-vcs-svn-tests")
remoteDir := tempDir + "/remoteDir"
localDir := tempDir + "/localDir"
remoteDir := tempDir + string(os.PathSeparator) + "remoteDir"
localDir := tempDir + string(os.PathSeparator) + "localDir"
if err != nil {
t.Error(err)
}
Expand Down
8 changes: 8 additions & 0 deletions vcs_local_lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ package vcs

import (
"os"
"runtime"
"strings"
)

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

// There are cases under windows that a path could start with a / and it needs
// to be stripped. For example, a path such as /C:\foio\bar.
if runtime.GOOS == "windows" && strings.HasPrefix(vcsPath, "/") {
vcsPath = strings.TrimPrefix(vcsPath, "/")
}

// When the local directory to the package doesn't exist
// it's not yet downloaded so we can't detect the type
// locally.
Expand Down
12 changes: 10 additions & 2 deletions vcs_remote_lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"runtime"
"strings"
"testing"
)
Expand Down Expand Up @@ -87,12 +88,19 @@ func TestVCSFileLookup(t *testing.T) {
}
}()

_, err = exec.Command("git", "init", tempDir).CombinedOutput()
out, err := exec.Command("git", "init", tempDir).CombinedOutput()
if err != nil {
t.Error(err)
}

pth := "file://" + tempDir
// On Windows it should be file:// followed by /C:\for\bar. That / before
// the drive needs to be included in testing.
var pth string
if runtime.GOOS == "windows" {
pth = "file:///" + tempDir
} else {
pth = "file://" + tempDir
}
ty, _, err := detectVcsFromRemote(pth)

if err != nil {
Expand Down

0 comments on commit 431ae8b

Please sign in to comment.