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

137 lines
3.2 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 (
"os"
"path/filepath"
"strings"
"time"
git "github.com/libgit2/git2go"
"github.com/qiniu/log"
11 years ago
)
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"`
}
// check if repository is exist
func IsRepositoryExist(user *User, reposName string) (bool, error) {
repo := Repo{OwnerId: user.Id}
has, err := orm.Where("lower_name = ?", strings.ToLower(reposName)).Get(&repo)
if err != nil {
return has, err
}
s, err := os.Stat(filepath.Join(RepoRootPath, user.Name, reposName))
if err != nil {
return false, nil
}
return s.IsDir(), nil
11 years ago
}
//
// create a repository for a user or orgnaziation
//
func CreateRepository(user *User, reposName string) (*Repo, error) {
f := RepoPath(user.Name, reposName)
11 years ago
_, err := git.InitRepository(f, false)
if err != nil {
return nil, err
}
repo := Repo{OwnerId: user.Id, Name: reposName}
session := orm.NewSession()
defer session.Close()
session.Begin()
_, err = session.Insert(&repo)
if err != nil {
err2 := os.RemoveAll(f)
if err2 != nil {
log.Error("delete repo directory %s/%s failed", user.Name, reposName)
}
11 years ago
session.Rollback()
return nil, err
}
_, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id)
if err != nil {
err2 := os.RemoveAll(f)
if err2 != nil {
log.Error("delete repo directory %s/%s failed", user.Name, reposName)
}
11 years ago
session.Rollback()
return nil, err
}
err = session.Commit()
if err != nil {
err2 := os.RemoveAll(f)
if err2 != nil {
log.Error("delete repo directory %s/%s failed", user.Name, reposName)
}
11 years ago
session.Rollback()
return nil, err
}
return &repo, nil
}
// GetRepositories returns the list of repositories of given user.
11 years ago
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() {
}
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.
func DeleteRepository(user *User, reposName string) (err error) {
11 years ago
session := orm.NewSession()
11 years ago
if _, err = session.Delete(&Repo{OwnerId: user.Id, Name: reposName}); err != nil {
11 years ago
session.Rollback()
return err
}
11 years ago
if _, err = session.Exec("update user set num_repos = num_repos - 1 where id = ?", user.Id); 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
}
if err = os.RemoveAll(RepoPath(user.Name, reposName)); err != nil {
11 years ago
// TODO: log and delete manully
log.Error("delete repo %s/%s failed", user.Name, reposName)
11 years ago
return err
}
return nil
}