Skip to content

Commit

Permalink
Add fetch tags and branches to local git provider [ci fast]
Browse files Browse the repository at this point in the history
  • Loading branch information
pditommaso committed Jan 28, 2022
1 parent 26b4be9 commit eeb9ac3
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package nextflow.scm

import org.eclipse.jgit.api.Git
import org.eclipse.jgit.lib.Constants
import org.eclipse.jgit.lib.Ref
import org.eclipse.jgit.revwalk.RevWalk
import org.eclipse.jgit.treewalk.TreeWalk
/**
Expand Down Expand Up @@ -101,5 +102,36 @@ class LocalRepositoryProvider extends RepositoryProvider {
}
}

@Override
List<TagInfo> getTags() {
final String prefix = 'refs/tags/'

try ( final git = Git.open(new File(this.path, project))) {
List<Ref> tags = git.tagList().call()
final result = new ArrayList(tags.size())
for (Ref ref : tags) {
if( ref.name.startsWith(prefix) ) {
result.add( new TagInfo(ref.name.substring(prefix.length()), ref.getObjectId().name()))
}
}
return result
}
}

@Override
List<BranchInfo> getBranches() {
final String prefix = 'refs/heads/'

try ( final git = Git.open(new File(this.path, project))) {
List<Ref> tags = git.branchList().call()
final result = new ArrayList(tags.size())
for (Ref ref : tags) {
if( ref.name.startsWith(prefix) ) {
result.add( new BranchInfo(ref.name.substring(prefix.length()), ref.getObjectId().name()) )
}
}
return result
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,20 @@ class LocalRepositoryProviderTest extends Specification {
@Shared
Path testFolder

@Shared
Git repo

def setup() {
testFolder = Files.createTempDirectory('test').toAbsolutePath()

// create the main project
testFolder.resolve('project_hello').mkdirs()
def dir = testFolder.resolve('project_hello').toFile()
def init = Git.init()
def repo = init.setDirectory( dir ).call()
this.repo = init.setDirectory( dir ).call()
new File(dir, 'main.nf').text = 'main script'
repo.add().addFilepattern('main.nf').call()
repo.commit().setMessage('First commit').call()

}

def cleanup() {
Expand Down Expand Up @@ -122,5 +124,55 @@ class LocalRepositoryProviderTest extends Specification {

}

def 'should list tags' () {
given:
def dir = testFolder.toFile()
def ref1 = repo.tag().setName('tag_1').setMessage('First tag').call()
// add new file
new File(dir, 'foo.nf').text = 'foo script'
repo.add().addFilepattern('foo.nf').call()
repo.commit().setMessage('Second commit').call()
def ref2 = repo.tag().setName('tag_2').setMessage('Second tag').call()

and:
def config = new ProviderConfig('local', [path: testFolder])
def manager = new LocalRepositoryProvider('project_hello', config)

when:
def tags = manager.getTags()
then:
tags.size() == 2
and:
tags[0].name == 'tag_1'
tags[0].getCommitId() == ref1.getObjectId().name()
and:
tags[1].name == 'tag_2'
tags[1].getCommitId() == ref2.getObjectId().name()
}

def 'should list branches' () {
given:
def dir = testFolder.toFile()
def ref1 = repo.branchCreate().setName('branch_1').call()
// add new file
new File(dir, 'foo.nf').text = 'foo script'
repo.add().addFilepattern('foo.nf').call()
repo.commit().setMessage('Second commit').call()
def ref2 = repo.branchCreate().setName('branch_2').call()

and:
def config = new ProviderConfig('local', [path: testFolder])
def manager = new LocalRepositoryProvider('project_hello', config)

when:
def branches = manager.getBranches()
then:
branches.size() == 3
and:
branches.find { it.name == 'master' }
and:
branches.find { it.name == 'branch_1' }.commitId == ref1.getObjectId().name()
and:
branches.find { it.name == 'branch_2' }.commitId == ref2.getObjectId().name()
}
}

0 comments on commit eeb9ac3

Please sign in to comment.