package giq import ( "fmt" "log" "os" "path/filepath" ) type Giqi interface { Whoami() string GitStruct(string, string, string, string) Git AddBareWorktree(string, string, string, string) RemoveBareWorktree(string, string, string, string) } type Git struct { RepoPath string Worktree string TmpRepoPath string IsBare bool Branch string PreviousCommit string LastCommit string IndexPath string Publish bool } type Gogit struct{} type Sysgit struct{} func check(e error) { if e != nil { log.Fatal(e) panic(e) } } func isGitDir(path string) (bool, error) { markers := []string{"HEAD", "objects", "refs"} for _, marker := range markers { _, err := os.Stat(filepath.Join(path, marker)) if err == nil { continue } if !os.IsNotExist(err) { return false, err } else { return false, nil } } return true, nil } func detectGitPath(path string) (string, bool, error) { path, err := filepath.Abs(path) if err != nil { return "", false, err } for { fi, err := os.Stat(filepath.Join(path, ".git")) if err == nil { if !fi.IsDir() { return "", false, fmt.Errorf(".git exist but is not a directory") } return path, false, nil } if !os.IsNotExist(err) { return "", false, err } ok, err := isGitDir(path) if err != nil { return "", false, err } if ok { return path, true, nil } if parent := filepath.Dir(path); parent == path { return "", false, fmt.Errorf(".git not found") } else { path = parent } } }