Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Closed
wants to merge 1 commit into from

2 participants

@jdhealy

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.
@jdhealy jdhealy If `git remote add` fails, fallback to `git remote set-url`.
Avoids git error `fatal: remote origin already exists.`
29612e5
@jacknagel jacknagel commented on the diff
@@ -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 Collaborator

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

@jdhealy
jdhealy added a note

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 Collaborator

Ahhh, yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jacknagel
Collaborator

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
@jdhealy

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.

@jacknagel
Collaborator

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 jacknagel referenced this pull request from a commit
@jacknagel jacknagel 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.
5cd6b35
@jacknagel jacknagel closed this
@jacknagel
Collaborator

Tweaked and applied.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 22, 2014
  1. @jdhealy

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

    jdhealy authored
    Avoids git error `fatal: remote origin already exists.`
This page is out of date. Refresh to see the latest.
Showing with 1 addition and 1 deletion.
  1. +1 −1  install
View
2  install
@@ -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 Collaborator

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

@jdhealy
jdhealy added a note

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 Collaborator

Ahhh, yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
args = git, "fetch", "origin", "master:refs/remotes/origin/master", "-n"
args << "--depth=1" if ARGV.include? "--fast"
Something went wrong with that request. Please try again.