git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] submodule: absorb git dir instead of dying on deinit
@ 2021-08-26 18:33 Mugdha Pattnaik via GitGitGadget
  2021-08-27  6:03 ` [PATCH v2] " Mugdha Pattnaik via GitGitGadget
  2021-08-27  7:51 ` [PATCH] " Bagas Sanjaya
  0 siblings, 2 replies; 18+ messages in thread
From: Mugdha Pattnaik via GitGitGadget @ 2021-08-26 18:33 UTC (permalink / raw)
  To: git; +Cc: Mugdha Pattnaik, mugdha

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.
    
    Thank you, Mugdha

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1078%2Fmugdhapattnaik%2Fsubmodule-deinit-absorbgitdirs-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1078/mugdhapattnaik/submodule-deinit-absorbgitdirs-v1
Pull-Request: https://github.com/git/git/pull/1078

 builtin/submodule--helper.c | 27 +++++++++++++++++----------
 t/t7400-submodule-basic.sh  | 10 +++++-----
 2 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index ef2776a9e45..253ddce1c41 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -1539,16 +1539,23 @@ 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 (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)"),
+						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

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v2] submodule: absorb git dir instead of dying on deinit
  2021-08-26 18:33 [PATCH] submodule: absorb git dir instead of dying on deinit Mugdha Pattnaik via GitGitGadget
@ 2021-08-27  6:03 ` Mugdha Pattnaik via GitGitGadget
  2021-08-27 13:20   ` Atharva Raykar
  2021-08-27 18:10   ` [PATCH v3] " Mugdha Pattnaik via GitGitGadget
  2021-08-27  7:51 ` [PATCH] " Bagas Sanjaya
  1 sibling, 2 replies; 18+ messages in thread
From: Mugdha Pattnaik via GitGitGadget @ 2021-08-27  6:03 UTC (permalink / raw)
  To: git; +Cc: Mugdha Pattnaik, mugdha

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

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH] submodule: absorb git dir instead of dying on deinit
  2021-08-26 18:33 [PATCH] submodule: absorb git dir instead of dying on deinit Mugdha Pattnaik via GitGitGadget
  2021-08-27  6:03 ` [PATCH v2] " Mugdha Pattnaik via GitGitGadget
@ 2021-08-27  7:51 ` Bagas Sanjaya
  2021-08-27 13:13   ` Mugdha Pattnaik
  1 sibling, 1 reply; 18+ messages in thread
From: Bagas Sanjaya @ 2021-08-27  7:51 UTC (permalink / raw)
  To: Mugdha Pattnaik via GitGitGadget, git; +Cc: Mugdha Pattnaik

On 27/08/21 01.33, Mugdha Pattnaik via GitGitGadget wrote:
> 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'.

Maybe the case of "repo inside the repo", with submodule as repo?

-- 
An old man doll... just what I always wanted! - Clara

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] submodule: absorb git dir instead of dying on deinit
  2021-08-27  7:51 ` [PATCH] " Bagas Sanjaya
@ 2021-08-27 13:13   ` Mugdha Pattnaik
  0 siblings, 0 replies; 18+ messages in thread
From: Mugdha Pattnaik @ 2021-08-27 13:13 UTC (permalink / raw)
  To: Bagas Sanjaya; +Cc: Mugdha Pattnaik via GitGitGadget, git

On Fri, Aug 27, 2021 at 1:21 PM Bagas Sanjaya <bagasdotme@gmail.com> wrote:
> Maybe the case of "repo inside the repo", with submodule as repo?

I am not certain what exactly you meant, but this patch deals with "Old-form"
submodules. This link (https://git-scm.com/docs/gitsubmodules#_forms)
states that old-style submodules, when deinitialized or deleted, have its Git
directory automatically moved to $GIT_DIR/modules/<name>/ of the
superproject. This is what this patch sets to achieve. Earlier, running deinit
on such a submodule would cause it to fail.

--
Mugdha

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] submodule: absorb git dir instead of dying on deinit
  2021-08-27  6:03 ` [PATCH v2] " Mugdha Pattnaik via GitGitGadget
@ 2021-08-27 13:20   ` Atharva Raykar
  2021-08-27 17:28     ` Junio C Hamano
  2021-08-27 18:10   ` [PATCH v3] " Mugdha Pattnaik via GitGitGadget
  1 sibling, 1 reply; 18+ messages in thread
From: Atharva Raykar @ 2021-08-27 13:20 UTC (permalink / raw)
  To: Mugdha Pattnaik via GitGitGadget; +Cc: git, mugdha


"Mugdha Pattnaik via GitGitGadget" <gitgitgadget@gmail.com> writes:

> 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 makes sense, a lot of commands seem to follow this pattern of
requiring a user to '--force' when the behaviour might not be different
from what is normally expected.

> 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.

Nice. Just like what the NEEDSWORK comment suggested.

> 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);
> +
> +		}

Looks okay to me. The quiet flag suppresses the warning, which is nice.

>  		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 &&

The original test case was written with the expectation that the deinit
won't happen. But now that it will, I suppose an mv was necessary so
that absorbgitdirs can write the gitdir back to the original place
successfully.

>  		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\.")"
>  '

Likewise here. The test case has been flipped to reflect the new
behaviour.

>  test_expect_success 'submodule with UTF-8 name' '
>
> base-commit: c4203212e360b25a1c69467b5a8437d45a373cac

BTW, I see you linked downthread the documentation[1] which says:

  When [old-form submodules] deinitialized or deleted (see below), the
  submodule’s Git directory is automatically moved to
  $GIT_DIR/modules/<name>/ of the superproject.

This was not what Git was doing before this patch, it used to die() out
instead. So I think this actually is a bug fix (although I am not sure
why the test suite also did not agree with the documentation).

Either way, I think this is a welcome change. Thanks for the patch!

[1] https://git-scm.com/docs/gitsubmodules#_forms

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] submodule: absorb git dir instead of dying on deinit
  2021-08-27 13:20   ` Atharva Raykar
@ 2021-08-27 17:28     ` Junio C Hamano
  0 siblings, 0 replies; 18+ messages in thread
From: Junio C Hamano @ 2021-08-27 17:28 UTC (permalink / raw)
  To: Atharva Raykar; +Cc: Mugdha Pattnaik via GitGitGadget, git, mugdha

Atharva Raykar <raykar.ath@gmail.com> writes:

> "Mugdha Pattnaik via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
>> 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 makes sense, a lot of commands seem to follow this pattern of
> requiring a user to '--force' when the behaviour might not be different
> from what is normally expected.
>
>> 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.
>
> Nice. Just like what the NEEDSWORK comment suggested.

Indeed.  Many use of the word "folder", especially when the thing is
called "git dir" in the title, irritates me, though.  We use files
and directories, not folders.

> BTW, I see you linked downthread the documentation[1] which says:
>
>   When [old-form submodules] deinitialized or deleted (see below), the
>   submodule’s Git directory is automatically moved to
>   $GIT_DIR/modules/<name>/ of the superproject.
>
> This was not what Git was doing before this patch, it used to die() out
> instead. So I think this actually is a bug fix (although I am not sure
> why the test suite also did not agree with the documentation).

Curious.

I see the description was added by d4803455 (submodules: overhaul
documentation, 2017-06-22), which claimed to "detangle" by "move"
various existing pieces of documentation into a new file.

I guess we added a wish without marking it as such ;-)

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH v3] submodule: absorb git dir instead of dying on deinit
  2021-08-27  6:03 ` [PATCH v2] " Mugdha Pattnaik via GitGitGadget
  2021-08-27 13:20   ` Atharva Raykar
@ 2021-08-27 18:10   ` Mugdha Pattnaik via GitGitGadget
  2021-08-27 18:51     ` [PATCH v4] " Mugdha Pattnaik via GitGitGadget
  1 sibling, 1 reply; 18+ messages in thread
From: Mugdha Pattnaik via GitGitGadget @ 2021-08-27 18:10 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Mugdha Pattnaik, mugdha

From: mugdha <mugdhapattnaik@gmail.com>

Currently, running 'git submodule deinit' on repos where the
submodule's '.git' is a directory, 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 dir 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
    
    Changes since v2:
    
     * Replaced all instances of the word "folder" with either "directory"
       or "git dir"
    
    Thank you, Mugdha
    
    cc: Bagas Sanjaya bagasdotme@gmail.com
    
    cc: Atharva Raykar raykar.ath@gmail.com

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1078%2Fmugdhapattnaik%2Fsubmodule-deinit-absorbgitdirs-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1078/mugdhapattnaik/submodule-deinit-absorbgitdirs-v3
Pull-Request: https://github.com/git/git/pull/1078

Range-diff vs v2:

 1:  37c9b598fde ! 1:  1ac65b2458b submodule: absorb git dir instead of dying on deinit
     @@ Commit message
          submodule: absorb git dir instead of dying on deinit
      
          Currently, running 'git submodule deinit' on repos where the
     -    submodule's '.git' is a folder aborts with a message that is not
     +    submodule's '.git' is a directory, 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
     +    moves the git dir 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.
      


 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

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v4] submodule: absorb git dir instead of dying on deinit
  2021-08-27 18:10   ` [PATCH v3] " Mugdha Pattnaik via GitGitGadget
@ 2021-08-27 18:51     ` Mugdha Pattnaik via GitGitGadget
  2021-09-28  7:30       ` Christian Couder
                         ` (2 more replies)
  0 siblings, 3 replies; 18+ messages in thread
From: Mugdha Pattnaik via GitGitGadget @ 2021-08-27 18:51 UTC (permalink / raw)
  To: git; +Cc: Bagas Sanjaya, Atharva Raykar, Junio C Hamano, Mugdha Pattnaik,
	mugdha

From: mugdha <mugdhapattnaik@gmail.com>

Currently, running 'git submodule deinit' on repos where the
submodule's '.git' is a directory, 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 dir 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
    
    Changes since v3:
    
     * Replaced 1 instance of the word "folder" with "directory"
     * Fixed tab spacing
    
    Thank you, Mugdha

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1078%2Fmugdhapattnaik%2Fsubmodule-deinit-absorbgitdirs-v4
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1078/mugdhapattnaik/submodule-deinit-absorbgitdirs-v4
Pull-Request: https://github.com/git/git/pull/1078

Range-diff vs v3:

 1:  1ac65b2458b ! 1:  7460fc0e12a submodule: absorb git dir instead of dying on deinit
     @@ builtin/submodule--helper.c: static void deinit_submodule(const char *path, cons
      +		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);
     ++				      ".git directory.\nUse --force if you want "
     ++				      "to move its contents to superproject's "
     ++				      "module directory 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);
     ++					  "directory. This will be replaced with a "
     ++					  ".git file by using absorbgitdirs."),
     ++					displaypath);
      +
      +			absorb_git_dir_into_superproject(displaypath, flags);
      +


 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..040b26f149d 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 directory 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

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH v4] submodule: absorb git dir instead of dying on deinit
  2021-08-27 18:51     ` [PATCH v4] " Mugdha Pattnaik via GitGitGadget
@ 2021-09-28  7:30       ` Christian Couder
  2021-09-28  9:53       ` Ævar Arnfjörð Bjarmason
  2021-10-06 12:02       ` [PATCH v5] " Mugdha Pattnaik via GitGitGadget
  2 siblings, 0 replies; 18+ messages in thread
From: Christian Couder @ 2021-09-28  7:30 UTC (permalink / raw)
  To: Mugdha Pattnaik via GitGitGadget
  Cc: git, Bagas Sanjaya, Atharva Raykar, Junio C Hamano,
	Mugdha Pattnaik

On Fri, Aug 27, 2021 at 8:53 PM Mugdha Pattnaik via GitGitGadget
<gitgitgadget@gmail.com> wrote:
>
> From: mugdha <mugdhapattnaik@gmail.com>
>
> Currently, running 'git submodule deinit' on repos where the
> submodule's '.git' is a directory, 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'.

It seems to me that this patch fell into the cracks. Or did I miss something?

> This internally calls 'absorb_git_dir_into_superproject()', which
> moves the git dir 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>

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v4] submodule: absorb git dir instead of dying on deinit
  2021-08-27 18:51     ` [PATCH v4] " Mugdha Pattnaik via GitGitGadget
  2021-09-28  7:30       ` Christian Couder
@ 2021-09-28  9:53       ` Ævar Arnfjörð Bjarmason
  2021-10-06 11:45         ` Mugdha Pattnaik
  2021-10-06 12:02       ` [PATCH v5] " Mugdha Pattnaik via GitGitGadget
  2 siblings, 1 reply; 18+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2021-09-28  9:53 UTC (permalink / raw)
  To: Mugdha Pattnaik via GitGitGadget
  Cc: git, Bagas Sanjaya, Atharva Raykar, Junio C Hamano, mugdha


On Fri, Aug 27 2021, Mugdha Pattnaik via GitGitGadget wrote:

> From: mugdha <mugdhapattnaik@gmail.com>
>
> Currently, running 'git submodule deinit' on repos where the
> submodule's '.git' is a directory, 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 dir 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
>     
>     Changes since v3:
>     
>      * Replaced 1 instance of the word "folder" with "directory"
>      * Fixed tab spacing
>     
>     Thank you, Mugdha
>
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1078%2Fmugdhapattnaik%2Fsubmodule-deinit-absorbgitdirs-v4
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1078/mugdhapattnaik/submodule-deinit-absorbgitdirs-v4
> Pull-Request: https://github.com/git/git/pull/1078
>
> Range-diff vs v3:
>
>  1:  1ac65b2458b ! 1:  7460fc0e12a submodule: absorb git dir instead of dying on deinit
>      @@ builtin/submodule--helper.c: static void deinit_submodule(const char *path, cons
>       +		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);
>      ++				      ".git directory.\nUse --force if you want "
>      ++				      "to move its contents to superproject's "
>      ++				      "module directory 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);
>      ++					  "directory. This will be replaced with a "
>      ++					  ".git file by using absorbgitdirs."),
>      ++					displaypath);
>       +
>       +			absorb_git_dir_into_superproject(displaypath, flags);
>       +
>
>
>  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..040b26f149d 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 directory 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\.")"

I see we don't have a "test_dir_is_missing" for the pre-image, but
shouldn't the post-image "! test -d" just be a test_path_is_missing?
I.e. should this pass if .git is a file? Probably not...

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v4] submodule: absorb git dir instead of dying on deinit
  2021-09-28  9:53       ` Ævar Arnfjörð Bjarmason
@ 2021-10-06 11:45         ` Mugdha Pattnaik
  0 siblings, 0 replies; 18+ messages in thread
From: Mugdha Pattnaik @ 2021-10-06 11:45 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason
  Cc: Mugdha Pattnaik via GitGitGadget, git, Bagas Sanjaya,
	Atharva Raykar, Junio C Hamano

On Tue, Sep 28, 2021 Ævar Arnfjörð Bjarmason <avarab@gmail.com> wrote:
> shouldn't the post-image "! test -d" just be a test_path_is_missing?
> I.e. should this pass if .git is a file? Probably not...

I agree, this test should not pass if .git is a file. I'll change it to
"test_path_is_missing" and send an update to the patch.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH v5] submodule: absorb git dir instead of dying on deinit
  2021-08-27 18:51     ` [PATCH v4] " Mugdha Pattnaik via GitGitGadget
  2021-09-28  7:30       ` Christian Couder
  2021-09-28  9:53       ` Ævar Arnfjörð Bjarmason
@ 2021-10-06 12:02       ` Mugdha Pattnaik via GitGitGadget
  2021-10-06 12:24         ` [PATCH v6] " Mugdha Pattnaik via GitGitGadget
  2 siblings, 1 reply; 18+ messages in thread
From: Mugdha Pattnaik via GitGitGadget @ 2021-10-06 12:02 UTC (permalink / raw)
  To: git; +Cc: Mugdha Pattnaik, mugdha

From: mugdha <mugdhapattnaik@gmail.com>

Currently, running 'git submodule deinit' on repos where the
submodule's '.git' is a directory, 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 dir 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
    
    Changes since v4:
    
     * Changed test case from "! test -d" to "test_path_is_missing"
    
    Thanks
    Mugdha

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1078%2Fmugdhapattnaik%2Fsubmodule-deinit-absorbgitdirs-v5
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1078/mugdhapattnaik/submodule-deinit-absorbgitdirs-v5
Pull-Request: https://github.com/git/git/pull/1078

Range-diff vs v4:

 1:  7460fc0e12a = 1:  c39cd681e71 submodule: absorb git dir instead of dying on deinit


 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..040b26f149d 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 directory 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

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v6] submodule: absorb git dir instead of dying on deinit
  2021-10-06 12:02       ` [PATCH v5] " Mugdha Pattnaik via GitGitGadget
@ 2021-10-06 12:24         ` Mugdha Pattnaik via GitGitGadget
  2021-10-07 19:41           ` Junio C Hamano
  2021-11-19 10:56           ` [PATCH v7] " Mugdha Pattnaik via GitGitGadget
  0 siblings, 2 replies; 18+ messages in thread
From: Mugdha Pattnaik via GitGitGadget @ 2021-10-06 12:24 UTC (permalink / raw)
  To: git
  Cc: Bagas Sanjaya, Atharva Raykar, Junio C Hamano, Christian Couder,
	Ævar Arnfjörð Bjarmason, Mugdha Pattnaik, mugdha

From: mugdha <mugdhapattnaik@gmail.com>

Currently, running 'git submodule deinit' on repos where the
submodule's '.git' is a directory, 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 dir 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
    
    Changes since v5:
    
     * Fixed accidental submission of old version
    
    Changes since v4:
    
     * Changed test case from "! test -d" to "test_path_is_missing"
    
    Thank you,
    Mugdha

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1078%2Fmugdhapattnaik%2Fsubmodule-deinit-absorbgitdirs-v6
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1078/mugdhapattnaik/submodule-deinit-absorbgitdirs-v6
Pull-Request: https://github.com/git/git/pull/1078

Range-diff vs v5:

 1:  c39cd681e71 ! 1:  384a6742388 submodule: absorb git dir instead of dying on deinit
     @@ t/t7400-submodule-basic.sh: test_expect_success 'submodule deinit is silent when
      -	test -d init/.git &&
      -	test -n "$(git config --get-regexp "submodule\.example\.")"
      +	git submodule deinit -f init &&
     -+	! test -d init/.git &&
     ++	test_path_is_missing init/.git &&
      +	test -z "$(git config --get-regexp "submodule\.example\.")"
       '
       


 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..040b26f149d 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 directory 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..f35479ea634 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_path_is_missing init/.git &&
+	test -z "$(git config --get-regexp "submodule\.example\.")"
 '
 
 test_expect_success 'submodule with UTF-8 name' '

base-commit: c4203212e360b25a1c69467b5a8437d45a373cac
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH v6] submodule: absorb git dir instead of dying on deinit
  2021-10-06 12:24         ` [PATCH v6] " Mugdha Pattnaik via GitGitGadget
@ 2021-10-07 19:41           ` Junio C Hamano
  2021-11-16 18:20             ` Mugdha Pattnaik
  2021-11-19 10:56           ` [PATCH v7] " Mugdha Pattnaik via GitGitGadget
  1 sibling, 1 reply; 18+ messages in thread
From: Junio C Hamano @ 2021-10-07 19:41 UTC (permalink / raw)
  To: Mugdha Pattnaik via GitGitGadget
  Cc: git, Bagas Sanjaya, Atharva Raykar, Christian Couder,
	Ævar Arnfjörð Bjarmason, Mugdha Pattnaik

"Mugdha Pattnaik via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: mugdha <mugdhapattnaik@gmail.com>

This line is called "in-body header" that is used to override the
author name that is automatically obtained from the e-mail's "From:"
header (which is set to "Mugdha Pattnaik via GitGitGadget" by GGG,
which is obviously not your name, hence we use an in-body header to
override it).  It should match what you use to sign off your patches,
the one we see below.

I'll hand-edit so that "git show" will say that the author is
"Mugdha Pattnaik", not "mugdha", while applying, but please make
sure your further submissions will not have this problem.

> Currently, running 'git submodule deinit' on repos where the
> submodule's '.git' is a directory, 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'.

OK.  That sounds like an improvement, albeit possibly an overly
cautious one, as a casual "deinit" user will get an error as before
without "--force", which may or may not be a good thing.  Requiring
"--force" means it is safer by default by not changing the on-disk
data.  But requiring "--force" also means we end up training users
to say "--force" when it shouldn't have to be.

A plausible alternative is to always absorb but with a warning "we
absorbed it for you", without requiring "--force".  If we didn't
have "git submodule deinit" command now and were designing it from
scratch, would we design it to fail because the submodule's git
directory is not absorbed?  I doubt it, as I do not think of a good
reason to do so offhand.

Does "git submodule" currently reject a "deinit" request due to some
_other_ conditions or safety concerns and require the "--force"
option to continue?  Requiring the "--force" option to resolve ".git
is a directory, and the user wants to make it absorbed" means that
the user will be _forced_ to bypass these _other_ safety valves only
to save the submodule repository from destruction when running
"deinit", which may not be a good trade-off between the safety
requirements of these _other_ conditions, if exists, and the one we
are dealing with.

> This internally calls 'absorb_git_dir_into_superproject()', which
> moves the git dir 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.

This is not wrong per-se, but such an implementation detail is
something best left for the patch.  

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

"match the new behaviour" in what way?

    In one test, we used to require "git submodule deinit" to fail
    even with the "--force" option when the submodule's .git/
    directory is not absorbed.  Adjust it to expect the operation to
    pass.

would be a description at the right level of detail, I think.

> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> index ef2776a9e45..040b26f149d 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 directory 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);

Shouldn't the first argument to this call be "path" not
"displaypath"?  The paths in messages may want to have the path from
the top to the submodule location prefixed for human consumption,
but the called function only cares about the path to the submodule
from the current directory, no?

The second parameter of this call seems totally bogus to me.  What
is the vocabulary of bits the called function takes?  Is that from
the same set the flags this function takes?  Does the called
function even understand OPT_QUIET, or does the bitpattern that
happens to correspond to OPT_QUIET have a totally different meaning
to the called function and we will trigger a behaviour that this
caller does not expect at all?

Thanks.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v6] submodule: absorb git dir instead of dying on deinit
  2021-10-07 19:41           ` Junio C Hamano
@ 2021-11-16 18:20             ` Mugdha Pattnaik
  2021-11-16 18:54               ` Junio C Hamano
  0 siblings, 1 reply; 18+ messages in thread
From: Mugdha Pattnaik @ 2021-11-16 18:20 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Mugdha Pattnaik via GitGitGadget, git, Bagas Sanjaya,
	Atharva Raykar, Christian Couder,
	Ævar Arnfjörð Bjarmason

Apologies for the late reply, I got caught up with other commitments.

> On Fri, Oct 8, 2021 at 1:11 AM Junio C Hamano <gitster@pobox.com> wrote:
>
> "Mugdha Pattnaik via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
> > From: mugdha <mugdhapattnaik@gmail.com>
>
> This line is called "in-body header" that is used to override the
> author name that is automatically obtained from the e-mail's "From:"
> header (which is set to "Mugdha Pattnaik via GitGitGadget" by GGG,
> which is obviously not your name, hence we use an in-body header to
> override it).  It should match what you use to sign off your patches,
> the one we see below.
>
> I'll hand-edit so that "git show" will say that the author is
> "Mugdha Pattnaik", not "mugdha", while applying, but please make
> sure your further submissions will not have this problem.

Okay, I will keep this in mind for future patches.


> > Currently, running 'git submodule deinit' on repos where the
> > submodule's '.git' is a directory, 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'.
>
> OK.  That sounds like an improvement, albeit possibly an overly
> cautious one, as a casual "deinit" user will get an error as before
> without "--force", which may or may not be a good thing.  Requiring
> "--force" means it is safer by default by not changing the on-disk
> data.  But requiring "--force" also means we end up training users
> to say "--force" when it shouldn't have to be.
>
> A plausible alternative is to always absorb but with a warning "we
> absorbed it for you", without requiring "--force".  If we didn't
> have "git submodule deinit" command now and were designing it from
> scratch, would we design it to fail because the submodule's git
> directory is not absorbed?  I doubt it, as I do not think of a good
> reason to do so offhand.

Okay, I understand now. I'll remove the condition that checks for "--force"
and will go ahead with absorbing the gitdir after displaying the suggested
warning message. Currently I suppress the warnings when the "--quiet"
flag is set; I think I will continue to do that, even after implementing the
above change. Do let me know if I should be doing otherwise.


> Does "git submodule" currently reject a "deinit" request due to some
> _other_ conditions or safety concerns and require the "--force"
> option to continue?  Requiring the "--force" option to resolve ".git
> is a directory, and the user wants to make it absorbed" means that
> the user will be _forced_ to bypass these _other_ safety valves only
> to save the submodule repository from destruction when running
> "deinit", which may not be a good trade-off between the safety
> requirements of these _other_ conditions, if exists, and the one we
> are dealing with.

This is definitely a situation we want to avoid. How about we try to run
a check for uncommitted local modifications first? If these are present,
then deinit can die with a warning. In case there are no uncommitted
local modifications, deinit can go ahead and absorb the gitdir and do the
rest.

The existing submodule--helper.c file already has a check for this, but it
seems to be doing it below the check for absorption. I can try to switch it
around to see how that works.


> > This internally calls 'absorb_git_dir_into_superproject()', which
> > moves the git dir 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.
>
> This is not wrong per-se, but such an implementation detail is
> something best left for the patch.
>
> > We also edit a test case such that it matches the new behaviour of
> > deinit.
>
> "match the new behaviour" in what way?
>
>     In one test, we used to require "git submodule deinit" to fail
>     even with the "--force" option when the submodule's .git/
>     directory is not absorbed.  Adjust it to expect the operation to
>     pass.
>
> would be a description at the right level of detail, I think.

Noted. I'll apply the above changes.


> > diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> > index ef2776a9e45..040b26f149d 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 directory 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);
>
> Shouldn't the first argument to this call be "path" not
> "displaypath"?  The paths in messages may want to have the path from
> the top to the submodule location prefixed for human consumption,
> but the called function only cares about the path to the submodule
> from the current directory, no?

Yes that makes sense, path is the better argument to pass.


> The second parameter of this call seems totally bogus to me.  What
> is the vocabulary of bits the called function takes?  Is that from
> the same set the flags this function takes?  Does the called
> function even understand OPT_QUIET, or does the bitpattern that
> happens to correspond to OPT_QUIET have a totally different meaning
> to the called function and we will trigger a behaviour that this
> caller does not expect at all?

I realised I was passing the wrong flags. On investigating further, the
flags in submodule.c do have different semantics. I also noticed that it
checks for recursive submodule absorption. I believe I should just be
passing the recursive flag to the function that absorbs gitdir. This way,
nested old-style gitdirs will also be handled.

I intend to incorporate these changes by the end of the week.

Thanks

--
Mugdha

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v6] submodule: absorb git dir instead of dying on deinit
  2021-11-16 18:20             ` Mugdha Pattnaik
@ 2021-11-16 18:54               ` Junio C Hamano
  2021-11-17  5:55                 ` Mugdha Pattnaik
  0 siblings, 1 reply; 18+ messages in thread
From: Junio C Hamano @ 2021-11-16 18:54 UTC (permalink / raw)
  To: Mugdha Pattnaik
  Cc: Mugdha Pattnaik via GitGitGadget, git, Bagas Sanjaya,
	Atharva Raykar, Christian Couder,
	Ævar Arnfjörð Bjarmason

Mugdha Pattnaik <mugdhapattnaik@gmail.com> writes:

>> OK.  That sounds like an improvement, albeit possibly an overly
>> cautious one, as a casual "deinit" user will get an error as before
>> without "--force", which may or may not be a good thing.  Requiring
>> "--force" means it is safer by default by not changing the on-disk
>> data.  But requiring "--force" also means we end up training users
>> to say "--force" when it shouldn't have to be.
>> ...
>> Does "git submodule" currently reject a "deinit" request due to some
>> _other_ conditions or safety concerns and require the "--force"
>> option to continue?  Requiring the "--force" option to resolve ".git
>> is a directory, and the user wants to make it absorbed" means that
>> the user will be _forced_ to bypass these _other_ safety valves only
>> to save the submodule repository from destruction when running
>> "deinit", which may not be a good trade-off between the safety
>> requirements of these _other_ conditions, if exists, and the one we
>> are dealing with.
>
> This is definitely a situation we want to avoid. How about we try to run
> a check for uncommitted local modifications first?

I am not sure if I follow.  If we stop (ab)using "--force" for the
situation (i.e. where today's "deinit" would die because .git needs
to be absorbed first), then the user would not have to say "--force"
which may override other safety valve.  You'd check if .git needs to
be absorbed, make it absorbed as needed while reporting the fact
that you did so, and then let the existing "deinit" code to take
over.  If there are other safety checks that needs "--force" to be
overridden, that is handled (presumably) correctly by the existing
code, no?  So other than "do we need absorbing, and if so do it for
the user" check, I do not think you'd need to add any new "we try to
run a check for ..." at all.



^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v6] submodule: absorb git dir instead of dying on deinit
  2021-11-16 18:54               ` Junio C Hamano
@ 2021-11-17  5:55                 ` Mugdha Pattnaik
  0 siblings, 0 replies; 18+ messages in thread
From: Mugdha Pattnaik @ 2021-11-17  5:55 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Mugdha Pattnaik via GitGitGadget, git, Bagas Sanjaya,
	Atharva Raykar, Christian Couder,
	Ævar Arnfjörð Bjarmason

On Wed, Nov 17, 2021 at 12:24 AM Junio C Hamano <gitster@pobox.com> wrote:
>
> Mugdha Pattnaik <mugdhapattnaik@gmail.com> writes:
>
> >> OK.  That sounds like an improvement, albeit possibly an overly
> >> cautious one, as a casual "deinit" user will get an error as before
> >> without "--force", which may or may not be a good thing.  Requiring
> >> "--force" means it is safer by default by not changing the on-disk
> >> data.  But requiring "--force" also means we end up training users
> >> to say "--force" when it shouldn't have to be.
> >> ...
> >> Does "git submodule" currently reject a "deinit" request due to some
> >> _other_ conditions or safety concerns and require the "--force"
> >> option to continue?  Requiring the "--force" option to resolve ".git
> >> is a directory, and the user wants to make it absorbed" means that
> >> the user will be _forced_ to bypass these _other_ safety valves only
> >> to save the submodule repository from destruction when running
> >> "deinit", which may not be a good trade-off between the safety
> >> requirements of these _other_ conditions, if exists, and the one we
> >> are dealing with.
> >
> > This is definitely a situation we want to avoid. How about we try to run
> > a check for uncommitted local modifications first?
>
> I am not sure if I follow.  If we stop (ab)using "--force" for the
> situation (i.e. where today's "deinit" would die because .git needs
> to be absorbed first), then the user would not have to say "--force"
> which may override other safety valve.  You'd check if .git needs to
> be absorbed, make it absorbed as needed while reporting the fact
> that you did so, and then let the existing "deinit" code to take
> over.  If there are other safety checks that needs "--force" to be
> overridden, that is handled (presumably) correctly by the existing
> code, no?  So other than "do we need absorbing, and if so do it for
> the user" check, I do not think you'd need to add any new "we try to
> run a check for ..." at all.

Yes, I understand why "--force" should not be used. The reason why I
suggested the check for local modifications is because I thought we
should warn users that they have local modifications before we absorb
the gitdir. But I see now that this is okay, considering deinit would
die in such a situation anyway, and users would not lose their work.
The only side-effect of running deinit despite users having local
modifications, would be that the gitdir of the submodule has been
absorbed and that should be okay.

-- 
Mugdha

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH v7] submodule: absorb git dir instead of dying on deinit
  2021-10-06 12:24         ` [PATCH v6] " Mugdha Pattnaik via GitGitGadget
  2021-10-07 19:41           ` Junio C Hamano
@ 2021-11-19 10:56           ` Mugdha Pattnaik via GitGitGadget
  1 sibling, 0 replies; 18+ messages in thread
From: Mugdha Pattnaik via GitGitGadget @ 2021-11-19 10:56 UTC (permalink / raw)
  To: git
  Cc: Bagas Sanjaya, Atharva Raykar, Junio C Hamano, Christian Couder,
	Ævar Arnfjörð Bjarmason, Mugdha Pattnaik,
	Mugdha Pattnaik

From: Mugdha Pattnaik <mugdhapattnaik@gmail.com>

Currently, running 'git submodule deinit' on repos where the
submodule's '.git' is a directory, aborts with a message that is not
exactly user friendly.

Let's change this to instead warn the user that the .git/ directory
has been absorbed into the superproject.
The rest of the deinit function can operate as it already does with
new-style submodules.

In one test, we used to require "git submodule deinit" to fail even
with the "--force" option when the submodule's .git/ directory is not
absorbed. Adjust it to expect the operation to pass.

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
    
    Changes since v6:
    
     * Edited commit message based on suggestions given.
     * Passed correct arguments to absorb gitdir function; path and recurse
       flag
     * Modified behaviour of deinit such that it absorbs the gitdir even
       when not forced.
    
    Changes since v5:
    
     * Fixed accidental submission of old version
    
    Changes since v4:
    
     * Changed test case from "! test -d" to "test_path_is_missing"
    
    Changes since v3:
    
     * Replaced 1 instance of the word "folder" with "directory"
     * Fixed tab spacing
    
    Changes since v2:
    
     * Replaced all instances of the word "folder" with either "directory"
       or "git dir"
    
    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
    
    ------------------------------------------------------------------------
    
    Currently, running 'git submodule deinit' on repos where the submodule's
    '.git' is a directory, aborts with a message that is not exactly user
    friendly.
    
    Let's change this to instead warn the user that the .git/ directory has
    been absorbed into the superproject. The rest of the deinit function can
    operate as it already does with new-style submodules.
    
    In one test, we used to require "git submodule deinit" to fail even with
    the "--force" option when the submodule's .git/ directory is not
    absorbed. Adjust it to expect the operation to pass.
    
    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' directory into the
    superproject's '.git/modules/' directory, and since this same directory
    already existed before, it was causing errors. So, before running
    deinit, instead of copying the '.git' directory into the submodule, if
    we move it there instead, this functionality can be appropriately
    tested.
    
    Thank you, Mugdha

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1078%2Fmugdhapattnaik%2Fsubmodule-deinit-absorbgitdirs-v7
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1078/mugdhapattnaik/submodule-deinit-absorbgitdirs-v7
Pull-Request: https://github.com/git/git/pull/1078

Range-diff vs v6:

 1:  384a6742388 ! 1:  f37724016f0 submodule: absorb git dir instead of dying on deinit
     @@
       ## Metadata ##
     -Author: mugdha <mugdhapattnaik@gmail.com>
     +Author: Mugdha Pattnaik <mugdhapattnaik@gmail.com>
      
       ## Commit message ##
          submodule: absorb git dir instead of dying on deinit
      
          Currently, running 'git submodule deinit' on repos where the
          submodule's '.git' is a directory, 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'.
     +    exactly user friendly.
      
     -    This internally calls 'absorb_git_dir_into_superproject()', which
     -    moves the git dir 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.
     +    Let's change this to instead warn the user that the .git/ directory
     +    has been absorbed into the superproject.
     +    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.
     +    In one test, we used to require "git submodule deinit" to fail even
     +    with the "--force" option when the submodule's .git/ directory is not
     +    absorbed. Adjust it to expect the operation to pass.
      
          Suggested-by: Atharva Raykar <raykar.ath@gmail.com>
          Signed-off-by: Mugdha Pattnaik <mugdhapattnaik@gmail.com>
     @@ builtin/submodule--helper.c: static void deinit_submodule(const char *path, cons
      -			      "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 directory 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);
     ++			absorb_git_dir_into_superproject(path,
     ++							 ABSORB_GITDIR_RECURSE_SUBMODULES);
      +
      +		}
       
     @@ t/t7400-submodule-basic.sh: test_expect_success 'submodule deinit is silent when
       '
       
      -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' '
     ++test_expect_success 'submodule deinit absorbs .git directory if .git is a directory' '
       	git submodule update --init &&
       	(
       		cd init &&
     @@ t/t7400-submodule-basic.sh: test_expect_success 'submodule deinit is silent when
      +		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 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 &&
     ++	git submodule deinit init &&
      +	test_path_is_missing init/.git &&
      +	test -z "$(git config --get-regexp "submodule\.example\.")"
       '


 builtin/submodule--helper.c | 21 +++++++++++----------
 t/t7400-submodule-basic.sh  | 11 +++++------
 2 files changed, 16 insertions(+), 16 deletions(-)

diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index ef2776a9e45..bbab562dec6 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -1539,16 +1539,17 @@ 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_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(path,
+							 ABSORB_GITDIR_RECURSE_SUBMODULES);
+
+		}
 
 		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..e7cec2e457a 100755
--- a/t/t7400-submodule-basic.sh
+++ b/t/t7400-submodule-basic.sh
@@ -1182,18 +1182,17 @@ 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 absorbs .git directory if .git is a directory' '
 	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 init &&
+	test_path_is_missing init/.git &&
+	test -z "$(git config --get-regexp "submodule\.example\.")"
 '
 
 test_expect_success 'submodule with UTF-8 name' '

base-commit: c4203212e360b25a1c69467b5a8437d45a373cac
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2021-11-19 10:56 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-26 18:33 [PATCH] submodule: absorb git dir instead of dying on deinit Mugdha Pattnaik via GitGitGadget
2021-08-27  6:03 ` [PATCH v2] " Mugdha Pattnaik via GitGitGadget
2021-08-27 13:20   ` Atharva Raykar
2021-08-27 17:28     ` Junio C Hamano
2021-08-27 18:10   ` [PATCH v3] " Mugdha Pattnaik via GitGitGadget
2021-08-27 18:51     ` [PATCH v4] " Mugdha Pattnaik via GitGitGadget
2021-09-28  7:30       ` Christian Couder
2021-09-28  9:53       ` Ævar Arnfjörð Bjarmason
2021-10-06 11:45         ` Mugdha Pattnaik
2021-10-06 12:02       ` [PATCH v5] " Mugdha Pattnaik via GitGitGadget
2021-10-06 12:24         ` [PATCH v6] " Mugdha Pattnaik via GitGitGadget
2021-10-07 19:41           ` Junio C Hamano
2021-11-16 18:20             ` Mugdha Pattnaik
2021-11-16 18:54               ` Junio C Hamano
2021-11-17  5:55                 ` Mugdha Pattnaik
2021-11-19 10:56           ` [PATCH v7] " Mugdha Pattnaik via GitGitGadget
2021-08-27  7:51 ` [PATCH] " Bagas Sanjaya
2021-08-27 13:13   ` Mugdha Pattnaik

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).