diff options
| author | Remy Suen | 2010-05-17 10:13:02 +0000 |
|---|---|---|
| committer | Remy Suen | 2010-05-17 10:13:02 +0000 |
| commit | 0c47dcbd35cae80f9a54847fbc17f49c2b65f65a (patch) | |
| tree | e1b6312dec599cbbaac718e314358630910d0fd8 | |
| parent | 856f4028793ccdacd09b4130ab944eb6741aa93d (diff) | |
| download | egit-0c47dcbd35cae80f9a54847fbc17f49c2b65f65a.tar.gz egit-0c47dcbd35cae80f9a54847fbc17f49c2b65f65a.tar.xz egit-0c47dcbd35cae80f9a54847fbc17f49c2b65f65a.zip | |
Prevent NPE when comparing an untracked file with the index
The compare request does not currently check whether a file is in
the index before displaying the compare editor. This causes
problems when the editor tries to ask the revision for its content
and JGit is unable to return something meaningful here because a
blob cannot actually be found. The fix is to check if an entry
actually exists in the index before trying to create a compare
editor input from it.
Change-Id: I9a1b8bd39aded3dcdeaf523b735f5fcf66f8d152
3 files changed, 39 insertions, 11 deletions
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/UIText.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/UIText.java index 967a672f74..be7917c426 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/UIText.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/UIText.java @@ -331,6 +331,9 @@ public class UIText extends NLS { public static String RepositorySearchDialog_ToggleSelection_button; /** */ + public static String CompareWithIndexAction_FileNotInIndex; + + /** */ public static String RepositoryAction_errorFindingRepo; /** */ diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareWithIndexAction.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareWithIndexAction.java index d670e763a4..af40616127 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareWithIndexAction.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/CompareWithIndexAction.java @@ -11,6 +11,7 @@ package org.eclipse.egit.ui.internal.actions; import java.io.File; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import org.eclipse.compare.CompareUI; import org.eclipse.compare.IContentChangeListener; @@ -26,6 +27,7 @@ import org.eclipse.egit.ui.internal.GitCompareFileRevisionEditorInput; import org.eclipse.jface.action.IAction; import org.eclipse.jgit.lib.GitIndex; import org.eclipse.jgit.lib.Repository; +import org.eclipse.osgi.util.NLS; import org.eclipse.team.core.TeamException; import org.eclipse.team.core.history.IFileRevision; import org.eclipse.team.ui.synchronize.SaveableCompareEditorInput; @@ -39,17 +41,41 @@ import org.eclipse.team.ui.synchronize.SaveableCompareEditorInput; public class CompareWithIndexAction extends RepositoryAction { @Override - public void execute(IAction action) { + public void execute(IAction action) throws InvocationTargetException { final IResource resource = getSelectedResources()[0]; - final RepositoryMapping mapping = RepositoryMapping.getMapping(resource.getProject()); - final Repository repository = mapping.getRepository(); - final String gitPath = mapping.getRepoRelativePath(resource); - - final IFileRevision nextFile = GitFileRevision.inIndex(repository, gitPath); final IFile baseFile = (IFile) resource; - final ITypedElement base = SaveableCompareEditorInput.createFileElement(baseFile); + final ITypedElement base = SaveableCompareEditorInput + .createFileElement(baseFile); + + final ITypedElement next = getHeadTypedElement(baseFile); + + final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput( + base, next, null); + CompareUI.openCompareEditor(in); + } + private ITypedElement getHeadTypedElement(final IFile baseFile) + throws InvocationTargetException { + final RepositoryMapping mapping = RepositoryMapping.getMapping(baseFile + .getProject()); + final Repository repository = mapping.getRepository(); + String gitPath = mapping.getRepoRelativePath(baseFile); + + try { + GitIndex index = repository.getIndex(); + if (index.getEntry(gitPath) == null) { + // the file cannot be found in the index + return new GitCompareFileRevisionEditorInput.EmptyTypedElement( + NLS.bind(UIText.CompareWithIndexAction_FileNotInIndex, + baseFile.getName())); + } + } catch (IOException e) { + // this exception is handled by TeamAction.run + throw new InvocationTargetException(e); + } + + IFileRevision nextFile = GitFileRevision.inIndex(repository, gitPath); final EditableRevision next = new EditableRevision(nextFile); IContentChangeListener listener = new IContentChangeListener() { @@ -73,10 +99,7 @@ public class CompareWithIndexAction extends RepositoryAction { }; next.addContentChangeListener(listener); - - final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput( - base, next, null); - CompareUI.openCompareEditor(in); + return next; } @Override diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties index 05a73c539d..12ed2fc0f5 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties @@ -100,6 +100,8 @@ GitProjectPropertyPage_ValueEmptyRepository=None (empty repository) GitProjectPropertyPage_ValueUnbornBranch=None (unborn branch) GitProjectsImportPage_NoProjectsMessage=No projects found +CompareWithIndexAction_FileNotInIndex={0} not in index + RepositoryAction_errorFindingRepo=Could not find a repository associated with this project RepositoryAction_errorFindingRepoTitle=Cannot Find Repository RepositoryAction_multiRepoSelection=Cannot perform reset on multiple repositories simultaneously.\n\nPlease select items from only one repository. |
