Skip to content

Commit

Permalink
Merge branch 'pw/add-patch-with-suppress-blank-empty' into seen
Browse files Browse the repository at this point in the history
* pw/add-patch-with-suppress-blank-empty:
  add-patch: use normalize_marker() when recounting edited hunk
  add-patch: handle splitting hunks with diff.suppressBlankEmpty
  • Loading branch information
gitster committed Jul 20, 2024
2 parents 689e49d + 60cf761 commit 19a8982
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
23 changes: 15 additions & 8 deletions add-patch.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,12 @@ static void complete_file(char marker, struct hunk *hunk)
hunk->splittable_into++;
}

/* Empty context lines may omit the leading ' ' */
static int normalize_marker(const char *p)
{
return p[0] == '\n' || (p[0] == '\r' && p[1] == '\n') ? ' ' : p[0];
}

static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
{
struct strvec args = STRVEC_INIT;
Expand Down Expand Up @@ -487,6 +493,7 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
while (p != pend) {
char *eol = memchr(p, '\n', pend - p);
const char *deleted = NULL, *mode_change = NULL;
char ch = normalize_marker(p);

if (!eol)
eol = pend;
Expand Down Expand Up @@ -534,7 +541,7 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
* Start counting into how many hunks this one can be
* split
*/
marker = *p;
marker = ch;
} else if (hunk == &file_diff->head &&
starts_with(p, "new file")) {
file_diff->added = 1;
Expand Down Expand Up @@ -588,10 +595,10 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
(int)(eol - (plain->buf + file_diff->head.start)),
plain->buf + file_diff->head.start);

if ((marker == '-' || marker == '+') && *p == ' ')
if ((marker == '-' || marker == '+') && ch == ' ')
hunk->splittable_into++;
if (marker && *p != '\\')
marker = *p;
if (marker && ch != '\\')
marker = ch;

p = eol == pend ? pend : eol + 1;
hunk->end = p - plain->buf;
Expand Down Expand Up @@ -815,7 +822,7 @@ static int merge_hunks(struct add_p_state *s, struct file_diff *file_diff,
(int)(hunk->end - hunk->start),
plain + hunk->start);

if (plain[overlap_end] != ' ')
if (normalize_marker(&plain[overlap_end]) != ' ')
return error(_("expected context line "
"#%d in\n%.*s"),
(int)(j + 1),
Expand Down Expand Up @@ -955,7 +962,7 @@ static int split_hunk(struct add_p_state *s, struct file_diff *file_diff,
context_line_count = 0;

while (splittable_into > 1) {
ch = s->plain.buf[current];
ch = normalize_marker(&s->plain.buf[current]);

if (!ch)
BUG("buffer overrun while splitting hunks");
Expand Down Expand Up @@ -1173,14 +1180,14 @@ static ssize_t recount_edited_hunk(struct add_p_state *s, struct hunk *hunk,

header->old_count = header->new_count = 0;
for (i = hunk->start; i < hunk->end; ) {
switch (s->plain.buf[i]) {
switch(normalize_marker(&s->plain.buf[i])) {
case '-':
header->old_count++;
break;
case '+':
header->new_count++;
break;
case ' ': case '\r': case '\n':
case ' ':
header->old_count++;
header->new_count++;
break;
Expand Down
19 changes: 19 additions & 0 deletions t/t3701-add-interactive.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1164,4 +1164,23 @@ test_expect_success 'reset -p with unmerged files' '
test_must_be_empty staged
'

test_expect_success 'hunk splitting works with diff.suppressBlankEmpty' '
test_config diff.suppressBlankEmpty true &&
write_script fake-editor.sh <<-\EOF &&
tr F G <"$1" >"$1.tmp" &&
mv "$1.tmp" "$1"
EOF
test_write_lines a b "" c d "" e f "" >file &&
git add file &&
test_write_lines A b "" c D "" e F "" >file &&
(
test_set_editor "$(pwd)/fake-editor.sh" &&
test_write_lines s n y e q | git add -p file
) &&
git cat-file blob :file >actual &&
test_write_lines a b "" c D "" e G "" >expect &&
test_cmp expect actual
'

test_done

0 comments on commit 19a8982

Please sign in to comment.