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

Refactor branch list using src-d/go-git and add GetRefs function #133

Merged
merged 6 commits into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
237 changes: 230 additions & 7 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@
[prune]
go-tests = true
unused-packages = true

[[constraint]]
name = "gopkg.in/src-d/go-git.v4"
version = "4.7.1"
18 changes: 18 additions & 0 deletions ref.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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

// Reference represents a Git ref.
type Reference struct {
Name string
repo *Repository
Object SHA1 // The id of this commit object
Type string
}

// Commit return the commit of the reference
func (ref *Reference) Commit() (*Commit, error) {
return ref.repo.getCommit(ref.Object)
}
21 changes: 16 additions & 5 deletions repo_branch.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2015 The Gogs Authors. All rights reserved.
// 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.

Expand All @@ -7,6 +8,9 @@ package git
import (
"fmt"
"strings"

"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
)

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

// GetBranches returns all branches of the repository.
func (repo *Repository) GetBranches() ([]string, error) {
stdout, err := NewCommand("for-each-ref", "--format=%(refname)", BranchPrefix).RunInDir(repo.Path)
r, err := git.PlainOpen(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)
branchIter, err := r.Branches()
if err != nil {
return nil, err
}
branches := make([]string, 0)
if err = branchIter.ForEach(func(branch *plumbing.Reference) error {
branches = append(branches, branch.Name().Short())
return nil
}); err != nil {
return nil, err
}

return branches, nil
}

Expand Down
39 changes: 39 additions & 0 deletions repo_branch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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)

branches, err := bareRepo1.GetBranches()

assert.NoError(t, err)
assert.Len(t, branches, 3)
assert.ElementsMatch(t, []string{"branch1", "branch2", "master"}, branches)
}

func BenchmarkRepository_GetBranches(b *testing.B) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
bareRepo1, err := OpenRepository(bareRepo1Path)
if err != nil {
b.Fatal(err)
}

for i := 0; i < b.N; i++ {
_, err := bareRepo1.GetBranches()
if err != nil {
b.Fatal(err)
}
}
}
2 changes: 1 addition & 1 deletion repo_commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/assert"
)

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