This repository has been archived by the owner. It is now read-only.

If `git remote add` fails, fallback to `git remote set-url`. #31107

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
2 participants
Contributor

jdhealy commented Jul 25, 2014

Avoids git error fatal: remote origin already exists.

$ ruby ./install
==> This script will install:
/tmp/homebrew-test/bin/brew
/tmp/homebrew-test/Library/...
/tmp/homebrew-test/share/man/man1/brew.1

Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /bin/mkdir /tmp/homebrew-test
Password:
==> /usr/bin/sudo /bin/chmod g+rwx /tmp/homebrew-test
==> /usr/bin/sudo /usr/bin/chgrp admin /tmp/homebrew-test
==> /usr/bin/sudo /bin/chmod g+rwx /Library/Caches/Homebrew
==> Downloading and installing Homebrew...
fatal: remote origin already exists.
If `git remote add` fails, fallback to `git remote set-url`.
Avoids git error `fatal: remote origin already exists.`
@@ -172,7 +172,7 @@ Dir.chdir HOMEBREW_PREFIX do
if git
# we do it in four steps to avoid merge errors when reinstalling
system git, "init", "-q"
- system git, "remote", "add", "origin", "https://github.com/Homebrew/homebrew"
+ system "#{git} remote add origin https://github.com/Homebrew/homebrew || #{git} remote set-url --add origin git://github.com/Homebrew/homebrew.git"
@jacknagel

jacknagel Jul 25, 2014

Contributor

system returns false if the command fails, so this can be done in ruby rather than in the shell.

@jdhealy

jdhealy Jul 25, 2014

Contributor

Changing to

(system git, "remote", "add", "origin", "https://github.com/Homebrew/homebrew") || (system git, "remote", "set-url", "--add", "origin", "git://github.com/Homebrew/homebrew.git")

gets me

==> This script will install:
/tmp/homebrew-test/bin/brew
/tmp/homebrew-test/Library/...
/tmp/homebrew-test/share/man/man1/brew.1

Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /bin/mkdir /tmp/homebrew-test
Password:
==> /usr/bin/sudo /bin/chmod g+rwx /tmp/homebrew-test
==> /usr/bin/sudo /usr/bin/chgrp admin /tmp/homebrew-test
==> /usr/bin/sudo /bin/chmod g+rwx /Library/Caches/Homebrew
==> Downloading and installing Homebrew...
fatal: remote origin already exists.
Failed during: git remote add origin https://github.com/Homebrew/homebrew

system is calling https://github.com/jdhealy/homebrew/blob/29612e58b619feda9629d7b648bcf08797bb5839/install#L34, not http://www.ruby-doc.org/core-2.1.2/Kernel.html#method-i-system, right?

@jacknagel

jacknagel Jul 26, 2014

Contributor

Ahhh, yes.

Contributor

jacknagel commented Jul 26, 2014

Hmm, under what circumstances does this happen? It seems like the installer should abort if the git repo exists:

https://github.com/Homebrew/homebrew/blob/b8ba5c4d7e423e271c83cabb7cebd8ce7ec458f9/install#L109-L113

@jacknagel jacknagel added the installer label Jul 26, 2014

Contributor

jdhealy commented Jul 26, 2014

So, I just did some debugging and I'm pretty sure that I'm hitting this because of this line in my ~/.gitconfig:

[remote "origin"]
    fetch = +refs/notes/*:refs/notes/*

which is in regards to Git Notes.

Not sure how popular global [remote "origin"] configs are in general, but that's what seems to have caused the issue for me.

Contributor

jacknagel commented Jul 27, 2014

I see. I use repository templates for that sort of thing, but if it's a valid config then I suppose the script should be robust against it.

We can structure it like Kernel.system() || system and avoid the shell while retaining the safety of the custom system method.

jacknagel added a commit that referenced this pull request Aug 12, 2014

Be robust against existing remote.origin config
I will take this opportunity to opine and say that I don't think sharing
remote config settings across repositories makes sense. Remotes belong
to the repository, and the URL has to be configured anyway. It breaks
the assumption that you can run "git remote add origin" in a blank repo.
Repository templates are a much nicer way to address any perceived
duplication.

Nonetheless, git allows this to happen and there are a number of
misguided blogposts advocating it, so we should be robust against it.

Closes #31107.

@jacknagel jacknagel closed this Aug 12, 2014

Contributor

jacknagel commented Aug 12, 2014

Tweaked and applied.

@xu-cheng xu-cheng locked and limited conversation to collaborators Feb 17, 2016

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.