]> source.dussan.org Git - jgit.git/commitdiff
Ignore submodule on checkout instead of deleting it 92/4192/5
authorRobin Rosenberg <robin.rosenberg@dewire.com>
Sat, 3 Sep 2011 20:54:37 +0000 (22:54 +0200)
committerRobin Rosenberg <robin.rosenberg@dewire.com>
Thu, 8 Sep 2011 14:46:20 +0000 (16:46 +0200)
The purpose of this commit is to prevent destruction of
submodules on checkout from a tree with a submodule to
another. For consistency we handle the reverse case too,
when we checkout a branch that has a submodule and the
submodule directory exists. And finally we ignore the
case where the submodule changes.

We do not update the submodules, we just try to ignore
them harder.

Bug: 356664
Change-Id: I202c695a57af99b13d0d7220803fd08def3d9b5e
Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java

index f0ca574e3c86b625e4ff48546a93f4f93c95d6a9..ee162dd50699de327218a2002a6050156f26fe1c 100644 (file)
@@ -414,9 +414,15 @@ public class DirCacheCheckout {
                        for (int i = removed.size() - 1; i >= 0; i--) {
                                String r = removed.get(i);
                                file = new File(repo.getWorkTree(), r);
-                               if (!file.delete() && file.exists())
-                                       toBeDeleted.add(r);
-                               else {
+                               if (!file.delete() && file.exists()) {
+                                       // The list of stuff to delete comes from the index
+                                       // which will only contain a directory if it is
+                                       // a submodule, in which case we shall not attempt
+                                       // to delete it. A submodule is not empty, so it
+                                       // is safe to check this after a failed delete.
+                                       if (!file.isDirectory())
+                                               toBeDeleted.add(r);
+                               } else {
                                        if (!isSamePrefix(r, last))
                                                removeEmptyParents(new File(repo.getWorkTree(), last));
                                        last = r;
@@ -654,11 +660,14 @@ public class DirCacheCheckout {
                if (i == null) {
                        // make sure not to overwrite untracked files
                        if (f != null) {
-                               // a dirty worktree: the index is empty but we have a
-                               // workingtree-file
-                               if (mId == null || !mId.equals(f.getEntryObjectId())) {
-                                       conflict(name, null, h, m);
-                                       return;
+                               // A submodule is not a file. We should ignore it
+                               if (!FileMode.GITLINK.equals(m.getEntryFileMode())) {
+                                       // a dirty worktree: the index is empty but we have a
+                                       // workingtree-file
+                                       if (mId == null || !mId.equals(f.getEntryObjectId())) {
+                                               conflict(name, null, h, m);
+                                               return;
+                                       }
                                }
                        }
 
@@ -720,21 +729,33 @@ public class DirCacheCheckout {
                                 * </pre>
                                 */
 
-                               if (hId.equals(iId)) {
-                                       if (f == null || f.isModified(dce, true))
+                               if (dce.getFileMode() == FileMode.GITLINK) {
+                                       // Submodules that disappear from the checkout must
+                                       // be removed from the index, but not deleted from disk.
+                                       remove(name);
+                               } else {
+                                       if (hId.equals(iId)) {
+                                               if (f == null || f.isModified(dce, true))
+                                                       conflict(name, dce, h, m);
+                                               else
+                                                       remove(name);
+                                       } else
                                                conflict(name, dce, h, m);
-                                       else
-                                               remove(name);
-                               } else
-                                       conflict(name, dce, h, m);
+                               }
                        } else {
                                if (!hId.equals(mId) && !hId.equals(iId) && !mId.equals(iId))
                                        conflict(name, dce, h, m);
                                else if (hId.equals(iId) && !mId.equals(iId)) {
-                                       if (dce != null && (f == null || f.isModified(dce, true)))
+                                       // For submodules just update the index with the new SHA-1
+                                       if (dce != null
+                                                       && FileMode.GITLINK.equals(dce.getFileMode())) {
+                                               update(name, mId, m.getEntryFileMode());
+                                       } else if (dce != null
+                                                       && (f == null || f.isModified(dce, true))) {
                                                conflict(name, dce, h, m);
-                                       else
+                                       } else {
                                                update(name, mId, m.getEntryFileMode());
+                                       }
                                } else {
                                        keep(dce);
                                }