Skip to content

Commit f104898

Browse files
zeripathtechknowlogick
authored andcommitted
Allow to merge if file path contains " or \ (#8629) (#8772)
* 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 83d04df commit f104898

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

modules/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

1718
"code.gitea.io/gitea/models"
@@ -324,16 +325,31 @@ func Merge(pr *models.PullRequest, doer *models.User, baseGitRepo *git.Repositor
324325
return nil
325326
}
326327

328+
var escapedSymbols = regexp.MustCompile(`([*[?! \\])`)
329+
327330
func getDiffTree(repoPath, baseBranch, headBranch string) (string, error) {
328331
getDiffTreeFromBranch := func(repoPath, baseBranch, headBranch string) (string, error) {
329332
var outbuf, errbuf strings.Builder
330333
// Compute the diff-tree for sparse-checkout
331-
if err := git.NewCommand("diff-tree", "--no-commit-id", "--name-only", "-r", "--root", baseBranch, headBranch, "--").RunInDirPipeline(repoPath, &outbuf, &errbuf); err != nil {
334+
if err := git.NewCommand("diff-tree", "--no-commit-id", "--name-only", "-r", "-z", "--root", baseBranch, headBranch, "--").RunInDirPipeline(repoPath, &outbuf, &errbuf); err != nil {
332335
return "", fmt.Errorf("git diff-tree [%s base:%s head:%s]: %s", repoPath, baseBranch, headBranch, errbuf.String())
333336
}
334337
return outbuf.String(), nil
335338
}
336339

340+
scanNullTerminatedStrings := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
341+
if atEOF && len(data) == 0 {
342+
return 0, nil, nil
343+
}
344+
if i := bytes.IndexByte(data, '\x00'); i >= 0 {
345+
return i + 1, data[0:i], nil
346+
}
347+
if atEOF {
348+
return len(data), data, nil
349+
}
350+
return 0, nil, nil
351+
}
352+
337353
list, err := getDiffTreeFromBranch(repoPath, baseBranch, headBranch)
338354
if err != nil {
339355
return "", err
@@ -342,8 +358,14 @@ func getDiffTree(repoPath, baseBranch, headBranch string) (string, error) {
342358
// Prefixing '/' for each entry, otherwise all files with the same name in subdirectories would be matched.
343359
out := bytes.Buffer{}
344360
scanner := bufio.NewScanner(strings.NewReader(list))
361+
scanner.Split(scanNullTerminatedStrings)
345362
for scanner.Scan() {
346-
fmt.Fprintf(&out, "/%s\n", scanner.Text())
363+
filepath := scanner.Text()
364+
// escape '*', '?', '[', spaces and '!' prefix
365+
filepath = escapedSymbols.ReplaceAllString(filepath, `\$1`)
366+
// no necessary to escape the first '#' symbol because the first symbol is '/'
367+
fmt.Fprintf(&out, "/%s\n", filepath)
347368
}
369+
348370
return out.String(), nil
349371
}

0 commit comments

Comments
 (0)