/
repo_commit.go
519 lines (450 loc) · 13.8 KB
/
repo_commit.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
// 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"
"container/list"
"errors"
"fmt"
"strconv"
"strings"
"time"
git "github.com/libgit2/git2go/v30"
"github.com/erda-project/erda/modules/gittar/pkg/gitmodule/tool"
)
const REMOTE_PREFIX = "refs/remotes/"
// getRefCommitID returns the last commit ID string of given reference (branch or tag).
func (repo *Repository) getRefCommitID(name string) (string, error) {
rawrepo, err := repo.GetRawRepo()
if err != nil {
return "", err
}
branch, err := rawrepo.LookupBranch(name, git.BranchLocal)
if err != nil {
return "", err
}
oid := branch.Target()
return oid.String(), nil
}
// GetBranchCommitID returns last commit ID string of given branch.
func (repo *Repository) GetBranchCommitID(name string) (string, error) {
return repo.getRefCommitID(name)
}
func (repo *Repository) GetCommit(commitId string) (*Commit, error) {
var result *Commit
err := Setting.CommitCache.Get(commitId, &result)
if err == nil {
log("Hit cache: %s", commitId)
return result, nil
}
rawrepo, err := repo.GetRawRepo()
if err != nil {
return nil, err
}
oid, err := git.NewOid(commitId)
if err != nil {
return nil, err
}
rawCommit, err := rawrepo.LookupCommit(oid)
if err != nil {
return nil, err
}
commit := NewCommitFromLibgit2(repo, rawCommit)
Setting.CommitCache.Set(commitId, commit)
return commit, nil
}
// GetBranchCommit returns the last commit of given branch.
func (repo *Repository) GetBranchCommit(name string) (*Commit, error) {
commitID, err := repo.GetBranchCommitID(name)
if err != nil {
return nil, errors.New("branch [" + name + "] not exist ")
}
return repo.GetCommit(commitID)
}
// GetDefaultBranch 获取默认分支
func (repo *Repository) GetDefaultBranch() (string, error) {
rawrepo, err := repo.GetRawRepo()
if err != nil {
return "", err
}
reference, err := rawrepo.Head()
if err != nil {
if e1 := repo.AutoSetDefaultBranch(); e1 != nil {
return "", err
}
return repo.GetDefaultBranch()
}
branch := strings.TrimPrefix(reference.Name(), BRANCH_PREFIX)
if branch == "" {
return "", errors.New("reference name is empty")
}
return branch, nil
}
func (repo *Repository) GetMergedBranches(baseBranch string) (map[string]int, error) {
stdout, err := NewCommand("branch", "--merged", baseBranch).RunInDirBytes(repo.DiskPath())
if err != nil {
return nil, err
}
result := map[string]int{}
for _, branch := range strings.Split(string(stdout), "\n") {
result[strings.TrimSpace(branch)] = 1
}
result[baseBranch] = 1
return result, nil
}
// AutoSetDefaultBranch 自动选择一个分支作为默认分支,优先选择 master / develop 分支
func (repo *Repository) AutoSetDefaultBranch() error {
branches, err := repo.GetBranches()
if err != nil {
return err
}
if len(branches) == 0 {
return errors.New("no branch")
}
defaultBranch := branches[0]
if tool.IsKeyInArray(branches, "develop") {
defaultBranch = "develop"
}
if tool.IsKeyInArray(branches, "master") {
defaultBranch = "master"
}
return repo.SetDefaultBranch(defaultBranch)
}
// SetDefaultBranch 设置默认分支
func (repo *Repository) SetDefaultBranch(branch string) error {
_, err := repo.GetBranchCommit(branch)
if err != nil {
return fmt.Errorf("branch not found: %s", branch)
}
rawrepo, err := repo.GetRawRepo()
if err != nil {
return err
}
return rawrepo.SetHead(BRANCH_PREFIX + branch)
}
// GetTagCommit returns the commit of given tag.
func (repo *Repository) GetTagCommit(name string) (*Commit, error) {
rawrepo, err := repo.GetRawRepo()
if err != nil {
return nil, err
}
ref, err := rawrepo.References.Lookup(TAG_PREFIX + name)
if err != nil {
return nil, err
}
object, err := rawrepo.Lookup(ref.Target())
if err != nil {
return nil, err
}
rawCommit, err := object.AsCommit()
if err != nil {
//不是轻量级tag 尝试 附注tag
rawTag, err := object.AsTag()
if err != nil {
return nil, err
} else {
commitId := rawTag.TargetId().String()
return repo.GetCommit(commitId)
}
} else {
//轻量级tag, tag就是commit本身别名
commit := NewCommitFromLibgit2(repo, rawCommit)
return commit, nil
}
}
func (repo *Repository) getCommitByPathWithID(id string, relpath string) (*Commit, error) {
cacheKey := id + "-" + relpath
var result *Commit
err := Setting.PathCommitCache.Get(cacheKey, &result)
if err == nil {
return result, nil
}
// File Name starts with ':' must be escaped.
if len(relpath) > 0 && relpath[0] == ':' {
relpath = `\` + relpath
}
logCmd := NewCommand("log", "-1", _PRETTY_LOG_FORMAT, id, "--", relpath)
//根路径不需要传path参数
if relpath == "" {
logCmd = NewCommand("log", "-1", _PRETTY_LOG_FORMAT, id)
}
id, err = logCmd.RunInDir(repo.DiskPath())
if err != nil {
return nil, err
}
commit, err := repo.GetCommit(id)
if err == nil {
Setting.PathCommitCache.Set(cacheKey, commit)
}
return commit, err
}
// GetCommitByPath returns the last commit of relative path.
func (repo *Repository) GetCommitByPath(relpath string) (*Commit, error) {
stdout, err := NewCommand("log", "-1", _PRETTY_LOG_FORMAT, "--", relpath).RunInDirBytes(repo.DiskPath())
if err != nil {
return nil, err
}
commits, err := repo.parsePrettyFormatLogToList(stdout)
if err != nil {
return nil, err
}
return commits[0], nil
}
func (repo *Repository) CommitsByRangeSize(id string, page, size int) ([]*Commit, error) {
/*
commit, err := repo.GetCommitByAny(id)
if err != nil {
return nil, err
}
rawrepo, err := repo.GetRawRepo()
if err != nil {
return nil, err
}
walker, err := rawrepo.Walk()
if err != nil {
return nil, err
}
oid, _ := git.NewOid(commit.ID)
walker.Push(oid)
walker.Sorting(git.SortTime)
commits := []*Commit{}
startPos := page * size
stopPos := startPos + size
hasIterated := 0
walker.Iterate(func(rawCommit *git.Commit) bool {
if hasIterated < startPos {
hasIterated += 1
return true
} else {
hasIterated += 1
commits = append(commits, NewCommitFromLibgit2(repo, rawCommit))
return hasIterated < stopPos
}
})
return commits, nil
*/
//todo 使用git命令行性能更好
stdout, err := NewCommand("log", id, "--skip="+strconv.Itoa((page-1)*size),
"--max-count="+strconv.Itoa(size), _PRETTY_LOG_FORMAT).RunInDirBytes(repo.DiskPath())
if err != nil {
return nil, err
}
return repo.parsePrettyFormatLogToList(stdout)
}
var DefaultCommitsPageSize = 30
func (repo *Repository) CommitsByRange(revision string, page int) ([]*Commit, error) {
return repo.CommitsByRangeSize(revision, page, DefaultCommitsPageSize)
}
func (repo *Repository) searchCommits(id string, keyword string) ([]*Commit, error) {
stdout, err := NewCommand("log", id, "-100", "-i", "--grep="+keyword, _PRETTY_LOG_FORMAT).RunInDirBytes(repo.DiskPath())
if err != nil {
return nil, err
}
return repo.parsePrettyFormatLogToList(stdout)
}
func (repo *Repository) getFilesChanged(id1 string, id2 string) ([]string, error) {
stdout, err := NewCommand("diff", "--Name-only", id1, id2).RunInDirBytes(repo.DiskPath())
if err != nil {
return nil, err
}
return strings.Split(string(stdout), "\n"), nil
}
func (repo *Repository) FileCommitsCount(revision, file string) (int64, error) {
return commitsCount(repo.DiskPath(), revision, file)
}
func (repo *Repository) CommitsByFileAndRangeSize(revision, file string, searchText string, page, size int) ([]*Commit, error) {
args := []string{"log", revision, "--skip=" + strconv.Itoa((page-1)*size),
"--max-count=" + strconv.Itoa(size), _PRETTY_LOG_FORMAT}
if searchText != "" {
args = append(args, "--grep="+searchText)
}
if file != "" {
args = append(args, "--", file)
}
stdout, err := NewCommand(args...).RunInDirBytes(repo.DiskPath())
if err != nil {
return nil, err
}
return repo.parsePrettyFormatLogToList(stdout)
}
func (repo *Repository) FilesCountBetween(startCommitID, endCommitID string) (int, error) {
stdout, err := NewCommand("diff", "--Name-only", startCommitID+"..."+endCommitID).RunInDir(repo.DiskPath())
if err != nil {
return 0, err
}
return len(strings.Split(stdout, "\n")) - 1, nil
}
// CommitsBetween returns a list that contains commits between [last, before).
func (repo *Repository) CommitsBetweenLimit(last *Commit, before *Commit, skip int, limit int) ([]*Commit, error) {
if before == nil {
stdout, err := NewCommand("rev-list", last.ID, "--all", "--skip="+strconv.Itoa(skip), "--max-count="+strconv.Itoa(limit)).RunInDirBytes(repo.DiskPath())
if err != nil {
return nil, err
}
return repo.parsePrettyFormatLogToList(bytes.TrimSpace(stdout))
}
stdout, err := NewCommand("rev-list", before.ID+"..."+last.ID, "--skip="+strconv.Itoa(skip), "--max-count="+strconv.Itoa(limit)).RunInDirBytes(repo.DiskPath())
if err != nil {
return nil, err
}
return repo.parsePrettyFormatLogToList(bytes.TrimSpace(stdout))
}
// CommitsBetween returns a list that contains commits between [last, before).
func (repo *Repository) CommitsCountBetween(last *Commit, before *Commit) (int, error) {
if before == nil {
stdout, err := NewCommand("rev-list", "--count", last.ID, "--all").RunInDirBytes(repo.DiskPath())
if err != nil {
return 0, err
}
return strconv.Atoi(strings.TrimSpace(string(stdout)))
}
stdout, err := NewCommand("rev-list", "--count", before.ID+"..."+last.ID).RunInDirBytes(repo.DiskPath())
if err != nil {
return 0, err
}
return strconv.Atoi(strings.TrimSpace(string(stdout)))
}
// CommitsBetween returns a list that contains commits between [last, before).
func (repo *Repository) CommitsBetween(last *Commit, before *Commit) ([]*Commit, error) {
//rawrepo, err := repo.GetRawRepo()
//if err != nil {
// return nil, err
//}
//walker, err := rawrepo.Walk()
//
//if err != nil {
// return nil, err
//}
////lastCommit, _ := git.NewOid(last.ID)
////walker.Push(lastCommit)
//walker.PushRange(last.ID+".."+before.ID)
////beforeCommit, _ := git.NewOid(before.ID)
//
//commits := []*Commit{}
//statusCountToAdd := 20
//err = walker.Iterate(func(commit *git.Commit) bool {
// println(commit.Id().String())
// if commit.Id().String() == before.ID {
// return false
// }
// commits = append(commits, NewCommitFromLibgit2(repo, commit))
// statusCountToAdd -= 1
// return true
//})
//if err != nil {
// return nil, err
//}
//return commits, nil
stdout, err := NewCommand("rev-list", before.ID+"..."+last.ID).RunInDirBytes(repo.DiskPath())
if err != nil {
return nil, err
}
return repo.parsePrettyFormatLogToList(bytes.TrimSpace(stdout))
}
func (repo *Repository) CommitsBetweenIDs(last, before string) ([]*Commit, error) {
lastCommit, err := repo.GetCommit(last)
if err != nil {
return nil, err
}
beforeCommit, err := repo.GetCommit(before)
if err != nil {
return nil, err
}
return repo.CommitsBetween(lastCommit, beforeCommit)
}
// The limit is depth, not total number of returned commits.
func (repo *Repository) commitsBefore(l *list.List, parent *list.Element, id string, current, limit int) error {
// Reach the limit
if limit > 0 && current > limit {
return nil
}
commit, err := repo.GetCommit(id)
if err != nil {
return fmt.Errorf("GetCommit: %v", err)
}
var e *list.Element
if parent == nil {
e = l.PushBack(commit)
} else {
var in = parent
for {
if in == nil {
break
} else if in.Value.(*Commit).ID == (commit.ID) {
return nil
} else if in.Next() == nil {
break
}
if in.Value.(*Commit).Committer.When.Equal(commit.Committer.When) {
break
}
if in.Value.(*Commit).Committer.When.After(commit.Committer.When) &&
in.Next().Value.(*Commit).Committer.When.Before(commit.Committer.When) {
break
}
in = in.Next()
}
e = l.InsertAfter(commit, in)
}
pr := parent
if commit.ParentCount() > 1 {
pr = e
}
for i := 0; i < commit.ParentCount(); i++ {
id, err := commit.ParentID(i)
if err != nil {
return err
}
err = repo.commitsBefore(l, pr, id, current+1, limit)
if err != nil {
return err
}
}
return nil
}
func (repo *Repository) getCommitsBefore(id string) (*list.List, error) {
l := list.New()
return l, repo.commitsBefore(l, nil, id, 1, 0)
}
func (repo *Repository) getCommitsBeforeLimit(id string, num int) ([]*Commit, error) {
l := []*Commit{}
return l, nil
//return l, repo.commitsBefore(l, nil, id, 1, num)
}
// CommitsAfterDate returns a list of commits which committed after given date.
// The format of date should be in RFC3339.
func (repo *Repository) CommitsAfterDate(date string) ([]*Commit, error) {
stdout, err := NewCommand("log", _PRETTY_LOG_FORMAT, "--since="+date).RunInDirBytes(repo.DiskPath())
if err != nil {
return nil, err
}
return repo.parsePrettyFormatLogToList(stdout)
}
// CommitsCount returns number of total commits of until given revision.
func CommitsCount(repoPath, revision string) (int64, error) {
return commitsCount(repoPath, revision, "")
}
// GetLatestCommitDate returns the date of latest commit of repository.
// If branch is empty, it returns the latest commit across all branches.
func GetLatestCommitDate(repoPath, branch string) (time.Time, error) {
cmd := NewCommand("for-each-ref", "--count=1", "--sort=-committerdate", "--format=%(committerdate:iso8601)")
if len(branch) > 0 {
cmd.AddArguments("refs/heads/" + branch)
}
stdout, err := cmd.RunInDir(repoPath)
if err != nil {
return time.Time{}, err
}
return time.Parse("2006-01-02 15:04:05 -0700", strings.TrimSpace(stdout))
}