summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
Diffstat (limited to 'models')
-rw-r--r--models/access.go2
-rw-r--r--models/action.go15
-rw-r--r--models/git.go26
-rw-r--r--models/issue.go71
-rw-r--r--models/models.go57
-rw-r--r--models/repo.go48
-rw-r--r--models/user.go3
7 files changed, 148 insertions, 74 deletions
diff --git a/models/access.go b/models/access.go
index 84cad17a3f..42fccae055 100644
--- a/models/access.go
+++ b/models/access.go
@@ -26,6 +26,8 @@ type Access struct {
// AddAccess adds new access record.
func AddAccess(access *Access) error {
+ access.UserName = strings.ToLower(access.UserName)
+ access.RepoName = strings.ToLower(access.RepoName)
_, err := orm.Insert(access)
return err
}
diff --git a/models/action.go b/models/action.go
index 9d99df8546..1e55df85e9 100644
--- a/models/action.go
+++ b/models/action.go
@@ -31,6 +31,7 @@ type Action struct {
OpType int // Operations: CREATE DELETE STAR ...
ActUserId int64 // Action user id.
ActUserName string // Action user name.
+ ActEmail string
RepoId int64
RepoName string
RefName string
@@ -46,6 +47,10 @@ func (a Action) GetActUserName() string {
return a.ActUserName
}
+func (a Action) GetActEmail() string {
+ return a.ActEmail
+}
+
func (a Action) GetRepoName() string {
return a.RepoName
}
@@ -59,7 +64,7 @@ func (a Action) GetContent() string {
}
// CommitRepoAction adds new action for committing repository.
-func CommitRepoAction(userId int64, userName string,
+func CommitRepoAction(userId int64, userName, actEmail string,
repoId int64, repoName string, refName string, commit *base.PushCommits) error {
log.Trace("action.CommitRepoAction(start): %d/%s", userId, repoName)
@@ -69,8 +74,8 @@ func CommitRepoAction(userId int64, userName string,
return err
}
- if err = NotifyWatchers(&Action{ActUserId: userId, ActUserName: userName, OpType: OP_COMMIT_REPO,
- Content: string(bs), RepoId: repoId, RepoName: repoName, RefName: refName}); err != nil {
+ if err = NotifyWatchers(&Action{ActUserId: userId, ActUserName: userName, ActEmail: actEmail,
+ OpType: OP_COMMIT_REPO, Content: string(bs), RepoId: repoId, RepoName: repoName, RefName: refName}); err != nil {
log.Error("action.CommitRepoAction(notify watchers): %d/%s", userId, repoName)
return err
}
@@ -93,8 +98,8 @@ func CommitRepoAction(userId int64, userName string,
// NewRepoAction adds new action for creating repository.
func NewRepoAction(user *User, repo *Repository) (err error) {
- if err = NotifyWatchers(&Action{ActUserId: user.Id, ActUserName: user.Name, OpType: OP_CREATE_REPO,
- RepoId: repo.Id, RepoName: repo.Name}); err != nil {
+ if err = NotifyWatchers(&Action{ActUserId: user.Id, ActUserName: user.Name, ActEmail: user.Email,
+ OpType: OP_CREATE_REPO, RepoId: repo.Id, RepoName: repo.Name}); err != nil {
log.Error("action.NewRepoAction(notify watchers): %d/%s", user.Id, repo.Name)
return err
}
diff --git a/models/git.go b/models/git.go
index 34f0267f65..d3bad6e0ce 100644
--- a/models/git.go
+++ b/models/git.go
@@ -70,9 +70,12 @@ func GetTargetFile(userName, repoName, branchName, commitId, rpath string) (*Rep
return nil, err
}
- commit, err := repo.GetCommit(branchName, commitId)
+ commit, err := repo.GetCommitOfBranch(branchName)
if err != nil {
- return nil, err
+ commit, err = repo.GetCommit(commitId)
+ if err != nil {
+ return nil, err
+ }
}
parts := strings.Split(path.Clean(rpath), "/")
@@ -110,13 +113,22 @@ func GetTargetFile(userName, repoName, branchName, commitId, rpath string) (*Rep
}
// GetReposFiles returns a list of file object in given directory of repository.
-func GetReposFiles(userName, repoName, branchName, commitId, rpath string) ([]*RepoFile, error) {
+// func GetReposFilesOfBranch(userName, repoName, branchName, rpath string) ([]*RepoFile, error) {
+// return getReposFiles(userName, repoName, commitId, rpath)
+// }
+
+// GetReposFiles returns a list of file object in given directory of repository.
+func GetReposFiles(userName, repoName, commitId, rpath string) ([]*RepoFile, error) {
+ return getReposFiles(userName, repoName, commitId, rpath)
+}
+
+func getReposFiles(userName, repoName, commitId string, rpath string) ([]*RepoFile, error) {
repo, err := git.OpenRepository(RepoPath(userName, repoName))
if err != nil {
return nil, err
}
- commit, err := repo.GetCommit(branchName, commitId)
+ commit, err := repo.GetCommit(commitId)
if err != nil {
return nil, err
}
@@ -216,13 +228,13 @@ func GetReposFiles(userName, repoName, branchName, commitId, rpath string) ([]*R
return append(repodirs, repofiles...), nil
}
-func GetCommit(userName, repoName, branchname, commitid string) (*git.Commit, error) {
+func GetCommit(userName, repoName, commitId string) (*git.Commit, error) {
repo, err := git.OpenRepository(RepoPath(userName, repoName))
if err != nil {
return nil, err
}
- return repo.GetCommit(branchname, commitid)
+ return repo.GetCommit(commitId)
}
// GetCommitsByBranch returns all commits of given branch of repository.
@@ -397,7 +409,7 @@ func GetDiff(repoPath, commitid string) (*Diff, error) {
return nil, err
}
- commit, err := repo.GetCommit("", commitid)
+ commit, err := repo.GetCommit(commitid)
if err != nil {
return nil, err
}
diff --git a/models/issue.go b/models/issue.go
index 39558ae225..f14030df5e 100644
--- a/models/issue.go
+++ b/models/issue.go
@@ -18,23 +18,24 @@ var (
// Issue represents an issue or pull request of repository.
type Issue struct {
- Id int64
- Index int64 // Index in one repository.
- Name string
- RepoId int64 `xorm:"index"`
- Repo *Repository `xorm:"-"`
- PosterId int64
- Poster *User `xorm:"-"`
- MilestoneId int64
- AssigneeId int64
- IsPull bool // Indicates whether is a pull request or not.
- IsClosed bool
- Labels string `xorm:"TEXT"`
- Mentions string `xorm:"TEXT"`
- Content string `xorm:"TEXT"`
- NumComments int
- Created time.Time `xorm:"created"`
- Updated time.Time `xorm:"updated"`
+ Id int64
+ Index int64 // Index in one repository.
+ Name string
+ RepoId int64 `xorm:"index"`
+ Repo *Repository `xorm:"-"`
+ PosterId int64
+ Poster *User `xorm:"-"`
+ MilestoneId int64
+ AssigneeId int64
+ IsPull bool // Indicates whether is a pull request or not.
+ IsClosed bool
+ Labels string `xorm:"TEXT"`
+ Mentions string `xorm:"TEXT"`
+ Content string `xorm:"TEXT"`
+ RenderedContent string `xorm:"-"`
+ NumComments int
+ Created time.Time `xorm:"created"`
+ Updated time.Time `xorm:"updated"`
}
// CreateIssue creates new issue for repository.
@@ -169,9 +170,17 @@ type Milestone struct {
Created time.Time `xorm:"created"`
}
+// Issue types.
+const (
+ IT_PLAIN = iota // Pure comment.
+ IT_REOPEN // Issue reopen status change prompt.
+ IT_CLOSE // Issue close status change prompt.
+)
+
// Comment represents a comment in commit and issue page.
type Comment struct {
Id int64
+ Type int
PosterId int64
Poster *User `xorm:"-"`
IssueId int64
@@ -182,21 +191,37 @@ type Comment struct {
}
// CreateComment creates comment of issue or commit.
-func CreateComment(userId, issueId, commitId, line int64, content string) error {
+func CreateComment(userId, repoId, issueId, commitId, line int64, cmtType int, content string) error {
sess := orm.NewSession()
defer sess.Close()
sess.Begin()
- if _, err := orm.Insert(&Comment{PosterId: userId, IssueId: issueId,
+ if _, err := orm.Insert(&Comment{PosterId: userId, Type: cmtType, IssueId: issueId,
CommitId: commitId, Line: line, Content: content}); err != nil {
sess.Rollback()
return err
}
- rawSql := "UPDATE `issue` SET num_comments = num_comments + 1 WHERE id = ?"
- if _, err := sess.Exec(rawSql, issueId); err != nil {
- sess.Rollback()
- return err
+ // Check comment type.
+ switch cmtType {
+ case IT_PLAIN:
+ rawSql := "UPDATE `issue` SET num_comments = num_comments + 1 WHERE id = ?"
+ if _, err := sess.Exec(rawSql, issueId); err != nil {
+ sess.Rollback()
+ return err
+ }
+ case IT_REOPEN:
+ rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues - 1 WHERE id = ?"
+ if _, err := sess.Exec(rawSql, repoId); err != nil {
+ sess.Rollback()
+ return err
+ }
+ case IT_CLOSE:
+ rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues + 1 WHERE id = ?"
+ if _, err := sess.Exec(rawSql, repoId); err != nil {
+ sess.Rollback()
+ return err
+ }
}
return sess.Commit()
}
diff --git a/models/models.go b/models/models.go
index 04a361c595..a626b98f6e 100644
--- a/models/models.go
+++ b/models/models.go
@@ -12,20 +12,27 @@ import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
"github.com/lunny/xorm"
+ // _ "github.com/mattn/go-sqlite3"
"github.com/gogits/gogs/modules/base"
)
var (
- orm *xorm.Engine
+ orm *xorm.Engine
+ HasEngine bool
DbCfg struct {
Type, Host, Name, User, Pwd, Path, SslMode string
}
+
+ UseSQLite3 bool
)
func LoadModelsConfig() {
DbCfg.Type = base.Cfg.MustValue("database", "DB_TYPE")
+ if DbCfg.Type == "sqlite3" {
+ UseSQLite3 = true
+ }
DbCfg.Host = base.Cfg.MustValue("database", "HOST")
DbCfg.Name = base.Cfg.MustValue("database", "NAME")
DbCfg.User = base.Cfg.MustValue("database", "USER")
@@ -34,11 +41,32 @@ func LoadModelsConfig() {
DbCfg.Path = base.Cfg.MustValue("database", "PATH", "data/gogs.db")
}
-func SetEngine() {
- var err error
+func NewTestEngine(x *xorm.Engine) (err error) {
+ switch DbCfg.Type {
+ case "mysql":
+ x, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8",
+ DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name))
+ case "postgres":
+ x, err = xorm.NewEngine("postgres", fmt.Sprintf("user=%s password=%s dbname=%s sslmode=%s",
+ DbCfg.User, DbCfg.Pwd, DbCfg.Name, DbCfg.SslMode))
+ // case "sqlite3":
+ // os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm)
+ // x, err = xorm.NewEngine("sqlite3", DbCfg.Path)
+ default:
+ return fmt.Errorf("Unknown database type: %s\n", DbCfg.Type)
+ }
+ if err != nil {
+ return fmt.Errorf("models.init(fail to conntect database): %v\n", err)
+ }
+
+ return x.Sync(new(User), new(PublicKey), new(Repository), new(Watch),
+ new(Action), new(Access), new(Issue), new(Comment))
+}
+
+func SetEngine() (err error) {
switch DbCfg.Type {
case "mysql":
- orm, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@%s/%s?charset=utf8",
+ orm, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8",
DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name))
case "postgres":
orm, err = xorm.NewEngine("postgres", fmt.Sprintf("user=%s password=%s dbname=%s sslmode=%s",
@@ -47,12 +75,10 @@ func SetEngine() {
os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm)
orm, err = xorm.NewEngine("sqlite3", DbCfg.Path)
default:
- fmt.Printf("Unknown database type: %s\n", DbCfg.Type)
- os.Exit(2)
+ return fmt.Errorf("Unknown database type: %s\n", DbCfg.Type)
}
if err != nil {
- fmt.Printf("models.init(fail to conntect database): %v\n", err)
- os.Exit(2)
+ return fmt.Errorf("models.init(fail to conntect database): %v\n", err)
}
// WARNNING: for serv command, MUST remove the output to os.stdout,
@@ -62,20 +88,21 @@ func SetEngine() {
//orm.ShowErr = true
f, err := os.Create("xorm.log")
if err != nil {
- fmt.Printf("models.init(fail to create xorm.log): %v\n", err)
- os.Exit(2)
+ return fmt.Errorf("models.init(fail to create xorm.log): %v\n", err)
}
orm.Logger = f
orm.ShowSQL = true
+ return nil
}
-func NewEngine() {
- SetEngine()
- if err := orm.Sync(new(User), new(PublicKey), new(Repository), new(Watch),
+func NewEngine() (err error) {
+ if err = SetEngine(); err != nil {
+ return err
+ } else if err = orm.Sync(new(User), new(PublicKey), new(Repository), new(Watch),
new(Action), new(Access), new(Issue), new(Comment)); err != nil {
- fmt.Printf("sync database struct error: %v\n", err)
- os.Exit(2)
+ return fmt.Errorf("sync database struct error: %v\n", err)
}
+ return nil
}
type Statistic struct {
diff --git a/models/repo.go b/models/repo.go
index 4be655d287..0c808f1845 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -10,8 +10,8 @@ import (
"io/ioutil"
"os"
"os/exec"
+ "path"
"path/filepath"
- "regexp"
"strings"
"time"
"unicode/utf8"
@@ -59,15 +59,6 @@ func NewRepoContext() {
os.Exit(2)
}
}
-
- // Initialize illegal patterns.
- for i := range illegalPatterns[1:] {
- pattern := ""
- for j := range illegalPatterns[i+1] {
- pattern += "[" + string(illegalPatterns[i+1][j]-32) + string(illegalPatterns[i+1][j]) + "]"
- }
- illegalPatterns[i+1] = pattern
- }
}
// Repository represents a git repository.
@@ -105,15 +96,20 @@ func IsRepositoryExist(user *User, repoName string) (bool, error) {
}
var (
- // Define as all lower case!!
- illegalPatterns = []string{"[.][Gg][Ii][Tt]", "raw", "user", "help", "stars", "issues", "pulls", "commits", "repo", "template", "admin"}
+ illegalEquals = []string{"raw", "install", "api", "avatar", "user", "help", "stars", "issues", "pulls", "commits", "repo", "template", "admin"}
+ illegalSuffixs = []string{".git"}
)
// IsLegalName returns false if name contains illegal characters.
func IsLegalName(repoName string) bool {
- for _, pattern := range illegalPatterns {
- has, _ := regexp.MatchString(pattern, repoName)
- if has {
+ repoName = strings.ToLower(repoName)
+ for _, char := range illegalEquals {
+ if repoName == char {
+ return false
+ }
+ }
+ for _, char := range illegalSuffixs {
+ if strings.HasSuffix(repoName, char) {
return false
}
}
@@ -161,8 +157,8 @@ func CreateRepository(user *User, repoName, desc, repoLang, license string, priv
}
access := Access{
- UserName: user.Name,
- RepoName: repo.Name,
+ UserName: user.LowerName,
+ RepoName: strings.ToLower(path.Join(user.Name, repo.Name)),
Mode: AU_WRITABLE,
}
if _, err = session.Insert(&access); err != nil {
@@ -198,12 +194,19 @@ func CreateRepository(user *User, repoName, desc, repoLang, license string, priv
c := exec.Command("git", "update-server-info")
c.Dir = repoPath
- err = c.Run()
- if err != nil {
+ if err = c.Run(); err != nil {
log.Error("repo.CreateRepository(exec update-server-info): %v", err)
}
- return repo, NewRepoAction(user, repo)
+ if err = NewRepoAction(user, repo); err != nil {
+ log.Error("repo.CreateRepository(NewRepoAction): %v", err)
+ }
+
+ if err = WatchRepo(user.Id, repo.Id, true); err != nil {
+ log.Error("repo.CreateRepository(WatchRepo): %v", err)
+ }
+
+ return repo, nil
}
// extractGitBareZip extracts git-bare.zip to repository path.
@@ -362,7 +365,7 @@ func GetRepos(num, offset int) ([]UserRepo, error) {
}
func RepoPath(userName, repoName string) string {
- return filepath.Join(UserPath(userName), repoName+".git")
+ return filepath.Join(UserPath(userName), strings.ToLower(repoName)+".git")
}
func UpdateRepository(repo *Repository) error {
@@ -395,7 +398,7 @@ func DeleteRepository(userId, repoId int64, userName string) (err error) {
session.Rollback()
return err
}
- if _, err := session.Delete(&Access{UserName: userName, RepoName: repo.Name}); err != nil {
+ if _, err := session.Delete(&Access{RepoName: strings.ToLower(path.Join(userName, repo.Name))}); err != nil {
session.Rollback()
return err
}
@@ -510,7 +513,6 @@ func NotifyWatchers(act *Action) error {
continue
}
- act.Id = 0
act.UserId = watches[i].UserId
if _, err = orm.InsertOne(act); err != nil {
return errors.New("repo.NotifyWatchers(create action): " + err.Error())
diff --git a/models/user.go b/models/user.go
index 6ca16ec32e..4908552f29 100644
--- a/models/user.go
+++ b/models/user.go
@@ -39,6 +39,7 @@ var (
ErrUserNotExist = errors.New("User does not exist")
ErrEmailAlreadyUsed = errors.New("E-mail already used")
ErrUserNameIllegal = errors.New("User name contains illegal characters")
+ ErrKeyNotExist = errors.New("Public key does not exist")
)
// User represents the object of individual and member of organization.
@@ -67,7 +68,7 @@ type User struct {
// HomeLink returns the user home page link.
func (user *User) HomeLink() string {
- return "/user/" + user.LowerName
+ return "/user/" + user.Name
}
// AvatarLink returns the user gravatar link.