Skip to content

Instantly share code, notes, and snippets.

@alex-pex
Last active March 19, 2018 14:36
Show Gist options
  • Save alex-pex/27905c43a244fefcf8d04eae5bbd6d10 to your computer and use it in GitHub Desktop.
Save alex-pex/27905c43a244fefcf8d04eae5bbd6d10 to your computer and use it in GitHub Desktop.
Script to create pull-requests when a new version is released https://hub.github.com/
#!/bin/env bash
hub --version || exit 1
echo
git stash -k
git pull || exit
REF=`git rev-parse --abbrev-ref HEAD`
HEAD=`git rev-parse HEAD`
HEAD_SHORT=`git rev-parse --short HEAD`
echo "$REF HEAD: $HEAD"
MERGE_BASE=`git merge-base $HEAD origin/develop~1`
echo "$REF MERGE_BASE: $MERGE_BASE"
BRANCHES=`git branch -r --contains $MERGE_BASE --no-contains $HEAD --format %\(refname\) | sed -e "s/^refs\/remotes\/origin\///g" | grep "^release/\|^develop"`
BRANCHES_INLINE=`echo $BRANCHES | sed "s/ /, /g"`
echo
echo "Trying to merge $REF into [$BRANCHES_INLINE] branches"
for BRANCH in $BRANCHES
do
echo
echo "=== Merging $REF into $BRANCH ==="
git checkout --quiet "origin/$BRANCH"
git merge --quiet --no-ff --no-commit $REF
git diff --cached --quiet && echo "No diff, skip merging" && continue
git reset --hard
MERGE_NAME=`echo "$HEAD_SHORT/into/$BRANCH" | sed -e "s/\//-/g"`
git checkout -b "merge/$MERGE_NAME"
if (git merge --quiet --no-ff $REF)
then
git push -u origin "merge/$MERGE_NAME"
else
git merge --abort
git reset --hard $REF
git push -u origin "merge/$MERGE_NAME"
fi
#hub pull-request -m "Merge $REF into $BRANCH" -b $BRANCH -l "patch/release-merge"
hub pull-request -m "Merge $REF into $BRANCH" -b $BRANCH
done
echo
git checkout $REF
echo Done!
@alex-pex
Copy link
Author

This script is useful if you use multiple release branches on a gitflow.

If you have

  • release/1.0
  • release/1.1
  • release/2.0
  • develop

And you want to publish a v1.1.1

It will create 2 pull requests

  • merge/v1.1.1-into-release-2.0
  • merge/v1.1.1-into-develop

@alex-pex
Copy link
Author

alex-pex commented Jan 22, 2018

You have to edit ~/.gitconfig and enable "ours" merge strategy

[merge "ours"]
    driver = true

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