use of org.eclipse.compare.ITypedElement in project egit by eclipse.
the class CompareUtils method compareWorkspaceWithRef.
/**
* Opens a compare editor comparing the working directory version of the
* given file or link with the version of that file corresponding to
* {@code refName}.
*
* @param repository
* The repository to load file revisions from.
* @param file
* Resource to compare revisions for. Must be either
* {@link IFile} or a symbolic link to directory ({@link IFolder}).
* @param refName
* Reference to compare with the workspace version of
* {@code file}. Can be either a commit ID, a reference or a
* branch name.
* @param page
* If not {@null} try to re-use a compare editor on this page if
* any is available. Otherwise open a new one.
*/
public static void compareWorkspaceWithRef(@NonNull final Repository repository, final IResource file, final String refName, final IWorkbenchPage page) {
if (file == null) {
return;
}
final IPath location = file.getLocation();
if (location == null) {
return;
}
Job job = new Job(UIText.CompareUtils_jobName) {
@Override
public IStatus run(IProgressMonitor monitor) {
if (monitor.isCanceled()) {
return Status.CANCEL_STATUS;
}
final RepositoryMapping mapping = RepositoryMapping.getMapping(file);
if (mapping == null) {
return Activator.createErrorStatus(NLS.bind(UIText.GitHistoryPage_errorLookingUpPath, location, repository));
}
final ITypedElement base;
if (Files.isSymbolicLink(location.toFile().toPath())) {
base = new LocalNonWorkspaceTypedElement(repository, location);
} else if (file instanceof IFile) {
base = SaveableCompareEditorInput.createFileElement((IFile) file);
} else {
return Activator.createErrorStatus(NLS.bind(UIText.CompareUtils_wrongResourceArgument, location, file));
}
final String gitPath = mapping.getRepoRelativePath(file);
CompareEditorInput in;
try {
in = prepareCompareInput(repository, gitPath, base, refName);
} catch (IOException e) {
return Activator.createErrorStatus(UIText.CompareWithRefAction_errorOnSynchronize, e);
}
if (monitor.isCanceled()) {
return Status.CANCEL_STATUS;
}
openCompareEditorRunnable(page, in);
return Status.OK_STATUS;
}
};
job.setUser(true);
job.schedule();
}
use of org.eclipse.compare.ITypedElement in project egit by eclipse.
the class CompareUtils method openInCompare.
/**
* Compares two files between the given commits, taking possible renames
* into account.
*
* @param commit1
* the "left" commit for the comparison editor
* @param commit2
* the "right" commit for the comparison editor
* @param commit1Path
* path to the file within commit1's tree
* @param commit2Path
* path to the file within commit2's tree
* @param repository
* the repository this commit was loaded out of
* @param workBenchPage
* the page to open the compare editor in
*/
public static void openInCompare(RevCommit commit1, RevCommit commit2, String commit1Path, String commit2Path, Repository repository, IWorkbenchPage workBenchPage) {
final ITypedElement base = CompareUtils.getFileRevisionTypedElement(commit1Path, commit1, repository);
final ITypedElement next = CompareUtils.getFileRevisionTypedElement(commit2Path, commit2, repository);
CompareEditorInput in = new GitCompareFileRevisionEditorInput(base, next, null);
CompareUtils.openInCompare(workBenchPage, in);
}
use of org.eclipse.compare.ITypedElement in project egit by eclipse.
the class CompareUtils method compareHeadWithWorkingTree.
/**
* Opens a compare editor. The working tree version of the given file is
* compared with the version in the HEAD commit. Use this method if the
* given file is outide the workspace.
*
* @param repository
* @param path
*/
public static void compareHeadWithWorkingTree(Repository repository, String path) {
ITypedElement base = getHeadTypedElement(repository, path);
if (base == null)
return;
IFileRevision nextFile;
nextFile = new WorkingTreeFileRevision(new File(repository.getWorkTree(), path));
String encoding = ResourcesPlugin.getEncoding();
ITypedElement next = new FileRevisionTypedElement(nextFile, encoding);
GitCompareFileRevisionEditorInput input = new GitCompareFileRevisionEditorInput(next, base, null);
CompareUI.openCompareDialog(input);
}
use of org.eclipse.compare.ITypedElement in project egit by eclipse.
the class CompareUtils method getFileRevisionTypedElementForCommonAncestor.
/**
* Creates a {@link ITypedElement} for the commit which is the common
* ancestor of the provided commits. Returns null if no such commit exists
* or if {@code gitPath} is not contained in the common ancestor or if the
* common ancestor is equal to one of the given commits
*
* @param gitPath
* path within the ancestor commit's tree of the file.
* @param commit1
* @param commit2
* @param db
* the repository this commit was loaded out of.
* @return an instance of {@link ITypedElement} which can be used in
* {@link CompareEditorInput}
*/
public static ITypedElement getFileRevisionTypedElementForCommonAncestor(final String gitPath, ObjectId commit1, ObjectId commit2, Repository db) {
ITypedElement ancestor = null;
RevCommit commonAncestor = null;
try {
commonAncestor = RevUtils.getCommonAncestor(db, commit1, commit2);
} catch (IOException e) {
Activator.logError(NLS.bind(UIText.CompareUtils_errorCommonAncestor, commit1.getName(), commit2.getName()), e);
}
if (commonAncestor != null) {
if (commit1.equals(commonAncestor.getId()) || commit2.equals(commonAncestor.getId())) {
// of given commits, see bug 512395
return null;
}
ITypedElement ancestorCandidate = CompareUtils.getFileRevisionTypedElement(gitPath, commonAncestor, db);
if (!(ancestorCandidate instanceof EmptyTypedElement))
ancestor = ancestorCandidate;
}
return ancestor;
}
use of org.eclipse.compare.ITypedElement in project egit by eclipse.
the class GitModelSynchronizeParticipant method asCompareInput.
@Override
public ICompareInput asCompareInput(Object object) {
final ICompareInput input = super.asCompareInput(object);
final ISynchronizationContext ctx = getContext();
if (input instanceof ResourceDiffCompareInput && ctx instanceof SubscriberMergeContext) {
// Team only considers local resources as "left"
// We'll use the cached data instead as left could be remote
final IResource resource = ((ResourceNode) input.getLeft()).getResource();
final Subscriber subscriber = ((SubscriberMergeContext) ctx).getSubscriber();
if (resource instanceof IFile && subscriber instanceof GitResourceVariantTreeSubscriber) {
try {
final IFileRevision revision = ((GitResourceVariantTreeSubscriber) subscriber).getSourceFileRevision((IFile) resource);
if (revision == null) {
final ITypedElement newSource = new GitCompareFileRevisionEditorInput.EmptyTypedElement(resource.getName());
((ResourceDiffCompareInput) input).setLeft(newSource);
} else if (!(revision instanceof WorkspaceFileRevision)) {
final ITypedElement newSource = new FileRevisionTypedElement(revision, getLocalEncoding(resource));
((ResourceDiffCompareInput) input).setLeft(newSource);
}
} catch (TeamException e) {
// Keep the input from super as-is
String error = NLS.bind(UIText.GitModelSynchronizeParticipant_noCachedSourceVariant, resource.getName());
Activator.logError(error, e);
}
}
}
return input;
}
Aggregations