2013-05-29 22:58:46 +04:00
|
|
|
package git
|
2013-04-22 07:36:21 +04:00
|
|
|
|
|
|
|
import (
|
2013-05-24 09:41:06 +04:00
|
|
|
"errors"
|
2013-04-29 00:41:45 +04:00
|
|
|
"fmt"
|
2013-05-29 22:58:46 +04:00
|
|
|
"github.com/jingweno/gh/cmd"
|
2013-05-28 23:42:11 +04:00
|
|
|
"os/exec"
|
2013-04-24 14:00:45 +04:00
|
|
|
"path/filepath"
|
2013-04-22 07:36:21 +04:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2013-05-29 22:58:46 +04:00
|
|
|
func Version() (string, error) {
|
2013-06-25 01:40:05 +04:00
|
|
|
output, err := execGitCmd("version")
|
2013-05-28 22:05:10 +04:00
|
|
|
if err != nil {
|
2013-05-29 00:14:40 +04:00
|
|
|
return "", errors.New("Can't load git version")
|
2013-05-28 22:05:10 +04:00
|
|
|
}
|
2013-05-28 21:52:27 +04:00
|
|
|
|
2013-05-29 00:14:40 +04:00
|
|
|
return output[0], nil
|
2013-05-28 21:52:27 +04:00
|
|
|
}
|
|
|
|
|
2013-05-29 22:58:46 +04:00
|
|
|
func Dir() (string, error) {
|
2013-06-25 01:40:05 +04:00
|
|
|
output, err := execGitCmd("rev-parse", "-q", "--git-dir")
|
2013-04-24 14:00:45 +04:00
|
|
|
if err != nil {
|
2013-05-29 00:14:40 +04:00
|
|
|
return "", errors.New("Not a git repository (or any of the parent directories): .git")
|
2013-04-24 14:00:45 +04:00
|
|
|
}
|
|
|
|
|
2013-05-24 09:41:06 +04:00
|
|
|
gitDir := output[0]
|
|
|
|
gitDir, err = filepath.Abs(gitDir)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return gitDir, nil
|
2013-04-22 07:36:21 +04:00
|
|
|
}
|
|
|
|
|
2013-05-29 22:58:46 +04:00
|
|
|
func PullReqMsgFile() (string, error) {
|
|
|
|
gitDir, err := Dir()
|
2013-05-28 23:42:11 +04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Join(gitDir, "PULLREQ_EDITMSG"), nil
|
|
|
|
}
|
|
|
|
|
2013-05-29 22:58:46 +04:00
|
|
|
func Editor() (string, error) {
|
2013-06-25 01:40:05 +04:00
|
|
|
output, err := execGitCmd("var", "GIT_EDITOR")
|
2013-05-24 09:41:06 +04:00
|
|
|
if err != nil {
|
2013-05-29 00:14:40 +04:00
|
|
|
return "", errors.New("Can't load git var: GIT_EDITOR")
|
2013-05-24 09:41:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return output[0], nil
|
2013-04-22 07:36:21 +04:00
|
|
|
}
|
|
|
|
|
2013-05-29 22:58:46 +04:00
|
|
|
func EditorPath() (string, error) {
|
|
|
|
gitEditor, err := Editor()
|
2013-05-28 23:42:11 +04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2013-05-29 06:11:03 +04:00
|
|
|
gitEditorWithParams := strings.Split(gitEditor, " ")
|
|
|
|
gitEditor = gitEditorWithParams[0]
|
|
|
|
gitEditorParams := gitEditorWithParams[1:]
|
|
|
|
|
2013-05-28 23:42:11 +04:00
|
|
|
editorPath, err := exec.LookPath(gitEditor)
|
|
|
|
if err != nil {
|
2013-05-29 00:14:40 +04:00
|
|
|
return "", errors.New("Can't locate git editor: " + gitEditor)
|
2013-05-28 23:42:11 +04:00
|
|
|
}
|
|
|
|
|
2013-05-29 06:11:03 +04:00
|
|
|
for _, p := range gitEditorParams {
|
|
|
|
editorPath = editorPath + " " + p
|
|
|
|
}
|
|
|
|
|
2013-05-28 23:42:11 +04:00
|
|
|
return editorPath, nil
|
|
|
|
}
|
|
|
|
|
2013-05-29 22:58:46 +04:00
|
|
|
func Head() (string, error) {
|
2013-06-25 01:40:05 +04:00
|
|
|
output, err := execGitCmd("symbolic-ref", "-q", "--short", "HEAD")
|
2013-05-24 09:41:06 +04:00
|
|
|
if err != nil {
|
2013-05-29 00:14:40 +04:00
|
|
|
return "master", errors.New("Can't load git HEAD")
|
2013-05-24 09:41:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return output[0], nil
|
2013-04-22 07:36:21 +04:00
|
|
|
}
|
|
|
|
|
2013-05-29 22:58:46 +04:00
|
|
|
func Ref(ref string) (string, error) {
|
2013-06-25 01:40:05 +04:00
|
|
|
output, err := execGitCmd("rev-parse", "-q", ref)
|
2013-05-29 21:15:43 +04:00
|
|
|
if err != nil {
|
|
|
|
return "", errors.New("Unknown revision or path not in the working tree: " + ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
return output[0], nil
|
|
|
|
}
|
|
|
|
|
2013-05-29 22:58:46 +04:00
|
|
|
func Log(sha1, sha2 string) (string, error) {
|
|
|
|
execCmd := cmd.New("git")
|
2013-04-29 00:41:45 +04:00
|
|
|
execCmd.WithArg("log").WithArg("--no-color")
|
|
|
|
execCmd.WithArg("--format=%h (%aN, %ar)%n%w(78,3,3)%s%n%+b")
|
|
|
|
execCmd.WithArg("--cherry")
|
|
|
|
shaRange := fmt.Sprintf("%s...%s", sha1, sha2)
|
|
|
|
execCmd.WithArg(shaRange)
|
|
|
|
|
2013-04-29 05:47:08 +04:00
|
|
|
outputs, err := execCmd.ExecOutput()
|
2013-04-29 00:41:45 +04:00
|
|
|
if err != nil {
|
2013-05-29 00:14:40 +04:00
|
|
|
return "", errors.New("Can't load git log " + sha1 + ".." + sha2)
|
2013-04-29 00:41:45 +04:00
|
|
|
}
|
|
|
|
|
2013-05-24 09:41:06 +04:00
|
|
|
return outputs, nil
|
2013-04-29 00:41:45 +04:00
|
|
|
}
|
|
|
|
|
2013-06-25 01:40:05 +04:00
|
|
|
func SysExec(command string, args ...string) error {
|
2013-06-22 00:17:52 +04:00
|
|
|
cmd := cmd.New("git")
|
2013-06-25 01:40:05 +04:00
|
|
|
cmd.WithArg(command)
|
2013-06-22 00:17:52 +04:00
|
|
|
for _, a := range args {
|
|
|
|
cmd.WithArg(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd.SysExec()
|
|
|
|
}
|
|
|
|
|
2013-06-25 01:40:05 +04:00
|
|
|
func Spawn(command string, args ...string) error {
|
2013-06-22 05:02:29 +04:00
|
|
|
cmd := cmd.New("git")
|
2013-06-25 01:40:05 +04:00
|
|
|
cmd.WithArg(command)
|
2013-06-22 05:02:29 +04:00
|
|
|
for _, a := range args {
|
|
|
|
cmd.WithArg(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := cmd.ExecOutput()
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-25 01:40:05 +04:00
|
|
|
func execGitCmd(input ...string) (outputs []string, err error) {
|
2013-05-29 22:58:46 +04:00
|
|
|
cmd := cmd.New("git")
|
2013-04-30 06:45:57 +04:00
|
|
|
for _, i := range input {
|
|
|
|
cmd.WithArg(i)
|
|
|
|
}
|
2013-04-22 07:36:21 +04:00
|
|
|
|
2013-04-30 06:45:57 +04:00
|
|
|
out, err := cmd.ExecOutput()
|
|
|
|
for _, line := range strings.Split(out, "\n") {
|
2013-04-22 07:36:21 +04:00
|
|
|
outputs = append(outputs, string(line))
|
|
|
|
}
|
|
|
|
|
2013-06-25 00:43:59 +04:00
|
|
|
return outputs, err
|
2013-04-22 07:36:21 +04:00
|
|
|
}
|