aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--git-checkout-history/main.go40
-rw-r--r--utils/utils.go17
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
+}