diff --git a/bots-common/git_utils.go b/bots-common/git_utils.go index 7de381b..0b312be 100644 --- a/bots-common/git_utils.go +++ b/bots-common/git_utils.go @@ -3,6 +3,7 @@ package common import ( "fmt" "io" + "log" "os" "os/exec" "path" @@ -11,7 +12,14 @@ import ( "sync" ) -//func (h *RequestHandler) ProcessBranchList() []string { +type GitHandler struct { + DebugLogger bool + + GitPath string + GitCommiter string +} + +//func (h *GitHandler) ProcessBranchList() []string { // if h.HasError() { // return make([]string, 0) // } @@ -119,22 +127,15 @@ func findGitDir(p string) (string, error) { return "", fmt.Errorf("Can't find git subdirectory in '%s'", p) } -func (e *RequestHandler) GitBranchHead(gitDir, branchName string) (string, error) { - if e.HasError() { - return "", e.Error - } - +func (e *GitHandler) GitBranchHead(gitDir, branchName string) (string, error) { path, err := findGitDir(path.Join(e.GitPath, gitDir)) if err != nil { - e.ErrLogger.Printf("Error identifying gitdir in `%s`: %v\n", gitDir, err) - e.Error = err + return "", fmt.Errorf("Error identifying gitdir in `%s`: %w", gitDir, err) } refs, err := processRefs(path) if err != nil { - e.ErrLogger.Printf("Error finding branches (%s): %v\n", branchName, err) - e.Error = err - return "", e.Error + return "", fmt.Errorf("Error finding branches (%s): %w\n", branchName, err) } for _, ref := range refs { @@ -143,9 +144,7 @@ func (e *RequestHandler) GitBranchHead(gitDir, branchName string) (string, error } } - e.Error = fmt.Errorf("Can't find default remote branch: %s", branchName) - e.ErrLogger.Println(e.Error.Error()) - return "", e.Error + return "", fmt.Errorf("Can't find default remote branch: %s", branchName) } type ExecStream interface { @@ -154,18 +153,12 @@ type ExecStream interface { GitExec(cwd string, param ...string) ExecStream } -func (e *RequestHandler) Close() { - if e.GitPath == "" { - return +func (e *GitHandler) Close() error { + if err := os.RemoveAll(e.GitPath); err != nil { + return err } - - e.Error = os.RemoveAll(e.GitPath) e.GitPath = "" - return -} - -func (e *RequestHandler) HasError() bool { - return e.Error != nil + return nil } type writeFunc func(data []byte) (int, error) @@ -184,12 +177,7 @@ func (h writeFunc) Close() error { return err } - -func (e *RequestHandler) GitExec(cwd string, params ...string) ExecStream { - if e.Error != nil { - return e - } - +func (e *GitHandler) GitExec(cwd string, params ...string) ExecStream { cmd := exec.Command("/usr/bin/git", params...) cmd.Env = []string{ "GIT_CEILING_DIRECTORIES=" + e.GitPath, @@ -200,18 +188,12 @@ func (e *RequestHandler) GitExec(cwd string, params ...string) ExecStream { "GIT_SSH_COMMAND=/usr/bin/ssh -o StrictHostKeyChecking=yes", } cmd.Dir = filepath.Join(e.GitPath, cwd) - cmd.Stdout = writeFunc(func(data []byte) (int, error) { - e.StdLogger.Println(data) - return len(data), nil - }) - cmd.Stderr = writeFunc(func(data []byte) (int, error) { - e.ErrLogger.Println(data) - return len(data), nil - }) cmd.Stdin = nil - e.StdLogger.Printf("git execute: %#v\n", cmd.Args) - e.Error = cmd.Run() + if e.DebugLogger { + log.Printf("git execute: %#v\n", cmd.Args) + } + out, err := cmd.CombinedOutput() return e } @@ -239,7 +221,7 @@ func (c *ChanIO) Read(data []byte) (idx int, err error) { idx++ for len(c.ch) > 0 && idx < len(data) { - data[idx], ok = <- c.ch + data[idx], ok = <-c.ch if !ok { err = io.EOF return @@ -322,9 +304,9 @@ func parseGitMsg(data <-chan byte) (gitMsg, error) { return gitMsg{}, fmt.Errorf("Missing format weird") } return gitMsg{ - hash: string(id[:]), + hash: string(id[:]), itemType: "missing", - size: 0, + size: 0, }, fmt.Errorf("Object not found: '%s'", string(id)) default: return gitMsg{}, fmt.Errorf("Invalid object type: '%s'", string(msgType)) @@ -369,7 +351,7 @@ func parseGitCommitMsg(data <-chan byte, l int) (string, error) { msg = append(msg, c) l-- } -// l-- + // l-- if l != 0 { return "", fmt.Errorf("Unexpected data in the git commit msg: l=%d", l) @@ -480,7 +462,7 @@ func parseGitBlob(data <-chan byte) ([]byte, error) { } d := make([]byte, hdr.size) - for l:=0; l