forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.go
More file actions
69 lines (64 loc) · 1.58 KB
/
git.go
File metadata and controls
69 lines (64 loc) · 1.58 KB
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
package create
import (
"bufio"
"bytes"
"io"
"os/exec"
"regexp"
)
type gitClientExec struct {
gitCommand func(args ...string) (*exec.Cmd, error)
}
// Push publishes a branch to a git remote and filters out "Create a pull request by visiting <URL>" lines
// before forwarding them to stderr.
func (g *gitClientExec) Push(args []string, stdout io.Writer, stderr io.Writer) error {
args = append([]string{"push"}, args...)
pushCmd, err := g.gitCommand(args...)
if err != nil {
return err
}
pushCmd.Stdout = stdout
r, err := pushCmd.StderrPipe()
if err != nil {
return err
}
if err = pushCmd.Start(); err != nil {
return err
}
if err = filterLines(stderr, r, gitPushRegexp); err != nil {
return err
}
return pushCmd.Wait()
}
func filterLines(w io.Writer, r io.Reader, re *regexp.Regexp) error {
s := bufio.NewScanner(r)
s.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
i := bytes.IndexAny(data, "\r\n")
if i >= 0 {
// encompass both CR & LF characters if they appear together
if data[i] == '\r' && len(data) > i+1 && data[i+1] == '\n' {
return i + 2, data[0 : i+2], nil
}
return i + 1, data[0 : i+1], nil
}
if atEOF {
return len(data), data, nil
}
// Request more data.
return 0, nil, nil
})
for s.Scan() {
line := s.Bytes()
if !re.Match(line) {
_, err := w.Write(line)
if err != nil {
return err
}
}
}
return s.Err()
}
var gitPushRegexp = regexp.MustCompile(`^remote: (Create a pull request.*by visiting|[[:space:]]*https://.*/pull/new/)`)