Teach rm to remove submodules when given with a trailing '/'
authorJens Lehmann <Jens.Lehmann@web.de>
Thu, 22 Nov 2012 22:32:26 +0000 (23:32 +0100)
committerJunio C Hamano <gitster@pobox.com>
Sat, 24 Nov 2012 02:35:15 +0000 (18:35 -0800)
Doing a "git rm submod/" on a submodule results in an error:
fatal: pathspec 'submod/' did not match any files
This is really inconvenient as e.g. using TAB completion in a shell on a
submodule automatically adds the trailing '/' when it completes the path
of the submodule directory. The user has then to remove the '/' herself to
make a "git rm" succeed. Doing a "git rm -r somedir/" is working fine, so
there is no reason why that shouldn't work for submodules too.

Teach git rm to not error out when a '/' is appended to the path of a
submodule. Achieve this by chopping off trailing slashes from the path
names given if they represent directories. Add tests to make sure that
logic only applies to directories and not to files.

Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/rm.c
t/t3600-rm.sh
index 4a881ab27fd4efa0bb16955bb2e8944c1461b11c..4e5e8abeb6a36a13cf272061fb4ee9e129d31740 100644 (file)
@@ -234,6 +234,21 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
        if (read_cache() < 0)
                die(_("index file corrupt"));
 
+       /*
+        * Drop trailing directory separators from directories so we'll find
+        * submodules in the index.
+        */
+       for (i = 0; i < argc; i++) {
+               size_t pathlen = strlen(argv[i]);
+               if (pathlen && is_dir_sep(argv[i][pathlen - 1]) &&
+                   is_directory(argv[i])) {
+                       do {
+                               pathlen--;
+                       } while (pathlen && is_dir_sep(argv[i][pathlen - 1]));
+                       argv[i] = xmemdupz(argv[i], pathlen);
+               }
+       }
+
        pathspec = get_pathspec(prefix, argv);
        refresh_index(&the_index, REFRESH_QUIET, pathspec, NULL, NULL);
 
index 97254e8d337ff066f633ece5d1a7c8a59108e703..06f63848eab384ae33eacd1fb46d5dc048143721 100755 (executable)
@@ -302,6 +302,23 @@ test_expect_success 'rm removes work tree of unmodified submodules' '
        test_cmp expect actual
 '
 
+test_expect_success 'rm removes a submodule with a trailing /' '
+       git reset --hard &&
+       git submodule update &&
+       git rm submod/ &&
+       test ! -d submod &&
+       git status -s -uno --ignore-submodules=none > actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'rm fails when given a file with a trailing /' '
+       test_must_fail git rm empty/
+'
+
+test_expect_success 'rm succeeds when given a directory with a trailing /' '
+       git rm -r frotz/
+'
+
 test_expect_success 'rm of a populated submodule with different HEAD fails unless forced' '
        git reset --hard &&
        git submodule update &&