From 5c18fcb81b126a288445d0034ad6e9f6b47a7209 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Sat, 10 Mar 2012 10:02:58 -0800 Subject: [PATCH] Keep submodules in index that are missing in working directory Submodules present in the index but missing from the working directory should not be staged for deletion when AddCommand is called with the update flag set to true. This mirrors the behavior of CGit. Submodules can still be staged for deletion by running by using the RmCommand. Change-Id: Iee508a67f9621269d1c28d422f88c6b8dd9f8e6e --- .../org/eclipse/jgit/api/AddCommandTest.java | 37 +++++++++++++++++++ .../src/org/eclipse/jgit/api/AddCommand.java | 5 ++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java index 2fb228e01..07173a9c3 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java @@ -44,7 +44,9 @@ package org.eclipse.jgit.api; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.File; @@ -61,6 +63,7 @@ import org.eclipse.jgit.lib.FileMode; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectInserter; +import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.RepositoryTestCase; import org.eclipse.jgit.lib.StoredConfig; import org.eclipse.jgit.revwalk.RevCommit; @@ -684,6 +687,40 @@ public boolean canExecute(File f) { assertEquals(FileMode.EXECUTABLE_FILE, walk.getFileMode(0)); } + @Test + public void testSubmoduleDeleteNotStagedWithUpdate() throws Exception { + Git git = new Git(db); + writeTrashFile("file.txt", "content"); + git.add().addFilepattern("file.txt").call(); + assertNotNull(git.commit().setMessage("create file").call()); + + SubmoduleAddCommand command = new SubmoduleAddCommand(db); + String path = "sub"; + command.setPath(path); + String uri = db.getDirectory().toURI().toString(); + command.setURI(uri); + Repository repo = command.call(); + assertNotNull(repo); + assertNotNull(git.commit().setMessage("add submodule").call()); + + assertTrue(git.status().call().isClean()); + + FileUtils.delete(repo.getWorkTree(), FileUtils.RECURSIVE); + FileUtils.mkdir(new File(db.getWorkTree(), path), false); + + assertNotNull(git.add().addFilepattern(".").setUpdate(true).call()); + + Status status = git.status().call(); + assertFalse(status.isClean()); + assertTrue(status.getAdded().isEmpty()); + assertTrue(status.getChanged().isEmpty()); + assertTrue(status.getRemoved().isEmpty()); + assertTrue(status.getUntracked().isEmpty()); + assertTrue(status.getModified().isEmpty()); + assertEquals(1, status.getMissing().size()); + assertEquals(path, status.getMissing().iterator().next()); + } + private DirCacheEntry addEntryToBuilder(String path, File file, ObjectInserter newObjectInserter, DirCacheBuilder builder, int stage) throws IOException { diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/AddCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/AddCommand.java index f3e47ae01..867945362 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/AddCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/AddCommand.java @@ -203,9 +203,10 @@ else if (!(path.equals(lastAddedFile))) { builder.add(c.getDirCacheEntry()); } - } else if (!update){ + } else if (c != null + && (!update || FileMode.GITLINK == c + .getEntryFileMode())) builder.add(c.getDirCacheEntry()); - } } } }