aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2015-01-11 19:09:14 -0500
committerTeddy Wing2015-01-11 19:09:14 -0500
commitceb22064e29582a85e3b4df6e87eb1397e8337b7 (patch)
treebb7433128f11cb252621c833c94af411a7c7bac9
parentc93b9f1f135da82f7d126d4e940c716c7cc6c870 (diff)
parentb89de92a5bb69e40f80e51ab4d4847c78598e48e (diff)
downloadgit-checkout-history-ceb22064e29582a85e3b4df6e87eb1397e8337b7.tar.bz2
Merge branch 'separate-branch-history-by-repository'v0.2.0
-rw-r--r--utils/utils.go53
1 files changed, 45 insertions, 8 deletions
diff --git a/utils/utils.go b/utils/utils.go
index 5f686f7..1f8ebb7 100644
--- a/utils/utils.go
+++ b/utils/utils.go
@@ -1,20 +1,20 @@
package utils
import (
+ "bytes"
"io/ioutil"
"log"
"os"
+ "os/exec"
"os/user"
+ "regexp"
+ "strconv"
"gopkg.in/yaml.v2"
)
var history_file string = ".git-checkout-history"
-type BranchList struct {
- Branches []string
-}
-
func getHomeDir() string {
usr, err := user.Current()
if err != nil {
@@ -23,6 +23,41 @@ func getHomeDir() string {
return usr.HomeDir
}
+func currentGitDir() string {
+ // Check git version
+ // If below 1.7.0 then exit 1. `--show-toplevel` is not available in prior
+ // versions.
+ // /git version (\d+\.\d+).*/
+ // Run `git rev-parse --show-toplevel`
+ // Return output from the command
+ cmd := exec.Command("git", "--version")
+ var version_string bytes.Buffer
+ cmd.Stdout = &version_string
+ if err := cmd.Run(); err != nil {
+ panic(err)
+ }
+
+ r, _ := regexp.Compile(`git version (\d+\.\d+).*`)
+ matches := r.FindStringSubmatch(version_string.String())
+
+ version_number, _ := strconv.ParseFloat(matches[1], 64)
+ if version_number >= 1.7 {
+ cmd = exec.Command("git", "rev-parse", "--show-toplevel")
+ var git_directory bytes.Buffer
+ cmd.Stdout = &git_directory
+ if err := cmd.Run(); err != nil {
+ panic(err)
+ }
+
+ dir_trimmed := git_directory.String()
+
+ // Trim newline at the end of the directory string
+ return dir_trimmed[:len(dir_trimmed) - 1]
+ }
+
+ return ""
+}
+
func OpenHistoryFile() (f *os.File, err error) {
file_path := getHomeDir() + "/" + history_file
if _, err := os.Stat(file_path); os.IsNotExist(err) {
@@ -33,7 +68,6 @@ func OpenHistoryFile() (f *os.File, err error) {
}
func Store(branch string) {
- branchList := BranchList{}
rcfile, err := OpenHistoryFile()
if err != nil {
log.Fatal(err)
@@ -46,12 +80,15 @@ func Store(branch string) {
log.Fatal(err)
}
+ branchList := make(map[string][]string)
+
err = yaml.Unmarshal(data, &branchList)
if err != nil {
log.Fatal(err)
}
- branchList.Branches = append([]string{branch}, branchList.Branches...)
+ current_git_dir := currentGitDir()
+ branchList[current_git_dir] = append([]string{branch}, branchList[current_git_dir]...)
data, err = yaml.Marshal(&branchList)
if err != nil {
@@ -65,7 +102,7 @@ func Store(branch string) {
}
func Branches() []string {
- branchList := BranchList{}
+ branchList := make(map[string][]string)
file_path := getHomeDir() + "/" + history_file
data, err := ioutil.ReadFile(file_path)
@@ -78,5 +115,5 @@ func Branches() []string {
log.Fatal(err)
}
- return branchList.Branches
+ return branchList[currentGitDir()]
}