Skip to content

Commit 015f9ce

Browse files
committed
Just quote/unquote patch
Signed-off-by: Andrew Thornton <[email protected]>
1 parent 4d7a16f commit 015f9ce

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

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)