forgejo-federation/models/repo.go

124 lines
2.8 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 (
"os"
"path/filepath"
"strings"
"time"
git "github.com/libgit2/git2go"
)
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}
2014-02-19 15:20:53 +05:30
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, err
}
return s.IsDir(), nil
2014-02-14 19:50:57 +05:30
}
//
// create a repository for a user or orgnaziation
//
func CreateRepository(user *User, reposName string) (*Repo, error) {
2014-02-19 15:20:53 +05:30
p := filepath.Join(RepoRootPath, user.Name)
2014-02-14 19:50:57 +05:30
os.MkdirAll(p, os.ModePerm)
2014-02-17 21:27:23 +05:30
f := filepath.Join(p, reposName+".git")
2014-02-14 19:50:57 +05:30
_, 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 {
os.RemoveAll(f)
session.Rollback()
return nil, err
}
_, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id)
if err != nil {
os.RemoveAll(f)
session.Rollback()
return nil, err
}
err = session.Commit()
if err != nil {
os.RemoveAll(f)
session.Rollback()
return nil, err
}
return &repo, nil
}
2014-02-19 23:34:31 +05:30
// GetRepositories returns the list of repositories of given user.
2014-02-14 19:50:57 +05:30
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-02-15 04:46:54 +05:30
// DeleteRepository deletes a repository for a user or orgnaztion.
func DeleteRepository(user *User, reposName string) (err error) {
2014-02-14 19:50:57 +05:30
session := orm.NewSession()
2014-02-15 04:46:54 +05:30
if _, err = session.Delete(&Repo{OwnerId: user.Id, Name: reposName}); 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.Exec("update user set num_repos = num_repos - 1 where id = ?", user.Id); 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-02-19 15:20:53 +05:30
if err = os.RemoveAll(filepath.Join(RepoRootPath, user.Name, reposName+".git")); err != nil {
2014-02-14 19:50:57 +05:30
// TODO: log and delete manully
return err
}
return nil
}