Search in sources :

Example 6 with MergeResult

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

the class GitFlowOperation method mergeTo.

/**
 * @param monitor
 * @param branchName
 * @param targetBranchName
 * @param squash
 * @param fastForwardSingleCommit Has no effect if {@code squash} is true.
 * @return result of merging back to targetBranchName
 * @throws CoreException
 * @since 4.1
 */
@NonNull
protected MergeResult mergeTo(IProgressMonitor monitor, String branchName, String targetBranchName, boolean squash, boolean fastForwardSingleCommit) throws CoreException {
    try {
        if (!repository.hasBranch(targetBranchName)) {
            throw new RuntimeException(String.format(CoreText.GitFlowOperation_branchNotFound, targetBranchName));
        }
        SubMonitor progress = SubMonitor.convert(monitor, 2);
        boolean dontCloseProjects = false;
        BranchOperation branchOperation = new BranchOperation(repository.getRepository(), targetBranchName, dontCloseProjects);
        branchOperation.execute(progress.newChild(1));
        Status status = branchOperation.getResult().getStatus();
        if (!CheckoutResult.Status.OK.equals(status)) {
            throw new CoreException(error(format(CoreText.GitFlowOperation_unableToCheckout, branchName, status.toString())));
        }
        MergeOperation mergeOperation = new MergeOperation(repository.getRepository(), branchName);
        mergeOperation.setSquash(squash);
        if (squash) {
            mergeOperation.setCommit(true);
        }
        if (!squash && (!fastForwardSingleCommit || hasMultipleCommits(branchName))) {
            mergeOperation.setFastForwardMode(NO_FF);
        }
        mergeOperation.execute(progress.newChild(1));
        MergeResult result = mergeOperation.getResult();
        if (result == null) {
            throw new CoreException(error(format(CoreText.GitFlowOperation_unableToMerge, branchName, targetBranchName)));
        }
        return result;
    } catch (GitAPIException | IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Status(org.eclipse.jgit.api.CheckoutResult.Status) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) MergeOperation(org.eclipse.egit.core.op.MergeOperation) CoreException(org.eclipse.core.runtime.CoreException) CreateLocalBranchOperation(org.eclipse.egit.core.op.CreateLocalBranchOperation) DeleteBranchOperation(org.eclipse.egit.core.op.DeleteBranchOperation) BranchOperation(org.eclipse.egit.core.op.BranchOperation) SubMonitor(org.eclipse.core.runtime.SubMonitor) MergeResult(org.eclipse.jgit.api.MergeResult) IOException(java.io.IOException) NonNull(org.eclipse.jgit.annotations.NonNull)

Example 7 with MergeResult

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

the class HotfixFinishHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final GitFlowRepository gfRepo = GitFlowHandlerUtil.getRepository(event);
    if (gfRepo == null) {
        return error(UIText.Handlers_noGitflowRepositoryFound);
    }
    HotfixFinishOperation hotfixFinishOperation;
    try {
        hotfixFinishOperation = new HotfixFinishOperation(gfRepo);
        String hotfixBranch = gfRepo.getRepository().getBranch();
        String develop = gfRepo.getConfig().getDevelop();
        JobUtil.scheduleUserWorkspaceJob(hotfixFinishOperation, UIText.HotfixFinishHandler_finishingHotfix, JobFamilies.GITFLOW_FAMILY);
        IJobManager jobMan = Job.getJobManager();
        jobMan.join(JobFamilies.GITFLOW_FAMILY, null);
        MergeResult mergeResult = hotfixFinishOperation.getMergeResult();
        MergeStatus mergeStatus = mergeResult.getMergeStatus();
        if (!MergeStatus.CONFLICTING.equals(mergeStatus)) {
            return null;
        }
        if (handleConflictsOnMaster(gfRepo)) {
            return null;
        }
        MultiStatus status = createMergeConflictInfo(develop, hotfixBranch, mergeResult);
        ErrorDialog.openError(null, UIText.HotfixFinishHandler_Conflicts, null, status);
    } catch (WrongGitFlowStateException | CoreException | IOException | OperationCanceledException | InterruptedException e) {
        return error(e.getMessage(), e);
    }
    return null;
}
Also used : OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) MergeResult(org.eclipse.jgit.api.MergeResult) MultiStatus(org.eclipse.core.runtime.MultiStatus) IJobManager(org.eclipse.core.runtime.jobs.IJobManager) IOException(java.io.IOException) CoreException(org.eclipse.core.runtime.CoreException) MergeStatus(org.eclipse.jgit.api.MergeResult.MergeStatus) WrongGitFlowStateException(org.eclipse.egit.gitflow.WrongGitFlowStateException) GitFlowRepository(org.eclipse.egit.gitflow.GitFlowRepository) HotfixFinishOperation(org.eclipse.egit.gitflow.op.HotfixFinishOperation)

Example 8 with MergeResult

use of org.eclipse.jgit.api.MergeResult in project repairnator by Spirals-Team.

the class GitHelper method mergeTwoCommitsForPR.

public boolean mergeTwoCommitsForPR(Git git, Build build, PRInformation prInformation, String repository, AbstractStep step, List<String> pathes) {
    try {
        String remoteBranchPath = CloneRepository.GITHUB_ROOT_REPO + prInformation.getOtherRepo().getSlug() + ".git";
        RemoteAddCommand remoteBranchCommand = git.remoteAdd();
        remoteBranchCommand.setName("PR");
        remoteBranchCommand.setUri(new URIish(remoteBranchPath));
        remoteBranchCommand.call();
        git.fetch().setRemote("PR").call();
        String commitHeadSha = this.testCommitExistence(git, prInformation.getHead().getSha(), step, build);
        String commitBaseSha = this.testCommitExistence(git, prInformation.getBase().getSha(), step, build);
        if (commitHeadSha == null) {
            step.addStepError("Commit head ref cannot be retrieved in the repository: " + prInformation.getHead().getSha() + ". Operation aborted.");
            this.getLogger().debug("Step " + step.getName() + " - " + prInformation.getHead().toString());
            return false;
        }
        if (commitBaseSha == null) {
            step.addStepError("Commit base ref cannot be retrieved in the repository: " + prInformation.getBase().getSha() + ". Operation aborted.");
            this.getLogger().debug("Step " + step.getName() + " - " + prInformation.getBase().toString());
            return false;
        }
        this.getLogger().debug("Step " + step.getName() + " - Get the commit " + commitHeadSha + " for repo " + repository);
        if (pathes != null) {
            git.checkout().setName(commitHeadSha).addPaths(pathes).call();
            PersonIdent personIdent = new PersonIdent("Luc Esape", "luc.esape@gmail.com");
            git.commit().setMessage("Undo changes on source code").setAuthor(personIdent).setCommitter(personIdent).call();
        } else {
            git.checkout().setName(commitHeadSha).call();
        }
        RevWalk revwalk = new RevWalk(git.getRepository());
        RevCommit revCommitBase = revwalk.lookupCommit(git.getRepository().resolve(commitBaseSha));
        this.getLogger().debug("Step " + step.getName() + " - Do the merge with the PR commit for repo " + repository);
        MergeResult result = git.merge().include(revCommitBase).setFastForward(MergeCommand.FastForwardMode.NO_FF).call();
        this.nbCommits++;
    } catch (Exception e) {
        step.addStepError(e.getMessage());
        this.getLogger().error("Step " + step.getName() + " - Repository " + repository + " cannot be cloned.", e);
        return false;
    }
    return true;
}
Also used : URIish(org.eclipse.jgit.transport.URIish) PersonIdent(org.eclipse.jgit.lib.PersonIdent) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) MergeResult(org.eclipse.jgit.api.MergeResult) RevWalk(org.eclipse.jgit.revwalk.RevWalk) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) PatchApplyException(org.eclipse.jgit.api.errors.PatchApplyException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) PatchFormatException(org.eclipse.jgit.api.errors.PatchFormatException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 9 with MergeResult

use of org.eclipse.jgit.api.MergeResult in project bndtools by bndtools.

the class GitOBRRepo method put.

@Override
public synchronized PutResult put(InputStream stream, PutOptions options) throws Exception {
    init();
    try {
        repository.incrementOpen();
        Git git = Git.wrap(repository);
        // Pull remote repository
        PullResult pullResult = git.pull().call();
        // Check result
        MergeResult mergeResult = pullResult.getMergeResult();
        if (mergeResult != null && (mergeResult.getMergeStatus() == MergeStatus.CONFLICTING || mergeResult.getMergeStatus() == MergeStatus.FAILED)) {
            // TODO: How to report failure
            throw new RuntimeException(String.format("Failed to merge changes from %s", gitUri));
        }
        // TODO: Check if jar already exists, is it ok to overwrite in all repositories?
        PutResult result = super.put(stream, options);
        if (result.artifact != null) {
            File newFile = new File(result.artifact);
            // Add, Commit and Push
            for (IRepositoryContentProvider provider : generatingProviders) {
                if (!provider.supportsGeneration())
                    continue;
                git.add().addFilepattern(getRelativePath(gitRootDir, newFile)).addFilepattern(getRelativePath(gitRootDir, new File(provider.getDefaultIndexName(pretty)))).call();
            }
            git.commit().setMessage("bndtools added bundle : " + getRelativePath(gitRootDir, newFile)).call();
            git.push().setCredentialsProvider(CredentialsProvider.getDefault()).call();
            // Re-read the index
            reset();
            init();
        }
        return result;
    } finally {
        if (repository != null) {
            repository.close();
        }
    }
}
Also used : Git(org.eclipse.jgit.api.Git) MergeResult(org.eclipse.jgit.api.MergeResult) IRepositoryContentProvider(aQute.bnd.deployer.repository.api.IRepositoryContentProvider) File(java.io.File) PullResult(org.eclipse.jgit.api.PullResult)

Example 10 with MergeResult

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

the class GitUtils method merge.

public static void merge(final Repository repo, final String localRef, final String remoteRef) {
    try (org.eclipse.jgit.api.Git git = new org.eclipse.jgit.api.Git(repo)) {
        CheckoutCommand checkoutCommand = git.checkout();
        checkoutCommand.setCreateBranch(false);
        checkoutCommand.setName(localRef);
        checkoutCommand.call();
        Ref mergeBranchRef = repo.exactRef(remoteRef);
        MergeResult merge = git.merge().include(mergeBranchRef).setFastForward(MergeCommand.FastForwardMode.FF_ONLY).call();
        if (merge.getConflicts() != null) {
            throw new RuntimeException("Merge has conflicts");
        }
    } catch (Exception e) {
        throw new ServiceException.UnexpectedErrorException("Unable to merge: " + remoteRef + " to: " + localRef, e);
    }
}
Also used : CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) MergeResult(org.eclipse.jgit.api.MergeResult) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitException(hudson.plugins.git.GitException) JSchException(com.jcraft.jsch.JSchException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConcurrentRefUpdateException(org.eclipse.jgit.api.errors.ConcurrentRefUpdateException) IOException(java.io.IOException) ServiceException(io.jenkins.blueocean.commons.ServiceException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) Ref(org.eclipse.jgit.lib.Ref) Git(org.jenkinsci.plugins.gitclient.Git) ServiceException(io.jenkins.blueocean.commons.ServiceException)

Aggregations

MergeResult (org.eclipse.jgit.api.MergeResult)22 IOException (java.io.IOException)10 Test (org.junit.Test)9 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)7 RevCommit (org.eclipse.jgit.revwalk.RevCommit)7 Git (org.eclipse.jgit.api.Git)6 CoreException (org.eclipse.core.runtime.CoreException)4 IndexDiffData (org.eclipse.egit.core.internal.indexdiff.IndexDiffData)4 MergeCommand (org.eclipse.jgit.api.MergeCommand)3 MergeStatus (org.eclipse.jgit.api.MergeResult.MergeStatus)3 Ref (org.eclipse.jgit.lib.Ref)3 File (java.io.File)2 IFile (org.eclipse.core.resources.IFile)2 SubMonitor (org.eclipse.core.runtime.SubMonitor)2 CheckoutCommand (org.eclipse.jgit.api.CheckoutCommand)2 CloneCommand (org.eclipse.jgit.api.CloneCommand)2 NoRemoteRepositoryException (org.eclipse.jgit.errors.NoRemoteRepositoryException)2 ObjectId (org.eclipse.jgit.lib.ObjectId)2 Repository (org.eclipse.jgit.lib.Repository)2 StoredConfig (org.eclipse.jgit.lib.StoredConfig)2