]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - setup.sh
setup.sh: Add support for find without -executable.
[config/dotfiles.git] / setup.sh
1 #!/bin/sh
2
3 # Runs setup.sh in all configuration directories. Must be run in the main
4 # configuration directory.
5
6
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 git_remote_init_update() {
11     # Make sure the requested directory exists.
12     mkdir -p "$1"
13     # Go to the target directory.
14     pwd=`pwd`
15     cd "$1"
16
17     # Create the git repository if it doesn't exist yet.
18     new=no
19     if [ ! -d .git ]; then
20         echo "Creating new git repository in '$1'."
21         git init > /dev/null
22         new=yes
23     fi
24
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 and abort if the fetch was unsuccessful.
32         if [ "$?" -ne "0" ]; then
33             git remote rm "$2"
34             exit 1
35         fi
36
37         # Pushing to the remote pushes only the master branch in remotes named
38         # the hostname of this machine. This makes it easy to see where
39         # changes came from.
40         echo "  push = +refs/heads/master:refs/remotes/`hostname`/master" \
41             >> .git/config
42     else
43         echo "Remote '$2' already exists in '$1'."
44     fi
45
46     # Merge with remote master if the repository was just created, otherwise
47     # the repository starts empty. Also run gc to compress the new repository.
48     if [ $new = yes ]; then
49         git merge "$2/master"
50         git gc > /dev/null
51     fi
52
53     # Go back to the starting directory.
54     cd "$pwd"
55     unset pwd
56 }
57
58 # Run setup.sh in each project.
59 if [ "$#" -eq "0" ]; then
60     for path in `find . -name setup.sh -type f`; do
61         # Skip this directory to prevent an infinite loop.
62         [ "$path" = "./setup.sh" ] && continue
63         # Skip non executable setup.sh files as an easy way to deactivate one.
64         [ ! -x "$path" ] && continue
65
66         project=`echo "$path" | sed 's|/setup.sh$||'`
67
68         echo "running setup.sh in '$project'"
69         ( cd "$project"; ./setup.sh > /dev/null )
70     done
71 # Create git repository if necessary and/or additional remotes and fetch them.
72 elif [ "$#" -ge "2" ]; then
73     # Get name of the remote and the remote location.
74     name="$1"
75     remote="$2"
76     # Remove $1 and $2 to get the possible subdirectories in $@.
77     shift
78     shift
79
80     # Create the git repository if necessary and add the remotes.
81     git_remote_init_update . "$name" "$remote"
82     for project in $@; do
83         git_remote_init_update "$project" "$name" "$remote"
84     done
85 # Usage message.
86 else
87     echo "Usage: ./setup.sh <remote-name> <git-repository> [subdirectories]*
88
89 Example:
90 ./setup.sh nightwish ssh://user@nightwish/home/user/dotfiles shell vcs
91
92 This will add the remote nightwish given by the ssh URL and fetch the master
93 for dotfiles, dotfiles/shell and dotfiles/vcs if they don't exist already. If
94 the remotes exist already nothing happens. This also sets up fetching/pushing
95 settings so only the master is fetched and pushed."
96 fi