Search in sources :

Example 1 with CheckoutResult

use of org.eclipse.jgit.api.CheckoutResult in project jbosstools-openshift by jbosstools.

the class ImportProjectOperation method checkoutBranch.

protected void checkoutBranch(String gitRef, String gitUrl, List<IProject> importedProjects, File repositoryFolder, IProgressMonitor monitor) throws CoreException {
    if (!StringUtils.isEmpty(gitRef) && !CollectionUtils.isEmpty(importedProjects)) {
        // all projects are in the same repo
        IProject project = importedProjects.get(0);
        Repository repository = EGitUtils.getRepository(project);
        try {
            if (!EGitUtils.hasBranch(gitRef, repository)) {
                Pattern gitURIPattern = Pattern.compile(RegExUtils.escapeRegex(gitUrl));
                RemoteConfig remote = EGitUtils.getRemoteByUrl(gitURIPattern, repository);
                fetchBranch(gitRef, remote, repository, monitor);
            }
            CheckoutResult result = EGitUtils.checkoutBranch(gitRef, repository, monitor);
            switch(result.getStatus()) {
                case CONFLICTS:
                case ERROR:
                    throw new CoreException(StatusFactory.errorStatus(OpenShiftCoreActivator.PLUGIN_ID, NLS.bind("Could not check out the branch {0} of the (reused) local repository at {1} because of {2}." + " Please resolve the problem and check out the branch manually so that it matches the code that's used in your OpenShift application.", new Object[] { gitRef, repositoryFolder, result.getStatus().toString().toLowerCase() })));
                default:
            }
        } catch (IOException e) {
            throw new CoreException(StatusFactory.errorStatus(OpenShiftCoreActivator.PLUGIN_ID, NLS.bind("Could check that branch {0} exists within the (reused) local repository at {1}.", gitRef, repositoryFolder), e));
        } catch (InvocationTargetException e) {
            throw new CoreException(StatusFactory.errorStatus(OpenShiftCoreActivator.PLUGIN_ID, NLS.bind("Could not fetch branch {0} from check that branch {0} exists within the (reused) local repository at {1}.", gitRef, repositoryFolder), e));
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Repository(org.eclipse.jgit.lib.Repository) CoreException(org.eclipse.core.runtime.CoreException) CheckoutResult(org.eclipse.jgit.api.CheckoutResult) IOException(java.io.IOException) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig) IProject(org.eclipse.core.resources.IProject) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with CheckoutResult

use of org.eclipse.jgit.api.CheckoutResult in project jbosstools-openshift by jbosstools.

the class EGitUtilsTest method shouldCheckoutBranch.

@Test
public void shouldCheckoutBranch() throws Exception {
    // given file committed in custom branch
    String branch = testRepository.getCurrentBranch();
    testRepository.createAndCheckoutBranch(Constants.HEAD, REPO_BRANCH);
    String fileName = "a.txt";
    String fileContent = "adietish@redhat.com";
    IFile file = commitFile(fileName, fileContent, testProject, testRepository);
    testUtils.assertRepositoryContainsFilesWithContent(testRepository.getRepository(), new String[] { testUtils.getPathInRepository(file), fileContent });
    testRepository.checkoutBranch(branch);
    testUtils.assertRepositoryMisses(testRepository.getRepository(), testUtils.getPathInRepository(file));
    // when checkout custom branch
    CheckoutResult result = EGitUtils.branch(REPO_BRANCH, testProject.getProject(), null);
    // then file should be present
    assertTrue(result.getStatus() == CheckoutResult.Status.OK);
    assertTrue(testRepository.isCurrentBranch(REPO_BRANCH));
    testUtils.assertRepositoryContainsFilesWithContent(testRepository.getRepository(), new String[] { testUtils.getPathInRepository(file), fileContent });
}
Also used : IFile(org.eclipse.core.resources.IFile) CheckoutResult(org.eclipse.jgit.api.CheckoutResult) Test(org.junit.Test)

Example 3 with CheckoutResult

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

the class FeatureCheckoutHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final GitFlowRepository gfRepo = GitFlowHandlerUtil.getRepository(event);
    if (gfRepo == null) {
        return error(UIText.Handlers_noGitflowRepositoryFound);
    }
    Repository repository = gfRepo.getRepository();
    final List<Ref> refs = gfRepo.getFeatureBranches();
    FeatureBranchSelectionDialog dialog = new FeatureBranchSelectionDialog(HandlerUtil.getActiveShell(event), refs, UIText.FeatureCheckoutHandler_selectFeature, UIText.FeatureCheckoutHandler_localFeatures, Constants.R_HEADS + gfRepo.getConfig().getFeaturePrefix(), gfRepo);
    if (dialog.open() != Window.OK) {
        return null;
    }
    final Ref ref = dialog.getSelectedNode();
    try {
        String featureName = gfRepo.getFeatureBranchName(ref);
        // TODO: consider using BranchOperationUI because checkout can take
        // a long time on large repositories
        FeatureCheckoutOperation checkoutOperation = new FeatureCheckoutOperation(gfRepo, featureName);
        JobUtil.scheduleUserWorkspaceJob(checkoutOperation, UIText.FeatureCheckoutHandler_checkingOutFeature, JobFamilies.GITFLOW_FAMILY);
        IJobManager jobMan = Job.getJobManager();
        try {
            jobMan.join(GITFLOW_FAMILY, null);
        } catch (OperationCanceledException | InterruptedException e) {
            return error(e.getMessage(), e);
        }
        CheckoutResult result = checkoutOperation.getResult();
        if (!CheckoutResult.Status.OK.equals(result.getStatus())) {
            Shell shell = HandlerUtil.getActiveShell(event);
            if (!handleUncommittedFiles(gfRepo.getRepository(), shell, repository.getWorkTree().getName())) {
                return Status.CANCEL_STATUS;
            } else {
                JobUtil.scheduleUserWorkspaceJob(checkoutOperation, UIText.FeatureCheckoutHandler_checkingOutFeature, JobFamilies.GITFLOW_FAMILY);
            }
        }
    } catch (GitAPIException e) {
        throw new RuntimeException(e);
    }
    return null;
}
Also used : FeatureCheckoutOperation(org.eclipse.egit.gitflow.op.FeatureCheckoutOperation) FeatureBranchSelectionDialog(org.eclipse.egit.gitflow.ui.internal.dialogs.FeatureBranchSelectionDialog) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IJobManager(org.eclipse.core.runtime.jobs.IJobManager) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitFlowRepository(org.eclipse.egit.gitflow.GitFlowRepository) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) Shell(org.eclipse.swt.widgets.Shell) CheckoutResult(org.eclipse.jgit.api.CheckoutResult) GitFlowRepository(org.eclipse.egit.gitflow.GitFlowRepository)

Example 4 with CheckoutResult

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

the class FeatureTrackOperation method execute.

@Override
public void execute(IProgressMonitor monitor) throws CoreException {
    SubMonitor progress = SubMonitor.convert(monitor, 3);
    try {
        String newLocalBranch = repository.getConfig().getFeatureBranchName(featureName);
        operationResult = fetch(progress.newChild(1), timeout);
        if (repository.hasBranch(newLocalBranch)) {
            String errorMessage = String.format(CoreText.FeatureTrackOperation_localBranchExists, newLocalBranch);
            throw new CoreException(error(errorMessage));
        }
        CreateLocalBranchOperation createLocalBranchOperation = new CreateLocalBranchOperation(repository.getRepository(), newLocalBranch, remoteFeature, BranchRebaseMode.NONE);
        createLocalBranchOperation.execute(progress.newChild(1));
        BranchOperation branchOperation = new BranchOperation(repository.getRepository(), newLocalBranch);
        branchOperation.execute(progress.newChild(1));
        CheckoutResult result = branchOperation.getResult();
        if (!Status.OK.equals(result.getStatus())) {
            String errorMessage = String.format(CoreText.FeatureTrackOperation_checkoutReturned, newLocalBranch, result.getStatus().name());
            throw new CoreException(error(errorMessage));
        }
        try {
            repository.setRemote(newLocalBranch, DEFAULT_REMOTE_NAME);
            repository.setUpstreamBranchName(newLocalBranch, repository.getConfig().getFullFeatureBranchName(featureName));
        } catch (IOException e) {
            throw new CoreException(error(CoreText.FeatureTrackOperation_unableToStoreGitConfig, e));
        }
    } catch (URISyntaxException e) {
        throw new CoreException(error(e.getMessage(), e));
    } catch (InvocationTargetException e) {
        Throwable targetException = e.getTargetException();
        throw new CoreException(error(targetException.getMessage(), targetException));
    } catch (GitAPIException e) {
        throw new CoreException(error(e.getMessage(), e));
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) CoreException(org.eclipse.core.runtime.CoreException) CreateLocalBranchOperation(org.eclipse.egit.core.op.CreateLocalBranchOperation) BranchOperation(org.eclipse.egit.core.op.BranchOperation) SubMonitor(org.eclipse.core.runtime.SubMonitor) CheckoutResult(org.eclipse.jgit.api.CheckoutResult) CreateLocalBranchOperation(org.eclipse.egit.core.op.CreateLocalBranchOperation) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

CheckoutResult (org.eclipse.jgit.api.CheckoutResult)4 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 CoreException (org.eclipse.core.runtime.CoreException)2 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)2 Repository (org.eclipse.jgit.lib.Repository)2 URISyntaxException (java.net.URISyntaxException)1 Pattern (java.util.regex.Pattern)1 IFile (org.eclipse.core.resources.IFile)1 IProject (org.eclipse.core.resources.IProject)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 IJobManager (org.eclipse.core.runtime.jobs.IJobManager)1 BranchOperation (org.eclipse.egit.core.op.BranchOperation)1 CreateLocalBranchOperation (org.eclipse.egit.core.op.CreateLocalBranchOperation)1 GitFlowRepository (org.eclipse.egit.gitflow.GitFlowRepository)1 FeatureCheckoutOperation (org.eclipse.egit.gitflow.op.FeatureCheckoutOperation)1 FeatureBranchSelectionDialog (org.eclipse.egit.gitflow.ui.internal.dialogs.FeatureBranchSelectionDialog)1 Ref (org.eclipse.jgit.lib.Ref)1 RemoteConfig (org.eclipse.jgit.transport.RemoteConfig)1