Skip to content

Commit 2890041

Browse files
committed
built-in add -p: show helpful hint when nothing can be staged
This patch will make `git add -p` show "No changes." or "Only binary files changed." in that case. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent a7bcf18 commit 2890041

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

add-patch.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct add_p_state {
4444
struct hunk head;
4545
struct hunk *hunk;
4646
size_t hunk_nr, hunk_alloc;
47-
unsigned deleted:1, mode_change:1;
47+
unsigned deleted:1, mode_change:1,binary:1;
4848
} *file_diff;
4949
size_t file_diff_nr;
5050
};
@@ -267,7 +267,9 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
267267
file_diff->mode_change = 1;
268268
} else if (file_diff->hunk_nr != 1)
269269
BUG("mode change after first hunk?");
270-
}
270+
} else if (hunk == &file_diff->head &&
271+
starts_with(p, "Binary files "))
272+
file_diff->binary = 1;
271273

272274
if (file_diff->deleted && file_diff->mode_change)
273275
BUG("diff contains delete *and* a mode change?!?\n%.*s",
@@ -1245,7 +1247,7 @@ int run_add_p(struct repository *r, const struct pathspec *ps)
12451247
struct add_p_state s = {
12461248
{ r }, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
12471249
};
1248-
size_t i;
1250+
size_t i, binary_count = 0;
12491251

12501252
if (init_add_i_state(r, &s.s))
12511253
return error("Could not read `add -i` config");
@@ -1258,9 +1260,16 @@ int run_add_p(struct repository *r, const struct pathspec *ps)
12581260
}
12591261

12601262
for (i = 0; i < s.file_diff_nr; i++)
1261-
if (patch_update_file(&s, s.file_diff + i))
1263+
if (s.file_diff[i].binary && !s.file_diff[i].hunk_nr)
1264+
binary_count++;
1265+
else if (patch_update_file(&s, s.file_diff + i))
12621266
break;
12631267

1268+
if (s.file_diff_nr == 0)
1269+
fprintf(stderr, _("No changes.\n"));
1270+
else if (binary_count == s.file_diff_nr)
1271+
fprintf(stderr, _("Only binary files changed.\n"));
1272+
12641273
strbuf_release(&s.answer);
12651274
strbuf_release(&s.buf);
12661275
strbuf_release(&s.plain);

0 commit comments

Comments
 (0)