};
static const char *prompt_mode[] = {
- N_("Stage mode change [y,n,a,d%s,?]? "),
- N_("Stage deletion [y,n,a,d%s,?]? "),
- N_("Stage this hunk [y,n,a,d%s,?]? ")
+ N_("Stage mode change [y,n,a,q,d%s,?]? "),
+ N_("Stage deletion [y,n,a,q,d%s,?]? "),
+ N_("Stage this hunk [y,n,a,q,d%s,?]? ")
};
struct hunk_header {
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;
};
BUG("'new mode' does not immediately follow "
"'old mode'?\n\n%.*s",
(int)(eol - plain->buf), plain->buf);
- }
+ } 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",
}
}
+#define SUMMARY_HEADER_WIDTH 20
+#define SUMMARY_LINE_WIDTH 80
+static void summarize_hunk(struct add_p_state *s, struct hunk *hunk,
+ struct strbuf *out)
+{
+ struct hunk_header *header = &hunk->header;
+ struct strbuf *plain = &s->plain;
+ size_t len = out->len, i;
+
+ strbuf_addf(out, " -%lu,%lu +%lu,%lu ",
+ header->old_offset, header->old_count,
+ header->new_offset, header->new_count);
+ if (out->len - len < SUMMARY_HEADER_WIDTH)
+ strbuf_addchars(out, ' ',
+ SUMMARY_HEADER_WIDTH + len - out->len);
+ for (i = hunk->start; i < hunk->end; i = find_next_line(plain, i))
+ if (plain->buf[i] != ' ')
+ break;
+ if (i < hunk->end)
+ strbuf_add(out, plain->buf + i, find_next_line(plain, i) - i);
+ if (out->len - len > SUMMARY_LINE_WIDTH)
+ strbuf_setlen(out, len + SUMMARY_LINE_WIDTH);
+ strbuf_complete_line(out);
+}
+
+#define DISPLAY_HUNKS_LINES 20
+static size_t display_hunks(struct add_p_state *s,
+ struct file_diff *file_diff, size_t start_index)
+{
+ size_t end_index = start_index + DISPLAY_HUNKS_LINES;
+
+ if (end_index > file_diff->hunk_nr)
+ end_index = file_diff->hunk_nr;
+
+ while (start_index < end_index) {
+ struct hunk *hunk = file_diff->hunk + start_index++;
+
+ strbuf_reset(&s->buf);
+ strbuf_addf(&s->buf, "%c%2d: ", hunk->use == USE_HUNK ? '+'
+ : hunk->use == SKIP_HUNK ? '-' : ' ',
+ (int)start_index);
+ summarize_hunk(s, hunk, &s->buf);
+ fputs(s->buf.buf, stdout);
+ }
+
+ return end_index;
+}
+
static const char help_patch_text[] =
N_("y - stage this hunk\n"
"n - do not stage this hunk\n"
+ "q - quit; do not stage this hunk or any of the remaining ones\n"
"a - stage this and all the remaining hunks\n"
- "d - do not stage this hunk nor any of the remaining hunks\n"
- "j - leave this hunk undecided, see next undecided hunk\n"
+ "d - do not stage this hunk nor any of the remaining hunks\n");
+
+static const char help_patch_remainder[] =
+N_("j - leave this hunk undecided, see next undecided hunk\n"
"J - leave this hunk undecided, see next hunk\n"
"k - leave this hunk undecided, see previous undecided hunk\n"
"K - leave this hunk undecided, see previous hunk\n"
+ "g - select a hunk to go to\n"
+ "/ - search for a hunk matching the given regex\n"
"s - split the current hunk into smaller hunks\n"
"e - manually edit the current hunk\n"
"? - print help\n");
struct hunk *hunk;
char ch;
struct child_process cp = CHILD_PROCESS_INIT;
- int colored = !!s->colored.len;
+ int colored = !!s->colored.len, quit = 0;
enum prompt_mode_type prompt_mode_type;
if (!file_diff->hunk_nr)
strbuf_addstr(&s->buf, ",j");
if (hunk_index + 1 < file_diff->hunk_nr)
strbuf_addstr(&s->buf, ",J");
+ if (file_diff->hunk_nr > 1)
+ strbuf_addstr(&s->buf, ",g,/");
if (hunk->splittable_into > 1)
strbuf_addstr(&s->buf, ",s");
if (hunk_index + 1 > file_diff->mode_change &&
if (hunk->use == UNDECIDED_HUNK)
hunk->use = USE_HUNK;
}
- } else if (ch == 'd') {
+ } else if (ch == 'd' || ch == 'q') {
for (; hunk_index < file_diff->hunk_nr; hunk_index++) {
hunk = file_diff->hunk + hunk_index;
if (hunk->use == UNDECIDED_HUNK)
hunk->use = SKIP_HUNK;
}
+ if (ch == 'q') {
+ quit = 1;
+ break;
+ }
} else if (s->answer.buf[0] == 'K') {
if (hunk_index)
hunk_index--;
hunk_index = undecided_next;
else
err(s, _("No next hunk"));
+ } else if (s->answer.buf[0] == 'g') {
+ char *pend;
+ unsigned long response;
+
+ if (file_diff->hunk_nr < 2) {
+ err(s, _("No other hunks to goto"));
+ continue;
+ }
+ strbuf_remove(&s->answer, 0, 1);
+ strbuf_trim(&s->answer);
+ i = hunk_index - DISPLAY_HUNKS_LINES / 2;
+ if (i < file_diff->mode_change)
+ i = file_diff->mode_change;
+ while (s->answer.len == 0) {
+ i = display_hunks(s, file_diff, i);
+ printf("%s", i < file_diff->hunk_nr ?
+ _("go to which hunk (<ret> to see "
+ "more)? ") : _("go to which hunk? "));
+ fflush(stdout);
+ if (strbuf_getline(&s->answer,
+ stdin) == EOF)
+ break;
+ strbuf_trim_trailing_newline(&s->answer);
+ }
+
+ strbuf_trim(&s->answer);
+ response = strtoul(s->answer.buf, &pend, 10);
+ if (*pend || pend == s->answer.buf)
+ err(s, _("Invalid number: '%s'"),
+ s->answer.buf);
+ else if (0 < response && response <= file_diff->hunk_nr)
+ hunk_index = response - 1;
+ else
+ err(s, Q_("Sorry, only %d hunk available.",
+ "Sorry, only %d hunks available.",
+ file_diff->hunk_nr),
+ (int)file_diff->hunk_nr);
+ } else if (s->answer.buf[0] == '/') {
+ regex_t regex;
+ int ret;
+
+ if (file_diff->hunk_nr < 2) {
+ err(s, _("No other hunks to search"));
+ continue;
+ }
+ strbuf_remove(&s->answer, 0, 1);
+ strbuf_trim_trailing_newline(&s->answer);
+ if (s->answer.len == 0) {
+ printf("%s", _("search for regex? "));
+ fflush(stdout);
+ if (strbuf_getline(&s->answer,
+ stdin) == EOF)
+ break;
+ strbuf_trim_trailing_newline(&s->answer);
+ if (s->answer.len == 0)
+ continue;
+ }
+ ret = regcomp(®ex, s->answer.buf,
+ REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
+ if (ret) {
+ char errbuf[1024];
+
+ regerror(ret, ®ex, errbuf, sizeof(errbuf));
+ err(s, _("Malformed search regexp %s: %s"),
+ s->answer.buf, errbuf);
+ continue;
+ }
+ i = hunk_index;
+ for (;;) {
+ /* render the hunk into a scratch buffer */
+ render_hunk(s, file_diff->hunk + i, 0, 0,
+ &s->buf);
+ if (regexec(®ex, s->buf.buf, 0, NULL, 0)
+ != REG_NOMATCH)
+ break;
+ i++;
+ if (i == file_diff->hunk_nr)
+ i = 0;
+ if (i != hunk_index)
+ continue;
+ err(s, _("No hunk matches the given pattern"));
+ break;
+ }
+ hunk_index = i;
} else if (s->answer.buf[0] == 's') {
size_t splittable_into = hunk->splittable_into;
if (splittable_into < 2)
hunk->use = USE_HUNK;
goto soft_increment;
}
- } else
- color_fprintf(stdout, s->s.help_color,
+ } else {
+ const char *p = _(help_patch_remainder), *eol = p;
+
+ color_fprintf(stdout, s->s.help_color, "%s",
_(help_patch_text));
+
+ /*
+ * Show only those lines of the remainder that are
+ * actually applicable with the current hunk.
+ */
+ for (; *p; p = eol + (*eol == '\n')) {
+ eol = strchrnul(p, '\n');
+
+ /*
+ * `s->buf` still contains the part of the
+ * commands shown in the prompt that are not
+ * always available.
+ */
+ if (*p != '?' && !strchr(s->buf.buf, *p))
+ continue;
+
+ color_fprintf_ln(stdout, s->s.help_color,
+ "%.*s", (int)(eol - p), p);
+ }
+ }
}
/* Any hunk to be used? */
}
putchar('\n');
- return 0;
+ return quit;
}
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);
}
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);