git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] Don't ignore write failure from git-diff, git-log, etc.

From
Jim Meyering <jim@meyering.net>
Date
May 30, 2007, 07:12 UTC
Message-ID
<874pluss22.fsf@rho.meyering.net>
In-Reply-To
<7v646b5gw5.fsf@assigned-by-dhcp.cox.net>
Junio C Hamano <junkio@cox.net> wrote:
Show 18 quoted lines
>> -		exit(p->fn(argc, argv, prefix));
>> +		status = p->fn(argc, argv, prefix);
>> +
>> +		/* Close stdout if necessary, and diagnose any failure
>> +		   other than EPIPE.  */
>> +		if (fcntl(fileno (stdout), F_GETFD) >= 0) {
>> +			errno = 0;
>> +			if ((ferror(stdout) || fclose(stdout))
>> +			    && errno != EPIPE) {
>> +				if (errno == 0)
>> +					die("write failure on standard output");
>> +				else
>> +					die("write failure on standard output"
>> +					    ": %s", strerror(errno));
>> +			}
>
> This makes the final write failure trump the breakage p->fn()
> already diagnosed, doesn't it?

Yes. Are there circumstances in which a nonzero status from some cmd_* function would mean something so grave that you wouldn't also want to know that standard output is incomplete or corrupt (and possibly use a different exit status)? So far, after a quick and incomplete survey, I haven't found any.

However, if some git command is documented to exit with
status N for some listed values of N, e.g.,
    1 A happened
    2 B happened
    3 any other failure
then the above choice of dying with "die" would be wrong.
E.g. git-diff's --exit-code comes close:
       --exit-code
           Make the program exit with codes similar to diff(1). That is, it
           exits with 1 if there were differences and 0 means no differences.
but doesn't say how it handles errors.
[ OT: Perhaps that documentation should be changed to look more like diff's,
  so that it says there is a different exit code for the third
  case (some failure):
    $ diff --help|tail -3|head -1
    Exit status is 0 if inputs are the same, 1 if different, 2 if trouble.
]
> Maybe if (fcntrl(...) >=0 )
> should read if (!status && fcntrl(...) >= 0).

No, because then something like git-diff's --exit-code could hide a write error.

If you want to preserve the exit status, then it should be enough to call set_die_routine with a function that will work just like "die" but exit with a specified (status) value.

Previous: Junio C HamanoNext: Linus Torvalds
Message 16 of 29 in “Don't ignore write failure from git-diff, git-log, etc.”
  1. Don't ignore write failure from git-diff, git-log, etc.Jim Meyering, May 26, 2007
  2. Linus TorvaldsMay 26, 2007
  3. Junio C HamanoMay 26, 2007
  4. Nicolas PitreMay 27, 2007
  5. Jim MeyeringMay 27, 2007
  6. Linus TorvaldsMay 27, 2007
  7. Jim MeyeringMay 28, 2007
  8. Marco RoelandMay 28, 2007
  9. Jim MeyeringMay 28, 2007
  10. Marco RoelandMay 28, 2007
  11. Jim MeyeringMay 28, 2007
  12. Petr BaudisMay 28, 2007
  13. Junio C HamanoMay 28, 2007
  14. Jim MeyeringMay 29, 2007
  15. Junio C HamanoMay 29, 2007
  16. Jim MeyeringMay 30, 2007
  17. Linus TorvaldsMay 28, 2007
  18. Jim MeyeringMay 28, 2007
  19. Linus TorvaldsMay 29, 2007
  20. Jim MeyeringMay 29, 2007
  21. Linus TorvaldsMay 29, 2007
  22. Jim MeyeringMay 30, 2007
  23. Linus TorvaldsMay 30, 2007
  24. Jim MeyeringMay 30, 2007
  25. Junio C HamanoMay 28, 2007
  26. Linus TorvaldsMay 29, 2007
  27. Jim MeyeringMay 30, 2007
  28. Junio C HamanoMay 30, 2007
  29. Jim MeyeringMay 30, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.