@matheusbsbs / matheus-bagliano / commits / f3670a5749
Fork 0

Let ls-files strip trailing slashes in submodules' paths

Tab completion makes it easy to add a trailing slash to a submodule path. As it is completely clear what the user actually wanted to say, be nice and strip that slash at the end. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Feb 7, 2009 at 14:43 UTC f3670a5749d704fe1edee4201f9b23adbf0bf967
2 files changed +26 -1
builtin-ls-files.c
+20 -1
@@ -262,6 +262,21 @@ static const char *verify_pathspec(const char *prefix)
262 return max ? xmemdupz(prev, max) : NULL;
263 }
264
265 +static void strip_trailing_slash_from_submodules(void)
266 +{
267 + const char **p;
268 +
269 + for (p = pathspec; *p != NULL; p++) {
270 + int len = strlen(*p), pos;
271 +
272 + if (len < 1 || (*p)[len - 1] != '/')
273 + continue;
274 + pos = cache_name_pos(*p, len - 1);
275 + if (pos >= 0 && S_ISGITLINK(active_cache[pos]->ce_mode))
276 + *p = xstrndup(*p, len - 1);
277 + }
278 +}
279 +
280 /*
281 * Read the tree specified with --with-tree option
282 * (typically, HEAD) into stage #1 and then
@@ -510,6 +525,11 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
525
526 pathspec = get_pathspec(prefix, argv + i);
527
528 + /* be nice with submodule patsh ending in a slash */
529 + read_cache();
530 + if (pathspec)
531 + strip_trailing_slash_from_submodules();
532 +
533 /* Verify that the pathspec matches the prefix */
534 if (pathspec)
535 prefix = verify_pathspec(prefix);
@@ -533,7 +553,6 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
553 show_killed | show_modified))
554 show_cached = 1;
555
536 - read_cache();
556 if (prefix)
557 prune_cache(prefix);
558 if (with_tree) {
t/t7400-submodule-basic.sh
+6
@@ -234,4 +234,10 @@ test_expect_success 'gracefully add submodule with a trailing slash' '
234
235 '
236
237 +test_expect_success 'ls-files gracefully handles trailing slash' '
238 +
239 + test "init" = "$(git ls-files init/)"
240 +
241 +'
242 +
243 test_done