diff options
Diffstat (limited to 'models')
-rw-r--r-- | models/action.go | 9 | ||||
-rw-r--r-- | models/error.go | 22 | ||||
-rw-r--r-- | models/git_diff.go | 6 | ||||
-rw-r--r-- | models/issue.go | 65 | ||||
-rw-r--r-- | models/models.go | 21 |
5 files changed, 97 insertions, 26 deletions
diff --git a/models/action.go b/models/action.go index de7f93a9..2beb42e5 100644 --- a/models/action.go +++ b/models/action.go @@ -14,6 +14,8 @@ import ( "time" "unicode" + "github.com/go-xorm/xorm" + "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/git" "github.com/gogits/gogs/modules/log" @@ -77,6 +79,13 @@ type Action struct { Created time.Time `xorm:"created"` } +func (a *Action) AfterSet(colName string, _ xorm.Cell) { + switch colName { + case "created": + a.Created = regulateTimeZone(a.Created) + } +} + func (a Action) GetOpType() int { return int(a.OpType) } diff --git a/models/error.go b/models/error.go index c3e48063..914aec8e 100644 --- a/models/error.go +++ b/models/error.go @@ -258,7 +258,27 @@ func IsErrIssueNotExist(err error) bool { } func (err ErrIssueNotExist) Error() string { - return fmt.Sprintf("issue does not exist [id: %d, repo_id: %d, index: %4]", err.ID, err.RepoID, err.Index) + return fmt.Sprintf("issue does not exist [id: %d, repo_id: %d, index: %d]", err.ID, err.RepoID, err.Index) +} + +// _________ __ +// \_ ___ \ ____ _____ _____ ____ _____/ |_ +// / \ \/ / _ \ / \ / \_/ __ \ / \ __\ +// \ \___( <_> ) Y Y \ Y Y \ ___/| | \ | +// \______ /\____/|__|_| /__|_| /\___ >___| /__| +// \/ \/ \/ \/ \/ + +type ErrCommentNotExist struct { + ID int64 +} + +func IsErrCommentNotExist(err error) bool { + _, ok := err.(ErrCommentNotExist) + return ok +} + +func (err ErrCommentNotExist) Error() string { + return fmt.Sprintf("comment does not exist [id: %d]", err.ID) } // .____ ___. .__ diff --git a/models/git_diff.go b/models/git_diff.go index 9681b3f4..66985650 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -169,6 +169,12 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff beg := len(DIFF_HEAD) a := line[beg : (len(line)-beg)/2+beg] + // In case file name is surrounded by double quotes(it happens only in git-shell). + if a[0] == '"' { + a = a[1 : len(a)-1] + a = strings.Replace(a, `\"`, `"`, -1) + } + curFile = &DiffFile{ Name: a[strings.Index(a, "/")+1:], Index: len(diff.Files) + 1, diff --git a/models/issue.go b/models/issue.go index 1e49fda7..f1ae400a 100644 --- a/models/issue.go +++ b/models/issue.go @@ -60,11 +60,6 @@ type Issue struct { Comments []*Comment `xorm:"-"` } -// HashTag returns unique hash tag for issue. -func (i *Issue) HashTag() string { - return "issue-" + com.ToStr(i.ID) -} - func (i *Issue) AfterSet(colName string, _ xorm.Cell) { var err error switch colName { @@ -97,9 +92,16 @@ func (i *Issue) AfterSet(colName string, _ xorm.Cell) { if err != nil { log.Error(3, "GetUserByID[%d]: %v", i.ID, err) } + case "created": + i.Created = regulateTimeZone(i.Created) } } +// HashTag returns unique hash tag for issue. +func (i *Issue) HashTag() string { + return "issue-" + com.ToStr(i.ID) +} + // IsPoster returns true if given user by ID is the poster. func (i *Issue) IsPoster(uid int64) bool { return i.PosterID == uid @@ -1337,16 +1339,6 @@ type Comment struct { ShowTag CommentTag `xorm:"-"` } -// HashTag returns unique hash tag for comment. -func (c *Comment) HashTag() string { - return "issuecomment-" + com.ToStr(c.ID) -} - -// EventTag returns unique event hash tag for comment. -func (c *Comment) EventTag() string { - return "event-" + com.ToStr(c.ID) -} - func (c *Comment) AfterSet(colName string, _ xorm.Cell) { var err error switch colName { @@ -1366,9 +1358,29 @@ func (c *Comment) AfterSet(colName string, _ xorm.Cell) { log.Error(3, "GetUserByID[%d]: %v", c.ID, err) } } + case "created": + c.Created = regulateTimeZone(c.Created) } } +func (c *Comment) AfterDelete() { + _, err := DeleteAttachmentsByComment(c.ID, true) + + if err != nil { + log.Info("Could not delete files for comment %d on issue #%d: %s", c.ID, c.IssueID, err) + } +} + +// HashTag returns unique hash tag for comment. +func (c *Comment) HashTag() string { + return "issuecomment-" + com.ToStr(c.ID) +} + +// EventTag returns unique event hash tag for comment. +func (c *Comment) EventTag() string { + return "event-" + com.ToStr(c.ID) +} + func createComment(e *xorm.Session, u *User, repo *Repository, issue *Issue, commitID, line int64, cmtType CommentType, content string, uuids []string) (_ *Comment, err error) { comment := &Comment{ PosterID: u.Id, @@ -1469,11 +1481,16 @@ func CreateIssueComment(doer *User, repo *Repository, issue *Issue, content stri return CreateComment(doer, repo, issue, 0, 0, COMMENT_TYPE_COMMENT, content, attachments) } -// GetCommentById returns the comment with the given id -func GetCommentById(id int64) (*Comment, error) { +// GetCommentByID returns the comment by given ID. +func GetCommentByID(id int64) (*Comment, error) { c := new(Comment) - _, err := x.Id(id).Get(c) - return c, err + has, err := x.Id(id).Get(c) + if err != nil { + return nil, err + } else if !has { + return nil, ErrCommentNotExist{id} + } + return c, nil } // GetCommentsByIssueID returns all comments of issue by given ID. @@ -1482,12 +1499,10 @@ func GetCommentsByIssueID(issueID int64) ([]*Comment, error) { return comments, x.Where("issue_id=?", issueID).Asc("created").Find(&comments) } -func (c *Comment) AfterDelete() { - _, err := DeleteAttachmentsByComment(c.ID, true) - - if err != nil { - log.Info("Could not delete files for comment %d on issue #%d: %s", c.ID, c.IssueID, err) - } +// UpdateComment updates information of comment. +func UpdateComment(c *Comment) error { + _, err := x.Id(c.ID).AllCols().Update(c) + return err } // Attachment represent a attachment of issue/comment/release. diff --git a/models/models.go b/models/models.go index e06d5cf8..7d02bb95 100644 --- a/models/models.go +++ b/models/models.go @@ -10,7 +10,9 @@ import ( "os" "path" "strings" + "time" + "github.com/Unknwon/com" _ "github.com/go-sql-driver/mysql" "github.com/go-xorm/core" "github.com/go-xorm/xorm" @@ -40,6 +42,25 @@ func sessionRelease(sess *xorm.Session) { sess.Close() } +// Note: get back time.Time from database Go sees it at UTC where they are really Local. +// So this function makes correct timezone offset. +func regulateTimeZone(t time.Time) time.Time { + if setting.UseSQLite3 { + return t + } + + zone := t.Local().Format("-0700") + if len(zone) != 5 { + return t + } + offset := com.StrTo(zone[2:3]).MustInt() + + if zone[0] == '-' { + return t.Add(time.Duration(offset) * time.Hour) + } + return t.Add(-1 * time.Duration(offset) * time.Hour) +} + var ( x *xorm.Engine tables []interface{} |