summaryrefslogtreecommitdiffstats
path: root/models/models.go
blob: d5a8c8cf5c9546fd83cfceac0ee2d32bc4e7530e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// 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.

package models

import (
	"os"
	"path/filepath"
	"github.com/lunny/xorm"

	git "github.com/libgit2/git2go"
)

const (
	UserRepo = iota
	OrgRepo
)

type User struct {
	Id   int64
	Name string `xorm:"unique"`
}

type Org struct {
	Id int64
}

type Repo struct {
	Id      int64
	OwnerId int64  `xorm:"unique(s)"`
	Type    int    `xorm:"unique(s)"`
	Name    string `xorm:"unique(s)"`
}

var (
	orm *xorm.Engine
)

//
// create a repository for a user or orgnaziation
//
func CreateUserRepository(root string, user *User, reposName string) error {
	p := filepath.Join(root, user.Name)
	os.MkdirAll(p, os.ModePerm)
	f := filepath.Join(p, reposName)
	_, err := git.InitRepository(f, false)
	if err != nil {
		return err
	}

	repo := Repo{OwnerId: user.Id, Type: UserRepo, Name: reposName}
	_, err = orm.Insert(&repo)
	if err != nil {
		os.RemoveAll(f)
	}
	return err
}

//
// delete a repository for a user or orgnaztion
//
func DeleteUserRepository(root string, user *User, reposName string) error {
	err := os.RemoveAll(filepath.Join(root, user.Name, reposName))
	if err != nil {
		return err
	}

	_, err = orm.Delete(&Repo{OwnerId: user.Id, Type: UserRepo, Name: reposName})
	return err
}