git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/3] completion: Add hook in Bash completion for commit message

From
Mikko Johannes Koivunalho <mikko.koivunalho@iki.fi>
Date
Jun 30, 2024, 21:33 UTC
Message-ID
<20240630213336.2212166-2-mikko.koivunalho@iki.fi>
In-Reply-To
<20240630213336.2212166-1-mikko.koivunalho@iki.fi>
Signed-off-by: Mikko Johannes Koivunalho <mikko.koivunalho@iki.fi>
---
 contrib/completion/git-completion.bash | 40 ++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 60a22d619a..5350531c8f 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -81,6 +81,34 @@
 #     case insensitively, even on systems with case sensitive file systems
 #     (e.g., completing tag name "FOO" on "git checkout f<TAB>").
 
+#   GIT_COMPLETION_COMMIT_MESSAGE_CALLBACK
+#
+#     Name of an executable or a script which returns a commit message.
+#     Use this, for example, to always start a commit message with a predefined
+#     prefix. Example:
+#     # Extract Jira ticket number from branch name:
+#     extract_ticket () {
+#       if [[ "$(git rev-parse --symbolic-full-name HEAD)" =~ ([[:alpha:]]{3,}-[[:digit:]]{1,}) ]]
+#       then
+#         echo "\"${BASH_REMATCH[1]^^} "
+#       fi
+#     }
+#     GIT_COMPLETION_COMMIT_MESSAGE_CALLBACK=extract_ticket
+#     # Doing commit:
+#     git commit --message=<TAB>
+#     # you would get (without the closing double quote):
+#     git commit --message="ABC-1234
+#
+# You can set the following Git configuration variables to influence the behavior of
+# the completion routines:
+#
+#   completion.commitMessageCallback
+#
+#     Name of an executable, script or shell functions which returns a commit message.
+#     See above environment variable GIT_COMPLETION_COMMIT_MESSAGE_CALLBACK.
+#     The config variable gets overriden by the environment variable if latter is present.
+#
+
 case "$COMP_WORDBREAKS" in
 *:*) : great ;;
 *)   COMP_WORDBREAKS="$COMP_WORDBREAKS:"
@@ -1824,6 +1852,18 @@ _git_commit ()
 			" "" "${cur##--cleanup=}"
 		return
 		;;
+	--message=*)
+		if test -n "${GIT_COMPLETION_COMMIT_MESSAGE_CALLBACK-}"
+		then
+			__gitcomp_direct_append "$(${GIT_COMPLETION_COMMIT_MESSAGE_CALLBACK})"
+		elif test -n "$(__git config completion.commitMessageCallback)"
+		then
+			__gitcomp_direct_append "$("$(__git config completion.commitMessageCallback)")"
+		else
+			__gitcomp_direct_append " "
+		fi
+		return
+		;;
 	--reuse-message=*|--reedit-message=*|\
 	--fixup=*|--squash=*)
 		__git_complete_refs --cur="${cur#*=}"
-- 
2.30.2
Previous: Mikko Johannes KoivunalhoNext: brian m. carlson
Message 4 of 6 in “Add a Hook To git commit --message Bash Completion”
  1. 0/3 Add a Hook To git commit --message Bash CompletionMikko Johannes Koivunalho, Jun 30, 2024
  2. 2/3 completion: Add hook in Bash completion for commit message; docsMikko Johannes Koivunalho, Jun 30, 2024
  3. 3/3 completion: Add hook in Bash completion for commit message; testsMikko Johannes Koivunalho, Jun 30, 2024
  4. 1/3 completion: Add hook in Bash completion for commit messageMikko Johannes Koivunalho, Jun 30, 2024
  5. brian m. carlsonJun 30, 2024
  6. Mikko Johannes KoivunalhoJun 30, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.