Skip to content

Commit

Permalink
Refactoring git.go
Browse files Browse the repository at this point in the history
  • Loading branch information
tcnksm committed Jul 29, 2014
1 parent 884012a commit 7ea9614
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
30 changes: 27 additions & 3 deletions git.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"fmt"
"io/ioutil"
"os/exec"
"regexp"
Expand All @@ -10,16 +11,39 @@ import (

var RepoNameRegexp = regexp.MustCompile(`.+/([^/]+)\.git$`)

func GitRepoName(url string) string {
func GetRepoName() (string, error) {
url, err := gitRemote()
if err != nil || url == "" {
return "", fmt.Errorf("Cound not retrieve remote repository url\n")
}

repo := retrieveRepoName(url)
if repo == "" {
return "", fmt.Errorf("Cound not retrieve repository name\n")
}
return repo, nil
}

func GetOwnerName() (string, error) {
owner, err := gitOwner()
if err != nil || owner == "" {
return "", fmt.Errorf("Cound not retrieve git user name\n")
}
return owner, err
}

func retrieveRepoName(url string) string {
matched := RepoNameRegexp.FindStringSubmatch(url)
return matched[1]
}

func GitRemote() (string, error) {
// git config --local remote.origin.url
func gitRemote() (string, error) {
return gitConfig("--local", "remote.origin.url")
}

func GitOwner() (string, error) {
// git config --global user.name
func gitOwner() (string, error) {
return gitConfig("--global", "user.name")
}

Expand Down
8 changes: 6 additions & 2 deletions git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ func TestGitConfig(t *testing.T) {
url, err := gitConfig("remote.origin.url")
Expect(err).NotTo(HaveOccurred())
Expect(url).To(Equal("https://github.com/tcnksm/test.git"))

blank, err := gitConfig("nothing.nothing")
Expect(err).To(HaveOccurred())
Expect(blank).To(Equal(""))
}

func TestGitRepoName(t *testing.T) {
func TestRetrieveRepoName(t *testing.T) {
RegisterTestingT(t)

repo := GitRepoName("https://github.com/tcnksm/ghr.git")
repo := retrieveRepoName("https://github.com/tcnksm/ghr.git")
Expect(repo).To(Equal("ghr"))
}

Expand Down
19 changes: 6 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,18 @@ func ghrMain() int {
path := os.Args[2]
debug(path)

// git config --global user.name
// tcnksm
owner, err := GitOwner()
if err != nil || owner == "" {
fmt.Fprintf(os.Stderr, "Please set `git config --global user.name`\n")
owner, err := GetOwnerName()
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
return 1
}
debug(owner)

// git config --local remote.origin.url
// https://github.com/tcnksm/ghr.git
remoteURL, err := GitRemote()
if err != nil || remoteURL == "" {
fmt.Fprintf(os.Stderr, "Please set remote host of your project\n")
repo, err := GetRepoName()
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
return 1
}
debug(remoteURL)

repo := GitRepoName(remoteURL)
debug(repo)

info := NewInfo()
Expand Down

0 comments on commit 7ea9614

Please sign in to comment.