@matheusbsbs / matheus-bagliano / commits / 424cce832d
Fork 0

bash: support user-supplied completion scripts for user's git commands

The bash completion script already provides support to complete aliases, options and refs for aliases (if the alias can be traced back to a supported git command by __git_aliased_command()), and the user's custom git commands, but it does not support the options of the user's custom git commands (of course; how could it know about the options of a custom git command?). Users of such custom git commands could extend git's bash completion script by writing functions to support their commands, but they might have issues with it: they might not have the rights to modify a system-wide git completion script, and they will need to track and merge upstream changes in the future. This patch addresses this by providing means for users to supply custom completion scriplets for their custom git commands without modifying the main git bash completion script. Instead of having a huge hard-coded list of command-completion function pairs (in _git()), the completion script will figure out which completion function to call based on the command's name. That is, when completing the options of 'git foo', the main completion script will check whether the function '_git_foo' is declared, and if declared, it will invoke that function to perform the completion. If such a function is not declared, it will fall back to complete file names. So, users will only need to provide this '_git_foo' completion function in a separate file, source that file, and it will be used the next time they press TAB after 'git foo '. There are two git commands (stage and whatchanged), for which the completion functions of other commands were used, therefore they got their own completion function. Signed-off-by: SZEDER Gábor <szeder@ira.uka.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Feb 23, 2010 at 22:02 UTC 424cce832d180843ab9b54eec3a7643948fc1afd
1 file changed +12 -55
contrib/completion/git-completion.bash
+12 -55
@@ -1439,6 +1439,11 @@ _git_send_email ()
1439 COMPREPLY=()
1440 }
1441
1442 +_git_stage ()
1443 +{
1444 + _git_add
1445 +}
1446 +
1447 __git_config_get_set_variables ()
1448 {
1449 local prevword word config_file= c=$COMP_CWORD
@@ -2170,6 +2175,11 @@ _git_tag ()
2175 esac
2176 }
2177
2178 +_git_whatchanged ()
2179 +{
2180 + _git_log
2181 +}
2182 +
2183 _git ()
2184 {
2185 local i c=1 command __git_dir
@@ -2209,61 +2219,8 @@ _git ()
2219 local expansion=$(__git_aliased_command "$command")
2220 [ "$expansion" ] && command="$expansion"
2221
2212 - case "$command" in
2213 - am) _git_am ;;
2214 - add) _git_add ;;
2215 - apply) _git_apply ;;
2216 - archive) _git_archive ;;
2217 - bisect) _git_bisect ;;
2218 - bundle) _git_bundle ;;
2219 - branch) _git_branch ;;
2220 - checkout) _git_checkout ;;
2221 - cherry) _git_cherry ;;
2222 - cherry-pick) _git_cherry_pick ;;
2223 - clean) _git_clean ;;
2224 - clone) _git_clone ;;
2225 - commit) _git_commit ;;
2226 - config) _git_config ;;
2227 - describe) _git_describe ;;
2228 - diff) _git_diff ;;
2229 - difftool) _git_difftool ;;
2230 - fetch) _git_fetch ;;
2231 - format-patch) _git_format_patch ;;
2232 - fsck) _git_fsck ;;
2233 - gc) _git_gc ;;
2234 - grep) _git_grep ;;
2235 - help) _git_help ;;
2236 - init) _git_init ;;
2237 - log) _git_log ;;
2238 - ls-files) _git_ls_files ;;
2239 - ls-remote) _git_ls_remote ;;
2240 - ls-tree) _git_ls_tree ;;
2241 - merge) _git_merge;;
2242 - mergetool) _git_mergetool;;
2243 - merge-base) _git_merge_base ;;
2244 - mv) _git_mv ;;
2245 - name-rev) _git_name_rev ;;
2246 - notes) _git_notes ;;
2247 - pull) _git_pull ;;
2248 - push) _git_push ;;
2249 - rebase) _git_rebase ;;
2250 - remote) _git_remote ;;
2251 - replace) _git_replace ;;
2252 - reset) _git_reset ;;
2253 - revert) _git_revert ;;
2254 - rm) _git_rm ;;
2255 - send-email) _git_send_email ;;
2256 - shortlog) _git_shortlog ;;
2257 - show) _git_show ;;
2258 - show-branch) _git_show_branch ;;
2259 - stash) _git_stash ;;
2260 - stage) _git_add ;;
2261 - submodule) _git_submodule ;;
2262 - svn) _git_svn ;;
2263 - tag) _git_tag ;;
2264 - whatchanged) _git_log ;;
2265 - *) COMPREPLY=() ;;
2266 - esac
2222 + local completion_func="_git_${command//-/_}"
2223 + declare -F $completion_func >/dev/null && $completion_func
2224 }
2225
2226 _gitk ()