From: "Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Johannes Schindelin <johannes.schindelin@gmx.de>,
Junio C Hamano <gitster@pobox.com>,
Johannes Schindelin <johannes.schindelin@gmx.de>
Subject: [PATCH 07/19] built-in add -p: support multi-file diffs
Date: Fri, 13 Dec 2019 08:07:54 +0000 [thread overview]
Message-ID: <0cd1522044c238286c4762dfb18d413e856f59e8.1576224486.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.173.git.1576224486.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>
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>
---
add-patch.c | 91 +++++++++++++++++++++++++++++++----------------------
1 file changed, 53 insertions(+), 38 deletions(-)
diff --git a/add-patch.c b/add-patch.c
index f59471cdf2..7c1b3b3935 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -29,9 +29,12 @@ struct add_p_state {
/* parsed diff */
struct strbuf plain, colored;
- struct hunk head;
- struct hunk *hunk;
- size_t hunk_nr, hunk_alloc;
+ struct file_diff {
+ struct hunk head;
+ 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, ...)
@@ -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 child_process cp = CHILD_PROCESS_INIT;
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;
int res;
@@ -171,7 +175,7 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
}
argv_array_clear(&args);
- /* parse hunks */
+ /* parse files and hunks */
p = plain->buf;
pend = p + plain->len;
while (p != pend) {
@@ -180,17 +184,23 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
eol = pend;
if (starts_with(p, "diff ")) {
- if (p != plain->buf)
- BUG("multi-file diff not yet handled");
- hunk = &s->head;
+ s->file_diff_nr++;
+ ALLOC_GROW(s->file_diff, s->file_diff_nr,
+ 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)
BUG("diff starts with unexpected line:\n"
"%.*s\n", (int)(eol - p), p);
else if (starts_with(p, "@@ ")) {
- s->hunk_nr++;
- ALLOC_GROW(s->hunk, s->hunk_nr,
- s->hunk_alloc);
- hunk = s->hunk + s->hunk_nr - 1;
+ file_diff->hunk_nr++;
+ ALLOC_GROW(file_diff->hunk, file_diff->hunk_nr,
+ file_diff->hunk_alloc);
+ hunk = file_diff->hunk + file_diff->hunk_nr - 1;
memset(hunk, 0, sizeof(*hunk));
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);
}
-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;
size_t i;
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++) {
- hunk = s->hunk + i;
+ for (i = 0; i < file_diff->hunk_nr; i++) {
+ hunk = file_diff->hunk + i;
if (hunk->use != USE_HUNK)
delta += hunk->header.old_count
- hunk->header.new_count;
@@ -294,7 +305,8 @@ N_("y - stage this hunk\n"
"K - leave this hunk undecided, see previous hunk\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;
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;
int colored = !!s->colored.len;
- if (!s->hunk_nr)
+ if (!file_diff->hunk_nr)
return 0;
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);
for (;;) {
- if (hunk_index >= s->hunk_nr)
+ if (hunk_index >= file_diff->hunk_nr)
hunk_index = 0;
- hunk = s->hunk + hunk_index;
+ hunk = file_diff->hunk + hunk_index;
undecided_previous = -1;
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;
break;
}
undecided_next = -1;
- for (i = hunk_index + 1; i < s->hunk_nr; i++)
- if (s->hunk[i].use == UNDECIDED_HUNK) {
+ for (i = hunk_index + 1; i < file_diff->hunk_nr; i++)
+ if (file_diff->hunk[i].use == UNDECIDED_HUNK) {
undecided_next = i;
break;
}
@@ -344,11 +356,12 @@ static int patch_update_file(struct add_p_state *s)
strbuf_addstr(&s->buf, ",K");
if (undecided_next >= 0)
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");
color_fprintf(stdout, s->s.prompt_color,
"(%"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,
_("Stage this hunk [y,n,a,d%s,?]? "),
s->buf.buf);
@@ -364,19 +377,19 @@ static int patch_update_file(struct add_p_state *s)
hunk->use = USE_HUNK;
soft_increment:
hunk_index = undecided_next < 0 ?
- s->hunk_nr : undecided_next;
+ file_diff->hunk_nr : undecided_next;
} else if (ch == 'n') {
hunk->use = SKIP_HUNK;
goto soft_increment;
} else if (ch == 'a') {
- for (; hunk_index < s->hunk_nr; hunk_index++) {
- hunk = s->hunk + hunk_index;
+ for (; hunk_index < file_diff->hunk_nr; hunk_index++) {
+ hunk = file_diff->hunk + hunk_index;
if (hunk->use == UNDECIDED_HUNK)
hunk->use = USE_HUNK;
}
} else if (ch == 'd') {
- for (; hunk_index < s->hunk_nr; hunk_index++) {
- hunk = s->hunk + hunk_index;
+ for (; hunk_index < file_diff->hunk_nr; hunk_index++) {
+ hunk = file_diff->hunk + hunk_index;
if (hunk->use == UNDECIDED_HUNK)
hunk->use = SKIP_HUNK;
}
@@ -386,7 +399,7 @@ static int patch_update_file(struct add_p_state *s)
else
err(s, _("No previous hunk"));
} else if (s->answer.buf[0] == 'J') {
- if (hunk_index + 1 < s->hunk_nr)
+ if (hunk_index + 1 < file_diff->hunk_nr)
hunk_index++;
else
err(s, _("No next hunk"));
@@ -406,14 +419,14 @@ static int patch_update_file(struct add_p_state *s)
}
/* Any hunk to be used? */
- for (i = 0; i < s->hunk_nr; i++)
- if (s->hunk[i].use == USE_HUNK)
+ for (i = 0; i < file_diff->hunk_nr; i++)
+ if (file_diff->hunk[i].use == USE_HUNK)
break;
- if (i < s->hunk_nr) {
+ if (i < file_diff->hunk_nr) {
/* At least one hunk selected: apply */
strbuf_reset(&s->buf);
- reassemble_patch(s, &s->buf);
+ reassemble_patch(s, file_diff, &s->buf);
discard_index(s->s.r->index);
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 = {
{ r }, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
};
+ size_t i;
init_add_i_state(&s.s, r);
@@ -446,8 +460,9 @@ int run_add_p(struct repository *r, const struct pathspec *ps)
return -1;
}
- if (s.hunk_nr)
- patch_update_file(&s);
+ for (i = 0; i < s.file_diff_nr; i++)
+ if (patch_update_file(&s, s.file_diff + i))
+ break;
strbuf_release(&s.answer);
strbuf_release(&s.buf);
--
gitgitgadget
next prev parent reply other threads:[~2019-12-13 8:08 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-12-13 8:07 [PATCH 00/19] Implement the git add --patch backend in C Johannes Schindelin via GitGitGadget
2019-12-13 8:07 ` [PATCH 01/19] built-in add -i: start implementing the `patch` functionality " Johannes Schindelin via GitGitGadget
2019-12-13 8:07 ` [PATCH 02/19] built-in add -i: wire up the new C code for the `patch` command Johannes Schindelin via GitGitGadget
2019-12-13 8:07 ` [PATCH 03/19] built-in add -p: show colored hunks by default Johannes Schindelin via GitGitGadget
2019-12-13 8:07 ` [PATCH 04/19] built-in add -p: adjust hunk headers as needed Johannes Schindelin via GitGitGadget
2019-12-13 8:07 ` [PATCH 05/19] built-in add -p: color the prompt and the help text Johannes Schindelin via GitGitGadget
2019-12-13 8:07 ` [PATCH 06/19] built-in add -p: offer a helpful error message when hunk navigation failed Johannes Schindelin via GitGitGadget
2019-12-13 8:07 ` Johannes Schindelin via GitGitGadget [this message]
2019-12-13 8:07 ` [PATCH 08/19] built-in add -p: handle deleted empty files Johannes Schindelin via GitGitGadget
2019-12-13 8:07 ` [PATCH 09/19] built-in app -p: allow selecting a mode change as a "hunk" Johannes Schindelin via GitGitGadget
2019-12-13 8:07 ` [PATCH 10/19] built-in add -p: show different prompts for mode changes and deletions Johannes Schindelin via GitGitGadget
2019-12-13 8:07 ` [PATCH 11/19] built-in add -p: implement the hunk splitting feature Johannes Schindelin via GitGitGadget
2019-12-13 8:07 ` [PATCH 12/19] built-in add -p: coalesce hunks after splitting them Johannes Schindelin via GitGitGadget
2019-12-13 8:08 ` [PATCH 13/19] strbuf: add a helper function to call the editor "on an strbuf" Johannes Schindelin via GitGitGadget
2019-12-13 8:08 ` [PATCH 14/19] built-in add -p: implement hunk editing Johannes Schindelin via GitGitGadget
2019-12-13 8:08 ` [PATCH 15/19] built-in add -p: implement the 'g' ("goto") command Johannes Schindelin via GitGitGadget
2019-12-13 8:08 ` [PATCH 16/19] built-in add -p: implement the '/' ("search regex") command Johannes Schindelin via GitGitGadget
2019-12-13 8:08 ` [PATCH 17/19] built-in add -p: implement the 'q' ("quit") command Johannes Schindelin via GitGitGadget
2019-12-13 8:08 ` [PATCH 18/19] built-in add -p: only show the applicable parts of the help text Johannes Schindelin via GitGitGadget
2019-12-13 8:08 ` [PATCH 19/19] built-in add -p: show helpful hint when nothing can be staged Johannes Schindelin via GitGitGadget
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=0cd1522044c238286c4762dfb18d413e856f59e8.1576224486.git.gitgitgadget@gmail.com \
--to=gitgitgadget@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=johannes.schindelin@gmx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).