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