add-patch: do not show UI messages on stderr

There is no need to show some UI messages on stderr, and yet doing so may produce some undesirable results, such as messages appearing in an unexpected order. Let's use stdout for all UI messages, and adjusts the tests accordingly. Signed-off-by: Rubén Justo <rjusto@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Rubén Justo committed Apr 29, 2024 at 20:37 UTC 9d225b025d96d37e8c914646c54cfa9fb5f52b80
2 files changed +12 -13
add-patch.c
+6 -7
@@ -293,10 +293,9 @@ static void err(struct add_p_state *s, const char *fmt, ...)
293 va_list args;
294
295 va_start(args, fmt);
296 - fputs(s->s.error_color, stderr);
297 - vfprintf(stderr, fmt, args);
298 - fputs(s->s.reset_color, stderr);
299 - fputc('\n', stderr);
296 + fputs(s->s.error_color, stdout);
297 + vprintf(fmt, args);
298 + puts(s->s.reset_color);
299 va_end(args);
300 }
301
@@ -1326,7 +1325,7 @@ static int apply_for_checkout(struct add_p_state *s, struct strbuf *diff,
1325 err(s, _("Nothing was applied.\n"));
1326 } else
1327 /* As a last resort, show the diff to the user */
1329 - fwrite(diff->buf, diff->len, 1, stderr);
1328 + fwrite(diff->buf, diff->len, 1, stdout);
1329
1330 return 0;
1331 }
@@ -1777,9 +1776,9 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
1776 break;
1777
1778 if (s.file_diff_nr == 0)
1780 - fprintf(stderr, _("No changes.\n"));
1779 + err(&s, _("No changes."));
1780 else if (binary_count == s.file_diff_nr)
1782 - fprintf(stderr, _("Only binary files changed.\n"));
1781 + err(&s, _("Only binary files changed."));
1782
1783 add_p_state_clear(&s);
1784 return 0;
t/t3701-add-interactive.sh
+6 -6
@@ -44,13 +44,13 @@ test_expect_success 'warn about add.interactive.useBuiltin' '
44 cat >expect <<-\EOF &&
45 warning: the add.interactive.useBuiltin setting has been removed!
46 See its entry in '\''git help config'\'' for details.
47 - No changes.
47 EOF
48 + echo "No changes." >expect.out &&
49
50 for v in = =true =false
51 do
52 git -c "add.interactive.useBuiltin$v" add -p >out 2>actual &&
53 - test_must_be_empty out &&
53 + test_cmp expect.out out &&
54 test_cmp expect actual || return 1
55 done
56 '
@@ -334,13 +334,13 @@ test_expect_success 'different prompts for mode change/deleted' '
334
335 test_expect_success 'correct message when there is nothing to do' '
336 git reset --hard &&
337 - git add -p 2>err &&
338 - test_grep "No changes" err &&
337 + git add -p >out &&
338 + test_grep "No changes" out &&
339 printf "\\0123" >binary &&
340 git add binary &&
341 printf "\\0abc" >binary &&
342 - git add -p 2>err &&
343 - test_grep "Only binary files changed" err
342 + git add -p >out &&
343 + test_grep "Only binary files changed" out
344 '
345
346 test_expect_success 'setup again' '