From 31f4b8e8c805438ac6d8914b38accb1d8aaf695e Mon Sep 17 00:00:00 2001 From: David Schneiderbauer Date: Sat, 26 May 2018 07:17:21 +0200 Subject: [PATCH] fix git version below 2.7 (#119) * fix git version below 2.7 * fmt * minor code improvement * copyright + import order * fmt --- commit_test.go | 1 + repo_commit.go | 32 +++++++++++++++++++++++++++----- repo_commit_test.go | 37 +++++++++++++++++++++++++++++++++++++ repo_test.go | 1 + 4 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 repo_commit_test.go diff --git a/commit_test.go b/commit_test.go index a30f2b859dd5d..121ba9c643f6f 100644 --- a/commit_test.go +++ b/commit_test.go @@ -6,6 +6,7 @@ package git import ( "testing" + "github.com/stretchr/testify/assert" ) diff --git a/repo_commit.go b/repo_commit.go index 56bebd7a3a12d..1acdfffb34738 100644 --- a/repo_commit.go +++ b/repo_commit.go @@ -9,6 +9,8 @@ import ( "container/list" "strconv" "strings" + + "github.com/mcuadros/go-version" ) // 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) { func (repo *Repository) commitsBefore(id SHA1, limit int) (*list.List, error) { cmd := NewCommand("log") if limit > 0 { - cmd.AddArguments("-"+ strconv.Itoa(limit), prettyLogFormat, id.String()) + cmd.AddArguments("-"+strconv.Itoa(limit), prettyLogFormat, id.String()) } else { cmd.AddArguments(prettyLogFormat, id.String()) } @@ -316,15 +318,35 @@ 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:strip=2)", "--contains", commit.ID.String(), BranchPrefix).RunInDir(repo.Path) + if err != nil { + return nil, err + } + + branches := strings.Fields(stdout) + 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 } diff --git a/repo_commit_test.go b/repo_commit_test.go new file mode 100644 index 0000000000000..db37135e708b5 --- /dev/null +++ b/repo_commit_test.go @@ -0,0 +1,37 @@ +// Copyright 2018 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package git + +import ( + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" +) + +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) + } +} diff --git a/repo_test.go b/repo_test.go index 971687f35a0c4..c5ce6f44473e7 100644 --- a/repo_test.go +++ b/repo_test.go @@ -7,6 +7,7 @@ package git import ( "testing" "time" + "github.com/stretchr/testify/assert" )