3 # Runs setup.sh in all configuration directories. Must be run in the main
4 # configuration directory.
7 # Creates a new git repository in $1, adds a new remote named $2 and fetches
8 # the master on $3. If the git repository already exists a new remote $2 for
9 # $3 is added. If the remote already exists nothing happens.
10 function git_remote_init_update() {
11 # Make sure the requested directory exists.
13 # Go to the target directory.
17 # Create the git repository if it doesn't exist yet.
19 if [ ! -d .git ]; then
20 echo "Creating new git repository in '$1'."
25 # If the remote doesn't exist add it and fetch from remote.
26 git remote | grep "$2" > /dev/null
27 if [ "$?" -ne "0" ]; then
28 echo "Adding remote '$2' to '$1'."
29 git remote add -t master "$2" "$3/$1"
30 git fetch "$2" > /dev/null
31 # Remove the remote if the fetch was unsuccessful.
32 if [ "$?" -ne "0" ]; then
36 # Pushing to the remote pushes only the master branch in remotes named
37 # the hostname of this machine. This makes it easy to see where
39 echo " push = +refs/heads/master:refs/remotes/`hostname`/master" \
42 echo "Remote '$2' already exists in '$1'."
45 # Merge with remote master if the repository was just created, otherwise
46 # the repository starts empty. Also run gc to compress the new repository.
47 if [ $new = yes ]; then
52 # Go back to the starting directory.
57 # Run setup.sh in each project.
58 if [ "$#" -eq "0" ]; then
59 for project in `find . -name .git -type d`; do
60 # Skip this directory to prevent an infinite loop.
61 [ "$project" = "./.git" ] && continue
63 [ -d "$project" ] && (echo "running setup.sh in '$project'";
64 cd "$project/.."; ./setup.sh > /dev/null)
66 # Create git repository if necessary and/or additional remotes and fetch them.
67 elif [ "$#" -ge "2" ]; then
68 # Get name of the remote and the remote location.
71 # Remove $1 and $2 to get the possible subdirectories in $@.
75 # Create the git repository if necessary and add the remotes.
76 git_remote_init_update . "$name" "$remote"
78 git_remote_init_update "$project" "$name" "$remote"
82 echo "Usage: ./setup.sh <remote-name> <git-repository> [subdirectories]*
85 ./setup.sh nightwish ssh://user@nightwish/home/user/dotfiles shell vcs
87 This will add the remote nightwish given by the ssh URL and fetch the master
88 for dotfiles, dotfiles/shell and dotfiles/vcs if they don't exist already. If
89 the remotes exist already nothing happens. This also sets up fetching/pushing
90 settings so only the master is fetched and pushed."