summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
author6543 <6543@obermui.de>2020-06-18 17:22:43 +0200
committerGitHub <noreply@github.com>2020-06-18 18:22:43 +0300
commit113c99512b0ba755a065c346d0ccf8b66da23c15 (patch)
tree1cd6b289f498e351953466db6dca9d373dcd2989 /models
parent82343f4943aac6ba375a258c40f7791a1c8d9dfa (diff)
downloadgitea-113c99512b0ba755a065c346d0ccf8b66da23c15.tar.gz
gitea-113c99512b0ba755a065c346d0ccf8b66da23c15.zip
Fix commenting on non-utf8 encoded files (#11916) (#11950)
* Add comment on non-unicode line to force fail Signed-off-by: Andrew Thornton <art27@cantab.net> * Just quote/unquote patch Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'models')
-rw-r--r--models/issue_comment.go32
1 files changed, 31 insertions, 1 deletions
diff --git a/models/issue_comment.go b/models/issue_comment.go
index bb3c4be7ba..28f45b59f6 100644
--- a/models/issue_comment.go
+++ b/models/issue_comment.go
@@ -9,7 +9,9 @@ package models
import (
"fmt"
"regexp"
+ "strconv"
"strings"
+ "unicode/utf8"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
@@ -139,7 +141,8 @@ type Comment struct {
RenderedContent string `xorm:"-"`
// Path represents the 4 lines of code cemented by this comment
- Patch string `xorm:"TEXT"`
+ Patch string `xorm:"-"`
+ PatchQuoted string `xorm:"TEXT patch"`
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
@@ -183,6 +186,33 @@ func (c *Comment) loadIssue(e Engine) (err error) {
return
}
+// BeforeInsert will be invoked by XORM before inserting a record
+func (c *Comment) BeforeInsert() {
+ c.PatchQuoted = c.Patch
+ if !utf8.ValidString(c.Patch) {
+ c.PatchQuoted = strconv.Quote(c.Patch)
+ }
+}
+
+// BeforeUpdate will be invoked by XORM before updating a record
+func (c *Comment) BeforeUpdate() {
+ c.PatchQuoted = c.Patch
+ if !utf8.ValidString(c.Patch) {
+ c.PatchQuoted = strconv.Quote(c.Patch)
+ }
+}
+
+// AfterLoad is invoked from XORM after setting the values of all fields of this object.
+func (c *Comment) AfterLoad(session *xorm.Session) {
+ c.Patch = c.PatchQuoted
+ if len(c.PatchQuoted) > 0 && c.PatchQuoted[0] == '"' {
+ unquoted, err := strconv.Unquote(c.PatchQuoted)
+ if err == nil {
+ c.Patch = unquoted
+ }
+ }
+}
+
func (c *Comment) loadPoster(e Engine) (err error) {
if c.PosterID <= 0 || c.Poster != nil {
return nil