a bit of cleaning up duplicating code in sysgit and gogit

This commit is contained in:
Marcell Mars 2021-04-18 01:52:41 +02:00
parent eb9c0b150e
commit fbe33cd3f6
4 changed files with 13 additions and 31 deletions

View file

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

View file

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

View file

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

View file

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