forgejo/models/repo.go

190 lines
4.4 KiB
Go
Raw Normal View History

2014-02-14 23:16:54 +00: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 14:20:57 +00:00
package models
import (
2014-03-10 00:06:29 +00:00
"errors"
2014-03-11 00:48:58 +00:00
"fmt"
2014-02-14 14:20:57 +00:00
"os"
"path/filepath"
"strings"
"time"
git "github.com/libgit2/git2go"
2014-03-07 22:22:15 +00:00
"github.com/gogits/gogs/modules/log"
2014-02-14 14:20:57 +00:00
)
2014-03-10 00:06:29 +00: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 14:20:57 +00:00
}
2014-03-02 13:58:20 +00:00
type Star struct {
Id int64
RepoId int64
UserId int64
Created time.Time `xorm:"created"`
}
2014-03-10 00:06:29 +00:00
var (
ErrRepoAlreadyExist = errors.New("Repository already exist")
)
2014-02-14 14:20:57 +00:00
// check if repository is exist
2014-03-10 00:06:29 +00: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 09:50:53 +00:00
if err != nil {
return has, err
}
2014-03-10 00:06:29 +00:00
s, err := os.Stat(RepoPath(user.Name, repoName))
2014-02-19 09:50:53 +00:00
if err != nil {
2014-02-20 06:53:56 +00:00
return false, nil
2014-02-19 09:50:53 +00:00
}
return s.IsDir(), nil
2014-02-14 14:20:57 +00:00
}
// CreateRepository creates a repository for given user or orgnaziation.
2014-03-10 00:06:29 +00:00
func CreateRepository(user *User, repoName, desc string, private bool) (*Repository, error) {
isExist, err := IsRepositoryExist(user, repoName)
2014-02-14 14:20:57 +00:00
if err != nil {
return nil, err
2014-03-10 00:06:29 +00:00
} else if isExist {
return nil, ErrRepoAlreadyExist
}
f := RepoPath(user.Name, repoName)
if _, err = git.InitRepository(f, true); err != nil {
return nil, err
2014-02-14 14:20:57 +00:00
}
2014-03-10 00:06:29 +00:00
repo := &Repository{
OwnerId: user.Id,
Name: repoName,
LowerName: strings.ToLower(repoName),
Description: desc,
Private: private,
}
2014-02-14 14:20:57 +00:00
session := orm.NewSession()
defer session.Close()
session.Begin()
2014-03-10 00:06:29 +00:00
if _, err = session.Insert(repo); err != nil {
if err2 := os.RemoveAll(f); err2 != nil {
2014-03-11 00:48:58 +00:00
return nil, errors.New(fmt.Sprintf(
"delete repo directory %s/%s failed", user.Name, repoName))
2014-02-20 06:53:56 +00:00
}
2014-02-14 14:20:57 +00:00
session.Rollback()
return nil, err
}
2014-03-10 00:06:29 +00:00
// TODO: RemoveAll may fail due to not root access.
access := Access{
UserName: user.Name,
2014-02-25 07:11:54 +00:00
RepoName: repo.Name,
Mode: AU_WRITABLE,
}
2014-03-10 00:06:29 +00:00
if _, err = session.Insert(&access); err != nil {
if err2 := os.RemoveAll(f); err2 != nil {
2014-03-11 00:48:58 +00:00
return nil, errors.New(fmt.Sprintf(
"delete repo directory %s/%s failed", user.Name, repoName))
2014-02-25 07:11:54 +00:00
}
session.Rollback()
return nil, err
}
2014-03-10 00:06:29 +00:00
if _, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id); err != nil {
if err2 := os.RemoveAll(f); err2 != nil {
2014-03-11 00:48:58 +00:00
return nil, errors.New(fmt.Sprintf(
"delete repo directory %s/%s failed", user.Name, repoName))
2014-02-20 06:53:56 +00:00
}
2014-02-14 14:20:57 +00:00
session.Rollback()
return nil, err
}
2014-03-10 00:06:29 +00:00
if err = session.Commit(); err != nil {
if err2 := os.RemoveAll(f); err2 != nil {
2014-03-11 00:48:58 +00:00
return nil, errors.New(fmt.Sprintf(
"delete repo directory %s/%s failed", user.Name, repoName))
2014-02-20 06:53:56 +00:00
}
2014-02-14 14:20:57 +00:00
session.Rollback()
return nil, err
}
2014-03-10 00:06:29 +00:00
return repo, nil
2014-02-14 14:20:57 +00:00
}
2014-03-11 00:48:58 +00:00
// InitRepository initializes README and .gitignore if needed.
func InitRepository(repo *Repository, initReadme bool, repoLang string) error {
// README.
// .gitignore
return nil
}
2014-02-19 18:04:31 +00:00
// GetRepositories returns the list of repositories of given user.
2014-03-10 00:06:29 +00:00
func GetRepositories(user *User) ([]Repository, error) {
repos := make([]Repository, 0, 10)
err := orm.Find(&repos, &Repository{OwnerId: user.Id})
2014-02-14 14:20:57 +00:00
return repos, err
}
func StarReposiory(user *User, repoName string) error {
return nil
}
func UnStarRepository() {
}
func WatchRepository() {
}
func UnWatchRepository() {
}
2014-03-02 13:58:20 +00:00
func ForkRepository(reposName string, userId int64) {
}
2014-02-20 06:53:56 +00:00
func RepoPath(userName, repoName string) string {
return filepath.Join(UserPath(userName), repoName+".git")
}
2014-02-14 23:16:54 +00:00
// DeleteRepository deletes a repository for a user or orgnaztion.
func DeleteRepository(user *User, reposName string) (err error) {
2014-02-14 14:20:57 +00:00
session := orm.NewSession()
2014-03-10 00:06:29 +00:00
if _, err = session.Delete(&Repository{OwnerId: user.Id, Name: reposName}); err != nil {
2014-02-14 14:20:57 +00:00
session.Rollback()
return err
}
2014-02-14 23:16:54 +00:00
if _, err = session.Exec("update user set num_repos = num_repos - 1 where id = ?", user.Id); err != nil {
2014-02-14 14:20:57 +00:00
session.Rollback()
return err
}
2014-02-14 23:16:54 +00:00
if err = session.Commit(); err != nil {
2014-02-14 14:20:57 +00:00
session.Rollback()
return err
}
2014-02-20 06:53:56 +00:00
if err = os.RemoveAll(RepoPath(user.Name, reposName)); err != nil {
2014-02-14 14:20:57 +00:00
// TODO: log and delete manully
2014-02-20 06:53:56 +00:00
log.Error("delete repo %s/%s failed", user.Name, reposName)
2014-02-14 14:20:57 +00:00
return err
}
return nil
}