Re: Possible bug in Git submodule update --inti

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Søren,

Le 2024-11-18 à 03:14, Bech Christensen, Søren a écrit :
> 
> Hi
> 
> I am trying to replace a submodule of a repository with a rewritten version.
> Somebody added a huge json file without enabling lfs, so I cannot move the submodule to GitHub without the rewrite.
> 
> The path of the submodule is: ./Submodules/Lodam.Standard, which I have no intensions of changing.
> 
> The url of the original submodule is: https://lodamrd.visualstudio.com/Features.Net/_git/Lodam.Standard
> The url of the rewritten submodule is: https://lodamrd.visualstudio.com/Features.Net/_git/Uniware.Standard
> 
> 
> I planned to do a:
> git submodule deinit -f <path-to-submodule>

Here you deinit the submodule, such that the "submodule Submodules/Lodam.Standard" section is 
removed from the config file of the superproject: this submodule is not considered
active anymore.

> git submodule set-url -- <path-to-submodule> <new-url>

Here you use 'set-url' to change the submodule URL. The man page states [1]:
"Sets the URL of the specified submodule to <newurl>. Then, it will automatically 
synchronize the submodule’s new remote URL configuration."

In your case, this modifies the 'url' config in the 
"submodule Submodules/Lodam.Standard" section of your .gitmodules. 
However it does not do anything else, since the submodule 
was deinitialized above. This is mentioned in the doc for 
'git submodule sync' [2]: 
"It will only affect those submodules which already have a URL entry 
in .git/config (that is the case when they are initialized or freshly added)."

> git submodule update --init <path-to-submodule>

This re-initializes your submodule, which copies the new URL from .gitmodules
to the 'url' config in the "submodule Submodules/Lodam.Standard"
section of your '.git/config', as mentioned
in the doc for 'git submodule init' [3]. Note that this is _not_ 
the exact command that you ran according to the trace output below, which is:

> D:\temp\Mjolnir [develop ≡ +0 ~1 -0 !]>  git submodule update --init --remote ./submodules/Lodam.Standard

Notably the '--remote' flag changes what happens next:
- without --remote, Git tries to checkout the submodule commit recorded in the superproject
- with --remote, Git tries to checkout the commit at the tip of the remote HEAD of the submodule [4]

I'll discuss what happens with '--remote' since this is what you appear to be using.

After writing the url to '.git/config' as noted above, Git checks if
it needs to clone the submodule. In your case it does find a Git repository for the 
submodule at .git/modules/Lodam.Standard, so it goes on to the next step.
Next it fetches the submodule remote to get the latest submodule commits
(since --remote was passed). We can see in the trace output that 'git fetch'
is invoked. This will thus fetch the default remote in the submodule 
(usually called "origin"). The URL used for this fetch is the value of
'remote.origin.url', which _still_ points to https://lodamrd.visualstudio.com/Features.Net/_git/Lodam.Standard
because none of the previous commands have updated it.

> But actually the sources for the submodule is fetched from the original (same commit SHAs  as before), whereas if I do:
> 
> git submodule deinit -f <path-to-submodule>
> git submodule set-url -- <path-to-submodule> <new-url>
> git clone <new-url> <path-to-submodule>
> 
> I get the submodule from the new url with the rewritten commit SHA’s.

If you manually do a 'git clone' you indeed to get the new objects from
the new URL, but the Git repository is not put into .git/modules/Submodules/Lodam.Standard
as is the case with 'git submodule'. Plus, you still have the old repository at 
.git/modules/Submodules/Lodam.Standard, so it is not a good solution in my opinion.

I think what you'd want to do is simply:

git submodule set-url -- <path-to-submodule> <new-url> 
git submodule update --init --remote

This will fetch the new URL and checkout the tip commit of its HEAD branch.
You will still have all the objects from the old URL locally though. If you want
to avoid that, you should completely remove the Git repository of the submodule:

git submodule set-url -- <path-to-submodule> <new-url> 
git submodule deinit -f <path-to-submodule>
rm -rf $(git rev-parse --git-dir)/modules/Submodules/Lodam.Standard
git submodule update --init --remote

Hope this helps,
Philippe.


[1] https://git-scm.com/docs/git-submodule#Documentation/git-submodule.txt-set-url--ltpathgtltnewurlgt
[2] https://git-scm.com/docs/git-submodule#Documentation/git-submodule.txt-sync--recursive--ltpathgt82308203
[3] https://git-scm.com/docs/git-submodule#Documentation/git-submodule.txt-init--ltpathgt82308203
[4] https://git-scm.com/docs/git-submodule#Documentation/git-submodule.txt---remote




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux