use of org.eclipse.jgit.merge.MergeStrategy in project egit by eclipse.
the class PullOperation method execute.
@Override
public void execute(IProgressMonitor m) throws CoreException {
if (!results.isEmpty())
throw new CoreException(new Status(IStatus.ERROR, Activator.getPluginId(), CoreText.OperationAlreadyExecuted));
SubMonitor totalProgress = SubMonitor.convert(m, NLS.bind(CoreText.PullOperation_TaskName, Integer.valueOf(repositories.length)), 1);
IWorkspaceRunnable action = new IWorkspaceRunnable() {
@Override
public void run(IProgressMonitor mymonitor) throws CoreException {
if (mymonitor.isCanceled())
throw new CoreException(Status.CANCEL_STATUS);
SubMonitor progress = SubMonitor.convert(mymonitor, repositories.length * 2);
for (int i = 0; i < repositories.length; i++) {
Repository repository = repositories[i];
IProject[] validProjects = ProjectUtil.getValidOpenProjects(repository);
PullResult pullResult = null;
try (Git git = new Git(repository)) {
PullCommand pull = git.pull();
SubMonitor newChild = progress.newChild(1, SubMonitor.SUPPRESS_NONE);
pull.setProgressMonitor(new EclipseGitProgressTransformer(newChild));
pull.setTimeout(timeout);
pull.setCredentialsProvider(credentialsProvider);
PullReferenceConfig config = configs.get(repository);
newChild.setTaskName(getPullTaskName(repository, config));
if (config != null) {
if (config.getRemote() != null) {
pull.setRemote(config.getRemote());
}
if (config.getReference() != null) {
pull.setRemoteBranchName(config.getReference());
}
pull.setRebase(config.getUpstreamConfig());
}
MergeStrategy strategy = Activator.getDefault().getPreferredMergeStrategy();
if (strategy != null) {
pull.setStrategy(strategy);
}
pullResult = pull.call();
results.put(repository, pullResult);
} catch (DetachedHeadException e) {
results.put(repository, Activator.error(CoreText.PullOperation_DetachedHeadMessage, e));
} catch (InvalidConfigurationException e) {
IStatus error = Activator.error(CoreText.PullOperation_PullNotConfiguredMessage, e);
results.put(repository, error);
} catch (GitAPIException e) {
results.put(repository, Activator.error(e.getMessage(), e));
} catch (JGitInternalException e) {
Throwable cause = e.getCause();
if (cause == null || !(cause instanceof TransportException))
cause = e;
results.put(repository, Activator.error(cause.getMessage(), cause));
} finally {
if (refreshNeeded(pullResult)) {
ProjectUtil.refreshValidProjects(validProjects, progress.newChild(1, SubMonitor.SUPPRESS_NONE));
} else {
progress.worked(1);
}
}
}
}
};
// lock workspace to protect working tree changes
ResourcesPlugin.getWorkspace().run(action, getSchedulingRule(), IWorkspace.AVOID_UPDATE, totalProgress);
}
use of org.eclipse.jgit.merge.MergeStrategy in project egit by eclipse.
the class RevertCommitOperation 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, 2);
progress.subTask(MessageFormat.format(CoreText.RevertCommitOperation_reverting, Integer.valueOf(commits.size())));
try (Git git = new Git(repo)) {
RevertCommand command = git.revert();
MergeStrategy strategy = Activator.getDefault().getPreferredMergeStrategy();
if (strategy != null) {
command.setStrategy(strategy);
}
for (RevCommit commit : commits) {
command.include(commit);
}
newHead = command.call();
reverted = command.getRevertedRefs();
result = command.getFailingResult();
progress.worked(1);
ProjectUtil.refreshValidProjects(ProjectUtil.getValidOpenProjects(repo), progress.newChild(1));
} catch (GitAPIException e) {
throw new TeamException(e.getLocalizedMessage(), e.getCause());
}
}
};
ResourcesPlugin.getWorkspace().run(action, getSchedulingRule(), IWorkspace.AVOID_UPDATE, m);
}
use of org.eclipse.jgit.merge.MergeStrategy in project gerrit by GerritCodeReview.
the class MergeUtil method createMergeCommit.
public static CodeReviewCommit createMergeCommit(ObjectInserter inserter, Config repoConfig, RevCommit mergeTip, RevCommit originalCommit, String mergeStrategy, boolean allowConflicts, PersonIdent authorIdent, PersonIdent committerIdent, String commitMsg, CodeReviewRevWalk rw) throws IOException, MergeIdenticalTreeException, MergeConflictException, InvalidMergeStrategyException {
if (!MergeStrategy.THEIRS.getName().equals(mergeStrategy) && rw.isMergedInto(originalCommit, mergeTip)) {
throw new ChangeAlreadyMergedException("'" + originalCommit.getName() + "' has already been merged");
}
Merger m = newMerger(inserter, repoConfig, mergeStrategy);
DirCache dc = DirCache.newInCore();
if (allowConflicts && m instanceof ResolveMerger) {
// The DirCache must be set on ResolveMerger before calling
// ResolveMerger#merge(AnyObjectId...) otherwise the entries in DirCache don't get populated.
((ResolveMerger) m).setDirCache(dc);
}
ObjectId tree;
ImmutableSet<String> filesWithGitConflicts;
if (m.merge(false, mergeTip, originalCommit)) {
filesWithGitConflicts = null;
tree = m.getResultTreeId();
} else {
List<String> conflicts = ImmutableList.of();
if (m instanceof ResolveMerger) {
conflicts = ((ResolveMerger) m).getUnmergedPaths();
}
if (!allowConflicts) {
throw new MergeConflictException(createConflictMessage(conflicts));
}
// For merging with conflict markers we need a ResolveMerger, double-check that we have one.
if (!(m instanceof ResolveMerger)) {
throw new MergeWithConflictsNotSupportedException(MergeStrategy.get(mergeStrategy));
}
Map<String, MergeResult<? extends Sequence>> mergeResults = ((ResolveMerger) m).getMergeResults();
filesWithGitConflicts = mergeResults.entrySet().stream().filter(e -> e.getValue().containsConflicts()).map(Map.Entry::getKey).collect(toImmutableSet());
tree = mergeWithConflicts(rw, inserter, dc, "TARGET BRANCH", mergeTip, "SOURCE BRANCH", originalCommit, mergeResults);
}
CommitBuilder mergeCommit = new CommitBuilder();
mergeCommit.setTreeId(tree);
mergeCommit.setParentIds(mergeTip, originalCommit);
mergeCommit.setAuthor(authorIdent);
mergeCommit.setCommitter(committerIdent);
mergeCommit.setMessage(commitMsg);
CodeReviewCommit commit = rw.parseCommit(inserter.insert(mergeCommit));
commit.setFilesWithGitConflicts(filesWithGitConflicts);
return commit;
}
use of org.eclipse.jgit.merge.MergeStrategy in project gerrit by GerritCodeReview.
the class MergeUtil method newMerger.
public static Merger newMerger(ObjectInserter inserter, Config repoConfig, String strategyName) {
MergeStrategy strategy = MergeStrategy.get(strategyName);
checkArgument(strategy != null, "invalid merge strategy: %s", strategyName);
return strategy.newMerger(new ObjectInserter.Filter() {
@Override
protected ObjectInserter delegate() {
return inserter;
}
@Override
public void flush() {
}
@Override
public void close() {
}
}, repoConfig);
}
use of org.eclipse.jgit.merge.MergeStrategy in project egit by eclipse.
the class Activator method getPreferredMergeStrategy.
/**
* Provides the 3-way merge strategy to use according to the user's
* preferences. The preferred merge strategy is JGit's default merge
* strategy unless the user has explicitly chosen a different strategy among
* the registered strategies.
*
* @return The MergeStrategy to use, can be {@code null}, in which case the
* default merge strategy should be used as defined by JGit.
* @since 4.1
*/
public MergeStrategy getPreferredMergeStrategy() {
// Get preferences set by user in the UI
final IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode(Activator.getPluginId());
String preferredMergeStrategyKey = prefs.get(GitCorePreferences.core_preferredMergeStrategy, null);
// Get default preferences, wherever they are defined
if (preferredMergeStrategyKey == null || preferredMergeStrategyKey.isEmpty()) {
final IEclipsePreferences defaultPrefs = DefaultScope.INSTANCE.getNode(Activator.getPluginId());
preferredMergeStrategyKey = defaultPrefs.get(GitCorePreferences.core_preferredMergeStrategy, null);
}
if (preferredMergeStrategyKey != null && !preferredMergeStrategyKey.isEmpty() && !GitCorePreferences.core_preferredMergeStrategy_Default.equals(preferredMergeStrategyKey)) {
MergeStrategy result = MergeStrategy.get(preferredMergeStrategyKey);
if (result != null) {
return result;
}
logError(NLS.bind(CoreText.Activator_invalidPreferredMergeStrategy, preferredMergeStrategyKey), null);
}
return null;
}
Aggregations