Record the command invocation path early
[git/git.git] / shell.c
1 #include "cache.h"
2 #include "quote.h"
3 #include "exec_cmd.h"
4 #include "strbuf.h"
5
6 /* Stubs for functions that make no sense for git-shell. These stubs
7 * are provided here to avoid linking in external redundant modules.
8 */
9 void release_pack_memory(size_t need, int fd){}
10 void trace_argv_printf(const char **argv, const char *fmt, ...){}
11 void trace_printf(const char *fmt, ...){}
12
13
14 static int do_generic_cmd(const char *me, char *arg)
15 {
16 const char *my_argv[4];
17
18 setup_path();
19 if (!arg || !(arg = sq_dequote(arg)))
20 die("bad argument");
21 if (prefixcmp(me, "git-"))
22 die("bad command");
23
24 my_argv[0] = me + 4;
25 my_argv[1] = arg;
26 my_argv[2] = NULL;
27
28 return execv_git_cmd(my_argv);
29 }
30
31 static int do_cvs_cmd(const char *me, char *arg)
32 {
33 const char *cvsserver_argv[3] = {
34 "cvsserver", "server", NULL
35 };
36
37 if (!arg || strcmp(arg, "server"))
38 die("git-cvsserver only handles server: %s", arg);
39
40 setup_path();
41 return execv_git_cmd(cvsserver_argv);
42 }
43
44
45 static struct commands {
46 const char *name;
47 int (*exec)(const char *me, char *arg);
48 } cmd_list[] = {
49 { "git-receive-pack", do_generic_cmd },
50 { "git-upload-pack", do_generic_cmd },
51 { "cvs", do_cvs_cmd },
52 { NULL },
53 };
54
55 int main(int argc, char **argv)
56 {
57 char *prog;
58 struct commands *cmd;
59
60 /*
61 * Special hack to pretend to be a CVS server
62 */
63 if (argc == 2 && !strcmp(argv[1], "cvs server"))
64 argv--;
65
66 /*
67 * We do not accept anything but "-c" followed by "cmd arg",
68 * where "cmd" is a very limited subset of git commands.
69 */
70 else if (argc != 3 || strcmp(argv[1], "-c"))
71 die("What do you think I am? A shell?");
72
73 prog = argv[2];
74 if (!strncmp(prog, "git", 3) && isspace(prog[3]))
75 /* Accept "git foo" as if the caller said "git-foo". */
76 prog[3] = '-';
77
78 for (cmd = cmd_list ; cmd->name ; cmd++) {
79 int len = strlen(cmd->name);
80 char *arg;
81 if (strncmp(cmd->name, prog, len))
82 continue;
83 arg = NULL;
84 switch (prog[len]) {
85 case '\0':
86 arg = NULL;
87 break;
88 case ' ':
89 arg = prog + len + 1;
90 break;
91 default:
92 continue;
93 }
94 exit(cmd->exec(cmd->name, arg));
95 }
96 die("unrecognized command '%s'", prog);
97 }