From 7187062d377da2bc2d334b1f2ab4620cb6226c82 Mon Sep 17 00:00:00 2001 From: Mykola Morhun Date: Mon, 27 Feb 2017 17:55:00 +0200 Subject: [PATCH] CHE-4015 Add deleted files to index(Regression) (#4187) CHE-4015: Fix deleted file is not added to index --- .../git/client/action/AddToIndexAction.java | 58 +++++++------------ .../org/eclipse/che/git/impl/AddTest.java | 26 +++++++++ .../che/git/impl/jgit/JGitConnection.java | 19 ++++++ 3 files changed, 67 insertions(+), 36 deletions(-) diff --git a/plugins/plugin-git/che-plugin-git-ext-git/src/main/java/org/eclipse/che/ide/ext/git/client/action/AddToIndexAction.java b/plugins/plugin-git/che-plugin-git-ext-git/src/main/java/org/eclipse/che/ide/ext/git/client/action/AddToIndexAction.java index afd4dfb9175..4e55577df6e 100644 --- a/plugins/plugin-git/che-plugin-git-ext-git/src/main/java/org/eclipse/che/ide/ext/git/client/action/AddToIndexAction.java +++ b/plugins/plugin-git/che-plugin-git-ext-git/src/main/java/org/eclipse/che/ide/ext/git/client/action/AddToIndexAction.java @@ -13,10 +13,6 @@ import com.google.inject.Inject; import com.google.inject.Singleton; -import org.eclipse.che.api.git.shared.Status; -import org.eclipse.che.api.promises.client.Operation; -import org.eclipse.che.api.promises.client.OperationException; -import org.eclipse.che.api.promises.client.PromiseError; import org.eclipse.che.ide.api.action.ActionEvent; import org.eclipse.che.ide.api.app.AppContext; import org.eclipse.che.ide.api.git.GitServiceClient; @@ -41,6 +37,7 @@ * @author Andrey Plotnikov * @author Vlad Zhukovskyi * @author Igor Vinokur + * @author Mykola Morhun **/ @Singleton public class AddToIndexAction extends GitAction { @@ -78,26 +75,20 @@ public void actionPerformed(ActionEvent e) { final GitOutputConsole console = gitOutputConsoleFactory.create(constant.addToIndexCommandName()); consolesPanelPresenter.addCommandOutput(devMachine.getId(), console); service.getStatus(devMachine, appContext.getRootProject().getLocation()) - .then(new Operation() { - @Override - public void apply(Status status) throws OperationException { - if (containsInSelected(status.getUntracked())) { - presenter.showDialog(); - } else if (containsInSelected(status.getModified())) { - addToIndex(console); - } else { - String message = resources.length > 1 ? constant.nothingAddToIndexMultiSelect() : constant.nothingAddToIndex(); - console.print(message); - notificationManager.notify(message); - } + .then(status -> { + if (containsInSelected(status.getUntracked())) { + presenter.showDialog(); + } else if (containsInSelected(status.getModified()) || containsInSelected(status.getMissing())) { + addToIndex(console); + } else { + String message = resources.length > 1 ? constant.nothingAddToIndexMultiSelect() : constant.nothingAddToIndex(); + console.print(message); + notificationManager.notify(message); } }) - .catchError(new Operation() { - @Override - public void apply(PromiseError error) throws OperationException { - console.printError(constant.statusFailed()); - notificationManager.notify(constant.statusFailed(), FAIL, FLOAT_MODE); - } + .catchError(error -> { + console.printError(constant.statusFailed()); + notificationManager.notify(constant.statusFailed(), FAIL, FLOAT_MODE); }); } @@ -109,27 +100,22 @@ private void addToIndex(final GitOutputConsole console) { paths[i] = path.segmentCount() == 0 ? Path.EMPTY : path; } service.add(appContext.getDevMachine(), appContext.getRootProject().getLocation(), false, paths) - .then(new Operation() { - @Override - public void apply(Void arg) throws OperationException { - console.print(constant.addSuccess()); - notificationManager.notify(constant.addSuccess()); - } + .then(voidArg -> { + console.print(constant.addSuccess()); + notificationManager.notify(constant.addSuccess()); }) - .catchError(new Operation() { - @Override - public void apply(PromiseError arg) throws OperationException { - console.printError(constant.addFailed()); - notificationManager.notify(constant.addFailed(), FAIL, FLOAT_MODE); - } + .catchError(error -> { + console.printError(constant.addFailed()); + notificationManager.notify(constant.addFailed(), FAIL, FLOAT_MODE); }); } private boolean containsInSelected(List items) { + Resource[] appContextResources = appContext.getResources(); for (String item : items) { - for (Resource selectedItem : appContext.getResources()) { + for (Resource selectedItem : appContextResources) { String selectedItemPath = selectedItem.getLocation() - .removeFirstSegments(appContext.getRootProject().getLocation().segmentCount()) + .removeFirstSegments(1) // remove project name from path .toString(); if (item.startsWith(selectedItemPath)) { return true; diff --git a/wsagent/che-core-api-git/src/test/java/org/eclipse/che/git/impl/AddTest.java b/wsagent/che-core-api-git/src/test/java/org/eclipse/che/git/impl/AddTest.java index 2373d9f1be9..ea9d5b79db4 100644 --- a/wsagent/che-core-api-git/src/test/java/org/eclipse/che/git/impl/AddTest.java +++ b/wsagent/che-core-api-git/src/test/java/org/eclipse/che/git/impl/AddTest.java @@ -14,6 +14,7 @@ import static org.eclipse.che.git.impl.GitTestUtil.addFile; import static org.eclipse.che.git.impl.GitTestUtil.cleanupTestRepo; import static org.eclipse.che.git.impl.GitTestUtil.connectToInitializedGitRepository; +import static org.eclipse.che.git.impl.GitTestUtil.deleteFile; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; @@ -27,6 +28,7 @@ import org.eclipse.che.api.git.params.CommitParams; import org.eclipse.che.api.git.params.LsFilesParams; import org.eclipse.che.api.git.shared.AddRequest; +import org.eclipse.che.api.git.shared.StatusFormat; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -38,6 +40,7 @@ /** * @author Eugene Voevodin * @author Sergii Kabnashniuk. + * @author Mykola Morhun */ public class AddTest { @@ -94,4 +97,27 @@ public void testAddUpdate(GitConnectionFactory connectionFactory) throws GitExce assertEquals(listFiles.size(), 1); assertTrue(listFiles.get(0).contains("README.txt")); } + + @Test(dataProvider = "GitConnectionFactory", dataProviderClass = GitConnectionFactoryProvider.class) + public void testAddDeletedFile(GitConnectionFactory connectionFactory) throws GitException, IOException { + // given + GitConnection connection = connectToInitializedGitRepository(connectionFactory, repository); + addFile(connection, "README.txt", CONTENT); + addFile(connection, "CHANGELOG.txt", "WE'VE FIXED ALL BUGS"); + connection.add(AddParams.create(ImmutableList.of("README.txt", "CHANGELOG.txt"))); + connection.commit(CommitParams.create("Initial add")); + + // when + // remove file from disk + deleteFile(connection, "CHANGELOG.txt"); + // add all files to index + connection.add(AddParams.create(AddRequest.DEFAULT_PATTERN)); + + // then + // the deleted file is added to index, so it becomes removed for git + List stagedDeletedFiles = connection.status(StatusFormat.SHORT).getRemoved(); + assertEquals(stagedDeletedFiles.size(), 1); + assertEquals(stagedDeletedFiles.get(0), "CHANGELOG.txt"); + } + } diff --git a/wsagent/che-core-git-impl-jgit/src/main/java/org/eclipse/che/git/impl/jgit/JGitConnection.java b/wsagent/che-core-git-impl-jgit/src/main/java/org/eclipse/che/git/impl/jgit/JGitConnection.java index bb4dfbd244e..492d2dfd5d2 100644 --- a/wsagent/che-core-git-impl-jgit/src/main/java/org/eclipse/che/git/impl/jgit/JGitConnection.java +++ b/wsagent/che-core-git-impl-jgit/src/main/java/org/eclipse/che/git/impl/jgit/JGitConnection.java @@ -190,6 +190,7 @@ /** * @author Andrey Parfonov * @author Igor Vinokur + * @author Mykola Morhun */ class JGitConnection implements GitConnection { private static final String REBASE_OPERATION_SKIP = "SKIP"; @@ -275,11 +276,29 @@ public void add(AddParams params) throws GitException { try { addCommand.call(); + + addDeletedFilesToIndex(filePatterns); } catch (GitAPIException exception) { throw new GitException(exception.getMessage(), exception); } } + /** To add deleted files in index it is required to perform git rm on them */ + private void addDeletedFilesToIndex(List filePatterns) throws GitAPIException { + Set deletedFiles = getGit().status().call().getMissing(); + if (!deletedFiles.isEmpty()) { + RmCommand rmCommand = getGit().rm(); + if (filePatterns.contains(".")) { + deletedFiles.forEach(rmCommand::addFilepattern); + } else { + filePatterns.forEach(filePattern -> deletedFiles.stream() + .filter(deletedFile -> deletedFile.startsWith(filePattern)) + .forEach(rmCommand::addFilepattern)); + } + rmCommand.call(); + } + } + @Override public void checkout(CheckoutParams params) throws GitException { CheckoutCommand checkoutCommand = getGit().checkout();