Skip to content

Commit 53e4c5d

Browse files
jlehmanngitster
authored andcommitted
Teach rm to remove submodules when given with a trailing '/'
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3469c7e commit 53e4c5d

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

builtin/rm.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,21 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
234234
if (read_cache() < 0)
235235
die(_("index file corrupt"));
236236

237+
/*
238+
* Drop trailing directory separators from directories so we'll find
239+
* submodules in the index.
240+
*/
241+
for (i = 0; i < argc; i++) {
242+
size_t pathlen = strlen(argv[i]);
243+
if (pathlen && is_dir_sep(argv[i][pathlen - 1]) &&
244+
is_directory(argv[i])) {
245+
do {
246+
pathlen--;
247+
} while (pathlen && is_dir_sep(argv[i][pathlen - 1]));
248+
argv[i] = xmemdupz(argv[i], pathlen);
249+
}
250+
}
251+
237252
pathspec = get_pathspec(prefix, argv);
238253
refresh_index(&the_index, REFRESH_QUIET, pathspec, NULL, NULL);
239254

t/t3600-rm.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,23 @@ test_expect_success 'rm removes work tree of unmodified submodules' '
302302
test_cmp expect actual
303303
'
304304

305+
test_expect_success 'rm removes a submodule with a trailing /' '
306+
git reset --hard &&
307+
git submodule update &&
308+
git rm submod/ &&
309+
test ! -d submod &&
310+
git status -s -uno --ignore-submodules=none > actual &&
311+
test_cmp expect actual
312+
'
313+
314+
test_expect_success 'rm fails when given a file with a trailing /' '
315+
test_must_fail git rm empty/
316+
'
317+
318+
test_expect_success 'rm succeeds when given a directory with a trailing /' '
319+
git rm -r frotz/
320+
'
321+
305322
test_expect_success 'rm of a populated submodule with different HEAD fails unless forced' '
306323
git reset --hard &&
307324
git submodule update &&

0 commit comments

Comments
 (0)