Skip to content

Commit 2bcb3b8

Browse files
zeripathYohann Delafollye
authored and
Yohann Delafollye
committed
Fix commenting on non-utf8 encoded files (go-gitea#11916)
* Add comment on non-unicode line to force fail Signed-off-by: Andrew Thornton <[email protected]> * Just quote/unquote patch Signed-off-by: Andrew Thornton <[email protected]>
1 parent d2ba000 commit 2bcb3b8

File tree

6 files changed

+41
-4
lines changed

6 files changed

+41
-4
lines changed

integrations/api_pull_review_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,19 @@ func TestAPIPullReview(t *testing.T) {
8686
Body: "first old line",
8787
OldLineNum: 1,
8888
NewLineNum: 0,
89+
}, {
90+
Path: "iso-8859-1.txt",
91+
Body: "this line contains a non-utf-8 character",
92+
OldLineNum: 0,
93+
NewLineNum: 1,
8994
},
9095
},
9196
})
9297
resp = session.MakeRequest(t, req, http.StatusOK)
9398
DecodeJSON(t, resp, &review)
9499
assert.EqualValues(t, 6, review.ID)
95100
assert.EqualValues(t, "PENDING", review.State)
96-
assert.EqualValues(t, 2, review.CodeCommentsCount)
101+
assert.EqualValues(t, 3, review.CodeCommentsCount)
97102

98103
// test SubmitPullReview
99104
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews/%d?token=%s", repo.OwnerName, repo.Name, pullIssue.Index, review.ID, token), &api.SubmitPullReviewOptions{
@@ -104,7 +109,7 @@ func TestAPIPullReview(t *testing.T) {
104109
DecodeJSON(t, resp, &review)
105110
assert.EqualValues(t, 6, review.ID)
106111
assert.EqualValues(t, "APPROVED", review.State)
107-
assert.EqualValues(t, 2, review.CodeCommentsCount)
112+
assert.EqualValues(t, 3, review.CodeCommentsCount)
108113

109114
// test DeletePullReview
110115
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews?token=%s", repo.OwnerName, repo.Name, pullIssue.Index, token), &api.CreatePullReviewOptions{

integrations/gitea-repositories-meta/user2/repo1.git/objects/90/dcd07da077d1e7cd6032b52d1f79ae2b5f19b2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
xe��N�0D�#���4
2+
J�A�5����,�x�zsV���5�D��ػ�7�,=��o.�E卢�q5J=���� r�=>4��O!�Š�����6ms�8��&\Ea�t�T��I�z��ԅ! �dso@a��&�K5�B)�r4��Q�`Y�L���b ���o`�a�3�@(��e�ԭ5���H�\s�H�9�9R�3)��@�S��_"��4sE0�R��.�U|/�m�ۿ]U��z�
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4a357436d925b5c974181ff12a994538ddc5a269
1+
5f22f7d0d95d614d25a5b68592adb345a4b5c7fd

models/issue_comment.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import (
1111
"encoding/json"
1212
"fmt"
1313
"regexp"
14+
"strconv"
1415
"strings"
16+
"unicode/utf8"
1517

1618
"code.gitea.io/gitea/modules/git"
1719
"code.gitea.io/gitea/modules/log"
@@ -143,7 +145,8 @@ type Comment struct {
143145
RenderedContent string `xorm:"-"`
144146

145147
// Path represents the 4 lines of code cemented by this comment
146-
Patch string `xorm:"TEXT"`
148+
Patch string `xorm:"-"`
149+
PatchQuoted string `xorm:"TEXT patch"`
147150

148151
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
149152
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
@@ -199,6 +202,33 @@ func (c *Comment) loadIssue(e Engine) (err error) {
199202
return
200203
}
201204

205+
// BeforeInsert will be invoked by XORM before inserting a record
206+
func (c *Comment) BeforeInsert() {
207+
c.PatchQuoted = c.Patch
208+
if !utf8.ValidString(c.Patch) {
209+
c.PatchQuoted = strconv.Quote(c.Patch)
210+
}
211+
}
212+
213+
// BeforeUpdate will be invoked by XORM before updating a record
214+
func (c *Comment) BeforeUpdate() {
215+
c.PatchQuoted = c.Patch
216+
if !utf8.ValidString(c.Patch) {
217+
c.PatchQuoted = strconv.Quote(c.Patch)
218+
}
219+
}
220+
221+
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
222+
func (c *Comment) AfterLoad(session *xorm.Session) {
223+
c.Patch = c.PatchQuoted
224+
if len(c.PatchQuoted) > 0 && c.PatchQuoted[0] == '"' {
225+
unquoted, err := strconv.Unquote(c.PatchQuoted)
226+
if err == nil {
227+
c.Patch = unquoted
228+
}
229+
}
230+
}
231+
202232
func (c *Comment) loadPoster(e Engine) (err error) {
203233
if c.PosterID <= 0 || c.Poster != nil {
204234
return nil

0 commit comments

Comments
 (0)