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();
}
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();
}
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;
}
}
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();
}
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);
}
}
Aggregations