-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Available values: go: Uses go-git, a Go native implementation of git. This is compiled with the multi-gitter binary, and no extra dependencies are needed. cmd: Calls out to the git command. This requires git to be installed and available with by calling "git". (default "go")
- Loading branch information
Showing
12 changed files
with
404 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package domain | ||
|
||
// GitConfig is configration for any git implementation | ||
type GitConfig struct { | ||
// Absolute path to the directory | ||
Directory string | ||
// The fetch depth used when cloning, if set to 0, the entire history will be used | ||
FetchDepth int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package cmdgit | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os/exec" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/lindell/multi-gitter/internal/domain" | ||
) | ||
|
||
// Git is an implementation of git that executes git as commands | ||
type Git struct { | ||
Directory string // The (temporary) directory that should be worked within | ||
FetchDepth int // Limit fetching to the specified number of commits | ||
} | ||
|
||
var errRe = regexp.MustCompile(`(^|\n)(error|fatal): (.+)`) | ||
|
||
func (g *Git) run(cmd *exec.Cmd) (string, error) { | ||
stderr := &bytes.Buffer{} | ||
stdout := &bytes.Buffer{} | ||
|
||
cmd.Dir = g.Directory | ||
cmd.Stderr = stderr | ||
cmd.Stdout = stdout | ||
|
||
err := cmd.Run() | ||
if err != nil { | ||
matches := errRe.FindStringSubmatch(stderr.String()) | ||
if matches != nil { | ||
return "", errors.New(matches[3]) | ||
} | ||
|
||
msg := fmt.Sprintf(`git command existed with %d`, | ||
cmd.ProcessState.ExitCode(), | ||
) | ||
|
||
return "", errors.New(msg) | ||
} | ||
return stdout.String(), nil | ||
} | ||
|
||
// Clone a repository | ||
func (g *Git) Clone(url string, baseName string) error { | ||
args := []string{"clone", url, "--branch", baseName, "--single-branch"} | ||
if g.FetchDepth > 0 { | ||
args = append(args, "--depth", fmt.Sprint(g.FetchDepth)) | ||
} | ||
args = append(args, g.Directory) | ||
|
||
cmd := exec.Command("git", args...) | ||
_, err := g.run(cmd) | ||
return err | ||
} | ||
|
||
// ChangeBranch changes the branch | ||
func (g *Git) ChangeBranch(branchName string) error { | ||
cmd := exec.Command("git", "checkout", "-b", branchName) | ||
_, err := g.run(cmd) | ||
return err | ||
} | ||
|
||
// Changes detect if any changes has been made in the directory | ||
func (g *Git) Changes() (bool, error) { | ||
cmd := exec.Command("git", "status", "-s") | ||
stdOut, err := g.run(cmd) | ||
return len(stdOut) > 0, err | ||
} | ||
|
||
// Commit and push all changes | ||
func (g *Git) Commit(commitAuthor *domain.CommitAuthor, commitMessage string) error { | ||
cmd := exec.Command("git", "add", ".") | ||
_, err := g.run(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cmd = exec.Command("git", "commit", "-m", commitMessage) | ||
|
||
if commitAuthor != nil { | ||
cmd.Env = append(cmd.Env, | ||
"GIT_AUTHOR_NAME="+commitAuthor.Name, | ||
"GIT_AUTHOR_EMAIL="+commitAuthor.Email, | ||
"GIT_COMMITTER_NAME="+commitAuthor.Name, | ||
"GIT_COMMITTER_EMAIL="+commitAuthor.Email, | ||
) | ||
} | ||
|
||
_, err = g.run(cmd) | ||
|
||
if err := g.logDiff(); err != nil { | ||
return err | ||
} | ||
|
||
return err | ||
} | ||
|
||
func (g *Git) logDiff() error { | ||
if !log.IsLevelEnabled(log.DebugLevel) { | ||
return nil | ||
} | ||
|
||
cmd := exec.Command("git", "diff", "HEAD~1") | ||
stdout, err := g.run(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Debug(stdout) | ||
|
||
return nil | ||
} | ||
|
||
// BranchExist checks if the new branch exists | ||
func (g *Git) BranchExist(remoteName, branchName string) (bool, error) { | ||
cmd := exec.Command("git", "ls-remote", "-q", "-h") | ||
stdOut, err := g.run(cmd) | ||
if err != nil { | ||
return false, err | ||
} | ||
return strings.Contains(stdOut, fmt.Sprintf("refs/heads/%s", branchName)), nil | ||
} | ||
|
||
// Push the committed changes to the remote | ||
func (g *Git) Push(remoteName string) error { | ||
cmd := exec.Command("git", "push", remoteName, "HEAD") | ||
_, err := g.run(cmd) | ||
return err | ||
} | ||
|
||
// AddRemote adds a new remote | ||
func (g *Git) AddRemote(name, url string) error { | ||
cmd := exec.Command("git", "remote", "add", name, url) | ||
_, err := g.run(cmd) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.