diff options
| author | Bernard Leach | 2011-05-14 23:26:30 +0000 |
|---|---|---|
| committer | Matthias Sohn | 2011-05-14 23:26:30 +0000 |
| commit | 5d19646b6fae72c1bb331fb582413175d3a0a01d (patch) | |
| tree | ededf9e16c8ad4035560a0f0da46c6b98bdfd77a | |
| parent | e5913363268a73685a1570e1f085ce889815a667 (diff) | |
| download | egit-5d19646b6fae72c1bb331fb582413175d3a0a01d.tar.gz egit-5d19646b6fae72c1bb331fb582413175d3a0a01d.tar.xz egit-5d19646b6fae72c1bb331fb582413175d3a0a01d.zip | |
Add a compare index with HEAD action
This new ActionCommand compares a staged selection against
the HEAD revision.
Change-Id: Ib74fd7d3e4fe0c309b16eb5e9d76511257663a24
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
5 files changed, 163 insertions, 0 deletions
diff --git a/org.eclipse.egit.ui/plugin.properties b/org.eclipse.egit.ui/plugin.properties index b8c162e164..8d26dc315c 100644 --- a/org.eclipse.egit.ui/plugin.properties +++ b/org.eclipse.egit.ui/plugin.properties @@ -46,6 +46,7 @@ CommitAction_label=&Commit... CommitAction_tooltip=Commit change CompareWithIndexAction_label=Git &Index CompareWithHeadAction_label=&HEAD Revision +CompareIndexWithHeadAction_label=Git Index With HEAD IgnoreAction_label=&Ignore ApplyPatchAction_label=Appl&y Patch... ShowResourceInHistoryAction_label=Show in Hist&ory @@ -192,6 +193,7 @@ AddToIndexCommand.name = Add to Git Index BranchCommand.name = Branch CommitCommand.name = Commit CompareWithHeadCommand.name = Compare with HEAD Revision +CompareIndexWithHeadCommand.name = Compare File in Git Index with HEAD Revision CompareWithHistoryCommand.name = Compare with History CompareWithIndexCommand.name = Compare with Git Index ReplaceWithIndexCommand.name = Replace with File in Git Index diff --git a/org.eclipse.egit.ui/plugin.xml b/org.eclipse.egit.ui/plugin.xml index e5bcc983fc..43cf475e84 100644 --- a/org.eclipse.egit.ui/plugin.xml +++ b/org.eclipse.egit.ui/plugin.xml @@ -129,6 +129,11 @@ label="%CompareWithHeadAction_label" menubarPath="compareWithMenu/gitCompareWithGroup"/> <action + class="org.eclipse.egit.ui.internal.actions.CompareIndexWithHeadAction" + id="org.eclipse.egit.ui.internal.actions.CompareIndexWithHeadAction" + label="%CompareIndexWithHeadAction_label" + menubarPath="compareWithMenu/gitCompareWithGroup"/> + <action class="org.eclipse.egit.ui.internal.actions.IgnoreAction" definitionId="org.eclipse.egit.ui.team.Ignore" icon="$nl$/icons/obj16/ignore.gif" @@ -3253,6 +3258,12 @@ </command> <command categoryId="org.eclipse.egit.ui.commandCategory" + defaultHandler="org.eclipse.egit.ui.internal.actions.CompareIndexWithHeadActionHandler" + id="org.eclipse.egit.ui.team.CompareIndexWithHead" + name="%CompareIndexWithHeadCommand.name"> + </command> + <command + categoryId="org.eclipse.egit.ui.commandCategory" defaultHandler="org.eclipse.egit.ui.internal.actions.CompareWithPreviousActionHandler" id="org.eclipse.egit.ui.team.CompareWithPrevious" name="%CompareWithPreviousCommand.name"> diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/ActionCommands.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/ActionCommands.java index 1f657ae803..fb92e85cfc 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/ActionCommands.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/ActionCommands.java @@ -29,6 +29,9 @@ public class ActionCommands { /** "Compare with head" action command id */ public static final String COMPARE_WITH_HEAD_ACTION = "org.eclipse.egit.ui.team.CompareWithHead"; //$NON-NLS-1$ + /** "Compare index with head" action command id */ + public static final String COMPARE_INDEX_WITH_HEAD_ACTION = "org.eclipse.egit.ui.team.CompareIndexWithHead"; //$NON-NLS-1$ + /** "Compare with index" action command id */ public static final String COMPARE_WITH_INDEX_ACTION = "org.eclipse.egit.ui.team.CompareWithIndex"; //$NON-NLS-1$ diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareIndexWithHeadAction.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareIndexWithHeadAction.java new file mode 100644 index 0000000000..1c1d211804 --- /dev/null +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareIndexWithHeadAction.java @@ -0,0 +1,23 @@ +/******************************************************************************* + * Copyright (C) 2011, Bernard Leach <leachbj@bouncycastle.org> + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ +package org.eclipse.egit.ui.internal.actions; + +/** + * Compares the index content of a file with the version of the file in + * the HEAD commit. + */ +public class CompareIndexWithHeadAction extends RepositoryAction { + + /** + * + */ + public CompareIndexWithHeadAction() { + super(ActionCommands.COMPARE_INDEX_WITH_HEAD_ACTION, new CompareIndexWithHeadActionHandler()); + } +} diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareIndexWithHeadActionHandler.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareIndexWithHeadActionHandler.java new file mode 100644 index 0000000000..dddbee3ca3 --- /dev/null +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareIndexWithHeadActionHandler.java @@ -0,0 +1,124 @@ +/******************************************************************************* + * Copyright (C) 2011, Bernard Leach <leachbj@bouncycastle.org> + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ +package org.eclipse.egit.ui.internal.actions; + +import java.io.IOException; +import java.util.Collections; + +import org.eclipse.compare.ITypedElement; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.egit.core.project.RepositoryMapping; +import org.eclipse.egit.ui.Activator; +import org.eclipse.egit.ui.UIText; +import org.eclipse.egit.ui.internal.CompareUtils; +import org.eclipse.egit.ui.internal.GitCompareFileRevisionEditorInput; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.IndexDiff; +import org.eclipse.jgit.lib.Ref; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.revwalk.RevWalk; +import org.eclipse.jgit.treewalk.FileTreeIterator; +import org.eclipse.jgit.treewalk.filter.PathFilterGroup; +import org.eclipse.osgi.util.NLS; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.handlers.HandlerUtil; + +/** + * Compares the index content of a file with the version of the file in + * the HEAD commit. + */ +public class CompareIndexWithHeadActionHandler extends RepositoryActionHandler { + public Object execute(ExecutionEvent event) throws ExecutionException { + final Repository repository = getRepository(true, event); + // assert all resources map to the same repository + if (repository == null) + return null; + final IResource[] resources = getSelectedResources(event); + final IFile baseFile = (IFile) resources[0]; + final String gitPath = RepositoryMapping.getMapping( + baseFile.getProject()).getRepoRelativePath(baseFile); + final ITypedElement base = CompareUtils.getFileCachedRevisionTypedElement(gitPath, repository); + + ITypedElement next; + try { + Ref head = repository.getRef(Constants.HEAD); + RevWalk rw = new RevWalk(repository); + RevCommit commit = rw.parseCommit(head.getObjectId()); + + next = CompareUtils.getFileRevisionTypedElement(gitPath, + commit, repository); + } catch (IOException e) { + Activator.handleError(e.getMessage(), e, true); + return null; + } + + final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput( + base, next, null); + + IWorkbenchPage workBenchPage = HandlerUtil.getActiveWorkbenchWindowChecked(event).getActivePage(); + CompareUtils.openInCompare(workBenchPage, in); + + return null; + } + + @Override + public boolean isEnabled() { + IStructuredSelection selection = getSelection(); + if (selection.size() != 1) + return false; + + IResource resource = (IResource) getAdapter(selection.getFirstElement(), IResource.class); + if (resource == null) + return false; + + Repository repository = getRepository(); + if (repository == null) + return false; + + return isStaged(repository, resource); + } + + private Object getAdapter(Object adaptable, Class c) { + if (c.isInstance(adaptable)) + return adaptable; + if (adaptable instanceof IAdaptable) { + IAdaptable a = (IAdaptable) adaptable; + Object adapter = a.getAdapter(c); + if (c.isInstance(adapter)) + return adapter; + } + return null; + } + + private boolean isStaged(Repository repository, + IResource resource) { + String resRelPath = RepositoryMapping.getMapping(resource).getRepoRelativePath(resource); + + try { + FileTreeIterator fileTreeIterator = new FileTreeIterator(repository); + IndexDiff indexDiff = new IndexDiff(repository, Constants.HEAD, + fileTreeIterator); + indexDiff.setFilter(PathFilterGroup.createFromStrings(Collections.singletonList(resRelPath))); + indexDiff.diff(); + + return indexDiff.getAdded().contains(resRelPath) || indexDiff.getChanged().contains(resRelPath) + || indexDiff.getRemoved().contains(resRelPath); + } catch (IOException e) { + Activator.error(NLS.bind(UIText.GitHistoryPage_errorLookingUpPath, + resource.getFullPath().toString()), e); + return false; + } + } +} |
