vcs_local_lookup.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package vcs
  2. import (
  3. "os"
  4. "runtime"
  5. "strings"
  6. )
  7. // DetectVcsFromFS detects the type from the local path.
  8. // Is there a better way to do this?
  9. func DetectVcsFromFS(vcsPath string) (Type, error) {
  10. // There are cases under windows that a path could start with a / and it needs
  11. // to be stripped. For example, a path such as /C:\foio\bar.
  12. if runtime.GOOS == "windows" && strings.HasPrefix(vcsPath, "/") {
  13. vcsPath = strings.TrimPrefix(vcsPath, "/")
  14. }
  15. // When the local directory to the package doesn't exist
  16. // it's not yet downloaded so we can't detect the type
  17. // locally.
  18. if _, err := os.Stat(vcsPath); os.IsNotExist(err) {
  19. return "", ErrCannotDetectVCS
  20. }
  21. separator := string(os.PathSeparator)
  22. // Walk through each of the different VCS types to see if
  23. // one can be detected. Do this is order of guessed popularity.
  24. if _, err := os.Stat(vcsPath + separator + ".git"); err == nil {
  25. return Git, nil
  26. }
  27. if _, err := os.Stat(vcsPath + separator + ".svn"); err == nil {
  28. return Svn, nil
  29. }
  30. if _, err := os.Stat(vcsPath + separator + ".hg"); err == nil {
  31. return Hg, nil
  32. }
  33. if _, err := os.Stat(vcsPath + separator + ".bzr"); err == nil {
  34. return Bzr, nil
  35. }
  36. // If one was not already detected than we default to not finding it.
  37. return "", ErrCannotDetectVCS
  38. }