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

fix git version below 2.7 #119

Merged
merged 5 commits into from
May 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions repo_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"container/list"
"strconv"
"strings"
"github.com/mcuadros/go-version"
Copy link
Member

@jonasfranz jonasfranz May 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix import order.

)

// GetRefCommitID returns the last commit ID string of given reference (branch or tag).
Expand Down Expand Up @@ -316,15 +317,39 @@ func (repo *Repository) getCommitsBeforeLimit(id SHA1, num int) (*list.List, err
}

func (repo *Repository) getBranches(commit *Commit, limit int) ([]string, error) {
stdout, err := NewCommand("for-each-ref", "--count="+ strconv.Itoa(limit), "--format=%(refname)", "--contains", commit.ID.String(), BranchPrefix).RunInDir(repo.Path)
if version.Compare(gitVersion, "2.7.0", ">=") {
stdout, err := NewCommand("for-each-ref", "--count="+ strconv.Itoa(limit), "--format=%(refname)", "--contains", commit.ID.String(), BranchPrefix).RunInDir(repo.Path)
if err != nil {
return nil, err
}

refs := strings.Split(stdout, "\n")
branches := make([]string, len(refs)-1)
for i, ref := range refs[:len(refs)-1] {
branches[i] = strings.TrimPrefix(ref, BranchPrefix)
}
return branches, nil
}

stdout, err := NewCommand("branch", "--contains", commit.ID.String()).RunInDir(repo.Path)
if err != nil {
return nil, err
}

refs := strings.Split(stdout, "\n")
branches := make([]string, len(refs)-1)
for i, ref := range refs[:len(refs)-1] {
branches[i] = strings.TrimPrefix(ref, BranchPrefix)

var max int
if len(refs) > limit {
max = limit
} else {
max = len(refs)-1
}

branches := make([]string, max)
for i, ref := range refs[:max] {
parts := strings.Fields(ref)

branches[i] = parts[len(parts)-1]
}
return branches, nil
}
33 changes: 33 additions & 0 deletions repo_commit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package git
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment header


import (
"testing"

"github.com/stretchr/testify/assert"
"path/filepath"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import order

)

func TestRepository_GetBranches(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
bareRepo1, err := OpenRepository(bareRepo1Path)
assert.NoError(t, err)

// these test case are specific to the repo1_bare test repo
testCases := []struct {
CommitID string
ExpectedBranches []string
}{
{"2839944139e0de9737a044f78b0e4b40d989a9e3", []string{"branch1"}},
{"5c80b0245c1c6f8343fa418ec374b13b5d4ee658", []string{"branch2"}},
{"37991dec2c8e592043f47155ce4808d4580f9123", []string{"master"}},
{"95bb4d39648ee7e325106df01a621c530863a653", []string{"branch1", "branch2"}},
{"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", []string{"branch2", "master"}},
}
for _, testCase := range testCases {
commit, err := bareRepo1.GetCommit(testCase.CommitID)
assert.NoError(t, err)
branches, err := bareRepo1.getBranches(commit, 2)
assert.NoError(t, err)
assert.Equal(t, testCase.ExpectedBranches, branches)
}
}