-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgit.go
246 lines (216 loc) · 7.69 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
// Copyright ⓒ 2013 Alexander Bauer and Luke Evers (see LICENSE.md)
import (
"os/exec"
"strconv"
"strings"
)
type Commit struct {
SHA string // Full SHA of the commit
Author string // Author of the commit
Email string // Email attached to the commit
Time string // Relative time of the commit
Subject string // Subject of the commit
Body string // Body of the commit
}
const (
gitHttpBackend = "git-http-backend"
gitLogFmt = "%H%n%cr%n%an%n%ae%n%s%n%b"
gitLogSep = "----GROVE-LOG-SEPARATOR----"
)
type git struct {
Path string // Directory path
}
// Set a number of git variables.
func gitVarExecPath() (execPath string) {
// Use 'git --exec-path' to get the path of the git executables.
g := &git{}
execPath, _ = g.execute("--exec-path")
execPath = strings.TrimRight(execPath, "\n")
return
}
func (g *git) User() (user string) {
// Use 'git config --global user.name to retrieve the variable.
user, _ = g.execute("config", "--global", "user.name")
return strings.TrimRight(user, "\n")
}
func (g *git) Email() (email string) {
// Use 'git config user.email to retrieve the variable. Note that
// it does not use '--global' so it can vary by repository.
email, _ = g.execute("config", "user.email")
return strings.TrimRight(email, "\n")
}
func (g *git) Branch(ref string) (branch string) {
branch, _ = g.execute("rev-parse", "--abbrev-ref", ref)
return strings.TrimRight(branch, "\n")
}
func (g *git) Branches() (branches []string) {
// Retrieve a list of branches separated by "\n" and indented by
// either two spaces or "* ".
branchList, _ := g.execute("branch", "--no-color")
// Prepare the slice by counting the number of newlines, including
// the final one.
branches = make([]string, strings.Count(branchList, "\n"))
for n, b := range strings.Split(
strings.TrimRight(branchList, "\n"), "\n") {
// The call to strings.TrimLeft() will remove any number of
// leading spaces and asterisks.
branches[n] = strings.TrimLeft(b, "* ")
}
return
}
// TopLevel invokes git rev-parse in order to determine the top level
// of current git repository. If it is not called from a git
// repository, it will return a blank string.
func (g *git) TopLevel() (toplevel string) {
toplevel, _ = g.execute("rev-parse", "--show-toplevel")
return strings.TrimRight(toplevel, "\n")
}
// Prefix invokes git rev-parse in order to retrieve the
// relative-to-the-toplevel path of the current Path. If it is not
// called from a git repository, it will return a blank string.
func (g *git) Prefix() (prefix string) {
prefix, _ = g.execute("rev-parse", "--show-prefix")
return strings.TrimRight(prefix, "\n")
}
// IsDir invokes git cat-file to determine whether the given path is a
// file or directory within a git repository.
func (g *git) IsDir(ref, file string) (isDir bool, err error) {
output, err := g.execute("cat-file", "-t", ref+":"+file)
return (output == "tree\n"), err
}
// GetBranchDescription uses git config to retrieve the branch
// description from the repository configuration file, if it's set. It
// will attempt to parse branch names from refs like
// `<oldRef>..<newRef>`.
func (g *git) GetBranchDescription(branch string) (description string) {
// Attempt to parse the branch name if it looks like it's in the
// form of a comparison.
if idx := strings.LastIndex(branch, ".."); idx > -1 {
branch = branch[idx+2:] // Add 2 to ignore the ".."
} // Otherwise, just continue.
output, _ := g.execute("config", "branch."+branch+".description")
return strings.TrimRight(output, "\n")
}
// GetFile retrives the contents of a file from the repository. The
// commit is either a SHA or pointer (such as HEAD, or HEAD^).
func (g *git) GetFile(commit, file string) (contents []byte) {
contents, _ = g.executeB("--no-pager", "show", commit+":"+file)
return contents
}
// Retrieve a list of items in a directory from the repository. The
// commit is either a SHA or a pointer (such as HEAD, or HEAD^).
func (g *git) GetDir(commit, dir string) (files []string) {
output, _ := g.execute("--no-pager", "show", "--name-only", commit+":"+dir)
parts := strings.SplitN(output, "\n\n", 2) // Split on the blank line
if len(parts) == 2 && strings.HasPrefix(parts[0], "tree") {
return strings.Split(strings.TrimRight(parts[1], "\n"), "\n")
}
return
}
// SHA retrieves the short form (minimum 8 characters) of the given
// reference.
func (g *git) SHA(ref string) (sha string) {
commit, _ := g.execute("rev-parse", "--short=8", ref)
return strings.TrimRight(commit, "\n")
}
// Tags retrieves a list of all tag names from the repository.
func (g *git) Tags() (tags []string) {
t, _ := g.execute("tag", "--list")
return strings.Split(strings.TrimRight(t, "\n"), "\n")
}
func (g *git) TotalCommits() (commits int) {
c, _ := g.execute("rev-list", "--all")
return len(strings.Split(strings.TrimRight(c, "\n"), "\n"))
}
func (g *git) RefExists(ref string) (exists bool) {
// If the exit status of 'git rev-list -n 1 <ref>' is nonzero, the
// ref does not exist in the current repository.
_, err := g.execute("rev-list", "-n 1", ref)
return err == nil
}
// Commits parses the log and returns an array of Commit types, up to
// the given max.
func (g *git) Commits(ref string, max int) (commits []*Commit) {
return g.parseLog(ref, max)
}
// CommitsByFile retrieves a list of commits which modify or otherwise
// affect a file, up to the given maximum number of commits.
func (g *git) CommitsByFile(ref, file string, max int) (commits []*Commit) {
return g.parseLog(ref, max, "--follow", "--", file)
}
// parseLog is a low-level utility for calling `git log` and producing
// a []*Commit with no phantom commits. It invokes gitParseCommit to
// parse individual commits.
func (g *git) parseLog(ref string, max int, arguments ...string) (commits []*Commit) {
// First, we have to go through the arduous process of creating
// the command.
command := []string{"--no-pager", "log", ref,
"--format=format:" + gitLogFmt + gitLogSep}
if max > 0 {
command = append(command, "-n "+strconv.Itoa(max))
}
command = append(command, arguments...)
log, _ := g.execute(command...)
// Now we must parse the output of that command.
commitLogs := strings.Split(log, gitLogSep)
// We will have a phantom commit here, though, so we must remove
// it.
commitLogs = commitLogs[:len(commitLogs)-1]
commits = make([]*Commit, len(commitLogs))
for n, l := range commitLogs {
commits[n] = gitParseCommit(strings.Split(l, "\n"))
}
return
}
// gitParseCommit is a low-level utility for parsing log formats of
// the following format. They are generated like this by gitLogFmt.
// <full hash>
// <commit time relative>
// <author name>
// <nonwrapped commit message>
func gitParseCommit(log []string) (commit *Commit) {
commit = new(Commit)
for _, l := range log {
if len(commit.SHA) == 0 {
// If l is empty, then this will be run again.
commit.SHA = l
continue
}
if len(commit.Time) == 0 {
commit.Time = l
continue
}
if len(commit.Author) == 0 {
commit.Author = l
continue
}
if len(commit.Email) == 0 {
commit.Email = l
continue
}
if len(commit.Subject) == 0 {
commit.Subject = l
continue
}
commit.Body += l + "\n"
}
// Now, remove the trailing "\n" characters.
commit.Body = strings.TrimRight(commit.Body, "\n")
return
}
// execute invokes exec.Command() with the given command, arguments,
// and working directory. All CR ('\r') characters are removed in
// output.
func (g *git) execute(args ...string) (output string, err error) {
out, err := g.executeB(args...)
return string(out), err
}
func (g *git) executeB(args ...string) (output []byte, err error) {
cmd := exec.Command("git", args...)
if len(g.Path) != 0 {
cmd.Dir = g.Path
}
out, err := cmd.Output()
return out, err
}