diff --git a/README.md b/README.md index 2d2f6de..78483eb 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ It uses the `hub` by Github program, available [here](https://hub.github.com/). - Make sure the script is executable : `chmod +x /usr/local/bin/git-pretty-pull-request` - In each of your projects, set the branches on which you want to open pull requests: `git config pretty-pull-request.pull-bases "integration preprod prod"` (or set it globally with the `--global` option) - (optional) `git pretty-pull-request` is tedious to type, I'd advise to alias it: `git config --global alias.pr pretty-pull-request`. +- (optional) In each of your projects, set the labels you want to add to your pull requests: `git config pretty-pull-request.labels.prod "Waiting for PO validation"` (or set it globally with the `--global` option) `git pr` is now an alias to `git pretty-pull-request`, which will call your git-pretty-pull-request script if it can be found in your $PATH! diff --git a/git-pretty-pull-request b/git-pretty-pull-request index 95d56f7..010d96a 100755 --- a/git-pretty-pull-request +++ b/git-pretty-pull-request @@ -48,6 +48,34 @@ function single_base() { [ ${#branches[@]} -eq 1 ] } +function set_options() { + while [ -n "$1" ]; do + case $1 in + --use-label) + useLabel=true + ;; + -m) + if [ -n "$2" ]; then + msg=$(echo "$2" | trim) + shift + else + echo 'WARN: no message found for "-m" option.' + fi + ;; + --) # End of all options. + shift + break + ;; + -?*) + printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2 + ;; + *) # Default case: No more options, so break out of the loop. + break + esac + shift + done +} + function init() { rootDir=$(git rev-parse --show-toplevel) msgFile=$rootDir/.git/PRETTY_PR_EDITMSG @@ -104,8 +132,7 @@ function should_open_editor() { function get_pull_request_message() { # If there's an argument, it's the PR message. Else, the message is the last commit message - [ -z "$1" ] && msg=$(git log -1 --pretty=%B) - [ -z "$msg" ] && msg=$1 + [ -z "$msg" ] && msg=$(git log -1 --pretty=%B) if ! should_open_editor; then echo "$msg" @@ -127,13 +154,14 @@ $template sed -e '/^;.*$/d' $msgFile # remove comments } - +useLabel=false init +set_options $@ sync_origin $head origin/$head & # in background SYNC_PID=$! # opens editor if necessary and trim -msg=$(get_pull_request_message "$1" | trim) +msg=$(get_pull_request_message) [[ "$msg" = *[![:space:]]* ]] || no_pr_message msgTitle=$(echo "$msg" | head -n 1) @@ -172,10 +200,23 @@ wait $SYNC_PID || echo -e "${RED}Error running \`git fetch\`" i=0 while [ $i -lt ${#branches[@]} ]; do if single_base; then - hub pull-request -m "$msg" -b ${branches[i]} -h $head | print_and_copy || true + echo -n "[${branches[i]}]: " + label=$(git config pretty-pull-request.labels.${branches[i]} || echo "error") + if [ "$useLabel" = true -a "$label" != "error" ];then + echo -n "{Label=$label} " + hub pull-request -m "$msg" -b ${branches[i]} -h $head -l "$label" | print_and_copy || true + else + hub pull-request -m "$msg" -b ${branches[i]} -h $head | print_and_copy || true + fi else echo -n "[${prefixes[i]}]: " - hub pull-request -m "[${prefixes[i]}] $msg" -b ${branches[i]} -h $head | print_and_copy || true + label=$(git config pretty-pull-request.labels.${branches[i]} || echo "error") + if [ "$useLabel" = true -a "$label" != "error" ];then + echo -n "{Label=$label} " + hub pull-request -m "[${prefixes[i]}] $msg" -b ${branches[i]} -h $head -l "$label" | print_and_copy || true + else + hub pull-request -m "[${prefixes[i]}] $msg" -b ${branches[i]} -h $head | print_and_copy || true + fi fi i=$[$i+1] done