Skip to content

Instantly share code, notes, and snippets.

@oggy
Created October 14, 2010 18:54
Show Gist options
  • Save oggy/626787 to your computer and use it in GitHub Desktop.
Save oggy/626787 to your computer and use it in GitHub Desktop.
#!/bin/sh
######################################################################
#
# switch_bundler_environment OLD-BRANCH NEW-BRANCH
#
# Swap out the old branch's Bundler environment, and swaps in the new
# branch's. Arguments may be branch names or SHAs.
#
# For best results, add this to .git/hooks/post-checkout:
#
# #!/bin/sh
# exec script/switch_bundler_environment $1 $2
#
######################################################################
branch()
{
local branchish=$1
if [ -f ".git/refs/heads/$branchish" ]; then
echo "$branchish"
else
for f in .git/refs/heads/*; do
if [ -f "$f" ]; then
local name=$(basename $f)
if [ "$(cat .git/refs/heads/$name)" = "$branchish" ]; then
echo "$name"
fi
fi
done
fi
}
save()
{
local branch=$1
if [ -n $branch ]; then
copy_directory .bundle ".branch_bundles/$branch/bundle"
fi
}
load()
{
local branch=$1
if [ -n $branch ]; then
copy_directory ".branch_bundles/$branch/bundle" .bundle
fi
}
copy_directory()
{
local src=$1
local dst=$2
rm -rf "$dst"
if [ -d "$src" ]; then
mkdir -p "$(dirname $dst)"
cp -r "$src" "$dst"
fi
}
if [ $# -ne 2 ]; then
echo "USAGE: #$0 FROM-BRANCHISH TO-BRANCHISH" >&2
exit 1
fi
old_branch=$(branch $1)
new_branch=$(branch $2)
if [ -n "$old_branch" ]; then
save $old_branch
fi
# If no bundler environment exists for the new branch, only kill the
# existing one if the Gemfile is different. This way forking a new
# branch off the current one doesn't require a "bundle install".
if [ -n "$new_branch" -a \
'(' \
-d ".branch_bundles/$new_branch/bundle" -o \
-n "$(git diff $old_branch:Gemfile $new_branch:Gemfile)" \
')' \
]; then
load $new_branch
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment