Skip to content

Commit eaa685a

Browse files
committed
Use custom scanLines function for RegexWriter
1 parent 2129936 commit eaa685a

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

pkg/cmd/pr/create/create.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"net/http"
77
"net/url"
8-
"os"
98
"regexp"
109
"strings"
1110
"time"
@@ -431,9 +430,9 @@ func createRun(opts *CreateOptions) error {
431430
pushTries := 0
432431
maxPushTries := 3
433432
for {
434-
regexp := regexp.MustCompile("^remote: (|Create a pull request for '.*' on GitHub by visiting:.*|.*https://github\\.com/.*/pull/new/.*)$")
435-
cmdErr := NewRegexWriter(os.Stderr, regexp, "")
436-
cmdOut := os.Stdout
433+
regexp := regexp.MustCompile("^remote: (Create a pull request.*by visiting|[[:space:]]*https://.*/pull/new/).*\n?$")
434+
cmdErr := NewRegexWriter(opts.IO.ErrOut, regexp, "")
435+
cmdOut := opts.IO.Out
437436
if err := git.Push(headRemote.Name, fmt.Sprintf("HEAD:%s", headBranch), cmdOut, cmdErr); err != nil {
438437
if didForkRepo && pushTries < maxPushTries {
439438
pushTries++

pkg/cmd/pr/create/regex_writer.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ func (s RegexWriter) Write(data []byte) (int, error) {
2121
filtered := []byte{}
2222
repl := []byte(s.repl)
2323
scanner := bufio.NewScanner(bytes.NewReader(data))
24+
scanner.Split(scanLines)
2425

2526
for scanner.Scan() {
2627
b := scanner.Bytes()
2728
f := s.regexp.ReplaceAll(b, repl)
2829
if len(f) > 0 {
2930
filtered = append(filtered, f...)
30-
filtered = append(filtered, []byte("\n")...)
3131
}
3232
}
3333

@@ -44,3 +44,19 @@ func (s RegexWriter) Write(data []byte) (int, error) {
4444

4545
return len(data), nil
4646
}
47+
48+
func scanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
49+
if atEOF && len(data) == 0 {
50+
return 0, nil, nil
51+
}
52+
53+
if i := bytes.IndexByte(data, '\n'); i >= 0 {
54+
return i + 1, data[0 : i+1], nil
55+
}
56+
57+
if atEOF {
58+
return len(data), data, nil
59+
}
60+
61+
return 0, nil, nil
62+
}

pkg/cmd/pr/create/regex_writer_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func Test_Write(t *testing.T) {
3434
},
3535
output: output{
3636
wantsErr: false,
37-
out: "some input line that has right information\n",
37+
out: "some input line that has right information",
3838
length: 42,
3939
},
4040
},
@@ -47,7 +47,7 @@ func Test_Write(t *testing.T) {
4747
},
4848
output: output{
4949
wantsErr: false,
50-
out: "multiple tests\nin this\ninput tests\n",
50+
out: "multiple tests\nin this\ninput tests",
5151
length: 34,
5252
},
5353
},
@@ -60,7 +60,7 @@ func Test_Write(t *testing.T) {
6060
},
6161
output: output{
6262
wantsErr: false,
63-
out: "this line has no matches\n",
63+
out: "this line has no matches",
6464
length: 24,
6565
},
6666
},
@@ -94,12 +94,12 @@ func Test_Write(t *testing.T) {
9494
name: "multiple lines removed",
9595
input: input{
9696
in: "begining line\nremove this whole line\nremove this one also\nnot this one",
97-
regexp: regexp.MustCompile("^remove.*$"),
97+
regexp: regexp.MustCompile("(?s)^remove.*$"),
9898
repl: "",
9999
},
100100
output: output{
101101
wantsErr: false,
102-
out: "begining line\nnot this one\n",
102+
out: "begining line\nnot this one",
103103
length: 70,
104104
},
105105
},
@@ -114,12 +114,12 @@ func Test_Write(t *testing.T) {
114114
remote:
115115
output: more information
116116
`),
117-
regexp: regexp.MustCompile("^remote: (|Create a pull request for '.*' on GitHub by visiting:.*|.*https://github\\.com/.*/pull/new/.*)$"),
117+
regexp: regexp.MustCompile("^remote: (Create a pull request.*by visiting|[[:space:]]*https://.*/pull/new/).*\n?$"),
118118
repl: "",
119119
},
120120
output: output{
121121
wantsErr: false,
122-
out: "output: some information\noutput: more information\n",
122+
out: "output: some information\nremote: \nremote: \noutput: more information\n",
123123
length: 192,
124124
},
125125
},

0 commit comments

Comments
 (0)