Skip to content

Commit 34bec2c

Browse files
agrngitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 64a43cb commit 34bec2c

File tree

1 file changed

+25
-26
lines changed

1 file changed

+25
-26
lines changed

sequencer.c

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,23 @@ N_("you have staged changes in your working tree\n"
769769
#define VERIFY_MSG (1<<4)
770770
#define CREATE_ROOT_COMMIT (1<<5)
771771

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+
772789
/*
773790
* If we are cherry-pick, and if the merge did not result in
774791
* 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,
823840

824841
cmd.git_cmd = 1;
825842

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);
834845

835-
return error(_(staged_changes_advice),
836-
gpg_opt, gpg_opt);
837-
}
846+
return error(_(staged_changes_advice),
847+
gpg_opt, gpg_opt);
838848
}
839849

840850
argv_array_push(&cmd.args, "commit");
@@ -864,21 +874,10 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts,
864874
if (opts->allow_empty_message)
865875
argv_array_push(&cmd.args, "--allow-empty-message");
866876

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);
882881
}
883882

884883
static int rest_is_empty(const struct strbuf *sb, int start)

0 commit comments

Comments
 (0)