Search in sources :

Example 1 with StatusCommand

use of org.eclipse.jgit.api.StatusCommand in project Android-Password-Store by zeapo.

the class GitAsyncTask method doInBackground.

@Override
protected String doInBackground(GitCommand... commands) {
    Integer nbChanges = null;
    for (GitCommand command : commands) {
        Log.d("doInBackground", "Executing the command <" + command.toString() + ">");
        try {
            if (command instanceof StatusCommand) {
                // in case we have changes, we want to keep track of it
                org.eclipse.jgit.api.Status status = ((StatusCommand) command).call();
                nbChanges = status.getChanged().size() + status.getMissing().size();
            } else if (command instanceof CommitCommand) {
                // the previous status will eventually be used to avoid a commit
                if (nbChanges == null || nbChanges > 0)
                    command.call();
            } else if (command instanceof PushCommand) {
                for (final PushResult result : ((PushCommand) command).call()) {
                    // Code imported (modified) from Gerrit PushOp, license Apache v2
                    for (final RemoteRefUpdate rru : result.getRemoteUpdates()) {
                        switch(rru.getStatus()) {
                            case REJECTED_NONFASTFORWARD:
                                return activity.getString(R.string.git_push_nff_error);
                            case REJECTED_NODELETE:
                            case REJECTED_REMOTE_CHANGED:
                            case NON_EXISTING:
                            case NOT_ATTEMPTED:
                                return activity.getString(R.string.git_push_generic_error) + rru.getStatus().name();
                            case REJECTED_OTHER_REASON:
                                if ("non-fast-forward".equals(rru.getMessage())) {
                                    return activity.getString(R.string.git_push_other_error);
                                } else {
                                    return activity.getString(R.string.git_push_generic_error) + rru.getMessage();
                                }
                            default:
                                break;
                        }
                    }
                }
            } else {
                command.call();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage() + "\nCaused by:\n" + e.getCause();
        }
    }
    return "";
}
Also used : RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) CommitCommand(org.eclipse.jgit.api.CommitCommand) PushResult(org.eclipse.jgit.transport.PushResult) StatusCommand(org.eclipse.jgit.api.StatusCommand) PushCommand(org.eclipse.jgit.api.PushCommand) GitCommand(org.eclipse.jgit.api.GitCommand)

Example 2 with StatusCommand

use of org.eclipse.jgit.api.StatusCommand 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 3 with StatusCommand

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

the class JGitEnvironmentRepositoryTests method shouldPullClean.

@Test
public void shouldPullClean() throws Exception {
    Git git = mock(Git.class);
    StatusCommand statusCommand = mock(StatusCommand.class);
    Status status = mock(Status.class);
    Repository repository = mock(Repository.class);
    StoredConfig storedConfig = mock(StoredConfig.class);
    when(git.status()).thenReturn(statusCommand);
    when(git.getRepository()).thenReturn(repository);
    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);
    JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment, new JGitEnvironmentProperties());
    boolean shouldPull = repo.shouldPull(git);
    assertThat("shouldPull was false", shouldPull, is(true));
}
Also used : Status(org.eclipse.jgit.api.Status) StoredConfig(org.eclipse.jgit.lib.StoredConfig) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) StatusCommand(org.eclipse.jgit.api.StatusCommand) Test(org.junit.Test)

Example 4 with StatusCommand

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

the class JGitEnvironmentRepositoryTests method shouldPullNotClean.

@Test
public void shouldPullNotClean() throws Exception {
    Git git = mock(Git.class);
    StatusCommand statusCommand = mock(StatusCommand.class);
    Status status = mock(Status.class);
    Repository repository = mock(Repository.class);
    StoredConfig storedConfig = mock(StoredConfig.class);
    when(git.status()).thenReturn(statusCommand);
    when(git.getRepository()).thenReturn(repository);
    when(repository.getConfig()).thenReturn(storedConfig);
    when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git");
    when(statusCommand.call()).thenReturn(status);
    when(status.isClean()).thenReturn(false);
    JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment, new JGitEnvironmentProperties());
    boolean shouldPull = repo.shouldPull(git);
    assertThat("shouldPull was true", shouldPull, is(false));
}
Also used : Status(org.eclipse.jgit.api.Status) StoredConfig(org.eclipse.jgit.lib.StoredConfig) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) StatusCommand(org.eclipse.jgit.api.StatusCommand) Test(org.junit.Test)

Example 5 with StatusCommand

use of org.eclipse.jgit.api.StatusCommand 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)

Aggregations

StatusCommand (org.eclipse.jgit.api.StatusCommand)9 Git (org.eclipse.jgit.api.Git)7 Status (org.eclipse.jgit.api.Status)7 Repository (org.eclipse.jgit.lib.Repository)7 StoredConfig (org.eclipse.jgit.lib.StoredConfig)7 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)4 CheckoutCommand (org.eclipse.jgit.api.CheckoutCommand)4 CloneCommand (org.eclipse.jgit.api.CloneCommand)4 FetchCommand (org.eclipse.jgit.api.FetchCommand)4 ListBranchCommand (org.eclipse.jgit.api.ListBranchCommand)4 MergeCommand (org.eclipse.jgit.api.MergeCommand)4 NotMergedException (org.eclipse.jgit.api.errors.NotMergedException)4 ObjectId (org.eclipse.jgit.lib.ObjectId)4 Ref (org.eclipse.jgit.lib.Ref)4 FetchResult (org.eclipse.jgit.transport.FetchResult)3 IOException (java.io.IOException)1 CommitCommand (org.eclipse.jgit.api.CommitCommand)1 DeleteBranchCommand (org.eclipse.jgit.api.DeleteBranchCommand)1 GitCommand (org.eclipse.jgit.api.GitCommand)1