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

From: "Bob van der Linden via GitGitGadget" <[email-masked]>
Date: Sat, 15 Oct 2022 19:54:55 +0000

From: Bob van der Linden <[email-masked]>

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 <[email-masked]>
---
    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