Merge branch 'master' of github.com:gogits/gogs

This commit is contained in:
Lunny Xiao 2014-03-30 10:19:36 +08:00
commit cd800d7837
31 changed files with 629 additions and 351 deletions

View file

@ -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
}

View file

@ -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()
}

View file

@ -34,8 +34,7 @@ func LoadModelsConfig() {
DbCfg.Path = base.Cfg.MustValue("database", "PATH", "data/gogs.db")
}
func SetEngine() {
var err error
func SetEngine() (err error) {
switch DbCfg.Type {
case "mysql":
orm, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8",
@ -47,12 +46,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 +59,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 {

View file

@ -12,7 +12,6 @@ import (
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"
"time"
"unicode/utf8"
@ -60,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.
@ -106,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
}
}
@ -199,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.
@ -363,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 {