use of org.eclipse.egit.core.project.RepositoryMapping in project egit by eclipse.
the class RepositoryActionHandler method getHeadCommit.
protected RevCommit getHeadCommit(IResource resource) throws IOException {
Repository repository = getRepository();
if (resource == null) {
return null;
}
RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
if (mapping == null) {
return null;
}
String path = mapping.getRepoRelativePath(resource);
if (path == null) {
return null;
}
try (RevWalk rw = new RevWalk(repository)) {
rw.sort(RevSort.COMMIT_TIME_DESC, true);
rw.sort(RevSort.BOUNDARY, true);
if (path.length() > 0) {
DiffConfig diffConfig = repository.getConfig().get(DiffConfig.KEY);
FollowFilter filter = FollowFilter.create(path, diffConfig);
rw.setTreeFilter(filter);
}
Ref head = repository.findRef(Constants.HEAD);
if (head == null) {
return null;
}
RevCommit headCommit = rw.parseCommit(head.getObjectId());
rw.close();
return headCommit;
}
}
use of org.eclipse.egit.core.project.RepositoryMapping in project egit by eclipse.
the class RepositoryActionHandler method findPreviousCommits.
/**
* Returns the previous commit of the given resources.
*
* @param resources
* The {@link IResource} for which the previous commit shall be
* determined.
* @return The second to last commit which touched any of the given
* resources.
* @throws IOException
* When the commit can not be parsed.
*/
protected List<RevCommit> findPreviousCommits(Collection<IResource> resources) throws IOException {
List<RevCommit> result = new ArrayList<>();
Repository repository = getRepository();
RepositoryMapping mapping = RepositoryMapping.getMapping(resources.iterator().next().getProject());
if (mapping == null) {
return result;
}
try (RevWalk rw = new RevWalk(repository)) {
rw.sort(RevSort.COMMIT_TIME_DESC, true);
rw.sort(RevSort.BOUNDARY, true);
List<TreeFilter> filters = new ArrayList<>();
DiffConfig diffConfig = repository.getConfig().get(DiffConfig.KEY);
for (IResource resource : resources) {
String path = mapping.getRepoRelativePath(resource);
if (path != null && path.length() > 0) {
filters.add(FollowFilter.create(path, diffConfig));
}
}
if (filters.size() >= 2) {
TreeFilter filter = OrTreeFilter.create(filters);
rw.setTreeFilter(filter);
} else if (filters.size() == 1) {
rw.setTreeFilter(filters.get(0));
}
Ref head = repository.findRef(Constants.HEAD);
if (head == null) {
return result;
}
RevCommit headCommit = rw.parseCommit(head.getObjectId());
rw.markStart(headCommit);
headCommit = rw.next();
if (headCommit == null)
return result;
List<RevCommit> directParents = Arrays.asList(headCommit.getParents());
RevCommit previousCommit = rw.next();
while (previousCommit != null && result.size() < directParents.size()) {
if (directParents.contains(previousCommit)) {
result.add(previousCommit);
}
previousCommit = rw.next();
}
rw.dispose();
}
return result;
}
use of org.eclipse.egit.core.project.RepositoryMapping in project egit by eclipse.
the class ShowBlameActionHandler method execute.
/**
* @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
*/
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
IStructuredSelection selection = getSelection(event);
if (selection.size() != 1) {
return null;
}
Object element = selection.getFirstElement();
IResource resource = AdapterUtils.adapt(element, IResource.class);
if (resource instanceof IFile) {
RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
if (mapping != null) {
String repoRelativePath = mapping.getRepoRelativePath(resource);
Shell shell = HandlerUtil.getActiveShell(event);
IWorkbenchPage page = HandlerUtil.getActiveSite(event).getPage();
JobUtil.scheduleUserJob(new BlameOperation(mapping.getRepository(), (IFile) resource, repoRelativePath, null, shell, page), UIText.ShowBlameHandler_JobName, JobFamilies.BLAME);
}
} else if (element instanceof CommitFileRevision) {
Shell shell = HandlerUtil.getActiveShell(event);
IWorkbenchPage page = HandlerUtil.getActiveSite(event).getPage();
JobUtil.scheduleUserJob(new BlameOperation((CommitFileRevision) element, shell, page), UIText.ShowBlameHandler_JobName, JobFamilies.BLAME);
}
return null;
}
use of org.eclipse.egit.core.project.RepositoryMapping in project egit by eclipse.
the class SynchronizeWithMenu method fill.
@Override
public void fill(final Menu menu, int index) {
if (srv == null)
return;
final IResource selectedResource = getSelection();
if (selectedResource == null || selectedResource.isLinked(IResource.CHECK_ANCESTORS))
return;
RepositoryMapping mapping = RepositoryMapping.getMapping(selectedResource.getProject());
if (mapping == null)
return;
final Repository repo = mapping.getRepository();
if (repo == null)
return;
List<Ref> refs = new LinkedList<>();
RefDatabase refDatabase = repo.getRefDatabase();
try {
refs.addAll(refDatabase.getAdditionalRefs());
} catch (IOException e) {
// do nothing
}
try {
refs.addAll(refDatabase.getRefs(RefDatabase.ALL).values());
} catch (IOException e) {
// do nothing
}
Collections.sort(refs, CommonUtils.REF_ASCENDING_COMPARATOR);
String currentBranch;
try {
currentBranch = repo.getFullBranch();
} catch (IOException e) {
// $NON-NLS-1$
currentBranch = "";
}
int count = 0;
String oldName = null;
int refsLength = R_REFS.length();
int tagsLength = R_TAGS.substring(refsLength).length();
for (Ref ref : refs) {
final String name = ref.getName();
if (name.equals(Constants.HEAD) || name.equals(currentBranch) || excludeTag(ref, repo))
continue;
if (name.startsWith(R_REFS) && oldName != null && !oldName.regionMatches(refsLength, name, refsLength, tagsLength))
new MenuItem(menu, SWT.SEPARATOR);
MenuItem item = new MenuItem(menu, SWT.PUSH);
item.setText(name);
if (name.startsWith(Constants.R_TAGS))
item.setImage(tagImage);
else if (name.startsWith(Constants.R_HEADS) || name.startsWith(Constants.R_REMOTES))
item.setImage(branchImage);
item.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent event) {
GitSynchronizeData data;
try {
data = new GitSynchronizeData(repo, HEAD, name, true);
if (!(selectedResource instanceof IProject)) {
HashSet<IResource> resources = new HashSet<>();
resources.add(selectedResource);
data.setIncludedResources(resources);
}
GitModelSynchronize.launch(data, new IResource[] { selectedResource });
} catch (IOException e) {
Activator.logError(e.getMessage(), e);
}
}
});
if (++count == MAX_NUM_MENU_ENTRIES)
break;
oldName = name;
}
if (count > 1)
new MenuItem(menu, SWT.SEPARATOR);
MenuItem custom = new MenuItem(menu, SWT.PUSH);
custom.setText(UIText.SynchronizeWithMenu_custom);
custom.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
GitSynchronizeWizard gitWizard = new GitSynchronizeWizard();
WizardDialog wizard = new WizardDialog(menu.getShell(), gitWizard);
wizard.create();
wizard.open();
}
});
}
use of org.eclipse.egit.core.project.RepositoryMapping in project egit by eclipse.
the class ResourcePropertyTester method internalTest.
private boolean internalTest(Object receiver, String property) {
if (!(receiver instanceof IResource))
return false;
IResource res = (IResource) receiver;
if ("isContainer".equals(property)) {
// $NON-NLS-1$
int type = res.getType();
return type == IResource.FOLDER || type == IResource.PROJECT;
}
RepositoryMapping mapping = RepositoryMapping.getMapping(res);
if (mapping != null) {
Repository repository = mapping.getRepository();
return testRepositoryState(repository, property);
}
return false;
}
Aggregations