Git with a cup of tea, painless self-hosted git service Mirror for internal git.with.parts use https://git.with.parts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gitea/models/repo.go

322 lines
7.3 KiB

11 years ago
// 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.
11 years ago
package models
import (
"errors"
"fmt"
"io/ioutil"
11 years ago
"os"
"path/filepath"
"strings"
"time"
"unicode/utf8"
11 years ago
"github.com/Unknwon/com"
11 years ago
git "github.com/libgit2/git2go"
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log"
11 years ago
)
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"`
11 years ago
}
11 years ago
type Star struct {
Id int64
RepoId int64
UserId int64
Created time.Time `xorm:"created"`
}
var (
LanguageIgns, Licenses []string
)
var (
ErrRepoAlreadyExist = errors.New("Repository already exist")
ErrRepoNotExist = errors.New("Repository does not exist")
)
func init() {
LanguageIgns = strings.Split(base.Cfg.MustValue("repository", "LANG_IGNS"), "|")
Licenses = strings.Split(base.Cfg.MustValue("repository", "LICENSES"), "|")
}
11 years ago
// check if repository is exist
func IsRepositoryExist(user *User, repoName string) (bool, error) {
repo := Repository{OwnerId: user.Id}
has, err := orm.Where("lower_name = ?", strings.ToLower(repoName)).Get(&repo)
if err != nil {
return has, err
}
s, err := os.Stat(RepoPath(user.Name, repoName))
if err != nil {
return false, nil
}
return s.IsDir(), nil
11 years ago
}
// CreateRepository creates a repository for given user or orgnaziation.
func CreateRepository(user *User, repoName, desc, repoLang, license string, private bool, initReadme bool) (*Repository, error) {
isExist, err := IsRepositoryExist(user, repoName)
11 years ago
if err != nil {
return nil, err
} else if isExist {
return nil, ErrRepoAlreadyExist
}
repo := &Repository{
OwnerId: user.Id,
Name: repoName,
LowerName: strings.ToLower(repoName),
Description: desc,
Private: private,
}
f := RepoPath(user.Name, repoName)
if err = initRepository(f, user, repo, initReadme, repoLang, license); err != nil {
return nil, err
}
11 years ago
session := orm.NewSession()
defer session.Close()
session.Begin()
if _, err = session.Insert(repo); err != nil {
if err2 := os.RemoveAll(f); err2 != nil {
return nil, errors.New(fmt.Sprintf(
"delete repo directory %s/%s failed", user.Name, repoName))
}
11 years ago
session.Rollback()
return nil, err
}
access := Access{
UserName: user.Name,
RepoName: repo.Name,
Mode: AU_WRITABLE,
}
if _, err = session.Insert(&access); err != nil {
session.Rollback()
if err2 := os.RemoveAll(f); err2 != nil {
return nil, errors.New(fmt.Sprintf(
"delete repo directory %s/%s failed", user.Name, repoName))
}
return nil, err
}
if _, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id); err != nil {
session.Rollback()
if err2 := os.RemoveAll(f); err2 != nil {
return nil, errors.New(fmt.Sprintf(
"delete repo directory %s/%s failed", user.Name, repoName))
}
11 years ago
return nil, err
}
if err = session.Commit(); err != nil {
session.Rollback()
if err2 := os.RemoveAll(f); err2 != nil {
return nil, errors.New(fmt.Sprintf(
"delete repo directory %s/%s failed", user.Name, repoName))
}
11 years ago
return nil, err
}
return repo, NewRepoAction(user, repo)
11 years ago
}
// InitRepository initializes README and .gitignore if needed.
func initRepository(f string, user *User, repo *Repository, initReadme bool, repoLang, license string) error {
11 years ago
fileName := map[string]string{}
if initReadme {
fileName["readme"] = "README.md"
}
if repoLang != "" {
fileName["gitign"] = ".gitignore"
}
11 years ago
if license != "" {
fileName["license"] = "LICENSE"
}
11 years ago
workdir := filepath.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()))
os.MkdirAll(workdir, os.ModePerm)
11 years ago
sig := user.NewGitSig()
// README
11 years ago
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
}
}
11 years ago
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
}
}
}
11 years ago
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
}
}
}
rp, err := git.InitRepository(f, true)
if err != nil {
return err
}
rp.SetWorkdir(workdir, false)
idx, err := rp.Index()
if err != nil {
return err
}
for _, name := range fileName {
if err = idx.AddByPath(name); err != nil {
return err
}
}
treeId, err := idx.WriteTree()
if err != nil {
return err
}
message := "Init commit"
tree, err := rp.LookupTree(treeId)
if err != nil {
return err
}
if _, err = rp.CreateCommit("HEAD", sig, sig, message, tree); err != nil {
return err
}
return nil
}
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
}
// GetRepositories returns the list of repositories of given user.
func GetRepositories(user *User) ([]Repository, error) {
repos := make([]Repository, 0, 10)
err := orm.Find(&repos, &Repository{OwnerId: user.Id})
11 years ago
return repos, err
}
func GetRepositoryCount(user *User) (int64, error) {
return orm.Count(&Repository{OwnerId: user.Id})
}
11 years ago
func StarReposiory(user *User, repoName string) error {
return nil
}
func UnStarRepository() {
}
func WatchRepository() {
}
func UnWatchRepository() {
}
11 years ago
func ForkRepository(reposName string, userId int64) {
}
func RepoPath(userName, repoName string) string {
return filepath.Join(UserPath(userName), repoName+".git")
}
11 years ago
// DeleteRepository deletes a repository for a user or orgnaztion.
11 years ago
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
}
11 years ago
session := orm.NewSession()
11 years ago
if _, err = session.Delete(&Repository{Id: repoId}); err != nil {
11 years ago
session.Rollback()
return err
}
if _, err := session.Delete(&Access{UserName: userName, RepoName: repo.Name}); err != nil {
session.Rollback()
return err
}
11 years ago
if _, err = session.Exec("update user set num_repos = num_repos - 1 where id = ?", userId); err != nil {
11 years ago
session.Rollback()
return err
}
11 years ago
if err = session.Commit(); err != nil {
11 years ago
session.Rollback()
return err
}
11 years ago
if err = os.RemoveAll(RepoPath(userName, repo.Name)); err != nil {
11 years ago
// TODO: log and delete manully
11 years ago
log.Error("delete repo %s/%s failed", userName, repo.Name)
11 years ago
return err
}
return nil
}