diff options
author | zeripath <art27@cantab.net> | 2021-02-28 13:19:51 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-28 15:19:51 +0200 |
commit | be25afc6def888f9ec83b3e8ebe1c1fed216b19a (patch) | |
tree | db1addef6601427c133f8ab46887fb2445457a20 /services/pull | |
parent | 90bf1e7961cf428c378e93726dcb7f2cb6da24c1 (diff) | |
download | gitea-be25afc6def888f9ec83b3e8ebe1c1fed216b19a.tar.gz gitea-be25afc6def888f9ec83b3e8ebe1c1fed216b19a.zip |
Fix a couple of CommentAsPatch issues. (#14804) (#14820)
Backport #14804
* CutDiffAroundLine makes the incorrect assumption that `---` and `+++` always represent part of the header of a diff.
This PR adds a flag to its parsing to prevent this problem and adds a streaming parsing technique to CutDiffAroundLine using an io.pipe instead of just sending data to an unbounded buffer.
Fix #14711
* Handle unquoted comment patch files
When making comment patches unfortunately the patch does not always quote the filename
This makes the diff --git header ambiguous again.
This PR finally adds handling for ambiguity in to parse patch
Fix #14812
* Add in testing for no error
There is no way currently for CutDiffAroundLine in this test to cause an
error however, it should still be tested.
Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'services/pull')
-rw-r--r-- | services/pull/review.go | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/services/pull/review.go b/services/pull/review.go index 8994a9e78a..08168fa40a 100644 --- a/services/pull/review.go +++ b/services/pull/review.go @@ -6,13 +6,14 @@ package pull import ( - "bytes" "fmt" + "io" "regexp" "strings" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/setting" ) @@ -179,11 +180,24 @@ func createCodeComment(doer *models.User, repo *models.Repository, issue *models if len(commitID) == 0 { commitID = headCommitID } - patchBuf := new(bytes.Buffer) - if err := git.GetRepoRawDiffForFile(gitRepo, pr.MergeBase, headCommitID, git.RawDiffNormal, treePath, patchBuf); err != nil { - return nil, fmt.Errorf("GetRawDiffForLine[%s, %s, %s, %s]: %v", gitRepo.Path, pr.MergeBase, headCommitID, treePath, err) + reader, writer := io.Pipe() + defer func() { + _ = reader.Close() + _ = writer.Close() + }() + go func() { + if err := git.GetRepoRawDiffForFile(gitRepo, pr.MergeBase, headCommitID, git.RawDiffNormal, treePath, writer); err != nil { + _ = writer.CloseWithError(fmt.Errorf("GetRawDiffForLine[%s, %s, %s, %s]: %v", gitRepo.Path, pr.MergeBase, headCommitID, treePath, err)) + return + } + _ = writer.Close() + }() + + patch, err = git.CutDiffAroundLine(reader, int64((&models.Comment{Line: line}).UnsignedLine()), line < 0, setting.UI.CodeCommentLines) + if err != nil { + log.Error("Error whilst generating patch: %v", err) + return nil, err } - patch = git.CutDiffAroundLine(patchBuf, int64((&models.Comment{Line: line}).UnsignedLine()), line < 0, setting.UI.CodeCommentLines) } return models.CreateComment(&models.CreateCommentOptions{ Type: models.CommentTypeCode, |