diff --git a/pkg/cmd/pr/diff/diff.go b/pkg/cmd/pr/diff/diff.go index c837a530ad1..acf17462c5e 100644 --- a/pkg/cmd/pr/diff/diff.go +++ b/pkg/cmd/pr/diff/diff.go @@ -51,11 +51,11 @@ func NewCmdDiff(f *cmdutil.Factory, runF func(*DiffOptions) error) *cobra.Comman Use: "diff [ | | ]", Short: "View changes in a pull request", Long: heredoc.Docf(` - View changes in a pull request. + View changes in a pull request. Without an argument, the pull request that belongs to the current branch is selected. - + With %[1]s--web%[1]s flag, open the pull request diff in a web browser instead. `, "`"), Args: cobra.MaximumNArgs(1), @@ -274,11 +274,29 @@ func changedFilesNames(w io.Writer, r io.Reader) error { return err } - pattern := regexp.MustCompile(`(?:^|\n)diff\s--git.*\sb/(.*)`) + // This is kind of a gnarly regex. We're looking lines of the format: + // diff --git a/9114-triage b/9114-triage + // diff --git "a/hello-\360\237\230\200-world" "b/hello-\360\237\230\200-world" + // + // From these lines we would look to extract: + // 9114-triage + // "hello-\360\237\230\200-world" + // + // Note that the b/ is removed but in the second case the preceeding quote remains. + // This is important for how git handles filenames that would be quoted with core.quotePath. + // https://git-scm.com/docs/git-config#Documentation/git-config.txt-corequotePath + // + // Thus we capture the quote if it exists, and everything that follows the b/ + // We then concatenate those two capture groups together which for the examples above would be: + // `` + 9114-triage + // `"`` + hello-\360\237\230\200-world" + // + // Where I'm using the `` to indicate a string to avoid confusion with the " character. + pattern := regexp.MustCompile(`(?:^|\n)diff\s--git.*\s(["]?)b/(.*)`) matches := pattern.FindAllStringSubmatch(string(diff), -1) for _, val := range matches { - name := strings.TrimSpace(val[1]) + name := strings.TrimSpace(val[1] + val[2]) if _, err := w.Write([]byte(name + "\n")); err != nil { return err } diff --git a/pkg/cmd/pr/diff/diff_test.go b/pkg/cmd/pr/diff/diff_test.go index cff9f04c8bc..be8c48428b3 100644 --- a/pkg/cmd/pr/diff/diff_test.go +++ b/pkg/cmd/pr/diff/diff_test.go @@ -358,6 +358,10 @@ func Test_changedFileNames(t *testing.T) { input: fmt.Sprintf("diff --git a/baz.go b/baz.go\n--- a/baz.go\n+++ b/baz.go\n+foo\n-b%sr", strings.Repeat("a", 2*lineBufferSize)), output: "baz.go\n", }, + { + input: "diff --git \"a/\343\202\212\343\203\274\343\201\251\343\201\277\343\203\274.md\" \"b/\343\202\212\343\203\274\343\201\251\343\201\277\343\203\274.md\"", + output: "\"\343\202\212\343\203\274\343\201\251\343\201\277\343\203\274.md\"\n", + }, } for _, tt := range inputs { buf := bytes.Buffer{}