Search in sources :

Example 6 with CheckoutCommand

use of org.eclipse.jgit.api.CheckoutCommand in project winery by eclipse.

the class GitBasedRepository method checkout.

public void checkout(String branchName) throws GitAPIException {
    try (Git git = getGit()) {
        CheckoutCommand checkout = git.checkout();
        checkout.setName(branchName);
        checkout.call();
    } catch (IOException e) {
        LOGGER.error("Error checking out.", e);
    }
}
Also used : CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) Git(org.eclipse.jgit.api.Git) IOException(java.io.IOException)

Example 7 with CheckoutCommand

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

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

the class JGitEnvironmentRepositoryTests method testFetchException.

@Test
public void testFetchException() 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);
    when(git.fetch()).thenReturn(fetchCommand);
    when(fetchCommand.setRemote(anyString())).thenReturn(fetchCommand);
    // here
    when(fetchCommand.call()).thenThrow(new InvalidRemoteException("invalid mock remote"));
    // 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
    MergeCommand mergeCommand = mock(MergeCommand.class);
    when(git.merge()).thenReturn(mergeCommand);
    // here
    when(mergeCommand.call()).thenThrow(new NotMergedException());
    // is
    // our
    // exception
    // we
    // are
    // testing
    // 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", null);
    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) 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) InvalidRemoteException(org.eclipse.jgit.api.errors.InvalidRemoteException) Test(org.junit.Test)

Example 9 with CheckoutCommand

use of org.eclipse.jgit.api.CheckoutCommand in project archi-modelrepository-plugin by archi-contribs.

the class MergeConflictHandler method checkout.

// Check out conflicting files either from us or them
private void checkout(Git git, Stage stage, List<String> paths) throws GitAPIException {
    CheckoutCommand checkoutCommand = git.checkout();
    checkoutCommand.setStage(stage);
    checkoutCommand.addPaths(paths);
    checkoutCommand.call();
}
Also used : CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand)

Example 10 with CheckoutCommand

use of org.eclipse.jgit.api.CheckoutCommand in project egit by eclipse.

the class BranchOperation method execute.

@Override
public void execute(IProgressMonitor m) throws CoreException {
    IWorkspaceRunnable action = new IWorkspaceRunnable() {

        @Override
        public void run(IProgressMonitor pm) throws CoreException {
            SubMonitor progress = SubMonitor.convert(pm, 4);
            preExecute(progress.newChild(1));
            closeProjectsMissingAfterCheckout(progress);
            try (Git git = new Git(repository)) {
                CheckoutCommand co = git.checkout();
                co.setName(target);
                try {
                    co.call();
                } catch (CheckoutConflictException e) {
                    return;
                } catch (JGitInternalException e) {
                    throw new CoreException(Activator.error(e.getMessage(), e));
                } catch (GitAPIException e) {
                    throw new CoreException(Activator.error(e.getMessage(), e));
                } finally {
                    result = co.getResult();
                }
                if (result.getStatus() == Status.NONDELETED) {
                    retryDelete(result.getUndeletedList());
                }
                progress.worked(1);
                refreshAffectedProjects(progress);
                postExecute(progress.newChild(1));
            }
        }

        private void closeProjectsMissingAfterCheckout(SubMonitor progress) throws CoreException {
            IProject[] missing = getMissingProjects(target, ProjectUtil.getValidOpenProjects(repository));
            progress.setTaskName(NLS.bind(CoreText.BranchOperation_performingBranch, target));
            progress.setWorkRemaining(missing.length > 0 ? 4 : 3);
            if (missing.length > 0) {
                SubMonitor closeMonitor = progress.newChild(1);
                closeMonitor.setWorkRemaining(missing.length);
                for (IProject project : missing) {
                    closeMonitor.subTask(MessageFormat.format(CoreText.BranchOperation_closingMissingProject, project.getName()));
                    project.close(closeMonitor.newChild(1));
                }
            }
        }

        private void refreshAffectedProjects(SubMonitor progress) throws CoreException {
            List<String> pathsToHandle = new ArrayList<String>();
            pathsToHandle.addAll(result.getModifiedList());
            pathsToHandle.addAll(result.getRemovedList());
            pathsToHandle.addAll(result.getConflictList());
            IProject[] refreshProjects = ProjectUtil.getProjectsContaining(repository, pathsToHandle);
            ProjectUtil.refreshValidProjects(refreshProjects, delete, progress.newChild(1));
        }
    };
    // lock workspace to protect working tree changes
    ResourcesPlugin.getWorkspace().run(action, getSchedulingRule(), IWorkspace.AVOID_UPDATE, m);
}
Also used : IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) SubMonitor(org.eclipse.core.runtime.SubMonitor) ArrayList(java.util.ArrayList) CheckoutConflictException(org.eclipse.jgit.api.errors.CheckoutConflictException) IProject(org.eclipse.core.resources.IProject) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Git(org.eclipse.jgit.api.Git) CoreException(org.eclipse.core.runtime.CoreException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException)

Aggregations

CheckoutCommand (org.eclipse.jgit.api.CheckoutCommand)14 Git (org.eclipse.jgit.api.Git)10 Ref (org.eclipse.jgit.lib.Ref)7 ArrayList (java.util.ArrayList)6 CloneCommand (org.eclipse.jgit.api.CloneCommand)5 FetchCommand (org.eclipse.jgit.api.FetchCommand)5 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)5 Repository (org.eclipse.jgit.lib.Repository)5 File (java.io.File)4 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 JGitInternalException (org.eclipse.jgit.api.errors.JGitInternalException)4 NotMergedException (org.eclipse.jgit.api.errors.NotMergedException)4 ObjectId (org.eclipse.jgit.lib.ObjectId)4 StoredConfig (org.eclipse.jgit.lib.StoredConfig)4 Test (org.junit.Test)4 FetchResult (org.eclipse.jgit.transport.FetchResult)3