use of org.eclipse.egit.core.project.RepositoryMapping 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.core.project.RepositoryMapping in project egit by eclipse.
the class ShowBlameHandler method getPath.
private String getPath(Repository repo, GitHistoryPage page) {
Object input = page.getInputInternal().getSingleItem();
if (input == null) {
return null;
}
if (input instanceof IFile) {
IFile file = (IFile) input;
RepositoryMapping mapping = RepositoryMapping.getMapping(file);
if (mapping != null) {
return mapping.getRepoRelativePath(file);
}
} else if (input instanceof File) {
return getRepoRelativePath(repo, (File) input);
}
return null;
}
use of org.eclipse.egit.core.project.RepositoryMapping 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.core.project.RepositoryMapping in project egit by eclipse.
the class StagingView method stage.
private void stage(IStructuredSelection selection) {
StagingViewContentProvider contentProvider = getContentProvider(unstagedViewer);
final Repository repository = currentRepository;
Iterator iterator = selection.iterator();
final List<String> addPaths = new ArrayList<>();
final List<String> rmPaths = new ArrayList<>();
resetPathsToExpand();
while (iterator.hasNext()) {
Object element = iterator.next();
if (element instanceof StagingEntry) {
StagingEntry entry = (StagingEntry) element;
selectEntryForStaging(entry, addPaths, rmPaths);
addPathAndParentPaths(entry.getParentPath(), pathsToExpandInStaged);
} else if (element instanceof StagingFolderEntry) {
StagingFolderEntry folder = (StagingFolderEntry) element;
List<StagingEntry> entries = contentProvider.getStagingEntriesFiltered(folder);
for (StagingEntry entry : entries) selectEntryForStaging(entry, addPaths, rmPaths);
addExpandedPathsBelowFolder(folder, unstagedViewer, pathsToExpandInStaged);
} else {
IResource resource = AdapterUtils.adaptToAnyResource(element);
if (resource != null) {
RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
// submodule of the mapped repo
if (mapping != null && mapping.getRepository() == currentRepository) {
String path = mapping.getRepoRelativePath(resource);
// If resource corresponds to root of working directory
if (// $NON-NLS-1$
"".equals(path))
// $NON-NLS-1$
addPaths.add(".");
else
addPaths.add(path);
}
}
}
}
// start long running operations
if (!addPaths.isEmpty()) {
Job addJob = new Job(UIText.StagingView_AddJob) {
@Override
protected IStatus run(IProgressMonitor monitor) {
try (Git git = new Git(repository)) {
AddCommand add = git.add();
for (String addPath : addPaths) add.addFilepattern(addPath);
add.call();
} catch (NoFilepatternException e1) {
// cannot happen
} catch (JGitInternalException e1) {
Activator.handleError(e1.getCause().getMessage(), e1.getCause(), true);
} catch (Exception e1) {
Activator.handleError(e1.getMessage(), e1, true);
}
return Status.OK_STATUS;
}
@Override
public boolean belongsTo(Object family) {
return family == JobFamilies.ADD_TO_INDEX;
}
};
schedule(addJob, true);
}
if (!rmPaths.isEmpty()) {
Job removeJob = new Job(UIText.StagingView_RemoveJob) {
@Override
protected IStatus run(IProgressMonitor monitor) {
try (Git git = new Git(repository)) {
RmCommand rm = git.rm().setCached(true);
for (String rmPath : rmPaths) rm.addFilepattern(rmPath);
rm.call();
} catch (NoFilepatternException e) {
// cannot happen
} catch (JGitInternalException e) {
Activator.handleError(e.getCause().getMessage(), e.getCause(), true);
} catch (Exception e) {
Activator.handleError(e.getMessage(), e, true);
}
return Status.OK_STATUS;
}
@Override
public boolean belongsTo(Object family) {
return family == JobFamilies.REMOVE_FROM_INDEX;
}
};
schedule(removeJob, true);
}
}
use of org.eclipse.egit.core.project.RepositoryMapping in project egit by eclipse.
the class StagingView method showResource.
private void showResource(final IResource resource) {
if (resource == null || !resource.isAccessible()) {
return;
}
Job.getJobManager().cancel(JobFamilies.UPDATE_SELECTION);
Job job = new Job(UIText.StagingView_GetRepo) {
@Override
protected IStatus run(IProgressMonitor monitor) {
if (monitor.isCanceled()) {
return Status.CANCEL_STATUS;
}
RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
if (mapping != null) {
Repository newRep = mapping.getRepository();
if (newRep != null && newRep != currentRepository) {
if (monitor.isCanceled()) {
return Status.CANCEL_STATUS;
}
reload(newRep);
}
}
return Status.OK_STATUS;
}
@Override
public boolean belongsTo(Object family) {
return JobFamilies.UPDATE_SELECTION == family;
}
@Override
public boolean shouldRun() {
return shouldUpdateSelection();
}
};
job.setSystem(true);
schedule(job, false);
}
Aggregations