aboutsummaryrefslogtreecommitdiff
path: root/models
diff options
context:
space:
mode:
Diffstat (limited to 'models')
-rw-r--r--models/action.go23
-rw-r--r--models/admin.go11
-rw-r--r--models/git_diff.go9
-rw-r--r--models/git_diff_test.go100
-rw-r--r--models/repo.go15
-rw-r--r--models/webhook.go7
6 files changed, 105 insertions, 60 deletions
diff --git a/models/action.go b/models/action.go
index 33d5246e..8d7dd6fa 100644
--- a/models/action.go
+++ b/models/action.go
@@ -593,12 +593,29 @@ func MergePullRequestAction(actUser *User, repo *Repository, pull *Issue) error
}
// GetFeeds returns action list of given user in given context.
-func GetFeeds(uid, offset int64, isProfile bool) ([]*Action, error) {
+// userID is the user who's requesting, ctxUserID is the user/org that is requested.
+// userID can be -1, if isProfile is true or in order to skip the permission check.
+func GetFeeds(ctxUserID, userID, offset int64, isProfile bool) ([]*Action, error) {
actions := make([]*Action, 0, 20)
- sess := x.Limit(20, int(offset)).Desc("id").Where("user_id=?", uid)
+ sess := x.Limit(20, int(offset)).Desc("id").Where("user_id=?", ctxUserID)
if isProfile {
- sess.And("is_private=?", false).And("act_user_id=?", uid)
+ sess.And("is_private=?", false).And("act_user_id=?", ctxUserID)
+ } else if ctxUserID != -1 {
+ ctxUser := &User{Id: ctxUserID}
+ if err := ctxUser.GetUserRepositories(userID); err != nil {
+ return nil, err
+ }
+
+ var repoIDs []int64
+ for _, repo := range ctxUser.Repos {
+ repoIDs = append(repoIDs, repo.ID)
+ }
+
+ if len(repoIDs) > 0 {
+ sess.In("repo_id", repoIDs)
+ }
}
+
err := sess.Find(&actions)
return actions, err
}
diff --git a/models/admin.go b/models/admin.go
index 811edde2..7756cd6a 100644
--- a/models/admin.go
+++ b/models/admin.go
@@ -7,6 +7,7 @@ package models
import (
"fmt"
"os"
+ "os/exec"
"strings"
"time"
@@ -14,6 +15,7 @@ import (
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log"
+ "github.com/gogits/gogs/modules/setting"
)
type NoticeType int
@@ -53,7 +55,14 @@ func CreateRepositoryNotice(desc string) error {
// RemoveAllWithNotice removes all directories in given path and
// creates a system notice when error occurs.
func RemoveAllWithNotice(title, path string) {
- if err := os.RemoveAll(path); err != nil {
+ var err error
+ if setting.IsWindows {
+ err = exec.Command("cmd", "/C", "rmdir", "/S", "/Q", path).Run()
+ } else {
+ err = os.RemoveAll(path)
+ }
+
+ if err != nil {
desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
log.Warn(desc)
if err = CreateRepositoryNotice(desc); err != nil {
diff --git a/models/git_diff.go b/models/git_diff.go
index e8bfe610..9796ef59 100644
--- a/models/git_diff.go
+++ b/models/git_diff.go
@@ -26,6 +26,7 @@ import (
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/process"
+ "github.com/gogits/gogs/modules/template/highlight"
)
type DiffLineType uint8
@@ -160,12 +161,20 @@ type DiffFile struct {
IsBin bool
IsRenamed bool
Sections []*DiffSection
+ HighlightClass string
}
func (diffFile *DiffFile) GetType() int {
return int(diffFile.Type)
}
+func (diffFile *DiffFile) GetHighlightClass() string {
+ if diffFile.HighlightClass == "" {
+ diffFile.HighlightClass = highlight.FileNameToHighlightClass(diffFile.Name)
+ }
+ return diffFile.HighlightClass
+}
+
type Diff struct {
TotalAddition, TotalDeletion int
Files []*DiffFile
diff --git a/models/git_diff_test.go b/models/git_diff_test.go
index 4084814e..3a1312ca 100644
--- a/models/git_diff_test.go
+++ b/models/git_diff_test.go
@@ -1,70 +1,70 @@
package models
import (
- dmp "github.com/sergi/go-diff/diffmatchpatch"
- "html/template"
- "testing"
+ 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)
- }
+ 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)
- }
+ 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=\"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))
+ 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},
- }}
+ 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, 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])
+ 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/repo.go b/models/repo.go
index f838de7e..4a8c8f05 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -414,7 +414,8 @@ func (repo *Repository) ComposePayload() *api.PayloadRepo {
Email: repo.MustOwner().Email,
UserName: repo.MustOwner().Name,
},
- Private: repo.IsPrivate,
+ Private: repo.IsPrivate,
+ DefaultBranch: repo.DefaultBranch,
}
}
@@ -957,10 +958,13 @@ func countRepositories(showPrivate bool) int64 {
sess := x.NewSession()
if !showPrivate {
- sess.Where("is_private=", false)
+ sess.Where("is_private=?", false)
}
- count, _ := sess.Count(new(Repository))
+ count, err := sess.Count(new(Repository))
+ if err != nil {
+ log.Error(4, "countRepositories: %v", err)
+ }
return count
}
@@ -1093,13 +1097,16 @@ func TransferOwnership(u *User, newOwnerName string, repo *Repository) error {
return fmt.Errorf("transferRepoAction: %v", err)
}
- // Change repository directory name.
+ // Rename remote repository to new path and delete local copy.
if err = os.Rename(RepoPath(owner.Name, repo.Name), RepoPath(newOwner.Name, repo.Name)); err != nil {
return fmt.Errorf("rename repository directory: %v", err)
}
+ RemoveAllWithNotice("Delete repository local copy", repo.LocalCopyPath())
+ // Rename remote wiki repository to new path and delete local copy.
wikiPath := WikiPath(owner.Name, repo.Name)
if com.IsExist(wikiPath) {
+ RemoveAllWithNotice("Delete repository wiki local copy", repo.LocalWikiPath())
if err = os.Rename(wikiPath, WikiPath(newOwner.Name, repo.Name)); err != nil {
return fmt.Errorf("rename repository wiki: %v", err)
}
diff --git a/models/webhook.go b/models/webhook.go
index 27ac75fe..bdfb62d8 100644
--- a/models/webhook.go
+++ b/models/webhook.go
@@ -398,6 +398,7 @@ func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) err
return nil
}
+ var payloader api.Payloader
for _, w := range ws {
switch event {
case HOOK_EVENT_CREATE:
@@ -410,14 +411,16 @@ func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) err
}
}
+ // Use separate objects so modifcations won't be made on payload on non-Gogs type hooks.
switch w.HookTaskType {
case SLACK:
- p, err = GetSlackPayload(p, event, w.Meta)
+ payloader, err = GetSlackPayload(p, event, w.Meta)
if err != nil {
return fmt.Errorf("GetSlackPayload: %v", err)
}
default:
p.SetSecret(w.Secret)
+ payloader = p
}
if err = CreateHookTask(&HookTask{
@@ -425,7 +428,7 @@ func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) err
HookID: w.ID,
Type: w.HookTaskType,
URL: w.URL,
- Payloader: p,
+ Payloader: payloader,
ContentType: w.ContentType,
EventType: HOOK_EVENT_PUSH,
IsSSL: w.IsSSL,