a bit of cleaning up duplicating code in sysgit and gogit

master
Marcell Mars 3 years ago
parent eb9c0b150e
commit fbe33cd3f6

@ -54,34 +54,34 @@ func isGitDir(path string) (bool, error) {
return true, nil
}
func detectGitPath(path string) (string, error) {
func detectGitPath(path string) (string, bool, error) {
path, err := filepath.Abs(path)
if err != nil {
return "", err
return "", false, 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 "", false, fmt.Errorf(".git exist but is not a directory")
}
return filepath.Join(path, ".git"), nil
return path, false, nil
}
if !os.IsNotExist(err) {
return "", err
return "", false, err
}
ok, err := isGitDir(path)
if err != nil {
return "", err
return "", false, err
}
if ok {
return path, nil
return path, true, nil
}
if parent := filepath.Dir(path); parent == path {
return "", fmt.Errorf(".git not found")
return "", false, fmt.Errorf(".git not found")
} else {
path = parent
}

@ -5,7 +5,6 @@ import (
"io"
"math/rand"
"os"
"path"
"path/filepath"
"strings"
@ -94,17 +93,9 @@ func (g *Gogit) GitStruct(hookPath, hookContext, hookStdinput, gexe string) Git
var git Git
_ = gexe
gitBase, err := detectGitPath(hookPath)
repoPath, gitIsBare, err := detectGitPath(hookPath)
check(err)
repoPath := gitBase
git.IsBare = true
base := path.Base(gitBase)
if base == ".git" {
git.IsBare = false
repoPath = strings.Replace(gitBase, ".git", "", 1)
}
git.IsBare = gitIsBare
r, err := gogit.PlainOpen(repoPath)
check(err)

@ -6,7 +6,6 @@ import (
"math/rand"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"time"
@ -75,17 +74,9 @@ func isPublished(gitRepoPath, gitIndexPath, prevCommit, lastCommit string) bool
func (g *Sysgit) GitStruct(hookPath, hookContext, hookStdinput, gexe string) Git {
var git Git
gitBase, err := detectGitPath(hookPath)
repoPath, gitIsBare, err := detectGitPath(hookPath)
check(err)
repoPath := gitBase
git.IsBare = true
base := path.Base(gitBase)
if base == ".git" {
git.IsBare = false
repoPath = strings.Replace(gitBase, ".git", "", 1)
}
git.IsBare = gitIsBare
if hookContext == "PostReceive" {
revs := strings.Fields(hookStdinput)

@ -15,7 +15,7 @@ import (
)
var (
version = "21.04.03"
version = "21.04.04"
logLines = ""
)

Loading…
Cancel
Save