2021-03-20 15:20:39 -07:00
|
|
|
package giq
|
|
|
|
|
2021-04-16 17:18:18 -07:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
2021-03-20 15:20:39 -07:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2021-04-16 17:18:18 -07:00
|
|
|
|
|
|
|
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, error) {
|
|
|
|
path, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
fi, err := os.Stat(filepath.Join(path, ".git"))
|
|
|
|
if err == nil {
|
|
|
|
if !fi.IsDir() {
|
|
|
|
return "", fmt.Errorf(".git exist but is not a directory")
|
|
|
|
}
|
|
|
|
return filepath.Join(path, ".git"), nil
|
|
|
|
}
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
ok, err := isGitDir(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if parent := filepath.Dir(path); parent == path {
|
|
|
|
return "", fmt.Errorf(".git not found")
|
|
|
|
} else {
|
|
|
|
path = parent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|