Skip to content

Commit 60c6339

Browse files
committed
shell: allow overriding built-in commands
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 <[email protected]>
1 parent 683c54c commit 60c6339

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

shell.c

Lines changed: 8 additions & 1 deletion
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,15 @@ int cmd_main(int argc, const char **argv)
210212
default:
211213
continue;
212214
}
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+
}
213221
return cmd->exec(cmd->name, arg);
214222
}
215223

216-
cd_to_homedir();
217224
count = split_cmdline(prog, &user_argv);
218225
if (count >= 0) {
219226
if (is_valid_cmd_name(user_argv[0])) {

0 commit comments

Comments
 (0)