# configuration directory.
-# Projects which use git.
-projects="browser lftp mail music os shell vcs vim"
+# 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
+}
# Run setup.sh in each project.
-for project in `find . -name .git -type d`; do
- # Skip this directory to prevent an infinite loop.
- [ "$project" = "./.git" ] && continue
+if [ "$#" -eq "0" ]; then
+ for project in `find . -name .git -type d`; do
+ # Skip this directory to prevent an infinite loop.
+ [ "$project" = "./.git" ] && continue
+
+ [ -d "$project" ] && (echo "running setup.sh in '$project'";
+ cd "$project/.."; ./setup.sh > /dev/null)
+ done
+# Create git repository if necessary and/or additional remotes and fetch them.
+elif [ "$#" -ge "2" ]; then
+ # Get name of the remote and the remote location.
+ name="$1"
+ remote="$2"
+ # Remove $1 and $2 to get the possible subdirectories in $@.
+ shift
+ shift
+
+ # Create the git repository if necessary and add the remotes.
+ git_remote_init_update . "$name" "$remote"
+ for project in $@; do
+ git_remote_init_update "$project" "$name" "$remote"
+ done
+# Usage message.
+else
+ echo "Usage: ./setup.sh <remote-name> <git-repository> [subdirectories]*
+
+Example:
+./setup.sh nightwish ssh://user@nightwish/home/user/dotfiles shell vcs
- [ -d "$project" ] && (echo "running setup.sh in '$project'";
- cd "$project/.."; ./setup.sh > /dev/null)
-done
+This will add the remote nightwish given by the ssh URL and fetch the master
+for dotfiles, dotfiles/shell and dotfiles/vcs if they don't exist already. If
+the remotes exist already nothing happens. This also sets up fetching/pushing
+settings so only the master is fetched and pushed."
+fi