]> git.ipfire.org Git - thirdparty/git.git/commitdiff
commit: avoid redundant scissor line with --cleanup=scissors -v
authorJosh Triplett <josh@joshtriplett.org>
Tue, 27 Feb 2024 09:16:09 +0000 (01:16 -0800)
committerJunio C Hamano <gitster@pobox.com>
Tue, 27 Feb 2024 17:40:46 +0000 (09:40 -0800)
`git commit --cleanup=scissors -v` prints two scissors lines:
one at the start of the comment lines, and the other right before the
diff. This is redundant, and pushes the diff further down in the user's
editor than it needs to be.

Make wt_status_add_cut_line() remember if it has added a cut line before,
and avoid adding a redundant one.

Add a test for this.

Signed-off-by: Josh Triplett <josh@joshtriplett.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/commit.c
t/t7502-commit-porcelain.sh
wt-status.c
wt-status.h

index 65196a28278bd5afb1bbc1e68864376225868abe..7860af552ea5f31f2571cb94bbe771840b1f92be 100644 (file)
@@ -926,7 +926,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
                if (whence != FROM_COMMIT) {
                        if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS &&
                                !merge_contains_scissors)
-                               wt_status_add_cut_line(s->fp);
+                               wt_status_add_cut_line(s);
                        status_printf_ln(
                                s, GIT_COLOR_NORMAL,
                                whence == FROM_MERGE ?
@@ -947,7 +947,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
                        status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_all, comment_line_char);
                else if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
                        if (whence == FROM_COMMIT && !merge_contains_scissors)
-                               wt_status_add_cut_line(s->fp);
+                               wt_status_add_cut_line(s);
                } else /* COMMIT_MSG_CLEANUP_SPACE, that is. */
                        status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_space, comment_line_char);
 
index a87c211d0b16d0ffc4f8c7c2d6366209af1198f5..b37e2018a74a7b28725fa277a95cca516daf5cbe 100755 (executable)
@@ -736,6 +736,11 @@ test_expect_success 'message shows date when it is explicitly set' '
          .git/COMMIT_EDITMSG
 '
 
+test_expect_success 'message does not have multiple scissors lines' '
+       git commit --cleanup=scissors -v --allow-empty -e -m foo &&
+       test $(grep -c -e "--- >8 ---" .git/COMMIT_EDITMSG) -eq 1
+'
+
 test_expect_success AUTOIDENT 'message shows committer when it is automatic' '
 
        echo >>negative &&
index 9e8c08003b990c44ca17dc1e778bf572eb694786..b17ca58f8bf8256aaab93ce57d0c8959495ca55d 100644 (file)
@@ -1107,12 +1107,15 @@ void wt_status_append_cut_line(struct strbuf *buf)
        strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_char);
 }
 
-void wt_status_add_cut_line(FILE *fp)
+void wt_status_add_cut_line(struct wt_status *s)
 {
        struct strbuf buf = STRBUF_INIT;
 
+       if (s->added_cut_line)
+               return;
+       s->added_cut_line = 1;
        wt_status_append_cut_line(&buf);
-       fputs(buf.buf, fp);
+       fputs(buf.buf, s->fp);
        strbuf_release(&buf);
 }
 
@@ -1143,11 +1146,12 @@ static void wt_longstatus_print_verbose(struct wt_status *s)
         * file (and even the "auto" setting won't work, since it
         * will have checked isatty on stdout). But we then do want
         * to insert the scissor line here to reliably remove the
-        * diff before committing.
+        * diff before committing, if we didn't already include one
+        * before.
         */
        if (s->fp != stdout) {
                rev.diffopt.use_color = 0;
-               wt_status_add_cut_line(s->fp);
+               wt_status_add_cut_line(s);
        }
        if (s->verbose > 1 && s->committable) {
                /* print_updated() printed a header, so do we */
index 819dcad72300c56900ac05f3943d27dd2b9ed7a4..5e99ba47073493aa4ee80a9501dd4375ec903d8f 100644 (file)
@@ -130,6 +130,7 @@ struct wt_status {
        int rename_score;
        int rename_limit;
        enum wt_status_format status_format;
+       unsigned char added_cut_line; /* boolean */
        struct wt_status_state state;
        struct object_id oid_commit; /* when not Initial */
 
@@ -147,7 +148,7 @@ struct wt_status {
 
 size_t wt_status_locate_end(const char *s, size_t len);
 void wt_status_append_cut_line(struct strbuf *buf);
-void wt_status_add_cut_line(FILE *fp);
+void wt_status_add_cut_line(struct wt_status *s);
 void wt_status_prepare(struct repository *r, struct wt_status *s);
 void wt_status_print(struct wt_status *s);
 void wt_status_collect(struct wt_status *s);