]> git.ipfire.org Git - thirdparty/git.git/blobdiff - add-patch.c
Merge branch 'master' of github.com:Softcatala/git-po into git-po-master
[thirdparty/git.git] / add-patch.c
index 5e9829a8b42342c54ad15677f928d9b950df42ae..2c46fe5b3332bf844007ea0d17dee250f0ba5756 100644 (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;
 };
@@ -294,7 +294,9 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
                                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",
@@ -1008,8 +1010,10 @@ 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"
@@ -1246,9 +1250,31 @@ soft_increment:
                                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? */
@@ -1280,7 +1306,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);
 
@@ -1294,9 +1320,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);