@matheusbsbs / matheus-bagliano / commits / 47dc4fd5eb
Fork 0

built-in add -p: handle deleted empty files

This addresses the same problem as 24ab81ae4d (add-interactive: handle deletion of empty files, 2009-10-27), although in a different way: we not only stick the "deleted file" line into its own pseudo hunk, but also the entire remainder (if any) of the same diff. That way, we do not have to play any funny games with regards to coalescing the diff after the user selected what (possibly pseudo-)hunks to stage. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Dec 13, 2019 at 08:07 UTC 47dc4fd5eb4ab8fbe8ea6bb10e35d398da991cea
1 file changed +11 -2
add-patch.c
+11 -2
@@ -33,6 +33,7 @@ struct add_p_state {
33 struct hunk head;
34 struct hunk *hunk;
35 size_t hunk_nr, hunk_alloc;
36 + unsigned deleted:1;
37 } *file_diff;
38 size_t file_diff_nr;
39 };
@@ -180,6 +181,8 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
181 pend = p + plain->len;
182 while (p != pend) {
183 char *eol = memchr(p, '\n', pend - p);
184 + const char *deleted = NULL;
185 +
186 if (!eol)
187 eol = pend;
188
@@ -196,7 +199,11 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
199 } else if (p == plain->buf)
200 BUG("diff starts with unexpected line:\n"
201 "%.*s\n", (int)(eol - p), p);
199 - else if (starts_with(p, "@@ ")) {
202 + else if (file_diff->deleted)
203 + ; /* keep the rest of the file in a single "hunk" */
204 + else if (starts_with(p, "@@ ") ||
205 + (hunk == &file_diff->head &&
206 + skip_prefix(p, "deleted file", &deleted))) {
207 file_diff->hunk_nr++;
208 ALLOC_GROW(file_diff->hunk, file_diff->hunk_nr,
209 file_diff->hunk_alloc);
@@ -207,7 +214,9 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
214 if (colored)
215 hunk->colored_start = colored_p - colored->buf;
216
210 - if (parse_hunk_header(s, hunk) < 0)
217 + if (deleted)
218 + file_diff->deleted = 1;
219 + else if (parse_hunk_header(s, hunk) < 0)
220 return -1;
221 }
222