Skip to content

Commit 4fe1887

Browse files
committed
[RFC] shell: allow overriding built-in commands
This patch allows overriding 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 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. CC: Junio C Hamano <[email protected]>, Jeff King <[email protected]> CC: Taylor Blau <[email protected]> Signed-off-by: Ayman Bagabas <[email protected]>
1 parent 683c54c commit 4fe1887

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

shell.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,11 @@ int cmd_main(int argc, const char **argv)
194194
/* Accept "git foo" as if the caller said "git-foo". */
195195
prog[3] = '-';
196196

197+
cd_to_homedir();
197198
for (cmd = cmd_list ; cmd->name ; cmd++) {
198199
int len = strlen(cmd->name);
199200
char *arg;
201+
char *full_cmd;
200202
if (strncmp(cmd->name, prog, len))
201203
continue;
202204
arg = NULL;
@@ -210,10 +212,16 @@ int cmd_main(int argc, const char **argv)
210212
default:
211213
continue;
212214
}
213-
return cmd->exec(cmd->name, arg);
215+
/* Allow overriding built-in commands */
216+
full_cmd = make_cmd(cmd->name);
217+
if (!access(full_cmd, F_OK)) {
218+
const char *argv[3] = { cmd->name, arg, NULL };
219+
return execv(full_cmd, (char *const *) argv);
220+
} else {
221+
return cmd->exec(cmd->name, arg);
222+
}
214223
}
215224

216-
cd_to_homedir();
217225
count = split_cmdline(prog, &user_argv);
218226
if (count >= 0) {
219227
if (is_valid_cmd_name(user_argv[0])) {

0 commit comments

Comments
 (0)