public static Single<ImmutableMap<String, GitCommitHash>> fetchTags(final String gitURL) { Preconditions.checkNotNull(gitURL); return Single.fromCallable(() -> { // The repository is not actually used, JGit just seems to require it. final Repository repository = FileRepositoryBuilder.create(Paths.get("").toFile()); final Collection<Ref> refs = new LsRemoteCommand(repository) .setRemote(gitURL) .setTags(true) .call(); final String prefix = "refs/tags/"; return refs.stream() .filter(x -> x.getTarget().getName().startsWith(prefix)) .collect(ImmutableMap.toImmutableMap( x -> x.getTarget().getName().substring(prefix.length()), x -> GitCommitHash.of((x.getPeeledObjectId() == null ? x.getObjectId() : x.getPeeledObjectId()).getName()))); }); }
@Override public List<RemoteReference> lsRemote(String remoteUrl) throws UnauthorizedException, GitException { LsRemoteCommand lsRemoteCommand = getGit().lsRemote().setRemote(remoteUrl); Collection<Ref> refs; try { refs = lsRemoteCommand.call(); } catch (GitAPIException exception) { if (exception.getMessage().contains(ERROR_AUTHENTICATION_REQUIRED)) { throw new UnauthorizedException(format(ERROR_AUTHENTICATION_FAILED, remoteUrl)); } else { throw new GitException(exception.getMessage(), exception); } } return refs.stream() .map( ref -> newDto(RemoteReference.class) .withCommitId(ref.getObjectId().name()) .withReferenceName(ref.getName())) .collect(toList()); }
@Test public void advertisedReferencesOmitPrivateChangesOfOtherUsers() throws Exception { allow("refs/heads/master", Permission.READ, REGISTERED_USERS); TestRepository<?> userTestRepository = cloneProject(project, user); try (Git git = userTestRepository.git()) { LsRemoteCommand lsRemoteCommand = git.lsRemote(); String change3RefName = c3.currentPatchSet().getRefName(); List<String> initialRefNames = lsRemoteCommand.call().stream().map(Ref::getName).collect(toList()); assertWithMessage("Precondition violated").that(initialRefNames).contains(change3RefName); gApi.changes().id(c3.getId().get()).setPrivate(true, null); List<String> refNames = lsRemoteCommand.call().stream().map(Ref::getName).collect(toList()); assertThat(refNames).doesNotContain(change3RefName); } }
@Test public void advertisedReferencesIncludePrivateChangesWhenAllRefsMayBeRead() throws Exception { allow("refs/*", Permission.READ, REGISTERED_USERS); TestRepository<?> userTestRepository = cloneProject(project, user); try (Git git = userTestRepository.git()) { LsRemoteCommand lsRemoteCommand = git.lsRemote(); String change3RefName = c3.currentPatchSet().getRefName(); List<String> initialRefNames = lsRemoteCommand.call().stream().map(Ref::getName).collect(toList()); assertWithMessage("Precondition violated").that(initialRefNames).contains(change3RefName); gApi.changes().id(c3.getId().get()).setPrivate(true, null); List<String> refNames = lsRemoteCommand.call().stream().map(Ref::getName).collect(toList()); assertThat(refNames).contains(change3RefName); } }
private ObjectId realFindRemoteRef() throws ProjectException { final Collection<Ref> remoteRefs; final LsRemoteCommand lsRemote = Git.lsRemoteRepository() .setRemote(url); if (auth != null) lsRemote.setCredentialsProvider(auth.build()); try { remoteRefs = lsRemote.call(); } catch (GitAPIException e) { throw new ProjectException("ls-remote failed for: " + url, e); } for (final Ref remoteRef : remoteRefs) { if (!ref.equals(remoteRef.getName())) { continue; } return remoteRef.getObjectId(); } return null; }
public Collection<Ref> allRemoteTags() throws SCMException { if (remoteTags == null) { final LsRemoteCommand lsRemoteCommand = getGit().lsRemote().setTags(true).setHeads(false); if (config.getRemoteUrlOrNull() != null) { lsRemoteCommand.setRemote(config.getRemoteUrlOrNull()); } try { remoteTags = lsRemoteCommand.call(); } catch (final GitAPIException e) { throw new SCMException(e, "Remote tags could not be listed!"); } } return remoteTags; }
@Test public void canDetectRemoteTags() throws Exception { final LsRemoteCommand cmd = mock(LsRemoteCommand.class); when(git.lsRemote()).thenReturn(cmd); when(cmd.setTags(true)).thenReturn(cmd); when(cmd.setHeads(false)).thenReturn(cmd); when(cmd.call()).thenReturn(Arrays.asList(ref)); final Collection<Ref> refs = repository.allRemoteTags(); assertEquals(1, refs.size()); assertEquals(ref, refs.iterator().next()); verify(cmd).setRemote(ANY_REMOTE_URL); }
@Override public Collection<Ref> getTags() throws GitAPIException { LsRemoteCommand lsRemoteCommand = git.lsRemote().setTags(true).setHeads(false); if (remoteUrl != null) { lsRemoteCommand.setRemote(remoteUrl); } return lsRemoteCommand.call(); }
@Test public void testLsRemoteOnHTTP() throws Exception { LsRemoteCommand command = Git.lsRemoteRepository(); command.setRemote("https://github.com/TheElegitTeam/TestRepository.git"); command.call(); }
public List<String> getBranchList(boolean omitMaster) throws Exception { LsRemoteCommand command = initLsRemoteCommand(); Collection<Ref> refs = command.setHeads(true).call(); List<String> branchList = getItemNameList(refs, "refs/heads/", true); if (omitMaster && branchList.contains(MASTER_NAME) && branchList.size() > 1) { branchList.remove(MASTER_NAME); } return branchList; }
/** {@inheritDoc} */ public Map<String, ObjectId> getRemoteReferences(String url, String pattern, boolean headsOnly, boolean tagsOnly) throws GitException, InterruptedException { Map<String, ObjectId> references = new HashMap<>(); String regexPattern = null; if (pattern != null) { regexPattern = createRefRegexFromGlob(pattern); } try (Repository repo = openDummyRepository()) { LsRemoteCommand lsRemote = new LsRemoteCommand(repo); if (headsOnly) { lsRemote.setHeads(headsOnly); } if (tagsOnly) { lsRemote.setTags(tagsOnly); } lsRemote.setRemote(url); lsRemote.setCredentialsProvider(getProvider()); Collection<Ref> refs = lsRemote.call(); for (final Ref r : refs) { final String refName = r.getName(); final ObjectId refObjectId = r.getPeeledObjectId() != null ? r.getPeeledObjectId() : r.getObjectId(); if (regexPattern != null) { if (refName.matches(regexPattern)) { references.put(refName, refObjectId); } } else { references.put(refName, refObjectId); } } } catch (GitAPIException | IOException e) { throw new GitException(e); } return references; }
public List<String> getTagList() throws Exception { LsRemoteCommand command = initLsRemoteCommand(); Collection<Ref> refs = command.setTags(true).call(); List<String> tagList = getItemNameList(refs, "refs/tags/", true); return tagList; }
private LsRemoteCommand initLsRemoteCommand() throws IOException { Git git = new Git(new NoLocalRepository()); return git.lsRemote().setCredentialsProvider(getCredentialsProvider()) .setRemote(this.repositoryUrl.toString()); }