diff options
author | Teddy Wing | 2014-12-14 13:15:11 -0500 |
---|---|---|
committer | Teddy Wing | 2014-12-14 13:15:11 -0500 |
commit | e02955d75b0f72147884de74ef6ff563828f1bc2 (patch) | |
tree | f04c05b47d2b4d5220c76edf7eea2ef09d176188 | |
parent | 8c64b8afcf6d98cefe1076ee2ec048aa75e6b4c7 (diff) | |
download | git-checkout-history-e02955d75b0f72147884de74ef6ff563828f1bc2.tar.bz2 |
git-checkout-history: Put duplicated code in utils.go
Extract code that was duplicated from checkout-history-store so that it
now lives in a single place: the utils.go file.
Add a new function to utils.go that returns the list of branches in
history so that we can grab this for git-checkout-history.
-rw-r--r-- | git-checkout-history/main.go | 40 | ||||
-rw-r--r-- | utils/utils.go | 17 |
2 files changed, 22 insertions, 35 deletions
diff --git a/git-checkout-history/main.go b/git-checkout-history/main.go index 5d8351b..3b74e9e 100644 --- a/git-checkout-history/main.go +++ b/git-checkout-history/main.go @@ -3,60 +3,30 @@ package main import ( "fmt" "github.com/teddywing/git-checkout-history/utils" - "io/ioutil" - "log" "os" "os/exec" - "os/user" "strconv" - - "gopkg.in/yaml.v2" ) -// TODO: move to package -type BranchList struct { - Branches []string -} - -// TODO: move to package -func getHomeDir() string { - usr, err := user.Current() - if err != nil { - log.Fatal(err) - } - return usr.HomeDir -} - func main() { - branchList := BranchList{} - - file_path := getHomeDir() + "/.git-checkout-history" - data, err := ioutil.ReadFile(file_path) - if err != nil { - log.Fatal(err) - } - - err = yaml.Unmarshal(data, &branchList) - if err != nil { - log.Fatal(err) - } + branches := utils.Branches() args := os.Args[1:] if len(args) > 0 { branchIndex, _ := strconv.Atoi(args[0]) - cmd := exec.Command("git", "checkout", branchList.Branches[branchIndex]) + cmd := exec.Command("git", "checkout", branches[branchIndex]) err := cmd.Run() if err != nil { fmt.Fprintf(os.Stderr, err.Error()) } - utils.Store(branchList.Branches[branchIndex]) + utils.Store(branches[branchIndex]) } else { // List branches in history - for i := 1; i < len(branchList.Branches); i++ { - fmt.Printf("[%d] %s\n", i, branchList.Branches[i]) + for i := 1; i < len(branches); i++ { + fmt.Printf("[%d] %s\n", i, branches[i]) } } } diff --git a/utils/utils.go b/utils/utils.go index 8efe6d2..01e2556 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -63,3 +63,20 @@ func Store(branch string) { log.Fatal(err) } } + +func Branches() []string { + branchList := BranchList{} + + file_path := getHomeDir() + "/" + history_file + data, err := ioutil.ReadFile(file_path) + if err != nil { + log.Fatal(err) + } + + err = yaml.Unmarshal(data, &branchList) + if err != nil { + log.Fatal(err) + } + + return branchList.Branches +} |