Skip to content

Instantly share code, notes, and snippets.

@rafi
Last active August 29, 2015 14:17
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 rafi/c061779d6b67abe6fe01 to your computer and use it in GitHub Desktop.
Save rafi/c061779d6b67abe6fe01 to your computer and use it in GitHub Desktop.
Git subtree push --rejoin for all branches that changed a directory, using git merge-base.
#!/usr/bin/env bash
set -e
set -u
# Exit program with error message.
die() {
echo -e "ERROR: $1"
exit $2
}
# $1 = branch name
ignore_branch() {
local branch branches=("HEAD" "develop" "master")
local pattern="release"
[[ "$1" == "$pattern"* ]] && return 0;
for branch in "${branches[@]}"; do
[[ "$1" == "$branch" ]] && return 0;
done
return 1
}
# $1 = remote name, $2 = branch name
modified_prefix() {
local prefix='Web' master="$1/develop"
local base="$(git merge-base ${1}/${2} "${master}")"
local diff="$(git diff --stat ${1}/${2}..${base} -- "$prefix")"
if [ -z "$diff" ]; then
return 0
fi
return 1
}
# $1 = remote name, $2 = branch name
checkout_branch() {
echo -n ":: Branch: ${1}/${2}"
if git show-ref --verify --quiet "refs/heads/${2}"; then
echo ' (checking out local branch...)'
git checkout "${2}"
git merge --ff-only "${1}/${2}" \
|| die "Diverged local branch (${2}), synchronize with ${1} first." 2
else
echo ' (creating new branch...)'
git checkout -b "${2}" "${1}/${2}"
fi
}
# Main procedure.
main () {
local remote='origin'
local refs="refs/remotes/$remote"
local branch branches=()
eval "$(git for-each-ref --format='branches+=(%(refname))' "$refs" \
| sed "s/refs\/remotes\/$remote\///g")"
# Iterate through all branches in specific ref prefix
for branch in "${branches[@]}"; do
# Ignore certain branches, certainly those who didn't change prefix.
if ignore_branch "$branch" || modified_prefix "$remote" "$branch"; then
continue
fi
checkout_branch "$remote" "$branch"
git log --decorate --oneline --graph -1
echo ':: Performing subtree push'
git subtree push --prefix Web webapp "$branch" --rejoin
echo ":: Finished branch \`$branch\` successfully, hooray!"
echo
done
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment