mbox series

[0/3] Add a Hook To git commit --message Bash Completion

Message ID 20240630213336.2212166-1-mikko.koivunalho@iki.fi (mailing list archive)
Headers show
Series Add a Hook To git commit --message Bash Completion | expand

Message

Mikko Johannes Koivunalho June 30, 2024, 9:33 p.m. UTC
I want to create part or all of the `git commit -m *` command's
message with a script. I work for one Jira ticket at a time and I
need to create many commits which all start with "TKT-123: "
(any Jira ticket number/identifier). To make the commits faster,
I run `git commit --message="TKT-123: add new file"` on the command
line.

I want the Bash completion mechanism to propose the message for
me. I would fetch the newest ticket number and place it on the command
line when I type `git commit --message=<TAB>`.

Example:
# Doing commit:
git commit --message=<TAB>
# you would get (also without the closing double quote):
git commit --message="ABC-1234


Mikko Johannes Koivunalho (3):
  completion: Add hook in Bash completion for commit message
  completion: Add hook in Bash completion for commit message; docs
  completion: Add hook in Bash completion for commit message; tests

 Documentation/config/completion.txt    | 11 +++++
 contrib/completion/git-completion.bash | 40 +++++++++++++++
 t/t9902-completion.sh                  | 68 ++++++++++++++++++++++++++
 3 files changed, 119 insertions(+)


base-commit: 790a17fb19d6eadd16c52e5d284a5c6921744766

Comments

brian m. carlson June 30, 2024, 9:44 p.m. UTC | #1
On 2024-06-30 at 21:33:33, Mikko Johannes Koivunalho wrote:
> I want to create part or all of the `git commit -m *` command's
> message with a script. I work for one Jira ticket at a time and I
> need to create many commits which all start with "TKT-123: "
> (any Jira ticket number/identifier). To make the commits faster,
> I run `git commit --message="TKT-123: add new file"` on the command
> line.
> 
> I want the Bash completion mechanism to propose the message for
> me. I would fetch the newest ticket number and place it on the command
> line when I type `git commit --message=<TAB>`.
> 
> Example:
> # Doing commit:
> git commit --message=<TAB>
> # you would get (also without the closing double quote):
> git commit --message="ABC-1234

This is easy to do with a prepare-commit-msg or commit-msg hook, and
those are the intended tools for this purpose.  I've used these hooks to
generate a message for a ticket based on the branch name at a past
company (so a branch called tkt-123 would result in the TKT-123: entry
in the appropriate place in the commit message).

While you certainly can commit on the command line, it's not encouraged
because you're supposed to write a commit message that explains the
commit in detail.  Only very rarely is a single line commit message
useful, and even in the case you've cited, I'd want to know why you
added a new file.  What does the file do?  What problem is it supposed
to solve?  Why are we adding a new file when we could add the changes to
an existing file?  Why is this change valuable at all?

That being said, all of this is possible with a prepare-commit-msg hook,
and that's a better and more generic way than using the bash completion,
which is specific to bash.
Mikko Johannes Koivunalho June 30, 2024, 10:12 p.m. UTC | #2
On 30/06/2024 23:44, brian m. carlson wrote:
> On 2024-06-30 at 21:33:33, Mikko Johannes Koivunalho wrote:
>> I want to create part or all of the `git commit -m *` command's
>> message with a script. I work for one Jira ticket at a time and I
>> need to create many commits which all start with "TKT-123: "
>> (any Jira ticket number/identifier). To make the commits faster,
>> I run `git commit --message="TKT-123: add new file"` on the command
>> line.
>>
>> I want the Bash completion mechanism to propose the message for
>> me. I would fetch the newest ticket number and place it on the command
>> line when I type `git commit --message=<TAB>`.
>>
>> Example:
>> # Doing commit:
>> git commit --message=<TAB>
>> # you would get (also without the closing double quote):
>> git commit --message="ABC-1234
> This is easy to do with a prepare-commit-msg or commit-msg hook, and
> those are the intended tools for this purpose.  I've used these hooks to
> generate a message for a ticket based on the branch name at a past
> company (so a branch called tkt-123 would result in the TKT-123: entry
> in the appropriate place in the commit message).
>
> While you certainly can commit on the command line, it's not encouraged
> because you're supposed to write a commit message that explains the
> commit in detail.  Only very rarely is a single line commit message
> useful, and even in the case you've cited, I'd want to know why you
> added a new file.  What does the file do?  What problem is it supposed
> to solve?  Why are we adding a new file when we could add the changes to
> an existing file?  Why is this change valuable at all?
>
> That being said, all of this is possible with a prepare-commit-msg hook,
> and that's a better and more generic way than using the bash completion,
> which is specific to bash.

I have used the prepare-commit-msg hook for this same purpose, and also 
commit-msg hook to check that the commit message does contain a valid 
ticket identificator string. When doing small changes it is just faster 
and more convenient for me to use command line completion to help. The 
projects where I work often don't require longer commit messages and 
explanations so formulating the commit message in editor is not 
necessary. When longer and more meticulous commit messages are required 
I do them in the editor.

--
Mikko Koivunalho