Search in sources :

Example 6 with FetchCommand

use of org.eclipse.jgit.api.FetchCommand in project spring-cloud-config by spring-cloud.

the class JGitEnvironmentRepositoryTests method testMergeException.

@Test
public void testMergeException() throws Exception {
    Git git = mock(Git.class);
    CloneCommand cloneCommand = mock(CloneCommand.class);
    MockGitFactory factory = new MockGitFactory(git, cloneCommand);
    this.repository.setGitFactory(factory);
    // 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);
    when(git.fetch()).thenReturn(fetchCommand);
    when(fetchCommand.setRemote(anyString())).thenReturn(fetchCommand);
    when(fetchCommand.call()).thenReturn(fetchResult);
    when(fetchResult.getTrackingRefUpdates()).thenReturn(Collections.<TrackingRefUpdate>emptyList());
    // 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
    MergeCommand mergeCommand = mock(MergeCommand.class);
    when(git.merge()).thenReturn(mergeCommand);
    // here is our exception we are testing
    when(mergeCommand.call()).thenThrow(new NotMergedException());
    // 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(git, times(0)).branchDelete();
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) Status(org.eclipse.jgit.api.Status) NotMergedException(org.eclipse.jgit.api.errors.NotMergedException) CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) FetchResult(org.eclipse.jgit.transport.FetchResult) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) MergeCommand(org.eclipse.jgit.api.MergeCommand) StatusCommand(org.eclipse.jgit.api.StatusCommand) StoredConfig(org.eclipse.jgit.lib.StoredConfig) ListBranchCommand(org.eclipse.jgit.api.ListBranchCommand) 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 7 with FetchCommand

use of org.eclipse.jgit.api.FetchCommand in project spring-cloud-config by spring-cloud.

the class JGitEnvironmentRepositoryTests method testResetHardException.

@Test
public void testResetHardException() throws Exception {
    Git git = mock(Git.class);
    CloneCommand cloneCommand = mock(CloneCommand.class);
    MockGitFactory factory = new MockGitFactory(git, cloneCommand);
    this.repository.setGitFactory(factory);
    // 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).thenReturn(false);
    // refresh()->fetch
    FetchCommand fetchCommand = mock(FetchCommand.class);
    FetchResult fetchResult = mock(FetchResult.class);
    when(git.fetch()).thenReturn(fetchCommand);
    when(fetchCommand.setRemote(anyString())).thenReturn(fetchCommand);
    when(fetchCommand.call()).thenReturn(fetchResult);
    when(fetchResult.getTrackingRefUpdates()).thenReturn(Collections.<TrackingRefUpdate>emptyList());
    // 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
    MergeCommand mergeCommand = mock(MergeCommand.class);
    when(git.merge()).thenReturn(mergeCommand);
    // here
    when(mergeCommand.call()).thenThrow(new NotMergedException());
    // is
    // our
    // exception
    // we
    // are
    // testing
    // refresh()->hardReset
    ResetCommand resetCommand = mock(ResetCommand.class);
    when(git.reset()).thenReturn(resetCommand);
    when(resetCommand.call()).thenReturn(ref);
    // 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(git, times(0)).branchDelete();
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) Status(org.eclipse.jgit.api.Status) NotMergedException(org.eclipse.jgit.api.errors.NotMergedException) CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) FetchResult(org.eclipse.jgit.transport.FetchResult) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) MergeCommand(org.eclipse.jgit.api.MergeCommand) StatusCommand(org.eclipse.jgit.api.StatusCommand) StoredConfig(org.eclipse.jgit.lib.StoredConfig) ListBranchCommand(org.eclipse.jgit.api.ListBranchCommand) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) FetchCommand(org.eclipse.jgit.api.FetchCommand) ResetCommand(org.eclipse.jgit.api.ResetCommand) Test(org.junit.Test)

Example 8 with FetchCommand

use of org.eclipse.jgit.api.FetchCommand in project statecharts by Yakindu.

the class GitRepositoryExampleService method isUpToDate.

@Override
public boolean isUpToDate(IProgressMonitor monitor) {
    java.nio.file.Path storageLocation = getStorageLocation();
    try {
        FetchCommand fetch = Git.open(storageLocation.toFile()).fetch();
        FetchResult result = fetch.setProgressMonitor(new EclipseGitProgressTransformer(monitor)).setDryRun(true).call();
        Collection<TrackingRefUpdate> trackingRefUpdates = result.getTrackingRefUpdates();
        return trackingRefUpdates.size() == 0;
    } catch (RepositoryNotFoundException ex) {
        // This is the case when the examples are imported manually
        return true;
    } catch (Exception ex) {
        ex.printStackTrace();
        return true;
    }
}
Also used : FetchResult(org.eclipse.jgit.transport.FetchResult) FetchCommand(org.eclipse.jgit.api.FetchCommand) TrackingRefUpdate(org.eclipse.jgit.transport.TrackingRefUpdate) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException)

Example 9 with FetchCommand

use of org.eclipse.jgit.api.FetchCommand in project gerrit by GerritCodeReview.

the class GitUtil method fetch.

public static void fetch(TestRepository<?> testRepo, String spec) throws GitAPIException {
    FetchCommand fetch = testRepo.git().fetch();
    fetch.setRefSpecs(new RefSpec(spec));
    fetch.call();
}
Also used : RefSpec(org.eclipse.jgit.transport.RefSpec) FetchCommand(org.eclipse.jgit.api.FetchCommand)

Example 10 with FetchCommand

use of org.eclipse.jgit.api.FetchCommand in project blueocean-plugin by jenkinsci.

the class GitUtils method fetch.

public static void fetch(final Repository repo, final StandardCredentials credential) {
    try (org.eclipse.jgit.api.Git git = new org.eclipse.jgit.api.Git(repo)) {
        FetchCommand fetchCommand = git.fetch();
        addCredential(repo, fetchCommand, credential);
        fetchCommand.setRemote("origin").setRemoveDeletedRefs(true).setRefSpecs(new RefSpec("+refs/heads/*:refs/remotes/origin/*")).call();
    } catch (GitAPIException ex) {
        if (ex.getMessage().contains("Auth fail")) {
            throw new ServiceException.UnauthorizedException("Not authorized", ex);
        }
        throw new RuntimeException(ex);
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.jenkinsci.plugins.gitclient.Git) RefSpec(org.eclipse.jgit.transport.RefSpec) ServiceException(io.jenkins.blueocean.commons.ServiceException) FetchCommand(org.eclipse.jgit.api.FetchCommand)

Aggregations

FetchCommand (org.eclipse.jgit.api.FetchCommand)19 Git (org.eclipse.jgit.api.Git)13 FetchResult (org.eclipse.jgit.transport.FetchResult)9 ArrayList (java.util.ArrayList)6 CloneCommand (org.eclipse.jgit.api.CloneCommand)6 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)6 Ref (org.eclipse.jgit.lib.Ref)6 RefSpec (org.eclipse.jgit.transport.RefSpec)6 Test (org.junit.Test)6 CheckoutCommand (org.eclipse.jgit.api.CheckoutCommand)5 Repository (org.eclipse.jgit.lib.Repository)5 StoredConfig (org.eclipse.jgit.lib.StoredConfig)5 IOException (java.io.IOException)4 ListBranchCommand (org.eclipse.jgit.api.ListBranchCommand)4 MergeCommand (org.eclipse.jgit.api.MergeCommand)4 Status (org.eclipse.jgit.api.Status)4 StatusCommand (org.eclipse.jgit.api.StatusCommand)4 NotMergedException (org.eclipse.jgit.api.errors.NotMergedException)4 ObjectId (org.eclipse.jgit.lib.ObjectId)4 File (java.io.File)3