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

[PATCH v2] submodule: absorb git dir instead of dying on deinit

From
Mugdha Pattnaik via GitGitGadget <gitgitgadget@gmail.com>
Date
Aug 27, 2021, 06:03 UTC
Message-ID
<pull.1078.v2.git.git.1630044219145.gitgitgadget@gmail.com>
In-Reply-To
<pull.1078.git.git.1630002794889.gitgitgadget@gmail.com>
From: mugdha <mugdhapattnaik@gmail.com>

Currently, running 'git submodule deinit' on repos where the submodule's '.git' is a folder aborts with a message that is not exactly user friendly. Let's change this to instead warn the user to rerun the command with '--force'.

This internally calls 'absorb_git_dir_into_superproject()', which moves the '.git' folder into the superproject and replaces it with a '.git' file. The rest of the deinit function can operate as it already does with new-style submodules.

We also edit a test case such that it matches the new behaviour of deinit.

Suggested-by: Atharva Raykar <raykar.ath@gmail.com>
Signed-off-by: Mugdha Pattnaik <mugdhapattnaik@gmail.com>
---
    submodule: absorb git dir instead of dying on deinit
    
    We also edit a test case such that it matches the new behaviour of
    deinit.
    
    I have changed the 'cp -R ../.git/modules/example .git' to 'mv
    ../.git/modules/example .git' since, at the time of testing, the test
    would fail - deinit now would be moving the '.git' folder into the
    superproject's '.git/modules/' folder, and since this same folder
    already existed before, it was causing errors. So, before running
    deinit, instead of copying the '.git' folder into the submodule, if we
    move it there instead, this functionality can be appropriately tested.
    
    Changes since v1:
    
     * Removed extra indent within the if statements
     * Moved absorb_git_dir_into_superproject() call outside the if
       condition checking for --quiet flag
    
    Thank you, Mugdha
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1078%2Fmugdhapattnaik%2Fsubmodule-deinit-absorbgitdirs-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1078/mugdhapattnaik/submodule-deinit-absorbgitdirs-v2
Pull-Request: https://github.com/git/git/pull/1078
Range-diff vs v1:
 1:  58814531d17 ! 1:  37c9b598fde submodule: absorb git dir instead of dying on deinit
     @@ builtin/submodule--helper.c: static void deinit_submodule(const char *path, cons
      -			    displaypath);
      +		if (is_directory(sub_git_dir)) {
      +			if (!(flags & OPT_FORCE))
     -+					die(_("Submodule work tree '%s' contains a "
     -+						  ".git directory (use --force if you want "
     -+						  "to move its contents to superproject's "
     -+						  "module folder and convert .git to a file "
     -+						  "and then proceed with deinit)"),
     ++				die(_("Submodule work tree '%s' contains a "
     ++					  ".git directory.\nUse --force if you want "
     ++					  "to move its contents to superproject's "
     ++					  "module folder and convert .git to a file "
     ++					  "and then proceed with deinit."),
     ++					displaypath);
     ++
     ++			if (!(flags & OPT_QUIET))
     ++				warning(_("Submodule work tree '%s' contains a .git "
     ++						  "directory. This will be replaced with a "
     ++						  ".git file by using absorbgitdirs."),
      +						displaypath);
      +
     -+			if (!(flags & OPT_QUIET)) {
     -+					warning(_("Submodule work tree '%s' contains a .git "
     -+							  "directory (this will be replaced with a "
     -+							  ".git file by using absorbgitdirs"),
     -+							displaypath);
     -+					absorb_git_dir_into_superproject(displaypath, flags);
     -+			}
     ++			absorb_git_dir_into_superproject(displaypath, flags);
     ++
      +		}
       
       		if (!(flags & OPT_FORCE)) {
 builtin/submodule--helper.c | 28 ++++++++++++++++++----------
 t/t7400-submodule-basic.sh  | 10 +++++-----
 2 files changed, 23 insertions(+), 15 deletions(-)
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index ef2776a9e45..4730dc141d4 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -1539,16 +1539,24 @@ static void deinit_submodule(const char *path, const char *prefix,
 		struct strbuf sb_rm = STRBUF_INIT;
 		const char *format;
 
-		/*
-		 * protect submodules containing a .git directory
-		 * NEEDSWORK: instead of dying, automatically call
-		 * absorbgitdirs and (possibly) warn.
-		 */
-		if (is_directory(sub_git_dir))
-			die(_("Submodule work tree '%s' contains a .git "
-			      "directory (use 'rm -rf' if you really want "
-			      "to remove it including all of its history)"),
-			    displaypath);
+		if (is_directory(sub_git_dir)) {
+			if (!(flags & OPT_FORCE))
+				die(_("Submodule work tree '%s' contains a "
+					  ".git directory.\nUse --force if you want "
+					  "to move its contents to superproject's "
+					  "module folder and convert .git to a file "
+					  "and then proceed with deinit."),
+					displaypath);
+
+			if (!(flags & OPT_QUIET))
+				warning(_("Submodule work tree '%s' contains a .git "
+						  "directory. This will be replaced with a "
+						  ".git file by using absorbgitdirs."),
+						displaypath);
+
+			absorb_git_dir_into_superproject(displaypath, flags);
+
+		}
 
 		if (!(flags & OPT_FORCE)) {
 			struct child_process cp_rm = CHILD_PROCESS_INIT;
diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh
index cb1b8e35dbf..3df71478d06 100755
--- a/t/t7400-submodule-basic.sh
+++ b/t/t7400-submodule-basic.sh
@@ -1182,18 +1182,18 @@ test_expect_success 'submodule deinit is silent when used on an uninitialized su
 	rmdir init example2
 '
 
-test_expect_success 'submodule deinit fails when submodule has a .git directory even when forced' '
+test_expect_success 'submodule deinit fails when submodule has a .git directory unless forced' '
 	git submodule update --init &&
 	(
 		cd init &&
 		rm .git &&
-		cp -R ../.git/modules/example .git &&
+		mv ../.git/modules/example .git &&
 		GIT_WORK_TREE=. git config --unset core.worktree
 	) &&
 	test_must_fail git submodule deinit init &&
-	test_must_fail git submodule deinit -f init &&
-	test -d init/.git &&
-	test -n "$(git config --get-regexp "submodule\.example\.")"
+	git submodule deinit -f init &&
+	! test -d init/.git &&
+	test -z "$(git config --get-regexp "submodule\.example\.")"
 '
 
 test_expect_success 'submodule with UTF-8 name' '

base-commit: c4203212e360b25a1c69467b5a8437d45a373cac
-- 
gitgitgadget
Previous: Mugdha Pattnaik via GitGitGadgetNext: Atharva Raykar
Message 2 of 18 in “submodule: absorb git dir instead of dying on deinit”
  1. submodule: absorb git dir instead of dying on deinitMugdha Pattnaik via GitGitGadget, Aug 26, 2021
  2. submodule: absorb git dir instead of dying on deinitMugdha Pattnaik via GitGitGadget, Aug 27, 2021
  3. Atharva RaykarAug 27, 2021
  4. Junio C HamanoAug 27, 2021
  5. submodule: absorb git dir instead of dying on deinitMugdha Pattnaik via GitGitGadget, Aug 27, 2021
  6. submodule: absorb git dir instead of dying on deinitMugdha Pattnaik via GitGitGadget, Aug 27, 2021
  7. Christian CouderSep 28, 2021
  8. Ævar Arnfjörð BjarmasonSep 28, 2021
  9. Mugdha PattnaikOct 6, 2021
  10. submodule: absorb git dir instead of dying on deinitMugdha Pattnaik via GitGitGadget, Oct 6, 2021
  11. submodule: absorb git dir instead of dying on deinitMugdha Pattnaik via GitGitGadget, Oct 6, 2021
  12. Junio C HamanoOct 7, 2021
  13. Mugdha PattnaikNov 16, 2021
  14. Junio C HamanoNov 16, 2021
  15. Mugdha PattnaikNov 17, 2021
  16. submodule: absorb git dir instead of dying on deinitMugdha Pattnaik via GitGitGadget, Nov 19, 2021
  17. Bagas SanjayaAug 27, 2021
  18. Mugdha PattnaikAug 27, 2021

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.