public
Last active

Replace a Git installation in /usr/bin/ with symlinks to a preferred git installation.

  • Download Gist
update_git.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#!/usr/bin/env python -tt
 
"""Xcode4 installs and expects to find a git installation at /usr/bin.
This is a pain if you want to control your own installation of git that
might be installed elsewhere. This script replaces the git files in
/usr/bin with symlinks to the preferred installation.
 
Update the 'src_directory' to the location of your preferred git installation.
"""
import sys
import os
 
#- Configuration -----------------------------------------------------------------
src_directory = '/usr/local/git/bin/' # preferred installation
#----------------------------------------------------------------------------------
 
dest_directory = '/usr/bin/'
files = ('git','git-cvsserver','git-receive-pack','git-shell','git-upload-archive','git-upload-pack','gitk')
 
def main():
if os.getuid():
print "This script needs to be run as 'sudo python update_git.py'"
sys.exit(1)
 
for a_file in files:
src_file = os.path.join(src_directory, a_file)
dest_file = os.path.join(dest_directory, a_file)
 
if os.path.exists(dest_file):
os.remove(dest_file)
 
os.symlink(src_file, dest_file)
 
if __name__ == '__main__':
main()

Notice the 'sudo' at line 18? That means you'll have to enter your password. No need to panic, I'm not trying to spoof you but need this to modify files in '/usr/bin'.

Why not just have the script do an ln -nfs and make the user invoke it with sudo ./update_git.py? Means they know it's sudo asking for the password, not your script (if they can't read .py)

Also, this should do the same job:

#!/usr/bin/env zsh
FROM_DIR=/usr/bin
TO_DIR=/usr/local/git/bin
for i in $FROM_DIR/git*; ln -nfs $i $(echo -n $i | sed -Ee "s#$FROM_DIR#$TO_DIR#")

Wondered why my git is coming from /usr/local/bin as per installed by homebrew, turns out I put /usr/local/bin before /usr/bin in my PATH, so no need to fiddle the symlinks, the shell picks homebrew's git to use already. And the git commands in /usr/bin are what Xcode expects them to be (shouldn't matter that much, but leaving them unchanged is always nice.)

Thanks for the Shell script. I wanted an excuse to play about with Python.

What caius said: don't do sudo inside the script, require that it gets called with sudo. Also, consider a "and your original files have been moved to ${SAFEPLACE}" option :-D

Applied some suggestions.

Script requires sudo to run and uses the OS module to create the symlinks.

Just a small note : caius' script does the contrary of the python script.
If you want to update git and use it as default, FROM_DIR and TO_DIR should be exchanged in the zsh script.

Thanks, maybe one day xcode will update their git..

The version of Git that comes with Xcode is fairly recent. This script is just for those who want to use the development builds of Git.

Can any one help me. I am setting up my local development environment. I have a server (MAC OS X Lion Server) and want that to house git and use the repository over http. I followed all the steps from http://www.pkgbox.org/wordpress/2011/08/setting-up-a-git-server-on-macos-x-lion/ but now I get fatal: http:/myname@server/repo/test-project.git/info/refs not found did you run git update-server-info on the server? Please help i am running out on time... I want to use git and start this project...

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.