From 8635a7899152abe3e55f9ffc9f060edec84c03a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fa=C3=AFz=20Hernawan?= <3030950+Abdillah@users.noreply.github.com> Date: Tue, 14 Nov 2023 19:25:00 +0700 Subject: [PATCH] completions/git: fix support for path relative to current directory (#666) Continuing effort on https://github.com/nushell/nu_scripts/pull/617 which only auto-completes path relative to git root. Before, ``` $ cd src/ $ git add src/main.rs Modified README.md Untracked $ git diff src/main.rs ``` Now it displays, ``` $ cd src/ $ git add main.rs Modified ../README.md Untracked $ git diff main.rs ``` --- custom-completions/git/git-completions.nu | 36 ++++++++++++++++------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/custom-completions/git/git-completions.nu b/custom-completions/git/git-completions.nu index 6180de5..f457cc0 100644 --- a/custom-completions/git/git-completions.nu +++ b/custom-completions/git/git-completions.nu @@ -88,23 +88,37 @@ def "nu-complete git tags" [] { # See `man git-status` under "Short Format" # This is incomplete, but should cover the most common cases. const short_status_descriptions = { - " D": "Deleted" - " M": "Modified" - "!!": "Ignored" - "??": "Untracked" + ".D": "Deleted" + ".M": "Modified" + "!" : "Ignored" + "?" : "Untracked" "AU": "Staged, not merged" "MD": "Some modifications staged, file deleted in work tree" "MM": "Some modifications staged, some modifications untracked" - "R ": "Renamed" + "R.": "Renamed" + "UU": "Both modified (in merge conflict)" } def "nu-complete git files" [] { - let relevant_statuses = ["??"," M", "MM", "MD", " D"] - ^git status --porcelain - | lines - | parse --regex "(?P.{2}) (?P.+)" - | where $it.short_status in $relevant_statuses - | insert "description" { |e| $short_status_descriptions | get $e.short_status} + let relevant_statuses = ["?",".M", "MM", "MD", ".D", "UU"] + ^git status -uall --porcelain=2 + | lines + | each { |$it| + if $it starts-with "1 " { + $it | parse --regex "1 (?P\\S+) (?:\\S+\\s?){6} (?P\\S+)" + } else if $it starts-with "2 " { + $it | parse --regex "2 (?P\\S+) (?:\\S+\\s?){6} (?P\\S+)" + } else if $it starts-with "u " { + $it | parse --regex "u (?P\\S+) (?:\\S+\\s?){8} (?P\\S+)" + } else if $it starts-with "? " { + $it | parse --regex "(?P.{1}) (?P.+)" + } else { + { short_status: 'unknown', value: $it } + } + } + | flatten + | where $it.short_status in $relevant_statuses + | insert "description" { |e| $short_status_descriptions | get $e.short_status} } def "nu-complete git built-in-refs" [] {