2014-02-14 18:16:54 -05:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-02-14 09:20:57 -05:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2014-03-09 19:06:29 -05:00
|
|
|
"errors"
|
2014-03-10 19:48:58 -05:00
|
|
|
"fmt"
|
2014-03-10 23:18:44 -05:00
|
|
|
"io/ioutil"
|
2014-02-14 09:20:57 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2014-03-10 23:53:53 -05:00
|
|
|
"unicode/utf8"
|
2014-02-14 09:20:57 -05:00
|
|
|
|
2014-03-11 00:32:36 -05:00
|
|
|
"github.com/Unknwon/com"
|
2014-02-14 09:20:57 -05:00
|
|
|
git "github.com/libgit2/git2go"
|
2014-03-07 17:22:15 -05:00
|
|
|
|
2014-03-11 00:32:36 -05:00
|
|
|
"github.com/gogits/gogs/modules/base"
|
2014-03-07 17:22:15 -05:00
|
|
|
"github.com/gogits/gogs/modules/log"
|
2014-02-14 09:20:57 -05:00
|
|
|
)
|
|
|
|
|
2014-03-09 19:06:29 -05:00
|
|
|
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
|
|
|
|
Private bool
|
|
|
|
NumWatchs int
|
|
|
|
NumStars int
|
|
|
|
NumForks int
|
|
|
|
Created time.Time `xorm:"created"`
|
|
|
|
Updated time.Time `xorm:"updated"`
|
2014-02-14 09:20:57 -05:00
|
|
|
}
|
|
|
|
|
2014-03-02 08:58:20 -05:00
|
|
|
type Star struct {
|
|
|
|
Id int64
|
|
|
|
RepoId int64
|
|
|
|
UserId int64
|
|
|
|
Created time.Time `xorm:"created"`
|
|
|
|
}
|
|
|
|
|
2014-03-11 00:32:36 -05:00
|
|
|
var (
|
|
|
|
LanguageIgns, Licenses []string
|
|
|
|
)
|
|
|
|
|
2014-03-09 19:06:29 -05:00
|
|
|
var (
|
|
|
|
ErrRepoAlreadyExist = errors.New("Repository already exist")
|
2014-03-12 21:27:11 -05:00
|
|
|
ErrRepoNotExist = errors.New("Repository does not exist")
|
2014-03-09 19:06:29 -05:00
|
|
|
)
|
|
|
|
|
2014-03-11 00:32:36 -05:00
|
|
|
func init() {
|
|
|
|
LanguageIgns = strings.Split(base.Cfg.MustValue("repository", "LANG_IGNS"), "|")
|
|
|
|
Licenses = strings.Split(base.Cfg.MustValue("repository", "LICENSES"), "|")
|
|
|
|
}
|
|
|
|
|
2014-02-14 09:20:57 -05:00
|
|
|
// check if repository is exist
|
2014-03-09 19:06:29 -05:00
|
|
|
func IsRepositoryExist(user *User, repoName string) (bool, error) {
|
|
|
|
repo := Repository{OwnerId: user.Id}
|
|
|
|
has, err := orm.Where("lower_name = ?", strings.ToLower(repoName)).Get(&repo)
|
2014-02-19 04:50:53 -05:00
|
|
|
if err != nil {
|
|
|
|
return has, err
|
|
|
|
}
|
2014-03-09 19:06:29 -05:00
|
|
|
s, err := os.Stat(RepoPath(user.Name, repoName))
|
2014-02-19 04:50:53 -05:00
|
|
|
if err != nil {
|
2014-02-20 01:53:56 -05:00
|
|
|
return false, nil
|
2014-02-19 04:50:53 -05:00
|
|
|
}
|
|
|
|
return s.IsDir(), nil
|
2014-02-14 09:20:57 -05:00
|
|
|
}
|
|
|
|
|
2014-03-07 17:08:21 -05:00
|
|
|
// CreateRepository creates a repository for given user or orgnaziation.
|
2014-03-11 00:32:36 -05:00
|
|
|
func CreateRepository(user *User, repoName, desc, repoLang, license string, private bool, initReadme bool) (*Repository, error) {
|
2014-03-09 19:06:29 -05:00
|
|
|
isExist, err := IsRepositoryExist(user, repoName)
|
2014-02-14 09:20:57 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-03-09 19:06:29 -05:00
|
|
|
} else if isExist {
|
|
|
|
return nil, ErrRepoAlreadyExist
|
|
|
|
}
|
|
|
|
|
|
|
|
repo := &Repository{
|
|
|
|
OwnerId: user.Id,
|
|
|
|
Name: repoName,
|
|
|
|
LowerName: strings.ToLower(repoName),
|
|
|
|
Description: desc,
|
|
|
|
Private: private,
|
|
|
|
}
|
2014-03-10 23:53:53 -05:00
|
|
|
|
|
|
|
f := RepoPath(user.Name, repoName)
|
2014-03-11 00:32:36 -05:00
|
|
|
if err = initRepository(f, user, repo, initReadme, repoLang, license); err != nil {
|
2014-03-10 23:53:53 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-02-14 09:20:57 -05:00
|
|
|
session := orm.NewSession()
|
|
|
|
defer session.Close()
|
|
|
|
session.Begin()
|
2014-03-09 19:06:29 -05:00
|
|
|
|
|
|
|
if _, err = session.Insert(repo); err != nil {
|
|
|
|
if err2 := os.RemoveAll(f); err2 != nil {
|
2014-03-10 19:48:58 -05:00
|
|
|
return nil, errors.New(fmt.Sprintf(
|
|
|
|
"delete repo directory %s/%s failed", user.Name, repoName))
|
2014-02-20 01:53:56 -05:00
|
|
|
}
|
2014-02-14 09:20:57 -05:00
|
|
|
session.Rollback()
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-03-09 19:06:29 -05:00
|
|
|
|
|
|
|
access := Access{
|
|
|
|
UserName: user.Name,
|
2014-02-25 02:11:54 -05:00
|
|
|
RepoName: repo.Name,
|
|
|
|
Mode: AU_WRITABLE,
|
|
|
|
}
|
2014-03-09 19:06:29 -05:00
|
|
|
if _, err = session.Insert(&access); err != nil {
|
2014-03-10 23:18:44 -05:00
|
|
|
session.Rollback()
|
2014-03-09 19:06:29 -05:00
|
|
|
if err2 := os.RemoveAll(f); err2 != nil {
|
2014-03-10 19:48:58 -05:00
|
|
|
return nil, errors.New(fmt.Sprintf(
|
|
|
|
"delete repo directory %s/%s failed", user.Name, repoName))
|
2014-02-25 02:11:54 -05:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-03-09 19:06:29 -05:00
|
|
|
|
|
|
|
if _, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id); err != nil {
|
2014-03-10 23:18:44 -05:00
|
|
|
session.Rollback()
|
2014-03-09 19:06:29 -05:00
|
|
|
if err2 := os.RemoveAll(f); err2 != nil {
|
2014-03-10 19:48:58 -05:00
|
|
|
return nil, errors.New(fmt.Sprintf(
|
|
|
|
"delete repo directory %s/%s failed", user.Name, repoName))
|
2014-02-20 01:53:56 -05:00
|
|
|
}
|
2014-02-14 09:20:57 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-03-09 19:06:29 -05:00
|
|
|
|
|
|
|
if err = session.Commit(); err != nil {
|
2014-03-10 23:18:44 -05:00
|
|
|
session.Rollback()
|
2014-03-09 19:06:29 -05:00
|
|
|
if err2 := os.RemoveAll(f); err2 != nil {
|
2014-03-10 19:48:58 -05:00
|
|
|
return nil, errors.New(fmt.Sprintf(
|
|
|
|
"delete repo directory %s/%s failed", user.Name, repoName))
|
2014-02-20 01:53:56 -05:00
|
|
|
}
|
2014-02-14 09:20:57 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-03-13 00:14:43 -05:00
|
|
|
|
|
|
|
return repo, NewRepoAction(user, repo)
|
2014-02-14 09:20:57 -05:00
|
|
|
}
|
|
|
|
|
2014-03-10 19:48:58 -05:00
|
|
|
// InitRepository initializes README and .gitignore if needed.
|
2014-03-11 00:32:36 -05:00
|
|
|
func initRepository(f string, user *User, repo *Repository, initReadme bool, repoLang, license string) error {
|
2014-03-11 05:10:19 -05:00
|
|
|
fileName := map[string]string{}
|
|
|
|
|
|
|
|
if initReadme {
|
|
|
|
fileName["readme"] = "README.md"
|
|
|
|
}
|
|
|
|
if repoLang != "" {
|
|
|
|
fileName["gitign"] = ".gitignore"
|
2014-03-10 23:53:53 -05:00
|
|
|
}
|
2014-03-11 05:10:19 -05:00
|
|
|
if license != "" {
|
|
|
|
fileName["license"] = "LICENSE"
|
|
|
|
}
|
|
|
|
|
2014-03-11 21:09:33 -05:00
|
|
|
workdir := filepath.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()))
|
2014-03-11 00:32:36 -05:00
|
|
|
os.MkdirAll(workdir, os.ModePerm)
|
2014-03-10 23:18:44 -05:00
|
|
|
|
2014-03-11 05:10:19 -05:00
|
|
|
sig := user.NewGitSig()
|
2014-03-10 23:18:44 -05:00
|
|
|
|
|
|
|
// README
|
2014-03-11 05:10:19 -05:00
|
|
|
if initReadme {
|
|
|
|
defaultReadme := repo.Name + "\n" + strings.Repeat("=",
|
|
|
|
utf8.RuneCountInString(repo.Name)) + "\n\n" + repo.Description
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(workdir, fileName["readme"]),
|
|
|
|
[]byte(defaultReadme), 0644); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-03-10 23:18:44 -05:00
|
|
|
}
|
2014-03-10 19:48:58 -05:00
|
|
|
|
2014-03-11 05:10:19 -05:00
|
|
|
if repoLang != "" {
|
|
|
|
// .gitignore
|
|
|
|
filePath := "conf/gitignore/" + repoLang
|
|
|
|
if com.IsFile(filePath) {
|
|
|
|
if _, err := com.Copy(filePath,
|
|
|
|
filepath.Join(workdir, fileName["gitign"])); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-03-11 00:32:36 -05:00
|
|
|
}
|
|
|
|
}
|
2014-03-10 23:53:53 -05:00
|
|
|
|
2014-03-11 05:10:19 -05:00
|
|
|
if license != "" {
|
|
|
|
// LICENSE
|
|
|
|
filePath := "conf/license/" + license
|
|
|
|
if com.IsFile(filePath) {
|
|
|
|
if _, err := com.Copy(filePath,
|
|
|
|
filepath.Join(workdir, fileName["license"])); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-03-11 00:32:36 -05:00
|
|
|
}
|
|
|
|
}
|
2014-03-10 23:18:44 -05:00
|
|
|
|
|
|
|
rp, err := git.InitRepository(f, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rp.SetWorkdir(workdir, false)
|
|
|
|
|
|
|
|
idx, err := rp.Index()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-03-11 00:32:36 -05:00
|
|
|
for _, name := range fileName {
|
|
|
|
if err = idx.AddByPath(name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-03-10 23:18:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
treeId, err := idx.WriteTree()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-03-10 23:53:53 -05:00
|
|
|
message := "Init commit"
|
2014-03-10 23:18:44 -05:00
|
|
|
tree, err := rp.LookupTree(treeId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-03-10 23:53:53 -05:00
|
|
|
if _, err = rp.CreateCommit("HEAD", sig, sig, message, tree); err != nil {
|
2014-03-10 23:18:44 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-03-10 19:48:58 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-12 21:27:11 -05:00
|
|
|
func GetRepositoryByName(user *User, repoName string) (*Repository, error) {
|
|
|
|
repo := &Repository{
|
|
|
|
OwnerId: user.Id,
|
|
|
|
LowerName: strings.ToLower(repoName),
|
|
|
|
}
|
|
|
|
has, err := orm.Get(repo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
|
|
|
return nil, ErrRepoNotExist
|
|
|
|
}
|
|
|
|
return repo, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetRepositoryById(id int64) (repo *Repository, err error) {
|
|
|
|
has, err := orm.Id(id).Get(repo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
|
|
|
return nil, ErrRepoNotExist
|
|
|
|
}
|
|
|
|
return repo, err
|
|
|
|
}
|
|
|
|
|
2014-02-19 13:04:31 -05:00
|
|
|
// GetRepositories returns the list of repositories of given user.
|
2014-03-09 19:06:29 -05:00
|
|
|
func GetRepositories(user *User) ([]Repository, error) {
|
|
|
|
repos := make([]Repository, 0, 10)
|
|
|
|
err := orm.Find(&repos, &Repository{OwnerId: user.Id})
|
2014-02-14 09:20:57 -05:00
|
|
|
return repos, err
|
|
|
|
}
|
|
|
|
|
2014-03-11 01:17:05 -05:00
|
|
|
func GetRepositoryCount(user *User) (int64, error) {
|
|
|
|
return orm.Count(&Repository{OwnerId: user.Id})
|
|
|
|
}
|
|
|
|
|
2014-02-14 09:20:57 -05:00
|
|
|
func StarReposiory(user *User, repoName string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnStarRepository() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func WatchRepository() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnWatchRepository() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-02 08:58:20 -05:00
|
|
|
func ForkRepository(reposName string, userId int64) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-20 01:53:56 -05:00
|
|
|
func RepoPath(userName, repoName string) string {
|
|
|
|
return filepath.Join(UserPath(userName), repoName+".git")
|
|
|
|
}
|
|
|
|
|
2014-02-14 18:16:54 -05:00
|
|
|
// DeleteRepository deletes a repository for a user or orgnaztion.
|
2014-03-13 01:39:09 -05:00
|
|
|
func DeleteRepository(userId, repoId int64, userName string) (err error) {
|
|
|
|
repo := &Repository{Id: repoId, OwnerId: userId}
|
|
|
|
has, err := orm.Get(repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !has {
|
|
|
|
return ErrRepoNotExist
|
|
|
|
}
|
|
|
|
|
2014-02-14 09:20:57 -05:00
|
|
|
session := orm.NewSession()
|
2014-03-13 01:39:09 -05:00
|
|
|
if _, err = session.Delete(&Repository{Id: repoId}); err != nil {
|
2014-02-14 09:20:57 -05:00
|
|
|
session.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
2014-03-13 02:39:18 -05:00
|
|
|
if _, err := session.Delete(&Access{UserName: userName, RepoName: repo.Name}); err != nil {
|
|
|
|
session.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
2014-03-13 01:39:09 -05:00
|
|
|
if _, err = session.Exec("update user set num_repos = num_repos - 1 where id = ?", userId); err != nil {
|
2014-02-14 09:20:57 -05:00
|
|
|
session.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
2014-02-14 18:16:54 -05:00
|
|
|
if err = session.Commit(); err != nil {
|
2014-02-14 09:20:57 -05:00
|
|
|
session.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
2014-03-13 01:39:09 -05:00
|
|
|
if err = os.RemoveAll(RepoPath(userName, repo.Name)); err != nil {
|
2014-02-14 09:20:57 -05:00
|
|
|
// TODO: log and delete manully
|
2014-03-13 01:39:09 -05:00
|
|
|
log.Error("delete repo %s/%s failed", userName, repo.Name)
|
2014-02-14 09:20:57 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|