|
|
|
@ -3,8 +3,9 @@ package giq
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
// "os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Giqi interface {
|
|
|
|
@ -36,54 +37,50 @@ func check(e error) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
func ifE(e string) string {
|
|
|
|
|
switch e {
|
|
|
|
|
case
|
|
|
|
|
"HEAD",
|
|
|
|
|
"objects",
|
|
|
|
|
"refs":
|
|
|
|
|
return "+"
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
// detectGitPath checks if there is either .git or
|
|
|
|
|
// the gitea's bare repoName.git/ and checks if those
|
|
|
|
|
// have HEAD, objects & refs. if it does it decides that
|
|
|
|
|
// /.git/ is not bare and /repoName.git/ is.
|
|
|
|
|
|
|
|
|
|
func detectGitPath(path string) (string, bool, error) {
|
|
|
|
|
path, err := filepath.Abs(path)
|
|
|
|
|
func detectGitPath(p string) (string, bool, error) {
|
|
|
|
|
path, err := filepath.Abs(p)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", false, err
|
|
|
|
|
}
|
|
|
|
|
gitDir := strings.Split(path, ".git/")
|
|
|
|
|
matches, _ := filepath.Glob(gitDir[0] + ".git/*")
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
threeE := ""
|
|
|
|
|
for _, match := range matches {
|
|
|
|
|
threeE += ifE(strings.ReplaceAll(match, gitDir[0]+".git/", ""))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ok, err := isGitDir(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", false, err
|
|
|
|
|
}
|
|
|
|
|
if ok {
|
|
|
|
|
return path, true, nil
|
|
|
|
|
isGit := false
|
|
|
|
|
if len(threeE) == 3 {
|
|
|
|
|
isGit = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if parent := filepath.Dir(path); parent == path {
|
|
|
|
|
return "", false, fmt.Errorf(".git not found")
|
|
|
|
|
if isGit {
|
|
|
|
|
repoPath := gitDir[0]
|
|
|
|
|
isBare := true
|
|
|
|
|
if (len(strings.Split(path, "/.git/"))) > 1 {
|
|
|
|
|
isBare = false
|
|
|
|
|
} else {
|
|
|
|
|
path = parent
|
|
|
|
|
repoPath = filepath.Join(gitDir[0] + ".git")
|
|
|
|
|
}
|
|
|
|
|
return repoPath, isBare, nil
|
|
|
|
|
} else {
|
|
|
|
|
return path, false, fmt.Errorf("It seems Git hook was run from non-git directory!")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|