1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
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"
func getHomeDir() string {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
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) {
return os.Create(file_path)
} else {
return os.Open(file_path)
}
}
func Store(branch string) {
rcfile, err := OpenHistoryFile()
if err != nil {
log.Fatal(err)
}
rcfile.Close()
file_path := getHomeDir() + "/" + history_file
data, err := ioutil.ReadFile(file_path)
if err != nil {
log.Fatal(err)
}
branchList := make(map[string][]string)
err = yaml.Unmarshal(data, &branchList)
if err != nil {
log.Fatal(err)
}
current_git_dir := currentGitDir()
branchList[current_git_dir] = append([]string{branch}, branchList[current_git_dir]...)
data, err = yaml.Marshal(&branchList)
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile(file_path, data, 0644)
if err != nil {
log.Fatal(err)
}
}
func Branches() []string {
branchList := make(map[string][]string)
file_path := getHomeDir() + "/" + history_file
data, err := ioutil.ReadFile(file_path)
if err != nil {
log.Fatal("ERROR: No checkout history file. Run `checkout-store` to create it.")
}
err = yaml.Unmarshal(data, &branchList)
if err != nil {
log.Fatal(err)
}
return branchList[currentGitDir()]
}
|