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

[PATCH v3] shell: allow overriding built-in commands

From
Ayman Bagabas via GitGitGadget <gitgitgadget@gmail.com>
Date
Mar 23, 2025, 15:29 UTC
Message-ID
<pull.1930.v3.git.git.1742743771108.gitgitgadget@gmail.com>
In-Reply-To
<pull.1930.v2.git.git.1742688740650.gitgitgadget@gmail.com>
From: Ayman Bagabas <ayman.bagabas@gmail.com>

This patch allows overriding the shell built-in commands by placing a script with the same name under git-shell-commands directory.

This is useful for users who want to extend the shell built-in commands without replacing the original command binary. For instance, a user wanting to allow only a subset of users to run the git-receive-pack can override the command with a script that checks the user and calls the original command if the user is allowed.

Signed-off-by: Ayman Bagabas <ayman.bagabas@gmail.com>
---
    shell: allow overriding built-in commands
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1930%2Faymanbagabas%2Fshell-override-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1930/aymanbagabas/shell-override-v3
Pull-Request: https://github.com/git/git/pull/1930
Range-diff vs v2:
 1:  60c6339e790 ! 1:  7e6996d199e shell: allow overriding built-in commands
     @@ shell.c: int cmd_main(int argc, const char **argv)
       		}
      +		/* Allow overriding built-in commands */
      +		full_cmd = make_cmd(cmd->name);
     -+		if (!access(full_cmd, F_OK)) {
     ++		if (!access(full_cmd, X_OK)) {
      +			const char *argv[3] = { cmd->name, arg, NULL };
      +			return execv(full_cmd, (char *const *) argv);
      +		}
 shell.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/shell.c b/shell.c
index 76333c80686..8c7f4388bd5 100644
--- a/shell.c
+++ b/shell.c
@@ -194,9 +194,11 @@ int cmd_main(int argc, const char **argv)
 		/* Accept "git foo" as if the caller said "git-foo". */
 		prog[3] = '-';
 
+	cd_to_homedir();
 	for (cmd = cmd_list ; cmd->name ; cmd++) {
 		int len = strlen(cmd->name);
 		char *arg;
+		char *full_cmd;
 		if (strncmp(cmd->name, prog, len))
 			continue;
 		arg = NULL;
@@ -210,10 +212,15 @@ int cmd_main(int argc, const char **argv)
 		default:
 			continue;
 		}
+		/* Allow overriding built-in commands */
+		full_cmd = make_cmd(cmd->name);
+		if (!access(full_cmd, X_OK)) {
+			const char *argv[3] = { cmd->name, arg, NULL };
+			return execv(full_cmd, (char *const *) argv);
+		}
 		return cmd->exec(cmd->name, arg);
 	}
 
-	cd_to_homedir();
 	count = split_cmdline(prog, &user_argv);
 	if (count >= 0) {
 		if (is_valid_cmd_name(user_argv[0])) {

base-commit: 683c54c999c301c2cd6f715c411407c413b1d84e
-- 
gitgitgadget
Previous: Chris TorekNext: Jeff King
Message 9 of 13 in “[RFC] shell: allow overriding built-in commands”
  1. [RFC] shell: allow overriding built-in commandsAyman Bagabas via GitGitGadget, Mar 22, 2025
  2. Elijah NewrenMar 22, 2025
  3. Ayman BagabasMar 22, 2025
  4. Elijah NewrenMar 22, 2025
  5. shell: allow overriding built-in commandsAyman Bagabas via GitGitGadget, Mar 23, 2025
  6. Chris TorekMar 23, 2025
  7. Ayman BagabasMar 23, 2025
  8. Chris TorekMar 23, 2025
  9. shell: allow overriding built-in commandsAyman Bagabas via GitGitGadget, Mar 23, 2025
  10. Jeff KingMar 24, 2025
  11. Junio C HamanoMar 24, 2025
  12. Jeff KingMar 24, 2025
  13. Ayman BagabasMar 25, 2025

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.