Skip to content

Instantly share code, notes, and snippets.

@erquhart
Last active November 7, 2016 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erquhart/9087c6b6a3ee43748ef26f9cf30677b3 to your computer and use it in GitHub Desktop.
Save erquhart/9087c6b6a3ee43748ef26f9cf30677b3 to your computer and use it in GitHub Desktop.
#!/bin/bash
# terminate script on first nonzero exit code
set -e
# color vars
# http://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
LIGHT_GRAY='\033[0;37m'
LIGHT_CYAN='\033[1;36m'
DEFAULT_COLOR=$LIGHT_GRAY
NOTIFICATION_COLOR=$LIGHT_CYAN
DC=$DEFAULT_COLOR
NC=$NOTIFICATION_COLOR
# utilities
alias () {
erq $1 ${@:3}
}
notify () {
printf "\n${NC}$1${DC}\n"
}
getHash () {
local format="%H"
if [ "$1" == "--short" ]; then
format="%h"
elif [ $1 ]; then
target=$1
fi
git log $target -1 --format="$format"
}
getBranchName () {
git rev-parse --abbrev-ref HEAD
}
getRepoName () {
echo -n ${PWD##*/}
}
ensureRepo () {
if [ ! -d ".git" ]; then
notify "erq cherrylog: Your working directory must be the root of a git repository."
exit 1
fi
}
# commands
# no command
if [ -z $1 ]; then
cat << ENDDOCS
erq v1.0.0
commands:
refresh - remove and reinstall npm/bower packages, uses rimraf
ENDDOCS
exit
# alias commands
elif [ $1 == 'j' ]; then
alias "jira" $@
elif [ $1 == 'b' ]; then
alias "backport" $@
elif [ $1 == 'bp' ]; then
alias "backport-prep" $@
# refresh command
elif [ $1 == 'refresh' ]; then
notify "Removing node packages..."
rimraf node_modules
notify "Removing bower packages..."
rimraf bower_components
notify "Installing npm packages..."
npm install
notify "Installing bower packages..."
bower install
notify "Refresh complete."
exit
# rebase command
elif [ $1 == 'rebase' ]; then
if [ -z $2 ]; then
notify "Please specify a branch to rebase onto."
exit
fi
notify "Checking out $2..."
git checkout $2
notify "Getting updates for $2..."
git pull
notify "Checking out last branch..."
git checkout -
notify "Starting interactive rebase onto $2..."
git rebase -i $2
notify "Rebase complete."
exit
# amend command
elif [ $1 == 'amend' ]; then
notify "Staging all changes..."
git add -A
notify "Amending previous commit..."
git commit --amend --no-edit
notify "Amend complete."
exit
# npmrc switcher
elif [ $1 == 'config' ]; then
if [ -z $2 ]; then
notify "Please specify a configuration profile."
exit 1
fi
if [ $2 == 'a' ]; then
configEmail="example@example.com"
elif [ $2 == 'b' ]; then
configEmail="example2@example.com"
else
notify "$2 is not a recognized profile. Profiles include:\n\n a\n b"
exit 1
fi
npmrcProfile=$2
notify "Setting git user email to $configEmail"
git config --global user.email "$configEmail"
notify "Activating ~/dotfiles/.npmrc-$npmrcProfile."
ln -sF ~/dotfiles/.npmrc-$npmrcProfile ~/.npmrc
exit
# bitbucket cherry-pick logger
elif [ $1 == 'cherrylog' ]; then
ensureRepo
if [ $2 ] && [ $2 != '-s' ]; then
hash=$(getHash $2)
else
hash=$(getHash)
fi
repoName=${PWD##*/}
shortHash=$(getHash --short)
url="http://stash.example.net/stash/projects/PROJECTNAME/repos/$repoName/commits"
link="[$shortHash]($url/$hash)"
if [ $2 ] && [ $2 == '-s' ]; then
message=$link
else
message="Backported to $(getBranchName) in $link."
fi
echo -n "$message" | pbcopy
notify "Markdown commit link copied."
git log -1 $hash
exit
# backport prep
elif [ $1 == 'backport-prep' ]; then
if [ -z $2 ]; then
target="master"
else
target=$2
fi
git pull && git checkout $target && git pull && git checkout - && git log -1 $target
exit
# backport command
elif [ $1 == 'backport' ]; then
if [ -z $2 ] || ([ $2 == '--from-merge' ] && [ -z $3 ]); then
notify "Please specify a commit reference or range."
exit 1
fi
if [ $2 == '--from-merge' ]; then
git cherry-pick $(git rev-list --reverse --no-merges $3^..$3)
else
git cherry-pick $2
fi
git push
if [ $2 == '--from-merge' ] && [ $4 ] && [ $4 == '-s' ]; then
erq cherrylog -s
elif [ $2 != '--from-merge' ] && [ $3 ] && [ $3 == '-s' ]; then
erq cherrylog -s
else
erq cherrylog
fi
exit
# add bitbucket markdown tag link to clipboard
elif [ $1 == 'taglink' ]; then
if [ ! -d ".git" ]; then
notify "erq taglink: Your working directory must be the root of a git repository."
exit 1
fi
if [ -z $2 ]; then
version=$(git describe)
else
version="$2"
fi
repoName=${PWD##*/}
url="http://stash.example.net/stash/projects/PROJECTNAME/repos/$repoName/commits?until=refs%2Ftags%2F"
echo -n "Released in [$version]($url$version)." | pbcopy
notify "Markdown tag link copied."
git log -1 $version
exit
# start jenkins job
elif [ $1 == 'jenkins' ]; then
startJenkinsJob () {
notify "Triggering build of $2 on $1..."
nestor build $1 "BRANCH_SPECIFIER=$2"
}
if [ -z $2 ]; then
job=$(getRepoName)
branch=$(getBranchName)
elif [ -z $3 ]; then
job=$(getRepoName)
branch=$2
else
job=$2
branch=$3
fi
jobAltSuffix="-verify"
startJenkinsJob $job $branch || startJenkinsJob $job$jobAltSuffix $branch
exit
# jira shortcuts
elif [ $1 == 'jira' ]; then
aUrl='https://jira.example.com/'
bUrl='http://projects.example.net/jira/browse/'
if [ -z $2 ]; then
url=$aUrl
path='issues'
elif [ $2 == 'create' ]; then
url=$aUrl
path='secure/CreateIssue!default.jspa'
elif [[ $2 =~ <regex> ]]; then
url=$bUrl
path=$2
else
url=$aUrl
path="browse/PROJECTNAME-$2"
fi
open $url$path
exit
# open PR
elif [ $1 == 'pr' ]; then
ensureRepo
repoName=$(getRepoName)
branchName=$(getBranchName)
url="http://stash.example.net/stash/projects/projectname/repos/$repoName/pull-requests"
query="create&targetBranch=refs%2Fheads%2Fmaster&sourceBranch=refs%2Fheads%2F$branchName"
if [ $2 ] && [ $2 == '-a' ]; then
notify "Pushing $branchName to origin..."
git push -u origin "$branchName"
erq jenkins
fi
notify "Opening new pull request in the browser..."
open $url?$query
exit
# open a file in Stash
elif [ $1 == 'stash' ]; then
ensureRepo
repoName=$(getRepoName)
head=$(getBranchName)
url="http://stash.example.net/stash/projects/projectname/repos/$repoName/browse"
if [ $2 ]; then
url+="/$2"
fi
if [ $head == 'HEAD' ]; then
head=$(getHash)
else
ref="refs%2Fheads%2F"
fi
query="at=$ref$head"
notify "Opening $2 from $head in Stash..."
open $url?$query
exit
# get the hash from the current ref
elif [ $1 == 'hash' ]; then
notify "Copying current ref hash to clipboard..."
echo -n $(getHash) | pbcopy
notify "Done."
exit
# tick time entry
elif [ $1 == 'tick' ]; then
if [ -z $2 ] || [ -z $3 ]; then
notify "erq tick: Please provide a date in MM-DD format and number of hours."
exit 1
fi
data="{ \"date\":\"2016-$2\", \"hours\":$3, \"task_id\":<obfuscated> }"
curl -H 'Authorization: Token token=<obfuscated>' \
-H 'Content-Type: application/json' \
-H 'User-Agent: Example Company (example@example.com)' \
-d "$data" \
https://www.tickspot.com/<obfuscated>/api/v2/entries.json
exit
# open ami details in browser
elif [ $1 == 'ami' ]; then
open "http://asgard.example.net/ob/fu/sca/ted/image/show/ami-$2"
exit
# npm version without commit/tag
elif [ $1 == 'version' ]; then
if [ -z $2 ]; then
type="patch"
else
type="$2"
fi
npm version "$type" --git-tag-version false
exit
# invalid command
else
printf "erq: '$1' is not a valid command.${DC}\n"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment