completion: complete remote branches with switch --track
PATCH 1/1 completion: complete remote branches with switch --track
From: Jacob Keller <[email protected]>
If the --track option is supplied to git switch, then a new branch will be created tracking the specified remote branch.
Fix git completion support so that remote branches will be completed when --track is enabled.
Add a couple of simple test cases to help cover this new behavior. Note that ideally completion for --track would only allow remote branches, and would not complete all refs like HEAD, FETCH_HEAD, etc, so one of the new tests is a test_expect_failure to capture this.
---
I wasn't able to figure out how to get completion to ignore things like tags and similar, but I think this is still an improvement.
contrib/completion/git-completion.bash | 8 +++++--- t/t9902-completion.sh | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-)
contrib/completion/git-completion.bash +5 -3
@@ -2235,12 +2235,14 @@ _git_switch () if [ -n "$(__git_find_on_cmdline "--guess")" ]; then track_opt='--track' fi - if [ -z "$(__git_find_on_cmdline "-d --detach")" ]; then - only_local_ref=y - else + if [ -n "$(__git_find_on_cmdline "-d --detach")" ]; then # --guess --detach is invalid combination, no # dwim will be done when --detach is specified track_opt= + elif [ -z "$(__git_find_on_cmdline "--track")" ]; then + # if neither --detach or --track are specified then + # match only local refs. + only_local_ref=y fi if [ $only_local_ref = y -a -z "$track_opt" ]; then __gitcomp_direct "$(__git_heads "" "$cur" " ")"
t/t9902-completion.sh +22 -0
@@ -1760,6 +1760,28 @@ do ' done +test_expect_success 'git switch - default local branches only' ' + test_completion "git switch m" <<-\EOF + master Z + master-in-other Z + mybranch Z + mytag Z + EOF +' + +test_expect_failure 'git switch - --track remote branches' ' + test_completion "git switch --track " <<-\EOF + other/branch-in-other Z + other/master-in-other Z + EOF +' + +test_expect_success 'git switch - --track remote branches partial completion' ' + test_completion "git switch --track other/master-in" <<-\EOF + other/master-in-other Z + EOF +' + test_expect_success 'git config - section' ' test_completion "git config br" <<-\EOF branch.Z
Signature · 1 line
2.25.2
Hi,
Quoted 13 lines · Jacob Keller
If the --track option is supplied to git switch, then a new branch will be created tracking the specified remote branch.
Fix git completion support so that remote branches will be completed when --track is enabled.
Add a couple of simple test cases to help cover this new behavior. Note that ideally completion for --track would only allow remote branches, and would not complete all refs like HEAD,
FETCH_HEAD, etc, so one of the new tests is atest_expect_failureto capture this.Fixes ae36fe694180 ("completion: support switch")Signed-off-by Jacob Keller <[email protected]>
Quoted 7 lines
---
I wasn't able to figure out how to get completion to ignore things like tags and similar, but I think this is still an improvement.contrib/completion/git-completion.bash | 8 +++++--- t/t9902-completion.sh | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-)
Thanks for writing it.
One part I found a little confusing is that --track is being used in two ways. On one hand, it's an option to __git_complete_refs, meaning to complete remote-tracking branches. On the other hand, it's an option to git switch, meaning to create a branch set up to "git pull" from a remote-tracking branch.
Can the commit message give a motivating example to describe what improvement to the user's life this change brings? ("So now you can type 'git ... ' and hit TAB and see ....)
Some nitpicks:
[...]
Quoted 5 lines
contrib/completion/git-completion.bash +0
-0@@ -2235,12 +2235,14 @@ _git_switch () if [ -n "$(__git_find_on_cmdline "--guess")" ]; then track_opt='--track'
contrib/completion/git-completion.bash _git_switch () · line 2243
Quoted 10 lines
contrib/completion/git-completion.bash +3
-3fi - if [ -z "$(__git_find_on_cmdline "-d --detach")" ]; then - only_local_ref=y - else + if [ -n "$(__git_find_on_cmdline "-d --detach")" ]; then # --guess --detach is invalid combination, no # dwim will be done when --detach is specified track_opt= + elif [ -z "$(__git_find_on_cmdline "--track")" ]; then + # if neither --detach or --track are specified then
language nits:
- s/or/nor/ (because the clause starts with "neither")
- s/are/is/ (because "either" and "neither" are singular)
English can be odd.
contrib/completion/git-completion.bash _git_switch () · line 2246
Quoted 3 lines
contrib/completion/git-completion.bash +2
-0+ # match only local refs. + only_local_ref=y fi
Let me check that I understand correctly:
If --detach is passed, the <start-point> parameter is an arbitrary commit. So we want all refs (or even all commits), not just commits that are eligible for "git switch --guess" (the default mode) dwimery.
If --track is passed, the <start-point> parameter should be an arbitrary remote-tracking branch, not just a remote-tracking branch without corresponding local branch that would be eligible for --guess. A few lines up we handle this by setting track_opt to empty.
If neither --detach nor --track is passed, then...
... I'm not sure I understand the neither --detach nor --track passed case. Wouldn't this be --guess mode, where "$track_opt" is set, so the value of "$only_local_ref" isn't used? Or is this about the case where (1) --detach is not passed, (2) --track is not passed, and (3) --no-guess or GIT_COMPLETION_CHECKOUT_NO_GUESS is passed?
Yes, it must be about that case. In that case, only_local_ref is right.
In any case, this is getting difficult to understand, so I wonder if some refactoring is in order.
[...]
t/t9902-completion.sh do · line 1763
Quoted 7 lines
t/t9902-completion.sh +1
-0@@ -1760,6 +1760,28 @@ do ' done +test_expect_success 'git switch - default local branches only' '
nit: "default to local branches only" or "the default is local branches only". In other words, this should be a sentence so the reader can understand what property we're testing for.
Quoted 4 lines
t/t9902-completion.sh +4
-0+ test_completion "git switch m" <<-\EOF + master Z + master-in-other Z + mybranch Z
t/t9902-completion.sh do · line 1777
Quoted 10 lines
t/t9902-completion.sh +10
-0+ mytag Z + EOF +' + +test_expect_failure 'git switch - --track remote branches' ' + test_completion "git switch --track " <<-\EOF + other/branch-in-other Z + other/master-in-other Z + EOF +'
Can this have a short comment describing the issue? If over time the behavior changes, we wouldn't have an easy place to see what the behavior was at the time this test was added.
t/t9902-completion.sh do · line 1779
Quoted 2 lines
t/t9902-completion.sh +2
-0+ +test_expect_success 'git switch - --track remote branches partial completion' '
"git switch --track: partially typed remote-tracking branch is completed"
t/t9902-completion.sh do · line 1787
Quoted 8 lines
t/t9902-completion.sh +5
-0+ test_completion "git switch --track other/master-in" <<-\EOF + other/master-in-other Z + EOF +' + test_expect_success 'git config - section' ' test_completion "git config br" <<-\EOF branch.Z
Thanks and hope that helps,
Jonathan
Quoted 26 lines · Jonathan Nieder
Hi,
Quoted 13 lines · Jacob Keller
If the --track option is supplied to git switch, then a new branch will be created tracking the specified remote branch.
Fix git completion support so that remote branches will be completed when --track is enabled.
Add a couple of simple test cases to help cover this new behavior. Note that ideally completion for --track would only allow remote branches, and would not complete all refs like HEAD,
FETCH_HEAD, etc, so one of the new tests is atest_expect_failureto capture this.Fixes ae36fe694180 ("completion: support switch")Signed-off-by Jacob Keller <[email protected]>Quoted 7 lines
---
I wasn't able to figure out how to get completion to ignore things like tags and similar, but I think this is still an improvement.contrib/completion/git-completion.bash | 8 +++++--- t/t9902-completion.sh | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-)
Quoted 8 lines
Thanks for writing it.
One part I found a little confusing is that --track is being used in two ways. On one hand, it's an option to
__git_complete_refs, meaning to complete remote-tracking branches. On the other hand, it's an option to git switch, meaning to create a branch set up to "git pull" from a remote-tracking branch.
Sure, I might actually just go and write a patch to switch --track to --dwim and allow --track only for backwards compatibility with external completions.
Quoted 25 lines
Can the commit message give a motivating example to describe what improvement to the user's life this change brings? ("So now you can type 'git ... ' and hit TAB and see ....)
Some nitpicks:
[...]
Quoted 5 lines
contrib/completion/git-completion.bash +0
-0@@ -2235,12 +2235,14 @@ _git_switch () if [ -n "$(__git_find_on_cmdline "--guess")" ]; then track_opt='--track'contrib/completion/git-completion.bash _git_switch () · line 2243
Quoted 10 lines
contrib/completion/git-completion.bash +3
-3fi - if [ -z "$(__git_find_on_cmdline "-d --detach")" ]; then - only_local_ref=y - else + if [ -n "$(__git_find_on_cmdline "-d --detach")" ]; then # --guess --detach is invalid combination, no # dwim will be done when --detach is specified track_opt= + elif [ -z "$(__git_find_on_cmdline "--track")" ]; then + # if neither --detach or --track are specified thenlanguage nits:
Quoted 5 lines
- s/or/nor/ (because the clause starts with "neither")
- s/are/is/ (because "either" and "neither" are singular)English can be odd.
Sure. It's easy to get this wrong, can fix.
Quoted 10 lines
contrib/completion/git-completion.bash _git_switch () · line 2246
Quoted 3 lines
contrib/completion/git-completion.bash +2
-0+ # match only local refs. + only_local_ref=y fiLet me check that I understand correctly:
If --detach is passed, the <start-point> parameter is an arbitrary commit. So we want all refs (or even all commits), not just commits that are eligible for "git switch --guess" (the default mode) dwimery.
Yes.
Quoted 13 lines
If --track is passed, the <start-point> parameter should be an arbitrary remote-tracking branch, not just a remote-tracking branch without corresponding local branch that would be eligible for --guess. A few lines up we handle this by setting
track_optto empty.If neither --detach nor --track is passed, then..
... I'm not sure I understand the neither --detach nor --track passed case. Wouldn't this be --guess mode, where "$track_opt" is set, so the value of "$only_local_ref" isn't used? Or is this about the case where (1) --detach is not passed, (2) --track is not passed, and (3) --no-guess or
GIT_COMPLETION_CHECKOUT_NO_GUESSis passed?
Quoted 6 lines
Yes, it must be about that case. In that case,
only_local_refis right.In any case, this is getting difficult to understand, so I wonder if some refactoring is in order.
I think so. There's also room for improvement, because currently with this patch:
git switch --track <TAB>
completes all refs including stuff like FETCH_HEAD, etc.
I think the most obvious thing would be to complete remote branches only. We could complete more points if both track and -c are present, indicating the user wants to track with a known name. (Since --track's automatic naming is based on extracting the origin.
I don't think the current functions we use can quite handle what we want so maybe it's time I try to dig into the gitref code to see if we can extract the right refs with new options..
Quoted 7 lines
[...]
Quoted 6 lines
t/t9902-completion.sh +0
-0@@ -1760,6 +1760,28 @@ do ' done
Quoted 6 lines
t/t9902-completion.sh do · line 1763
Quoted 1 line
t/t9902-completion.sh +1
-0+test_expect_success 'git switch - default local branches only' 'nit: "default to local branches only" or "the default is local branches only". In other words, this should be a sentence so the reader can understand what property we're testing for.
Yea makes sense.
Quoted 10 lines
Quoted 10 lines
t/t9902-completion.sh +10
-0+ test_completion "git switch m" <<-\EOF + master Z + master-in-other Z + mybranch Z + mytag Z + EOF +' + +test_expect_failure 'git switch - --track remote branches' ' + test_completion "git switch --track " <<-\EOF
Quoted 9 lines
t/t9902-completion.sh do · line 1777
Quoted 4 lines
t/t9902-completion.sh +4
-0+ other/branch-in-other Z + other/master-in-other Z + EOF +'Can this have a short comment describing the issue? If over time the behavior changes, we wouldn't have an easy place to see what the behavior was at the time this test was added.
Yes, I'll add a sentence.
Quoted 14 lines
t/t9902-completion.sh do · line 1779
Quoted 2 lines
t/t9902-completion.sh +2
-0+ +test_expect_success 'git switch - --track remote branches partial completion' '"git switch --track: partially typed remote-tracking branch is completed"
t/t9902-completion.sh do · line 1787
Quoted 8 lines
t/t9902-completion.sh +5
-0+ test_completion "git switch --track other/master-in" <<-\EOF + other/master-in-other Z + EOF +' + test_expect_success 'git config - section' ' test_completion "git config br" <<-\EOF branch.Z
Quoted 2 lines
Thanks and hope that helps,
Jonathan
Thanks. I greatly appreciate the review!
Regards,
Jake
Quoted 3 lines · Jonathan Nieder
Let me check that I understand correctly:
I do not believe your understanding here is correct. This is about the case where you try to complete
git switch --track <TAB>
I expect this to complete to the list of all remote branches, and *not* to include local branches that already exist.
Today, on git version 2.25.2,
git switch --track <TAB> completes only to local branches.
git switch <TAB> appears to complete to basically any reference.
Quoted 4 lines
If --detach is passed, the <start-point> parameter is an arbitrary commit. So we want all refs (or even all commits), not just commits that are eligible for "git switch --guess" (the default mode) dwimery.
Yep, I agree here.
Quoted 5 lines
If --track is passed, the <start-point> parameter should be an arbitrary remote-tracking branch, not just a remote-tracking branch without corresponding local branch that would be eligible for --guess. A few lines up we handle this by setting
track_optto empty.
Sort of. Yes, we do disable "track_opt", but, because --detach is *not* set, we also set only_local_ref=y.
In this case, when only_local_ref=y, and when track_opt is not set, we trigger the call to __git_heads instead of __git_complete_refs.
I *think* the problem is that we actually got something inverted in a sense:
We probably do want git switch to complete only local branches and dwims, not arbitrary references.
Quoted 11 lines
If neither --detach nor --track is passed, then...
... I'm not sure I understand the neither --detach nor --track passed case. Wouldn't this be --guess mode, where "$track_opt" is set, so the value of "$only_local_ref" isn't used? Or is this about the case where (1) --detach is not passed, (2) --track is not passed, and (3) --no-guess or
GIT_COMPLETION_CHECKOUT_NO_GUESSis passed?Yes, it must be about that case. In that case,
only_local_refis right.
that seems to behave properly, i.e.
git switch --no-guess <TAB> will complete only local branches.
Quoted 3 lines
In any case, this is getting difficult to understand, so I wonder if some refactoring is in order.
The trouble here is that we accidentally trigger that behavior if you do
git switch --track
because we set track_opt to none, *and* we set only_local_refs to y, resulting in just calling git_heads.
I think the trouble is that __git_complete_refs prints far more than we want, so it needs some new options to avoid printing certain settings.
I *think* the problem is that --dwim lists *both* local refs *and* unique branches from remotes, and because it lists local references, we end up displaying a bunch of unnecessary refs like tags and such.
Hmm.
Thanks,
Jake