-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.go
222 lines (175 loc) · 5.07 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
package vcsview
import (
"bufio"
"fmt"
"os"
"os/exec"
"regexp"
"runtime"
"strings"
"time"
)
const (
gitLogFormat = "%H%n%P%n%an%n%ae%n%ad%n%s"
gitLogDateLayout = "Mon Jan 2 15:04:05 2006 -0700"
)
// CLI wrapper for GIT
type Git struct {
Cli
}
// add specific params to command
func (g *Git) createCommand(dir string, params ...string) *exec.Cmd {
return g.Cli.command(dir, append([]string{"--no-pager"}, params...)...)
}
// Returns repository settings pathname
// like .git, .hg, etc.
func (g Git) RepositoryPathname() string {
return ".git"
}
// Check Git version
// returns error if git command not found, or it hasn't version arguments
func (g Git) Version() (string, error) {
versionPattern := regexp.MustCompile(`([\d]+\.?([\d]+)?\.([\d]+)?)`)
var (
result string
done = make(chan interface{}, 1)
)
cmd := g.createCommand(".", "--version")
reader := cmdReaderFunc(func(s *bufio.Scanner) {
for s.Scan() {
result += s.Text() + "\n"
}
done <- struct{}{}
})
e := g.executor(cmd, reader)
err := e.Run()
<- done
close(done)
return versionPattern.FindString(result), err
}
// Check project repository
// projectPath is absolute path to project path
// Returns error if repository not found at provided projectPath
// Returns nil if repository found
func (g Git) CheckRepository(projectPath string) error {
repoPath := projectPath+pathSeparator+g.RepositoryPathname()
stats, err := os.Stat(repoPath)
if err != nil {
return err
}
if !stats.IsDir() {
return fmt.Errorf("Git repository not found here: %s", projectPath)
}
return nil
}
// Check the repository status
// Throws an error if repository doesnt exists at the path
func (g Git) StatusRepository(projectPath string) (string, error) {
var (
result string
done = make(chan interface{}, 1)
)
cmd := g.createCommand(projectPath, "status", "--short")
reader := cmdReaderFunc(func(s *bufio.Scanner) {
for s.Scan() {
result += s.Text()+"\n"
}
done <- struct{}{}
})
e := g.executor(cmd, reader)
err := e.Run()
return result, err
}
// Fetch repository branches asynchronously
// ProjectPath is the absolute path to project with Git repository
func (g Git) ReadBranches(projectPath string, result chan Branch) *Executor {
// pattern to read branches line by line
p := regexp.MustCompile(`^\*?[\s+|\t]+(?P<id>[^\s]+)[\s+|\t]+(?P<head>[a-fA-F0-9]+)[\s+|\t]+(?P<message>.*)$`)
cmd := g.createCommand(projectPath, "branch", "-a", "-v")
reader := cmdReaderFunc(func(s *bufio.Scanner) {
for s.Scan() {
line := s.Bytes()
if !p.Match(line) {
continue
}
matches := p.FindSubmatch(line)
isCurrent := string(line[:1]) == "*"
id := string(matches[1])
head := string(matches[2])
result <- Branch{id, head, isCurrent}
}
})
return g.executor(cmd, reader)
}
// Wrapper for read commits from command line stdout
// Commits will going by such lines:
// 313604a7f4ecd265e56102fa2e22de35726f4687 <--- Commit sha256
// 1e16e4aeeef941bd037ed5f70e9d2abcf459ca2e 313604a7f4ecd265e56102fa2ee2de3572df4687 <--- Parents commit sha256
// Max Kalyabin <--- Author name
// [email protected] <--- Author email
// Wed Feb 27 14:51:45 2019 +0300 <--- Commit date and time
// read git commit <--- Commit message
func (g *Git) readCommitsPipe(s *bufio.Scanner, result chan Commit) {
data := make([]string, 6)
key := 0
for s.Scan() {
str := s.Text()
data[key] = str
key++
if key == 6 {
time, _ := time.Parse(gitLogDateLayout, data[4])
commit := Commit{
id: data[0],
parents: strings.Split(data[1], " "),
author: Contributor{
name: data[2],
email: data[3],
},
date: time,
message: data[5],
}
result <- commit
runtime.Gosched()
data = make([]string, 6)
key = 0
}
}
}
// Fetch repository commit by identifier asynchronously
// ProjectPath is the absolute path to project with Git repository
// CommitId is the sha256 commit identifier (or short copy)
func (g Git) ReadCommit(projectPath string, commitId string, result chan Commit) *Executor {
cmd := g.createCommand(projectPath, "show", "--quiet", commitId, `--pretty=format:`+gitLogFormat)
reader := cmdReaderFunc(func(s *bufio.Scanner) {
g.readCommitsPipe(s, result)
})
return g.executor(cmd, reader)
}
// Read commits history
// projectPath should contains absolute path to project with Git repository
// path should contains relative path of file for history
// If need provide whole repository history, path should be empty
// Branch should contain branch identifier if need get specified branch results
func (g Git) ReadHistory(projectPath string, path string, branch string, offset int, limit int, result chan Commit) *Executor {
if branch == "" {
branch = "*"
} else {
branch = "*"+branch+"*"
}
args := append(
make([]string, 0, 6),
"log",
`--format=`+gitLogFormat,
`-n`,
fmt.Sprintf("%d", limit),
fmt.Sprintf("--skip=%d", offset),
"--branches="+branch)
if path != "" {
args = append(args, "--", path)
}
cmd := g.createCommand(projectPath, args...)
reader := cmdReaderFunc(func(s *bufio.Scanner) {
g.readCommitsPipe(s, result)
})
return g.executor(cmd, reader)
}