Skip to content

Commit fcc8e38

Browse files
committed
custom-completions: git: Include remote branches
This commit adds an extra completion command which includes remote branches with prefixes such as `origin/main`, `upstream/feature-a` etc. We need to differentiate which command accepts which completion. Addresses nushell#406
1 parent 2024d52 commit fcc8e38

File tree

1 file changed

+56
-30
lines changed

1 file changed

+56
-30
lines changed

custom-completions/git/git-completions.nu

+56-30
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
1+
# This does not include remote branches in any form.
2+
# Accepted at:
3+
# `git checkout <branch>`
4+
# `git switch <branch>`
5+
# `git rebase --onto <branch> <branch>`
16
def "nu-complete git branches" [] {
27
^git branch | lines | each { |line| $line | str replace '\* ' "" | str trim }
38
}
49

10+
# This includes remote branches without remote prefix, which will be created
11+
# locally if switched to.
12+
# Accepted at:
13+
# `git checkout <branch>`
14+
# `git switch <branch>`
515
def "nu-complete git switchable branches" [] {
6-
let remotes_regex = (["(", ((nu-complete git remotes | each {|r| ['remotes/', $r, '/'] | str join}) | str join "|"), ")"] | str join)
7-
^git branch -a
8-
| lines
9-
| parse -r (['^[\* ]+', $remotes_regex, '?(?P<branch>\S+)'] | flatten | str join)
10-
| get branch
11-
| uniq
12-
| where {|branch| $branch != "HEAD"}
16+
let remotes_regex = (["(", ((nu-complete git remotes | each {|r| ['remotes/', $r, '/'] | str join}) | str join "|"), ")"] | str join)
17+
^git branch -a
18+
| lines
19+
| parse -r (['^[\* ]+', $remotes_regex, '?(?P<branch>\S+)'] | flatten | str join)
20+
| get branch
21+
| uniq
22+
| where {|branch| $branch != "HEAD"}
23+
}
24+
25+
# This includes remote branches with remote prefix.
26+
# Accepted at:
27+
# `git checkout <branch>`
28+
# `git rebase --onto <branch> <branch>`
29+
def "nu-complete git local and remote branches" [] {
30+
^git branch
31+
| lines
32+
| parse -r '\*?\s*(?P<branch>\S+)'
33+
| get branch
34+
| append (^git branch -r | lines | parse -r '^\*?(\s*|\s*\S* -> )(?P<branch>\S*$)' | get branch | uniq)
1335
}
1436

1537
def "nu-complete git remotes" [] {
@@ -24,37 +46,41 @@ def "nu-complete git commits" [] {
2446
^git rev-list --all --remotes --pretty=oneline | lines | parse "{value} {description}"
2547
}
2648

49+
# This includes local branches, remote branches with remote prefix and commits.
50+
# Accepted at:
51+
# `git checkout <value>`
52+
# `git rebase --onto <value> <value>`
2753
def "nu-complete git branches and commits" [] {
28-
nu-complete git switchable branches
54+
nu-complete git local and remote branches
2955
| parse "{value}"
3056
| insert description Branch
3157
| append (nu-complete git commits)
3258
}
3359

3460
# Check out git branches and files
3561
export extern "git checkout" [
36-
...targets: string@"nu-complete git switchable branches" # name of the branch or files to checkout
37-
--conflict: string # conflict style (merge or diff3)
38-
--detach(-d) # detach HEAD at named commit
39-
--force(-f) # force checkout (throw away local modifications)
40-
--guess # second guess 'git checkout <no-such-branch>' (default)
41-
--ignore-other-worktrees # do not check if another worktree is holding the given ref
42-
--ignore-skip-worktree-bits # do not limit pathspecs to sparse entries only
43-
--merge(-m) # perform a 3-way merge with the new branch
44-
--orphan: string # new unparented branch
45-
--ours(-2) # checkout our version for unmerged files
46-
--overlay # use overlay mode (default)
47-
--overwrite-ignore # update ignored files (default)
48-
--patch(-p) # select hunks interactively
49-
--pathspec-from-file: string # read pathspec from file
50-
--progress # force progress reporting
51-
--quiet(-q) # suppress progress reporting
52-
--recurse-submodules: string # control recursive updating of submodules
53-
--theirs(-3) # checkout their version for unmerged files
54-
--track(-t) # set upstream info for new branch
55-
-b: string # create and checkout a new branch
56-
-B: string # create/reset and checkout a branch
57-
-l # create reflog for new branch
62+
...targets: string@"nu-complete git local and remote branches" # name of the branch or files to checkout
63+
--conflict: string # conflict style (merge or diff3)
64+
--detach(-d) # detach HEAD at named commit
65+
--force(-f) # force checkout (throw away local modifications)
66+
--guess # second guess 'git checkout <no-such-branch>' (default)
67+
--ignore-other-worktrees # do not check if another worktree is holding the given ref
68+
--ignore-skip-worktree-bits # do not limit pathspecs to sparse entries only
69+
--merge(-m) # perform a 3-way merge with the new branch
70+
--orphan: string # new unparented branch
71+
--ours(-2) # checkout our version for unmerged files
72+
--overlay # use overlay mode (default)
73+
--overwrite-ignore # update ignored files (default)
74+
--patch(-p) # select hunks interactively
75+
--pathspec-from-file: string # read pathspec from file
76+
--progress # force progress reporting
77+
--quiet(-q) # suppress progress reporting
78+
--recurse-submodules: string # control recursive updating of submodules
79+
--theirs(-3) # checkout their version for unmerged files
80+
--track(-t) # set upstream info for new branch
81+
-b: string # create and checkout a new branch
82+
-B: string # create/reset and checkout a branch
83+
-l # create reflog for new branch
5884
]
5985

6086
# Download objects and refs from another repository

0 commit comments

Comments
 (0)