Skip to content

Commit 6b873ab

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 794a908 commit 6b873ab

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
};
@@ -179,6 +180,8 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
179180
pend = p + plain->len;
180181
while (p != pend) {
181182
char *eol = memchr(p, '\n', pend - p);
183+
const char *deleted = NULL;
184+
182185
if (!eol)
183186
eol = pend;
184187

@@ -195,7 +198,11 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
195198
} else if (p == plain->buf)
196199
BUG("diff starts with unexpected line:\n"
197200
"%.*s\n", (int)(eol - p), p);
198-
else if (starts_with(p, "@@ ")) {
201+
else if (file_diff->deleted)
202+
; /* keep the rest of the file in a single "hunk" */
203+
else if (starts_with(p, "@@ ") ||
204+
(hunk == &file_diff->head &&
205+
skip_prefix(p, "deleted file", &deleted))) {
199206
file_diff->hunk_nr++;
200207
ALLOC_GROW(file_diff->hunk, file_diff->hunk_nr,
201208
file_diff->hunk_alloc);
@@ -206,7 +213,9 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
206213
if (colored)
207214
hunk->colored_start = colored_p - colored->buf;
208215

209-
if (parse_hunk_header(s, hunk) < 0)
216+
if (deleted)
217+
file_diff->deleted = 1;
218+
else if (parse_hunk_header(s, hunk) < 0)
210219
return -1;
211220
}
212221

0 commit comments

Comments
 (0)