-
Notifications
You must be signed in to change notification settings - Fork 375
/
repo.go
539 lines (478 loc) · 13.9 KB
/
repo.go
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// +build !codeanalysis
package gitmodule
import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"time"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/bundle"
"github.com/erda-project/erda/modules/pkg/diceworkspace"
git "github.com/libgit2/git2go/v30"
"github.com/sirupsen/logrus"
)
// Repository represents a Git repository.
type Repository struct {
ID int64 `json:"id"`
ProjectId int64 `json:"project_id"`
ApplicationId int64 `json:"application_id"`
OrgId int64 `json:"org_id"`
Url string `json:"url"`
Path string `json:"path"` //相对路径
Bundle *bundle.Bundle
branchRules []*apistructs.BranchRule
Size int64 `json:"-"`
RootPath string `json:"-"`
RefName string `json:"-"` //需要调用ParseRefAndTreePath才能得到
TreePath string `json:"-"` //需要调用ParseRefAndTreePath才能得到
RefType string `json:"-"`
Commit *Commit `json:"-"`
tree *Tree `json:"-"` //只有RefType是tree才有值
IsLocked bool `json:"-"`
IsExternal bool // 是否是外置仓库
}
const (
REF_TYPE_BRANCH = "branch"
REF_TYPE_TAG = "tag"
REF_TYPE_COMMIT = "commit"
REF_TYPE_TREE = "tree"
)
func (repo *Repository) IsProtectBranch(branch string) bool {
// repo是http请求级别的实例,一个请求中不重复更新规则
if repo.branchRules == nil {
rules, err := repo.Bundle.GetAppBranchRules(uint64(repo.ApplicationId))
if err != nil {
return false
}
repo.branchRules = rules
}
gitReference := diceworkspace.GetValidBranchByGitReference(branch, repo.branchRules)
return gitReference.IsProtect
}
func (repo *Repository) IsProtectBranchWithRules(branch string, rules []*apistructs.BranchRule) bool {
gitReference := diceworkspace.GetValidBranchByGitReference(branch, rules)
return gitReference.IsProtect
}
func (repo *Repository) ParseRefAndTreePath(path string) error {
hasRefMatched := false
var refName string
path = strings.Trim(path, "/")
parts := strings.Split(path, "/")
//探测tag branch
for i := range parts {
refName = strings.Join(parts[:i+1], "/")
if repo.IsBranchExist(refName) ||
repo.IsTagExist(refName) {
if i < len(parts)-1 {
repo.TreePath = strings.Join(parts[i+1:], "/")
}
hasRefMatched = true
break
}
}
if !hasRefMatched && len(parts[0]) == 40 {
refName = parts[0]
repo.TreePath = strings.Join(parts[1:], "/")
}
var err error
if repo.IsBranchExist(refName) {
repo.Commit, err = repo.GetBranchCommit(refName)
repo.RefName = BRANCH_PREFIX + refName
repo.RefType = REF_TYPE_BRANCH
if err != nil {
return err
}
} else if repo.IsTagExist(refName) {
repo.Commit, err = repo.GetTagCommit(refName)
repo.RefName = TAG_PREFIX + refName
repo.RefType = REF_TYPE_TAG
if err != nil {
return err
}
} else if len(refName) >= 6 {
//commit id 和tree id都尝试解析
repo.Commit, err = repo.GetCommit(refName)
if err == nil {
repo.RefName = "sha/" + refName
repo.RefType = REF_TYPE_COMMIT
return nil
}
//尝试 treeId
tree, err := repo.GetTree(refName)
if err != nil {
return fmt.Errorf("无效分支 : %s ", refName)
}
repo.tree = tree
repo.RefName = "tree_sha/" + refName
repo.RefType = REF_TYPE_TREE
return nil
} else {
return fmt.Errorf("branch or tag not exist: %s", refName)
}
return nil
}
func (repo *Repository) GetParsedTreeEntry() (*TreeEntry, error) {
if repo.RefType == REF_TYPE_TREE {
return repo.tree.GetTreeEntryByPath(repo.TreePath)
} else {
return repo.GetTreeEntryByPath(repo.Commit.ID, repo.TreePath)
}
}
func (repo *Repository) GetCommitByAny(ref string) (*Commit, error) {
var err error
var commit *Commit
commit, err = repo.GetBranchCommit(ref)
if err == nil {
return commit, nil
}
commit, err = repo.GetTagCommit(ref)
if err == nil {
return commit, nil
}
commit, err = repo.GetCommit(ref)
if err == nil {
return commit, nil
}
return nil, fmt.Errorf("unknown ref %s", ref)
}
func (repo *Repository) GetRepoStats(path string) (map[string]interface{}, error) {
if path == "" {
path = "/"
}
tags, _ := repo.GetTags()
branches, _ := repo.GetBranches()
var commit *Commit
var err error
defaultBranch, _ := repo.GetDefaultBranch()
if path != "/" {
err := repo.ParseRefAndTreePath(path)
if err != nil {
return nil, err
}
commit = repo.Commit
} else {
if defaultBranch != "" {
commit, err = repo.GetBranchCommit(defaultBranch)
if err != nil {
return nil, err
}
repo.RefName = BRANCH_PREFIX + defaultBranch
} else {
//没有分支判断为空仓库
result := map[string]interface{}{
"commitsCount": 0,
"contributorCount": 0,
"tags": tags,
"branches": branches,
"defaultBranch": defaultBranch,
"empty": true,
"commitId": "",
}
return result, nil
}
}
rawrepo, err := repo.GetRawRepo()
if err != nil {
return nil, err
}
key := repo.FullName() + "-" + commit.ID
refName := repo.RefName
if refName == "" {
refName = "sha/" + commit.ID
}
var result map[string]interface{}
err = Setting.RepoStatsCache.Get(key, &result)
if err == nil {
//以下信息不使用缓存
result["branches"] = branches
result["tags"] = tags
result["defaultBranch"] = defaultBranch
result["commitId"] = commit.ID
result["refName"] = refName
return result, nil
}
walker, err := rawrepo.Walk()
if err != nil {
return nil, err
}
oid, _ := git.NewOid(commit.ID)
err = walker.Push(oid)
if err != nil {
return nil, err
}
var repoCommitsCount int
contributors := map[string]map[string]interface{}{}
if defaultBranch != "" {
walker.Iterate(func(commit *git.Commit) bool {
repoCommitsCount++
author := commit.Author()
if contributors[author.Email] == nil {
stats := map[string]interface{}{
"name": author.Name,
"commitsCount": 1,
}
contributors[author.Email] = stats
} else {
stats := contributors[author.Email]
count := stats["commitsCount"].(int)
count++
stats["commitsCount"] = count
}
//if commit.ParentCount() == 0 {
// repoCreatedAt = commit.Author().When
// return false
//}
return true
})
}
result = map[string]interface{}{
"commitsCount": repoCommitsCount,
"contributorCount": len(contributors),
"tags": tags,
"branches": branches,
"defaultBranch": defaultBranch,
"empty": false,
"refName": refName,
"commitId": commit.ID,
}
cacheStats := map[string]interface{}{
"commitsCount": repoCommitsCount,
"contributorCount": len(contributors),
"empty": false,
}
Setting.RepoStatsCache.Set(key, cacheStats)
return result, nil
}
func (repo *Repository) Root() string {
return repo.RootPath
}
func (repo *Repository) DiskPath() string {
return path.Join(repo.Root(), repo.Path)
}
func (repo *Repository) FullName() string {
return repo.Path
}
const _PRETTY_LOG_FORMAT = `--pretty=format:%H`
func (repo *Repository) parsePrettyFormatLogToList(logs []byte) ([]*Commit, error) {
commits := []*Commit{}
if len(logs) == 0 {
return commits, nil
}
parts := bytes.Split(logs, []byte{'\n'})
for _, commitId := range parts {
commit, err := repo.GetCommit(string(commitId))
if err != nil {
return nil, err
}
commits = append(commits, commit)
}
return commits, nil
}
func (repo *Repository) GetRawRepo() (*git.Repository, error) {
return git.OpenRepository(repo.DiskPath())
}
type NetworkOptions struct {
URL string
Timeout time.Duration
}
// InitRepository initializes a new Git repository.
func InitRepository(repoPath string, bare bool) error {
if _, err := os.Stat(repoPath); os.IsNotExist(err) {
logrus.Infof("init repo %s", repoPath)
os.MkdirAll(repoPath, 0755)
_, err := git.InitRepository(repoPath, true)
logrus.Printf("git repository %s is created!", repoPath)
if err != nil {
return err
}
}
return nil
}
// InitExternalRepository 初始化外部托管仓库
func InitExternalRepository(repoPath string, url string, username string, password string) error {
if _, err := os.Stat(repoPath); os.IsNotExist(err) {
logrus.Infof("init repo %s", repoPath)
os.MkdirAll(repoPath, 0755)
urlWithBasicAuth, err := GetUrlWithBasicAuth(url, username, password)
if err != nil {
return err
}
cmd := exec.Command("git", "clone", "--mirror", urlWithBasicAuth, repoPath)
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("err:%s output:%s", err, RemoveAuthInfo(string(output), urlWithBasicAuth))
}
}
return nil
}
// UpdateExternalRepository 更新远程仓库配置
func UpdateExternalRepository(repoPath string, url string, username string, password string) error {
urlWithBasicAuth, err := GetUrlWithBasicAuth(url, username, password)
if err != nil {
return err
}
cmd := exec.Command("git", "remote", "set-url", "origin", urlWithBasicAuth)
cmd.Dir = repoPath
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("err:%s output:%s", err, RemoveAuthInfo(string(output), urlWithBasicAuth))
}
return nil
}
// SyncExternalRepository 同步外部仓库
func SyncExternalRepository(repoPath string) error {
//mirrorRepoUrl := GetMirrorRepoUrl(repoPath)
cmd := exec.Command("git", "remote", "update", "--prune")
cmd.Dir = repoPath
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("err:%s output:%s", err, string(output))
}
return nil
}
// PushExternalRepository 推送到外部仓库
func PushExternalRepository(repoPath string) error {
mirrorRepoUrl := GetMirrorRepoUrl(repoPath)
cmd := exec.Command("git", "push", "origin")
cmd.Dir = repoPath
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("err:%s output:%s", err, RemoveAuthInfo(string(output), mirrorRepoUrl))
}
return nil
}
func OpenRepository(rootPath string, path string) (*Repository, error) {
repository := &Repository{
Path: path,
RootPath: rootPath,
}
repoPath, err := filepath.Abs(repository.DiskPath())
if err != nil {
return nil, err
} else if !isDir(repoPath) {
return nil, errors.New("repo path not exist")
}
return repository, nil
}
func OpenRepositoryWithInit(rootPath string, repoPath string) (*Repository, error) {
err := InitRepository(path.Join(rootPath, repoPath), true)
if err != nil {
return nil, err
}
return OpenRepository(rootPath, repoPath)
}
type CloneRepoOptions struct {
Mirror bool
Bare bool
Quiet bool
Branch string
Timeout time.Duration
}
type CheckoutOptions struct {
Branch string
OldBranch string
Timeout time.Duration
}
// Checkout checkouts a branch
func Checkout(repoPath string, opts CheckoutOptions) error {
cmd := NewCommand("checkout")
if len(opts.OldBranch) > 0 {
cmd.AddArguments("-b")
}
cmd.AddArguments(opts.Branch)
if len(opts.OldBranch) > 0 {
cmd.AddArguments(opts.OldBranch)
}
if opts.Timeout <= 0 {
opts.Timeout = -1
}
_, err := cmd.RunInDirTimeout(opts.Timeout, repoPath)
return err
}
// MoveFile moves a file to another file or directory.
func MoveFile(repoPath, oldTreeName, newTreeName string) error {
_, err := NewCommand("mv").AddArguments(oldTreeName, newTreeName).RunInDir(repoPath)
return err
}
// CountObject represents disk usage report of Git repository.
type CountObject struct {
Count int64
Size int64
InPack int64
Packs int64
SizePack int64
PrunePackable int64
Garbage int64
SizeGarbage int64
}
const (
_STAT_COUNT = "count: "
_STAT_SIZE = "size: "
_STAT_IN_PACK = "in-pack: "
_STAT_PACKS = "packs: "
_STAT_SIZE_PACK = "size-pack: "
_STAT_PRUNE_PACKABLE = "prune-packable: "
_STAT_GARBAGE = "garbage: "
_STAT_SIZE_GARBAGE = "size-garbage: "
)
// CalcRepoSize returns disk usage report of repository in given path.
func (repo *Repository) CalcRepoSize() (int64, error) {
cmd := NewCommand("count-objects", "-v")
repoPath := repo.DiskPath()
stdout, err := cmd.RunInDir(repoPath)
if err != nil {
return 0, err
}
countObject := new(CountObject)
for _, line := range strings.Split(stdout, "\n") {
switch {
case strings.HasPrefix(line, _STAT_COUNT):
countObject.Count = StrTo(line[7:]).MustInt64()
case strings.HasPrefix(line, _STAT_SIZE):
countObject.Size = StrTo(line[6:]).MustInt64() * 1024
case strings.HasPrefix(line, _STAT_IN_PACK):
countObject.InPack = StrTo(line[9:]).MustInt64()
case strings.HasPrefix(line, _STAT_PACKS):
countObject.Packs = StrTo(line[7:]).MustInt64()
case strings.HasPrefix(line, _STAT_SIZE_PACK):
countObject.SizePack = StrTo(line[11:]).MustInt64() * 1024
case strings.HasPrefix(line, _STAT_PRUNE_PACKABLE):
countObject.PrunePackable = StrTo(line[16:]).MustInt64()
case strings.HasPrefix(line, _STAT_GARBAGE):
countObject.Garbage = StrTo(line[9:]).MustInt64()
case strings.HasPrefix(line, _STAT_SIZE_GARBAGE):
countObject.SizeGarbage = StrTo(line[14:]).MustInt64() * 1024
}
}
size := countObject.Size + countObject.SizeGarbage + countObject.SizePack
return size, nil
}
// InfoPacksPath for Repository
func (r *Repository) InfoPacksPath() string {
return path.Join(r.DiskPath(), "info", "packs")
}
// LooseObjectPath for Repository
func (r *Repository) LooseObjectPath(prefix string, suffix string) string {
return path.Join(r.DiskPath(), "objects", prefix, suffix)
}
// PackIdxPath for Repository
func (r *Repository) PackIdxPath(pack string) string {
return path.Join(r.DiskPath(), "pack", pack)
}