Skip to content

Commit 2e40831

Browse files
dschogitster
authored andcommitted
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]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 54d9d9b commit 2e40831

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
};
@@ -294,7 +294,9 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
294294
BUG("'new mode' does not immediately follow "
295295
"'old mode'?\n\n%.*s",
296296
(int)(eol - plain->buf), plain->buf);
297-
}
297+
} else if (hunk == &file_diff->head &&
298+
starts_with(p, "Binary files "))
299+
file_diff->binary = 1;
298300

299301
if (file_diff->deleted && file_diff->mode_change)
300302
BUG("diff contains delete *and* a mode change?!?\n%.*s",
@@ -1304,7 +1306,7 @@ int run_add_p(struct repository *r, const struct pathspec *ps)
13041306
struct add_p_state s = {
13051307
{ r }, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
13061308
};
1307-
size_t i;
1309+
size_t i, binary_count = 0;
13081310

13091311
init_add_i_state(&s.s, r);
13101312

@@ -1318,9 +1320,16 @@ int run_add_p(struct repository *r, const struct pathspec *ps)
13181320
}
13191321

13201322
for (i = 0; i < s.file_diff_nr; i++)
1321-
if (patch_update_file(&s, s.file_diff + i))
1323+
if (s.file_diff[i].binary && !s.file_diff[i].hunk_nr)
1324+
binary_count++;
1325+
else if (patch_update_file(&s, s.file_diff + i))
13221326
break;
13231327

1328+
if (s.file_diff_nr == 0)
1329+
fprintf(stderr, _("No changes.\n"));
1330+
else if (binary_count == s.file_diff_nr)
1331+
fprintf(stderr, _("Only binary files changed.\n"));
1332+
13241333
strbuf_release(&s.answer);
13251334
strbuf_release(&s.buf);
13261335
strbuf_release(&s.plain);

0 commit comments

Comments
 (0)