Skip to content

Instantly share code, notes, and snippets.

@kabir
Created March 14, 2019 10:13
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 kabir/984758c756bc96ded4a5a2eff2453d99 to your computer and use it in GitHub Desktop.
Save kabir/984758c756bc96ded4a5a2eff2453d99 to your computer and use it in GitHub Desktop.
PR script

Add to ~/.profile:

# git pull request processing
pull() {
  if [ "x$1" = "x" ]; then 
    echo No branch or PRs specified
    echo either:
    echo pull branch-name 111 222 333
    echo or:
    echo pull 111 222 333
    echo where 111, 222, 333 are the PR \#s
    echo and branch-name is the target branch for the merge
    return
  fi 
  
  BRANCH=master
  HAS_BRANCH=0
  
  if [[ $1 =~ ^[0-9]+$ ]]; then
    echo No branch specfied, defaulting to master
    HAS_BRANCH=0
  else
    BRANCH=$1
    HAS_BRANCH=1
  fi

  echo Branch is $BRANCH

  cmd="git fetch upstream $BRANCH"
  FIRST=1
  for var in "$@"
  do
    if [ $HAS_BRANCH = "1" ] && [ $FIRST = "1" ] ; then
      FIRST=0
    else 
      cmd="$cmd pull/$var/head:pullRequest$var"
    fi
  done

  $cmd

  git branch -D pulls-$BRANCH
  git checkout $BRANCH
  git rebase upstream/$BRANCH
  git checkout -b pulls-$BRANCH
  
  for var in "$@"
  do
    git checkout pullRequest$var
    git rebase pulls-$BRANCH
    git checkout pulls-$BRANCH
    git merge pullRequest$var
    git branch -D pullRequest$var
  done
}

When merging to master, I just do pull 123 234 345 from within the clone of the repo in question. If everything rebases properly, the results are in a new branch called pulls-master. I then git reset --hard master-ignore to that and force push.

When the pull requests are targetting another branch, say 3.0.x pass in the branch name first pull 3.0.x 234 345 567. This ends up in a new pulls-3.0.x branch.

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