Search in sources :

Example 11 with FetchResult

use of org.eclipse.jgit.transport.FetchResult in project jbosstools-openshift by jbosstools.

the class EGitUtils method fetch.

/**
 * Fetches the source ref(s) (from the given ref spec(s)) from the given uri
 * to the given destination(s) (in the given ref spec(s)) to the given
 * repository.
 *
 * @param uri
 *            the uri to fetch from
 * @param fetchRefsRefSpecs
 *            the references with the sources and destinations
 * @param repository
 *            the repository to fetch to
 * @param monitor
 *            the monitor to report progress to
 * @return
 * @throws InvocationTargetException
 * @throws CoreException
 */
private static Collection<Ref> fetch(URIish uri, List<RefSpec> fetchRefsRefSpecs, Repository repository, IProgressMonitor monitor) throws InvocationTargetException, CoreException {
    FetchOperation fetch = new FetchOperation(repository, uri, fetchRefsRefSpecs, 10 * 1024, false);
    fetch.run(monitor);
    FetchResult result = fetch.getOperationResult();
    return result.getAdvertisedRefs();
}
Also used : FetchOperation(org.eclipse.egit.core.op.FetchOperation) FetchResult(org.eclipse.jgit.transport.FetchResult)

Example 12 with FetchResult

use of org.eclipse.jgit.transport.FetchResult in project spring-cloud-config by spring-cloud.

the class JGitEnvironmentRepository method fetch.

protected FetchResult fetch(Git git, String label) {
    FetchCommand fetch = git.fetch();
    fetch.setRemote("origin");
    fetch.setTagOpt(TagOpt.FETCH_TAGS);
    fetch.setRemoveDeletedRefs(deleteUntrackedBranches);
    configureCommand(fetch);
    try {
        FetchResult result = fetch.call();
        if (result.getTrackingRefUpdates() != null && result.getTrackingRefUpdates().size() > 0) {
            logger.info("Fetched for remote " + label + " and found " + result.getTrackingRefUpdates().size() + " updates");
        }
        return result;
    } catch (Exception ex) {
        String message = "Could not fetch remote for " + label + " remote: " + git.getRepository().getConfig().getString("remote", "origin", "url");
        warn(message, ex);
        return null;
    }
}
Also used : FetchResult(org.eclipse.jgit.transport.FetchResult) FetchCommand(org.eclipse.jgit.api.FetchCommand) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) NoRemoteRepositoryException(org.eclipse.jgit.errors.NoRemoteRepositoryException) IOException(java.io.IOException) RefNotFoundException(org.eclipse.jgit.api.errors.RefNotFoundException)

Example 13 with FetchResult

use of org.eclipse.jgit.transport.FetchResult in project spring-cloud-config by spring-cloud.

the class JGitEnvironmentRepository method refresh.

/**
 * Get the working directory ready.
 */
public String refresh(String label) {
    Git git = null;
    try {
        git = createGitClient();
        if (shouldPull(git)) {
            FetchResult fetchStatus = fetch(git, label);
            if (deleteUntrackedBranches && fetchStatus != null) {
                deleteUntrackedLocalBranches(fetchStatus.getTrackingRefUpdates(), git);
            }
            // checkout after fetch so we can get any new branches, tags, ect.
            checkout(git, label);
            if (isBranch(git, label)) {
                // merge results from fetch
                merge(git, label);
                if (!isClean(git, label)) {
                    logger.warn("The local repository is dirty or ahead of origin. Resetting" + " it to origin/" + label + ".");
                    resetHard(git, label, LOCAL_BRANCH_REF_PREFIX + label);
                }
            }
        } else {
            // nothing to update so just checkout
            checkout(git, label);
        }
        // always return what is currently HEAD as the version
        return git.getRepository().findRef("HEAD").getObjectId().getName();
    } catch (RefNotFoundException e) {
        throw new NoSuchLabelException("No such label: " + label, e);
    } catch (NoRemoteRepositoryException e) {
        throw new NoSuchRepositoryException("No such repository: " + getUri(), e);
    } catch (GitAPIException e) {
        throw new NoSuchRepositoryException("Cannot clone or checkout repository: " + getUri(), e);
    } catch (Exception e) {
        throw new IllegalStateException("Cannot load environment", e);
    } finally {
        try {
            if (git != null) {
                git.close();
            }
        } catch (Exception e) {
            this.logger.warn("Could not close git repository", e);
        }
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.eclipse.jgit.api.Git) FetchResult(org.eclipse.jgit.transport.FetchResult) RefNotFoundException(org.eclipse.jgit.api.errors.RefNotFoundException) NoRemoteRepositoryException(org.eclipse.jgit.errors.NoRemoteRepositoryException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) NoRemoteRepositoryException(org.eclipse.jgit.errors.NoRemoteRepositoryException) IOException(java.io.IOException) RefNotFoundException(org.eclipse.jgit.api.errors.RefNotFoundException)

Example 14 with FetchResult

use of org.eclipse.jgit.transport.FetchResult in project spring-cloud-config by spring-cloud.

the class JGitEnvironmentRepositoryTests method shouldHandleExceptionWhileRemovingBranches.

@Test
public void shouldHandleExceptionWhileRemovingBranches() throws Exception {
    Git git = mock(Git.class);
    CloneCommand cloneCommand = mock(CloneCommand.class);
    MockGitFactory factory = new MockGitFactory(git, cloneCommand);
    this.repository.setGitFactory(factory);
    this.repository.setDeleteUntrackedBranches(true);
    // refresh()->shouldPull
    StatusCommand statusCommand = mock(StatusCommand.class);
    Status status = mock(Status.class);
    when(git.status()).thenReturn(statusCommand);
    Repository repository = mock(Repository.class);
    when(git.getRepository()).thenReturn(repository);
    StoredConfig storedConfig = mock(StoredConfig.class);
    when(repository.getConfig()).thenReturn(storedConfig);
    when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git");
    when(statusCommand.call()).thenReturn(status);
    when(status.isClean()).thenReturn(true);
    // refresh()->fetch
    FetchCommand fetchCommand = mock(FetchCommand.class);
    FetchResult fetchResult = mock(FetchResult.class);
    TrackingRefUpdate trackingRefUpdate = mock(TrackingRefUpdate.class);
    Collection<TrackingRefUpdate> trackingRefUpdates = Collections.singletonList(trackingRefUpdate);
    when(git.fetch()).thenReturn(fetchCommand);
    when(fetchCommand.setRemote(anyString())).thenReturn(fetchCommand);
    when(fetchCommand.call()).thenReturn(fetchResult);
    when(fetchResult.getTrackingRefUpdates()).thenReturn(trackingRefUpdates);
    // refresh()->deleteBranch
    ReceiveCommand receiveCommand = mock(ReceiveCommand.class);
    when(trackingRefUpdate.asReceiveCommand()).thenReturn(receiveCommand);
    when(receiveCommand.getType()).thenReturn(ReceiveCommand.Type.DELETE);
    when(trackingRefUpdate.getLocalName()).thenReturn("refs/remotes/origin/feature/deletedBranchFromOrigin");
    DeleteBranchCommand deleteBranchCommand = mock(DeleteBranchCommand.class);
    when(git.branchDelete()).thenReturn(deleteBranchCommand);
    when(deleteBranchCommand.setBranchNames(eq("feature/deletedBranchFromOrigin"))).thenReturn(deleteBranchCommand);
    when(deleteBranchCommand.setForce(true)).thenReturn(deleteBranchCommand);
    // here
    when(deleteBranchCommand.call()).thenThrow(new NotMergedException());
    // is
    // our
    // exception
    // we
    // are
    // testing
    // refresh()->checkout
    CheckoutCommand checkoutCommand = mock(CheckoutCommand.class);
    // refresh()->checkout->containsBranch
    ListBranchCommand listBranchCommand = mock(ListBranchCommand.class);
    when(git.checkout()).thenReturn(checkoutCommand);
    when(git.branchList()).thenReturn(listBranchCommand);
    List<Ref> refs = new ArrayList<>();
    Ref ref = mock(Ref.class);
    refs.add(ref);
    when(ref.getName()).thenReturn("/master");
    when(listBranchCommand.call()).thenReturn(refs);
    // refresh()->merge
    MergeResult mergeResult = mock(MergeResult.class);
    MergeResult.MergeStatus mergeStatus = mock(MergeResult.MergeStatus.class);
    MergeCommand mergeCommand = mock(MergeCommand.class);
    when(git.merge()).thenReturn(mergeCommand);
    when(mergeCommand.call()).thenReturn(mergeResult);
    when(mergeResult.getMergeStatus()).thenReturn(mergeStatus);
    when(mergeStatus.isSuccessful()).thenReturn(true);
    // refresh()->return
    // git.getRepository().findRef("HEAD").getObjectId().getName();
    Ref headRef = mock(Ref.class);
    when(repository.findRef(anyString())).thenReturn(headRef);
    ObjectId newObjectId = ObjectId.fromRaw(new int[] { 1, 2, 3, 4, 5 });
    when(headRef.getObjectId()).thenReturn(newObjectId);
    SearchPathLocator.Locations locations = this.repository.getLocations("bar", "staging", "master");
    assertEquals(locations.getVersion(), newObjectId.getName());
    verify(deleteBranchCommand).setBranchNames(eq("feature/deletedBranchFromOrigin"));
    verify(deleteBranchCommand).setForce(true);
    verify(deleteBranchCommand).call();
}
Also used : ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) FetchResult(org.eclipse.jgit.transport.FetchResult) ArrayList(java.util.ArrayList) MergeResult(org.eclipse.jgit.api.MergeResult) MergeCommand(org.eclipse.jgit.api.MergeCommand) TrackingRefUpdate(org.eclipse.jgit.transport.TrackingRefUpdate) StoredConfig(org.eclipse.jgit.lib.StoredConfig) ListBranchCommand(org.eclipse.jgit.api.ListBranchCommand) CloneCommand(org.eclipse.jgit.api.CloneCommand) Status(org.eclipse.jgit.api.Status) NotMergedException(org.eclipse.jgit.api.errors.NotMergedException) DeleteBranchCommand(org.eclipse.jgit.api.DeleteBranchCommand) ObjectId(org.eclipse.jgit.lib.ObjectId) StatusCommand(org.eclipse.jgit.api.StatusCommand) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) FetchCommand(org.eclipse.jgit.api.FetchCommand) Test(org.junit.Test)

Example 15 with FetchResult

use of org.eclipse.jgit.transport.FetchResult in project fabric8 by jboss-fuse.

the class GitPatchManagementServiceImpl method fetchFabricPatchData.

/**
 * Fetches all refs from "fabric git repo" to "patch git repo" - very important operation that keeps
 * both repositories synchronized.
 * @param mainRepository
 * @throws GitAPIException
 */
private void fetchFabricPatchData(Git mainRepository) throws GitAPIException {
    if (mainRepository.getRepository().getConfig() != null) {
        String url = mainRepository.getRepository().getConfig().getString("remote", "origin", "url");
        if (url != null) {
            Activator.log(LogService.LOG_INFO, "Fetching data from " + url);
            // let's fetch from origin to check if someone else already does fabric patch management
            FetchResult result = mainRepository.fetch().setRemote("origin").setRefSpecs(new RefSpec("+refs/heads/*:refs/remotes/origin/*")).setTagOpt(TagOpt.FETCH_TAGS).call();
            Set<String> tags = new TreeSet<>();
            for (Ref ref : result.getAdvertisedRefs()) {
                if (ref.getName().startsWith("refs/tags/baseline")) {
                    tags.add(ref.getName().substring("refs/tags/".length()));
                }
            }
            Activator.log(LogService.LOG_INFO, "Available tags: " + tags);
        } else {
            Activator.log(LogService.LOG_WARNING, "Repository " + mainRepository.getRepository().getWorkTree() + " is not connected with Fabric git repository");
        }
    }
}
Also used : Ref(org.eclipse.jgit.lib.Ref) RefSpec(org.eclipse.jgit.transport.RefSpec) FetchResult(org.eclipse.jgit.transport.FetchResult) TreeSet(java.util.TreeSet)

Aggregations

FetchResult (org.eclipse.jgit.transport.FetchResult)22 Ref (org.eclipse.jgit.lib.Ref)8 FetchCommand (org.eclipse.jgit.api.FetchCommand)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 Git (org.eclipse.jgit.api.Git)6 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)6 StoredConfig (org.eclipse.jgit.lib.StoredConfig)6 RefSpec (org.eclipse.jgit.transport.RefSpec)6 ObjectId (org.eclipse.jgit.lib.ObjectId)5 Repository (org.eclipse.jgit.lib.Repository)5 CheckoutCommand (org.eclipse.jgit.api.CheckoutCommand)3 NoRemoteRepositoryException (org.eclipse.jgit.errors.NoRemoteRepositoryException)3 Test (org.junit.Test)3 GeneralSecurityException (java.security.GeneralSecurityException)2 CoreException (org.eclipse.core.runtime.CoreException)2 CloneCommand (org.eclipse.jgit.api.CloneCommand)2 ListBranchCommand (org.eclipse.jgit.api.ListBranchCommand)2 MergeCommand (org.eclipse.jgit.api.MergeCommand)2 Status (org.eclipse.jgit.api.Status)2