From c85083919e6ed4febd08a127cf6b69eda3bf3b57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20H=E1=BB=93ng=20Qu=C3=A2n?= Date: Sun, 16 Feb 2025 20:21:30 +0700 Subject: [PATCH] Autocomplete for "git merge". (#1048) Just support most often-used options and flags, though. ![image](https://github.com/user-attachments/assets/e9d411b7-6665-4796-bd7e-f06a80d06b73) ![image](https://github.com/user-attachments/assets/780482af-0022-4c16-9ffa-bc78a041c2de) --- custom-completions/git/git-completions.nu | 38 ++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/custom-completions/git/git-completions.nu b/custom-completions/git/git-completions.nu index 6853dfe..311ecd6 100644 --- a/custom-completions/git/git-completions.nu +++ b/custom-completions/git/git-completions.nu @@ -43,6 +43,16 @@ def "nu-complete git remote branches nonlocal without prefix" [] { ^git branch --no-color -r | lines | parse -r (['^[\* ]+', $remotes_regex, '?(?P\S+)'] | flatten | str join) | get branch | uniq | where {|branch| $branch != "HEAD"} | where {|branch| $branch not-in $local_branches } } +# Yield local and remote branch names which can be passed to `git merge` +def "nu-complete git mergable sources" [] { + let current = (^git branch --show-current) + let long_current = $'origin/($current)' + let git_table = ^git branch -a --format '%(refname:lstrip=2)%09%(upstream:lstrip=2)' | lines | str trim | where { ($in != $long_current) and not ($in starts-with $"($current)\t") and not ($in ends-with 'HEAD') } | each {|v| if "\t" in $v { $v | split row "\t" -n 2 | {'n': $in.0, 'u': $in.1 } } else {'n': $v, 'u': null } } + let siblings = $git_table | where u == null and n starts-with 'origin/' | get n | str substring 7.. + let remote_branches = $git_table | filter {|r| $r.u == null and not ($r.n starts-with 'origin/') } | get n + [...($siblings | wrap value | insert description Local), ...($remote_branches | wrap value | insert description Remote)] +} + def "nu-complete git switch" [] { (nu-complete git local branches) | parse "{value}" @@ -163,6 +173,14 @@ def "nu-complete git pull rebase" [] { ["false","true","merges","interactive"] } +def "nu-complete git merge strategies" [] { + ['ort', 'octopus'] +} + +def "nu-complete git merge strategy options" [] { + ['ours', 'theirs'] +} + # Check out git branches and files export extern "git checkout" [ @@ -383,6 +401,24 @@ export extern "git rebase" [ --root # start rebase from root commit ] +# Merge from a branch +export extern "git merge" [ + # For now, to make it simple, we only complete branches (not commits) and support single-parent case. + branch?: string@"nu-complete git mergable sources" # The source branch + --edit(-e) # Edit the commit message prior to committing + --no-edit # Do not edit commit message + --no-commit(-n) # Apply changes without making any commit + --signoff # Add Signed-off-by line to the commit message + --ff # Fast-forward if possible + --continue # Continue after resolving a conflict + --abort # Abort resolving conflict and go back to original state + --quit # Forget about the current merge in progress + --strategy(-s): string@"nu-complete git merge strategies" # Merge strategy + -X: string@"nu-complete git merge strategy options" # Option for merge strategy + --verbose(-v) + --help +] + # List or change branches export extern "git branch" [ branch?: string@"nu-complete git local branches" # name of branch to operate on @@ -800,5 +836,5 @@ export extern "git grep" [ ] export extern "git" [ - command?: string@"nu-complete git subcommands" # subcommand to show help for + command?: string@"nu-complete git subcommands" # subcommands ]