-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgit.go
163 lines (134 loc) · 3.54 KB
/
git.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
type gitRemote struct {
base string
path string
}
type gitDir string
/// Get working directory
func (here gitDir) Getwd() (string, error) {
return filepath.Abs(strings.TrimSuffix(string(here), ".git"))
}
type ErrUnknownRemote string
func (e ErrUnknownRemote) Error() string {
return fmt.Sprintf("Could not find remote: %s\n", e)
}
func getRemoteUrlFromRemoteVOutput(remoteName string, output []byte) (string, error) {
lines := strings.Split(string(output), "\n")
prefix := remoteName + "\t"
for _, line := range lines {
if strings.HasPrefix(line, prefix) {
remoteLine := strings.TrimPrefix(line, prefix)
remoteLine = strings.TrimSuffix(remoteLine, " (fetch)")
remoteLine = strings.TrimSuffix(remoteLine, " (push)")
return remoteLine, nil
}
}
return "", ErrUnknownRemote(remoteName)
}
func (here gitDir) checkout(arg string) error {
// Get working directory
wd, err := here.Getwd()
if nil != err {
return err
}
// Fetch first
fetchCmd := exec.Command("git", "fetch")
fetchCmd.Dir = wd
fetchCmd.Stdout = os.Stdout
fetchCmd.Stdin = os.Stdin
err = fetchCmd.Run()
if nil != err {
return err
}
cmd := exec.Command("git", "checkout", "origin/"+arg)
cmd.Dir = wd
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func (here gitDir) diff2(left, right string) error {
// Get working directory
wd, err := here.Getwd()
if nil != err {
return err
}
remotes := map[string]bool{"origin": true}
// Left remote?
if strings.Contains(left, "/") {
leftRemote := strings.Split(left, "/")[0]
remotes[leftRemote] = true
} else {
left = "origin/" + left
}
if strings.Contains(right, "/") {
rightRemote := strings.Split(right, "/")[0]
remotes[rightRemote] = true
} else {
right = "origin/" + right
}
for remote, _ := range remotes {
// Fetch first
fetchCmd := exec.Command("git", "fetch", remote)
fetchCmd.Dir = wd
fetchCmd.Stdout = os.Stdout
fetchCmd.Stdin = os.Stdin
err = fetchCmd.Run()
if nil != err {
return err
}
}
cmd := exec.Command("git", "diff", left+".."+right, "--")
cmd.Dir = wd
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func (here gitDir) getCurrentBranch() (string, error) {
cmd := exec.Command("git", "--git-dir", string(here), "name-rev", "--name-only", "HEAD")
output, err := cmd.CombinedOutput()
if nil != err {
return "", err
}
ref := strings.Trim(string(output), "\n\t ")
// If ref name, eg. remotes/origin/my-branch
if strings.Contains(ref, "/") {
refParts := strings.Split(ref, "/")
ref = refParts[len(refParts)-1]
}
return ref, nil
}
func parseRemote(remoteAddr string) (remote gitRemote) {
// Strip user info
if atIndex := strings.IndexByte(remoteAddr, '@'); atIndex >= 0 {
remoteAddr = remoteAddr[atIndex+1 : len(remoteAddr)]
}
if schemeIndex := strings.Index(remoteAddr, "://"); schemeIndex >= 0 {
remoteAddr = remoteAddr[schemeIndex+3 : len(remoteAddr)]
}
if i := strings.IndexAny(remoteAddr, ":/"); i >= 0 {
remote = gitRemote{
remoteAddr[0:i],
remoteAddr[i+1 : len(remoteAddr)],
}
} else {
// relative remote? Not interested anyway
}
remote.path = strings.TrimSuffix(remote.path, ".git")
return remote
}
/// Get origin for given remote name
func (here gitDir) getRemoteUrl(remoteName string) (string, error) {
cmd := exec.Command("git", "--git-dir", string(here), "remote", "-v")
output, err := cmd.CombinedOutput()
if nil != err {
return "", fmt.Errorf("%s\n", output)
}
return getRemoteUrlFromRemoteVOutput(remoteName, output)
}