SandpointsGitHook/giq/giq.go

90 lines
1.5 KiB
Go
Raw Normal View History

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, 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
}
}
}