// Checkout executes the git checkout command. // If remote is specified, first delete the local branch if any // and recreate according to that remote. func Checkout(ref, remote string) (err error) { if remote == "" { _, err = qexec.Run("git", "checkout", ref) return } qexec.Run("git", "branch", "-D", ref) _, err = qexec.Run("git", "checkout", "-b", ref, remote) return }
// Remote returns the requested remote url func Remote(name string) (string, error) { remote, err := qexec.Run("git", "config", "--get", fmt.Sprintf("remote.%s.url", name)) if err != nil { return "", err } return strings.TrimSpace(remote), err }
// RevParse executes the git rev-parse command. func RevParse(arg string) (string, error) { ref, err := qexec.Run("git", "rev-parse", "-q", arg) if err != nil { return "", nil } return strings.TrimSpace(ref), nil }
// Branch returns the current branch. func Branch() (string, error) { branch, err := qexec.Run("git", "rev-parse", "--abbrev-ref", "HEAD") if err != nil { return "", err } return strings.TrimSpace(branch), nil }
// HasLocalDiff returns true if repo has local modifications. func HasLocalDiff() (bool, error) { _, err := qexec.Run("git", "diff", "--quiet", "HEAD") status, err := qexec.ExitStatus(err) if err != nil { return false, err } return status != 0, nil }
// RefExists checks if the ref exists in the repository. func RefExists(ref string) (bool, error) { _, err := qexec.Run("git", "rev-parse", "--quiet", "--verify", ref) if err != nil { status, err := qexec.ExitStatus(err) if err != nil || status != 1 { return false, err } return false, nil } return true, nil }
// Log returns the list of commits from the requested range. func Log(start, end string) ([]Commit, error) { output, err := qexec.Run("git", "log", start+".."+end, "--oneline") if err != nil { return nil, err } lines := strings.Split(strings.TrimSpace(output), "\n") commits := make([]Commit, 0) for _, line := range lines { tokens := strings.SplitN(line, " ", 2) if len(tokens) != 2 { continue } commits = append(commits, Commit{tokens[0], tokens[1]}) } return commits, nil }
// Pull executes the git pull command func Pull(remote string) error { _, err := qexec.Run("git", "pull", remote) return err }
// AddRemote add a new remote to the repository. func AddRemote(name, url string) error { _, err := qexec.Run("git", "remote", "add", name, url) return err }
// ResetHard executes the git reset --hard command. func ResetHard(ref string) error { _, err := qexec.Run("git", "reset", "--hard", ref) return err }
// Fetch executes the git fetch command. func Fetch(remote string) error { _, err := qexec.Run("git", "fetch", remote) return err }
// Merge executes the git merge command. func Merge(ref string) error { _, err := qexec.Run("git", "merge", ref) return err }