built-in add -p: support multi-file diffs
For simplicity, the initial implementation in C handled only a single modified file. Now it handles an arbitrary number of files. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
7584dd3c66
commit
80399aec5a
91
add-patch.c
91
add-patch.c
@ -29,9 +29,12 @@ struct add_p_state {
|
|||||||
|
|
||||||
/* parsed diff */
|
/* parsed diff */
|
||||||
struct strbuf plain, colored;
|
struct strbuf plain, colored;
|
||||||
struct hunk head;
|
struct file_diff {
|
||||||
struct hunk *hunk;
|
struct hunk head;
|
||||||
size_t hunk_nr, hunk_alloc;
|
struct hunk *hunk;
|
||||||
|
size_t hunk_nr, hunk_alloc;
|
||||||
|
} *file_diff;
|
||||||
|
size_t file_diff_nr;
|
||||||
};
|
};
|
||||||
|
|
||||||
static void err(struct add_p_state *s, const char *fmt, ...)
|
static void err(struct add_p_state *s, const char *fmt, ...)
|
||||||
@ -131,7 +134,8 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
|
|||||||
struct strbuf *plain = &s->plain, *colored = NULL;
|
struct strbuf *plain = &s->plain, *colored = NULL;
|
||||||
struct child_process cp = CHILD_PROCESS_INIT;
|
struct child_process cp = CHILD_PROCESS_INIT;
|
||||||
char *p, *pend, *colored_p = NULL, *colored_pend = NULL;
|
char *p, *pend, *colored_p = NULL, *colored_pend = NULL;
|
||||||
size_t i, color_arg_index;
|
size_t file_diff_alloc = 0, i, color_arg_index;
|
||||||
|
struct file_diff *file_diff = NULL;
|
||||||
struct hunk *hunk = NULL;
|
struct hunk *hunk = NULL;
|
||||||
int res;
|
int res;
|
||||||
|
|
||||||
@ -171,7 +175,7 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
|
|||||||
}
|
}
|
||||||
argv_array_clear(&args);
|
argv_array_clear(&args);
|
||||||
|
|
||||||
/* parse hunks */
|
/* parse files and hunks */
|
||||||
p = plain->buf;
|
p = plain->buf;
|
||||||
pend = p + plain->len;
|
pend = p + plain->len;
|
||||||
while (p != pend) {
|
while (p != pend) {
|
||||||
@ -180,17 +184,23 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
|
|||||||
eol = pend;
|
eol = pend;
|
||||||
|
|
||||||
if (starts_with(p, "diff ")) {
|
if (starts_with(p, "diff ")) {
|
||||||
if (p != plain->buf)
|
s->file_diff_nr++;
|
||||||
BUG("multi-file diff not yet handled");
|
ALLOC_GROW(s->file_diff, s->file_diff_nr,
|
||||||
hunk = &s->head;
|
file_diff_alloc);
|
||||||
|
file_diff = s->file_diff + s->file_diff_nr - 1;
|
||||||
|
memset(file_diff, 0, sizeof(*file_diff));
|
||||||
|
hunk = &file_diff->head;
|
||||||
|
hunk->start = p - plain->buf;
|
||||||
|
if (colored_p)
|
||||||
|
hunk->colored_start = colored_p - colored->buf;
|
||||||
} else if (p == plain->buf)
|
} else if (p == plain->buf)
|
||||||
BUG("diff starts with unexpected line:\n"
|
BUG("diff starts with unexpected line:\n"
|
||||||
"%.*s\n", (int)(eol - p), p);
|
"%.*s\n", (int)(eol - p), p);
|
||||||
else if (starts_with(p, "@@ ")) {
|
else if (starts_with(p, "@@ ")) {
|
||||||
s->hunk_nr++;
|
file_diff->hunk_nr++;
|
||||||
ALLOC_GROW(s->hunk, s->hunk_nr,
|
ALLOC_GROW(file_diff->hunk, file_diff->hunk_nr,
|
||||||
s->hunk_alloc);
|
file_diff->hunk_alloc);
|
||||||
hunk = s->hunk + s->hunk_nr - 1;
|
hunk = file_diff->hunk + file_diff->hunk_nr - 1;
|
||||||
memset(hunk, 0, sizeof(*hunk));
|
memset(hunk, 0, sizeof(*hunk));
|
||||||
|
|
||||||
hunk->start = p - plain->buf;
|
hunk->start = p - plain->buf;
|
||||||
@ -265,16 +275,17 @@ static void render_hunk(struct add_p_state *s, struct hunk *hunk,
|
|||||||
hunk->end - hunk->start);
|
hunk->end - hunk->start);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void reassemble_patch(struct add_p_state *s, struct strbuf *out)
|
static void reassemble_patch(struct add_p_state *s,
|
||||||
|
struct file_diff *file_diff, struct strbuf *out)
|
||||||
{
|
{
|
||||||
struct hunk *hunk;
|
struct hunk *hunk;
|
||||||
size_t i;
|
size_t i;
|
||||||
ssize_t delta = 0;
|
ssize_t delta = 0;
|
||||||
|
|
||||||
render_hunk(s, &s->head, 0, 0, out);
|
render_hunk(s, &file_diff->head, 0, 0, out);
|
||||||
|
|
||||||
for (i = 0; i < s->hunk_nr; i++) {
|
for (i = 0; i < file_diff->hunk_nr; i++) {
|
||||||
hunk = s->hunk + i;
|
hunk = file_diff->hunk + i;
|
||||||
if (hunk->use != USE_HUNK)
|
if (hunk->use != USE_HUNK)
|
||||||
delta += hunk->header.old_count
|
delta += hunk->header.old_count
|
||||||
- hunk->header.new_count;
|
- hunk->header.new_count;
|
||||||
@ -294,7 +305,8 @@ N_("y - stage this hunk\n"
|
|||||||
"K - leave this hunk undecided, see previous hunk\n"
|
"K - leave this hunk undecided, see previous hunk\n"
|
||||||
"? - print help\n");
|
"? - print help\n");
|
||||||
|
|
||||||
static int patch_update_file(struct add_p_state *s)
|
static int patch_update_file(struct add_p_state *s,
|
||||||
|
struct file_diff *file_diff)
|
||||||
{
|
{
|
||||||
size_t hunk_index = 0;
|
size_t hunk_index = 0;
|
||||||
ssize_t i, undecided_previous, undecided_next;
|
ssize_t i, undecided_previous, undecided_next;
|
||||||
@ -303,27 +315,27 @@ static int patch_update_file(struct add_p_state *s)
|
|||||||
struct child_process cp = CHILD_PROCESS_INIT;
|
struct child_process cp = CHILD_PROCESS_INIT;
|
||||||
int colored = !!s->colored.len;
|
int colored = !!s->colored.len;
|
||||||
|
|
||||||
if (!s->hunk_nr)
|
if (!file_diff->hunk_nr)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
strbuf_reset(&s->buf);
|
strbuf_reset(&s->buf);
|
||||||
render_hunk(s, &s->head, 0, colored, &s->buf);
|
render_hunk(s, &file_diff->head, 0, colored, &s->buf);
|
||||||
fputs(s->buf.buf, stdout);
|
fputs(s->buf.buf, stdout);
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (hunk_index >= s->hunk_nr)
|
if (hunk_index >= file_diff->hunk_nr)
|
||||||
hunk_index = 0;
|
hunk_index = 0;
|
||||||
hunk = s->hunk + hunk_index;
|
hunk = file_diff->hunk + hunk_index;
|
||||||
|
|
||||||
undecided_previous = -1;
|
undecided_previous = -1;
|
||||||
for (i = hunk_index - 1; i >= 0; i--)
|
for (i = hunk_index - 1; i >= 0; i--)
|
||||||
if (s->hunk[i].use == UNDECIDED_HUNK) {
|
if (file_diff->hunk[i].use == UNDECIDED_HUNK) {
|
||||||
undecided_previous = i;
|
undecided_previous = i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
undecided_next = -1;
|
undecided_next = -1;
|
||||||
for (i = hunk_index + 1; i < s->hunk_nr; i++)
|
for (i = hunk_index + 1; i < file_diff->hunk_nr; i++)
|
||||||
if (s->hunk[i].use == UNDECIDED_HUNK) {
|
if (file_diff->hunk[i].use == UNDECIDED_HUNK) {
|
||||||
undecided_next = i;
|
undecided_next = i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -344,11 +356,12 @@ static int patch_update_file(struct add_p_state *s)
|
|||||||
strbuf_addstr(&s->buf, ",K");
|
strbuf_addstr(&s->buf, ",K");
|
||||||
if (undecided_next >= 0)
|
if (undecided_next >= 0)
|
||||||
strbuf_addstr(&s->buf, ",j");
|
strbuf_addstr(&s->buf, ",j");
|
||||||
if (hunk_index + 1 < s->hunk_nr)
|
if (hunk_index + 1 < file_diff->hunk_nr)
|
||||||
strbuf_addstr(&s->buf, ",J");
|
strbuf_addstr(&s->buf, ",J");
|
||||||
color_fprintf(stdout, s->s.prompt_color,
|
color_fprintf(stdout, s->s.prompt_color,
|
||||||
"(%"PRIuMAX"/%"PRIuMAX") ",
|
"(%"PRIuMAX"/%"PRIuMAX") ",
|
||||||
(uintmax_t)hunk_index + 1, (uintmax_t)s->hunk_nr);
|
(uintmax_t)hunk_index + 1,
|
||||||
|
(uintmax_t)file_diff->hunk_nr);
|
||||||
color_fprintf(stdout, s->s.prompt_color,
|
color_fprintf(stdout, s->s.prompt_color,
|
||||||
_("Stage this hunk [y,n,a,d%s,?]? "),
|
_("Stage this hunk [y,n,a,d%s,?]? "),
|
||||||
s->buf.buf);
|
s->buf.buf);
|
||||||
@ -364,19 +377,19 @@ static int patch_update_file(struct add_p_state *s)
|
|||||||
hunk->use = USE_HUNK;
|
hunk->use = USE_HUNK;
|
||||||
soft_increment:
|
soft_increment:
|
||||||
hunk_index = undecided_next < 0 ?
|
hunk_index = undecided_next < 0 ?
|
||||||
s->hunk_nr : undecided_next;
|
file_diff->hunk_nr : undecided_next;
|
||||||
} else if (ch == 'n') {
|
} else if (ch == 'n') {
|
||||||
hunk->use = SKIP_HUNK;
|
hunk->use = SKIP_HUNK;
|
||||||
goto soft_increment;
|
goto soft_increment;
|
||||||
} else if (ch == 'a') {
|
} else if (ch == 'a') {
|
||||||
for (; hunk_index < s->hunk_nr; hunk_index++) {
|
for (; hunk_index < file_diff->hunk_nr; hunk_index++) {
|
||||||
hunk = s->hunk + hunk_index;
|
hunk = file_diff->hunk + hunk_index;
|
||||||
if (hunk->use == UNDECIDED_HUNK)
|
if (hunk->use == UNDECIDED_HUNK)
|
||||||
hunk->use = USE_HUNK;
|
hunk->use = USE_HUNK;
|
||||||
}
|
}
|
||||||
} else if (ch == 'd') {
|
} else if (ch == 'd') {
|
||||||
for (; hunk_index < s->hunk_nr; hunk_index++) {
|
for (; hunk_index < file_diff->hunk_nr; hunk_index++) {
|
||||||
hunk = s->hunk + hunk_index;
|
hunk = file_diff->hunk + hunk_index;
|
||||||
if (hunk->use == UNDECIDED_HUNK)
|
if (hunk->use == UNDECIDED_HUNK)
|
||||||
hunk->use = SKIP_HUNK;
|
hunk->use = SKIP_HUNK;
|
||||||
}
|
}
|
||||||
@ -386,7 +399,7 @@ soft_increment:
|
|||||||
else
|
else
|
||||||
err(s, _("No previous hunk"));
|
err(s, _("No previous hunk"));
|
||||||
} else if (s->answer.buf[0] == 'J') {
|
} else if (s->answer.buf[0] == 'J') {
|
||||||
if (hunk_index + 1 < s->hunk_nr)
|
if (hunk_index + 1 < file_diff->hunk_nr)
|
||||||
hunk_index++;
|
hunk_index++;
|
||||||
else
|
else
|
||||||
err(s, _("No next hunk"));
|
err(s, _("No next hunk"));
|
||||||
@ -406,14 +419,14 @@ soft_increment:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Any hunk to be used? */
|
/* Any hunk to be used? */
|
||||||
for (i = 0; i < s->hunk_nr; i++)
|
for (i = 0; i < file_diff->hunk_nr; i++)
|
||||||
if (s->hunk[i].use == USE_HUNK)
|
if (file_diff->hunk[i].use == USE_HUNK)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (i < s->hunk_nr) {
|
if (i < file_diff->hunk_nr) {
|
||||||
/* At least one hunk selected: apply */
|
/* At least one hunk selected: apply */
|
||||||
strbuf_reset(&s->buf);
|
strbuf_reset(&s->buf);
|
||||||
reassemble_patch(s, &s->buf);
|
reassemble_patch(s, file_diff, &s->buf);
|
||||||
|
|
||||||
discard_index(s->s.r->index);
|
discard_index(s->s.r->index);
|
||||||
setup_child_process(s, &cp, "apply", "--cached", NULL);
|
setup_child_process(s, &cp, "apply", "--cached", NULL);
|
||||||
@ -434,6 +447,7 @@ int run_add_p(struct repository *r, const struct pathspec *ps)
|
|||||||
struct add_p_state s = {
|
struct add_p_state s = {
|
||||||
{ r }, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
|
{ r }, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
|
||||||
};
|
};
|
||||||
|
size_t i;
|
||||||
|
|
||||||
init_add_i_state(&s.s, r);
|
init_add_i_state(&s.s, r);
|
||||||
|
|
||||||
@ -446,8 +460,9 @@ int run_add_p(struct repository *r, const struct pathspec *ps)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (s.hunk_nr)
|
for (i = 0; i < s.file_diff_nr; i++)
|
||||||
patch_update_file(&s);
|
if (patch_update_file(&s, s.file_diff + i))
|
||||||
|
break;
|
||||||
|
|
||||||
strbuf_release(&s.answer);
|
strbuf_release(&s.answer);
|
||||||
strbuf_release(&s.buf);
|
strbuf_release(&s.buf);
|
||||||
|
Reference in New Issue
Block a user