Goit

Simple and lightweight Git web server
git clone http://git.omkov.net/Goit
Log | Tree | Refs | README | Download

Goit/src/goit/repo.go (330 lines, 7.2 KiB) -rw-r--r-- file download

ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
0
// Copyright (C) 2023, Jakob Wakeling
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
1
// All rights reserved.
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
2
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
3
package goit
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
4
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
5
import (
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
6
	"database/sql"
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
7
	"errors"
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
8
	"log"
4a160dd Jakob Wakeling 2023-07-21 00:47:21
9
	"os"
70fd6ec Jakob Wakeling 2023-12-24 11:00:09
10
	"path/filepath"
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
11
	"strings"
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
12
570144e Jakob Wakeling 2023-12-15 23:28:06
13
	"github.com/Jamozed/Goit/src/util"
e08e053 Jakob Wakeling 2023-07-18 21:31:14
14
	"github.com/go-git/go-git/v5"
570144e Jakob Wakeling 2023-12-15 23:28:06
15
	gitconfig "github.com/go-git/go-git/v5/config"
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
16
	"github.com/go-git/go-git/v5/plumbing"
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
17
)
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
18
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
19
type Repo struct {
778d467 Jakob Wakeling 2024-01-17 18:29:55
20
	Id            int64      `json:"id"`
778d467 Jakob Wakeling 2024-01-17 18:29:55
21
	OwnerId       int64      `json:"owner_id"`
778d467 Jakob Wakeling 2024-01-17 18:29:55
22
	Name          string     `json:"name"`
778d467 Jakob Wakeling 2024-01-17 18:29:55
23
	Description   string     `json:"description"`
778d467 Jakob Wakeling 2024-01-17 18:29:55
24
	DefaultBranch string     `json:"default_branch"`
778d467 Jakob Wakeling 2024-01-17 18:29:55
25
	Upstream      string     `json:"upstream"`
778d467 Jakob Wakeling 2024-01-17 18:29:55
26
	Visibility    Visibility `json:"visibility"`
778d467 Jakob Wakeling 2024-01-17 18:29:55
27
	IsMirror      bool       `json:"is_mirror"`
778d467 Jakob Wakeling 2024-01-17 18:29:55
28
}
778d467 Jakob Wakeling 2024-01-17 18:29:55
29
778d467 Jakob Wakeling 2024-01-17 18:29:55
30
type Visibility int32
778d467 Jakob Wakeling 2024-01-17 18:29:55
31
778d467 Jakob Wakeling 2024-01-17 18:29:55
32
const (
778d467 Jakob Wakeling 2024-01-17 18:29:55
33
	Public  Visibility = 0
778d467 Jakob Wakeling 2024-01-17 18:29:55
34
	Private Visibility = 1
778d467 Jakob Wakeling 2024-01-17 18:29:55
35
	Limited Visibility = 2
778d467 Jakob Wakeling 2024-01-17 18:29:55
36
)
778d467 Jakob Wakeling 2024-01-17 18:29:55
37
778d467 Jakob Wakeling 2024-01-17 18:29:55
38
func VisibilityFromString(s string) Visibility {
778d467 Jakob Wakeling 2024-01-17 18:29:55
39
	switch strings.ToLower(s) {
778d467 Jakob Wakeling 2024-01-17 18:29:55
40
	case "public":
778d467 Jakob Wakeling 2024-01-17 18:29:55
41
		return Public
778d467 Jakob Wakeling 2024-01-17 18:29:55
42
	case "private":
778d467 Jakob Wakeling 2024-01-17 18:29:55
43
		return Private
778d467 Jakob Wakeling 2024-01-17 18:29:55
44
	case "limited":
778d467 Jakob Wakeling 2024-01-17 18:29:55
45
		return Limited
778d467 Jakob Wakeling 2024-01-17 18:29:55
46
	default:
778d467 Jakob Wakeling 2024-01-17 18:29:55
47
		return -1
778d467 Jakob Wakeling 2024-01-17 18:29:55
48
	}
778d467 Jakob Wakeling 2024-01-17 18:29:55
49
}
778d467 Jakob Wakeling 2024-01-17 18:29:55
50
778d467 Jakob Wakeling 2024-01-17 18:29:55
51
func (v Visibility) String() string {
778d467 Jakob Wakeling 2024-01-17 18:29:55
52
	return [...]string{"public", "private", "limited"}[v]
e08e053 Jakob Wakeling 2023-07-18 21:31:14
53
}
e08e053 Jakob Wakeling 2023-07-18 21:31:14
54
463cf58 Jakob Wakeling 2023-11-22 23:25:37
55
func GetRepos() ([]Repo, error) {
463cf58 Jakob Wakeling 2023-11-22 23:25:37
56
	repos := []Repo{}
463cf58 Jakob Wakeling 2023-11-22 23:25:37
57
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
58
	rows, err := db.Query(
778d467 Jakob Wakeling 2024-01-17 18:29:55
59
		"SELECT id, owner_id, name, description, default_branch, upstream, visibility, is_mirror FROM repos",
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
60
	)
463cf58 Jakob Wakeling 2023-11-22 23:25:37
61
	if err != nil {
463cf58 Jakob Wakeling 2023-11-22 23:25:37
62
		return nil, err
463cf58 Jakob Wakeling 2023-11-22 23:25:37
63
	}
463cf58 Jakob Wakeling 2023-11-22 23:25:37
64
463cf58 Jakob Wakeling 2023-11-22 23:25:37
65
	defer rows.Close()
463cf58 Jakob Wakeling 2023-11-22 23:25:37
66
463cf58 Jakob Wakeling 2023-11-22 23:25:37
67
	for rows.Next() {
463cf58 Jakob Wakeling 2023-11-22 23:25:37
68
		r := Repo{}
570144e Jakob Wakeling 2023-12-15 23:28:06
69
		if err := rows.Scan(
778d467 Jakob Wakeling 2024-01-17 18:29:55
70
			&r.Id, &r.OwnerId, &r.Name, &r.Description, &r.DefaultBranch, &r.Upstream, &r.Visibility, &r.IsMirror,
570144e Jakob Wakeling 2023-12-15 23:28:06
71
		); err != nil {
463cf58 Jakob Wakeling 2023-11-22 23:25:37
72
			return nil, err
463cf58 Jakob Wakeling 2023-11-22 23:25:37
73
		}
463cf58 Jakob Wakeling 2023-11-22 23:25:37
74
463cf58 Jakob Wakeling 2023-11-22 23:25:37
75
		repos = append(repos, r)
463cf58 Jakob Wakeling 2023-11-22 23:25:37
76
	}
463cf58 Jakob Wakeling 2023-11-22 23:25:37
77
463cf58 Jakob Wakeling 2023-11-22 23:25:37
78
	if rows.Err() != nil {
463cf58 Jakob Wakeling 2023-11-22 23:25:37
79
		return nil, err
463cf58 Jakob Wakeling 2023-11-22 23:25:37
80
	}
463cf58 Jakob Wakeling 2023-11-22 23:25:37
81
463cf58 Jakob Wakeling 2023-11-22 23:25:37
82
	return repos, nil
463cf58 Jakob Wakeling 2023-11-22 23:25:37
83
}
463cf58 Jakob Wakeling 2023-11-22 23:25:37
84
988a708 Jakob Wakeling 2023-10-23 23:05:22
85
func GetRepo(rid int64) (*Repo, error) {
a0ba6ae Jakob Wakeling 2023-07-21 14:52:36
86
	r := &Repo{}
a0ba6ae Jakob Wakeling 2023-07-21 14:52:36
87
a0ba6ae Jakob Wakeling 2023-07-21 14:52:36
88
	if err := db.QueryRow(
778d467 Jakob Wakeling 2024-01-17 18:29:55
89
		`SELECT id, owner_id, name, description, default_branch, upstream, visibility, is_mirror FROM repos
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
90
		WHERE id = ?`, rid,
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
91
	).Scan(
778d467 Jakob Wakeling 2024-01-17 18:29:55
92
		&r.Id, &r.OwnerId, &r.Name, &r.Description, &r.DefaultBranch, &r.Upstream, &r.Visibility, &r.IsMirror,
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
93
	); err != nil {
a0ba6ae Jakob Wakeling 2023-07-21 14:52:36
94
		if !errors.Is(err, sql.ErrNoRows) {
a0ba6ae Jakob Wakeling 2023-07-21 14:52:36
95
			return nil, err
a0ba6ae Jakob Wakeling 2023-07-21 14:52:36
96
		}
64718e1 Jakob Wakeling 2023-07-24 21:56:07
97
64718e1 Jakob Wakeling 2023-07-24 21:56:07
98
		return nil, nil
a0ba6ae Jakob Wakeling 2023-07-21 14:52:36
99
	} else {
a0ba6ae Jakob Wakeling 2023-07-21 14:52:36
100
		return r, nil
a0ba6ae Jakob Wakeling 2023-07-21 14:52:36
101
	}
a0ba6ae Jakob Wakeling 2023-07-21 14:52:36
102
}
a0ba6ae Jakob Wakeling 2023-07-21 14:52:36
103
a0ba6ae Jakob Wakeling 2023-07-21 14:52:36
104
func GetRepoByName(name string) (*Repo, error) {
e08e053 Jakob Wakeling 2023-07-18 21:31:14
105
	r := &Repo{}
e08e053 Jakob Wakeling 2023-07-18 21:31:14
106
64718e1 Jakob Wakeling 2023-07-24 21:56:07
107
	if err := db.QueryRow(
778d467 Jakob Wakeling 2024-01-17 18:29:55
108
		`SELECT id, owner_id, name, description, default_branch, upstream, visibility, is_mirror FROM repos
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
109
		WHERE name = ?`, name,
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
110
	).Scan(
778d467 Jakob Wakeling 2024-01-17 18:29:55
111
		&r.Id, &r.OwnerId, &r.Name, &r.Description, &r.DefaultBranch, &r.Upstream, &r.Visibility, &r.IsMirror,
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
112
	); err != nil {
64718e1 Jakob Wakeling 2023-07-24 21:56:07
113
		if !errors.Is(err, sql.ErrNoRows) {
64718e1 Jakob Wakeling 2023-07-24 21:56:07
114
			return nil, err
64718e1 Jakob Wakeling 2023-07-24 21:56:07
115
		}
64718e1 Jakob Wakeling 2023-07-24 21:56:07
116
e08e053 Jakob Wakeling 2023-07-18 21:31:14
117
		return nil, nil
e08e053 Jakob Wakeling 2023-07-18 21:31:14
118
	}
e08e053 Jakob Wakeling 2023-07-18 21:31:14
119
e08e053 Jakob Wakeling 2023-07-18 21:31:14
120
	return r, nil
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
121
}
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
122
570144e Jakob Wakeling 2023-12-15 23:28:06
123
func CreateRepo(repo Repo) (int64, error) {
20732d3 Jakob Wakeling 2023-07-26 22:38:15
124
	tx, err := db.Begin()
20732d3 Jakob Wakeling 2023-07-26 22:38:15
125
	if err != nil {
570144e Jakob Wakeling 2023-12-15 23:28:06
126
		return -1, err
20732d3 Jakob Wakeling 2023-07-26 22:38:15
127
	}
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
128
	defer tx.Rollback()
20732d3 Jakob Wakeling 2023-07-26 22:38:15
129
570144e Jakob Wakeling 2023-12-15 23:28:06
130
	res, err := tx.Exec(
778d467 Jakob Wakeling 2024-01-17 18:29:55
131
		`INSERT INTO repos (owner_id, name, name_lower, description, default_branch, upstream, visibility, is_mirror)
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
132
		VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, repo.OwnerId, repo.Name, strings.ToLower(repo.Name), repo.Description,
778d467 Jakob Wakeling 2024-01-17 18:29:55
133
		repo.DefaultBranch, repo.Upstream, repo.Visibility, repo.IsMirror,
570144e Jakob Wakeling 2023-12-15 23:28:06
134
	)
570144e Jakob Wakeling 2023-12-15 23:28:06
135
	if err != nil {
570144e Jakob Wakeling 2023-12-15 23:28:06
136
		return -1, err
20732d3 Jakob Wakeling 2023-07-26 22:38:15
137
	}
20732d3 Jakob Wakeling 2023-07-26 22:38:15
138
570144e Jakob Wakeling 2023-12-15 23:28:06
139
	r, err := git.PlainInit(RepoPath(repo.Name, true), true)
570144e Jakob Wakeling 2023-12-15 23:28:06
140
	if err != nil {
570144e Jakob Wakeling 2023-12-15 23:28:06
141
		return -1, err
20732d3 Jakob Wakeling 2023-07-26 22:38:15
142
	}
20732d3 Jakob Wakeling 2023-07-26 22:38:15
143
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
144
	ref := plumbing.NewSymbolicReference(plumbing.HEAD, plumbing.NewBranchReferenceName(repo.DefaultBranch))
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
145
	if err := r.Storer.SetReference(ref); err != nil {
08dff9a Jakob Wakeling 2023-11-07 21:03:04
146
		os.RemoveAll(RepoPath(repo.Name, true))
570144e Jakob Wakeling 2023-12-15 23:28:06
147
		return -1, err
570144e Jakob Wakeling 2023-12-15 23:28:06
148
	}
570144e Jakob Wakeling 2023-12-15 23:28:06
149
570144e Jakob Wakeling 2023-12-15 23:28:06
150
	if repo.Upstream != "" {
570144e Jakob Wakeling 2023-12-15 23:28:06
151
		if _, err := r.CreateRemote(&gitconfig.RemoteConfig{
570144e Jakob Wakeling 2023-12-15 23:28:06
152
			Name:   "origin",
570144e Jakob Wakeling 2023-12-15 23:28:06
153
			URLs:   []string{repo.Upstream},
570144e Jakob Wakeling 2023-12-15 23:28:06
154
			Mirror: util.If(repo.IsMirror, true, false),
570144e Jakob Wakeling 2023-12-15 23:28:06
155
			Fetch:  []gitconfig.RefSpec{gitconfig.RefSpec("+refs/heads/*:refs/heads/*")},
570144e Jakob Wakeling 2023-12-15 23:28:06
156
		}); err != nil {
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
157
			os.RemoveAll(RepoPath(repo.Name, true))
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
158
			return -1, err
570144e Jakob Wakeling 2023-12-15 23:28:06
159
		}
20732d3 Jakob Wakeling 2023-07-26 22:38:15
160
	}
20732d3 Jakob Wakeling 2023-07-26 22:38:15
161
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
162
	if err := tx.Commit(); err != nil {
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
163
		os.RemoveAll(RepoPath(repo.Name, true))
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
164
		return -1, err
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
165
	}
570144e Jakob Wakeling 2023-12-15 23:28:06
166
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
167
	rid, _ := res.LastInsertId()
570144e Jakob Wakeling 2023-12-15 23:28:06
168
	return rid, nil
20732d3 Jakob Wakeling 2023-07-26 22:38:15
169
}
20732d3 Jakob Wakeling 2023-07-26 22:38:15
170
988a708 Jakob Wakeling 2023-10-23 23:05:22
171
func DelRepo(rid int64) error {
988a708 Jakob Wakeling 2023-10-23 23:05:22
172
	repo, err := GetRepo(rid)
988a708 Jakob Wakeling 2023-10-23 23:05:22
173
	if err != nil {
20732d3 Jakob Wakeling 2023-07-26 22:38:15
174
		return err
20732d3 Jakob Wakeling 2023-07-26 22:38:15
175
	}
20732d3 Jakob Wakeling 2023-07-26 22:38:15
176
08dff9a Jakob Wakeling 2023-11-07 21:03:04
177
	if err := os.RemoveAll(RepoPath(repo.Name, true)); err != nil {
988a708 Jakob Wakeling 2023-10-23 23:05:22
178
		return err
988a708 Jakob Wakeling 2023-10-23 23:05:22
179
	}
988a708 Jakob Wakeling 2023-10-23 23:05:22
180
988a708 Jakob Wakeling 2023-10-23 23:05:22
181
	if _, err := db.Exec("DELETE FROM repos WHERE id = ?", rid); err != nil {
20732d3 Jakob Wakeling 2023-07-26 22:38:15
182
		return err
20732d3 Jakob Wakeling 2023-07-26 22:38:15
183
	}
20732d3 Jakob Wakeling 2023-07-26 22:38:15
184
1547a71 Jakob Wakeling 2023-12-16 15:24:55
185
	Cron.RemoveFor(rid)
1547a71 Jakob Wakeling 2023-12-16 15:24:55
186
	Cron.Update()
1547a71 Jakob Wakeling 2023-12-16 15:24:55
187
20732d3 Jakob Wakeling 2023-07-26 22:38:15
188
	return nil
20732d3 Jakob Wakeling 2023-07-26 22:38:15
189
}
20732d3 Jakob Wakeling 2023-07-26 22:38:15
190
a0ba6ae Jakob Wakeling 2023-07-21 14:52:36
191
func RepoExists(name string) (bool, error) {
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
192
	if err := db.QueryRow(
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
193
		"SELECT name FROM repos WHERE name_lower = ?", strings.ToLower(name),
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
194
	).Scan(&name); err != nil {
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
195
		if !errors.Is(err, sql.ErrNoRows) {
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
196
			return false, err
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
197
		} else {
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
198
			return false, nil
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
199
		}
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
200
	} else {
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
201
		return true, nil
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
202
	}
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
203
}
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
204
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
205
func UpdateRepo(rid int64, repo Repo) error {
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
206
	old, err := GetRepo(rid)
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
207
	if err != nil {
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
208
		return err
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
209
	}
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
210
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
211
	tx, err := db.Begin()
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
212
	if err != nil {
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
213
		return err
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
214
	}
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
215
	defer tx.Rollback()
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
216
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
217
	if _, err := tx.Exec(
778d467 Jakob Wakeling 2024-01-17 18:29:55
218
		`UPDATE repos SET name = ?, name_lower = ?, description = ?, default_branch = ?, upstream = ?, visibility = ?,
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
219
		is_mirror = ? WHERE id = ?`, repo.Name, strings.ToLower(repo.Name), repo.Description, repo.DefaultBranch,
778d467 Jakob Wakeling 2024-01-17 18:29:55
220
		repo.Upstream, repo.Visibility, repo.IsMirror, rid,
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
221
	); err != nil {
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
222
		return err
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
223
	}
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
224
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
225
	if repo.Name != old.Name {
70fd6ec Jakob Wakeling 2023-12-24 11:00:09
226
		if err := os.MkdirAll(filepath.Dir(RepoPath(repo.Name, true)), 0o777); err != nil {
70fd6ec Jakob Wakeling 2023-12-24 11:00:09
227
			return err
70fd6ec Jakob Wakeling 2023-12-24 11:00:09
228
		}
70fd6ec Jakob Wakeling 2023-12-24 11:00:09
229
08dff9a Jakob Wakeling 2023-11-07 21:03:04
230
		if err := os.Rename(RepoPath(old.Name, true), RepoPath(repo.Name, true)); err != nil {
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
231
			return err
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
232
		}
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
233
	}
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
234
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
235
	var r *git.Repository
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
236
	if repo.DefaultBranch != old.DefaultBranch {
b6c2a7b Jakob Wakeling 2024-03-15 21:11:22
237
		// if r == nil {
b6c2a7b Jakob Wakeling 2024-03-15 21:11:22
238
		r, err = git.PlainOpen(RepoPath(repo.Name, true))
b6c2a7b Jakob Wakeling 2024-03-15 21:11:22
239
		if err != nil {
b6c2a7b Jakob Wakeling 2024-03-15 21:11:22
240
			return err
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
241
		}
b6c2a7b Jakob Wakeling 2024-03-15 21:11:22
242
		// }
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
243
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
244
		ref := plumbing.NewSymbolicReference(plumbing.HEAD, plumbing.NewBranchReferenceName(repo.DefaultBranch))
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
245
		if err := r.Storer.SetReference(ref); err != nil {
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
246
			return err
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
247
		}
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
248
	}
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
249
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
250
	/* If the upstream URL has been removed, remove the remote */
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
251
	if repo.Upstream == "" && old.Upstream != "" {
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
252
		if r == nil {
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
253
			r, err = git.PlainOpen(RepoPath(repo.Name, true))
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
254
			if err != nil {
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
255
				return err
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
256
			}
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
257
		}
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
258
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
259
		if err := r.DeleteRemote("origin"); err != nil {
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
260
			return err
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
261
		}
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
262
	}
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
263
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
264
	/* If the upstream URL has been added or changed, update the remote */
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
265
	if repo.Upstream != "" && repo.Upstream != old.Upstream {
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
266
		if r == nil {
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
267
			r, err = git.PlainOpen(RepoPath(repo.Name, true))
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
268
			if err != nil {
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
269
				return err
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
270
			}
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
271
		}
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
272
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
273
		if err := r.DeleteRemote("origin"); err != nil && !errors.Is(err, git.ErrRemoteNotFound) {
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
274
			return err
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
275
		}
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
276
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
277
		if _, err := r.CreateRemote(&gitconfig.RemoteConfig{
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
278
			Name:   "origin",
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
279
			URLs:   []string{repo.Upstream},
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
280
			Mirror: util.If(repo.IsMirror, true, false),
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
281
			Fetch:  []gitconfig.RefSpec{gitconfig.RefSpec("+refs/heads/*:refs/heads/*")},
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
282
		}); err != nil {
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
283
			log.Println("[repo/update]", err.Error())
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
284
		}
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
285
	}
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
286
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
287
	if err := tx.Commit(); err != nil {
08dff9a Jakob Wakeling 2023-11-07 21:03:04
288
		os.Rename(RepoPath(repo.Name, true), RepoPath(old.Name, true))
fc3cc2f Jakob Wakeling 2023-12-16 17:51:02
289
		log.Println("[repo/update]", "error while editing, check repo \""+old.Name+"\"/\""+repo.Name+"\"")
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
290
		return err
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
291
	}
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
292
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
293
	return nil
ceeeb52 Jakob Wakeling 2023-08-19 12:11:35
294
}
988a708 Jakob Wakeling 2023-10-23 23:05:22
295
988a708 Jakob Wakeling 2023-10-23 23:05:22
296
func ChownRepo(rid int64, uid int64) error {
988a708 Jakob Wakeling 2023-10-23 23:05:22
297
	if _, err := db.Exec("UPDATE repos SET owner_id = ? WHERE id = ?", uid, rid); err != nil {
988a708 Jakob Wakeling 2023-10-23 23:05:22
298
		return err
988a708 Jakob Wakeling 2023-10-23 23:05:22
299
	}
988a708 Jakob Wakeling 2023-10-23 23:05:22
300
988a708 Jakob Wakeling 2023-10-23 23:05:22
301
	return nil
988a708 Jakob Wakeling 2023-10-23 23:05:22
302
}
570144e Jakob Wakeling 2023-12-15 23:28:06
303
570144e Jakob Wakeling 2023-12-15 23:28:06
304
func Pull(rid int64) error {
570144e Jakob Wakeling 2023-12-15 23:28:06
305
	repo, err := GetRepo(rid)
570144e Jakob Wakeling 2023-12-15 23:28:06
306
	if err != nil {
570144e Jakob Wakeling 2023-12-15 23:28:06
307
		return err
570144e Jakob Wakeling 2023-12-15 23:28:06
308
	}
570144e Jakob Wakeling 2023-12-15 23:28:06
309
570144e Jakob Wakeling 2023-12-15 23:28:06
310
	r, err := git.PlainOpen(RepoPath(repo.Name, true))
570144e Jakob Wakeling 2023-12-15 23:28:06
311
	if err != nil {
570144e Jakob Wakeling 2023-12-15 23:28:06
312
		return err
570144e Jakob Wakeling 2023-12-15 23:28:06
313
	}
570144e Jakob Wakeling 2023-12-15 23:28:06
314
570144e Jakob Wakeling 2023-12-15 23:28:06
315
	if err := r.Fetch(&git.FetchOptions{}); err != nil {
570144e Jakob Wakeling 2023-12-15 23:28:06
316
		return err
570144e Jakob Wakeling 2023-12-15 23:28:06
317
	}
570144e Jakob Wakeling 2023-12-15 23:28:06
318
570144e Jakob Wakeling 2023-12-15 23:28:06
319
	return nil
570144e Jakob Wakeling 2023-12-15 23:28:06
320
}
778d467 Jakob Wakeling 2024-01-17 18:29:55
321
778d467 Jakob Wakeling 2024-01-17 18:29:55
322
func IsVisible(repo *Repo, auth bool, user *User) bool {
778d467 Jakob Wakeling 2024-01-17 18:29:55
323
	if repo.Visibility == Public || (repo.Visibility == Limited && auth) || (auth && user.Id == repo.OwnerId) {
778d467 Jakob Wakeling 2024-01-17 18:29:55
324
		return true
778d467 Jakob Wakeling 2024-01-17 18:29:55
325
	}
778d467 Jakob Wakeling 2024-01-17 18:29:55
326
778d467 Jakob Wakeling 2024-01-17 18:29:55
327
	return false
778d467 Jakob Wakeling 2024-01-17 18:29:55
328
}
329