Skip to content

Instantly share code, notes, and snippets.

@dedeibel
Last active October 8, 2019 19:00
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 dedeibel/a07cde2f2c8de7a722092f7b54039026 to your computer and use it in GitHub Desktop.
Save dedeibel/a07cde2f2c8de7a722092f7b54039026 to your computer and use it in GitHub Desktop.
zsh widget that prepares a git or svn commit command with message containing the jira ticket number if applicable
# save as ~/.zsh/function/vcs_commit
#
# In your ~/.zshrc
#
# fpath=(~/.zsh/function $fpath)
# autoload vcs_commit
# zle -N vcs_commit
# bindkey '\ec' vcs_commit
#
# Loads the widget and also binds it to Alt+c
#
# Use:
# Assuming you are in a git repository of a bitbucket / jira branch using the atlassian jira git flow conventions
# example "feature/EXP-1337-cool-new-feature
#
# > Alt+x
# will open the widget prompt
# execute: _
# > Type: commit ENTER
# > git commit -m 'EXP-1337: '
# resting the cursor just before the last quote
#
function git_commit_with_branch {
local ticket end
BRANCH=$(git rev-parse --abbrev-ref HEAD 2> /dev/null)
TICKET=$(echo "$BRANCH" | \
sed -r 's#^(feature|hotfix|bugfix|experiment)?/?([[:alpha:]]+-[[:digit:].]+).*$#\2#')
[ "$TICKET" = "$BRANCH" ] && TICKET=""
[[ -n $TICKET ]] && TICKET="${TICKET}: "
# zle -U does not work since it is only executed after the widget
BUFFER="git commit -m '${TICKET}'"
zle end-of-line
end=$CURSOR
CURSOR=$(($end - 1))
true
}
function vcs_commit {
# local ticket end
# BRANCH=$(git rev-parse --abbrev-ref HEAD 2> /dev/null)
# TICKET=$(echo "$BRANCH" | \
# sed -r 's#^(feature|hotfix|bugfix|experiment)?/?([[:alpha:]]+-[[:digit:].]+).*$#\2#')
# [ "$TICKET" = "$BRANCH" ] && TICKET=""
# [[ -n $TICKET ]] && TICKET="${TICKET}: "
# zle -U does not work since it is only executed after the widget
if (svn info 2> /dev/null > /dev/null); then
BUFFER="svn commit -m 'BP: '"
else
BUFFER="git commit -m 'BP: '"
fi
zle end-of-line
end=$CURSOR
CURSOR=$(($end - 1))
true
}
@dedeibel
Copy link
Author

dedeibel commented Feb 2, 2018

@dedeibel
Copy link
Author

dedeibel commented Oct 8, 2019

Changes required for mac - sed differs change directly or use variable:

 if [ "$(uname)" == 'Darwin' ];
  then
    sedr="sed -E"
  else
    sedr="sed -r"
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment