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"
|
2015-06-15 08:50:37 +00:00
|
|
|
"os"
|
2014-09-23 15:40:23 +00:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2015-05-13 19:43:41 +00:00
|
|
|
|
2015-05-25 18:20:50 +00:00
|
|
|
"github.com/github/git-lfs/vendor/_nuts/github.com/rubyist/tracerx"
|
2014-09-23 15:40:23 +00:00
|
|
|
)
|
|
|
|
|
2015-04-24 17:43:29 +00:00
|
|
|
func LsRemote(remote, remoteRef string) (string, error) {
|
|
|
|
if remote == "" {
|
|
|
|
return "", errors.New("remote required")
|
2014-09-29 19:35:59 +00:00
|
|
|
}
|
2015-04-24 17:43:29 +00:00
|
|
|
if remoteRef == "" {
|
2015-06-15 11:52:35 +00:00
|
|
|
return simpleExec("git", "ls-remote", remote)
|
2014-09-29 19:35:59 +00:00
|
|
|
|
|
|
|
}
|
2015-06-15 11:52:35 +00:00
|
|
|
return simpleExec("git", "ls-remote", remote, remoteRef)
|
2014-09-29 19:35:59 +00:00
|
|
|
}
|
|
|
|
|
2015-05-21 14:36:42 +00:00
|
|
|
func ResolveRef(ref string) (string, error) {
|
2015-06-15 11:52:35 +00:00
|
|
|
return simpleExec("git", "rev-parse", ref)
|
2015-05-21 14:36:42 +00:00
|
|
|
}
|
|
|
|
|
2014-10-28 19:22:38 +00:00
|
|
|
func CurrentRef() (string, error) {
|
2015-05-21 14:36:42 +00:00
|
|
|
return ResolveRef("HEAD")
|
2014-10-28 19:22:38 +00:00
|
|
|
}
|
|
|
|
|
2014-10-27 15:12:14 +00:00
|
|
|
func CurrentBranch() (string, error) {
|
2015-06-15 11:52:35 +00:00
|
|
|
return simpleExec("git", "rev-parse", "--abbrev-ref", "HEAD")
|
2014-10-27 15:12:14 +00:00
|
|
|
}
|
|
|
|
|
2014-10-28 19:22:38 +00:00
|
|
|
func CurrentRemoteRef() (string, error) {
|
|
|
|
remote, err := CurrentRemote()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-05-21 14:36:42 +00:00
|
|
|
return ResolveRef(remote)
|
2014-10-28 19:22:38 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:10:01 +00:00
|
|
|
func UpdateIndex(file string) error {
|
2015-06-15 11:52:35 +00:00
|
|
|
_, err := simpleExec("git", "update-index", "-q", "--refresh", file)
|
2015-05-22 14:10:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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 {
|
2015-06-15 11:52:35 +00:00
|
|
|
output, _ := simpleExec("git", "config", val)
|
2014-09-23 21:42:47 +00:00
|
|
|
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) {
|
2015-06-15 11:52:35 +00:00
|
|
|
simpleExec("git", "config", "--global", "--add", key, val)
|
2014-09-23 21:42:47 +00:00
|
|
|
}
|
|
|
|
|
2015-05-29 23:49:11 +00:00
|
|
|
// UnsetGlobal 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) {
|
2015-06-15 11:52:35 +00:00
|
|
|
simpleExec("git", "config", "--global", "--unset", key)
|
2014-09-23 21:42:47 +00:00
|
|
|
}
|
|
|
|
|
2015-06-02 15:19:59 +00:00
|
|
|
// SetLocal sets the git config value for the key in the specified config file
|
|
|
|
func (c *gitConfig) SetLocal(file, key, val string) {
|
2015-06-15 20:17:31 +00:00
|
|
|
simpleExec("git", "config", "--file", file, "--add", key, val)
|
2015-06-02 15:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnsetLocalKey removes the git config value for the key from the specified config file
|
|
|
|
func (c *gitConfig) UnsetLocalKey(file, key string) {
|
2015-06-15 20:17:31 +00:00
|
|
|
simpleExec("git", "config", "--file", file, "--unset", key)
|
2015-06-02 15:19:59 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2015-06-15 11:52:35 +00:00
|
|
|
return simpleExec("git", "config", "-l")
|
2014-09-23 21:42:47 +00:00
|
|
|
}
|
|
|
|
|
2014-09-24 17:10:29 +00:00
|
|
|
// ListFromFile lists all of the git config values in the given config file
|
2015-06-01 18:53:08 +00:00
|
|
|
func (c *gitConfig) ListFromFile(f string) (string, error) {
|
2015-06-15 08:50:37 +00:00
|
|
|
if _, err := os.Stat(f); os.IsNotExist(err) {
|
|
|
|
return "", nil
|
|
|
|
}
|
2015-06-15 11:52:35 +00:00
|
|
|
return simpleExec("git", "config", "-l", "-f", f)
|
2014-09-23 21:42:47 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2015-06-15 11:52:35 +00:00
|
|
|
return simpleExec("git", "version")
|
2014-09-23 21:42:47 +00:00
|
|
|
}
|
|
|
|
|
2015-06-15 11:52:35 +00:00
|
|
|
// simpleExec is a small wrapper around os/exec.Command.
|
2015-06-15 11:55:03 +00:00
|
|
|
func simpleExec(name string, args ...string) (string, error) {
|
|
|
|
tracerx.Printf("run_command: '%s' %s", name, strings.Join(args, " "))
|
2015-06-17 10:14:30 +00:00
|
|
|
cmd := execCommand(name, args...)
|
2014-09-23 15:40:23 +00:00
|
|
|
|
|
|
|
output, err := cmd.Output()
|
|
|
|
if _, ok := err.(*exec.ExitError); ok {
|
|
|
|
return "", nil
|
2015-05-12 08:45:06 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2015-06-15 11:55:03 +00:00
|
|
|
return fmt.Sprintf("Error running %s %s", name, args), err
|
2014-09-23 15:40:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Trim(string(output), " \n"), nil
|
|
|
|
}
|