#!/bin/sh
-# Fetches the current master from all remotes. Used to sync with all remote
-# hosts. No merging is done.
+# Syncs with remote hosts in the git (sub)directories.
+#
+# Allows fetching (no merge) and pushing at the moment.
-# Select the method, fetch and push is possible.
-if [ x$1 = x -o x$1 = xfetch ]; then
+# Select the method: fetch, push and status is possible.
+if [ x$1 = x -o x$1 = xstatus -o x$1 = xst ]; then
+ method=status
+elif [ $1 = fetch -o $1 = fe ]; then
method=fetch
-elif [ x$1 = xpush ]; then
+elif [ $1 = push -o $1 = pu ]; then
method=push
else
- echo "Unsupported method '$1'. Only 'fetch' and 'push' is supported." >&2
+ echo "Usage: sync.sh [fetch | fe | push | pu | status | st]"
+ echo
+ echo "fetch: Fetch from all remotes."
+ echo "push: Push to all remotes."
+ echo "status: Display status for all git repositories."
exit 1
fi
-# Fetch from/Push to each remote of each git subdirectory.
+# Execute the given method for each git subdirectory.
for project in `find . -name .git -type d`; do
- if [ -d "$project" ]; then
- pwd=`pwd`
- cd "$project"
+ pwd=`pwd`
+ cd "$project"
- # Fetch from/Push to all remotes.
+ # Remove .git from repository name.
+ project=`echo "$project" | sed "s|.git||"`
+
+ # Display all commits not in the master branch (which have to be merged
+ # from the remote branches) or not in the remote branches (which have to
+ # be pushed to the remote hosts).
+ if [ $method = status ]; then
+ # Display a log similar to gitk but for the console.
+ git_log() {
+ output=`git log --graph --all --pretty=oneline --color $1`
+ if [ "x$output" != x ]; then
+ echo $2
+ echo "$output"
+ fi
+ }
+
+ # Display unmerged commits.
+ git_log master.. "$project needs merge:"
+
+ # Display not pushed commits.
+ for branch in `git branch -r`; do
+ branch=`echo "$branch" | sed 's|* ||g'`
+ git_log "$branch.." "$project ($branch) needs push:"
+ done
+
+ # Fetch from/Push to all remotes.
+ else
for remote in `git remote`; do
echo "$project: ${method}ing $remote ..."
git $method "$remote"
done
-
- cd "$pwd"
fi
+
+ cd "$pwd"
done
+
+# Run status after a fetch to see new changes.
+if [ $method = fetch ]; then
+ ./sync.sh status
+fi