github.com/komosa/bug@v0.3.1/scm/Detect.go (about) 1 package scm 2 3 import ( 4 "github.com/driusan/bug/bugs" 5 "os" 6 "strings" 7 ) 8 9 type SCMNotFound struct{} 10 11 func (a SCMNotFound) Error() string { 12 return "No SCM found" 13 } 14 15 func walkAndSearch(startpath string, dirnames []string) (fullpath, scmtype string) { 16 for _, scmtype := range dirnames { 17 if dirinfo, err := os.Stat(startpath + "/" + scmtype); err == nil && dirinfo.IsDir() { 18 return startpath + "/" + scmtype, scmtype 19 } 20 } 21 22 pieces := strings.Split(startpath, "/") 23 24 for i := len(pieces); i > 0; i -= 1 { 25 dir := strings.Join(pieces[0:i], "/") 26 for _, scmtype := range dirnames { 27 if dirinfo, err := os.Stat(dir + "/" + scmtype); err == nil && dirinfo.IsDir() { 28 return dir + "/" + scmtype, scmtype 29 } 30 } 31 } 32 return "", "" 33 } 34 35 func DetectSCM(options map[string]bool) (SCMHandler, bugs.Directory, error) { 36 // First look for a Git directory 37 wd, _ := os.Getwd() 38 39 dirFound, scmtype := walkAndSearch(wd, []string{".git", ".hg"}) 40 if dirFound != "" && scmtype == ".git" { 41 if val, exists := options["autoclose"]; exists && val == true { 42 return GitManager{Autoclose: true}, bugs.Directory(dirFound), nil 43 } 44 return GitManager{Autoclose: false}, bugs.Directory(dirFound), nil 45 } 46 if dirFound != "" && scmtype == ".hg" { 47 return HgManager{}, bugs.Directory(dirFound), nil 48 } 49 50 return nil, "", SCMNotFound{} 51 }