2014-02-15 03:16:54 +04: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 18:20:57 +04:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
git "github.com/libgit2/git2go"
|
2014-03-08 02:22:15 +04:00
|
|
|
|
|
|
|
"github.com/gogits/gogs/modules/log"
|
2014-02-14 18:20:57 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Repo struct {
|
|
|
|
Id int64
|
|
|
|
OwnerId int64 `xorm:"unique(s)"`
|
|
|
|
ForkId int64
|
|
|
|
LowerName string `xorm:"unique(s) index not null"`
|
|
|
|
Name string `xorm:"index not null"`
|
|
|
|
NumWatchs int
|
|
|
|
NumStars int
|
|
|
|
NumForks int
|
|
|
|
Created time.Time `xorm:"created"`
|
|
|
|
Updated time.Time `xorm:"updated"`
|
|
|
|
}
|
|
|
|
|
2014-03-02 17:58:20 +04:00
|
|
|
type Star struct {
|
|
|
|
Id int64
|
|
|
|
RepoId int64
|
|
|
|
UserId int64
|
|
|
|
Created time.Time `xorm:"created"`
|
|
|
|
}
|
|
|
|
|
2014-02-14 18:20:57 +04:00
|
|
|
// check if repository is exist
|
|
|
|
func IsRepositoryExist(user *User, reposName string) (bool, error) {
|
|
|
|
repo := Repo{OwnerId: user.Id}
|
2014-02-19 13:50:53 +04:00
|
|
|
has, err := orm.Where("lower_name = ?", strings.ToLower(reposName)).Get(&repo)
|
|
|
|
if err != nil {
|
|
|
|
return has, err
|
|
|
|
}
|
2014-02-20 13:52:07 +04:00
|
|
|
s, err := os.Stat(RepoPath(user.Name, reposName))
|
2014-02-19 13:50:53 +04:00
|
|
|
if err != nil {
|
2014-02-20 10:53:56 +04:00
|
|
|
return false, nil
|
2014-02-19 13:50:53 +04:00
|
|
|
}
|
|
|
|
return s.IsDir(), nil
|
2014-02-14 18:20:57 +04:00
|
|
|
}
|
|
|
|
|
2014-03-08 02:08:21 +04:00
|
|
|
// CreateRepository creates a repository for given user or orgnaziation.
|
2014-02-14 18:20:57 +04:00
|
|
|
func CreateRepository(user *User, reposName string) (*Repo, error) {
|
2014-02-20 10:53:56 +04:00
|
|
|
f := RepoPath(user.Name, reposName)
|
2014-02-25 11:11:54 +04:00
|
|
|
_, err := git.InitRepository(f, true)
|
2014-02-14 18:20:57 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-02-25 11:11:54 +04:00
|
|
|
repo := Repo{OwnerId: user.Id, Name: reposName, LowerName: strings.ToLower(reposName)}
|
2014-02-14 18:20:57 +04:00
|
|
|
session := orm.NewSession()
|
|
|
|
defer session.Close()
|
|
|
|
session.Begin()
|
|
|
|
_, err = session.Insert(&repo)
|
|
|
|
if err != nil {
|
2014-02-20 10:53:56 +04:00
|
|
|
err2 := os.RemoveAll(f)
|
|
|
|
if err2 != nil {
|
|
|
|
log.Error("delete repo directory %s/%s failed", user.Name, reposName)
|
|
|
|
}
|
2014-02-14 18:20:57 +04:00
|
|
|
session.Rollback()
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-02-25 11:11:54 +04:00
|
|
|
access := Access{UserName: user.Name,
|
|
|
|
RepoName: repo.Name,
|
|
|
|
Mode: AU_WRITABLE,
|
|
|
|
}
|
|
|
|
_, err = session.Insert(&access)
|
|
|
|
if err != nil {
|
|
|
|
err2 := os.RemoveAll(f)
|
|
|
|
if err2 != nil {
|
|
|
|
log.Error("delete repo directory %s/%s failed", user.Name, reposName)
|
|
|
|
}
|
|
|
|
session.Rollback()
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-02-14 18:20:57 +04:00
|
|
|
_, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id)
|
|
|
|
if err != nil {
|
2014-02-20 10:53:56 +04:00
|
|
|
err2 := os.RemoveAll(f)
|
|
|
|
if err2 != nil {
|
|
|
|
log.Error("delete repo directory %s/%s failed", user.Name, reposName)
|
|
|
|
}
|
2014-02-14 18:20:57 +04:00
|
|
|
session.Rollback()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = session.Commit()
|
|
|
|
if err != nil {
|
2014-02-20 10:53:56 +04:00
|
|
|
err2 := os.RemoveAll(f)
|
|
|
|
if err2 != nil {
|
|
|
|
log.Error("delete repo directory %s/%s failed", user.Name, reposName)
|
|
|
|
}
|
2014-02-14 18:20:57 +04:00
|
|
|
session.Rollback()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &repo, nil
|
|
|
|
}
|
|
|
|
|
2014-02-19 22:04:31 +04:00
|
|
|
// GetRepositories returns the list of repositories of given user.
|
2014-02-14 18:20:57 +04:00
|
|
|
func GetRepositories(user *User) ([]Repo, error) {
|
|
|
|
repos := make([]Repo, 0)
|
|
|
|
err := orm.Find(&repos, &Repo{OwnerId: user.Id})
|
|
|
|
return repos, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func StarReposiory(user *User, repoName string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnStarRepository() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func WatchRepository() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnWatchRepository() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-02 17:58:20 +04:00
|
|
|
func ForkRepository(reposName string, userId int64) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:56 +04:00
|
|
|
func RepoPath(userName, repoName string) string {
|
|
|
|
return filepath.Join(UserPath(userName), repoName+".git")
|
|
|
|
}
|
|
|
|
|
2014-02-15 03:16:54 +04:00
|
|
|
// DeleteRepository deletes a repository for a user or orgnaztion.
|
|
|
|
func DeleteRepository(user *User, reposName string) (err error) {
|
2014-02-14 18:20:57 +04:00
|
|
|
session := orm.NewSession()
|
2014-02-15 03:16:54 +04:00
|
|
|
if _, err = session.Delete(&Repo{OwnerId: user.Id, Name: reposName}); err != nil {
|
2014-02-14 18:20:57 +04:00
|
|
|
session.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
2014-02-15 03:16:54 +04:00
|
|
|
if _, err = session.Exec("update user set num_repos = num_repos - 1 where id = ?", user.Id); err != nil {
|
2014-02-14 18:20:57 +04:00
|
|
|
session.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
2014-02-15 03:16:54 +04:00
|
|
|
if err = session.Commit(); err != nil {
|
2014-02-14 18:20:57 +04:00
|
|
|
session.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
2014-02-20 10:53:56 +04:00
|
|
|
if err = os.RemoveAll(RepoPath(user.Name, reposName)); err != nil {
|
2014-02-14 18:20:57 +04:00
|
|
|
// TODO: log and delete manully
|
2014-02-20 10:53:56 +04:00
|
|
|
log.Error("delete repo %s/%s failed", user.Name, reposName)
|
2014-02-14 18:20:57 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|