sequencer: add a new function to silence a command, except if it fails
This adds a new function, run_command_silent_on_success(), to redirect the stdout and stderr of a command to a strbuf, and then to run that command. This strbuf is printed only if the command fails. It is functionnaly similar to output() from git-rebase.sh. run_git_commit() is then refactored to use of run_command_silent_on_success(). Signed-off-by: Alban Gruin <alban.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Alban Gruin committed
Aug 10, 2018 at 18:51 UTC
34bec2c458474bdc05ced34d6789ee6c9fb7f051
1 file changed
+25
-26
sequencer.c
+25
-26
@@ -769,6 +769,23 @@ N_("you have staged changes in your working tree\n"
769
#define VERIFY_MSG (1<<4)
770
#define CREATE_ROOT_COMMIT (1<<5)
771
772
+static int run_command_silent_on_success(struct child_process *cmd)
773
+{
774
+ struct strbuf buf = STRBUF_INIT;
775
+ int rc;
776
+
777
+ cmd->stdout_to_stderr = 1;
778
+ rc = pipe_command(cmd,
779
+ NULL, 0,
780
+ NULL, 0,
781
+ &buf, 0);
782
+
783
+ if (rc)
784
+ fputs(buf.buf, stderr);
785
+ strbuf_release(&buf);
786
+ return rc;
787
+}
788
+
789
/*
790
* If we are cherry-pick, and if the merge did not result in
791
* hand-editing, we will hit this commit and inherit the original
@@ -823,18 +840,11 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts,
840
841
cmd.git_cmd = 1;
842
826
- if (is_rebase_i(opts)) {
827
- if (!(flags & EDIT_MSG)) {
828
- cmd.stdout_to_stderr = 1;
829
- cmd.err = -1;
830
- }
831
-
832
- if (read_env_script(&cmd.env_array)) {
833
- const char *gpg_opt = gpg_sign_opt_quoted(opts);
843
+ if (is_rebase_i(opts) && read_env_script(&cmd.env_array)) {
844
+ const char *gpg_opt = gpg_sign_opt_quoted(opts);
845
835
- return error(_(staged_changes_advice),
836
- gpg_opt, gpg_opt);
837
- }
846
+ return error(_(staged_changes_advice),
847
+ gpg_opt, gpg_opt);
848
}
849
850
argv_array_push(&cmd.args, "commit");
@@ -864,21 +874,10 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts,
874
if (opts->allow_empty_message)
875
argv_array_push(&cmd.args, "--allow-empty-message");
876
867
- if (cmd.err == -1) {
868
- /* hide stderr on success */
869
- struct strbuf buf = STRBUF_INIT;
870
- int rc = pipe_command(&cmd,
871
- NULL, 0,
872
- /* stdout is already redirected */
873
- NULL, 0,
874
- &buf, 0);
875
- if (rc)
876
- fputs(buf.buf, stderr);
877
- strbuf_release(&buf);
878
- return rc;
879
- }
880
-
881
- return run_command(&cmd);
877
+ if (is_rebase_i(opts) && !(flags & EDIT_MSG))
878
+ return run_command_silent_on_success(&cmd);
879
+ else
880
+ return run_command(&cmd);
881
}
882
883
static int rest_is_empty(const struct strbuf *sb, int start)