Skip to content

Commit ac0fb36

Browse files
Ilya33zeripath
authored andcommitted
Allow to merge if file path contains " or \ (#8629)
* if a filename in a repository contains " or \ the owner can't merge pull request with this files because "git diff-tree" adds double quotes to that filepath example: filepath is ab"cd but "git diff-tree" returns "ab\"cd" now, when the owner click "Merge Pull Request" button the server returns 500 this commit fix it Signed-off-by: Ilya Pavlov <ilux@cpan.org> * add -z option to getDiffTree escape spec symbols for sparse-checkout Signed-off-by: Ilya Pavlov <ilux@cpan.org> * go fmt Signed-off-by: Ilya Pavlov <ilux@cpan.org> * typo Signed-off-by: Ilya Pavlov <ilux@cpan.org> * escape '\' escape all spaces and '!' * use regexp.ReplaceAllString() Signed-off-by: Ilya Pavlov <ilux@cpan.org> * strings.ReplaceAll was added in go 1.12 Signed-off-by: Ilya Pavlov <ilux@cpan.org> * add '\' to regexp.MustCompile Signed-off-by: Ilya Pavlov <ilux@cpan.org>
1 parent 4ee986e commit ac0fb36

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

services/pull/merge.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"io/ioutil"
1313
"os"
1414
"path/filepath"
15+
"regexp"
1516
"strings"
1617
"time"
1718

@@ -374,16 +375,31 @@ func Merge(pr *models.PullRequest, doer *models.User, baseGitRepo *git.Repositor
374375
return nil
375376
}
376377

378+
var escapedSymbols = regexp.MustCompile(`([*[?! \\])`)
379+
377380
func getDiffTree(repoPath, baseBranch, headBranch string) (string, error) {
378381
getDiffTreeFromBranch := func(repoPath, baseBranch, headBranch string) (string, error) {
379382
var outbuf, errbuf strings.Builder
380383
// Compute the diff-tree for sparse-checkout
381-
if err := git.NewCommand("diff-tree", "--no-commit-id", "--name-only", "-r", "--root", baseBranch, headBranch, "--").RunInDirPipeline(repoPath, &outbuf, &errbuf); err != nil {
384+
if err := git.NewCommand("diff-tree", "--no-commit-id", "--name-only", "-r", "-z", "--root", baseBranch, headBranch, "--").RunInDirPipeline(repoPath, &outbuf, &errbuf); err != nil {
382385
return "", fmt.Errorf("git diff-tree [%s base:%s head:%s]: %s", repoPath, baseBranch, headBranch, errbuf.String())
383386
}
384387
return outbuf.String(), nil
385388
}
386389

390+
scanNullTerminatedStrings := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
391+
if atEOF && len(data) == 0 {
392+
return 0, nil, nil
393+
}
394+
if i := bytes.IndexByte(data, '\x00'); i >= 0 {
395+
return i + 1, data[0:i], nil
396+
}
397+
if atEOF {
398+
return len(data), data, nil
399+
}
400+
return 0, nil, nil
401+
}
402+
387403
list, err := getDiffTreeFromBranch(repoPath, baseBranch, headBranch)
388404
if err != nil {
389405
return "", err
@@ -392,8 +408,14 @@ func getDiffTree(repoPath, baseBranch, headBranch string) (string, error) {
392408
// Prefixing '/' for each entry, otherwise all files with the same name in subdirectories would be matched.
393409
out := bytes.Buffer{}
394410
scanner := bufio.NewScanner(strings.NewReader(list))
411+
scanner.Split(scanNullTerminatedStrings)
395412
for scanner.Scan() {
396-
fmt.Fprintf(&out, "/%s\n", scanner.Text())
413+
filepath := scanner.Text()
414+
// escape '*', '?', '[', spaces and '!' prefix
415+
filepath = escapedSymbols.ReplaceAllString(filepath, `\$1`)
416+
// no necessary to escape the first '#' symbol because the first symbol is '/'
417+
fmt.Fprintf(&out, "/%s\n", filepath)
397418
}
419+
398420
return out.String(), nil
399421
}

0 commit comments

Comments
 (0)