User:Brouberol

From Wikitech

Useful snippets

pcc

This is a bash function I have in my zshrc

# This function allows me to run `pcc` in my puppet repository checkout
# and have it automatically start a PCC run on the gerrit change ID
# associated with the patch change ID, without having me to copy and paste
# anything.
pcc () {
	if [[ $PWD != "${WMF_HOME}/puppet" ]]
	then
		echo "Not in the puppet root dir. Exiting."
	else
		change_id=$(git show | grep Change-Id | awk '{ print $2 }')
		gerrit_change_id=$(curl -s "https://gerrit.wikimedia.org/r/changes/${change_id}" | sed 1d | jq -r "._number")
		./utils/pcc ${gerrit_change_id}
	fi
}
prepare-commit-msg

I automatically inject this script in my git hooks to avoid having to manually type Bug: TXXXXXX in my commit messages. Note: it relies on the phabricator ticket number to appear in the git branch name.

#!/bin/sh

#
# Inspects branch name and checks if it contains a Phabricator ticket number (i.e. T123456).
# If yes, commit message will be automatically suffixed with "Bug: {num}".
#
# Useful for looking through git history and relating a commit or group of commits
# back to a user story.
#
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)


# Ensure BRANCH_NAME is not empty and is not in a detached HEAD state (i.e. rebase).
# SKIP_PREPARE_COMMIT_MSG may be used as an escape hatch to disable this hook,
# while still allowing other githooks to run.
if [ ! -z "$BRANCH_NAME" ] && [ "$BRANCH_NAME" != "HEAD" ] && [ "$SKIP_PREPARE_COMMIT_MSG" != 1 ]; then
  PREFIX_PATTERN='T[0-9]{6,7}'

  [[ $BRANCH_NAME =~ $PREFIX_PATTERN ]]

  PREFIX=${BASH_REMATCH[0]}

  PREFIX_IN_COMMIT=$(grep -c "Bug: $PREFIX" $1)

  # Ensure PREFIX exists in BRANCH_NAME and is not already present in the commit message
  if [[ -n "$PREFIX" ]] && ! [[ $PREFIX_IN_COMMIT -ge 1 ]]; then
    echo "\nBug: ${PREFIX}" >> $1
  fi

fi