Skip to content
This repository was archived by the owner on Apr 12, 2019. It is now read-only.

Commit 31f4b8e

Browse files
daviianlunny
authored andcommitted
fix git version below 2.7 (#119)
* fix git version below 2.7 * fmt * minor code improvement * copyright + import order * fmt
1 parent 0077deb commit 31f4b8e

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

commit_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package git
66

77
import (
88
"testing"
9+
910
"github.com/stretchr/testify/assert"
1011
)
1112

repo_commit.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"container/list"
1010
"strconv"
1111
"strings"
12+
13+
"github.com/mcuadros/go-version"
1214
)
1315

1416
// GetRefCommitID returns the last commit ID string of given reference (branch or tag).
@@ -274,7 +276,7 @@ func (repo *Repository) CommitsCountBetween(start, end string) (int64, error) {
274276
func (repo *Repository) commitsBefore(id SHA1, limit int) (*list.List, error) {
275277
cmd := NewCommand("log")
276278
if limit > 0 {
277-
cmd.AddArguments("-"+ strconv.Itoa(limit), prettyLogFormat, id.String())
279+
cmd.AddArguments("-"+strconv.Itoa(limit), prettyLogFormat, id.String())
278280
} else {
279281
cmd.AddArguments(prettyLogFormat, id.String())
280282
}
@@ -316,15 +318,35 @@ func (repo *Repository) getCommitsBeforeLimit(id SHA1, num int) (*list.List, err
316318
}
317319

318320
func (repo *Repository) getBranches(commit *Commit, limit int) ([]string, error) {
319-
stdout, err := NewCommand("for-each-ref", "--count="+ strconv.Itoa(limit), "--format=%(refname)", "--contains", commit.ID.String(), BranchPrefix).RunInDir(repo.Path)
321+
if version.Compare(gitVersion, "2.7.0", ">=") {
322+
stdout, err := NewCommand("for-each-ref", "--count="+strconv.Itoa(limit), "--format=%(refname:strip=2)", "--contains", commit.ID.String(), BranchPrefix).RunInDir(repo.Path)
323+
if err != nil {
324+
return nil, err
325+
}
326+
327+
branches := strings.Fields(stdout)
328+
return branches, nil
329+
}
330+
331+
stdout, err := NewCommand("branch", "--contains", commit.ID.String()).RunInDir(repo.Path)
320332
if err != nil {
321333
return nil, err
322334
}
323335

324336
refs := strings.Split(stdout, "\n")
325-
branches := make([]string, len(refs)-1)
326-
for i, ref := range refs[:len(refs)-1] {
327-
branches[i] = strings.TrimPrefix(ref, BranchPrefix)
337+
338+
var max int
339+
if len(refs) > limit {
340+
max = limit
341+
} else {
342+
max = len(refs) - 1
343+
}
344+
345+
branches := make([]string, max)
346+
for i, ref := range refs[:max] {
347+
parts := strings.Fields(ref)
348+
349+
branches[i] = parts[len(parts)-1]
328350
}
329351
return branches, nil
330352
}

repo_commit_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2018 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package git
6+
7+
import (
8+
"path/filepath"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestRepository_GetBranches(t *testing.T) {
15+
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
16+
bareRepo1, err := OpenRepository(bareRepo1Path)
17+
assert.NoError(t, err)
18+
19+
// these test case are specific to the repo1_bare test repo
20+
testCases := []struct {
21+
CommitID string
22+
ExpectedBranches []string
23+
}{
24+
{"2839944139e0de9737a044f78b0e4b40d989a9e3", []string{"branch1"}},
25+
{"5c80b0245c1c6f8343fa418ec374b13b5d4ee658", []string{"branch2"}},
26+
{"37991dec2c8e592043f47155ce4808d4580f9123", []string{"master"}},
27+
{"95bb4d39648ee7e325106df01a621c530863a653", []string{"branch1", "branch2"}},
28+
{"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", []string{"branch2", "master"}},
29+
}
30+
for _, testCase := range testCases {
31+
commit, err := bareRepo1.GetCommit(testCase.CommitID)
32+
assert.NoError(t, err)
33+
branches, err := bareRepo1.getBranches(commit, 2)
34+
assert.NoError(t, err)
35+
assert.Equal(t, testCase.ExpectedBranches, branches)
36+
}
37+
}

repo_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package git
77
import (
88
"testing"
99
"time"
10+
1011
"github.com/stretchr/testify/assert"
1112
)
1213

0 commit comments

Comments
 (0)