Subject: Re: Git submodule remove

From: Junio C Hamano <[email-masked]>
Date: Thu, 21 Oct 2021 15:47:14 -0700

"brian m. carlson" <[email-masked]> writes:

> On 2021-10-21 at 17:25:38, Kalyan Sriram wrote:
>> Hello,
>> 
>> I was curious why git-submodule does not have an `rm` command. Currently
>> I have to manually delete it from .gitmodules, .git/config,
>> .git/modules/, etc. See [0].
>> 
>> I'd like to contribute my first patch to this project by adding this
>> feature, but wanted to check first with the community if there's any
>> particular reason it was chosen not to not be implemented, or if it's
>> simply that nobody has gotten around to it - it seems to be a relatively
>> common feature someone might want.
>> 
>> Anyway, please let me know if this is something that would be accepted,
>> or if anyone has any comments or suggestions.
>
> I think the reason it hasn't been implemented is that nobody's gotten
> around to it yet.  I certainly would find this useful and have wanted
> the same thing myself, so I can't see a reason why the right series
> wouldn't be accepted.

I tend to agree that nobody felt the need strongly enough.  Code
tends to accumulate without ever getting removed, and removal of a
file, removal of a directory, or removal of a submodule is a much
rarer event compared to other changes people would need to make.
Adding such a feature would have been much more work for those who
faced such a rare occasion to want to use it than just doing it by
hand and committing the result.

I'd imagine that the happy-case implementation should be fairly
straight-forward.  You would:

 - ensure that the submodule is "absorbed" already;

 - run "git rm -f" the submodule to remove the gitlink from the index
   and remove the directory from the working tree; and

 - remove the .gitmodules entry for the submodule.

and you'd leave the final "record the state of the index as a
commit" to the user, simply because the user would want to have
other changes related to the removal of the submodule in the same
commit (like changes to files in the superproject that refer to the
submodule contents or removal of other submodules).

The hard part is unhappy-cases.  There are too many things that can
go wrong and you need to handle all the error cases correctly so
that you do not leave the user's repository in an uncontrollably
messy state.

Thanks.