Skip to content

Instantly share code, notes, and snippets.

@andykingking
Last active August 5, 2018 23:31
Show Gist options
  • Save andykingking/6ed23cace4c005dbd60a8d095aed179a to your computer and use it in GitHub Desktop.
Save andykingking/6ed23cace4c005dbd60a8d095aed179a to your computer and use it in GitHub Desktop.
Branching workflow

cmcrc-progress

#!/usr/bin/env bash
INITIAL_BRANCH=$(git symbolic-ref --short -q HEAD)
BRANCH_CARD_REGEX='[A-Z]{3,4}-[0-9]{1,4}'
BRANCH_TYPE_REGEX='^[a-z]+'
BRANCH_NAME_REGEX='[a-z][a-z_]+$'
COMMAND="$1"
function raise() {
echo "ERROR: $1" 1>&2
git checkout "$INITIAL_BRANCH" > /dev/null 2>&1
exit 1
}
function run() {
$@ > /dev/null 2>&1
if [ $? -ne 0 ]; then
raise "Something went wrong!"
fi
}
function list_cards() {
git branch | egrep -o "$BRANCH_CARD_REGEX" | uniq -u
}
function list_names_for_card() {
git branch | egrep ".*$1.*" | egrep -o "$BRANCH_NAME_REGEX" | egrep -v "^master$" | uniq -u
}
function get_current_branch() {
CURRENT_BRANCH=$(git symbolic-ref --short -q HEAD)
echo "$CURRENT_BRANCH"
}
function assert_local_changes() {
if [ "$(git status -uno --porcelain)" != "" ]; then
raise "There are local changes - cannot proceed"
fi
}
function update_current_branch() {
assert_local_changes
run git pull
}
function change_to_master() {
if [ "$(get_current_branch)" != "master" ]; then
run git checkout master
fi
}
function get_new_branch_card() {
read -p 'Enter JIRA code: ' JIRA_CODE
if [[ "$JIRA_CODE" =~ ^[A-Z]{3,4}-[0-9]{1,4}$ ]]; then
echo "$JIRA_CODE"
else
raise "Invalid JIRA code: $JIRA_CODE"
fi
}
function get_new_branch_name() {
read -p 'Enter branch name: ' BRANCH_NAME
if [[ "$BRANCH_NAME" =~ ^[a-z][a-z_]+$ ]]; then
echo "$BRANCH_NAME"
else
raise "Invalid branch name: $BRANCH_NAME"
fi
}
function create_new_branch() {
run git checkout -b "$1"/"$2"_"$3"
}
function get_current_branch_card() {
echo "$(get_current_branch)" | egrep -o "$BRANCH_CARD_REGEX"
}
function get_current_branch_type() {
echo "$(get_current_branch)" | egrep -o "$BRANCH_TYPE_REGEX"
}
function get_current_branch_name() {
echo "$(get_current_branch)" | egrep -o "$BRANCH_NAME_REGEX"
}
function assert_on_wip_branch() {
if [ "$(get_current_branch_type)" != "wip" ]; then
raise "Not on wip branch for $(get_current_branch_card)_$(get_current_branch_name)"
fi
}
function assert_on_review_branch() {
if [ "$(get_current_branch_type)" != "review" ]; then
raise "Not on review branch for $(get_current_branch_card)_$(get_current_branch_name)"
fi
}
function assert_card_exists() {
CARD="$1"
list_cards | grep "$CARD" > /dev/null 2>&1
if [ $? -ne 0 ]; then
raise "No branch exists for $CARD"
fi
}
case "$COMMAND" in
"create")
change_to_master
update_current_branch
create_new_branch wip $(get_new_branch_card) $(get_new_branch_name)
;;
"review")
assert_on_wip_branch
create_new_branch review $(get_current_branch_card) $(get_current_branch_name)
;;
"close")
assert_on_review_branch
run git pull --rebase
run git push -u origin $(get_current_branch)
change_to_master
;;
"patch")
assert_local_changes
list_cards | sed -e 's/^/\* /'
CARD="$(get_new_branch_card)"
assert_card_exists "$CARD"
list_names_for_card "$CARD" | sed -e 's/^/\* /'
NAME="$(get_new_branch_name)"
run git checkout -b review/"$CARD"_"$NAME"
run git pull
;;
"list")
git branch | egrep -o "$BRANCH_CARD_REGEX"_"$BRANCH_NAME_REGEX"
;;
*)
raise "Unknown command: $COMMAND"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment