-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (81 loc) · 1.84 KB
/
main.go
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
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"os/exec"
"strings"
)
var commits []string
func main() {
fmt.Println("Initializing gwalk v0.1.0")
branch := flag.String("b", "master", "branch to use")
flag.Parse()
fmt.Println("Checking out branch ", *branch)
cmd := exec.Command("git", "checkout", *branch)
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
history := generateHistory()
commits = strings.Split(string(history), "\n")
lengthOfCommits := len(commits) - 2
fmt.Printf("%d commits found ", lengthOfCommits)
fmt.Println("Available actions - init,next,prev,exit")
i := 0
for {
fmt.Print(">")
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
switch strings.TrimRight(text, "\n") {
case "init":
fmt.Println("Checking out initial commit")
i = 0
checkout(i, commits)
case "next":
fmt.Println("Moving forward by one commit")
if i == lengthOfCommits {
fmt.Println("Cannot move forward. Already at the latest commit.")
break
}
i++
checkout(i, commits)
case "prev":
fmt.Println("Moving backward by one commit")
if i == 0 {
fmt.Println("Cannot move backward. Already at first commit.")
break
}
i--
checkout(i, commits)
case "exit":
os.Exit(0)
default:
fmt.Println(".....")
}
}
}
func checkout(i int, commit []string) {
cmd := exec.Command("git", "checkout", commits[i])
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(fmt.Sprint(err) + ": " + string(output))
os.Exit(1)
}
return
}
func generateHistory() []byte {
var (
cmdOut []byte
err error
)
cmdName := "git"
cmdArgs := []string{"log", "--reverse", "--pretty=%h"}
if cmdOut, err = exec.Command(cmdName, cmdArgs...).Output(); err != nil {
fmt.Fprintln(os.Stderr, "There was an error running git log command: ", err)
os.Exit(1)
}
return cmdOut
}