Last active
October 8, 2019 19:00
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
} |
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
Version for fish or bash https://gist.github.com/MaZderMind/1c79bf1be1a5bf92c3a10608e8710c3c