Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #11 from msysgit/hv/read_output_from_tools

always read stdout and stderr from forked commands

This fixes #2
  • Loading branch information...
commit a6c375976e057492088fe885705504cc58b92512 2 parents d8d0116 + f743394
@dscho dscho authored
Showing with 32 additions and 37 deletions.
  1. +32 −37 common/exec.c
View
69 common/exec.c
@@ -46,6 +46,12 @@ int exec_program_v(const char *working_directory,
int status = 0;
int ret;
+ struct strbuf strout = STRBUF_INIT, strerr = STRBUF_INIT;
+ if (!output)
+ output = &strout;
+ if (!error_output)
+ error_output = &strerr;
+
reporter *debug = QUIETMODE & flags ? _debug_git : _debug_git_mbox;
if (!git_path()) {
@@ -53,27 +59,23 @@ int exec_program_v(const char *working_directory,
return -1;
}
- if (output) {
- if (pipe(fdout) < 0) {
- return -ERR_RUN_COMMAND_PIPE;
- }
- s1 = dup(1);
- dup2(fdout[1], 1);
-
- flags |= WAITMODE;
+ if (pipe(fdout) < 0) {
+ return -ERR_RUN_COMMAND_PIPE;
}
+ s1 = dup(1);
+ dup2(fdout[1], 1);
- if (error_output) {
- if (pipe(fderr) < 0) {
- if (output)
- close_pair(fdout);
- return -ERR_RUN_COMMAND_PIPE;
- }
- s2 = dup(2);
- dup2(fderr[1], 2);
+ flags |= WAITMODE;
- flags |= WAITMODE;
+ if (pipe(fderr) < 0) {
+ if (output)
+ close_pair(fdout);
+ return -ERR_RUN_COMMAND_PIPE;
}
+ s2 = dup(2);
+ dup2(fderr[1], 2);
+
+ flags |= WAITMODE;
pid = fork_process(argv[0], argv, working_directory);
@@ -83,17 +85,13 @@ int exec_program_v(const char *working_directory,
dup2(s2, 2), close(s2);
if (pid < 0) {
- if (output)
- close_pair(fdout);
- if (error_output)
- close_pair(fderr);
+ close_pair(fdout);
+ close_pair(fderr);
return -ERR_RUN_COMMAND_FORK;
}
- if (output)
- close(fdout[1]);
- if (error_output)
- close(fderr[1]);
+ close(fdout[1]);
+ close(fderr[1]);
if (WAITMODE & flags) {
ret = wait_for_process(pid, MAX_PROCESSING_TIME,
@@ -109,15 +107,11 @@ int exec_program_v(const char *working_directory,
status = -1;
}
- if (output) {
- strbuf_read(output, fdout[0], 0);
- debug_git("STDOUT:\r\n%s\r\n*** end of STDOUT ***\r\n", output->buf);
- }
+ strbuf_read(output, fdout[0], 0);
+ debug_git("STDOUT:\r\n%s\r\n*** end of STDOUT ***\r\n", output->buf);
- if (error_output) {
- strbuf_read(error_output, fderr[0], 0);
- debug_git("STDERR:\r\n%s\r\n*** end of STDERR ***\r\n", error_output->buf);
- }
+ strbuf_read(error_output, fderr[0], 0);
+ debug_git("STDERR:\r\n%s\r\n*** end of STDERR ***\r\n", error_output->buf);
} else {
status = -ERR_RUN_COMMAND_WAITPID_NOEXIT;
debug_git("[ERROR] process timed out; "
@@ -127,10 +121,11 @@ int exec_program_v(const char *working_directory,
}
close_process(pid);
- if (output)
- close(fdout[0]);
- if (error_output)
- close(fderr[0]);
+ close(fdout[0]);
+ close(fderr[0]);
+
+ strbuf_release(&strerr);
+ strbuf_release(&strout);
return status;
}
Please sign in to comment.
Something went wrong with that request. Please try again.