Skip to content

Instantly share code, notes, and snippets.

@motemen
Created November 8, 2010 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motemen/667573 to your computer and use it in GitHub Desktop.
Save motemen/667573 to your computer and use it in GitHub Desktop.
#!/bin/sh
root=`git rev-parse --show-toplevel`
echo=/bin/echo
git ls-files --stage | grep '^160000 ' |
while read mode sha1 stage path
do
$echo -n "Submodule path '$path': ";
cd $root; cd $path
track_branch=`git config --file $root/.gitmodules submodule.$path.track`
if [ -z "$track_branch" ]; then
$echo 'tracking branch not found; skip.'
else
$echo -n "track $track_branch... "
current_branch=`git symbolic-ref --quiet HEAD | sed 's#^refs/heads/##'`
local_branch=${track_branch#*/}
if [ "$current_branch" = "$local_branch" ]; then
$echo "already on $current_branch."
git pull
else
if git rev-parse --quiet --verify $local_branch > /dev/null; then
$echo "checkout existing branch $local_branch."
git checkout $local_branch && git pull
else
$echo "start tracking $track_branch."
git fetch && git checkout --track $track_branch
fi
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment