Skip to content

Commit

Permalink
Merge pull request cli#9115 from anda3/feature/nonascii-path-regex-im…
Browse files Browse the repository at this point in the history
…provement
  • Loading branch information
williammartin authored May 23, 2024
2 parents 105bafd + a66a646 commit 99568e6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
26 changes: 22 additions & 4 deletions pkg/cmd/pr/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ func NewCmdDiff(f *cmdutil.Factory, runF func(*DiffOptions) error) *cobra.Comman
Use: "diff [<number> | <url> | <branch>]",
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),
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/pr/diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down

0 comments on commit 99568e6

Please sign in to comment.