Skip to content

Instantly share code, notes, and snippets.

@aclima93
Last active August 22, 2023 16:38
Show Gist options
  • Save aclima93/c685ea6ea10465c3da4db4aa27393a28 to your computer and use it in GitHub Desktop.
Save aclima93/c685ea6ea10465c3da4db4aa27393a28 to your computer and use it in GitHub Desktop.
✨ pretty print ✨ the PR title from current git branch
#! /bin/bash
# datetime
datetime=$(date +%d/%m/%Y)
# git branch name
branch_name=$(git rev-parse --abbrev-ref HEAD)
# for branches that follow the `<dir>/<ticket|NOJIRA>-<title>` naming convention and
# regex `(?<branch_dir>(\w+\/)+)?(?<ticket>(\w+-\d+)|(NOJIRA))[-//](?<title>(\w+-?)+)`
# e.g. `dev/TEST-123-The-short-title`, `dev/TEST-123/The-short-title`, `dev/NOJIRA-The-short-title`
# regex utils
d="[0-9]"
w="[a-zA-Z0-9_]"
branch_dir_regex="($w+\/)+"
ticket_regex=".*\/(($w+-$d+)|(NOJIRA))[-//].*"
title_regex=".*-$d+[-//](($w+-?)+)"
# regex matches using `sed`
branch_dir=$(sed -En "s/$branch_dir_regex/\1/p" <<< "$branch_name")
ticket=$(sed -En "s/$ticket_regex/\1/p" <<< "$branch_name")
full_title=$(sed -En "s/$title_regex/\1/p" <<< "$branch_name")
title=$(sed 's/-/ /g' <<< "$full_title") # replace all instances of '-' with ' ' in title
# output
echo "$datetime - $ticket - $title"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment