Skip to content

Commit ff64c39

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 ff64c39

File tree

3 files changed

+110
-8
lines changed

3 files changed

+110
-8
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: 71 additions & 1 deletion
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)
@@ -259,7 +261,8 @@ static void list(struct add_i_state *s, struct string_list *list, int *selected,
259261
opts->print_item(i, selected ? selected[i] : 0, list->items + i,
260262
opts->print_item_data);
261263

262-
if ((opts->columns) && ((i + 1) % (opts->columns))) {
264+
if (i < list->nr - 1 &&
265+
(opts->columns) && ((i + 1) % (opts->columns))) {
263266
putchar('\t');
264267
last_lf = 0;
265268
}
@@ -1049,6 +1052,60 @@ static int run_diff(struct add_i_state *s, const struct pathspec *ps,
10491052
return res;
10501053
}
10511054

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

@@ -1089,6 +1148,16 @@ static void choose_prompt_help(struct add_i_state *s)
10891148
_("(empty) finish selecting"));
10901149
}
10911150

1151+
static void choose_prompt_help_context(struct add_i_state *s)
1152+
{
1153+
color_fprintf_ln(stdout, s->help_color, "%s",
1154+
_("Prompt help:"));
1155+
color_fprintf_ln(stdout, s->help_color, "<n> - %s",
1156+
_("specify new context lines amount"));
1157+
color_fprintf_ln(stdout, s->help_color, " - %s",
1158+
_("(empty) finish selecting"));
1159+
}
1160+
10921161
typedef int (*command_t)(struct add_i_state *s, const struct pathspec *ps,
10931162
struct prefix_item_list *files,
10941163
struct list_and_choose_options *opts);
@@ -1149,6 +1218,7 @@ int run_add_i(struct repository *r, const struct pathspec *ps,
11491218
{ "add untracked", run_add_untracked },
11501219
{ "patch", run_patch },
11511220
{ "diff", run_diff },
1221+
{ "context", run_context },
11521222
{ "quit", NULL },
11531223
{ "help", run_help },
11541224
};

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)