bench-forgejo/models/repo.go

354 lines
8.4 KiB
Go
Raw Normal View History

2014-02-15 04:46:54 +05:30
// 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 19:50:57 +05:30
package models
import (
2014-03-10 05:36:29 +05:30
"errors"
2014-03-11 06:18:58 +05:30
"fmt"
"io/ioutil"
2014-02-14 19:50:57 +05:30
"os"
"path/filepath"
"strings"
2014-03-17 21:26:50 +05:30
"sync"
2014-02-14 19:50:57 +05:30
"time"
2014-03-11 10:23:53 +05:30
"unicode/utf8"
2014-02-14 19:50:57 +05:30
2014-03-17 21:26:50 +05:30
"github.com/Unknwon/cae/zip"
2014-03-11 11:02:36 +05:30
"github.com/Unknwon/com"
2014-03-17 21:26:50 +05:30
"github.com/gogits/git"
2014-03-08 03:52:15 +05:30
2014-03-11 11:02:36 +05:30
"github.com/gogits/gogs/modules/base"
2014-03-08 03:52:15 +05:30
"github.com/gogits/gogs/modules/log"
2014-02-14 19:50:57 +05:30
)
2014-03-10 05:36:29 +05:30
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 19:50:57 +05:30
}
2014-03-02 19:28:20 +05:30
type Star struct {
Id int64
RepoId int64
UserId int64
Created time.Time `xorm:"created"`
}
2014-03-11 11:02:36 +05:30
var (
2014-03-17 21:26:50 +05:30
gitInitLocker = sync.Mutex{}
2014-03-11 11:02:36 +05:30
LanguageIgns, Licenses []string
)
2014-03-10 05:36:29 +05:30
var (
ErrRepoAlreadyExist = errors.New("Repository already exist")
ErrRepoNotExist = errors.New("Repository does not exist")
2014-03-10 05:36:29 +05:30
)
2014-03-11 11:02:36 +05:30
func init() {
LanguageIgns = strings.Split(base.Cfg.MustValue("repository", "LANG_IGNS"), "|")
Licenses = strings.Split(base.Cfg.MustValue("repository", "LICENSES"), "|")
2014-03-17 21:26:50 +05:30
zip.Verbose = false
2014-03-11 11:02:36 +05:30
}
2014-02-14 19:50:57 +05:30
// check if repository is exist
2014-03-10 05:36:29 +05:30
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 15:20:53 +05:30
if err != nil {
return has, err
}
2014-03-10 05:36:29 +05:30
s, err := os.Stat(RepoPath(user.Name, repoName))
2014-02-19 15:20:53 +05:30
if err != nil {
2014-03-17 21:26:50 +05:30
return false, nil // Error simply means does not exist, but we don't want to show up.
2014-02-19 15:20:53 +05:30
}
return s.IsDir(), nil
2014-02-14 19:50:57 +05:30
}
// CreateRepository creates a repository for given user or orgnaziation.
2014-03-11 11:02:36 +05:30
func CreateRepository(user *User, repoName, desc, repoLang, license string, private bool, initReadme bool) (*Repository, error) {
2014-03-10 05:36:29 +05:30
isExist, err := IsRepositoryExist(user, repoName)
2014-02-14 19:50:57 +05:30
if err != nil {
return nil, err
2014-03-10 05:36:29 +05:30
} else if isExist {
return nil, ErrRepoAlreadyExist
}
repo := &Repository{
OwnerId: user.Id,
Name: repoName,
LowerName: strings.ToLower(repoName),
Description: desc,
Private: private,
}
2014-03-11 10:23:53 +05:30
f := RepoPath(user.Name, repoName)
2014-03-11 11:02:36 +05:30
if err = initRepository(f, user, repo, initReadme, repoLang, license); err != nil {
2014-03-11 10:23:53 +05:30
return nil, err
}
2014-02-14 19:50:57 +05:30
session := orm.NewSession()
defer session.Close()
session.Begin()
2014-03-10 05:36:29 +05:30
if _, err = session.Insert(repo); err != nil {
if err2 := os.RemoveAll(f); err2 != nil {
2014-03-11 06:18:58 +05:30
return nil, errors.New(fmt.Sprintf(
"delete repo directory %s/%s failed", user.Name, repoName))
2014-02-20 12:23:56 +05:30
}
2014-02-14 19:50:57 +05:30
session.Rollback()
return nil, err
}
2014-03-10 05:36:29 +05:30
access := Access{
UserName: user.Name,
2014-02-25 12:41:54 +05:30
RepoName: repo.Name,
Mode: AU_WRITABLE,
}
2014-03-10 05:36:29 +05:30
if _, err = session.Insert(&access); err != nil {
session.Rollback()
2014-03-10 05:36:29 +05:30
if err2 := os.RemoveAll(f); err2 != nil {
2014-03-11 06:18:58 +05:30
return nil, errors.New(fmt.Sprintf(
"delete repo directory %s/%s failed", user.Name, repoName))
2014-02-25 12:41:54 +05:30
}
return nil, err
}
2014-03-10 05:36:29 +05:30
if _, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id); err != nil {
session.Rollback()
2014-03-10 05:36:29 +05:30
if err2 := os.RemoveAll(f); err2 != nil {
2014-03-11 06:18:58 +05:30
return nil, errors.New(fmt.Sprintf(
"delete repo directory %s/%s failed", user.Name, repoName))
2014-02-20 12:23:56 +05:30
}
2014-02-14 19:50:57 +05:30
return nil, err
}
2014-03-10 05:36:29 +05:30
if err = session.Commit(); err != nil {
session.Rollback()
2014-03-10 05:36:29 +05:30
if err2 := os.RemoveAll(f); err2 != nil {
2014-03-11 06:18:58 +05:30
return nil, errors.New(fmt.Sprintf(
"delete repo directory %s/%s failed", user.Name, repoName))
2014-02-20 12:23:56 +05:30
}
2014-02-14 19:50:57 +05:30
return nil, err
}
return repo, NewRepoAction(user, repo)
2014-03-17 21:26:50 +05:30
return nil, nil
}
// extractGitBareZip extracts git-bare.zip to repository path.
func extractGitBareZip(repoPath string) error {
z, err := zip.Open("conf/content/git-bare.zip")
if err != nil {
fmt.Println("shi?")
return err
}
defer z.Close()
return z.ExtractTo(repoPath)
}
// initRepoCommit temporarily changes with work directory.
func initRepoCommit(tmpPath string, sig *git.Signature) error {
gitInitLocker.Lock()
defer gitInitLocker.Unlock()
// Change work directory.
curPath, err := os.Getwd()
if err != nil {
return err
} else if err = os.Chdir(tmpPath); err != nil {
return err
}
defer os.Chdir(curPath)
if _, _, err := com.ExecCmd("git", "add", "--all"); err != nil {
return err
}
if _, _, err := com.ExecCmd("git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email),
"-m", "Init commit"); err != nil {
return err
}
if _, _, err := com.ExecCmd("git", "push", "origin", "master"); err != nil {
return err
}
return nil
2014-02-14 19:50:57 +05:30
}
2014-03-11 06:18:58 +05:30
// InitRepository initializes README and .gitignore if needed.
2014-03-11 11:02:36 +05:30
func initRepository(f string, user *User, repo *Repository, initReadme bool, repoLang, license string) error {
2014-03-17 21:26:50 +05:30
repoPath := RepoPath(user.Name, repo.Name)
2014-03-11 15:40:19 +05:30
2014-03-17 21:26:50 +05:30
// Create bare new repository.
if err := extractGitBareZip(repoPath); err != nil {
return err
}
// Initialize repository according to user's choice.
fileName := map[string]string{}
2014-03-11 15:40:19 +05:30
if initReadme {
fileName["readme"] = "README.md"
}
if repoLang != "" {
fileName["gitign"] = ".gitignore"
2014-03-11 10:23:53 +05:30
}
2014-03-11 15:40:19 +05:30
if license != "" {
fileName["license"] = "LICENSE"
}
2014-03-17 21:26:50 +05:30
// Clone to temprory path and do the init commit.
tmpDir := filepath.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()))
os.MkdirAll(tmpDir, os.ModePerm)
2014-03-17 21:26:50 +05:30
if _, _, err := com.ExecCmd("git", "clone", repoPath, tmpDir); err != nil {
return err
}
// README
2014-03-11 15:40:19 +05:30
if initReadme {
defaultReadme := repo.Name + "\n" + strings.Repeat("=",
utf8.RuneCountInString(repo.Name)) + "\n\n" + repo.Description
2014-03-17 21:26:50 +05:30
if err := ioutil.WriteFile(filepath.Join(tmpDir, fileName["readme"]),
2014-03-11 15:40:19 +05:30
[]byte(defaultReadme), 0644); err != nil {
return err
}
}
2014-03-11 06:18:58 +05:30
2014-03-17 21:26:50 +05:30
// .gitignore
2014-03-11 15:40:19 +05:30
if repoLang != "" {
filePath := "conf/gitignore/" + repoLang
if com.IsFile(filePath) {
if _, err := com.Copy(filePath,
2014-03-17 21:26:50 +05:30
filepath.Join(tmpDir, fileName["gitign"])); err != nil {
2014-03-11 15:40:19 +05:30
return err
}
2014-03-11 11:02:36 +05:30
}
}
2014-03-11 10:23:53 +05:30
2014-03-17 21:26:50 +05:30
// LICENSE
2014-03-11 15:40:19 +05:30
if license != "" {
filePath := "conf/license/" + license
if com.IsFile(filePath) {
if _, err := com.Copy(filePath,
2014-03-17 21:26:50 +05:30
filepath.Join(tmpDir, fileName["license"])); err != nil {
2014-03-11 15:40:19 +05:30
return err
}
2014-03-11 11:02:36 +05:30
}
}
2014-03-17 21:26:50 +05:30
// Apply changes and commit.
if err := initRepoCommit(tmpDir, user.NewGitSig()); err != nil {
2014-03-16 20:02:33 +05:30
return err
}
2014-03-17 21:26:50 +05:30
return nil
2014-03-11 06:18:58 +05:30
}
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 23:34:31 +05:30
// GetRepositories returns the list of repositories of given user.
2014-03-10 05:36:29 +05:30
func GetRepositories(user *User) ([]Repository, error) {
repos := make([]Repository, 0, 10)
2014-03-16 11:58:24 +05:30
err := orm.Desc("updated").Find(&repos, &Repository{OwnerId: user.Id})
2014-02-14 19:50:57 +05:30
return repos, err
}
2014-03-11 11:47:05 +05:30
func GetRepositoryCount(user *User) (int64, error) {
return orm.Count(&Repository{OwnerId: user.Id})
}
2014-02-14 19:50:57 +05:30
func StarReposiory(user *User, repoName string) error {
return nil
}
func UnStarRepository() {
}
func WatchRepository() {
}
func UnWatchRepository() {
}
2014-03-02 19:28:20 +05:30
func ForkRepository(reposName string, userId int64) {
}
2014-02-20 12:23:56 +05:30
func RepoPath(userName, repoName string) string {
return filepath.Join(UserPath(userName), repoName+".git")
}
2014-02-15 04:46:54 +05:30
// DeleteRepository deletes a repository for a user or orgnaztion.
2014-03-13 12:09:09 +05:30
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 19:50:57 +05:30
session := orm.NewSession()
2014-03-17 12:06:28 +05:30
if err = session.Begin(); err != nil {
return err
}
2014-03-13 12:09:09 +05:30
if _, err = session.Delete(&Repository{Id: repoId}); err != nil {
2014-02-14 19:50:57 +05:30
session.Rollback()
return err
}
if _, err := session.Delete(&Access{UserName: userName, RepoName: repo.Name}); err != nil {
session.Rollback()
return err
}
2014-03-13 12:09:09 +05:30
if _, err = session.Exec("update user set num_repos = num_repos - 1 where id = ?", userId); err != nil {
2014-02-14 19:50:57 +05:30
session.Rollback()
return err
}
2014-02-15 04:46:54 +05:30
if err = session.Commit(); err != nil {
2014-02-14 19:50:57 +05:30
session.Rollback()
return err
}
2014-03-13 12:09:09 +05:30
if err = os.RemoveAll(RepoPath(userName, repo.Name)); err != nil {
2014-02-14 19:50:57 +05:30
// TODO: log and delete manully
2014-03-13 12:09:09 +05:30
log.Error("delete repo %s/%s failed", userName, repo.Name)
2014-02-14 19:50:57 +05:30
return err
}
return nil
}