From eaa685ac9b2dafe84ad3a051f1ab801ae127bdb3 Mon Sep 17 00:00:00 2001 From: Sam Coe Date: Thu, 1 Oct 2020 16:22:11 +0200 Subject: [PATCH] Use custom scanLines function for RegexWriter --- pkg/cmd/pr/create/create.go | 7 +++---- pkg/cmd/pr/create/regex_writer.go | 18 +++++++++++++++++- pkg/cmd/pr/create/regex_writer_test.go | 14 +++++++------- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/pkg/cmd/pr/create/create.go b/pkg/cmd/pr/create/create.go index 12a1a9f2b..bbb2e39d9 100644 --- a/pkg/cmd/pr/create/create.go +++ b/pkg/cmd/pr/create/create.go @@ -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++ diff --git a/pkg/cmd/pr/create/regex_writer.go b/pkg/cmd/pr/create/regex_writer.go index 101a2cea5..5c13c71b7 100644 --- a/pkg/cmd/pr/create/regex_writer.go +++ b/pkg/cmd/pr/create/regex_writer.go @@ -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 +} diff --git a/pkg/cmd/pr/create/regex_writer_test.go b/pkg/cmd/pr/create/regex_writer_test.go index e228c58aa..6efda0840 100644 --- a/pkg/cmd/pr/create/regex_writer_test.go +++ b/pkg/cmd/pr/create/regex_writer_test.go @@ -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, }, },