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

[PATCH] submodule helper: accept '.' for repositories with no submodules

From
Stefan Beller <sbeller@google.com>
Date
Mar 22, 2016, 17:59 UTC
Message-ID
<1458669579-32160-1-git-send-email-sbeller@google.com>

In 74703a1e4d (2015-09-02, submodule: rewrite `module_list` shell function in C), "submodule deinit ." was broken.

The original module_list accepted '.' as a pathspec just fine, as it was using

  git ls-files -z --error-unmatch --stage -- "$@" || { custom filtering}

and git ls-files doesn't make a difference between "." and no arguments there. When using the parse_pathspec function in C, this is a difference however, when no path matches.

'submodule deinit' asks users to explicitely to give '.' instead of empty arguments to specify all submodules (as a safety measure?), so we have to support that as well.

Add a test case to prevent this error coming up again and fix this by special casing '.' in the new module_list to reduce the difference between the old and new module_list.

Signed-off-by: Stefan Beller <sbeller@google.com>
---
 This applies on v2.7.4
 
 I looked at alternatives of how to fix it, e.g.
 later in module_list to make an exception for calling
        if (ps_matched && report_path_error(ps_matched, pathspec, prefix))
             result = -1;
 but that is similarly ugly.
 builtin/submodule--helper.c |  8 ++++++++
 t/t7400-submodule-basic.sh  | 10 ++++++++++
 2 files changed, 18 insertions(+)
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index ed764c9..47e6839 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -23,6 +23,14 @@ static int module_list_compute(int argc, const char **argv,
 {
 	int i, result = 0;
 	char *ps_matched = NULL;
+
+	/*
+	 * We need to treat a path spec of '.' the same as an empty
+	 * path spec, because "submodule deinit" wants to be given '.'
+	 * instead of an empty list.
+	 */
+	if (argc == 1 && !strcmp(".", argv[0]))
+		argv[0] = NULL;
 	parse_pathspec(pathspec, 0,
 		       PATHSPEC_PREFER_FULL |
 		       PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh
index be82a75..fdf7105 100755
--- a/t/t7400-submodule-basic.sh
+++ b/t/t7400-submodule-basic.sh
@@ -849,6 +849,16 @@ test_expect_success 'set up a second submodule' '
 	git commit -m "submodule example2 added"
 '
 
+test_expect_success 'submodule deinit -f . works on empty repository' '
+	test_when_finished "rm -rf newdirectory" &&
+	mkdir newdirectory &&
+	(
+		cd newdirectory &&
+		git init &&
+		git submodule deinit .
+	)
+'
+
 test_expect_success 'submodule deinit should remove the whole submodule section from .git/config' '
 	git config submodule.example.foo bar &&
 	git config submodule.example2.frotz nitfol &&
-- 
2.7.4.1.g33fcf9d
Next: Junio C Hamano
Message 1 of 10 in “submodule helper: accept '.' for repositories with no submodules”
  1. submodule helper: accept '.' for repositories with no submodulesStefan Beller, Mar 22, 2016
  2. Junio C HamanoMar 22, 2016
  3. Stefan BellerMar 22, 2016
  4. Junio C HamanoMar 22, 2016
  5. Stefan BellerMar 22, 2016
  6. Junio C HamanoMar 22, 2016
  7. Stefan BellerMar 22, 2016
  8. Junio C HamanoMar 22, 2016
  9. Stefan BellerMar 22, 2016
  10. Junio C HamanoMar 22, 2016

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.