You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
1.6 KiB
Go

package giq
import (
"fmt"
"log"
// "os"
"path/filepath"
"strings"
)
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 ifE(e string) string {
switch e {
case
"HEAD",
"objects",
"refs":
return "+"
}
return ""
}
// 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(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/*")
threeE := ""
for _, match := range matches {
threeE += ifE(strings.ReplaceAll(match, gitDir[0]+".git/", ""))
}
isGit := false
if len(threeE) == 3 {
isGit = true
}
if isGit {
repoPath := gitDir[0]
isBare := true
if (len(strings.Split(path, "/.git/"))) > 1 {
isBare = false
} else {
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!")
}
}