Skip to content

Commit 45523e0

Browse files
newrengitster
authored andcommitted
completion: avoid user confusion in non-cone mode
It is tempting to think of "files and directories" of the current directory as valid inputs to the add and set subcommands of git sparse-checkout. However, in non-cone mode, they often aren't and using them as potential completions leads to *many* forms of confusion: Issue #1. It provides the *wrong* files and directories. For git sparse-checkout add we always want to add files and directories not currently in our sparse checkout, which means we want file and directories not currently present in the current working tree. Providing the files and directories currently present is thus always wrong. For git sparse-checkout set we have a similar problem except in the subset of cases where we are trying to narrow our checkout to a strict subset of what we already have. That is not a very common scenario, especially since it often does not even happen to be true for the first use of the command; for years we required users to create a sparse-checkout via git sparse-checkout init git sparse-checkout set <args...> (or use a clone option that did the init step for you at clone time). The init command creates a minimal sparse-checkout with just the top-level directory present, meaning the set command has to be used to expand the checkout. Thus, only in a special and perhaps unusual cases would any of the suggestions from normal file and directory completion be appropriate. Issue #2: Suggesting patterns that lead to warnings is unfriendly. If the user specifies any regular file and omits the leading '/', then the sparse-checkout command will warn the user that their command is problematic and suggest they use a leading slash instead. Issue #3: Completion gets confused by leading '/', and provides wrong paths. Users often want to anchor their patterns to the toplevel of the repository, especially when listing individual files. There are a number of reasons for this, but notably even sparse-checkout encourages them to do so (as noted above). However, if users do so (via adding a leading '/' to their pattern), then bash completion will interpret the leading slash not as a request for a path at the toplevel of the repository, but as a request for a path at the root of the filesytem. That means at best that completion cannot help with such paths, and if it does find any completions, they are almost guaranteed to be wrong. Issue #4: Suggesting invalid patterns from subdirectories is unfriendly. There is no per-directory equivalent to .gitignore with sparse-checkouts. There is only a single worktree-global $GIT_DIR/info/sparse-checkout file. As such, paths to files must be specified relative to the toplevel of a repository. Providing suggestions of paths that are relative to the current working directory, as bash completion defaults to, is wrong when the current working directory is not the worktree toplevel directory. Issue #5: Paths with special characters will be interpreted incorrectly The entries in the sparse-checkout file are patterns, not paths. While most paths also qualify as patterns (though even in such cases it would be better for users to not use them directly but prefix them with a leading '/'), there are a variety of special characters that would need special escaping beyond the normal shell escaping: '*', '?', '\', '[', ']', and any leading '#' or '!'. If completion suggests any such paths, users will likely expect them to be treated as an exact path rather than as a pattern that might match some number of files other than 1. Because of the combination of the above issues, turn completion off for the `set` and `add` subcommands of `sparse-checkout` when in non-cone mode, but leave a NEEDSWORK comment specifying what could theoretically be done if someone wanted to provide completion rules that were more helpful than harmful. Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4b41437 commit 45523e0

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

contrib/completion/git-completion.bash

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3138,6 +3138,68 @@ _git_sparse_checkout ()
31383138
fi
31393139
if [[ "$using_cone" == "true" ]]; then
31403140
__gitcomp_directories
3141+
else
3142+
# NEEDSWORK: It might be useful to provide a
3143+
# completion function which:
3144+
#
3145+
# 1. Provides completions based on
3146+
# files/directories that exist in HEAD (or in
3147+
# the index since sparse-index isn't possible
3148+
# in non-cone mode), not just those currently
3149+
# present in the working tree. Bash's
3150+
# default file and directory completion is
3151+
# totally useless for "git sparse-checkout
3152+
# add" because of this. It is likewise
3153+
# problematic for "git sparse-checkout set"
3154+
# except in those subset of cases when trying
3155+
# to narrow scope to a strict subset of what
3156+
# you already have checked out.
3157+
#
3158+
# 2. Always provides file/directory completions
3159+
# with a prepended leading '/', so that
3160+
# files/directories are only searched at the
3161+
# relevant level rather than throughout all
3162+
# trees in the hierarchy. Doing this also
3163+
# avoids suggesting the user run a
3164+
# sparse-checkout command that will result in
3165+
# a warning be thrown at the user.
3166+
#
3167+
# 3. Does not accidentally search the root of
3168+
# the filesystem when a path with a leading
3169+
# slash is specified. ("git sparse-checkout
3170+
# add /ho<TAB>" should not complete to
3171+
# "/home" but to e.g. "/hooks" if there is a
3172+
# "hooks" in the top of the repository.)
3173+
#
3174+
# 4. Provides no completions when run from a
3175+
# subdirectory of the repository root. (If we
3176+
# did provide file/directory completions, the
3177+
# user would just get a "please run from the
3178+
# toplevel directory" error message when they
3179+
# ran it. *Further*, if the user did rerun
3180+
# the command from the toplevel, the
3181+
# completions we previously provided would
3182+
# likely be wrong as they'd be relative to the
3183+
# subdirectory rather than the repository
3184+
# root. That could lead to users getting a
3185+
# nasty surprise based on trying to use a
3186+
# command we helped them create.)
3187+
#
3188+
# 5. Provides escaped completions for any paths
3189+
# containing a '*', '?', '\', '[', ']', or
3190+
# leading '#' or '!'. (These characters might
3191+
# already be escaped to protect from the
3192+
# shell, but they need an *extra* layer of
3193+
# escaping to prevent the pattern parsing in
3194+
# Git from seeing them as special characters.)
3195+
#
3196+
# Of course, this would be a lot of work, so for now,
3197+
# just avoid the many forms of user confusion that
3198+
# could be caused by providing bad completions by
3199+
# providing a fake completion to avoid falling back to
3200+
# bash's normal file and directory completion.
3201+
3202+
COMPREPLY=( "" )
31413203
fi
31423204
esac
31433205
}

0 commit comments

Comments
 (0)