Skip to content

Commit 2b1c01d

Browse files
pcloudsgitster
authored andcommitted
parse-options: option to let --git-completion-helper show negative form
When 7fb6aef (Merge branch 'nd/parseopt-completion' - 2018-03-14) is merged, the completion for negative form is left out because the series is alread long and it could be done in a follow up series. This is it. --git-completion-helper now provides --no-xxx so that git-completion.bash can drop the extra custom --no-xxx in the script. It adds a lot more --no-xxx than what's current provided by the git-completion.bash script. We'll trim that down later. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e144d12 commit 2b1c01d

File tree

3 files changed

+46
-30
lines changed

3 files changed

+46
-30
lines changed

contrib/completion/git-completion.bash

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,7 @@ _git_am ()
11261126
return
11271127
;;
11281128
--*)
1129-
__gitcomp_builtin am "--no-utf8" \
1129+
__gitcomp_builtin am "" \
11301130
"$__git_am_inprogress_options"
11311131
return
11321132
esac
@@ -1226,9 +1226,7 @@ _git_branch ()
12261226
__git_complete_refs --cur="${cur##--set-upstream-to=}"
12271227
;;
12281228
--*)
1229-
__gitcomp_builtin branch "--no-color --no-abbrev
1230-
--no-track --no-column
1231-
"
1229+
__gitcomp_builtin branch
12321230
;;
12331231
*)
12341232
if [ $only_local_ref = "y" -a $has_r = "n" ]; then
@@ -1269,7 +1267,7 @@ _git_checkout ()
12691267
__gitcomp "diff3 merge" "" "${cur##--conflict=}"
12701268
;;
12711269
--*)
1272-
__gitcomp_builtin checkout "--no-track --no-recurse-submodules"
1270+
__gitcomp_builtin checkout
12731271
;;
12741272
*)
12751273
# check if --track, --no-track, or --no-guess was specified
@@ -1332,7 +1330,7 @@ _git_clone ()
13321330
{
13331331
case "$cur" in
13341332
--*)
1335-
__gitcomp_builtin clone "--no-single-branch"
1333+
__gitcomp_builtin clone
13361334
return
13371335
;;
13381336
esac
@@ -1365,7 +1363,7 @@ _git_commit ()
13651363
return
13661364
;;
13671365
--*)
1368-
__gitcomp_builtin commit "--no-edit --verify"
1366+
__gitcomp_builtin commit
13691367
return
13701368
esac
13711369

@@ -1468,7 +1466,7 @@ _git_fetch ()
14681466
return
14691467
;;
14701468
--*)
1471-
__gitcomp_builtin fetch "--no-tags"
1469+
__gitcomp_builtin fetch
14721470
return
14731471
;;
14741472
esac
@@ -1505,7 +1503,7 @@ _git_fsck ()
15051503
{
15061504
case "$cur" in
15071505
--*)
1508-
__gitcomp_builtin fsck "--no-reflogs"
1506+
__gitcomp_builtin fsck
15091507
return
15101508
;;
15111509
esac
@@ -1612,7 +1610,7 @@ _git_ls_files ()
16121610
{
16131611
case "$cur" in
16141612
--*)
1615-
__gitcomp_builtin ls-files "--no-empty-directory"
1613+
__gitcomp_builtin ls-files
16161614
return
16171615
;;
16181616
esac
@@ -1763,12 +1761,7 @@ _git_merge ()
17631761

17641762
case "$cur" in
17651763
--*)
1766-
__gitcomp_builtin merge "--no-rerere-autoupdate
1767-
--no-commit --no-edit --no-ff
1768-
--no-log --no-progress
1769-
--no-squash --no-stat
1770-
--no-verify-signatures
1771-
"
1764+
__gitcomp_builtin merge
17721765
return
17731766
esac
17741767
__git_complete_refs
@@ -1867,10 +1860,7 @@ _git_pull ()
18671860
return
18681861
;;
18691862
--*)
1870-
__gitcomp_builtin pull "--no-autostash --no-commit --no-edit
1871-
--no-ff --no-log --no-progress --no-rebase
1872-
--no-squash --no-stat --no-tags
1873-
--no-verify-signatures"
1863+
__gitcomp_builtin pull
18741864

18751865
return
18761866
;;
@@ -2061,7 +2051,7 @@ _git_status ()
20612051
return
20622052
;;
20632053
--*)
2064-
__gitcomp_builtin status "--no-column"
2054+
__gitcomp_builtin status
20652055
return
20662056
;;
20672057
esac
@@ -2615,7 +2605,7 @@ _git_remote ()
26152605

26162606
case "$subcommand,$cur" in
26172607
add,--*)
2618-
__gitcomp_builtin remote_add "--no-tags"
2608+
__gitcomp_builtin remote_add
26192609
;;
26202610
add,*)
26212611
;;
@@ -2695,7 +2685,7 @@ _git_revert ()
26952685
fi
26962686
case "$cur" in
26972687
--*)
2698-
__gitcomp_builtin revert "--no-edit" \
2688+
__gitcomp_builtin revert "" \
26992689
"$__git_revert_inprogress_options"
27002690
return
27012691
;;
@@ -2765,7 +2755,7 @@ _git_show_branch ()
27652755
{
27662756
case "$cur" in
27672757
--*)
2768-
__gitcomp_builtin show-branch "--no-color"
2758+
__gitcomp_builtin show-branch
27692759
return
27702760
;;
27712761
esac

parse-options.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -427,15 +427,12 @@ void parse_options_start(struct parse_opt_ctx_t *ctx,
427427
parse_options_check(options);
428428
}
429429

430-
/*
431-
* TODO: we are not completing the --no-XXX form yet because there are
432-
* many options that do not suppress it properly.
433-
*/
434430
static int show_gitcomp(struct parse_opt_ctx_t *ctx,
435431
const struct option *opts)
436432
{
437433
for (; opts->type != OPTION_END; opts++) {
438434
const char *suffix = "";
435+
int has_unset_form = 0;
439436

440437
if (!opts->long_name)
441438
continue;
@@ -450,6 +447,8 @@ static int show_gitcomp(struct parse_opt_ctx_t *ctx,
450447
case OPTION_INTEGER:
451448
case OPTION_MAGNITUDE:
452449
case OPTION_CALLBACK:
450+
has_unset_form = 1;
451+
453452
if (opts->flags & PARSE_OPT_NOARG)
454453
break;
455454
if (opts->flags & PARSE_OPT_OPTARG)
@@ -458,12 +457,27 @@ static int show_gitcomp(struct parse_opt_ctx_t *ctx,
458457
break;
459458
suffix = "=";
460459
break;
460+
case OPTION_BIT:
461+
case OPTION_NEGBIT:
462+
case OPTION_COUNTUP:
463+
case OPTION_SET_INT:
464+
has_unset_form = 1;
465+
break;
461466
default:
462467
break;
463468
}
464469
if (opts->flags & PARSE_OPT_COMP_ARG)
465470
suffix = "=";
466471
printf(" --%s%s", opts->long_name, suffix);
472+
473+
if (has_unset_form && !(opts->flags & PARSE_OPT_NONEG)) {
474+
const char *name;
475+
476+
if (skip_prefix(opts->long_name, "no-", &name))
477+
printf(" --%s", name);
478+
else
479+
printf(" --no-%s", opts->long_name);
480+
}
467481
}
468482
fputc('\n', stdout);
469483
exit(0);

t/t9902-completion.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,20 +1237,31 @@ test_expect_success 'double dash "git" itself' '
12371237
test_expect_success 'double dash "git checkout"' '
12381238
test_completion "git checkout --" <<-\EOF
12391239
--quiet Z
1240+
--no-quiet Z
12401241
--detach Z
1242+
--no-detach Z
12411243
--track Z
1244+
--no-track Z
12421245
--orphan=Z
1246+
--no-orphan Z
12431247
--ours Z
1248+
--no-ours Z
12441249
--theirs Z
1250+
--no-theirs Z
12451251
--merge Z
1252+
--no-merge Z
12461253
--conflict=Z
1254+
--no-conflict Z
12471255
--patch Z
1256+
--no-patch Z
12481257
--ignore-skip-worktree-bits Z
1258+
--no-ignore-skip-worktree-bits Z
12491259
--ignore-other-worktrees Z
1260+
--no-ignore-other-worktrees Z
12501261
--recurse-submodules Z
1251-
--progress Z
1252-
--no-track Z
12531262
--no-recurse-submodules Z
1263+
--progress Z
1264+
--no-progress Z
12541265
EOF
12551266
'
12561267

@@ -1457,6 +1468,7 @@ test_expect_success 'completion used <cmd> completion for alias: !f() { : git <c
14571468
test_expect_success 'completion without explicit _git_xxx function' '
14581469
test_completion "git version --" <<-\EOF
14591470
--build-options Z
1471+
--no-build-options Z
14601472
EOF
14611473
'
14621474

0 commit comments

Comments
 (0)