diff options
author | Teddy Wing | 2014-12-31 12:50:30 -0500 |
---|---|---|
committer | Teddy Wing | 2014-12-31 12:52:23 -0500 |
commit | 7d6ab76e8c5b382aff8a0cb7812c904f6dda9272 (patch) | |
tree | e83d523051bc02e767f837224ba01de96768d370 /utils/utils.go | |
parent | 2be3f192975944b7798ba0d4794af12101c2bcd0 (diff) | |
download | git-checkout-history-7d6ab76e8c5b382aff8a0cb7812c904f6dda9272.tar.bz2 |
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
Diffstat (limited to 'utils/utils.go')
-rw-r--r-- | utils/utils.go | 35 |
1 files changed, 35 insertions, 0 deletions
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) { |