Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
CHE-4015 Add deleted files to index(Regression) (#4187)
CHE-4015: Fix deleted file is not added to index
  • Loading branch information
Mykola Morhun committed Feb 27, 2017
1 parent d76ba5c commit 7187062
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 36 deletions.
Expand Up @@ -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;
Expand All @@ -41,6 +37,7 @@
* @author Andrey Plotnikov
* @author Vlad Zhukovskyi
* @author Igor Vinokur
* @author Mykola Morhun
**/
@Singleton
public class AddToIndexAction extends GitAction {
Expand Down Expand Up @@ -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<Status>() {
@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<PromiseError>() {
@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);
});
}

Expand All @@ -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<Void>() {
@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<PromiseError>() {
@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<String> 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;
Expand Down
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -38,6 +40,7 @@
/**
* @author Eugene Voevodin
* @author Sergii Kabnashniuk.
* @author Mykola Morhun
*/
public class AddTest {

Expand Down Expand Up @@ -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<String> stagedDeletedFiles = connection.status(StatusFormat.SHORT).getRemoved();
assertEquals(stagedDeletedFiles.size(), 1);
assertEquals(stagedDeletedFiles.get(0), "CHANGELOG.txt");
}

}
Expand Up @@ -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";
Expand Down Expand Up @@ -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<String> filePatterns) throws GitAPIException {
Set<String> 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();
Expand Down

0 comments on commit 7187062

Please sign in to comment.