nvm icon indicating copy to clipboard operation
nvm copied to clipboard

[Fix] specify 'origin' remote name with `git clone ...` installs

Open rivy opened this issue 1 year ago • 12 comments

I ran into this as an installation failure when installing nvm on a PC using some git configuration customizations.

Since git may be configured locally to use a non-'origin' default remote name, specify 'origin' as the remote name when cloning to get the expected local repo setup.

rivy avatar May 01 '24 14:05 rivy

Thanks, this is a great catch!

Unfortunately, i believe git didn't add this option until v2.29, and we currently only require git v1.7.10. Would there be an alternative approach where instead of hardcoding "origin", we derive the default remote name, and just use that throughout the install script?

ljharb avatar May 01 '24 15:05 ljharb

Hmm.

To my knowledge, there is no real default remote after the repo is created. I don't believe git holds any special regard for 'origin', or any other remote, as a default (other than then as the default initial remote name). There's no configuration info that I've seen that makes any differentiation. If there is more than one remote, git remote just displays all of them in alpha order.

A couple of ways to deal with the issue that come to mind are just using the git ... command as amended in the PR followed by the original version as an alternate. From looking at the docs, I believe that prior to v1.1.0 with the '-o' option [ref: https://github.com/git/git/commit/e6c310fd0d7384973efc6b1d5999a5e8a5b2f3bd], there was no way to clone a repo to anything other than the 'origin' remote. It looks like the configuration variable 'clone.defaultRemoteName' was added in v2.30.0 [ref: https://github.com/git/git/commit/de9ed3ef3740f8227cc924e845032954d1f1b1b7]. So, if command git clone "$(nvm_source)" --depth=1 --origin=origin "${INSTALL_DIR}" fails then command git clone "$(nvm_source)" --depth=1 "${INSTALL_DIR}" should always install the remote as 'origin'.

Then using command git clone "$(nvm_source)" --depth=1 --origin=origin "${INSTALL_DIR}" || command git clone "$(nvm_source)" --depth=1 "${INSTALL_DIR}" || { ... } should always set up the repo to have the remote 'origin' pointing at the expected URL.

Alternatively, the script could cd "${INSTALL_DIR" ; git remote rename $(git remote) origin immediately after the clone (without a --origin=origin flag). That should also work for any version and set up the remote 'origin' correctly. (git doesn't throw an error if you rename a remote to the same name.)

I think the first option is likely the better choice. It should work for any version, be localized to a single code location, and should always result in the expected 'origin' setup.

rivy avatar May 03 '24 04:05 rivy

I pushed a modified version.

rivy avatar May 03 '24 04:05 rivy

git docs say Overrides clone.defaultRemoteName from the config. which suggests that git config clone.defaultRemoteName || echo origin would be the value to use?

ljharb avatar May 03 '24 05:05 ljharb

git docs say Overrides clone.defaultRemoteName from the config. which suggests that git config clone.defaultRemoteName || echo origin would be the value to use?

Yep, but that's only really good at the time of initial cloning.

rivy avatar May 03 '24 05:05 rivy

ah, so you're saying that that would work for the initial clone, but wouldn't help with future git operations?

wouldn't we be able to assume that the default, if configured, is unlikely to change?

ljharb avatar May 03 '24 05:05 ljharb

I added references and corrected the mentioned versions to my second post. And, in looking at them again, it looks like -o was added in v1.1.0 [ref: https://github.com/git/git/commit/e6c310fd0d7384973efc6b1d5999a5e8a5b2f3bd], though I don't see the where --origin was added.

I could just simply change it to use -o origin and that would, I believe, fit the version restriction.

rivy avatar May 03 '24 05:05 rivy

ah, so you're saying that that would work for the initial clone, but wouldn't help with future git operations?

wouldn't we be able to assume that the default, if configured, is unlikely to change?

If some user changed it, I think it's actually more likely to change in the future. And the change will be external and unknown as a change to the repo. I think that setting it up with the expected 'origin' name from the start is going to be much more robust. Much less likely that a user would go into the repo and rename it than rethinking the config choice.

Personally, I had 'main' for a while, then went with 'local' on my home PC because I have a lot of customized soft-forks.

But, I'm likely a 0.1%-er here (or even 0.001%-er 😄).

rivy avatar May 03 '24 05:05 rivy

alrighty, let's go with -o origin, and just hardcode it, then.

ljharb avatar May 03 '24 05:05 ljharb

Can there be any install tests that cover this?

Sure. I'll take a look at your tests and try to work up a something that fits.

I think I should be able to add something to install_nvm_from_git.

Are you testing with the minimum git version? I don't see that anywhere.

rivy avatar May 03 '24 17:05 rivy

No, git version is just an implicit requirement.

ljharb avatar May 03 '24 18:05 ljharb