21.06.01 fixed regression with detectGitPath
This commit is contained in:
parent
c3f8d5d059
commit
5b06753927
5 changed files with 64 additions and 47 deletions
21
LICENSE
Normal file
21
LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2020 Marcell Mars
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
75
giq/giq.go
75
giq/giq.go
|
@ -3,8 +3,9 @@ package giq
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
// "os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Giqi interface {
|
type Giqi interface {
|
||||||
|
@ -36,54 +37,50 @@ func check(e error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func isGitDir(path string) (bool, error) {
|
func ifE(e string) string {
|
||||||
markers := []string{"HEAD", "objects", "refs"}
|
switch e {
|
||||||
|
case
|
||||||
for _, marker := range markers {
|
"HEAD",
|
||||||
_, err := os.Stat(filepath.Join(path, marker))
|
"objects",
|
||||||
if err == nil {
|
"refs":
|
||||||
continue
|
return "+"
|
||||||
}
|
|
||||||
if !os.IsNotExist(err) {
|
|
||||||
return false, err
|
|
||||||
} else {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return ""
|
||||||
return true, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func detectGitPath(path string) (string, bool, error) {
|
// detectGitPath checks if there is either .git or
|
||||||
path, err := filepath.Abs(path)
|
// 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 {
|
if err != nil {
|
||||||
return "", false, err
|
return "", false, err
|
||||||
}
|
}
|
||||||
|
gitDir := strings.Split(path, ".git/")
|
||||||
|
matches, _ := filepath.Glob(gitDir[0] + ".git/*")
|
||||||
|
|
||||||
for {
|
threeE := ""
|
||||||
fi, err := os.Stat(filepath.Join(path, ".git"))
|
for _, match := range matches {
|
||||||
if err == nil {
|
threeE += ifE(strings.ReplaceAll(match, gitDir[0]+".git/", ""))
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
ok, err := isGitDir(path)
|
isGit := false
|
||||||
if err != nil {
|
if len(threeE) == 3 {
|
||||||
return "", false, err
|
isGit = true
|
||||||
}
|
}
|
||||||
if ok {
|
|
||||||
return path, true, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if parent := filepath.Dir(path); parent == path {
|
if isGit {
|
||||||
return "", false, fmt.Errorf(".git not found")
|
repoPath := gitDir[0]
|
||||||
|
isBare := true
|
||||||
|
if (len(strings.Split(path, "/.git/"))) > 1 {
|
||||||
|
isBare = false
|
||||||
} else {
|
} 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!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
13
main.go
13
main.go
|
@ -15,7 +15,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
version = "21.04.05"
|
version = "21.06.01"
|
||||||
logLines = ""
|
logLines = ""
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -64,8 +64,8 @@ func gitExePath(hook *Hook) string {
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
logLines := fmt.Sprintf("~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\nSANDPOINTS GITHOOK (%s)\n\n", version)
|
logLines := fmt.Sprintf("\n~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\nSANDPOINTS GITHOOK (%s)\n\n", version)
|
||||||
|
fmt.Println(logLines)
|
||||||
// init global struct/variables
|
// init global struct/variables
|
||||||
var hook *Hook
|
var hook *Hook
|
||||||
var hugo *Hugo
|
var hugo *Hugo
|
||||||
|
@ -130,9 +130,10 @@ func main() {
|
||||||
logLines = logLines + fmt.Sprintf("\n~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\nTotal githook time: %d ms\n~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\n", durationMillseconds)
|
logLines = logLines + fmt.Sprintf("\n~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\nTotal githook time: %d ms\n~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\n", durationMillseconds)
|
||||||
logLines = logLines + startTime.Format(time.RFC822) + "\n~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\n"
|
logLines = logLines + startTime.Format(time.RFC822) + "\n~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\n"
|
||||||
|
|
||||||
// logLines = logLines + "\n####################################\n"
|
logLines = logLines + "\n####################################\n"
|
||||||
// logLines = logLines + fmt.Sprintf("%#v", hugo)
|
// logLines = logLines + fmt.Sprintf("%#v\n", hugo)
|
||||||
// logLines = logLines + fmt.Sprintf("%#v", hook)
|
// logLines = logLines + fmt.Sprintf("%#v\n", hook)
|
||||||
|
// logLines = logLines + fmt.Sprintf("%#v", git)
|
||||||
|
|
||||||
writeLastCommitLog(logLines, git.IsBare, hugo, hook)
|
writeLastCommitLog(logLines, git.IsBare, hugo, hook)
|
||||||
fmt.Println(logLines)
|
fmt.Println(logLines)
|
||||||
|
|
BIN
sphook
BIN
sphook
Binary file not shown.
2
vendor/modules.txt
vendored
2
vendor/modules.txt
vendored
|
@ -459,8 +459,6 @@ github.com/jstemmer/go-junit-report/parser
|
||||||
github.com/kevinburke/ssh_config
|
github.com/kevinburke/ssh_config
|
||||||
# github.com/kyokomi/emoji/v2 v2.2.8
|
# github.com/kyokomi/emoji/v2 v2.2.8
|
||||||
github.com/kyokomi/emoji/v2
|
github.com/kyokomi/emoji/v2
|
||||||
# github.com/magefile/mage v1.11.0
|
|
||||||
## explicit
|
|
||||||
# github.com/magiconair/properties v1.8.5
|
# github.com/magiconair/properties v1.8.5
|
||||||
## explicit
|
## explicit
|
||||||
github.com/magiconair/properties
|
github.com/magiconair/properties
|
||||||
|
|
Loading…
Add table
Reference in a new issue