blob: e4864e04da3b0e237342c2ca0548c0ec0082c171 [file] [log] [blame]
Linus Torvalds35eb2d32005-10-23 14:30:45 -07001#include "cache.h"
2#include "quote.h"
Michal Ostrowski77cb17e2006-01-10 21:12:17 -05003#include "exec_cmd.h"
Johannes Schindelin0c696fe2007-10-09 15:33:25 +01004#include "strbuf.h"
Linus Torvalds35eb2d32005-10-23 14:30:45 -07005
6static int do_generic_cmd(const char *me, char *arg)
7{
8 const char *my_argv[4];
9
Johannes Sixte1464ca2008-07-21 21:19:52 +020010 setup_path();
Junio C Hamanoab5f8622005-11-25 20:57:02 -080011 if (!arg || !(arg = sq_dequote(arg)))
Linus Torvalds35eb2d32005-10-23 14:30:45 -070012 die("bad argument");
Junio C Hamanocc44c762007-02-20 01:53:29 -080013 if (prefixcmp(me, "git-"))
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050014 die("bad command");
Linus Torvalds35eb2d32005-10-23 14:30:45 -070015
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050016 my_argv[0] = me + 4;
Linus Torvalds35eb2d32005-10-23 14:30:45 -070017 my_argv[1] = arg;
18 my_argv[2] = NULL;
19
Junio C Hamano9201c702006-03-05 02:47:29 -080020 return execv_git_cmd(my_argv);
Linus Torvalds35eb2d32005-10-23 14:30:45 -070021}
22
Johannes Schindelin0c696fe2007-10-09 15:33:25 +010023static int do_cvs_cmd(const char *me, char *arg)
24{
25 const char *cvsserver_argv[3] = {
26 "cvsserver", "server", NULL
27 };
Johannes Schindelin0c696fe2007-10-09 15:33:25 +010028
29 if (!arg || strcmp(arg, "server"))
30 die("git-cvsserver only handles server: %s", arg);
31
Johannes Sixte1464ca2008-07-21 21:19:52 +020032 setup_path();
Johannes Schindelin0c696fe2007-10-09 15:33:25 +010033 return execv_git_cmd(cvsserver_argv);
34}
35
36
Linus Torvalds35eb2d32005-10-23 14:30:45 -070037static struct commands {
38 const char *name;
39 int (*exec)(const char *me, char *arg);
40} cmd_list[] = {
41 { "git-receive-pack", do_generic_cmd },
42 { "git-upload-pack", do_generic_cmd },
Erik Broes79f72b92009-04-09 21:58:52 +020043 { "git-upload-archive", do_generic_cmd },
Johannes Schindelin0c696fe2007-10-09 15:33:25 +010044 { "cvs", do_cvs_cmd },
Linus Torvalds35eb2d32005-10-23 14:30:45 -070045 { NULL },
46};
47
Junio C Hamano1e7abc52008-08-25 22:39:17 -070048int main(int argc, char **argv)
Linus Torvalds35eb2d32005-10-23 14:30:45 -070049{
50 char *prog;
51 struct commands *cmd;
Paolo Bonzini0cfeed22008-08-27 17:20:35 +020052 int devnull_fd;
53
54 /*
55 * Always open file descriptors 0/1/2 to avoid clobbering files
56 * in die(). It also avoids not messing up when the pipes are
57 * dup'ed onto stdin/stdout/stderr in the child processes we spawn.
58 */
59 devnull_fd = open("/dev/null", O_RDWR);
60 while (devnull_fd >= 0 && devnull_fd <= 2)
61 devnull_fd = dup(devnull_fd);
62 if (devnull_fd == -1)
Thomas Rastd824cbb2009-06-27 17:58:46 +020063 die_errno("opening /dev/null failed");
Paolo Bonzini0cfeed22008-08-27 17:20:35 +020064 close (devnull_fd);
Linus Torvalds35eb2d32005-10-23 14:30:45 -070065
Junio C Hamanobc7c73e2007-12-01 22:16:19 -080066 /*
67 * Special hack to pretend to be a CVS server
68 */
Johannes Schindelin0c696fe2007-10-09 15:33:25 +010069 if (argc == 2 && !strcmp(argv[1], "cvs server"))
70 argv--;
Junio C Hamanobc7c73e2007-12-01 22:16:19 -080071
72 /*
73 * We do not accept anything but "-c" followed by "cmd arg",
74 * where "cmd" is a very limited subset of git commands.
75 */
Johannes Schindelin0c696fe2007-10-09 15:33:25 +010076 else if (argc != 3 || strcmp(argv[1], "-c"))
Linus Torvalds35eb2d32005-10-23 14:30:45 -070077 die("What do you think I am? A shell?");
78
Junio C Hamano1e7abc52008-08-25 22:39:17 -070079 prog = argv[2];
Junio C Hamanobc7c73e2007-12-01 22:16:19 -080080 if (!strncmp(prog, "git", 3) && isspace(prog[3]))
81 /* Accept "git foo" as if the caller said "git-foo". */
82 prog[3] = '-';
83
Linus Torvalds35eb2d32005-10-23 14:30:45 -070084 for (cmd = cmd_list ; cmd->name ; cmd++) {
85 int len = strlen(cmd->name);
86 char *arg;
87 if (strncmp(cmd->name, prog, len))
88 continue;
89 arg = NULL;
90 switch (prog[len]) {
91 case '\0':
92 arg = NULL;
93 break;
94 case ' ':
95 arg = prog + len + 1;
96 break;
97 default:
98 continue;
99 }
100 exit(cmd->exec(cmd->name, arg));
101 }
102 die("unrecognized command '%s'", prog);
103}