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

From: Shourya Shukla <[email-masked]>
Date: Fri, 5 Mar 2021 23:28:16 +0530

Hi Junio!

Really really sorry for the late reply. I was busy in some personal
engagements and was travelling for the past 8-9 days.

On 22/02 10:58, Junio C Hamano wrote:
> Shourya Shukla <[email-masked]> writes:
> 
> > 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.

Sure. Will make it more precise and rather technically connect.

> And that is all for what is done to a submodule.
> 
> Makes sense so far.
> 
> > +		}
> > +		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().
> 
> >  	}
> >  
> >  	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.

Won't this be deviating from the current behaviour of 'git rm'?
Currently, the above case won't process and the user will be asked to
stage or undo the mods they made before moving forward. If I am not
mistaken, won't we deviate from the case if we do the above? As of now,
I tried this:

	if (!index_only) {
		if (git_config_rename_section_in_file(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);
			return -1;
		}
	}
	if (git_config_rename_section_in_file(GITMODULES_INDEX , 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);
		return -1;
	}

Everything else being unchanged. Therefore, we still have the
'stage_updated_gitmodules()' call. If we don't call this function then
won't we be NOT adding the updated '.gitmodules' to the staging area,
something which is done as of now?

Or am I mising something here?

Regards,
Shourya Shukla