]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - sync.sh
lib.sh: Add support for FreeBSD to os().
[config/dotfiles.git] / sync.sh
1 #!/bin/sh
2
3 # Syncs with remote hosts in the git (sub)directories.
4 #
5 # Allows fetching (no merge) and pushing at the moment.
6
7
8 # Select the method: fetch, push and status is possible.
9 if [ x$1 = x -o x$1 = xstatus -o x$1 = xst ]; then
10     method=status
11 elif [ $1 = fetch -o $1 = fe ]; then
12     method=fetch
13 elif [ $1 = push -o $1 = pu ]; then
14     method=push
15 else
16     echo "Usage: sync.sh [fetch | fe | push | pu | status | st]"
17     echo
18     echo "fetch: Fetch from all remotes."
19     echo "push: Push to all remotes."
20     echo "status: Display status for all git repositories."
21     exit 1
22 fi
23
24 # Execute the given method for each git subdirectory.
25 for project in `find . -name .git -type d`; do
26     pwd=`pwd`
27     cd "$project"
28
29     # Remove .git from repository name.
30     project=`echo "$project" | sed "s|.git||"`
31
32     # Display all commits not in the master branch (which have to be merged
33     # from the remote branches) or not in the remote branches (which have to
34     # be pushed to the remote hosts).
35     if [ $method = status ]; then
36         # Display a log similar to gitk but for the console.
37         git_log() {
38             output=`git log --graph --all --pretty=oneline --color $1`
39             if [ "x$output" != x ]; then
40                 echo $2
41                 echo "$output"
42             fi
43         }
44
45         # Display unmerged commits.
46         git_log master.. "$project needs merge:"
47
48         # Display not pushed commits.
49         for branch in `git branch -r`; do
50             branch=`echo "$branch" | sed 's|* ||g'`
51             git_log "$branch.." "$project ($branch) needs push:"
52         done
53
54     # Fetch from/Push to all remotes.
55     else
56         for remote in `git remote`; do
57             echo "$project: ${method}ing $remote ..."
58             git $method "$remote"
59         done
60     fi
61
62     cd "$pwd"
63 done
64
65 # Run status after a fetch to see new changes.
66 if [ $method = fetch ]; then
67     ./sync.sh status
68 fi