File: vcs_local_lookup.go

package info (click to toggle)
golang-github-masterminds-vcs-dev 1.12.0-4.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 224 kB
  • sloc: makefile: 37
file content (46 lines) | stat: -rw-r--r-- 1,291 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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.
	if _, err := os.Stat(vcsPath); os.IsNotExist(err) {
		return "", ErrCannotDetectVCS
	}

	separator := string(os.PathSeparator)

	// Walk through each of the different VCS types to see if
	// one can be detected. Do this is order of guessed popularity.
	if _, err := os.Stat(vcsPath + separator + ".git"); err == nil {
		return Git, nil
	}
	if _, err := os.Stat(vcsPath + separator + ".svn"); err == nil {
		return Svn, nil
	}
	if _, err := os.Stat(vcsPath + separator + ".hg"); err == nil {
		return Hg, nil
	}
	if _, err := os.Stat(vcsPath + separator + ".bzr"); err == nil {
		return Bzr, nil
	}

	// If one was not already detected than we default to not finding it.
	return "", ErrCannotDetectVCS

}