Skip to content

Instantly share code, notes, and snippets.

@colemanw
Last active October 14, 2016 13:10
Show Gist options
  • Save colemanw/01dae2a2937f6f39c2300b701636b4a8 to your computer and use it in GitHub Desktop.
Save colemanw/01dae2a2937f6f39c2300b701636b4a8 to your computer and use it in GitHub Desktop.
GitPR script

GitPR script

Installation

Change the FIXME on line 38 to the name of your remote, then put the script in your /usr/local/sbin.

Basic Usage

  1. Get onto a clean branch (e.g. master, or 4.6, or 4.7.15-rc)

  2. Make some changes to files. When done, type:

     gitpr "CRM-12345 - Fix foo and bar"
    

The script will automatically:

  1. Run civilint and abort if there are style errors
  2. Create a branch named CRM-12345
  3. Make a commit with the message "CRM-12345 - Fix foo and bar"
  4. Push to your fork and set the upstream so you can easily push more changes
  5. Open a pull request against the correct branch of civicrm

Advanced Usage

If you are working on a bigger PR and want to make several commits before submitting it, do it the normal way and then use this script at the end. For example:

  1. Do git checkout -b CRM-12345 to manually create your branch
  2. Make one or more commits
  3. When your work is complete, type gitpr "CRM-12345 - Fix foo and bar"

If there are any unstaged changes the script will commit them before submitting the PR.

Custom Branch Name

By default this script makes the first word of your commit message into the branch name. This works well for messages like gitpr "CRM-12345 - Fix foo and bar". If you want to specify a different branch name, type it before the message:

gitpr fooBar "Fix foo and bar"
#!/bin/bash
# start at top directory if we're not already there
pushd "$(git rev-parse --show-toplevel)" > /dev/null
# run style checker
civilint
if [[ $? == 1 ]]
then
echo "PR aborted"
exit 1
fi
# get current branch
branch=$(git rev-parse --abbrev-ref HEAD)
branchName="$1"
comment="$2"
# branch name is optional, we'll use the first word of the comment if ommitted
if [ "$comment" = "" ] ; then
comment="$1"
words=($comment)
branchName="${words[0]}"
fi
if [ "$branchName" = "$branch" ] ; then
# get branch name from packages. A hack but it works (for the civicrm-core repo anyway)
pushd packages > /dev/null
base=$(git rev-parse --abbrev-ref HEAD)
popd > /dev/null
else
# create new branch
git checkout -b "$branchName"
base="$branch"
fi
git commit -am "$comment"
git push --set-upstream FIXME "$branchName"
hub pull-request -b "civicrm:$base" -m "$comment"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment