Skip to content

Commit 0484407

Browse files
committed
add-interactive: add new "context" subcommand
This teaches `add/commit --interactive` a new "context" subcommand, which changes the amount of context lines subsequent subcommands like "patch" or "diff" generate in their diffs. Signed-off-by: Leon Michalak <[email protected]>
1 parent 66ed9f6 commit 0484407

File tree

3 files changed

+108
-7
lines changed

3 files changed

+108
-7
lines changed

Documentation/git-add.adoc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,15 @@ and type return, like this:
265265
------------
266266
*** Commands ***
267267
1: status 2: update 3: revert 4: add untracked
268-
5: patch 6: diff 7: quit 8: help
268+
5: patch 6: diff 7: context 8: quit
269+
9: help
269270
What now> 1
270271
------------
271272
272273
You also could say `s` or `sta` or `status` above as long as the
273274
choice is unique.
274275
275-
The main command loop has 6 subcommands (plus help and quit).
276+
The main command loop has 7 subcommands (plus help and quit).
276277
277278
status::
278279
@@ -373,6 +374,11 @@ diff::
373374
This lets you review what will be committed (i.e. between
374375
`HEAD` and index).
375376
377+
context::
378+
379+
This lets you change the amount of context lines shown in diffs that
380+
the 'patch' and 'diff' subcommands generate.
381+
376382
377383
EDITING PATCHES
378384
---------------

add-interactive.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "prompt.h"
2121
#include "tree.h"
2222

23+
static void choose_prompt_help_context(struct add_i_state *s);
24+
2325
static void init_color(struct repository *r, struct add_i_state *s,
2426
const char *section_and_slot, char *dst,
2527
const char *default_color)
@@ -1049,6 +1051,60 @@ static int run_diff(struct add_i_state *s, const struct pathspec *ps,
10491051
return res;
10501052
}
10511053

1054+
static int run_context(struct add_i_state *s, const struct pathspec *ps UNUSED,
1055+
struct prefix_item_list *files UNUSED,
1056+
struct list_and_choose_options *opts UNUSED)
1057+
{
1058+
struct diff_options diffopts;
1059+
struct strbuf input = STRBUF_INIT;
1060+
int res = 0;
1061+
1062+
repo_diff_setup(s->r, &diffopts);
1063+
1064+
for (;;) {
1065+
int new_context;
1066+
char *endp;
1067+
1068+
strbuf_reset(&input);
1069+
1070+
color_fprintf(stdout, s->header_color, " %s:", N_("Current"));
1071+
fprintf(stdout, " %i\n", s->context == -1 ?
1072+
diffopts.context : s->context);
1073+
1074+
color_fprintf(stdout, s->prompt_color, "%s", N_("Change context"));
1075+
fputs("> ", stdout);
1076+
fflush(stdout);
1077+
1078+
if (git_read_line_interactively(&input) == EOF) {
1079+
putchar('\n');
1080+
break;
1081+
}
1082+
1083+
if (!input.len)
1084+
break;
1085+
1086+
if (!strcmp(input.buf, "?")) {
1087+
choose_prompt_help_context(s);
1088+
continue;
1089+
}
1090+
1091+
new_context = strtol(input.buf, &endp, 10);
1092+
if (*endp) {
1093+
color_fprintf_ln(stderr, s->error_color,
1094+
_("Context must be a numerical value"));
1095+
continue;
1096+
}
1097+
1098+
s->context = new_context;
1099+
1100+
break;
1101+
}
1102+
1103+
strbuf_release(&input);
1104+
putchar('\n');
1105+
return res;
1106+
}
1107+
10521108
static int run_help(struct add_i_state *s, const struct pathspec *ps UNUSED,
10531109
struct prefix_item_list *files UNUSED,
10541110
struct list_and_choose_options *opts UNUSED)
@@ -1063,6 +1119,8 @@ static int run_help(struct add_i_state *s, const struct pathspec *ps UNUSED,
10631119
_("pick hunks and update selectively"));
10641120
color_fprintf_ln(stdout, s->help_color, "diff - %s",
10651121
_("view diff between HEAD and index"));
1122+
color_fprintf_ln(stdout, s->help_color, "context - %s",
1123+
_("change how many context lines diffs are generated with"));
10661124
color_fprintf_ln(stdout, s->help_color, "add untracked - %s",
10671125
_("add contents of untracked files to the staged set of changes"));
10681126

@@ -1089,6 +1147,16 @@ static void choose_prompt_help(struct add_i_state *s)
10891147
_("(empty) finish selecting"));
10901148
}
10911149

1150+
static void choose_prompt_help_context(struct add_i_state *s)
1151+
{
1152+
color_fprintf_ln(stdout, s->help_color, "%s",
1153+
_("Prompt help:"));
1154+
color_fprintf_ln(stdout, s->help_color, "<n> - %s",
1155+
_("specify new context lines amount"));
1156+
color_fprintf_ln(stdout, s->help_color, " - %s",
1157+
_("(empty) finish selecting"));
1158+
}
1159+
10921160
typedef int (*command_t)(struct add_i_state *s, const struct pathspec *ps,
10931161
struct prefix_item_list *files,
10941162
struct list_and_choose_options *opts);
@@ -1149,6 +1217,7 @@ int run_add_i(struct repository *r, const struct pathspec *ps,
11491217
{ "add untracked", run_add_untracked },
11501218
{ "patch", run_patch },
11511219
{ "diff", run_diff },
1220+
{ "context", run_context },
11521221
{ "quit", NULL },
11531222
{ "help", run_help },
11541223
};

t/t3701-add-interactive.sh

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -758,16 +758,19 @@ test_expect_success 'colors can be overridden' '
758758
759759
<RED>*** Commands ***<RESET>
760760
1: <YELLOW>s<RESET>tatus 2: <YELLOW>u<RESET>pdate 3: <YELLOW>r<RESET>evert 4: <YELLOW>a<RESET>dd untracked
761-
5: <YELLOW>p<RESET>atch 6: <YELLOW>d<RESET>iff 7: <YELLOW>q<RESET>uit 8: <YELLOW>h<RESET>elp
761+
5: <YELLOW>p<RESET>atch 6: <YELLOW>d<RESET>iff 7: <YELLOW>c<RESET>ontext 8: <YELLOW>q<RESET>uit
762+
9: <YELLOW>h<RESET>elp
762763
<YELLOW>What now<RESET>> <GREEN>status - show paths with changes<RESET>
763764
<GREEN>update - add working tree state to the staged set of changes<RESET>
764765
<GREEN>revert - revert staged set of changes back to the HEAD version<RESET>
765766
<GREEN>patch - pick hunks and update selectively<RESET>
766767
<GREEN>diff - view diff between HEAD and index<RESET>
768+
<GREEN>context - change how many context lines diffs are generated with<RESET>
767769
<GREEN>add untracked - add contents of untracked files to the staged set of changes<RESET>
768770
<RED>*** Commands ***<RESET>
769771
1: <YELLOW>s<RESET>tatus 2: <YELLOW>u<RESET>pdate 3: <YELLOW>r<RESET>evert 4: <YELLOW>a<RESET>dd untracked
770-
5: <YELLOW>p<RESET>atch 6: <YELLOW>d<RESET>iff 7: <YELLOW>q<RESET>uit 8: <YELLOW>h<RESET>elp
772+
5: <YELLOW>p<RESET>atch 6: <YELLOW>d<RESET>iff 7: <YELLOW>c<RESET>ontext 8: <YELLOW>q<RESET>uit
773+
9: <YELLOW>h<RESET>elp
771774
<YELLOW>What now<RESET>> Bye.
772775
EOF
773776
test_cmp expect actual &&
@@ -831,7 +834,8 @@ test_expect_success 'brackets appear without color' '
831834
|
832835
|*** Commands ***
833836
| 1: [s]tatus 2: [u]pdate 3: [r]evert 4: [a]dd untracked
834-
| 5: [p]atch 6: [d]iff 7: [q]uit 8: [h]elp
837+
| 5: [p]atch 6: [d]iff 7: [c]ontext 8: [q]uit
838+
| 9: [h]elp
835839
|What now> Bye.
836840
EOF
837841
@@ -1172,16 +1176,19 @@ test_expect_success 'show help from add--helper' '
11721176
11731177
<BOLD>*** Commands ***<RESET>
11741178
1: <BOLD;BLUE>s<RESET>tatus 2: <BOLD;BLUE>u<RESET>pdate 3: <BOLD;BLUE>r<RESET>evert 4: <BOLD;BLUE>a<RESET>dd untracked
1175-
5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
1179+
5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>c<RESET>ontext 8: <BOLD;BLUE>q<RESET>uit
1180+
9: <BOLD;BLUE>h<RESET>elp
11761181
<BOLD;BLUE>What now<RESET>> <BOLD;RED>status - show paths with changes<RESET>
11771182
<BOLD;RED>update - add working tree state to the staged set of changes<RESET>
11781183
<BOLD;RED>revert - revert staged set of changes back to the HEAD version<RESET>
11791184
<BOLD;RED>patch - pick hunks and update selectively<RESET>
11801185
<BOLD;RED>diff - view diff between HEAD and index<RESET>
1186+
<BOLD;RED>context - change how many context lines diffs are generated with<RESET>
11811187
<BOLD;RED>add untracked - add contents of untracked files to the staged set of changes<RESET>
11821188
<BOLD>*** Commands ***<RESET>
11831189
1: <BOLD;BLUE>s<RESET>tatus 2: <BOLD;BLUE>u<RESET>pdate 3: <BOLD;BLUE>r<RESET>evert 4: <BOLD;BLUE>a<RESET>dd untracked
1184-
5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
1190+
5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>c<RESET>ontext 8: <BOLD;BLUE>q<RESET>uit
1191+
9: <BOLD;BLUE>h<RESET>elp
11851192
<BOLD;BLUE>What now<RESET>>$SP
11861193
Bye.
11871194
EOF
@@ -1230,4 +1237,23 @@ test_expect_success 'hunk splitting works with diff.suppressBlankEmpty' '
12301237
test_cmp expect actual
12311238
'
12321239

1240+
test_expect_success 'change context works' '
1241+
git reset --hard &&
1242+
cat >template <<-\EOF &&
1243+
firstline
1244+
preline
1245+
TARGET
1246+
postline
1247+
lastline
1248+
EOF
1249+
sed "/TARGET/d" >x <template &&
1250+
git update-index --add x &&
1251+
git commit -m initial &&
1252+
sed "s/TARGET/ADDED/" >x <template &&
1253+
test_write_lines p 1 | git add -i >output &&
1254+
grep firstline output &&
1255+
test_write_lines c 0 p 1 | git add -i >output &&
1256+
! grep firstline output
1257+
'
1258+
12331259
test_done

0 commit comments

Comments
 (0)