diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2014-03-28 10:51:42 +0800 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2014-03-28 10:51:42 +0800 |
commit | 89258e868b070ff3046de912e1d9a3009f923b62 (patch) | |
tree | 96d582c68daf862d0328e0734ede3da88e308fb8 /models | |
parent | 922a189f4061796b0d4afeeb45e508c36cc5e7fc (diff) | |
parent | 5344a0300383c4921e4a5810dff58c7686412f0c (diff) | |
download | gitea-89258e868b070ff3046de912e1d9a3009f923b62.tar.gz gitea-89258e868b070ff3046de912e1d9a3009f923b62.zip |
Merge branch 'master' of github.com:gogits/gogs
Diffstat (limited to 'models')
-rw-r--r-- | models/access.go | 4 | ||||
-rw-r--r-- | models/action.go | 31 | ||||
-rw-r--r-- | models/git.go | 41 | ||||
-rw-r--r-- | models/issue.go | 57 | ||||
-rw-r--r-- | models/models_test.go | 23 | ||||
-rw-r--r-- | models/repo.go | 99 |
6 files changed, 146 insertions, 109 deletions
diff --git a/models/access.go b/models/access.go index 36d9405f52..84cad17a3f 100644 --- a/models/access.go +++ b/models/access.go @@ -15,7 +15,7 @@ const ( AU_WRITABLE ) -// Access represents the accessibility of user and repository. +// Access represents the accessibility of user to repository. type Access struct { Id int64 UserName string `xorm:"unique(s)"` @@ -30,7 +30,7 @@ func AddAccess(access *Access) error { return err } -// HasAccess returns true if someone can read or write given repository. +// HasAccess returns true if someone can read or write to given repository. func HasAccess(userName, repoName string, mode int) (bool, error) { return orm.Get(&Access{ Id: 0, diff --git a/models/action.go b/models/action.go index edf1bf58f9..9d99df8546 100644 --- a/models/action.go +++ b/models/action.go @@ -23,7 +23,8 @@ const ( OP_PULL_REQUEST ) -// Action represents user operation type and information to the repository. +// Action represents user operation type and other information to repository., +// it implemented interface base.Actioner so that can be used in template render. type Action struct { Id int64 UserId int64 // Receiver user id. @@ -57,23 +58,24 @@ func (a Action) GetContent() string { return a.Content } -// CommitRepoAction records action for commit repository. +// CommitRepoAction adds new action for committing repository. func CommitRepoAction(userId int64, userName string, - repoId int64, repoName string, refName string, commits *base.PushCommits) error { + repoId int64, repoName string, refName string, commit *base.PushCommits) error { log.Trace("action.CommitRepoAction(start): %d/%s", userId, repoName) - bs, err := json.Marshal(commits) + bs, err := json.Marshal(commit) if err != nil { log.Error("action.CommitRepoAction(json): %d/%s", userId, repoName) return err } - if err = NotifyWatchers(userId, repoId, OP_COMMIT_REPO, userName, repoName, refName, string(bs)); err != nil { + if err = NotifyWatchers(&Action{ActUserId: userId, ActUserName: userName, 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 } - // Update repository last update time. + // Change repository bare status and update last updated time. repo, err := GetRepositoryByName(userId, repoName) if err != nil { log.Error("action.CommitRepoAction(GetRepositoryByName): %d/%s", userId, repoName) @@ -89,16 +91,13 @@ func CommitRepoAction(userId int64, userName string, return nil } -// NewRepoAction records action for create repository. -func NewRepoAction(user *User, repo *Repository) error { - _, err := orm.InsertOne(&Action{ - UserId: user.Id, - ActUserId: user.Id, - ActUserName: user.Name, - OpType: OP_CREATE_REPO, - RepoId: repo.Id, - RepoName: repo.Name, - }) +// 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 { + log.Error("action.NewRepoAction(notify watchers): %d/%s", user.Id, repo.Name) + return err + } log.Trace("action.NewRepoAction: %s/%s", user.LowerName, repo.LowerName) return err diff --git a/models/git.go b/models/git.go index 8e1bc4e32c..e2ee52083d 100644 --- a/models/git.go +++ b/models/git.go @@ -38,8 +38,8 @@ func (file *RepoFile) LookupBlob() (*git.Blob, error) { } // GetBranches returns all branches of given repository. -func GetBranches(userName, reposName string) ([]string, error) { - repo, err := git.OpenRepository(RepoPath(userName, reposName)) +func GetBranches(userName, repoName string) ([]string, error) { + repo, err := git.OpenRepository(RepoPath(userName, repoName)) if err != nil { return nil, err } @@ -56,8 +56,16 @@ func GetBranches(userName, reposName string) ([]string, error) { return brs, nil } -func GetTargetFile(userName, reposName, branchName, commitId, rpath string) (*RepoFile, error) { - repo, err := git.OpenRepository(RepoPath(userName, reposName)) +func IsBranchExist(userName, repoName, branchName string) bool { + repo, err := git.OpenRepository(RepoPath(userName, repoName)) + if err != nil { + return false + } + return repo.IsBranchExist(branchName) +} + +func GetTargetFile(userName, repoName, branchName, commitId, rpath string) (*RepoFile, error) { + repo, err := git.OpenRepository(RepoPath(userName, repoName)) if err != nil { return nil, err } @@ -102,8 +110,8 @@ func GetTargetFile(userName, reposName, branchName, commitId, rpath string) (*Re } // GetReposFiles returns a list of file object in given directory of repository. -func GetReposFiles(userName, reposName, branchName, commitId, rpath string) ([]*RepoFile, error) { - repo, err := git.OpenRepository(RepoPath(userName, reposName)) +func GetReposFiles(userName, repoName, branchName, commitId, rpath string) ([]*RepoFile, error) { + repo, err := git.OpenRepository(RepoPath(userName, repoName)) if err != nil { return nil, err } @@ -217,13 +225,26 @@ func GetCommit(userName, repoName, branchname, commitid string) (*git.Commit, er return repo.GetCommit(branchname, commitid) } -// GetCommits returns all commits of given branch of repository. -func GetCommits(userName, reposName, branchname string) (*list.List, error) { - repo, err := git.OpenRepository(RepoPath(userName, reposName)) +// GetCommitsByBranch returns all commits of given branch of repository. +func GetCommitsByBranch(userName, repoName, branchName string) (*list.List, error) { + repo, err := git.OpenRepository(RepoPath(userName, repoName)) + if err != nil { + return nil, err + } + r, err := repo.LookupReference(fmt.Sprintf("refs/heads/%s", branchName)) + if err != nil { + return nil, err + } + return r.AllCommits() +} + +// GetCommitsByCommitId returns all commits of given commitId of repository. +func GetCommitsByCommitId(userName, repoName, commitId string) (*list.List, error) { + repo, err := git.OpenRepository(RepoPath(userName, repoName)) if err != nil { return nil, err } - r, err := repo.LookupReference(fmt.Sprintf("refs/heads/%s", branchname)) + r, err := repo.LookupReference(commitId) if err != nil { return nil, err } diff --git a/models/issue.go b/models/issue.go index 97e51a0c58..39558ae225 100644 --- a/models/issue.go +++ b/models/issue.go @@ -21,7 +21,8 @@ type Issue struct { Id int64 Index int64 // Index in one repository. Name string - RepoId int64 `xorm:"index"` + RepoId int64 `xorm:"index"` + Repo *Repository `xorm:"-"` PosterId int64 Poster *User `xorm:"-"` MilestoneId int64 @@ -37,17 +38,16 @@ type Issue struct { } // CreateIssue creates new issue for repository. -func CreateIssue(userId, repoId, milestoneId, assigneeId int64, name, labels, content string, isPull bool) (*Issue, error) { - count, err := GetIssueCount(repoId) - if err != nil { - return nil, err - } - +func CreateIssue(userId, repoId, milestoneId, assigneeId int64, issueCount int, name, labels, content string, isPull bool) (issue *Issue, err error) { // TODO: find out mentions mentions := "" - issue := &Issue{ - Index: count + 1, + sess := orm.NewSession() + defer sess.Close() + sess.Begin() + + issue = &Issue{ + Index: int64(issueCount) + 1, Name: name, RepoId: repoId, PosterId: userId, @@ -58,13 +58,23 @@ func CreateIssue(userId, repoId, milestoneId, assigneeId int64, name, labels, co Mentions: mentions, Content: content, } - _, err = orm.Insert(issue) - return issue, err -} + if _, err = sess.Insert(issue); err != nil { + sess.Rollback() + return nil, err + } -// GetIssueCount returns count of issues in the repository. -func GetIssueCount(repoId int64) (int64, error) { - return orm.Count(&Issue{RepoId: repoId}) + rawSql := "UPDATE `repository` SET num_issues = num_issues + 1 WHERE id = ?" + if _, err = sess.Exec(rawSql, repoId); err != nil { + sess.Rollback() + return nil, err + } + + if err = sess.Commit(); err != nil { + sess.Rollback() + return nil, err + } + + return issue, nil } // GetIssueById returns issue object by given id. @@ -127,18 +137,18 @@ func GetIssues(userId, repoId, posterId, milestoneId int64, page int, isClosed, return issues, err } +// GetUserIssueCount returns the number of issues that were created by given user in repository. +func GetUserIssueCount(userId, repoId int64) int64 { + count, _ := orm.Where("poster_id=?", userId).And("repo_id=?", repoId).Count(new(Issue)) + return count +} + // UpdateIssue updates information of issue. func UpdateIssue(issue *Issue) error { - _, err := orm.Update(issue, &Issue{RepoId: issue.RepoId, Index: issue.Index}) + _, err := orm.Id(issue.Id).AllCols().Update(issue) return err } -func CloseIssue() { -} - -func ReopenIssue() { -} - // Label represents a list of labels of repository for issues. type Label struct { Id int64 @@ -178,8 +188,7 @@ func CreateComment(userId, issueId, commitId, line int64, content string) error sess.Begin() if _, err := orm.Insert(&Comment{PosterId: userId, IssueId: issueId, - CommitId: commitId, Line: line, Content: content, - }); err != nil { + CommitId: commitId, Line: line, Content: content}); err != nil { sess.Rollback() return err } diff --git a/models/models_test.go b/models/models_test.go index d0f734d678..b808f41d21 100644 --- a/models/models_test.go +++ b/models/models_test.go @@ -10,12 +10,12 @@ import ( "github.com/lunny/xorm" _ "github.com/mattn/go-sqlite3" + . "github.com/smartystreets/goconvey/convey" + + "github.com/gogits/gogs/modules/base" ) func init() { - LoadModelsConfig() - NewEngine() - var err error orm, err = xorm.NewEngine("sqlite3", "./test.db") if err != nil { @@ -25,26 +25,31 @@ func init() { orm.ShowSQL = true orm.ShowDebug = true - err = orm.Sync(&User{}, &Repo{}) + err = orm.Sync(&User{}, &Repository{}) if err != nil { fmt.Println(err) } - root = "test" + base.RepoRootPath = "test" } func TestCreateRepository(t *testing.T) { - user := User{Id: 1, Type: Individual} - _, err := CreateRepository(&user, "test") + user := User{Id: 1, Name: "foobar", Type: UT_INDIVIDUAL} + _, err := CreateRepository(&user, "test", "", "", "test repo desc", false, false) if err != nil { t.Error(err) } } func TestDeleteRepository(t *testing.T) { - user := User{Id: 1, Type: Individual} - err := DeleteRepository(&user, "test") + err := DeleteRepository(1, 1, "foobar") if err != nil { t.Error(err) } } + +func TestCommitRepoAction(t *testing.T) { + Convey("Create a commit repository action", t, func() { + + }) +} diff --git a/models/repo.go b/models/repo.go index 0ef049cc6e..726d435d33 100644 --- a/models/repo.go +++ b/models/repo.go @@ -72,20 +72,23 @@ func NewRepoContext() { // Repository represents a git repository. type Repository struct { - Id int64 - OwnerId int64 `xorm:"unique(s)"` - ForkId int64 - LowerName string `xorm:"unique(s) index not null"` - Name string `xorm:"index not null"` - Description string - Website string - NumWatches int - NumStars int - NumForks int - IsPrivate bool - IsBare bool - Created time.Time `xorm:"created"` - Updated time.Time `xorm:"updated"` + Id int64 + OwnerId int64 `xorm:"unique(s)"` + ForkId int64 + LowerName string `xorm:"unique(s) index not null"` + Name string `xorm:"index not null"` + Description string + Website string + NumWatches int + NumStars int + NumForks int + NumIssues int + NumClosedIssues int + NumOpenIssues int `xorm:"-"` + IsPrivate bool + IsBare bool + Created time.Time `xorm:"created"` + Updated time.Time `xorm:"updated"` } // IsRepositoryExist returns true if the repository with given name under user has already existed. @@ -94,17 +97,16 @@ func IsRepositoryExist(user *User, repoName string) (bool, error) { has, err := orm.Where("lower_name = ?", strings.ToLower(repoName)).Get(&repo) if err != nil { return has, err + } else if !has { + return false, nil } - s, err := os.Stat(RepoPath(user.Name, repoName)) - if err != nil { - return false, nil // Error simply means does not exist, but we don't want to show up. - } - return s.IsDir(), nil + + return com.IsDir(RepoPath(user.Name, repoName)), nil } var ( // Define as all lower case!! - illegalPatterns = []string{"[.][Gg][Ii][Tt]", "raw", "user", "help", "stars", "issues", "pulls", "commits", "admin", "repo", "template", "admin"} + illegalPatterns = []string{"[.][Gg][Ii][Tt]", "raw", "user", "help", "stars", "issues", "pulls", "commits", "repo", "template", "admin"} ) // IsLegalName returns false if name contains illegal characters. @@ -222,16 +224,24 @@ func initRepoCommit(tmpPath string, sig *git.Signature) (err error) { if _, stderr, err = com.ExecCmdDir(tmpPath, "git", "add", "--all"); err != nil { return err } - log.Trace("stderr(1): %s", stderr) + if len(stderr) > 0 { + log.Trace("stderr(1): %s", stderr) + } + if _, stderr, err = com.ExecCmdDir(tmpPath, "git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", "Init commit"); err != nil { return err } - log.Trace("stderr(2): %s", stderr) + if len(stderr) > 0 { + log.Trace("stderr(2): %s", stderr) + } + if _, stderr, err = com.ExecCmdDir(tmpPath, "git", "push", "origin", "master"); err != nil { return err } - log.Trace("stderr(3): %s", stderr) + if len(stderr) > 0 { + log.Trace("stderr(3): %s", stderr) + } return nil } @@ -241,10 +251,9 @@ func createHookUpdate(hookPath, content string) error { return err } defer pu.Close() - if _, err = pu.WriteString(content); err != nil { - return err - } - return nil + + _, err = pu.WriteString(content) + return err } // InitRepository initializes README and .gitignore if needed. @@ -320,10 +329,7 @@ func initRepository(f string, user *User, repo *Repository, initReadme bool, rep } // Apply changes and commit. - if err := initRepoCommit(tmpDir, user.NewGitSig()); err != nil { - return err - } - return nil + return initRepoCommit(tmpDir, user.NewGitSig()) } // UserRepo reporesents a repository with user name. @@ -430,7 +436,8 @@ func GetRepositoryByName(userId int64, repoName string) (*Repository, error) { } // GetRepositoryById returns the repository by given id if exists. -func GetRepositoryById(id int64) (repo *Repository, err error) { +func GetRepositoryById(id int64) (*Repository, error) { + repo := &Repository{} has, err := orm.Id(id).Get(repo) if err != nil { return nil, err @@ -485,30 +492,26 @@ func GetWatches(repoId int64) ([]Watch, error) { } // NotifyWatchers creates batch of actions for every watcher. -func NotifyWatchers(userId, repoId int64, opType int, userName, repoName, refName, content string) error { +func NotifyWatchers(act *Action) error { // Add feeds for user self and all watchers. - watches, err := GetWatches(repoId) + watches, err := GetWatches(act.RepoId) if err != nil { return errors.New("repo.NotifyWatchers(get watches): " + err.Error()) } - watches = append(watches, Watch{UserId: userId}) + + // Add feed for actioner. + act.UserId = act.ActUserId + if _, err = orm.InsertOne(act); err != nil { + return errors.New("repo.NotifyWatchers(create action): " + err.Error()) + } for i := range watches { - if userId == watches[i].UserId && i > 0 { - continue // Do not add twice in case author watches his/her repository. + if act.ActUserId == watches[i].UserId { + continue } - _, err = orm.InsertOne(&Action{ - UserId: watches[i].UserId, - ActUserId: userId, - ActUserName: userName, - OpType: opType, - Content: content, - RepoId: repoId, - RepoName: repoName, - RefName: refName, - }) - if err != nil { + act.UserId = watches[i].UserId + if _, err = orm.InsertOne(act); err != nil { return errors.New("repo.NotifyWatchers(create action): " + err.Error()) } } |