Skip to content

Commit

Permalink
Merge pull request #105 from nesterov-n/features/contains
Browse files Browse the repository at this point in the history
Implement --contains flag for branch list
  • Loading branch information
ajoberstar committed Mar 21, 2016
2 parents f5aaa39 + 89450b5 commit 8fcad92
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.ajoberstar.grgit.operation

import org.ajoberstar.grgit.service.ResolveService

import java.util.concurrent.Callable

import org.ajoberstar.grgit.Branch
Expand Down Expand Up @@ -47,6 +49,12 @@ import org.eclipse.jgit.api.errors.GitAPIException
* def branches = grgit.branch.list(mode: BranchListOp.Mode.ALL)
* </pre>
*
* <p>To list all branches contains specified commit</p>
*
* <pre>
* def branches = grgit.branch.list(contains: %Commit hash or tag name%)
* </pre>
*
* See <a href="http://git-scm.com/docs/git-branch">git-branch Manual Page</a>.
*
* @since 0.2.0
Expand All @@ -60,14 +68,21 @@ class BranchListOp implements Callable<List<Branch>> {
*/
Mode mode = Mode.LOCAL

/**
* Commit ref branches must contains
*/
Object contains = null

BranchListOp(Repository repo) {
this.repo = repo
}

List<Branch> call() {
ListBranchCommand cmd = repo.jgit.branchList()
cmd.listMode = mode.jgit

if (contains) {
cmd.contains = new ResolveService(repo).toRevisionString(contains)
}
try {
return cmd.call().collect {
JGitUtil.resolveBranch(repo, it.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/
package org.ajoberstar.grgit.operation

import org.ajoberstar.grgit.Commit
import org.ajoberstar.grgit.Grgit
import org.ajoberstar.grgit.fixtures.GitTestUtil
import org.ajoberstar.grgit.fixtures.MultiGitOpSpec

import spock.lang.Unroll

class BranchListOpSpec extends MultiGitOpSpec {
Expand All @@ -33,20 +33,35 @@ class BranchListOpSpec extends MultiGitOpSpec {

remoteGrgit.branch.add(name: 'my-branch')

repoFile(remoteGrgit, '2.txt') << '2'
remoteGrgit.commit(message: 'another', all: true)
remoteGrgit.tag.add(name: 'test-tag');

localGrgit = clone('local', remoteGrgit)
}

@Unroll('list branch with #arguments lists #expected')
def 'list branch without arguments only lists local'() {
given:
def expectedBranches = expected.collect { GitTestUtil.branch(*it) }
def head = localGrgit.head()
expect:
localGrgit.branch.list(arguments) == expectedBranches
where:
arguments | expected
[:] | [['refs/heads/master', 'refs/remotes/origin/master']]
[mode: BranchListOp.Mode.LOCAL] | [['refs/heads/master', 'refs/remotes/origin/master']]
[mode: BranchListOp.Mode.REMOTE] | [['refs/remotes/origin/master'], ['refs/remotes/origin/my-branch']]
[mode: BranchListOp.Mode.ALL] | [['refs/heads/master', 'refs/remotes/origin/master'], ['refs/remotes/origin/master'], ['refs/remotes/origin/my-branch']]
arguments | expected
[:] | [['refs/heads/master', 'refs/remotes/origin/master']]
[mode: BranchListOp.Mode.LOCAL] | [['refs/heads/master', 'refs/remotes/origin/master']]
[mode: BranchListOp.Mode.REMOTE] | [['refs/remotes/origin/master'], ['refs/remotes/origin/my-branch']]
[mode: BranchListOp.Mode.ALL] | [['refs/heads/master', 'refs/remotes/origin/master'], ['refs/remotes/origin/master'], ['refs/remotes/origin/my-branch']]
[mode: BranchListOp.Mode.REMOTE, contains: 'test-tag'] | [['refs/remotes/origin/master']]
}

def 'list branch receives Commit object as contains flag'() {
given:
def expectedBranches = [GitTestUtil.branch('refs/remotes/origin/master')]
def head = localGrgit.head()
def arguments = [mode: BranchListOp.Mode.REMOTE, contains: head]
expect:
localGrgit.branch.list(arguments) == expectedBranches
}
}

0 comments on commit 8fcad92

Please sign in to comment.