+# Creates a new git repository in $1, adds a new remote named $2 and fetches
+# the master on $3. If the git repository already exists a new remote $2 for
+# $3 is added. If the remote already exists nothing happens.
+function git_remote_init_update() {
+ # Make sure the requested directory exists.
+ mkdir -p "$1"
+ # Go to the target directory.
+ pwd=`pwd`
+ cd "$1"
+
+ # Create the git repository if it doesn't exist yet.
+ new=no
+ if [ ! -d .git ]; then
+ echo "Creating new git repository in '$1'."
+ git init > /dev/null
+ new=yes
+ fi
+
+ # If the remote doesn't exist add it and fetch from remote.
+ git remote | grep "$2" > /dev/null
+ if [ "$?" -ne "0" ]; then
+ echo "Adding remote '$2' to '$1'."
+ git remote add -t master "$2" "$3/$1"
+ git fetch "$2" > /dev/null
+ # Pushing to the remote pushes only the master branch in remotes named
+ # the hostname of this machine. This makes it easy to see where
+ # changes came from.
+ echo " push = +refs/heads/master:refs/remotes/`hostname`/master" \
+ >> .git/config
+ else
+ echo "Remote '$2' already exists in '$1'."
+ fi
+
+ # Merge with remote master if the repository was just created, otherwise
+ # the repository starts empty.
+ if [ $new = yes ]; then
+ git merge "$2/master"
+ fi
+
+ # Go back to the starting directory.
+ cd "$pwd"
+ unset pwd
+}