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

Commit 578ad8f

Browse files
lafriksjonasfranz
authored andcommitted
Refactor branch list using src-d/go-git and add GetRefs function (#133)
* Refactor branch list using src-d/go-git and add GetRefs function * Fix copyright * Fix to reuse ObjectType * Add function to filter refs by prefix * Fix import order * Optimize if structure
1 parent d945eda commit 578ad8f

File tree

456 files changed

+106076
-749
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

456 files changed

+106076
-749
lines changed

Diff for: Gopkg.lock

+230-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Gopkg.toml

+4
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@
3232
[prune]
3333
go-tests = true
3434
unused-packages = true
35+
36+
[[constraint]]
37+
name = "gopkg.in/src-d/go-git.v4"
38+
version = "4.7.1"

Diff for: ref.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
// Reference represents a Git ref.
8+
type Reference struct {
9+
Name string
10+
repo *Repository
11+
Object SHA1 // The id of this commit object
12+
Type string
13+
}
14+
15+
// Commit return the commit of the reference
16+
func (ref *Reference) Commit() (*Commit, error) {
17+
return ref.repo.getCommit(ref.Object)
18+
}

Diff for: repo_branch.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2015 The Gogs Authors. All rights reserved.
2+
// Copyright 2018 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

@@ -7,6 +8,9 @@ package git
78
import (
89
"fmt"
910
"strings"
11+
12+
"gopkg.in/src-d/go-git.v4"
13+
"gopkg.in/src-d/go-git.v4/plumbing"
1014
)
1115

1216
// BranchPrefix base dir of the branch information file store on git
@@ -60,16 +64,23 @@ func (repo *Repository) SetDefaultBranch(name string) error {
6064

6165
// GetBranches returns all branches of the repository.
6266
func (repo *Repository) GetBranches() ([]string, error) {
63-
stdout, err := NewCommand("for-each-ref", "--format=%(refname)", BranchPrefix).RunInDir(repo.Path)
67+
r, err := git.PlainOpen(repo.Path)
6468
if err != nil {
6569
return nil, err
6670
}
6771

68-
refs := strings.Split(stdout, "\n")
69-
branches := make([]string, len(refs)-1)
70-
for i, ref := range refs[:len(refs)-1] {
71-
branches[i] = strings.TrimPrefix(ref, BranchPrefix)
72+
branchIter, err := r.Branches()
73+
if err != nil {
74+
return nil, err
7275
}
76+
branches := make([]string, 0)
77+
if err = branchIter.ForEach(func(branch *plumbing.Reference) error {
78+
branches = append(branches, branch.Name().Short())
79+
return nil
80+
}); err != nil {
81+
return nil, err
82+
}
83+
7384
return branches, nil
7485
}
7586

Diff for: repo_branch_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
branches, err := bareRepo1.GetBranches()
20+
21+
assert.NoError(t, err)
22+
assert.Len(t, branches, 3)
23+
assert.ElementsMatch(t, []string{"branch1", "branch2", "master"}, branches)
24+
}
25+
26+
func BenchmarkRepository_GetBranches(b *testing.B) {
27+
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
28+
bareRepo1, err := OpenRepository(bareRepo1Path)
29+
if err != nil {
30+
b.Fatal(err)
31+
}
32+
33+
for i := 0; i < b.N; i++ {
34+
_, err := bareRepo1.GetBranches()
35+
if err != nil {
36+
b.Fatal(err)
37+
}
38+
}
39+
}

Diff for: repo_commit_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/stretchr/testify/assert"
1212
)
1313

14-
func TestRepository_GetBranches(t *testing.T) {
14+
func TestRepository_GetCommitBranches(t *testing.T) {
1515
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
1616
bareRepo1, err := OpenRepository(bareRepo1Path)
1717
assert.NoError(t, err)

0 commit comments

Comments
 (0)