Skip to content

Commit 8b0eaa4

Browse files
szedergitster
authored andcommitted
completion: clear cached --options when sourcing the completion script
The established way to update the completion script in an already running shell is to simply source it again: this brings in any new --options and features, and clears caching variables. E.g. it clears the variables caching the list of (all|porcelain) git commands, so when they are later lazy-initialized again, then they will list and cache any newly installed commmands as well. Unfortunately, since d401f3d (git-completion.bash: introduce __gitcomp_builtin, 2018-02-09) and subsequent patches this doesn't work for a lot of git commands' options. To eliminate a lot of hard-to-maintain hard-coded lists of options, those commits changed the completion script to use a bunch of programmatically created and lazy-initialized variables to cache the options of those builtin porcelain commands that use parse-options. These variables are not cleared upon sourcing the completion script, therefore they continue caching the old lists of options, even when some commands recently learned new options or when deprecated options were removed. Always 'unset' these variables caching the options of builtin commands when sourcing the completion script. Redirect 'unset's stderr to /dev/null, because ZSH's 'unset' complains if it's invoked without any arguments, i.e. no variables caching builtin's options are set. This can happen, if someone were to source the completion script twice without completing any --options in between. Bash stays silent in this case. Add tests to ensure that these variables are indeed cleared when the completion script is sourced; not just the variables caching options, but all other caching variables, i.e. the variables caching commands, porcelain commands and merge strategies as well. Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 27b42d0 commit 8b0eaa4

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

contrib/completion/git-completion.bash

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ __gitcomp ()
280280
esac
281281
}
282282

283+
# Clear the variables caching builtins' options when (re-)sourcing
284+
# the completion script.
285+
unset $(set |sed -ne 's/^\(__gitcomp_builtin_[a-zA-Z0-9_][a-zA-Z0-9_]*\)=.*/\1/p') 2>/dev/null
286+
283287
# This function is equivalent to
284288
#
285289
# __gitcomp "$(git xxx --git-completion-helper) ..."

t/t9902-completion.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,4 +1497,35 @@ do
14971497
'
14981498
done
14991499

1500+
test_expect_success 'sourcing the completion script clears cached commands' '
1501+
__git_compute_all_commands &&
1502+
verbose test -n "$__git_all_commands" &&
1503+
. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
1504+
verbose test -z "$__git_all_commands"
1505+
'
1506+
1507+
test_expect_success 'sourcing the completion script clears cached porcelain commands' '
1508+
__git_compute_porcelain_commands &&
1509+
verbose test -n "$__git_porcelain_commands" &&
1510+
. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
1511+
verbose test -z "$__git_porcelain_commands"
1512+
'
1513+
1514+
test_expect_success 'sourcing the completion script clears cached merge strategies' '
1515+
__git_compute_merge_strategies &&
1516+
verbose test -n "$__git_merge_strategies" &&
1517+
. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
1518+
verbose test -z "$__git_merge_strategies"
1519+
'
1520+
1521+
test_expect_success 'sourcing the completion script clears cached --options' '
1522+
__gitcomp_builtin checkout &&
1523+
verbose test -n "$__gitcomp_builtin_checkout" &&
1524+
__gitcomp_builtin notes_edit &&
1525+
verbose test -n "$__gitcomp_builtin_notes_edit" &&
1526+
. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
1527+
verbose test -z "$__gitcomp_builtin_checkout" &&
1528+
verbose test -z "$__gitcomp_builtin_notes_edit"
1529+
'
1530+
15001531
test_done

0 commit comments

Comments
 (0)