Skip to content

Commit db0d7a1

Browse files
committed
clean git support for ver < 2.0
1 parent a005163 commit db0d7a1

File tree

10 files changed

+130
-173
lines changed

10 files changed

+130
-173
lines changed

modules/git/commit.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -206,26 +206,17 @@ func (c *Commit) HasPreviousCommit(commitHash SHA1) (bool, error) {
206206
return false, nil
207207
}
208208

209-
if err := CheckGitVersionAtLeast("1.8"); err == nil {
210-
_, _, err := NewCommand(c.repo.Ctx, "merge-base", "--is-ancestor", that, this).RunStdString(&RunOpts{Dir: c.repo.Path})
211-
if err == nil {
212-
return true, nil
209+
_, _, err := NewCommand(c.repo.Ctx, "merge-base", "--is-ancestor", that, this).RunStdString(&RunOpts{Dir: c.repo.Path})
210+
if err == nil {
211+
return true, nil
212+
}
213+
var exitError *exec.ExitError
214+
if errors.As(err, &exitError) {
215+
if exitError.ProcessState.ExitCode() == 1 && len(exitError.Stderr) == 0 {
216+
return false, nil
213217
}
214-
var exitError *exec.ExitError
215-
if errors.As(err, &exitError) {
216-
if exitError.ProcessState.ExitCode() == 1 && len(exitError.Stderr) == 0 {
217-
return false, nil
218-
}
219-
}
220-
return false, err
221-
}
222-
223-
result, _, err := NewCommand(c.repo.Ctx, "rev-list", "--ancestry-path", "-n1", that+".."+this, "--").RunStdString(&RunOpts{Dir: c.repo.Path})
224-
if err != nil {
225-
return false, err
226218
}
227-
228-
return len(strings.TrimSpace(result)) > 0, nil
219+
return false, err
229220
}
230221

231222
// CommitsBeforeLimit returns num commits before current revision

modules/git/repo_attribute.go

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ type CheckAttributeOpts struct {
3030
func (repo *Repository) CheckAttribute(opts CheckAttributeOpts) (map[string]map[string]string, error) {
3131
env := []string{}
3232

33-
if len(opts.IndexFile) > 0 && CheckGitVersionAtLeast("1.7.8") == nil {
33+
if len(opts.IndexFile) > 0 {
3434
env = append(env, "GIT_INDEX_FILE="+opts.IndexFile)
3535
}
36-
if len(opts.WorkTree) > 0 && CheckGitVersionAtLeast("1.7.8") == nil {
36+
if len(opts.WorkTree) > 0 {
3737
env = append(env, "GIT_WORK_TREE="+opts.WorkTree)
3838
}
3939

@@ -56,8 +56,7 @@ func (repo *Repository) CheckAttribute(opts CheckAttributeOpts) (map[string]map[
5656
}
5757
}
5858

59-
// git check-attr --cached first appears in git 1.7.8
60-
if opts.CachedOnly && CheckGitVersionAtLeast("1.7.8") == nil {
59+
if opts.CachedOnly {
6160
cmdArgs = append(cmdArgs, "--cached")
6261
}
6362

@@ -125,12 +124,12 @@ type CheckAttributeReader struct {
125124
func (c *CheckAttributeReader) Init(ctx context.Context) error {
126125
cmdArgs := []string{"check-attr", "--stdin", "-z"}
127126

128-
if len(c.IndexFile) > 0 && CheckGitVersionAtLeast("1.7.8") == nil {
127+
if len(c.IndexFile) > 0 {
129128
cmdArgs = append(cmdArgs, "--cached")
130129
c.env = append(c.env, "GIT_INDEX_FILE="+c.IndexFile)
131130
}
132131

133-
if len(c.WorkTree) > 0 && CheckGitVersionAtLeast("1.7.8") == nil {
132+
if len(c.WorkTree) > 0 {
134133
c.env = append(c.env, "GIT_WORK_TREE="+c.WorkTree)
135134
}
136135

@@ -160,17 +159,10 @@ func (c *CheckAttributeReader) Init(ctx context.Context) error {
160159
return err
161160
}
162161

163-
if CheckGitVersionAtLeast("1.8.5") == nil {
164-
lw := new(nulSeparatedAttributeWriter)
165-
lw.attributes = make(chan attributeTriple, 5)
166-
lw.closed = make(chan struct{})
167-
c.stdOut = lw
168-
} else {
169-
lw := new(lineSeparatedAttributeWriter)
170-
lw.attributes = make(chan attributeTriple, 5)
171-
lw.closed = make(chan struct{})
172-
c.stdOut = lw
173-
}
162+
lw := new(nulSeparatedAttributeWriter)
163+
lw.attributes = make(chan attributeTriple, 5)
164+
lw.closed = make(chan struct{})
165+
c.stdOut = lw
174166
return nil
175167
}
176168

modules/git/repo_compare.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,7 @@ func (repo *Repository) GetDiff(base, head string, w io.Writer) error {
255255

256256
// GetDiffBinary generates and returns patch data between given revisions, including binary diffs.
257257
func (repo *Repository) GetDiffBinary(base, head string, w io.Writer) error {
258-
if CheckGitVersionAtLeast("1.7.7") == nil {
259-
return NewCommand(repo.Ctx, "diff", "-p", "--binary", "--histogram", base, head).Run(&RunOpts{
260-
Dir: repo.Path,
261-
Stdout: w,
262-
})
263-
}
264-
return NewCommand(repo.Ctx, "diff", "-p", "--binary", "--patience", base, head).Run(&RunOpts{
258+
return NewCommand(repo.Ctx, "diff", "-p", "--binary", "--histogram", base, head).Run(&RunOpts{
265259
Dir: repo.Path,
266260
Stdout: w,
267261
})

modules/git/repo_language_stats_gogit.go

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,28 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
4545

4646
var checker *CheckAttributeReader
4747

48-
if CheckGitVersionAtLeast("1.7.8") == nil {
49-
indexFilename, workTree, deleteTemporaryFile, err := repo.ReadTreeToTemporaryIndex(commitID)
50-
if err == nil {
51-
defer deleteTemporaryFile()
52-
checker = &CheckAttributeReader{
53-
Attributes: []string{"linguist-vendored", "linguist-generated", "linguist-language", "gitlab-language"},
54-
Repo: repo,
55-
IndexFile: indexFilename,
56-
WorkTree: workTree,
57-
}
58-
ctx, cancel := context.WithCancel(DefaultContext)
59-
if err := checker.Init(ctx); err != nil {
60-
log.Error("Unable to open checker for %s. Error: %v", commitID, err)
61-
} else {
62-
go func() {
63-
err = checker.Run()
64-
if err != nil {
65-
log.Error("Unable to open checker for %s. Error: %v", commitID, err)
66-
cancel()
67-
}
68-
}()
69-
}
70-
defer cancel()
48+
indexFilename, workTree, deleteTemporaryFile, err := repo.ReadTreeToTemporaryIndex(commitID)
49+
if err == nil {
50+
defer deleteTemporaryFile()
51+
checker = &CheckAttributeReader{
52+
Attributes: []string{"linguist-vendored", "linguist-generated", "linguist-language", "gitlab-language"},
53+
Repo: repo,
54+
IndexFile: indexFilename,
55+
WorkTree: workTree,
56+
}
57+
ctx, cancel := context.WithCancel(DefaultContext)
58+
if err := checker.Init(ctx); err != nil {
59+
log.Error("Unable to open checker for %s. Error: %v", commitID, err)
60+
} else {
61+
go func() {
62+
err = checker.Run()
63+
if err != nil {
64+
log.Error("Unable to open checker for %s. Error: %v", commitID, err)
65+
cancel()
66+
}
67+
}()
7168
}
69+
defer cancel()
7270
}
7371

7472
sizes := make(map[string]int64)

modules/git/repo_language_stats_nogogit.go

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -65,33 +65,31 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
6565

6666
var checker *CheckAttributeReader
6767

68-
if CheckGitVersionAtLeast("1.7.8") == nil {
69-
indexFilename, worktree, deleteTemporaryFile, err := repo.ReadTreeToTemporaryIndex(commitID)
70-
if err == nil {
71-
defer deleteTemporaryFile()
72-
checker = &CheckAttributeReader{
73-
Attributes: []string{"linguist-vendored", "linguist-generated", "linguist-language", "gitlab-language"},
74-
Repo: repo,
75-
IndexFile: indexFilename,
76-
WorkTree: worktree,
77-
}
78-
ctx, cancel := context.WithCancel(repo.Ctx)
79-
if err := checker.Init(ctx); err != nil {
80-
log.Error("Unable to open checker for %s. Error: %v", commitID, err)
81-
} else {
82-
go func() {
83-
err = checker.Run()
84-
if err != nil {
85-
log.Error("Unable to open checker for %s. Error: %v", commitID, err)
86-
cancel()
87-
}
88-
}()
89-
}
90-
defer func() {
91-
_ = checker.Close()
92-
cancel()
68+
indexFilename, worktree, deleteTemporaryFile, err := repo.ReadTreeToTemporaryIndex(commitID)
69+
if err == nil {
70+
defer deleteTemporaryFile()
71+
checker = &CheckAttributeReader{
72+
Attributes: []string{"linguist-vendored", "linguist-generated", "linguist-language", "gitlab-language"},
73+
Repo: repo,
74+
IndexFile: indexFilename,
75+
WorkTree: worktree,
76+
}
77+
ctx, cancel := context.WithCancel(repo.Ctx)
78+
if err := checker.Init(ctx); err != nil {
79+
log.Error("Unable to open checker for %s. Error: %v", commitID, err)
80+
} else {
81+
go func() {
82+
err = checker.Run()
83+
if err != nil {
84+
log.Error("Unable to open checker for %s. Error: %v", commitID, err)
85+
cancel()
86+
}
9387
}()
9488
}
89+
defer func() {
90+
_ = checker.Close()
91+
cancel()
92+
}()
9593
}
9694

9795
contentBuf := bytes.Buffer{}

modules/git/repo_tree.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ func (repo *Repository) CommitTree(author, committer *Signature, tree *Tree, opt
4545
_, _ = messageBytes.WriteString(opts.Message)
4646
_, _ = messageBytes.WriteString("\n")
4747

48-
if CheckGitVersionAtLeast("1.7.9") == nil && (opts.KeyID != "" || opts.AlwaysSign) {
48+
if opts.KeyID != "" || opts.AlwaysSign {
4949
cmd.AddArguments(fmt.Sprintf("-S%s", opts.KeyID))
5050
}
5151

52-
if CheckGitVersionAtLeast("2.0.0") == nil && opts.NoGPGSign {
52+
if opts.NoGPGSign {
5353
cmd.AddArguments("--no-gpg-sign")
5454
}
5555

modules/repository/init.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -322,19 +322,17 @@ func initRepoCommit(ctx context.Context, tmpPath string, repo *repo_model.Reposi
322322
"-m", "Initial commit",
323323
}
324324

325-
if git.CheckGitVersionAtLeast("1.7.9") == nil {
326-
sign, keyID, signer, _ := asymkey_service.SignInitialCommit(ctx, tmpPath, u)
327-
if sign {
328-
args = append(args, "-S"+keyID)
329-
330-
if repo.GetTrustModel() == repo_model.CommitterTrustModel || repo.GetTrustModel() == repo_model.CollaboratorCommitterTrustModel {
331-
// need to set the committer to the KeyID owner
332-
committerName = signer.Name
333-
committerEmail = signer.Email
334-
}
335-
} else if git.CheckGitVersionAtLeast("2.0.0") == nil {
336-
args = append(args, "--no-gpg-sign")
325+
sign, keyID, signer, _ := asymkey_service.SignInitialCommit(ctx, tmpPath, u)
326+
if sign {
327+
args = append(args, "-S"+keyID)
328+
329+
if repo.GetTrustModel() == repo_model.CommitterTrustModel || repo.GetTrustModel() == repo_model.CollaboratorCommitterTrustModel {
330+
// need to set the committer to the KeyID owner
331+
committerName = signer.Name
332+
committerEmail = signer.Email
337333
}
334+
} else {
335+
args = append(args, "--no-gpg-sign")
338336
}
339337

340338
env = append(env,

services/gitdiff/gitdiff.go

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,34 +1418,32 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff
14181418

14191419
var checker *git.CheckAttributeReader
14201420

1421-
if git.CheckGitVersionAtLeast("1.7.8") == nil {
1422-
indexFilename, worktree, deleteTemporaryFile, err := gitRepo.ReadTreeToTemporaryIndex(opts.AfterCommitID)
1423-
if err == nil {
1424-
defer deleteTemporaryFile()
1425-
1426-
checker = &git.CheckAttributeReader{
1427-
Attributes: []string{"linguist-vendored", "linguist-generated", "linguist-language", "gitlab-language"},
1428-
Repo: gitRepo,
1429-
IndexFile: indexFilename,
1430-
WorkTree: worktree,
1431-
}
1432-
ctx, cancel := context.WithCancel(gitRepo.Ctx)
1433-
if err := checker.Init(ctx); err != nil {
1434-
log.Error("Unable to open checker for %s. Error: %v", opts.AfterCommitID, err)
1435-
} else {
1436-
go func() {
1437-
err := checker.Run()
1438-
if err != nil && err != ctx.Err() {
1439-
log.Error("Unable to open checker for %s. Error: %v", opts.AfterCommitID, err)
1440-
}
1441-
cancel()
1442-
}()
1443-
}
1444-
defer func() {
1445-
_ = checker.Close()
1421+
indexFilename, worktree, deleteTemporaryFile, err := gitRepo.ReadTreeToTemporaryIndex(opts.AfterCommitID)
1422+
if err == nil {
1423+
defer deleteTemporaryFile()
1424+
1425+
checker = &git.CheckAttributeReader{
1426+
Attributes: []string{"linguist-vendored", "linguist-generated", "linguist-language", "gitlab-language"},
1427+
Repo: gitRepo,
1428+
IndexFile: indexFilename,
1429+
WorkTree: worktree,
1430+
}
1431+
ctx, cancel := context.WithCancel(gitRepo.Ctx)
1432+
if err := checker.Init(ctx); err != nil {
1433+
log.Error("Unable to open checker for %s. Error: %v", opts.AfterCommitID, err)
1434+
} else {
1435+
go func() {
1436+
err := checker.Run()
1437+
if err != nil && err != ctx.Err() {
1438+
log.Error("Unable to open checker for %s. Error: %v", opts.AfterCommitID, err)
1439+
}
14461440
cancel()
14471441
}()
14481442
}
1443+
defer func() {
1444+
_ = checker.Close()
1445+
cancel()
1446+
}()
14491447
}
14501448

14511449
for _, diffFile := range diff.Files {

services/pull/merge.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -274,15 +274,8 @@ func rawMerge(ctx context.Context, pr *models.PullRequest, doer *user_model.User
274274
return "", fmt.Errorf("Unable to write .git/info/sparse-checkout file in tmpBasePath: %v", err)
275275
}
276276

277-
var gitConfigCommand func() *git.Command
278-
if git.CheckGitVersionAtLeast("1.8.0") == nil {
279-
gitConfigCommand = func() *git.Command {
280-
return git.NewCommand(ctx, "config", "--local")
281-
}
282-
} else {
283-
gitConfigCommand = func() *git.Command {
284-
return git.NewCommand(ctx, "config")
285-
}
277+
gitConfigCommand := func() *git.Command {
278+
return git.NewCommand(ctx, "config", "--local")
286279
}
287280

288281
// Switch off LFS process (set required, clean and smudge here also)
@@ -364,16 +357,14 @@ func rawMerge(ctx context.Context, pr *models.PullRequest, doer *user_model.User
364357

365358
// Determine if we should sign
366359
signArg := ""
367-
if git.CheckGitVersionAtLeast("1.7.9") == nil {
368-
sign, keyID, signer, _ := asymkey_service.SignMerge(ctx, pr, doer, tmpBasePath, "HEAD", trackingBranch)
369-
if sign {
370-
signArg = "-S" + keyID
371-
if pr.BaseRepo.GetTrustModel() == repo_model.CommitterTrustModel || pr.BaseRepo.GetTrustModel() == repo_model.CollaboratorCommitterTrustModel {
372-
committer = signer
373-
}
374-
} else if git.CheckGitVersionAtLeast("2.0.0") == nil {
375-
signArg = "--no-gpg-sign"
360+
sign, keyID, signer, _ := asymkey_service.SignMerge(ctx, pr, doer, tmpBasePath, "HEAD", trackingBranch)
361+
if sign {
362+
signArg = "-S" + keyID
363+
if pr.BaseRepo.GetTrustModel() == repo_model.CommitterTrustModel || pr.BaseRepo.GetTrustModel() == repo_model.CollaboratorCommitterTrustModel {
364+
committer = signer
376365
}
366+
} else {
367+
signArg = "--no-gpg-sign"
377368
}
378369

379370
commitTimeStr := time.Now().Format(time.RFC3339)

0 commit comments

Comments
 (0)