diff options
| author | Tomasz Zarna | 2012-04-18 23:33:26 +0000 |
|---|---|---|
| committer | Matthias Sohn | 2012-04-18 23:33:51 +0000 |
| commit | 878e2622d0df5087c6a2f47ae795bc9748f56509 (patch) | |
| tree | 651cecf70f311ce29f576c99caaf6eeabcffb842 | |
| parent | 48d06a026afa1921412560b42127667fe5a3c23a (diff) | |
| download | egit-878e2622d0df5087c6a2f47ae795bc9748f56509.tar.gz egit-878e2622d0df5087c6a2f47ae795bc9748f56509.tar.xz egit-878e2622d0df5087c6a2f47ae795bc9748f56509.zip | |
Create Patch on staged file broken
The patch wizard should open if the selection contains a modified,
untracked or missing file. Otherwise, the "No changes" dialog should
be shown. The fix utilizes IndexDiffCache to achieve that.
Bug: 376187
Change-Id: I7dd7358dfc1b2a05fcbd6dec24bc819d96d31c4f
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
4 files changed, 78 insertions, 16 deletions
diff --git a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/common/LocalRepositoryTestCase.java b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/common/LocalRepositoryTestCase.java index 0148f53ed8..c3c151d272 100644 --- a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/common/LocalRepositoryTestCase.java +++ b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/common/LocalRepositoryTestCase.java @@ -31,6 +31,7 @@ import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.Path; import org.eclipse.egit.core.Activator; import org.eclipse.egit.core.RepositoryCache; +import org.eclipse.egit.core.op.AddToIndexOperation; import org.eclipse.egit.core.op.CloneOperation; import org.eclipse.egit.core.op.CommitOperation; import org.eclipse.egit.core.op.ConnectProviderOperation; @@ -458,17 +459,41 @@ public abstract class LocalRepositoryTestCase extends EGitTestCase { * @throws Exception */ protected static IFile touch(final String newContent) throws Exception { + return touch(PROJ1, "folder/test.txt", newContent); + } + + /** + * Modify the specified file with the given content. + * + * @param projectName + * project name + * @param filePath + * file path under the given project + * @param newContent + * new file content + * @return the modified file + * @throws Exception + */ + protected static IFile touch(String projectName, String filePath, + String newContent) throws Exception { IProject prj = ResourcesPlugin.getWorkspace().getRoot() - .getProject(PROJ1); + .getProject(projectName); if (!prj.isAccessible()) throw new IllegalStateException("No project to touch"); - IFile file = prj.getFile(new Path("folder/test.txt")); + IFile file = prj.getFile(new Path(filePath)); file.setContents( new ByteArrayInputStream(newContent.getBytes(prj .getDefaultCharset())), 0, null); return file; } + protected static void stage(IFile file) throws Exception { + ArrayList<IFile> unstaged = new ArrayList<IFile>(); + unstaged.addAll(Arrays.asList(new IFile[] { file })); + AddToIndexOperation op = new AddToIndexOperation(unstaged); + op.execute(null); + } + protected static void addAndCommit(IFile file, String commitMessage) throws Exception { IProject prj = file.getProject(); diff --git a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/CreatePatchActionTest.java b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/CreatePatchActionTest.java index ff48bf8f53..54622128cb 100644 --- a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/CreatePatchActionTest.java +++ b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/CreatePatchActionTest.java @@ -171,6 +171,20 @@ public class CreatePatchActionTest extends LocalRepositoryTestCase { } @Test + public void testNoChangesInSelection() throws Exception { + IFile fileToStage = touch(PROJ1, "folder/test.txt", "new content in " + + PROJ1); + stage(fileToStage); + touch(PROJ2, "folder/test.txt", "new content in " + PROJ2); + + CreatePatchWizard.openWizard(PROJ1); + + NoChangesPopup popup = new NoChangesPopup( + bot.shell(UIText.GitCreatePatchAction_cannotCreatePatch)); + popup.cancelPopup(); + } + + @Test public void testClipboard() throws Exception { touchAndSubmit("oldContent", null); touch("newContent"); diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/patch/PatchOperationUI.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/patch/PatchOperationUI.java index 9ba4c0fd2f..b8fd95c03a 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/patch/PatchOperationUI.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/patch/PatchOperationUI.java @@ -8,15 +8,18 @@ *******************************************************************************/ package org.eclipse.egit.ui.internal.patch; +import static org.eclipse.jgit.lib.Repository.stripWorkDir; + import java.util.Collection; +import java.util.Set; import org.eclipse.core.resources.IResource; +import org.eclipse.egit.core.internal.indexdiff.IndexDiffCache; +import org.eclipse.egit.core.internal.indexdiff.IndexDiffData; import org.eclipse.egit.core.op.CreatePatchOperation; import org.eclipse.egit.ui.UIText; import org.eclipse.egit.ui.internal.history.GitCreatePatchWizard; import org.eclipse.jface.dialogs.MessageDialog; -import org.eclipse.jgit.api.Git; -import org.eclipse.jgit.api.Status; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.swt.widgets.Shell; @@ -104,21 +107,41 @@ public class PatchOperationUI { } private boolean isWorkingTreeClean() { - Git git = new Git(repository); - try { - Status status = git.status().call(); - return status.getModified().isEmpty() - && status.getUntracked().isEmpty() - && status.getMissing().isEmpty(); - } catch (Exception e) { - MessageDialog.openError(getShell(), - UIText.GitCreatePatchAction_cannotCreatePatch, e - .getMessage() == null ? e.getMessage() - : UIText.GitCreatePatchWizard_InternalError); + IndexDiffCache diffCache = org.eclipse.egit.core.Activator.getDefault() + .getIndexDiffCache(); + if (diffCache != null) { + IndexDiffData diffData = diffCache.getIndexDiffCacheEntry( + repository).getIndexDiff(); + if (diffData != null) { + Set<String> modified = diffData.getModified(); + Set<String> untracked = diffData.getUntracked(); + Set<String> missing = diffData.getMissing(); + for (IResource resource : resources) { + String repoRelativePath = makeRepoRelative(resource); + if (containsPrefix(modified, repoRelativePath)) + return false; + if (containsPrefix(untracked, repoRelativePath)) + return false; + if (containsPrefix(missing, repoRelativePath)) + return false; + } + } } return true; } + private String makeRepoRelative(IResource res) { + return stripWorkDir(repository.getWorkTree(), res.getLocation() + .toFile()); + } + + private boolean containsPrefix(Set<String> collection, String prefix) { + for (String path : collection) + if (path.startsWith(prefix)) + return true; + return false; + } + private Shell getShell() { return getShell(part); } 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 b9022cb362..5340436be6 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 @@ -1044,7 +1044,7 @@ GitCreateGeneralProjectPage_FileNotDirMessage=File {0} is not a directory GitCreateGeneralProjectPage_PorjectAlreadyExistsMessage=Project {0} already exists GitCreateGeneralProjectPage_ProjectNameLabel=Project name GitCreatePatchAction_cannotCreatePatch=Cannot create patch -GitCreatePatchAction_workingTreeClean=There are no changes in working tree +GitCreatePatchAction_workingTreeClean=There are no changes in working tree for the current selection GitCreatePatchWizard_Browse=B&rowse... GitCreatePatchWizard_Clipboard=&Clipboard GitCreatePatchWizard_ContextMustBePositiveInt=Context must be a valid number of lines ( >= 0 ) |
