Пример #1
0
// 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
}
Пример #2
0
// 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
}
Пример #3
0
// 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
}
Пример #4
0
// 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
}
Пример #5
0
// 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
}
Пример #6
0
// 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
}
Пример #7
0
// 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
}
Пример #8
0
// Pull executes the git pull command
func Pull(remote string) error {
	_, err := qexec.Run("git", "pull", remote)
	return err
}
Пример #9
0
// AddRemote add a new remote to the repository.
func AddRemote(name, url string) error {
	_, err := qexec.Run("git", "remote", "add", name, url)
	return err
}
Пример #10
0
// ResetHard executes the git reset --hard command.
func ResetHard(ref string) error {
	_, err := qexec.Run("git", "reset", "--hard", ref)
	return err
}
Пример #11
0
// Fetch executes the git fetch command.
func Fetch(remote string) error {
	_, err := qexec.Run("git", "fetch", remote)
	return err
}
Пример #12
0
// Merge executes the git merge command.
func Merge(ref string) error {
	_, err := qexec.Run("git", "merge", ref)
	return err
}