Skip to content

Instantly share code, notes, and snippets.

@fffx
Created May 10, 2018 14:54
Show Gist options
  • Save fffx/303df2aa20d946eecc20070d1fef2d82 to your computer and use it in GitHub Desktop.
Save fffx/303df2aa20d946eecc20070d1fef2d82 to your computer and use it in GitHub Desktop.
git hook
#!/usr/bin/env bash
#!/bin/bash -xv # puts this line in the beginning for debug
# This hook, auto checkout related repo to corresponding branch
# Current branch
branch=$(git symbolic-ref --short HEAD)
# Dependended repos
repos=(dependened_repo_a dependened_repo_b)
working_dir=$(git rev-parse --show-toplevel)
# Check out policy
# Determine target branch
if [[ $branch =~ ^test ]]; then
target_branch="test"
elif [[ $branch =~ ^master ]]; then
target_branch="master"
elif [[ $branch =~ ^hotfix ]]; then
target_branch="release"
elif [[ $branch =~ ^release ]]; then
target_branch="release"
#else
# taeget_branch="master"
fi
# Exit if target_branch not set
# if you don't have a default target_branch
if [ -z ${target_branch+x} ]; then
echo 'target branch empty exited'
exit 0;
fi
# Check out each repo to corresponding branch
for repo in "${repos[@]}"; do
# subshell
repo_dir="$working_dir/../$repo"
if [ -d $repo_dir ]; then
echo "checking out $repo to $target_branch ----------------------------"
(cd $repo_dir && git checkout $target_branch)
echo "done ------------------------------------------------------------"
else
echo "$repo_dir not exists, skipped"
fi
done
exit 0
@fffx
Copy link
Author

fffx commented May 10, 2018

TODO:

  • Auto stash save , stash pop and git pull
  • Supress checkout message 2>&1 > /dev/null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment