From 7d6ab76e8c5b382aff8a0cb7812c904f6dda9272 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 31 Dec 2014 12:50:30 -0500 Subject: utils.go: Add function to get current git repo path Query git to get the current repo path. I plan on using that string as the key for branches. This will allow us to only store and query checkout history for the current repository. TODO: get rid of trailing newline --- utils/utils.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/utils/utils.go b/utils/utils.go index 52af88a..79b2ca2 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -1,10 +1,14 @@ package utils import ( + "bytes" "io/ioutil" "log" "os" + "os/exec" "os/user" + "regexp" + "strconv" "gopkg.in/yaml.v2" ) @@ -23,6 +27,37 @@ 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) + } + return git_directory.String() + } + + return "" +} + func OpenHistoryFile() (f *os.File, err error) { file_path := getHomeDir() + "/" + history_file if _, err := os.Stat(file_path); os.IsNotExist(err) { -- cgit v1.2.3 From faad1a93bf0cbadc06f245a46d1057ced4f3710c Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 2 Jan 2015 22:01:39 -0500 Subject: utils.go: Trim trailing newline on repo directory The repository directory location string had a trailing newline at the end which I don't want because this is going to be the key of a hash, so it doesn't make sense to have it. --- utils/utils.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/utils/utils.go b/utils/utils.go index 79b2ca2..0c7760e 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -52,7 +52,11 @@ func currentGitDir() string { if err := cmd.Run(); err != nil { panic(err) } - return git_directory.String() + + dir_trimmed := git_directory.String() + + // Trim newline at the end of the directory string + return dir_trimmed[:len(dir_trimmed) - 1] } return "" -- cgit v1.2.3 From b89de92a5bb69e40f80e51ab4d4847c78598e48e Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 11 Jan 2015 18:15:31 -0500 Subject: utils.go: Separate history by repository Use a different history list for each repo. Now the YAML file is organised in this way: /path/to/repo: - branch name - branch name /path/to/another/repo: - branch name --- utils/utils.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/utils/utils.go b/utils/utils.go index 0c7760e..82da017 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -15,10 +15,6 @@ import ( var history_file string = ".git-checkout-history" -type BranchList struct { - Branches []string -} - func getHomeDir() string { usr, err := user.Current() if err != nil { @@ -72,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) @@ -85,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 { @@ -104,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) @@ -117,5 +115,5 @@ func Branches() []string { log.Fatal(err) } - return branchList.Branches + return branchList[currentGitDir()] } -- cgit v1.2.3