Use custom scanLines function for RegexWriter

This commit is contained in:
Sam Coe 2020-10-01 16:22:11 +02:00
parent 212993632b
commit eaa685ac9b
No known key found for this signature in database
GPG key ID: 8E322C20F811D086
3 changed files with 27 additions and 12 deletions

View file

@ -5,7 +5,6 @@ import (
"fmt"
"net/http"
"net/url"
"os"
"regexp"
"strings"
"time"
@ -431,9 +430,9 @@ func createRun(opts *CreateOptions) error {
pushTries := 0
maxPushTries := 3
for {
regexp := regexp.MustCompile("^remote: (|Create a pull request for '.*' on GitHub by visiting:.*|.*https://github\\.com/.*/pull/new/.*)$")
cmdErr := NewRegexWriter(os.Stderr, regexp, "")
cmdOut := os.Stdout
regexp := regexp.MustCompile("^remote: (Create a pull request.*by visiting|[[:space:]]*https://.*/pull/new/).*\n?$")
cmdErr := NewRegexWriter(opts.IO.ErrOut, regexp, "")
cmdOut := opts.IO.Out
if err := git.Push(headRemote.Name, fmt.Sprintf("HEAD:%s", headBranch), cmdOut, cmdErr); err != nil {
if didForkRepo && pushTries < maxPushTries {
pushTries++

View file

@ -21,13 +21,13 @@ func (s RegexWriter) Write(data []byte) (int, error) {
filtered := []byte{}
repl := []byte(s.repl)
scanner := bufio.NewScanner(bytes.NewReader(data))
scanner.Split(scanLines)
for scanner.Scan() {
b := scanner.Bytes()
f := s.regexp.ReplaceAll(b, repl)
if len(f) > 0 {
filtered = append(filtered, f...)
filtered = append(filtered, []byte("\n")...)
}
}
@ -44,3 +44,19 @@ func (s RegexWriter) Write(data []byte) (int, error) {
return len(data), nil
}
func scanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.IndexByte(data, '\n'); i >= 0 {
return i + 1, data[0 : i+1], nil
}
if atEOF {
return len(data), data, nil
}
return 0, nil, nil
}

View file

@ -34,7 +34,7 @@ func Test_Write(t *testing.T) {
},
output: output{
wantsErr: false,
out: "some input line that has right information\n",
out: "some input line that has right information",
length: 42,
},
},
@ -47,7 +47,7 @@ func Test_Write(t *testing.T) {
},
output: output{
wantsErr: false,
out: "multiple tests\nin this\ninput tests\n",
out: "multiple tests\nin this\ninput tests",
length: 34,
},
},
@ -60,7 +60,7 @@ func Test_Write(t *testing.T) {
},
output: output{
wantsErr: false,
out: "this line has no matches\n",
out: "this line has no matches",
length: 24,
},
},
@ -94,12 +94,12 @@ func Test_Write(t *testing.T) {
name: "multiple lines removed",
input: input{
in: "begining line\nremove this whole line\nremove this one also\nnot this one",
regexp: regexp.MustCompile("^remove.*$"),
regexp: regexp.MustCompile("(?s)^remove.*$"),
repl: "",
},
output: output{
wantsErr: false,
out: "begining line\nnot this one\n",
out: "begining line\nnot this one",
length: 70,
},
},
@ -114,12 +114,12 @@ func Test_Write(t *testing.T) {
remote:
output: more information
`),
regexp: regexp.MustCompile("^remote: (|Create a pull request for '.*' on GitHub by visiting:.*|.*https://github\\.com/.*/pull/new/.*)$"),
regexp: regexp.MustCompile("^remote: (Create a pull request.*by visiting|[[:space:]]*https://.*/pull/new/).*\n?$"),
repl: "",
},
output: output{
wantsErr: false,
out: "output: some information\noutput: more information\n",
out: "output: some information\nremote: \nremote: \noutput: more information\n",
length: 192,
},
},