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

[PATCH 2/2] run-command: report exec error even on ENOENT

From
René Scharfe <l.s.r@web.de>
Date
Jun 10, 2023, 14:51 UTC
Message-ID
<753d6b82-580d-bcff-c0c2-e7ad53f13b21@web.de>
In-Reply-To
<xmqqwn0h4ek3.fsf@gitster.g>

If execve(2) fails with ENOENT and we report the error, we use the format "cannot run %s", followed by the actual error message. For other errors we use "cannot exec '%s'".

Stop making this subtle distinction and use the second format for all execve(2) errors. This simplifies the code and makes the prefix more precise by indicating the failed operation. It also allows us to slightly simplify t1800.16.

On Windows -- which lacks execve(2) -- we already use a single format in all cases: "cannot spawn %s".

Signed-off-by: René Scharfe <l.s.r@web.de>
---
Patch formatted with -U6 for easier review.
 run-command.c   | 14 +++-----------
 t/t1800-hook.sh |  2 +-
 2 files changed, 4 insertions(+), 12 deletions(-)
diff --git a/run-command.c b/run-command.c
index 60c9419866..758f8534da 100644
--- a/run-command.c
+++ b/run-command.c
@@ -304,13 +304,12 @@ static int child_notifier = -1;

 enum child_errcode {
 	CHILD_ERR_CHDIR,
 	CHILD_ERR_DUP2,
 	CHILD_ERR_CLOSE,
 	CHILD_ERR_SIGPROCMASK,
-	CHILD_ERR_ENOENT,
 	CHILD_ERR_SILENT,
 	CHILD_ERR_ERRNO
 };

 struct child_err {
 	enum child_errcode err;
@@ -387,15 +386,12 @@ static void child_err_spew(struct child_process *cmd, struct child_err *cerr)
 	case CHILD_ERR_CLOSE:
 		error_errno("close() in child failed");
 		break;
 	case CHILD_ERR_SIGPROCMASK:
 		error_errno("sigprocmask failed restoring signals");
 		break;
-	case CHILD_ERR_ENOENT:
-		error_errno("cannot run %s", cmd->args.v[0]);
-		break;
 	case CHILD_ERR_SILENT:
 		break;
 	case CHILD_ERR_ERRNO:
 		error_errno("cannot exec '%s'", cmd->args.v[0]);
 		break;
 	}
@@ -843,19 +839,15 @@ int start_command(struct child_process *cmd)
 		execve(argv.v[1], (char *const *) argv.v + 1,
 		       (char *const *) childenv);
 		if (errno == ENOEXEC)
 			execve(argv.v[0], (char *const *) argv.v,
 			       (char *const *) childenv);

-		if (errno == ENOENT) {
-			if (cmd->silent_exec_failure)
-				child_die(CHILD_ERR_SILENT);
-			child_die(CHILD_ERR_ENOENT);
-		} else {
-			child_die(CHILD_ERR_ERRNO);
-		}
+		if (cmd->silent_exec_failure && errno == ENOENT)
+			child_die(CHILD_ERR_SILENT);
+		child_die(CHILD_ERR_ERRNO);
 	}
 	atfork_parent(&as);
 	if (cmd->pid < 0)
 		error_errno("cannot fork() for %s", cmd->args.v[0]);
 	else if (cmd->clean_on_exit)
 		mark_child_for_cleanup(cmd->pid, cmd);
diff --git a/t/t1800-hook.sh b/t/t1800-hook.sh
index c156d6decc..8b0234cf2d 100755
--- a/t/t1800-hook.sh
+++ b/t/t1800-hook.sh
@@ -161,13 +161,13 @@ test_expect_success 'git hook run a hook with a bad shebang' '
 		hook run test-hook >out 2>err &&
 	test_must_be_empty out &&

 	# TODO: We should emit the same (or at least a more similar)
 	# error on MINGW (essentially Git for Windows) and all other
 	# platforms.. See the OS-specific code in start_command()
-	grep -E "^(error|fatal): cannot (exec|run|spawn) .*bad-hooks/test-hook" err
+	grep -E "^(error|fatal): cannot (exec|spawn) .*bad-hooks/test-hook" err
 '

 test_expect_success 'stdin to hooks' '
 	write_script .git/hooks/test-hook <<-\EOF &&
 	echo BEGIN stdin
 	cat
--
2.41.0
Previous: Junio C Hamano
Message 13 of 13 in “[BUG] Git 2.28.0-rc1 t1800 message text comparison”
  1. rsbecker@nexbridge.comSep 22, 2022
  2. rsbecker@nexbridge.comSep 22, 2022
  3. rsbecker@nexbridge.comSep 23, 2022
  4. rsbecker@nexbridge.comDec 14, 2022
  5. René ScharfeMay 22, 2023
  6. rsbecker@nexbridge.comMay 22, 2023
  7. rsbecker@nexbridge.comMay 22, 2023
  8. René ScharfeJun 4, 2023
  9. René ScharfeJun 4, 2023
  10. Junio C HamanoJun 6, 2023
  11. 1/2 t1800: loosen matching of error message for bad shebangRené Scharfe, Jun 10, 2023
  12. Junio C HamanoJun 12, 2023
  13. 2/2 run-command: report exec error even on ENOENTRené Scharfe, Jun 10, 2023

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.