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

[PATCH] completion: support amend and reword in git commit fixup option

From
Bob van der Linden via GitGitGadget <gitgitgadget@gmail.com>
Date
Oct 15, 2022, 19:54 UTC
Message-ID
<pull.1365.git.git.1665863696303.gitgitgadget@gmail.com>
From: Bob van der Linden <bobvanderlinden@gmail.com>

The option `git commit --fixup=` has the ability to create amend and reword commits, by specifying `--fixup=amend:REF` and `--fixup=reword:REF`.

This patch allows completion of amend: and reword: prefixes when a user has already typed the --fixup= option.

In addition, this patch fixes completion of refs that can be filled in after the amend: and reword: prefixes. Previously `amend:` and `reword:` were considered part of the ref to complete, which always resulted in 0 potential completions.

Signed-off-by: Bob van der Linden <bobvanderlinden@gmail.com>
---
    completion: support amend and reword in git commit fixup option
    
    I'd like to use git commit --fixup= more in my day-to-day work. However,
    it dawned on me that the options --fixup=amend: and --fixup=reword: were
    not supported by bash completion.
    
    I attempted to solve this by first adding amend: and reword: to the
    completions of --fixup=. In addition, I made sure that --fixup=amend:ref
    does not consider amend: to be part of the reference, thus allowing
    completion of references when --fixup=amend: and --fixup=reword: are
    used.
    
    The patch also includes completion tests for --fixup=, --fixup=amend:
    and --fixup=reword:.
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1365%2Fbobvanderlinden%2Fpr-fixup-amend-reword-completion-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1365/bobvanderlinden/pr-fixup-amend-reword-completion-v1
Pull-Request: https://github.com/git/git/pull/1365
 contrib/completion/git-completion.bash | 11 +++++++-
 t/t9902-completion.sh                  | 35 ++++++++++++++++++++++++++
 2 files changed, 45 insertions(+), 1 deletion(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index ba5c395d2d8..8d1a713dc4d 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1672,8 +1672,17 @@ _git_commit ()
 			" "" "${cur##--cleanup=}"
 		return
 		;;
+	--fixup=amend:*|--fixup=reword:*)
+		__git_complete_refs --cur="${cur#*:}"
+		return
+		;;
+	--fixup=*)
+		__git_complete_refs --cur="${cur#*=}"
+		__gitcomp_direct_append "$(compgen -W "amend: reword:" "${cur#*=}")"
+		return
+		;;
 	--reuse-message=*|--reedit-message=*|\
-	--fixup=*|--squash=*)
+	--squash=*)
 		__git_complete_refs --cur="${cur#*=}"
 		return
 		;;
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index 43de868b800..bb3dcab5d9d 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -1444,6 +1444,41 @@ test_expect_success 'git checkout - with --detach, complete only references' '
 	EOF
 '
 
+test_expect_success 'git commit - with --fixup=, completes references along with amend: and reword:' '
+	test_completion "git commit --fixup=" <<-\EOF
+	HEAD Z
+	main Z
+	matching-branch Z
+	matching-tag Z
+	other/branch-in-other Z
+	other/main-in-other Z
+	amend:Z
+	reword:Z
+	EOF
+'
+
+test_expect_success 'git commit - with --fixup=amend:, completes references' '
+	test_completion "git commit --fixup=amend:" <<-\EOF
+	HEAD Z
+	main Z
+	matching-branch Z
+	matching-tag Z
+	other/branch-in-other Z
+	other/main-in-other Z
+	EOF
+'
+
+test_expect_success 'git commit - with --fixup=reword:, completes references' '
+	test_completion "git commit --fixup=reword:" <<-\EOF
+	HEAD Z
+	main Z
+	matching-branch Z
+	matching-tag Z
+	other/branch-in-other Z
+	other/main-in-other Z
+	EOF
+'
+
 test_expect_success 'setup sparse-checkout tests' '
 	# set up sparse-checkout repo
 	git init sparse-checkout &&

base-commit: 3dcec76d9df911ed8321007b1d197c1a206dc164
-- 
gitgitgadget
Message 1 of 1 in “completion: support amend and reword in git commit fixup option”
  1. completion: support amend and reword in git commit fixup optionBob van der Linden via GitGitGadget, Oct 15, 2022

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.