From 9d0a182a8636ad00222d4605f1be5c2846638782 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fa=C3=AFz=20Hernawan?= <3030950+Abdillah@users.noreply.github.com> Date: Sat, 23 Sep 2023 04:29:05 +0700 Subject: [PATCH] completions/git: add filename completion to diff and checkout (#617) --- custom-completions/git/git-completions.nu | 50 +++++++++++++++-------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/custom-completions/git/git-completions.nu b/custom-completions/git/git-completions.nu index 99590a81..170e0053 100644 --- a/custom-completions/git/git-completions.nu +++ b/custom-completions/git/git-completions.nu @@ -63,6 +63,7 @@ def "nu-complete git checkout" [] { | parse "{value}" | insert description "remote branch") | append (nu-complete git commits all) + | append (nu-complete git files | where description != "Untracked" | select value) } # Arguments to `git rebase --onto ` @@ -84,22 +85,6 @@ def "nu-complete git tags" [] { ^git tag | lines } -def "nu-complete git built-in-refs" [] { - [HEAD FETCH_HEAD ORIG_HEAD] -} - -def "nu-complete git refs" [] { - nu-complete git switchable branches - | parse "{value}" - | insert description Branch - | append (nu-complete git tags | parse "{value}" | insert description Tag) - | append (nu-complete git built-in-refs) -} - -def "nu-complete git subcommands" [] { - ^git help -a | lines | where $it starts-with " " | parse -r '\s*(?P[^ ]+) \s*(?P\w.*)' -} - # See `man git-status` under "Short Format" # This is incomplete, but should cover the most common cases. const short_status_descriptions = { @@ -113,7 +98,7 @@ const short_status_descriptions = { "R ": "Renamed" } -def "nu-complete git add" [] { +def "nu-complete git files" [] { let relevant_statuses = ["??"," M", "MM", "MD", " D"] ^git status --porcelain | lines @@ -122,6 +107,35 @@ def "nu-complete git add" [] { | insert "description" { |e| $short_status_descriptions | get $e.short_status} } +def "nu-complete git built-in-refs" [] { + [HEAD FETCH_HEAD ORIG_HEAD] +} + +def "nu-complete git refs" [] { + nu-complete git switchable branches + | parse "{value}" + | insert description Branch + | append (nu-complete git tags | parse "{value}" | insert description Tag) + | append (nu-complete git built-in-refs) +} + +def "nu-complete git files-or-refs" [] { + nu-complete git switchable branches + | parse "{value}" + | insert description Branch + | append (nu-complete git files | where description == "Modified" | select value) + | append (nu-complete git tags | parse "{value}" | insert description Tag) + | append (nu-complete git built-in-refs) +} + +def "nu-complete git subcommands" [] { + ^git help -a | lines | where $it starts-with " " | parse -r '\s*(?P[^ ]+) \s*(?P\w.*)' +} + +def "nu-complete git add" [] { + nu-complete git files +} + # Check out git branches and files export extern "git checkout" [ ...targets: string@"nu-complete git checkout" # name of the branch or files to checkout @@ -337,7 +351,7 @@ export extern "git remote set-url" [ # Show changes between commits, working tree etc export extern "git diff" [ - rev1?: string@"nu-complete git refs" + rev1_or_file?: string@"nu-complete git files-or-refs" rev2?: string@"nu-complete git refs" --cached # show staged changes --name-only # only show names of changed files