Move git methods to Git struct

This commit is contained in:
Jingwen Owen Ou 2013-05-28 11:05:10 -07:00
Родитель 81c038fa84
Коммит 13b66491e9
5 изменённых файлов: 27 добавлений и 23 удалений

21
git.go
Просмотреть файл

@ -14,11 +14,14 @@ type Git struct {
func (git *Git) Version() (string, error) {
output, err := git.execGitCmd([]string{"version"})
if err != nil {
return "", err
}
return output[0], err
}
func FetchGitDir() (string, error) {
func (git *Git) Dir() (string, error) {
output, err := git.execGitCmd([]string{"rev-parse", "-q", "--git-dir"})
if err != nil {
return "", err
@ -33,7 +36,7 @@ func FetchGitDir() (string, error) {
return gitDir, nil
}
func FetchGitEditor() (string, error) {
func (git *Git) Editor() (string, error) {
output, err := git.execGitCmd([]string{"var", "GIT_EDITOR"})
if err != nil {
return "", err
@ -42,8 +45,8 @@ func FetchGitEditor() (string, error) {
return output[0], nil
}
func FetchGitOwner() (string, error) {
remote, err := FetchGitRemote()
func (git *Git) Owner() (string, error) {
remote, err := git.Remote()
if err != nil {
return "", err
}
@ -51,8 +54,8 @@ func FetchGitOwner() (string, error) {
return mustMatchGitUrl(remote)[1], nil
}
func FetchGitProject() (string, error) {
remote, err := FetchGitRemote()
func (git *Git) Project() (string, error) {
remote, err := git.Remote()
if err != nil {
return "", err
}
@ -60,7 +63,7 @@ func FetchGitProject() (string, error) {
return mustMatchGitUrl(remote)[2], nil
}
func FetchGitHead() (string, error) {
func (git *Git) Head() (string, error) {
output, err := git.execGitCmd([]string{"symbolic-ref", "-q", "--short", "HEAD"})
if err != nil {
return "master", err
@ -70,7 +73,7 @@ func FetchGitHead() (string, error) {
}
// FIXME: only care about origin push remote now
func FetchGitRemote() (string, error) {
func (git *Git) Remote() (string, error) {
r := regexp.MustCompile("origin\t(.+github.com.+) \\(push\\)")
output, err := git.execGitCmd([]string{"remote", "-v"})
if err != nil {
@ -86,7 +89,7 @@ func FetchGitRemote() (string, error) {
return "", errors.New("Can't find remote")
}
func FetchGitCommitLogs(sha1, sha2 string) (string, error) {
func (git *Git) Log(sha1, sha2 string) (string, error) {
execCmd := NewExecCmd("git")
execCmd.WithArg("log").WithArg("--no-color")
execCmd.WithArg("--format=%h (%aN, %ar)%n%w(78,3,3)%s%n%+b")

Просмотреть файл

@ -56,7 +56,7 @@ func NewGitHub() *GitHub {
}
if len(user) == 0 {
owner, err := FetchGitOwner()
owner, err := git.Owner()
if err != nil {
// prompt for user
log.Fatal(err)

Просмотреть файл

@ -7,26 +7,27 @@ import (
)
func TestGitMethods(t *testing.T) {
gitDir, _ := FetchGitDir()
git = Git{"git"}
gitDir, _ := git.Dir()
assert.T(t, strings.Contains(gitDir, ".git"))
gitEditor, err := FetchGitEditor()
gitEditor, err := git.Editor()
if err == nil {
assert.NotEqual(t, "", gitEditor)
}
gitRemote, _ := FetchGitRemote()
gitRemote, _ := git.Remote()
assert.T(t, strings.Contains(gitRemote, "jingweno/gh.git"))
gitOwner, _ := FetchGitOwner()
gitOwner, _ := git.Owner()
assert.Equal(t, "jingweno", gitOwner)
gitProject, _ := FetchGitProject()
gitProject, _ := git.Project()
assert.Equal(t, "gh", gitProject)
gitHead, _ := FetchGitHead()
gitHead, _ := git.Head()
assert.NotEqual(t, "", gitHead)
logs, _ := FetchGitCommitLogs("master", "HEAD")
logs, _ := git.Log("master", "HEAD")
assert.T(t, len(logs) >= 0)
}

Просмотреть файл

@ -80,7 +80,7 @@ func writePullRequestChanges(repo *Repo, messageFile string) error {
startRegexp := regexp.MustCompilePOSIX("^")
endRegexp := regexp.MustCompilePOSIX(" +$")
commitLogs, _ := FetchGitCommitLogs(repo.Base, repo.Head)
commitLogs, _ := git.Log(repo.Base, repo.Head)
var changesMsg string
if len(commitLogs) > 0 {
commitLogs = strings.TrimSpace(commitLogs)

10
repo.go
Просмотреть файл

@ -34,13 +34,13 @@ func NewRepo(base, head string) *Repo {
base = "master"
}
if head == "" {
head, _ = FetchGitHead()
head, _ = git.Head()
}
dir, _ := FetchGitDir()
editor, _ := FetchGitEditor()
owner, _ := FetchGitOwner()
project, _ := FetchGitProject()
dir, _ := git.Dir()
editor, _ := git.Editor()
owner, _ := git.Owner()
project, _ := git.Project()
return &Repo{dir, editor, owner, project, base, head}
}