Skip to content

Instantly share code, notes, and snippets.

@schnabear
Last active October 11, 2018 07:58
Show Gist options
  • Save schnabear/50399c4799cb0a4add72 to your computer and use it in GitHub Desktop.
Save schnabear/50399c4799cb0a4add72 to your computer and use it in GitHub Desktop.
Git Mass Merge
#!/usr/bin/env python
import argparse
import subprocess
def main():
parser = argparse.ArgumentParser(description='Git Mass Merge')
parser.add_argument('-p', type=str, required=True, dest='parent_branch')
parser.add_argument('-c', type=str, required=True, dest='child_branch', nargs='+')
parser.add_argument('-u', action='store_true', default=False, dest='is_update_upstream')
parser.add_argument('-a', action='store_true', default=False, dest='is_no_edit')
parser.add_argument('-f', action='store_true', default=False, dest='is_force_push')
option = parser.parse_args()
print 'git fetch --all'
print subprocess.check_output(['git', 'fetch', '--all'], stderr=subprocess.STDOUT)
print 'git checkout ' + option.parent_branch
print subprocess.check_output(['git', 'checkout', option.parent_branch], stderr=subprocess.STDOUT)
if not option.is_force_push:
try:
print 'git pull origin ' + option.parent_branch
print subprocess.check_output(['git', 'pull', 'origin', option.parent_branch], stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print e.output
for branch in option.child_branch:
try:
if option.is_no_edit:
print 'git pull --no-edit origin ' + branch
print subprocess.check_output(['git', 'pull', '--no-edit', 'origin', branch], stderr=subprocess.STDOUT)
else:
print 'git pull origin ' + branch
print subprocess.check_output(['git', 'pull', 'origin', branch], stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print e.output
raise SystemExit()
extras = ''
if option.is_force_push:
extras = '-f'
if option.is_update_upstream:
if option.is_force_push:
print 'git push -f origin ' + option.parent_branch
print subprocess.check_output(['git', 'push', '-f', 'origin', option.parent_branch], stderr=subprocess.STDOUT)
else:
print 'git push origin ' + option.parent_branch
print subprocess.check_output(['git', 'push', 'origin', option.parent_branch], stderr=subprocess.STDOUT)
print 'DONE'
if __name__ == "__main__":
main()
#!/bin/bash
show_usage() {
echo "USAGE: `basename $0` -p PARENT_BRANCH -c CHILD_BRANCH,..,CHILD_BRANCH [-u] [-a] [-f] [-h]"
echo "-p PARENT BRANCH"
echo "-c CHILD BRANCH"
echo "-u UPDATE UPSTREAM"
echo "-a ACCEPT AUTO GENERATED COMMIT MESSAGE"
echo "-f FORCE PUSH"
echo "-h HELP"
exit 1
}
my_eval() {
echo "$1"
eval "$1" >&1
local EXIT_CODE=$?
if test "$EXIT_CODE" -gt "0"; then
exit $EXIT_CODE
fi
}
my_error() {
echo "$1" >&2
exit 1
}
if [[ $# -lt 2 ]]; then
show_usage
fi
while getopts ":p:c:uafh" OPT; do
case $OPT in
p)
PARENT_BRANCH="$OPTARG"
;;
c)
CHILD_BRANCH="$(echo $OPTARG | sed 's/,/ /g')"
;;
u)
UPDATE_UPSTREAM="Y"
;;
a)
NO_EDIT="Y"
;;
f)
FORCE_PUSH="Y"
;;
h)
show_usage
;;
\?)
echo "Invalid option: -$OPTARG" >&2
show_usage
;;
:)
echo "Option -$OPTARG requires an argument." >&2
show_usage
;;
esac
done
shift $((OPTIND-1))
my_eval "git fetch --all"
my_eval "git checkout \"$PARENT_BRANCH\""
if [[ "$FORCE_PUSH" != "Y" ]]; then
my_eval "git pull origin \"$PARENT_BRANCH\""
fi
OPTIONS=""
if [[ "$NO_EDIT" = "Y" ]]; then
OPTIONS="--no-edit"
fi
for branch in $CHILD_BRANCH; do
my_eval "git pull $OPTIONS origin \"$branch\""
done
OPTIONS=""
if [[ "$FORCE_PUSH" = "Y" ]]; then
OPTIONS="-f"
fi
if [[ "$UPDATE_UPSTREAM" = "Y" ]]; then
my_eval "git push $OPTIONS origin \"$PARENT_BRANCH\""
fi
echo "DONE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment