Skip to content

Instantly share code, notes, and snippets.

@scjudd
Created February 14, 2020 17:55
Show Gist options
  • Save scjudd/0173a7b19793a6f32c4b57b47d4c6175 to your computer and use it in GitHub Desktop.
Save scjudd/0173a7b19793a6f32c4b57b47d4c6175 to your computer and use it in GitHub Desktop.
git-ticket
#!/bin/bash
set -eo pipefail
refspec="+refs/tickets/*:refs/tickets/*"
function default_remote {
echo $(git config ticket.remote) || {
>&2 echo "No remote specified and no default configured."
>&2 echo "Run \`git ticket setup <REMOTE_NAME>\` to set a default remote."
exit 1
}
}
function current_branch {
git rev-parse --abbrev-ref HEAD
}
function associate {
url=$1
branch=${2:-$(current_branch)}
if [ -z "$url" ]; then
>&2 echo "Please specify a ticket URL to associate with this branch"
exit 1
fi
git hash-object -w --stdin <<< "$url" \
| xargs git update-ref refs/tickets/${branch}
echo "$branch -> $url"
}
function open {
branch=${1:-$(current_branch)}
object=$(git rev-parse refs/tickets/${branch} 2>/dev/null) || {
>&2 echo "No ticket URL has been associated with this branch!"
>&2 echo "Try a \`git ticket fetch\` and try again."
exit 1
}
url=$(git cat-file -p $object)
echo "Opening $url in your default browser"
xdg-open $url >/dev/null & disown
}
function fetch {
remote=${1:-$(default_remote)}
git fetch $remote $refspec
}
function push {
remote=${1:-$(default_remote)}
git push $remote $refspec
}
function setup {
remote=$1
if [ -z "$remote" ]; then
>&2 echo "Please specify a remote to fetch ticket refs from (e.g., 'origin')"
exit 1
fi
git config ticket.remote $remote
git config --add remote.${remote}.fetch $refspec
}
function invalid_subcommand {
>&2 echo "$1 is not a valid git-ticket subcommand."
>&2 usage
exit 1
}
function usage {
echo "Usage:"
echo " git ticket [open [BRANCH]]"
echo " git ticket setup <REMOTE>"
echo " git ticket associate <TICKET_URL> [BRANCH]"
echo " git ticket fetch [REMOTE]"
echo " git ticket push [REMOTE]"
echo " git ticket help"
}
if [ -z "$1" ]; then
subcommand=open
else
subcommand=$1
shift
fi
case $subcommand in
"open")
open $@
;;
"setup")
setup $@
;;
"associate")
associate $@
;;
"fetch")
fetch $@
;;
"push")
push $@
;;
"help")
usage
;;
*)
invalid_subcommand
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment