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 <johannes.schindelin@gmx.de>
This commit is contained in:
Johannes Schindelin
2019-03-24 22:54:01 +01:00
parent 297df572a4
commit 621ae45ee8

View File

@@ -44,7 +44,7 @@ struct add_p_state {
struct hunk head;
struct hunk *hunk;
size_t hunk_nr, hunk_alloc;
unsigned deleted:1, mode_change:1;
unsigned deleted:1, mode_change:1,binary:1;
} *file_diff;
size_t file_diff_nr;
};
@@ -267,7 +267,9 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
file_diff->mode_change = 1;
} else if (file_diff->hunk_nr != 1)
BUG("mode change after first hunk?");
}
} else if (hunk == &file_diff->head &&
starts_with(p, "Binary files "))
file_diff->binary = 1;
if (file_diff->deleted && file_diff->mode_change)
BUG("diff contains delete *and* a mode change?!?\n%.*s",
@@ -1273,7 +1275,7 @@ int run_add_p(struct repository *r, const struct pathspec *ps)
struct add_p_state s = {
{ r }, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
};
size_t i;
size_t i, binary_count = 0;
init_add_i_state(&s.s, r);
@@ -1287,9 +1289,16 @@ int run_add_p(struct repository *r, const struct pathspec *ps)
}
for (i = 0; i < s.file_diff_nr; i++)
if (patch_update_file(&s, s.file_diff + i))
if (s.file_diff[i].binary && !s.file_diff[i].hunk_nr)
binary_count++;
else if (patch_update_file(&s, s.file_diff + i))
break;
if (s.file_diff_nr == 0)
fprintf(stderr, _("No changes.\n"));
else if (binary_count == s.file_diff_nr)
fprintf(stderr, _("Only binary files changed.\n"));
strbuf_release(&s.answer);
strbuf_release(&s.buf);
strbuf_release(&s.plain);