2014-09-24 17:10:29 +00:00
|
|
|
// Package git contains various commands that shell out to git
|
2014-09-23 15:40:23 +00:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2014-09-23 21:42:47 +00:00
|
|
|
"errors"
|
2014-09-23 15:40:23 +00:00
|
|
|
"fmt"
|
2014-10-04 16:10:02 +00:00
|
|
|
"github.com/rubyist/tracerx"
|
2014-09-23 15:40:23 +00:00
|
|
|
"io"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-09-29 19:35:59 +00:00
|
|
|
func LsRemote(repo, refspec string) (string, error) {
|
|
|
|
if repo == "" {
|
|
|
|
return "", errors.New("repo required")
|
|
|
|
}
|
|
|
|
if refspec == "" {
|
|
|
|
return simpleExec(nil, "git", "ls-remote", repo)
|
|
|
|
|
|
|
|
}
|
|
|
|
return simpleExec(nil, "git", "ls-remote", repo, refspec)
|
|
|
|
}
|
|
|
|
|
2014-10-28 19:22:38 +00:00
|
|
|
func CurrentRef() (string, error) {
|
|
|
|
return simpleExec(nil, "git", "rev-parse", "HEAD")
|
|
|
|
}
|
|
|
|
|
2014-10-27 15:12:14 +00:00
|
|
|
func CurrentBranch() (string, error) {
|
|
|
|
return simpleExec(nil, "git", "rev-parse", "--abbrev-ref", "HEAD")
|
|
|
|
}
|
|
|
|
|
2014-10-28 19:22:38 +00:00
|
|
|
func CurrentRemoteRef() (string, error) {
|
|
|
|
remote, err := CurrentRemote()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return simpleExec(nil, "git", "rev-parse", remote)
|
|
|
|
}
|
|
|
|
|
2014-10-27 16:52:28 +00:00
|
|
|
func CurrentRemote() (string, error) {
|
|
|
|
branch, err := CurrentBranch()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if branch == "HEAD" {
|
|
|
|
return "", errors.New("not on a branch")
|
|
|
|
}
|
|
|
|
|
|
|
|
remote := Config.Find(fmt.Sprintf("branch.%s.remote", branch))
|
|
|
|
if remote == "" {
|
|
|
|
return "", errors.New("remote not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return remote + "/" + branch, nil
|
|
|
|
}
|
|
|
|
|
2014-09-23 21:42:47 +00:00
|
|
|
type gitConfig struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
var Config = &gitConfig{}
|
|
|
|
|
2014-09-24 17:10:29 +00:00
|
|
|
// Find returns the git config value for the key
|
2014-09-23 21:42:47 +00:00
|
|
|
func (c *gitConfig) Find(val string) string {
|
|
|
|
output, _ := simpleExec(nil, "git", "config", val)
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2014-09-24 17:10:29 +00:00
|
|
|
// SetGlobal sets the git config value for the key in the global config
|
2014-09-23 21:42:47 +00:00
|
|
|
func (c *gitConfig) SetGlobal(key, val string) {
|
|
|
|
simpleExec(nil, "git", "config", "--global", "--add", key, val)
|
|
|
|
}
|
|
|
|
|
2014-09-24 17:10:29 +00:00
|
|
|
// SetGlobal removes the git config value for the key from the global config
|
2014-09-23 21:42:47 +00:00
|
|
|
func (c *gitConfig) UnsetGlobal(key string) {
|
|
|
|
simpleExec(nil, "git", "config", "--global", "--unset", key)
|
|
|
|
}
|
|
|
|
|
2014-09-24 17:10:29 +00:00
|
|
|
// List lists all of the git config values
|
2014-09-23 21:42:47 +00:00
|
|
|
func (c *gitConfig) List() (string, error) {
|
|
|
|
return simpleExec(nil, "git", "config", "-l")
|
|
|
|
}
|
|
|
|
|
2014-09-24 17:10:29 +00:00
|
|
|
// ListFromFile lists all of the git config values in the given config file
|
2014-09-23 21:42:47 +00:00
|
|
|
func (c *gitConfig) ListFromFile() (string, error) {
|
|
|
|
return simpleExec(nil, "git", "config", "-l", "-f", ".gitconfig")
|
|
|
|
}
|
|
|
|
|
2014-09-24 17:10:29 +00:00
|
|
|
// Version returns the git version
|
2014-09-23 21:42:47 +00:00
|
|
|
func (c *gitConfig) Version() (string, error) {
|
|
|
|
return simpleExec(nil, "git", "version")
|
|
|
|
}
|
|
|
|
|
2014-09-24 17:10:29 +00:00
|
|
|
// simpleExec is a small wrapper around os/exec.Command. If the passed stdin
|
|
|
|
// is not nil it will be hooked up to the subprocess stdin.
|
2014-09-23 15:40:23 +00:00
|
|
|
func simpleExec(stdin io.Reader, name string, arg ...string) (string, error) {
|
2014-10-04 16:10:02 +00:00
|
|
|
tracerx.Printf("run_command: '%s' %s", name, strings.Join(arg, " "))
|
2014-09-23 15:40:23 +00:00
|
|
|
cmd := exec.Command(name, arg...)
|
|
|
|
if stdin != nil {
|
|
|
|
cmd.Stdin = stdin
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := cmd.Output()
|
|
|
|
if _, ok := err.(*exec.ExitError); ok {
|
|
|
|
return "", nil
|
|
|
|
} else if err != nil {
|
|
|
|
return fmt.Sprintf("Error running %s %s", name, arg), err
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Trim(string(output), " \n"), nil
|
|
|
|
}
|