use of org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput in project egit by eclipse.
the class CompareUtils method compareHeadWithWorkspace.
/**
* Opens a compare editor. The workspace version of the given file is
* compared with the version in the HEAD commit.
*
* @param repository
* @param file
*/
public static void compareHeadWithWorkspace(Repository repository, @NonNull IFile file) {
RepositoryMapping mapping = RepositoryMapping.getMapping(file);
if (mapping == null) {
Activator.error(NLS.bind(UIText.GitHistoryPage_errorLookingUpPath, file.getLocation(), repository), null);
return;
}
String path = mapping.getRepoRelativePath(file);
ITypedElement base = getHeadTypedElement(repository, path);
if (base == null)
return;
IFileRevision nextFile = new WorkspaceFileRevision(file);
String encoding = null;
try {
encoding = file.getCharset();
} catch (CoreException e) {
Activator.handleError(UIText.CompareUtils_errorGettingEncoding, e, true);
}
ITypedElement next = new FileRevisionTypedElement(nextFile, encoding);
GitCompareFileRevisionEditorInput input = new GitCompareFileRevisionEditorInput(next, base, null);
CompareUI.openCompareDialog(input);
}
use of org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput in project egit by eclipse.
the class CompareUtils method prepareCompareInput.
/*
* Creates a compare input that can be used to compare a given local file
* with another reference. The given "base" element should always reflect a
* local file, either in the workspace (IFile) or on the file system
* (java.io.File) since we'll use "HEAD" to find a common ancestor of this
* base and the reference we compare it with.
*/
private static CompareEditorInput prepareCompareInput(Repository repository, String gitPath, ITypedElement base, String refName) throws IOException {
final ITypedElement destCommit;
ITypedElement commonAncestor = null;
if (GitFileRevision.INDEX.equals(refName))
destCommit = getIndexTypedElement(repository, gitPath);
else if (Constants.HEAD.equals(refName))
destCommit = getHeadTypedElement(repository, gitPath);
else {
final ObjectId destCommitId = repository.resolve(refName);
try (RevWalk rw = new RevWalk(repository)) {
RevCommit commit = rw.parseCommit(destCommitId);
destCommit = getFileRevisionTypedElement(gitPath, commit, repository);
if (base != null && commit != null) {
final ObjectId headCommitId = repository.resolve(Constants.HEAD);
commonAncestor = getFileRevisionTypedElementForCommonAncestor(gitPath, headCommitId, destCommitId, repository);
}
}
}
final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(base, destCommit, commonAncestor, null);
in.getCompareConfiguration().setRightLabel(refName);
return in;
}
use of org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput in project egit by eclipse.
the class CompareWithWorkingTreeHandler method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IStructuredSelection selection = getSelection(event);
if (selection.isEmpty())
return null;
// Even if there's more than one element, only consider the first
RevCommit commit = (RevCommit) selection.getFirstElement();
Object input = getPage(event).getInputInternal().getSingleFile();
IWorkbenchPage workBenchPage = HandlerUtil.getActiveWorkbenchWindowChecked(event).getActivePage();
if (input instanceof IFile) {
IFile file = (IFile) input;
final RepositoryMapping mapping = RepositoryMapping.getMapping(file);
if (mapping != null) {
final String gitPath = mapping.getRepoRelativePath(file);
final String commitPath = getRenamedPath(gitPath, commit);
ITypedElement right = CompareUtils.getFileRevisionTypedElement(commitPath, commit, mapping.getRepository());
final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(SaveableCompareEditorInput.createFileElement(file), right, null);
CompareUtils.openInCompare(workBenchPage, in);
}
} else if (input instanceof File) {
File file = (File) input;
// TODO can we create a ITypedElement from the local file?
Repository repo = getRepository(event);
RevCommit leftCommit;
try (RevWalk rw = new RevWalk(repo)) {
leftCommit = rw.parseCommit(repo.resolve(Constants.HEAD));
} catch (Exception e) {
throw new ExecutionException(e.getMessage(), e);
}
final String leftCommitPath = getRepoRelativePath(repo, file);
final String rightCommitPath = getRenamedPath(leftCommitPath, commit);
CompareUtils.openInCompare(leftCommit, commit, leftCommitPath, rightCommitPath, repo, workBenchPage);
}
return null;
}
use of org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput in project egit by eclipse.
the class ShowVersionsHandler method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
boolean compareMode = Boolean.TRUE.toString().equals(event.getParameter(COMPARE_MODE_PARAM));
IStructuredSelection selection = getSelection(getPage());
if (selection.size() < 1)
return null;
Object input = getPage().getInputInternal().getSingleFile();
if (input == null)
return null;
IWorkbenchPage workBenchPage = HandlerUtil.getActiveWorkbenchWindowChecked(event).getActivePage();
boolean errorOccurred = false;
List<ObjectId> ids = new ArrayList<>();
String gitPath = null;
if (input instanceof IFile) {
IFile resource = (IFile) input;
final RepositoryMapping map = RepositoryMapping.getMapping(resource);
if (map != null) {
gitPath = map.getRepoRelativePath(resource);
Iterator<?> it = selection.iterator();
while (it.hasNext()) {
RevCommit commit = (RevCommit) it.next();
String commitPath = getRenamedPath(gitPath, commit);
IFileRevision rev = null;
try {
rev = CompareUtils.getFileRevision(commitPath, commit, map.getRepository(), null);
} catch (IOException e) {
Activator.logError(NLS.bind(UIText.GitHistoryPage_errorLookingUpPath, gitPath, commit.getId()), e);
errorOccurred = true;
}
if (rev != null) {
if (compareMode) {
ITypedElement right = CompareUtils.getFileRevisionTypedElement(commitPath, commit, map.getRepository());
final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(SaveableCompareEditorInput.createFileElement(resource), right, null);
try {
CompareUtils.openInCompare(workBenchPage, in);
} catch (Exception e) {
errorOccurred = true;
}
} else
try {
EgitUiEditorUtils.openEditor(getPart(event).getSite().getPage(), rev, new NullProgressMonitor());
} catch (CoreException e) {
Activator.logError(UIText.GitHistoryPage_openFailed, e);
errorOccurred = true;
}
} else {
ids.add(commit.getId());
}
}
}
}
if (input instanceof File) {
File fileInput = (File) input;
Repository repo = getRepository(event);
gitPath = getRepoRelativePath(repo, fileInput);
Iterator<?> it = selection.iterator();
while (it.hasNext()) {
RevCommit commit = (RevCommit) it.next();
String commitPath = getRenamedPath(gitPath, commit);
IFileRevision rev = null;
try {
rev = CompareUtils.getFileRevision(commitPath, commit, repo, null);
} catch (IOException e) {
Activator.logError(NLS.bind(UIText.GitHistoryPage_errorLookingUpPath, commitPath, commit.getId()), e);
errorOccurred = true;
}
if (rev != null) {
if (compareMode)
try (RevWalk rw = new RevWalk(repo)) {
ITypedElement left = CompareUtils.getFileRevisionTypedElement(gitPath, rw.parseCommit(repo.resolve(Constants.HEAD)), repo);
ITypedElement right = CompareUtils.getFileRevisionTypedElement(commitPath, commit, repo);
final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(left, right, null);
CompareUtils.openInCompare(workBenchPage, in);
} catch (IOException e) {
errorOccurred = true;
}
else
try {
EgitUiEditorUtils.openEditor(getPart(event).getSite().getPage(), rev, new NullProgressMonitor());
} catch (CoreException e) {
Activator.logError(UIText.GitHistoryPage_openFailed, e);
errorOccurred = true;
}
} else
ids.add(commit.getId());
}
}
if (errorOccurred)
Activator.showError(UIText.GitHistoryPage_openFailed, null);
if (ids.size() > 0) {
// $NON-NLS-1$
StringBuilder idList = new StringBuilder("");
for (ObjectId objectId : ids) idList.append(objectId.getName()).append(' ');
MessageDialog.openError(getPart(event).getSite().getShell(), UIText.GitHistoryPage_fileNotFound, NLS.bind(UIText.GitHistoryPage_notContainedInCommits, gitPath, idList.toString()));
}
return null;
}
use of org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput in project egit by eclipse.
the class CompareTreeView method reactOnOpen.
private void reactOnOpen(OpenEvent event) {
Object selected = ((IStructuredSelection) event.getSelection()).getFirstElement();
ITypedElement left;
ITypedElement right;
if (selected instanceof ContainerNode) {
// open/close folder
TreeViewer tv = (TreeViewer) event.getViewer();
tv.setExpandedState(selected, !tv.getExpandedState(selected));
} else if (selected instanceof FileNode) {
FileNode fileNode = (FileNode) selected;
boolean compareMode = Activator.getDefault().getPreferenceStore().getBoolean(UIPreferences.TREE_COMPARE_COMPARE_MODE);
if (compareMode) {
left = getTypedElement(fileNode, fileNode.leftRevision, getBaseVersionText());
right = getTypedElement(fileNode, fileNode.rightRevision, getCompareVersionText());
GitCompareFileRevisionEditorInput compareInput = new GitCompareFileRevisionEditorInput(left, right, PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage());
CompareUtils.openInCompare(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), compareInput);
} else {
IFile file = fileNode.getFile();
if (file != null) {
DiffViewer.openFileInEditor(file.getLocation().toFile(), -1);
}
}
}
}
Aggregations