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

[PATCH v3] tab completion of filenames for 'git restore'

From
David Cantrell via GitGitGadget <gitgitgadget@gmail.com>
Date
Mar 15, 2022, 22:13 UTC
Message-ID
<pull.1227.v3.git.git.1647382437475.gitgitgadget@gmail.com>
In-Reply-To
<pull.1227.v2.git.git.1647305547.gitgitgadget@gmail.com>
From: David Cantrell <david@cantrell.org.uk>

If no --args are present after 'git restore' it assumes that you want to tab-complete one of the files with unstaged uncommitted changes.

If a file has been staged we don't want to list it, as restoring those requires a slightly more complex `git restore --staged`, so we only list those files that are --modified. While --committable also looks like a good candidate, that includes changes that have been staged.

Signed-off-by: David Cantrell <david@cantrell.org.uk>
---
    Improved bash tab completion for 'git restore' - adds support for
    auto-completing filenames
    
    This adds tab-completion of filenames to the bash completions for git
    restore.
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1227%2FDrHyde%2Ffilename-completion-for-git-restore-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1227/DrHyde/filename-completion-for-git-restore-v3
Pull-Request: https://github.com/git/git/pull/1227
Range-diff vs v2:
 1:  2bb8f1cb1c4 ! 1:  779744b9fc5 tab completion of filenames for 'git restore'
     @@ Commit message
          tab completion of filenames for 'git restore'
      
          If no --args are present after 'git restore' it assumes that you want
     -    to tab-complete one of the files with uncommitted changes
     +    to tab-complete one of the files with unstaged uncommitted changes.
     +
     +    If a file has been staged we don't want to list it, as restoring those
     +    requires a slightly more complex `git restore --staged`, so we only list
     +    those files that are --modified. While --committable also looks like
     +    a good candidate, that includes changes that have been staged.
      
          Signed-off-by: David Cantrell <david@cantrell.org.uk>
      
       ## contrib/completion/git-completion.bash ##
      @@ contrib/completion/git-completion.bash: _git_restore ()
     - 	case "$cur" in
     - 	--conflict=*)
     - 		__gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
     -+		return
     - 		;;
     - 	--source=*)
     - 		__git_complete_refs --cur="${cur##--source=}"
     -+		return
     - 		;;
       	--*)
       		__gitcomp_builtin restore
     -+		return
       		;;
     ++	*)
     ++		if __git rev-parse --verify --quiet HEAD >/dev/null; then
     ++			__git_complete_index_file "--modified"
     ++		fi
       	esac
     -+
     -+	if __git rev-parse --verify --quiet HEAD >/dev/null; then
     -+		__git_complete_index_file "--committable"
     -+	fi
       }
       
     - __git_revert_inprogress_options=$__git_sequencer_inprogress_options
 2:  16aa4d0b2e4 < -:  ----------- if a file has been staged we don't want to list it
 contrib/completion/git-completion.bash | 4 ++++
 1 file changed, 4 insertions(+)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 49a328aa8a4..ba5c395d2d8 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -2890,6 +2890,10 @@ _git_restore ()
 	--*)
 		__gitcomp_builtin restore
 		;;
+	*)
+		if __git rev-parse --verify --quiet HEAD >/dev/null; then
+			__git_complete_index_file "--modified"
+		fi
 	esac
 }
 

base-commit: 1a4874565fa3b6668042216189551b98b4dc0b1b
-- 
gitgitgadget
Previous: Junio C HamanoNext: Junio C Hamano
Message 11 of 13 in “tab completion of filenames for 'git restore'”
  1. tab completion of filenames for 'git restore'David Cantrell via GitGitGadget, Mar 11, 2022
  2. Junio C HamanoMar 13, 2022
  3. David CantrellMar 14, 2022
  4. Junio C HamanoMar 15, 2022
  5. 0/2 Improved bash tab completion for 'git restore' - adds support for auto-completing filenamesDavid Cantrell via GitGitGadget, Mar 15, 2022
  6. 2/2 if a file has been staged we don't want to list itDavid Cantrell via GitGitGadget, Mar 15, 2022
  7. Bagas SanjayaMar 16, 2022
  8. 1/2 tab completion of filenames for 'git restore'David Cantrell via GitGitGadget, Mar 15, 2022
  9. Junio C HamanoMar 15, 2022
  10. Junio C HamanoMar 15, 2022
  11. tab completion of filenames for 'git restore'David Cantrell via GitGitGadget, Mar 15, 2022
  12. Junio C HamanoMar 16, 2022
  13. David CantrellMar 17, 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.