diff options
Diffstat (limited to 'models')
-rw-r--r-- | models/action.go | 40 | ||||
-rw-r--r-- | models/git_diff.go | 118 | ||||
-rw-r--r-- | models/git_diff_test.go | 70 | ||||
-rw-r--r-- | models/issue.go | 6 | ||||
-rw-r--r-- | models/login.go | 13 | ||||
-rw-r--r-- | models/user.go | 7 |
6 files changed, 220 insertions, 34 deletions
diff --git a/models/action.go b/models/action.go index 9ecf67b4..4e5f461a 100644 --- a/models/action.go +++ b/models/action.go @@ -90,54 +90,66 @@ func (a *Action) AfterSet(colName string, _ xorm.Cell) { } } -func (a Action) GetOpType() int { +func (a *Action) GetOpType() int { return int(a.OpType) } -func (a Action) GetActUserName() string { +func (a *Action) GetActUserName() string { return a.ActUserName } -func (a Action) GetActEmail() string { +func (a *Action) ShortActUserName() string { + return base.EllipsisString(a.ActUserName, 20) +} + +func (a *Action) GetActEmail() string { return a.ActEmail } -func (a Action) GetRepoUserName() string { +func (a *Action) GetRepoUserName() string { return a.RepoUserName } -func (a Action) GetRepoName() string { +func (a *Action) ShortRepoUserName() string { + return base.EllipsisString(a.RepoUserName, 20) +} + +func (a *Action) GetRepoName() string { return a.RepoName } -func (a Action) GetRepoPath() string { - return path.Join(a.RepoUserName, a.RepoName) +func (a *Action) ShortRepoName() string { + return base.EllipsisString(a.RepoName, 33) +} + +func (a *Action) GetRepoPath() string { + return path.Join(a.ShortRepoUserName(), a.ShortRepoName()) } -func (a Action) GetRepoLink() string { +func (a *Action) GetRepoLink() string { if len(setting.AppSubUrl) > 0 { return path.Join(setting.AppSubUrl, a.GetRepoPath()) } return "/" + a.GetRepoPath() } -func (a Action) GetBranch() string { +func (a *Action) GetBranch() string { return a.RefName } -func (a Action) GetContent() string { +func (a *Action) GetContent() string { return a.Content } -func (a Action) GetCreate() time.Time { +func (a *Action) GetCreate() time.Time { return a.Created } -func (a Action) GetIssueInfos() []string { +func (a *Action) GetIssueInfos() []string { return strings.SplitN(a.Content, "|", 2) } -func (a Action) GetIssueTitle() string { +func (a *Action) GetIssueTitle() string { index := com.StrTo(a.GetIssueInfos()[0]).MustInt64() issue, err := GetIssueByIndex(a.RepoID, index) if err != nil { @@ -147,7 +159,7 @@ func (a Action) GetIssueTitle() string { return issue.Name } -func (a Action) GetIssueContent() string { +func (a *Action) GetIssueContent() string { index := com.StrTo(a.GetIssueInfos()[0]).MustInt64() issue, err := GetIssueByIndex(a.RepoID, index) if err != nil { diff --git a/models/git_diff.go b/models/git_diff.go index 22075ef7..1913ec46 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -8,6 +8,8 @@ import ( "bufio" "bytes" "fmt" + "html" + "html/template" "io" "io/ioutil" "os" @@ -15,6 +17,7 @@ import ( "strings" "github.com/Unknwon/com" + "github.com/sergi/go-diff/diffmatchpatch" "golang.org/x/net/html/charset" "golang.org/x/text/transform" @@ -25,30 +28,34 @@ import ( "github.com/gogits/gogs/modules/process" ) -// Diff line types. +type DiffLineType uint8 + const ( - DIFF_LINE_PLAIN = iota + 1 + DIFF_LINE_PLAIN DiffLineType = iota + 1 DIFF_LINE_ADD DIFF_LINE_DEL DIFF_LINE_SECTION ) +type DiffFileType uint8 + const ( - DIFF_FILE_ADD = iota + 1 + DIFF_FILE_ADD DiffFileType = iota + 1 DIFF_FILE_CHANGE DIFF_FILE_DEL DIFF_FILE_RENAME ) type DiffLine struct { - LeftIdx int - RightIdx int - Type int - Content string + LeftIdx int + RightIdx int + Type DiffLineType + Content string + ParsedContent template.HTML } -func (d DiffLine) GetType() int { - return d.Type +func (d *DiffLine) GetType() int { + return int(d.Type) } type DiffSection struct { @@ -56,12 +63,99 @@ type DiffSection struct { Lines []*DiffLine } +var ( + addedCodePrefix = []byte("<span class=\"added-code\">") + removedCodePrefix = []byte("<span class=\"removed-code\">") + codeTagSuffix = []byte("</span>") +) + +func diffToHTML(diffs []diffmatchpatch.Diff, lineType DiffLineType) template.HTML { + var buf bytes.Buffer + for i := range diffs { + if diffs[i].Type == diffmatchpatch.DiffInsert && lineType == DIFF_LINE_ADD { + buf.Write(addedCodePrefix) + buf.WriteString(html.EscapeString(diffs[i].Text)) + buf.Write(codeTagSuffix) + } else if diffs[i].Type == diffmatchpatch.DiffDelete && lineType == DIFF_LINE_DEL { + buf.Write(removedCodePrefix) + buf.WriteString(html.EscapeString(diffs[i].Text)) + buf.Write(codeTagSuffix) + } else if diffs[i].Type == diffmatchpatch.DiffEqual { + buf.WriteString(html.EscapeString(diffs[i].Text)) + } + } + + return template.HTML(buf.Bytes()) +} + +// get an specific line by type (add or del) and file line number +func (diffSection *DiffSection) GetLine(lineType DiffLineType, idx int) *DiffLine { + difference := 0 + + for _, diffLine := range diffSection.Lines { + if diffLine.Type == DIFF_LINE_PLAIN { + // get the difference of line numbers between ADD and DEL versions + difference = diffLine.RightIdx - diffLine.LeftIdx + continue + } + + if lineType == DIFF_LINE_DEL { + if diffLine.RightIdx == 0 && diffLine.LeftIdx == idx-difference { + return diffLine + } + } else if lineType == DIFF_LINE_ADD { + if diffLine.LeftIdx == 0 && diffLine.RightIdx == idx+difference { + return diffLine + } + } + } + return nil +} + +// computes diff of each diff line and set the HTML on diffLine.ParsedContent +func (diffSection *DiffSection) ComputeLinesDiff() { + for _, diffLine := range diffSection.Lines { + var compareDiffLine *DiffLine + var diff1, diff2 string + + diffLine.ParsedContent = template.HTML(html.EscapeString(diffLine.Content[1:])) + + // just compute diff for adds and removes + if diffLine.Type != DIFF_LINE_ADD && diffLine.Type != DIFF_LINE_DEL { + continue + } + + // try to find equivalent diff line. ignore, otherwise + if diffLine.Type == DIFF_LINE_ADD { + compareDiffLine = diffSection.GetLine(DIFF_LINE_DEL, diffLine.RightIdx) + if compareDiffLine == nil { + continue + } + diff1 = compareDiffLine.Content + diff2 = diffLine.Content + } else { + compareDiffLine = diffSection.GetLine(DIFF_LINE_ADD, diffLine.LeftIdx) + if compareDiffLine == nil { + continue + } + diff1 = diffLine.Content + diff2 = compareDiffLine.Content + } + + dmp := diffmatchpatch.New() + diffRecord := dmp.DiffMain(diff1[1:], diff2[1:], true) + diffRecord = dmp.DiffCleanupSemantic(diffRecord) + + diffLine.ParsedContent = diffToHTML(diffRecord, diffLine.Type) + } +} + type DiffFile struct { Name string OldName string Index int Addition, Deletion int - Type int + Type DiffFileType IsCreated bool IsDeleted bool IsBin bool @@ -69,6 +163,10 @@ type DiffFile struct { Sections []*DiffSection } +func (diffFile *DiffFile) GetType() int { + return int(diffFile.Type) +} + type Diff struct { TotalAddition, TotalDeletion int Files []*DiffFile diff --git a/models/git_diff_test.go b/models/git_diff_test.go new file mode 100644 index 00000000..4084814e --- /dev/null +++ b/models/git_diff_test.go @@ -0,0 +1,70 @@ +package models + +import ( + dmp "github.com/sergi/go-diff/diffmatchpatch" + "html/template" + "testing" +) + +func assertEqual(t *testing.T, s1 string, s2 template.HTML) { + if s1 != string(s2) { + t.Errorf("%s should be equal %s", s2, s1) + } +} + +func assertLineEqual(t *testing.T, d1 *DiffLine, d2 *DiffLine) { + if d1 != d2 { + t.Errorf("%v should be equal %v", d1, d2) + } +} + +func TestDiffToHTML(t *testing.T) { + assertEqual(t, "foo <span class=\"added-code\">bar</span> biz", diffToHTML([]dmp.Diff{ + dmp.Diff{dmp.DiffEqual, "foo "}, + dmp.Diff{dmp.DiffInsert, "bar"}, + dmp.Diff{dmp.DiffDelete, " baz"}, + dmp.Diff{dmp.DiffEqual, " biz"}, + }, DIFF_LINE_ADD)) + + assertEqual(t, "foo <span class=\"removed-code\">bar</span> biz", diffToHTML([]dmp.Diff{ + dmp.Diff{dmp.DiffEqual, "foo "}, + dmp.Diff{dmp.DiffDelete, "bar"}, + dmp.Diff{dmp.DiffInsert, " baz"}, + dmp.Diff{dmp.DiffEqual, " biz"}, + }, DIFF_LINE_DEL)) +} + +// test if GetLine is return the correct lines +func TestGetLine(t *testing.T) { + ds := DiffSection{Lines: []*DiffLine{ + &DiffLine{LeftIdx: 28, RightIdx: 28, Type: DIFF_LINE_PLAIN}, + &DiffLine{LeftIdx: 29, RightIdx: 29, Type: DIFF_LINE_PLAIN}, + &DiffLine{LeftIdx: 30, RightIdx: 30, Type: DIFF_LINE_PLAIN}, + &DiffLine{LeftIdx: 31, RightIdx: 0, Type: DIFF_LINE_DEL}, + &DiffLine{LeftIdx: 0, RightIdx: 31, Type: DIFF_LINE_ADD}, + &DiffLine{LeftIdx: 0, RightIdx: 32, Type: DIFF_LINE_ADD}, + &DiffLine{LeftIdx: 32, RightIdx: 33, Type: DIFF_LINE_PLAIN}, + &DiffLine{LeftIdx: 33, RightIdx: 0, Type: DIFF_LINE_DEL}, + &DiffLine{LeftIdx: 34, RightIdx: 0, Type: DIFF_LINE_DEL}, + &DiffLine{LeftIdx: 35, RightIdx: 0, Type: DIFF_LINE_DEL}, + &DiffLine{LeftIdx: 36, RightIdx: 0, Type: DIFF_LINE_DEL}, + &DiffLine{LeftIdx: 0, RightIdx: 34, Type: DIFF_LINE_ADD}, + &DiffLine{LeftIdx: 0, RightIdx: 35, Type: DIFF_LINE_ADD}, + &DiffLine{LeftIdx: 0, RightIdx: 36, Type: DIFF_LINE_ADD}, + &DiffLine{LeftIdx: 0, RightIdx: 37, Type: DIFF_LINE_ADD}, + &DiffLine{LeftIdx: 37, RightIdx: 38, Type: DIFF_LINE_PLAIN}, + &DiffLine{LeftIdx: 38, RightIdx: 39, Type: DIFF_LINE_PLAIN}, + }} + + assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 31), ds.Lines[4]) + assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 31), ds.Lines[3]) + + assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 33), ds.Lines[11]) + assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 34), ds.Lines[12]) + assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 35), ds.Lines[13]) + assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 36), ds.Lines[14]) + assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 34), ds.Lines[7]) + assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 35), ds.Lines[8]) + assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 36), ds.Lines[9]) + assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 37), ds.Lines[10]) +} diff --git a/models/issue.go b/models/issue.go index 22ea7251..6188da5c 100644 --- a/models/issue.go +++ b/models/issue.go @@ -1595,13 +1595,13 @@ func createComment(e *xorm.Session, u *User, repo *Repository, issue *Issue, com // Notify watchers. act := &Action{ ActUserID: u.Id, - ActUserName: u.LowerName, + ActUserName: u.Name, ActEmail: u.Email, OpType: COMMENT_ISSUE, Content: fmt.Sprintf("%d|%s", issue.Index, strings.Split(content, "\n")[0]), RepoID: repo.ID, - RepoUserName: repo.Owner.LowerName, - RepoName: repo.LowerName, + RepoUserName: repo.Owner.Name, + RepoName: repo.Name, IsPrivate: repo.IsPrivate, } if err = notifyWatchers(e, act); err != nil { diff --git a/models/login.go b/models/login.go index e36171a3..df890509 100644 --- a/models/login.go +++ b/models/login.go @@ -105,10 +105,21 @@ type LoginSource struct { Updated time.Time `xorm:"UPDATED"` } +// Cell2Int64 converts a xorm.Cell type to int64, +// and handles possible irregular cases. +func Cell2Int64(val xorm.Cell) int64 { + switch (*val).(type) { + case []uint8: + log.Trace("Cell2Int64 ([]uint8): %v", *val) + return com.StrTo(string((*val).([]uint8))).MustInt64() + } + return (*val).(int64) +} + func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) { switch colName { case "type": - switch LoginType((*val).(int64)) { + switch LoginType(Cell2Int64(val)) { case LOGIN_LDAP, LOGIN_DLDAP: source.Cfg = new(LDAPConfig) case LOGIN_SMTP: diff --git a/models/user.go b/models/user.go index f648a8ab..5c43a23a 100644 --- a/models/user.go +++ b/models/user.go @@ -429,13 +429,8 @@ func (u *User) DisplayName() string { return u.Name } -// ShortName returns shorted user name with given maximum length, -// it adds "..." at the end if user name has more length than maximum. func (u *User) ShortName(length int) string { - if len(u.Name) < length { - return u.Name - } - return u.Name[:length] + "..." + return base.EllipsisString(u.Name, length) } // IsUserExist checks if given user name exist, |