git-lfs/git/git.go

100 lines
2.5 KiB
Go
Raw Normal View History

2014-09-24 17:10:29 +00:00
// Package git contains various commands that shell out to git
package git
import (
"errors"
"fmt"
2014-10-04 16:10:02 +00:00
"github.com/rubyist/tracerx"
"io"
"os/exec"
"strings"
)
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)
}
func CurrentBranch() (string, error) {
return simpleExec(nil, "git", "rev-parse", "--abbrev-ref", "HEAD")
}
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
}
type gitConfig struct {
}
var Config = &gitConfig{}
2014-09-24 17:10:29 +00:00
// Find returns the git config value for the key
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
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
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
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
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
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.
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, " "))
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
}