Skip to content

Commit d51b61f

Browse files
pks-tgitster
authored andcommitted
add-patch: remove dependency on "add-interactive" subsystem
With the preceding commit we have split out interactive configuration that is used by both "git add -p" and "git add -i". But we still initialize that configuration in the "add -p" subsystem by calling `init_add_i_state()`, even though we only do so to initialize the interactive configuration as well as a repository pointer. Stop doing so and instead store and initialize the interactive configuration in `struct add_p_state` directly. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent e3d4d77 commit d51b61f

1 file changed

Lines changed: 46 additions & 42 deletions

File tree

add-patch.c

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define DISABLE_SIGN_COMPARE_WARNINGS
33

44
#include "git-compat-util.h"
5-
#include "add-interactive.h"
65
#include "add-patch.h"
76
#include "advice.h"
87
#include "config.h"
@@ -263,7 +262,8 @@ struct hunk {
263262
};
264263

265264
struct add_p_state {
266-
struct add_i_state s;
265+
struct repository *r;
266+
struct interactive_config cfg;
267267
struct strbuf answer, buf;
268268

269269
/* parsed diff */
@@ -409,7 +409,7 @@ static void add_p_state_clear(struct add_p_state *s)
409409
for (i = 0; i < s->file_diff_nr; i++)
410410
free(s->file_diff[i].hunk);
411411
free(s->file_diff);
412-
clear_add_i_state(&s->s);
412+
interactive_config_clear(&s->cfg);
413413
}
414414

415415
__attribute__((format (printf, 2, 3)))
@@ -418,9 +418,9 @@ static void err(struct add_p_state *s, const char *fmt, ...)
418418
va_list args;
419419

420420
va_start(args, fmt);
421-
fputs(s->s.cfg.error_color, stdout);
421+
fputs(s->cfg.error_color, stdout);
422422
vprintf(fmt, args);
423-
puts(s->s.cfg.reset_color_interactive);
423+
puts(s->cfg.reset_color_interactive);
424424
va_end(args);
425425
}
426426

@@ -438,7 +438,7 @@ static void setup_child_process(struct add_p_state *s,
438438

439439
cp->git_cmd = 1;
440440
strvec_pushf(&cp->env,
441-
INDEX_ENVIRONMENT "=%s", s->s.r->index_file);
441+
INDEX_ENVIRONMENT "=%s", s->r->index_file);
442442
}
443443

444444
static int parse_range(const char **p,
@@ -543,12 +543,12 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
543543
int res;
544544

545545
strvec_pushv(&args, s->mode->diff_cmd);
546-
if (s->s.cfg.context != -1)
547-
strvec_pushf(&args, "--unified=%i", s->s.cfg.context);
548-
if (s->s.cfg.interhunkcontext != -1)
549-
strvec_pushf(&args, "--inter-hunk-context=%i", s->s.cfg.interhunkcontext);
550-
if (s->s.cfg.interactive_diff_algorithm)
551-
strvec_pushf(&args, "--diff-algorithm=%s", s->s.cfg.interactive_diff_algorithm);
546+
if (s->cfg.context != -1)
547+
strvec_pushf(&args, "--unified=%i", s->cfg.context);
548+
if (s->cfg.interhunkcontext != -1)
549+
strvec_pushf(&args, "--inter-hunk-context=%i", s->cfg.interhunkcontext);
550+
if (s->cfg.interactive_diff_algorithm)
551+
strvec_pushf(&args, "--diff-algorithm=%s", s->cfg.interactive_diff_algorithm);
552552
if (s->revision) {
553553
struct object_id oid;
554554
strvec_push(&args,
@@ -577,9 +577,9 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
577577
}
578578
strbuf_complete_line(plain);
579579

580-
if (want_color_fd(1, s->s.cfg.use_color_diff)) {
580+
if (want_color_fd(1, s->cfg.use_color_diff)) {
581581
struct child_process colored_cp = CHILD_PROCESS_INIT;
582-
const char *diff_filter = s->s.cfg.interactive_diff_filter;
582+
const char *diff_filter = s->cfg.interactive_diff_filter;
583583

584584
setup_child_process(s, &colored_cp, NULL);
585585
xsnprintf((char *)args.v[color_arg_index], 8, "--color");
@@ -812,7 +812,7 @@ static void render_hunk(struct add_p_state *s, struct hunk *hunk,
812812
hunk->colored_end - hunk->colored_start);
813813
return;
814814
} else {
815-
strbuf_addstr(out, s->s.cfg.fraginfo_color);
815+
strbuf_addstr(out, s->cfg.fraginfo_color);
816816
p = s->colored.buf + header->colored_extra_start;
817817
len = header->colored_extra_end
818818
- header->colored_extra_start;
@@ -834,7 +834,7 @@ static void render_hunk(struct add_p_state *s, struct hunk *hunk,
834834
if (len)
835835
strbuf_add(out, p, len);
836836
else if (colored)
837-
strbuf_addf(out, "%s\n", s->s.cfg.reset_color_diff);
837+
strbuf_addf(out, "%s\n", s->cfg.reset_color_diff);
838838
else
839839
strbuf_addch(out, '\n');
840840
}
@@ -1223,12 +1223,12 @@ static void recolor_hunk(struct add_p_state *s, struct hunk *hunk)
12231223

12241224
strbuf_addstr(&s->colored,
12251225
plain[current] == '-' ?
1226-
s->s.cfg.file_old_color :
1226+
s->cfg.file_old_color :
12271227
plain[current] == '+' ?
1228-
s->s.cfg.file_new_color :
1229-
s->s.cfg.context_color);
1228+
s->cfg.file_new_color :
1229+
s->cfg.context_color);
12301230
strbuf_add(&s->colored, plain + current, eol - current);
1231-
strbuf_addstr(&s->colored, s->s.cfg.reset_color_diff);
1231+
strbuf_addstr(&s->colored, s->cfg.reset_color_diff);
12321232
if (next > eol)
12331233
strbuf_add(&s->colored, plain + eol, next - eol);
12341234
current = next;
@@ -1357,7 +1357,7 @@ static int run_apply_check(struct add_p_state *s,
13571357

13581358
static int read_single_character(struct add_p_state *s)
13591359
{
1360-
if (s->s.cfg.use_single_key) {
1360+
if (s->cfg.use_single_key) {
13611361
int res = read_key_without_echo(&s->answer);
13621362
printf("%s\n", res == EOF ? "" : s->answer.buf);
13631363
return res;
@@ -1371,7 +1371,7 @@ static int read_single_character(struct add_p_state *s)
13711371
static int prompt_yesno(struct add_p_state *s, const char *prompt)
13721372
{
13731373
for (;;) {
1374-
color_fprintf(stdout, s->s.cfg.prompt_color, "%s", _(prompt));
1374+
color_fprintf(stdout, s->cfg.prompt_color, "%s", _(prompt));
13751375
fflush(stdout);
13761376
if (read_single_character(s) == EOF)
13771377
return -1;
@@ -1559,7 +1559,7 @@ static void apply_patch(struct add_p_state *s, struct file_diff *file_diff)
15591559
strbuf_reset(&s->buf);
15601560
reassemble_patch(s, file_diff, 0, &s->buf);
15611561

1562-
discard_index(s->s.r->index);
1562+
discard_index(s->r->index);
15631563
if (s->mode->apply_for_checkout)
15641564
apply_for_checkout(s, &s->buf,
15651565
s->mode->is_reverse);
@@ -1570,9 +1570,9 @@ static void apply_patch(struct add_p_state *s, struct file_diff *file_diff)
15701570
NULL, 0, NULL, 0))
15711571
error(_("'git apply' failed"));
15721572
}
1573-
if (repo_read_index(s->s.r) >= 0)
1574-
repo_refresh_and_write_index(s->s.r, REFRESH_QUIET, 0,
1575-
1, NULL, NULL, NULL);
1573+
if (repo_read_index(s->r) >= 0)
1574+
repo_refresh_and_write_index(s->r, REFRESH_QUIET, 0,
1575+
1, NULL, NULL, NULL);
15761576
}
15771577

15781578
}
@@ -1660,7 +1660,7 @@ static size_t patch_update_file(struct add_p_state *s, size_t idx)
16601660
/* Everything decided? */
16611661
if (undecided_previous < 0 && undecided_next < 0 &&
16621662
hunk->use != UNDECIDED_HUNK) {
1663-
if (!s->s.cfg.auto_advance)
1663+
if (!s->cfg.auto_advance)
16641664
all_decided = 1;
16651665
else {
16661666
patch_update_resp++;
@@ -1714,11 +1714,11 @@ static size_t patch_update_file(struct add_p_state *s, size_t idx)
17141714
permitted |= ALLOW_EDIT;
17151715
strbuf_addstr(&s->buf, ",e");
17161716
}
1717-
if (!s->s.cfg.auto_advance && s->file_diff_nr > 1) {
1717+
if (!s->cfg.auto_advance && s->file_diff_nr > 1) {
17181718
permitted |= ALLOW_GOTO_NEXT_FILE;
17191719
strbuf_addstr(&s->buf, ",>");
17201720
}
1721-
if (!s->s.cfg.auto_advance && s->file_diff_nr > 1) {
1721+
if (!s->cfg.auto_advance && s->file_diff_nr > 1) {
17221722
permitted |= ALLOW_GOTO_PREVIOUS_FILE;
17231723
strbuf_addstr(&s->buf, ",<");
17241724
}
@@ -1733,7 +1733,7 @@ static size_t patch_update_file(struct add_p_state *s, size_t idx)
17331733
else
17341734
prompt_mode_type = PROMPT_HUNK;
17351735

1736-
printf("%s(%"PRIuMAX"/%"PRIuMAX") ", s->s.cfg.prompt_color,
1736+
printf("%s(%"PRIuMAX"/%"PRIuMAX") ", s->cfg.prompt_color,
17371737
(uintmax_t)hunk_index + 1,
17381738
(uintmax_t)(file_diff->hunk_nr
17391739
? file_diff->hunk_nr
@@ -1746,8 +1746,8 @@ static size_t patch_update_file(struct add_p_state *s, size_t idx)
17461746
}
17471747
printf(_(s->mode->prompt_mode[prompt_mode_type]),
17481748
hunk_use_decision, s->buf.buf);
1749-
if (*s->s.cfg.reset_color_interactive)
1750-
fputs(s->s.cfg.reset_color_interactive, stdout);
1749+
if (*s->cfg.reset_color_interactive)
1750+
fputs(s->cfg.reset_color_interactive, stdout);
17511751
fflush(stdout);
17521752
if (read_single_character(s) == EOF) {
17531753
patch_update_resp = s->file_diff_nr;
@@ -1798,7 +1798,7 @@ static size_t patch_update_file(struct add_p_state *s, size_t idx)
17981798
} else if (ch == 'q') {
17991799
patch_update_resp = s->file_diff_nr;
18001800
break;
1801-
} else if (!s->s.cfg.auto_advance && s->answer.buf[0] == '>') {
1801+
} else if (!s->cfg.auto_advance && s->answer.buf[0] == '>') {
18021802
if (permitted & ALLOW_GOTO_NEXT_FILE) {
18031803
if (patch_update_resp == s->file_diff_nr - 1)
18041804
patch_update_resp = 0;
@@ -1809,7 +1809,7 @@ static size_t patch_update_file(struct add_p_state *s, size_t idx)
18091809
err(s, _("No next file"));
18101810
continue;
18111811
}
1812-
} else if (!s->s.cfg.auto_advance && s->answer.buf[0] == '<') {
1812+
} else if (!s->cfg.auto_advance && s->answer.buf[0] == '<') {
18131813
if (permitted & ALLOW_GOTO_PREVIOUS_FILE) {
18141814
if (patch_update_resp == 0)
18151815
patch_update_resp = s->file_diff_nr - 1;
@@ -1932,7 +1932,7 @@ static size_t patch_update_file(struct add_p_state *s, size_t idx)
19321932
err(s, _("Sorry, cannot split this hunk"));
19331933
} else if (!split_hunk(s, file_diff,
19341934
hunk - file_diff->hunk)) {
1935-
color_fprintf_ln(stdout, s->s.cfg.header_color,
1935+
color_fprintf_ln(stdout, s->cfg.header_color,
19361936
_("Split into %d hunks."),
19371937
(int)splittable_into);
19381938
rendered_hunk_index = -1;
@@ -1950,7 +1950,7 @@ static size_t patch_update_file(struct add_p_state *s, size_t idx)
19501950
} else if (s->answer.buf[0] == '?') {
19511951
const char *p = _(help_patch_remainder), *eol = p;
19521952

1953-
color_fprintf(stdout, s->s.cfg.help_color, "%s",
1953+
color_fprintf(stdout, s->cfg.help_color, "%s",
19541954
_(s->mode->help_patch_text));
19551955

19561956
/*
@@ -1974,13 +1974,13 @@ static size_t patch_update_file(struct add_p_state *s, size_t idx)
19741974
if (file_diff->hunk[i].use == SKIP_HUNK)
19751975
skipped += 1;
19761976
}
1977-
color_fprintf_ln(stdout, s->s.cfg.help_color, _(p),
1977+
color_fprintf_ln(stdout, s->cfg.help_color, _(p),
19781978
total, used, skipped);
19791979
}
19801980
if (*p != '?' && !strchr(s->buf.buf, *p))
19811981
continue;
19821982

1983-
color_fprintf_ln(stdout, s->s.cfg.help_color,
1983+
color_fprintf_ln(stdout, s->cfg.help_color,
19841984
"%.*s", (int)(eol - p), p);
19851985
}
19861986
} else {
@@ -1989,7 +1989,7 @@ static size_t patch_update_file(struct add_p_state *s, size_t idx)
19891989
}
19901990
}
19911991

1992-
if (s->s.cfg.auto_advance)
1992+
if (s->cfg.auto_advance)
19931993
apply_patch(s, file_diff);
19941994

19951995
putchar('\n');
@@ -2001,11 +2001,15 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
20012001
const struct pathspec *ps)
20022002
{
20032003
struct add_p_state s = {
2004-
{ r }, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
2004+
.r = r,
2005+
.answer = STRBUF_INIT,
2006+
.buf = STRBUF_INIT,
2007+
.plain = STRBUF_INIT,
2008+
.colored = STRBUF_INIT,
20052009
};
20062010
size_t i, binary_count = 0;
20072011

2008-
init_add_i_state(&s.s, r, opts);
2012+
interactive_config_init(&s.cfg, r, opts);
20092013

20102014
if (mode == ADD_P_STASH)
20112015
s.mode = &patch_mode_stash;
@@ -2051,7 +2055,7 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
20512055
if ((i = patch_update_file(&s, i)) == s.file_diff_nr)
20522056
break;
20532057
}
2054-
if (!s.s.cfg.auto_advance)
2058+
if (!s.cfg.auto_advance)
20552059
for (i = 0; i < s.file_diff_nr; i++)
20562060
apply_patch(&s, s.file_diff + i);
20572061

0 commit comments

Comments
 (0)