{"thread":{"id":"31659","subject":"[PATCH v4] Teach rm to remove submodules unless they contain a git directory","startedAt":"2012-09-26T18:21:13Z","lastAt":"2012-09-29T14:23:39Z","messageCount":3,"participants":["Jens Lehmann","Junio C Hamano"],"isPatch":true,"patchVersion":4,"patchTotal":null},"messages":[{"id":"199956","messageId":"50634799.7090807@web.de","threadId":"31659","inReplyTo":null,"subject":"[PATCH v4] Teach rm to remove submodules unless they contain a git directory","fromName":"Jens Lehmann","fromEmail":"jens.lehmann@web.de","sentAt":"2012-09-26T18:21:13Z","receivedAt":"2012-09-26T18:21:13Z","isPatch":true,"sender":{"key":"jens.lehmann@web.de","avatar":"https://avatars.githubusercontent.com/u/135220?v=4"},"body":"Currently using \"git rm\" on a submodule - populated or not - fails with\nthis error:\n\tfatal: git rm: '<submodule path>': Is a directory\nThis made sense in the past as there was no way to remove a submodule\nwithout possibly removing unpushed parts of the submodule's history\ncontained in its .git directory too, so erroring out here protected the\nuser from possible loss of data.\n\nBut submodules cloned with a recent git version do not contain the .git\ndirectory anymore, they use a gitfile to point to their git directory\nwhich is safely stored inside the superproject's .git directory. The work\ntree of these submodules can safely be removed without loosing history, so\nlet's teach git to do so.\n\nUsing rm on an unpopulated submodule now removes the empty directory from\nthe work tree and the gitlink from the index. If the submodule's directory\nis missing from the work tree, it will still be removed from the index.\n\nUsing rm on a populated submodule using a gitfile will apply the usual\nchecks for work tree modification adapted to submodules (unless forced).\nFor a submodule that means that the HEAD is the same as recorded in the\nindex, no tracked files are modified and no untracked files that aren't\nignored are present in the submodules work tree (ignored files are deemed\nexpendable and won't stop a submodule's work tree from being removed).\nThat logic has to be applied in all nested submodules too.\n\nUsing rm on a submodule which has its .git directory inside the work trees\ntop level directory will just error out like it did before to protect the\nrepository, even when forced. In the future git could either provide a\nmessage informing the user to convert the submodule to use a gitfile or\neven attempt to do the conversion itself, but that is not part of this\nchange.\n\nSigned-off-by: Jens Lehmann <Jens.Lehmann@web.de>\n---\n\n\nChanges since v3:\n- Added get_ours_cache_pos() helper to only check stage 2 of a conflict\n- Added tests for modified submodules in the conflict case\n\n\n Documentation/git-rm.txt |  15 +++\n builtin/rm.c             | 125 ++++++++++++++---\n submodule.c              |  80 +++++++++++\n submodule.h              |   2 +\n t/t3600-rm.sh            | 343 +++++++++++++++++++++++++++++++++++++++++++++++\n 5 files changed, 550 insertions(+), 15 deletions(-)\n\ndiff --git a/Documentation/git-rm.txt b/Documentation/git-rm.txt\nindex 5d31860..882cb11 100644\n--- a/Documentation/git-rm.txt\n+++ b/Documentation/git-rm.txt\n@@ -107,6 +107,21 @@ as well as modifications of existing paths.\n Typically you would first remove all tracked files from the working\n tree using this command:\n\n+Submodules\n+~~~~~~~~~~\n+Only submodules using a gitfile (which means they were cloned\n+with a git version 1.7.8 or newer) will be removed from the work\n+tree, as their repository lives inside the .git directory of the\n+superproject. If a submodule (or one of those nested inside it)\n+still uses a .git directory, `git rm` will fail - no matter if forced\n+or not - to protect the submodule's history.\n+\n+A submodule is considered up-to-date when the HEAD is the same as\n+recorded in the index, no tracked files are modified and no untracked\n+files that aren't ignored are present in the submodules work tree.\n+Ignored files are deemed expendable and won't stop a submodule's work\n+tree from being removed.\n+\n ----------------\n git ls-files -z | xargs -0 rm -f\n ----------------\ndiff --git a/builtin/rm.c b/builtin/rm.c\nindex b384c4c..99dcbe6 100644\n--- a/builtin/rm.c\n+++ b/builtin/rm.c\n@@ -9,6 +9,7 @@\n #include \"cache-tree.h\"\n #include \"tree-walk.h\"\n #include \"parse-options.h\"\n+#include \"submodule.h\"\n\n static const char * const builtin_rm_usage[] = {\n \tN_(\"git rm [options] [--] <file>...\"),\n@@ -17,9 +18,58 @@ static const char * const builtin_rm_usage[] = {\n\n static struct {\n \tint nr, alloc;\n-\tconst char **name;\n+\tstruct {\n+\t\tconst char *name;\n+\t\tchar is_submodule;\n+\t} *entry;\n } list;\n\n+static int get_ours_cache_pos(const char *path, int pos)\n+{\n+\tint i = -pos - 1;\n+\n+\twhile ((i < active_nr) && !strcmp(active_cache[i]->name, path)) {\n+\t\tif (ce_stage(active_cache[i]) == 2)\n+\t\t\treturn i;\n+\t\ti++;\n+\t}\n+\treturn -1;\n+}\n+\n+static int check_submodules_use_gitfiles(void)\n+{\n+\tint i;\n+\tint errs = 0;\n+\n+\tfor (i = 0; i < list.nr; i++) {\n+\t\tconst char *name = list.entry[i].name;\n+\t\tint pos;\n+\t\tstruct cache_entry *ce;\n+\t\tstruct stat st;\n+\n+\t\tpos = cache_name_pos(name, strlen(name));\n+\t\tif (pos < 0) {\n+\t\t\tpos = get_ours_cache_pos(name, pos);\n+\t\t\tif (pos < 0)\n+\t\t\t\tcontinue;\n+\t\t}\n+\t\tce = active_cache[pos];\n+\n+\t\tif (!S_ISGITLINK(ce->ce_mode) ||\n+\t\t    (lstat(ce->name, &st) < 0) ||\n+\t\t    is_empty_dir(name))\n+\t\t\tcontinue;\n+\n+\t\tif (!submodule_uses_gitfile(name))\n+\t\t\terrs = error(_(\"submodule '%s' (or one of its nested \"\n+\t\t\t\t     \"submodules) uses a .git directory\\n\"\n+\t\t\t\t     \"(use 'rm -rf' if you really want to remove \"\n+\t\t\t\t     \"it including all of its history)\"), name);\n+\t}\n+\n+\treturn errs;\n+}\n+\n static int check_local_mod(unsigned char *head, int index_only)\n {\n \t/*\n@@ -37,15 +87,26 @@ static int check_local_mod(unsigned char *head, int index_only)\n \t\tstruct stat st;\n \t\tint pos;\n \t\tstruct cache_entry *ce;\n-\t\tconst char *name = list.name[i];\n+\t\tconst char *name = list.entry[i].name;\n \t\tunsigned char sha1[20];\n \t\tunsigned mode;\n \t\tint local_changes = 0;\n \t\tint staged_changes = 0;\n\n \t\tpos = cache_name_pos(name, strlen(name));\n-\t\tif (pos < 0)\n-\t\t\tcontinue; /* removing unmerged entry */\n+\t\tif (pos < 0) {\n+\t\t\t/*\n+\t\t\t * Skip unmerged entries except for populated submodules\n+\t\t\t * that could loose history when removed.\n+\t\t\t */\n+\t\t\tpos = get_ours_cache_pos(name, pos);\n+\t\t\tif (pos < 0)\n+\t\t\t\tcontinue;\n+\n+\t\t\tif (!S_ISGITLINK(active_cache[pos]->ce_mode) ||\n+\t\t\t    is_empty_dir(name))\n+\t\t\t\tcontinue;\n+\t\t}\n \t\tce = active_cache[pos];\n\n \t\tif (lstat(ce->name, &st) < 0) {\n@@ -58,9 +119,10 @@ static int check_local_mod(unsigned char *head, int index_only)\n \t\t\t/* if a file was removed and it is now a\n \t\t\t * directory, that is the same as ENOENT as\n \t\t\t * far as git is concerned; we do not track\n-\t\t\t * directories.\n+\t\t\t * directories unless they are submodules.\n \t\t\t */\n-\t\t\tcontinue;\n+\t\t\tif (!S_ISGITLINK(ce->ce_mode))\n+\t\t\t\tcontinue;\n \t\t}\n\n \t\t/*\n@@ -80,8 +142,11 @@ static int check_local_mod(unsigned char *head, int index_only)\n\n \t\t/*\n \t\t * Is the index different from the file in the work tree?\n+\t\t * If it's a submodule, is its work tree modified?\n \t\t */\n-\t\tif (ce_match_stat(ce, &st, 0))\n+\t\tif (ce_match_stat(ce, &st, 0) ||\n+\t\t    (S_ISGITLINK(ce->ce_mode) &&\n+\t\t     !ok_to_remove_submodule(ce->name)))\n \t\t\tlocal_changes = 1;\n\n \t\t/*\n@@ -115,10 +180,18 @@ static int check_local_mod(unsigned char *head, int index_only)\n \t\t\t\terrs = error(_(\"'%s' has changes staged in the index\\n\"\n \t\t\t\t\t     \"(use --cached to keep the file, \"\n \t\t\t\t\t     \"or -f to force removal)\"), name);\n-\t\t\tif (local_changes)\n-\t\t\t\terrs = error(_(\"'%s' has local modifications\\n\"\n-\t\t\t\t\t     \"(use --cached to keep the file, \"\n-\t\t\t\t\t     \"or -f to force removal)\"), name);\n+\t\t\tif (local_changes) {\n+\t\t\t\tif (S_ISGITLINK(ce->ce_mode) &&\n+\t\t\t\t    !submodule_uses_gitfile(name)) {\n+\t\t\t\t\terrs = error(_(\"submodule '%s' (or one of its nested \"\n+\t\t\t\t\t\t     \"submodules) uses a .git directory\\n\"\n+\t\t\t\t\t\t     \"(use 'rm -rf' if you really want to remove \"\n+\t\t\t\t\t\t     \"it including all of its history)\"), name);\n+\t\t\t\t} else\n+\t\t\t\t\terrs = error(_(\"'%s' has local modifications\\n\"\n+\t\t\t\t\t\t     \"(use --cached to keep the file, \"\n+\t\t\t\t\t\t     \"or -f to force removal)\"), name);\n+\t\t\t}\n \t\t}\n \t}\n \treturn errs;\n@@ -173,8 +246,9 @@ int cmd_rm(int argc, const char **argv, const char *prefix)\n \t\tstruct cache_entry *ce = active_cache[i];\n \t\tif (!match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen))\n \t\t\tcontinue;\n-\t\tALLOC_GROW(list.name, list.nr + 1, list.alloc);\n-\t\tlist.name[list.nr++] = ce->name;\n+\t\tALLOC_GROW(list.entry, list.nr + 1, list.alloc);\n+\t\tlist.entry[list.nr].name = ce->name;\n+\t\tlist.entry[list.nr++].is_submodule = S_ISGITLINK(ce->ce_mode);\n \t}\n\n \tif (pathspec) {\n@@ -215,6 +289,9 @@ int cmd_rm(int argc, const char **argv, const char *prefix)\n \t\t\thashclr(sha1);\n \t\tif (check_local_mod(sha1, index_only))\n \t\t\texit(1);\n+\t} else if (!index_only) {\n+\t\tif (check_submodules_use_gitfiles())\n+\t\t\texit(1);\n \t}\n\n \t/*\n@@ -222,7 +299,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)\n \t * the index unless all of them succeed.\n \t */\n \tfor (i = 0; i < list.nr; i++) {\n-\t\tconst char *path = list.name[i];\n+\t\tconst char *path = list.entry[i].name;\n \t\tif (!quiet)\n \t\t\tprintf(\"rm '%s'\\n\", path);\n\n@@ -244,7 +321,25 @@ int cmd_rm(int argc, const char **argv, const char *prefix)\n \tif (!index_only) {\n \t\tint removed = 0;\n \t\tfor (i = 0; i < list.nr; i++) {\n-\t\t\tconst char *path = list.name[i];\n+\t\t\tconst char *path = list.entry[i].name;\n+\t\t\tif (list.entry[i].is_submodule) {\n+\t\t\t\tif (is_empty_dir(path)) {\n+\t\t\t\t\tif (!rmdir(path)) {\n+\t\t\t\t\t\tremoved = 1;\n+\t\t\t\t\t\tcontinue;\n+\t\t\t\t\t}\n+\t\t\t\t} else {\n+\t\t\t\t\tstruct strbuf buf = STRBUF_INIT;\n+\t\t\t\t\tstrbuf_addstr(&buf, path);\n+\t\t\t\t\tif (!remove_dir_recursively(&buf, 0)) {\n+\t\t\t\t\t\tremoved = 1;\n+\t\t\t\t\t\tstrbuf_release(&buf);\n+\t\t\t\t\t\tcontinue;\n+\t\t\t\t\t}\n+\t\t\t\t\tstrbuf_release(&buf);\n+\t\t\t\t\t/* Fallthrough and let remove_path() fail. */\n+\t\t\t\t}\n+\t\t\t}\n \t\t\tif (!remove_path(path)) {\n \t\t\t\tremoved = 1;\n \t\t\t\tcontinue;\ndiff --git a/submodule.c b/submodule.c\nindex 50f213e..e3e0b45 100644\n--- a/submodule.c\n+++ b/submodule.c\n@@ -759,6 +759,86 @@ unsigned is_submodule_modified(const char *path, int ignore_untracked)\n \treturn dirty_submodule;\n }\n\n+int submodule_uses_gitfile(const char *path)\n+{\n+\tstruct child_process cp;\n+\tconst char *argv[] = {\n+\t\t\"submodule\",\n+\t\t\"foreach\",\n+\t\t\"--quiet\",\n+\t\t\"--recursive\",\n+\t\t\"test -f .git\",\n+\t\tNULL,\n+\t};\n+\tstruct strbuf buf = STRBUF_INIT;\n+\tconst char *git_dir;\n+\n+\tstrbuf_addf(&buf, \"%s/.git\", path);\n+\tgit_dir = read_gitfile(buf.buf);\n+\tif (!git_dir) {\n+\t\tstrbuf_release(&buf);\n+\t\treturn 0;\n+\t}\n+\tstrbuf_release(&buf);\n+\n+\t/* Now test that all nested submodules use a gitfile too */\n+\tmemset(&cp, 0, sizeof(cp));\n+\tcp.argv = argv;\n+\tcp.env = local_repo_env;\n+\tcp.git_cmd = 1;\n+\tcp.no_stdin = 1;\n+\tcp.no_stderr = 1;\n+\tcp.no_stdout = 1;\n+\tcp.dir = path;\n+\tif (run_command(&cp))\n+\t\treturn 0;\n+\n+\treturn 1;\n+}\n+\n+int ok_to_remove_submodule(const char *path)\n+{\n+\tstruct stat st;\n+\tssize_t len;\n+\tstruct child_process cp;\n+\tconst char *argv[] = {\n+\t\t\"status\",\n+\t\t\"--porcelain\",\n+\t\t\"-u\",\n+\t\t\"--ignore-submodules=none\",\n+\t\tNULL,\n+\t};\n+\tstruct strbuf buf = STRBUF_INIT;\n+\tint ok_to_remove = 1;\n+\n+\tif ((lstat(path, &st) < 0) || is_empty_dir(path))\n+\t\treturn 1;\n+\n+\tif (!submodule_uses_gitfile(path))\n+\t\treturn 0;\n+\n+\tmemset(&cp, 0, sizeof(cp));\n+\tcp.argv = argv;\n+\tcp.env = local_repo_env;\n+\tcp.git_cmd = 1;\n+\tcp.no_stdin = 1;\n+\tcp.out = -1;\n+\tcp.dir = path;\n+\tif (start_command(&cp))\n+\t\tdie(\"Could not run 'git status --porcelain -uall --ignore-submodules=none' in submodule %s\", path);\n+\n+\tlen = strbuf_read(&buf, cp.out, 1024);\n+\tif (len > 2)\n+\t\tok_to_remove = 0;\n+\tclose(cp.out);\n+\n+\tif (finish_command(&cp))\n+\t\tdie(\"'git status --porcelain -uall --ignore-submodules=none' failed in submodule %s\", path);\n+\n+\tstrbuf_release(&buf);\n+\treturn ok_to_remove;\n+}\n+\n static int find_first_merges(struct object_array *result, const char *path,\n \t\tstruct commit *a, struct commit *b)\n {\ndiff --git a/submodule.h b/submodule.h\nindex 594b50d..f2e8271 100644\n--- a/submodule.h\n+++ b/submodule.h\n@@ -28,6 +28,8 @@ int fetch_populated_submodules(const struct argv_array *options,\n \t\t\t       const char *prefix, int command_line_option,\n \t\t\t       int quiet);\n unsigned is_submodule_modified(const char *path, int ignore_untracked);\n+int submodule_uses_gitfile(const char *path);\n+int ok_to_remove_submodule(const char *path);\n int merge_submodule(unsigned char result[20], const char *path, const unsigned char base[20],\n \t\t    const unsigned char a[20], const unsigned char b[20], int search);\n int find_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name,\ndiff --git a/t/t3600-rm.sh b/t/t3600-rm.sh\nindex 9fd28bc..97254e8 100755\n--- a/t/t3600-rm.sh\n+++ b/t/t3600-rm.sh\n@@ -262,4 +262,347 @@ test_expect_success 'rm removes subdirectories recursively' '\n \t! test -d dir\n '\n\n+cat >expect <<EOF\n+D  submod\n+EOF\n+\n+cat >expect.modified <<EOF\n+ M submod\n+EOF\n+\n+test_expect_success 'rm removes empty submodules from work tree' '\n+\tmkdir submod &&\n+\tgit update-index --add --cacheinfo 160000 $(git rev-parse HEAD) submod &&\n+\tgit config -f .gitmodules submodule.sub.url ./. &&\n+\tgit config -f .gitmodules submodule.sub.path submod &&\n+\tgit submodule init &&\n+\tgit add .gitmodules &&\n+\tgit commit -m \"add submodule\" &&\n+\tgit rm submod &&\n+\ttest ! -e submod &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'rm removes removed submodule from index' '\n+\tgit reset --hard &&\n+\tgit submodule update &&\n+\trm -rf submod &&\n+\tgit rm submod &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'rm removes work tree of unmodified submodules' '\n+\tgit reset --hard &&\n+\tgit submodule update &&\n+\tgit rm submod &&\n+\ttest ! -d submod &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'rm of a populated submodule with different HEAD fails unless forced' '\n+\tgit reset --hard &&\n+\tgit submodule update &&\n+\t(cd submod &&\n+\t\tgit checkout HEAD^\n+\t) &&\n+\ttest_must_fail git rm submod &&\n+\ttest -d submod &&\n+\ttest -f submod/.git &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect.modified actual &&\n+\tgit rm -f submod &&\n+\ttest ! -d submod &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'rm of a populated submodule with modifications fails unless forced' '\n+\tgit reset --hard &&\n+\tgit submodule update &&\n+\t(cd submod &&\n+\t\techo X >empty\n+\t) &&\n+\ttest_must_fail git rm submod &&\n+\ttest -d submod &&\n+\ttest -f submod/.git &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect.modified actual &&\n+\tgit rm -f submod &&\n+\ttest ! -d submod &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'rm of a populated submodule with untracked files fails unless forced' '\n+\tgit reset --hard &&\n+\tgit submodule update &&\n+\t(cd submod &&\n+\t\techo X >untracked\n+\t) &&\n+\ttest_must_fail git rm submod &&\n+\ttest -d submod &&\n+\ttest -f submod/.git &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect.modified actual &&\n+\tgit rm -f submod &&\n+\ttest ! -d submod &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'setup submodule conflict' '\n+\tgit reset --hard &&\n+\tgit submodule update &&\n+\tgit checkout -b branch1 &&\n+\techo 1 >nitfol &&\n+\tgit add nitfol &&\n+\tgit commit -m \"added nitfol 1\" &&\n+\tgit checkout -b branch2 master &&\n+\techo 2 >nitfol &&\n+\tgit add nitfol &&\n+\tgit commit -m \"added nitfol 2\" &&\n+\tgit checkout -b conflict1 master &&\n+\t(cd submod &&\n+\t\tgit fetch &&\n+\t\tgit checkout branch1\n+\t) &&\n+\tgit add submod &&\n+\tgit commit -m \"submod 1\" &&\n+\tgit checkout -b conflict2 master &&\n+\t(cd submod &&\n+\t\tgit checkout branch2\n+\t) &&\n+\tgit add submod &&\n+\tgit commit -m \"submod 2\"\n+'\n+\n+cat >expect.conflict <<EOF\n+UU submod\n+EOF\n+\n+test_expect_success 'rm removes work tree of unmodified conflicted submodule' '\n+\tgit checkout conflict1 &&\n+\tgit reset --hard &&\n+\tgit submodule update &&\n+\ttest_must_fail git merge conflict2 &&\n+\tgit rm submod &&\n+\ttest ! -d submod &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'rm of a conflicted populated submodule with different HEAD fails unless forced' '\n+\tgit checkout conflict1 &&\n+\tgit reset --hard &&\n+\tgit submodule update &&\n+\t(cd submod &&\n+\t\tgit checkout HEAD^\n+\t) &&\n+\ttest_must_fail git merge conflict2 &&\n+\ttest_must_fail git rm submod &&\n+\ttest -d submod &&\n+\ttest -f submod/.git &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect.conflict actual &&\n+\tgit rm -f submod &&\n+\ttest ! -d submod &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'rm of a conflicted populated submodule with modifications fails unless forced' '\n+\tgit checkout conflict1 &&\n+\tgit reset --hard &&\n+\tgit submodule update &&\n+\t(cd submod &&\n+\t\techo X >empty\n+\t) &&\n+\ttest_must_fail git merge conflict2 &&\n+\ttest_must_fail git rm submod &&\n+\ttest -d submod &&\n+\ttest -f submod/.git &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect.conflict actual &&\n+\tgit rm -f submod &&\n+\ttest ! -d submod &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'rm of a conflicted populated submodule with untracked files fails unless forced' '\n+\tgit checkout conflict1 &&\n+\tgit reset --hard &&\n+\tgit submodule update &&\n+\t(cd submod &&\n+\t\techo X >untracked\n+\t) &&\n+\ttest_must_fail git merge conflict2 &&\n+\ttest_must_fail git rm submod &&\n+\ttest -d submod &&\n+\ttest -f submod/.git &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect.conflict actual &&\n+\tgit rm -f submod &&\n+\ttest ! -d submod &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'rm of a conflicted populated submodule with a .git directory fails even when forced' '\n+\tgit checkout conflict1 &&\n+\tgit reset --hard &&\n+\tgit submodule update &&\n+\t(cd submod &&\n+\t\trm .git &&\n+\t\tcp -a ../.git/modules/sub .git &&\n+\t\tGIT_WORK_TREE=. git config --unset core.worktree\n+\t) &&\n+\ttest_must_fail git merge conflict2 &&\n+\ttest_must_fail git rm submod &&\n+\ttest -d submod &&\n+\ttest -d submod/.git &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect.conflict actual &&\n+\ttest_must_fail git rm -f submod &&\n+\ttest -d submod &&\n+\ttest -d submod/.git &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect.conflict actual &&\n+\tgit merge --abort &&\n+\trm -rf submod\n+'\n+\n+test_expect_success 'rm of a conflicted unpopulated submodule succeeds' '\n+\tgit checkout conflict1 &&\n+\tgit reset --hard &&\n+\ttest_must_fail git merge conflict2 &&\n+\tgit rm submod &&\n+\ttest ! -d submod &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'rm of a populated submodule with a .git directory fails even when forced' '\n+\tgit checkout -f master &&\n+\tgit reset --hard &&\n+\tgit submodule update &&\n+\t(cd submod &&\n+\t\trm .git &&\n+\t\tcp -a ../.git/modules/sub .git &&\n+\t\tGIT_WORK_TREE=. git config --unset core.worktree\n+\t) &&\n+\ttest_must_fail git rm submod &&\n+\ttest -d submod &&\n+\ttest -d submod/.git &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\t! test -s actual &&\n+\ttest_must_fail git rm -f submod &&\n+\ttest -d submod &&\n+\ttest -d submod/.git &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\t! test -s actual &&\n+\trm -rf submod\n+'\n+\n+cat >expect.deepmodified <<EOF\n+ M submod/subsubmod\n+EOF\n+\n+test_expect_success 'setup subsubmodule' '\n+\tgit reset --hard &&\n+\tgit submodule update &&\n+\t(cd submod &&\n+\t\tgit update-index --add --cacheinfo 160000 $(git rev-parse HEAD) subsubmod &&\n+\t\tgit config -f .gitmodules submodule.sub.url ../. &&\n+\t\tgit config -f .gitmodules submodule.sub.path subsubmod &&\n+\t\tgit submodule init &&\n+\t\tgit add .gitmodules &&\n+\t\tgit commit -m \"add subsubmodule\" &&\n+\t\tgit submodule update subsubmod\n+\t) &&\n+\tgit commit -a -m \"added deep submodule\"\n+'\n+\n+test_expect_success 'rm recursively removes work tree of unmodified submodules' '\n+\tgit rm submod &&\n+\ttest ! -d submod &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'rm of a populated nested submodule with different nested HEAD fails unless forced' '\n+\tgit reset --hard &&\n+\tgit submodule update --recursive &&\n+\t(cd submod/subsubmod &&\n+\t\tgit checkout HEAD^\n+\t) &&\n+\ttest_must_fail git rm submod &&\n+\ttest -d submod &&\n+\ttest -f submod/.git &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect.modified actual &&\n+\tgit rm -f submod &&\n+\ttest ! -d submod &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'rm of a populated nested submodule with nested modifications fails unless forced' '\n+\tgit reset --hard &&\n+\tgit submodule update --recursive &&\n+\t(cd submod/subsubmod &&\n+\t\techo X >empty\n+\t) &&\n+\ttest_must_fail git rm submod &&\n+\ttest -d submod &&\n+\ttest -f submod/.git &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect.modified actual &&\n+\tgit rm -f submod &&\n+\ttest ! -d submod &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'rm of a populated nested submodule with nested untracked files fails unless forced' '\n+\tgit reset --hard &&\n+\tgit submodule update --recursive &&\n+\t(cd submod/subsubmod &&\n+\t\techo X >untracked\n+\t) &&\n+\ttest_must_fail git rm submod &&\n+\ttest -d submod &&\n+\ttest -f submod/.git &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect.modified actual &&\n+\tgit rm -f submod &&\n+\ttest ! -d submod &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'rm of a populated nested submodule with a nested .git directory fails even when forced' '\n+\tgit reset --hard &&\n+\tgit submodule update --recursive &&\n+\t(cd submod/subsubmod &&\n+\t\trm .git &&\n+\t\tcp -a ../../.git/modules/sub/modules/sub .git &&\n+\t\tGIT_WORK_TREE=. git config --unset core.worktree\n+\t) &&\n+\ttest_must_fail git rm submod &&\n+\ttest -d submod &&\n+\ttest -d submod/subsubmod/.git &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\t! test -s actual &&\n+\ttest_must_fail git rm -f submod &&\n+\ttest -d submod &&\n+\ttest -d submod/subsubmod/.git &&\n+\tgit status -s -uno --ignore-submodules=none > actual &&\n+\t! test -s actual &&\n+\trm -rf submod\n+'\n+\n test_done\n-- \n1.7.12.1.429.g2919c44\n"},{"id":"200015","messageId":"7v8vbvs8z6.fsf@alter.siamese.dyndns.org","threadId":"31659","inReplyTo":"50634799.7090807@web.de","subject":"Re: [PATCH v4] Teach rm to remove submodules unless they contain a git directory","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2012-09-27T16:53:33Z","receivedAt":"2012-09-27T16:53:33Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jens Lehmann <Jens.Lehmann@web.de> writes:\n\n> Changes since v3:\n> - Added get_ours_cache_pos() helper to only check stage 2 of a conflict\n> - Added tests for modified submodules in the conflict case\n\nThanks.\n\n> +\t\t\t/*\n> +\t\t\t * Skip unmerged entries except for populated submodules\n> +\t\t\t * that could loose history when removed.\n> +\t\t\t */\n\ns/loose/lose/, I think.\n"},{"id":"200174","messageId":"5067046B.9040109@web.de","threadId":"31659","inReplyTo":"7v8vbvs8z6.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH v4] Teach rm to remove submodules unless they contain a git directory","fromName":"Jens Lehmann","fromEmail":"jens.lehmann@web.de","sentAt":"2012-09-29T14:23:39Z","receivedAt":"2012-09-29T14:23:39Z","isPatch":true,"sender":{"key":"jens.lehmann@web.de","avatar":"https://avatars.githubusercontent.com/u/135220?v=4"},"body":"Am 27.09.2012 18:53, schrieb Junio C Hamano:\n> Jens Lehmann <Jens.Lehmann@web.de> writes:\n>> +\t\t\t/*\n>> +\t\t\t * Skip unmerged entries except for populated submodules\n>> +\t\t\t * that could loose history when removed.\n>> +\t\t\t */\n> \n> s/loose/lose/, I think.\n\nThanks for squashing that one in!\n\nI also used \"loose\" in the commit message where \"lose\" would have been\ncorrect. I can resend a new version correcting both if you want.\n"}]}