Skip to content

Commit 06c81a1

Browse files
devdekunlegitster
authored andcommitted
interactive -p: add new --auto-advance flag
When using the interactive add, reset, stash or checkout machinery, we do not have the option of reworking with a file when selecting hunks, because the session automatically advances to the next file or ends if we have just one file. Introduce the flag `--auto-advance` which auto advances by default, when interactively selecting patches with the '--patch' option. However, the `--no-auto-advance` option does not auto advance, thereby allowing users the option to rework with files. Signed-off-by: Abraham Samuel Adekunle <abrahamadekunle50@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 68cb7f9 commit 06c81a1

7 files changed

Lines changed: 29 additions & 1 deletion

File tree

add-interactive.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ void init_add_i_state(struct add_i_state *s, struct repository *r,
6464
s->r = r;
6565
s->context = -1;
6666
s->interhunkcontext = -1;
67+
s->auto_advance = add_p_opt->auto_advance;
6768

6869
s->use_color_interactive = check_color_config(r, "color.interactive");
6970

@@ -1017,6 +1018,7 @@ static int run_patch(struct add_i_state *s, const struct pathspec *ps,
10171018
struct add_p_opt add_p_opt = {
10181019
.context = s->context,
10191020
.interhunkcontext = s->interhunkcontext,
1021+
.auto_advance = s->auto_advance
10201022
};
10211023
struct strvec args = STRVEC_INIT;
10221024
struct pathspec ps_selected = { 0 };

add-interactive.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
struct add_p_opt {
77
int context;
88
int interhunkcontext;
9+
int auto_advance;
910
};
1011

11-
#define ADD_P_OPT_INIT { .context = -1, .interhunkcontext = -1 }
12+
#define ADD_P_OPT_INIT { .context = -1, .interhunkcontext = -1, .auto_advance = 1 }
1213

1314
struct add_i_state {
1415
struct repository *r;
@@ -29,6 +30,7 @@ struct add_i_state {
2930
int use_single_key;
3031
char *interactive_diff_filter, *interactive_diff_algorithm;
3132
int context, interhunkcontext;
33+
int auto_advance;
3234
};
3335

3436
void init_add_i_state(struct add_i_state *s, struct repository *r,

builtin/add.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ static struct option builtin_add_options[] = {
256256
OPT_GROUP(""),
257257
OPT_BOOL('i', "interactive", &add_interactive, N_("interactive picking")),
258258
OPT_BOOL('p', "patch", &patch_interactive, N_("select hunks interactively")),
259+
OPT_BOOL(0, "auto-advance", &add_p_opt.auto_advance,
260+
N_("auto advance to the next file when selecting hunks interactively")),
259261
OPT_DIFF_UNIFIED(&add_p_opt.context),
260262
OPT_DIFF_INTERHUNK_CONTEXT(&add_p_opt.interhunkcontext),
261263
OPT_BOOL('e', "edit", &edit_interactive, N_("edit current diff and apply")),
@@ -418,6 +420,8 @@ int cmd_add(int argc,
418420
die(_("the option '%s' requires '%s'"), "--unified", "--interactive/--patch");
419421
if (add_p_opt.interhunkcontext != -1)
420422
die(_("the option '%s' requires '%s'"), "--inter-hunk-context", "--interactive/--patch");
423+
if (!add_p_opt.auto_advance)
424+
die(_("the option '%s' requires '%s'"), "--no-auto-advance", "--interactive/--patch");
421425
}
422426

423427
if (edit_interactive) {

builtin/checkout.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ struct checkout_opts {
6363
int patch_mode;
6464
int patch_context;
6565
int patch_interhunk_context;
66+
int auto_advance;
6667
int quiet;
6768
int merge;
6869
int force;
@@ -111,6 +112,7 @@ struct checkout_opts {
111112
.merge = -1, \
112113
.patch_context = -1, \
113114
.patch_interhunk_context = -1, \
115+
.auto_advance = 1, \
114116
}
115117

116118
struct branch_info {
@@ -549,6 +551,7 @@ static int checkout_paths(const struct checkout_opts *opts,
549551
struct add_p_opt add_p_opt = {
550552
.context = opts->patch_context,
551553
.interhunkcontext = opts->patch_interhunk_context,
554+
.auto_advance = opts->auto_advance
552555
};
553556
const char *rev = new_branch_info->name;
554557
char rev_oid[GIT_MAX_HEXSZ + 1];
@@ -1801,6 +1804,8 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
18011804
die(_("the option '%s' requires '%s'"), "--unified", "--patch");
18021805
if (opts->patch_interhunk_context != -1)
18031806
die(_("the option '%s' requires '%s'"), "--inter-hunk-context", "--patch");
1807+
if (!opts->auto_advance)
1808+
die(_("the option '%s' requires '%s'"), "--no-auto-advance", "--patch");
18041809
}
18051810

18061811
if (opts->show_progress < 0) {
@@ -1999,6 +2004,8 @@ int cmd_checkout(int argc,
19992004
OPT_BOOL(0, "guess", &opts.dwim_new_local_branch,
20002005
N_("second guess 'git checkout <no-such-branch>' (default)")),
20012006
OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode (default)")),
2007+
OPT_BOOL(0, "auto-advance", &opts.auto_advance,
2008+
N_("auto advance to the next file when selecting hunks interactively")),
20022009
OPT_END()
20032010
};
20042011

builtin/reset.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ int cmd_reset(int argc,
371371
PARSE_OPT_OPTARG,
372372
option_parse_recurse_submodules_worktree_updater),
373373
OPT_BOOL('p', "patch", &patch_mode, N_("select hunks interactively")),
374+
OPT_BOOL(0, "auto-advance", &add_p_opt.auto_advance,
375+
N_("auto advance to the next file when selecting hunks interactively")),
374376
OPT_DIFF_UNIFIED(&add_p_opt.context),
375377
OPT_DIFF_INTERHUNK_CONTEXT(&add_p_opt.interhunkcontext),
376378
OPT_BOOL('N', "intent-to-add", &intent_to_add,
@@ -443,6 +445,8 @@ int cmd_reset(int argc,
443445
die(_("the option '%s' requires '%s'"), "--unified", "--patch");
444446
if (add_p_opt.interhunkcontext != -1)
445447
die(_("the option '%s' requires '%s'"), "--inter-hunk-context", "--patch");
448+
if (!add_p_opt.auto_advance)
449+
die(_("the option '%s' requires '%s'"), "--no-auto-advance", "--patch");
446450
}
447451

448452
/* git reset tree [--] paths... can be used to

builtin/stash.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,6 +1849,8 @@ static int push_stash(int argc, const char **argv, const char *prefix,
18491849
N_("stash staged changes only")),
18501850
OPT_BOOL('p', "patch", &patch_mode,
18511851
N_("stash in patch mode")),
1852+
OPT_BOOL(0, "auto-advance", &add_p_opt.auto_advance,
1853+
N_("auto advance to the next file when selecting hunks interactively")),
18521854
OPT_DIFF_UNIFIED(&add_p_opt.context),
18531855
OPT_DIFF_INTERHUNK_CONTEXT(&add_p_opt.interhunkcontext),
18541856
OPT__QUIET(&quiet, N_("quiet mode")),
@@ -1911,6 +1913,8 @@ static int push_stash(int argc, const char **argv, const char *prefix,
19111913
die(_("the option '%s' requires '%s'"), "--unified", "--patch");
19121914
if (add_p_opt.interhunkcontext != -1)
19131915
die(_("the option '%s' requires '%s'"), "--inter-hunk-context", "--patch");
1916+
if (!add_p_opt.auto_advance)
1917+
die(_("the option '%s' requires '%s'"), "--no-auto-advance", "--patch");
19141918
}
19151919

19161920
if (add_p_opt.context < -1)
@@ -1952,6 +1956,8 @@ static int save_stash(int argc, const char **argv, const char *prefix,
19521956
N_("stash staged changes only")),
19531957
OPT_BOOL('p', "patch", &patch_mode,
19541958
N_("stash in patch mode")),
1959+
OPT_BOOL(0, "auto-advance", &add_p_opt.auto_advance,
1960+
N_("auto advance to the next file when selecting hunks interactively")),
19551961
OPT_DIFF_UNIFIED(&add_p_opt.context),
19561962
OPT_DIFF_INTERHUNK_CONTEXT(&add_p_opt.interhunkcontext),
19571963
OPT__QUIET(&quiet, N_("quiet mode")),
@@ -1983,6 +1989,8 @@ static int save_stash(int argc, const char **argv, const char *prefix,
19831989
die(_("the option '%s' requires '%s'"), "--unified", "--patch");
19841990
if (add_p_opt.interhunkcontext != -1)
19851991
die(_("the option '%s' requires '%s'"), "--inter-hunk-context", "--patch");
1992+
if (!add_p_opt.auto_advance)
1993+
die(_("the option '%s' requires '%s'"), "--no-auto-advance", "--patch");
19861994
}
19871995

19881996
ret = do_push_stash(&ps, stash_msg, quiet, keep_index,

t/t9902-completion.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2601,6 +2601,7 @@ test_expect_success 'double dash "git checkout"' '
26012601
--ignore-skip-worktree-bits Z
26022602
--ignore-other-worktrees Z
26032603
--recurse-submodules Z
2604+
--auto-advance Z
26042605
--progress Z
26052606
--guess Z
26062607
--no-guess Z

0 commit comments

Comments
 (0)