miércoles, 8 de noviembre de 2017

Finally publishing commit-msg-prefix.el

It's been a few months since I released commit-msg-prefix.
Just to remember a bit what was it about, I created a gif so that we all can relate again to it.


The user story is the following:

  • In your company/organisation, you have to follow some rule of prefixing all your commits with either a keyword (Add/Remove/Fix/Hotfix/Bump/Release) or an issue number, or something like that.
  • When you are about to commit that, you know what you just did (you should!), but you can't remember which particular issue number was that.
  • Then you go to jira/trello/github-issues/younameit and look for that and insert it.  
Usually, as it's been a long running issue, you have commits with that number, in your recent commit history, so an alternative to the last bullet point is to have a quick look at git log.

Well, commit-msg-prefix makes this automatically for you, so it shows the latest commits (filtered as you want), and lets you choose one (find-as-you-type provided by helm/ivy/ido), and inserts the first word of the commit.

Obviously, most of the relevant settings are configurable, so you can have support for fancier substitutions, or use mercurial logs.

The package itself is pretty simple, but it might be useful to others, and if it is, I'm happy to share it.  

I just issued the Pull Request for it to be in melpa, so more users can give it a try and I can test it a bit further. I hope it will be accepted  and will be available in melpa soon.

In the future, if it has some adoption, it might make sense to merge it into magit, but I don't want to bother magintainers when the idea is not widely tested yet.

Happy hacking!

2 comentarios:

Anónimo dijo...

If it's of interest to anyone else...

We have issue numbers in our git branch names, so I pick them out of that automatically and prefix them to the commit message.

I originally used elisp for this:

;; Custom major mode for commits (via git-commit.el)
(define-derived-mode my-git-commit-mode text-mode "Git commit"
;; Insert a WR# prefix based on the branch name. e.g.:
;; # On branch phil/wr123456-feature-description
(save-match-data
(unless (looking-at "\\`[^#\n]")
(let ((wr "^# On branch \\(?:[^/]+/\\)?[wW][rR]#?\\([0-9]+\\)"))
(when (save-excursion (re-search-forward wr nil :noerror))
(insert (format "WR#%s - " (match-string 1)))
(open-line 1))))))

(setq git-commit-major-mode 'my-git-commit-mode)

However in the end I wrote a prepare-commit-msg git hook instead:

#!/bin/sh

# Git hook (prepare-commit-msg) to prepend the WR number to the commit
# message when the branch name includes the WR number.
#
# Author: Phil S.
# Version: 1.1

## Change Log:
# 1.1 Use GIT_COMMIT_WR_FORMAT environment variable
# 1.0 Initial version

## General information on Git prepare-commit-msg hooks:
#
# This hook is invoked by git commit right after preparing the default log
# message, and before the editor is started.
#
# It takes one to three parameters:
#
# 1. The first is the name of the file that contains the commit log message.
#
# 2. The second is the source of the commit message, and can be:
# - message: -m or -F option was given
# - template: -t option was given, or config option commit.template is set
# - merge: the commit is a merge or a .git/MERGE_MSG file exists
# - squash: .git/SQUASH_MSG file exists
# - commit: -c, -C or --amend option was given
#
# For a simple 'git commit' command with no pre-existing message source
# specified or implied, this second parameter is empty.
#
# 3. The third is a commit SHA-1 when source is 'commit'.
#
# If the exit status is non-zero, git commit will abort.
#
# The purpose of the hook is to edit the message file in place, and it is
# not suppressed by the --no-verify option. A non-zero exit means a failure
# of the hook and aborts the commit.

##############################################################################

file=$1
source=$2
hash=$3

# Only act if the user is creating/editing a new commit message.
if [ -n "${source}" ]; then
exit 0
fi

# Detect a WR number in the branch name.
wr=$(sed -n '/^# On branch / s/.*[wW][rR]#\?\([0-9]\+\).*/\1/p' "${file}")

# If a WR was found, insert it at the beginning of the file.
if [ -n "${wr}" ]; then
sed -i "1 i \
$(printf "${GIT_COMMIT_WR_FORMAT:-WR#%d - }" "${wr}")" "${file}"
fi

exit 0

Raimon Grau dijo...

Thanks for sharing that Phil!

In a past job I also could get the info from the branch name, and I wrote something to get info from the branch name. I remember using (magit-get-current-branch) to get it and then doing similar things to what you're doing.

Cheers