Skip to content

Commit b7856f2

Browse files
committed
built-in add -p: handle deleted empty files
This addresses the same problem as 24ab81a (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 <[email protected]>
1 parent 9aa3dd8 commit b7856f2

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

add-patch.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ struct add_p_state {
3333
struct hunk head;
3434
struct hunk *hunk;
3535
size_t hunk_nr, hunk_alloc;
36+
unsigned deleted:1;
3637
} *file_diff;
3738
size_t file_diff_nr;
3839
};
@@ -180,6 +181,8 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
180181
pend = p + plain->len;
181182
while (p != pend) {
182183
char *eol = memchr(p, '\n', pend - p);
184+
const char *deleted = NULL;
185+
183186
if (!eol)
184187
eol = pend;
185188

@@ -196,7 +199,11 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
196199
} else if (p == plain->buf)
197200
BUG("diff starts with unexpected line:\n"
198201
"%.*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))) {
200207
file_diff->hunk_nr++;
201208
ALLOC_GROW(file_diff->hunk, file_diff->hunk_nr,
202209
file_diff->hunk_alloc);
@@ -207,7 +214,9 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
207214
if (colored)
208215
hunk->colored_start = colored_p - colored->buf;
209216

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)
211220
return -1;
212221
}
213222

0 commit comments

Comments
 (0)