git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/2] Let ls-files strip trailing slashes in submodules' paths

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Feb 7, 2009, 13:43 UTC
Message-ID
<2bff02915466796c36d93ac17435f4256c46403f.1234013924u.git.johannes.schindelin@gmx.de>
In-Reply-To
<7vfxiqy76k.fsf@gitster.siamese.dyndns.org>

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>
---
 builtin-ls-files.c         |   21 ++++++++++++++++++++-
 t/t7400-submodule-basic.sh |    6 ++++++
 2 files changed, 26 insertions(+), 1 deletions(-)
diff --git a/builtin-ls-files.c b/builtin-ls-files.c
index 3434031..9dec282 100644
--- a/builtin-ls-files.c
+++ b/builtin-ls-files.c
@@ -262,6 +262,21 @@ static const char *verify_pathspec(const char *prefix)
 	return max ? xmemdupz(prev, max) : NULL;
 }
 
+static void strip_trailing_slash_from_submodules(void)
+{
+	const char **p;
+
+	for (p = pathspec; *p != NULL; p++) {
+		int len = strlen(*p), pos;
+
+		if (len < 1 || (*p)[len - 1] != '/')
+			continue;
+		pos = cache_name_pos(*p, len - 1);
+		if (pos >= 0 && S_ISGITLINK(active_cache[pos]->ce_mode))
+			*p = xstrndup(*p, len - 1);
+	}
+}
+
 /*
  * Read the tree specified with --with-tree option
  * (typically, HEAD) into stage #1 and then
@@ -510,6 +525,11 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
 
 	pathspec = get_pathspec(prefix, argv + i);
 
+	/* be nice with submodule patsh ending in a slash */
+	read_cache();
+	if (pathspec)
+		strip_trailing_slash_from_submodules();
+
 	/* Verify that the pathspec matches the prefix */
 	if (pathspec)
 		prefix = verify_pathspec(prefix);
@@ -533,7 +553,6 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
 	      show_killed | show_modified))
 		show_cached = 1;
 
-	read_cache();
 	if (prefix)
 		prune_cache(prefix);
 	if (with_tree) {
diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh
index 2ec7ac6..a74f24c 100755
--- a/t/t7400-submodule-basic.sh
+++ b/t/t7400-submodule-basic.sh
@@ -234,4 +234,10 @@ test_expect_success 'gracefully add submodule with a trailing slash' '
 
 '
 
+test_expect_success 'ls-files gracefully handles trailing slash' '
+
+	test "init" = "$(git ls-files init/)"
+
+'
+
 test_done
-- 
1.6.1.2.630.g01a7e
Previous: Junio C HamanoNext: Johannes Schindelin
Message 7 of 8 in “submodule: handle trailing slash, warn about non-submodules”
  1. submodule: handle trailing slash, warn about non-submodulesJohannes Schindelin, Feb 6, 2009
  2. Junio C HamanoFeb 6, 2009
  3. Johannes SchindelinFeb 6, 2009
  4. Junio C HamanoFeb 7, 2009
  5. 0/2 submodule path handling fixesJohannes Schindelin, Feb 7, 2009
  6. Junio C HamanoFeb 7, 2009
  7. 1/2 Let ls-files strip trailing slashes in submodules' pathsJohannes Schindelin, Feb 7, 2009
  8. 2/2 submodule: warn about non-submodulesJohannes Schindelin, Feb 7, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.