git-gohooks/main.go
Ingo Oeser d81ea3f35c filepath love
* use filepath to get correct result of file path operation on every OS.
 * use filepath.Walk to get a better idea what is being done here
 * simpler way to get the basename of a program; also works on Windows
2015-08-22 23:45:53 +02:00

39 lines
741 B
Go

package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/mthie/git-gohooks/general"
)
func main() {
gitroot, _ := filepath.Abs(filepath.Dir(general.GetGitRoot()))
hookBase := filepath.Base(os.Args[0])
hookPrefix := fmt.Sprintf("%s_", hookBase)
os.Chdir(gitroot)
filepath.Walk(gitroot, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return filepath.SkipDir
}
file := info.Name()
if strings.HasPrefix(file, hookPrefix) {
result, errCode := general.RunCommand(filepath.Join(gitroot, "/.git/hooks", file))
if errCode != 0 {
fmt.Fprintf(os.Stderr, "Error: %s", result)
os.Exit(errCode)
return nil
}
}
return nil
})
}