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

Re: [PATCH v2 1/1] rm: stage submodule removal from '.gitmodules' when using '--cached'

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 22, 2021, 18:58 UTC
Message-ID
<xmqqv9ak6iac.fsf@gitster.g>
In-Reply-To
<20210222172623.69313-2-periperidip@gmail.com>
Shourya Shukla <periperidip@gmail.com> writes:
Show 9 quoted lines
> Currently, using 'git rm --cached <submodule>' removes the submodule
> <submodule> from the index and leaves the submodule working tree
> intact in the superproject working tree, but does not stage any
> changes to the '.gitmodules' file, in contrast to 'git rm <submodule>',
> which removes both the submodule and its configuration in '.gitmodules'
> from the worktree and index.
>
> Fix this inconsistency by also staging the removal of the entry of the
> submodule from the '.gitmodules' file, leaving the worktree copy intact,

The "also" above felt a bit puzzling, as we would be removing the entry only from the indexed copy without touching the working tree (by the way, I try to be precise in terminology between worktree and working tree, and please follow suit. A working tree is what you have in a non-bare repository that let's you "less" "gcc" etc. on the files checked out. A worktree refers to the mechanism that lets you have separate working tree by borrowing from a repository, or refers to an instance of a working tree plus .git file created by the mechanism. You mean "working tree" in the above sentence), but it refers to "remove the submodules directory and also entry", so it is OK.

Show 17 quoted lines
> diff --git a/builtin/rm.c b/builtin/rm.c
> index 4858631e0f..5854ef0996 100644
> --- a/builtin/rm.c
> +++ b/builtin/rm.c
> @@ -254,7 +254,7 @@ static struct option builtin_rm_options[] = {
>  int cmd_rm(int argc, const char **argv, const char *prefix)
>  {
>  	struct lock_file lock_file = LOCK_INIT;
> -	int i;
> +	int i, removed = 0;
>  	struct pathspec pathspec;
>  	char *seen;
>  
> @@ -365,30 +365,33 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
>  	if (show_only)
>  		return 0;
>  
Show 20 quoted lines
> +	for (i = 0; i < list.nr; i++) {
> +		const char *path = list.entry[i].name;
> +		if (list.entry[i].is_submodule) {
> +			/*
> +			 * Then, unless we used "--cached", remove the filenames from
> +			 * the workspace. If we fail to remove the first one, we
> +			 * abort the "git rm" (but once we've successfully removed
> +			 * any file at all, we'll go ahead and commit to it all:
> +			 * by then we've already committed ourselves and can't fail
> +			 * in the middle)
> +			 */
> +			if (!index_only) {
> +				struct strbuf buf = STRBUF_INIT;
>  				strbuf_reset(&buf);
>  				strbuf_addstr(&buf, path);
>  				if (remove_dir_recursively(&buf, 0))
>  					die(_("could not remove '%s'"), path);
>  
>  				removed = 1;
> +				strbuf_release(&buf);
OK, so this part now only deals with the submodule directory.
>  			}
> +			if (!remove_path_from_gitmodules(path, index_only))
> +				stage_updated_gitmodules(&the_index);

And the entry for it in .gitmodules is handled by the helper, whether --cached or not.

This somehow feels wrong for the index_only case; doesn't the helper take contents from the .gitmodules in the working tree and add it to the index?

Unless you touched stage_updated_gitmodules() not to do that in the index_only case, that is.

> +			continue;
And that is all for what is done to a submodule.
Makes sense so far.
Show 12 quoted lines
> +		}
> +		if (!index_only) {
>  			if (!remove_path(path)) {
>  				removed = 1;
>  				continue;
> @@ -396,9 +399,6 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
>  			if (!removed)
>  				die_errno("git rm: '%s'", path);
>  		}
> -		strbuf_release(&buf);
> -		if (gitmodules_modified)
> -			stage_updated_gitmodules(&the_index);

OK, because this should have been done where we called remove_path_from_gitmodules().

Show 26 quoted lines
>  	}
>  
>  	if (write_locked_index(&the_index, &lock_file,
> diff --git a/submodule.c b/submodule.c
> index 9767ba9893..6ce8c8d0d8 100644
> --- a/submodule.c
> +++ b/submodule.c
> @@ -131,7 +131,7 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath)
>   * path is configured. Return 0 only if a .gitmodules file was found, a section
>   * with the correct path=<path> setting was found and we could remove it.
>   */
> -int remove_path_from_gitmodules(const char *path)
> +int remove_path_from_gitmodules(const char *path, int index_only)
>  {
>  	struct strbuf sect = STRBUF_INIT;
>  	const struct submodule *submodule;
> @@ -149,7 +149,8 @@ int remove_path_from_gitmodules(const char *path)
>  	}
>  	strbuf_addstr(&sect, "submodule.");
>  	strbuf_addstr(&sect, submodule->name);
> -	if (git_config_rename_section_in_file(GITMODULES_FILE, sect.buf, NULL) < 0) {
> +	if (git_config_rename_section_in_file(index_only ? GITMODULES_INDEX :
> +					      GITMODULES_FILE, sect.buf, NULL) < 0) {
>  		/* Maybe the user already did that, don't error out here */
>  		warning(_("Could not remove .gitmodules entry for %s"), path);
>  		strbuf_release(&sect);

When !index_only, do we have any guarantee that .gitmodules in the working tree and .gitmodules in the index are in sync? I somehow doubt it.

I would have expected that the updated remove_path_from_gitmodules() would look more like:

 - only if !index_only, nuke the section for the submodule in
   .gitmodules in the working tree.
 - nuke the section for the submodule in .gitmodules in the
   index.

IOW, there would be two git_config_rename_section_in_file() calls to remove the section in !index_only case.

Doing so would also mean that you should not have the caller call stage_updated_gitmodules() at all, even in !index_only case. Imagine if the .gitmodules file in the working tree had local changes (e.g. registered a few more submodules, or updated the url field of a few submodules) that are not yet added to the index when "git rm" removed a submodule. The user does not want them to be in the index yet and "git rm" should not add these unrelated local changes to the index.

Thanks.
Previous: Shourya ShuklaNext: Shourya Shukla
Message 13 of 20 in “rm: changes in the '.gitmodules' are staged after using '--cached'”
  1. Shourya ShuklaFeb 18, 2021
  2. 1/2 rm: changes in the '.gitmodules' are staged after using '--cached'Shourya Shukla, Feb 18, 2021
  3. Philippe BlainFeb 18, 2021
  4. Philippe BlainFeb 18, 2021
  5. Shourya ShuklaFeb 19, 2021
  6. Junio C HamanoFeb 18, 2021
  7. Shourya ShuklaFeb 19, 2021
  8. Junio C HamanoFeb 20, 2021
  9. 2/2 t3600: amend test 46 to check for '.gitmodules' modificationShourya Shukla, Feb 18, 2021
  10. Philippe BlainFeb 18, 2021
  11. 0/1 rm: stage submodule removal from '.gitmodules'Shourya Shukla, Feb 22, 2021
  12. 1/1 rm: stage submodule removal from '.gitmodules' when using '--cached'Shourya Shukla, Feb 22, 2021
  13. Junio C HamanoFeb 22, 2021
  14. Shourya ShuklaMar 5, 2021
  15. Junio C HamanoMar 5, 2021
  16. Junio C HamanoFeb 22, 2021
  17. Shourya ShuklaMar 7, 2021
  18. Junio C HamanoMar 7, 2021
  19. Shourya ShuklaMar 9, 2021
  20. Junio C HamanoMar 9, 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.