shell.con commit replace_object: convert struct replace_object to object_id (1731a1e)
   1#include "cache.h"
   2#include "quote.h"
   3#include "exec_cmd.h"
   4#include "strbuf.h"
   5#include "run-command.h"
   6
   7#define COMMAND_DIR "git-shell-commands"
   8#define HELP_COMMAND COMMAND_DIR "/help"
   9#define NOLOGIN_COMMAND COMMAND_DIR "/no-interactive-login"
  10
  11static int do_generic_cmd(const char *me, char *arg)
  12{
  13        const char *my_argv[4];
  14
  15        setup_path();
  16        if (!arg || !(arg = sq_dequote(arg)) || *arg == '-')
  17                die("bad argument");
  18        if (!starts_with(me, "git-"))
  19                die("bad command");
  20
  21        my_argv[0] = me + 4;
  22        my_argv[1] = arg;
  23        my_argv[2] = NULL;
  24
  25        return execv_git_cmd(my_argv);
  26}
  27
  28static int is_valid_cmd_name(const char *cmd)
  29{
  30        /* Test command contains no . or / characters */
  31        return cmd[strcspn(cmd, "./")] == '\0';
  32}
  33
  34static char *make_cmd(const char *prog)
  35{
  36        return xstrfmt("%s/%s", COMMAND_DIR, prog);
  37}
  38
  39static void cd_to_homedir(void)
  40{
  41        const char *home = getenv("HOME");
  42        if (!home)
  43                die("could not determine user's home directory; HOME is unset");
  44        if (chdir(home) == -1)
  45                die("could not chdir to user's home directory");
  46}
  47
  48static void run_shell(void)
  49{
  50        int done = 0;
  51        static const char *help_argv[] = { HELP_COMMAND, NULL };
  52
  53        if (!access(NOLOGIN_COMMAND, F_OK)) {
  54                /* Interactive login disabled. */
  55                const char *argv[] = { NOLOGIN_COMMAND, NULL };
  56                int status;
  57
  58                status = run_command_v_opt(argv, 0);
  59                if (status < 0)
  60                        exit(127);
  61                exit(status);
  62        }
  63
  64        /* Print help if enabled */
  65        run_command_v_opt(help_argv, RUN_SILENT_EXEC_FAILURE);
  66
  67        do {
  68                struct strbuf line = STRBUF_INIT;
  69                const char *prog;
  70                char *full_cmd;
  71                char *rawargs;
  72                char *split_args;
  73                const char **argv;
  74                int code;
  75                int count;
  76
  77                fprintf(stderr, "git> ");
  78                if (strbuf_getline_lf(&line, stdin) == EOF) {
  79                        fprintf(stderr, "\n");
  80                        strbuf_release(&line);
  81                        break;
  82                }
  83                strbuf_trim(&line);
  84                rawargs = strbuf_detach(&line, NULL);
  85                split_args = xstrdup(rawargs);
  86                count = split_cmdline(split_args, &argv);
  87                if (count < 0) {
  88                        fprintf(stderr, "invalid command format '%s': %s\n", rawargs,
  89                                split_cmdline_strerror(count));
  90                        free(split_args);
  91                        free(rawargs);
  92                        continue;
  93                }
  94
  95                prog = argv[0];
  96                if (!strcmp(prog, "")) {
  97                } else if (!strcmp(prog, "quit") || !strcmp(prog, "logout") ||
  98                           !strcmp(prog, "exit") || !strcmp(prog, "bye")) {
  99                        done = 1;
 100                } else if (is_valid_cmd_name(prog)) {
 101                        full_cmd = make_cmd(prog);
 102                        argv[0] = full_cmd;
 103                        code = run_command_v_opt(argv, RUN_SILENT_EXEC_FAILURE);
 104                        if (code == -1 && errno == ENOENT) {
 105                                fprintf(stderr, "unrecognized command '%s'\n", prog);
 106                        }
 107                        free(full_cmd);
 108                } else {
 109                        fprintf(stderr, "invalid command format '%s'\n", prog);
 110                }
 111
 112                free(argv);
 113                free(rawargs);
 114        } while (!done);
 115}
 116
 117static struct commands {
 118        const char *name;
 119        int (*exec)(const char *me, char *arg);
 120} cmd_list[] = {
 121        { "git-receive-pack", do_generic_cmd },
 122        { "git-upload-pack", do_generic_cmd },
 123        { "git-upload-archive", do_generic_cmd },
 124        { NULL },
 125};
 126
 127int cmd_main(int argc, const char **argv)
 128{
 129        char *prog;
 130        const char **user_argv;
 131        struct commands *cmd;
 132        int count;
 133
 134        /*
 135         * Special hack to pretend to be a CVS server
 136         */
 137        if (argc == 2 && !strcmp(argv[1], "cvs server")) {
 138                argv--;
 139        } else if (argc == 1) {
 140                /* Allow the user to run an interactive shell */
 141                cd_to_homedir();
 142                if (access(COMMAND_DIR, R_OK | X_OK) == -1) {
 143                        die("Interactive git shell is not enabled.\n"
 144                            "hint: ~/" COMMAND_DIR " should exist "
 145                            "and have read and execute access.");
 146                }
 147                run_shell();
 148                exit(0);
 149        } else if (argc != 3 || strcmp(argv[1], "-c")) {
 150                /*
 151                 * We do not accept any other modes except "-c" followed by
 152                 * "cmd arg", where "cmd" is a very limited subset of git
 153                 * commands or a command in the COMMAND_DIR
 154                 */
 155                die("Run with no arguments or with -c cmd");
 156        }
 157
 158        prog = xstrdup(argv[2]);
 159        if (!strncmp(prog, "git", 3) && isspace(prog[3]))
 160                /* Accept "git foo" as if the caller said "git-foo". */
 161                prog[3] = '-';
 162
 163        for (cmd = cmd_list ; cmd->name ; cmd++) {
 164                int len = strlen(cmd->name);
 165                char *arg;
 166                if (strncmp(cmd->name, prog, len))
 167                        continue;
 168                arg = NULL;
 169                switch (prog[len]) {
 170                case '\0':
 171                        arg = NULL;
 172                        break;
 173                case ' ':
 174                        arg = prog + len + 1;
 175                        break;
 176                default:
 177                        continue;
 178                }
 179                exit(cmd->exec(cmd->name, arg));
 180        }
 181
 182        cd_to_homedir();
 183        count = split_cmdline(prog, &user_argv);
 184        if (count >= 0) {
 185                if (is_valid_cmd_name(user_argv[0])) {
 186                        prog = make_cmd(user_argv[0]);
 187                        user_argv[0] = prog;
 188                        execv(user_argv[0], (char *const *) user_argv);
 189                }
 190                free(prog);
 191                free(user_argv);
 192                die("unrecognized command '%s'", argv[2]);
 193        } else {
 194                free(prog);
 195                die("invalid command format '%s': %s", argv[2],
 196                    split_cmdline_strerror(count));
 197        }
 198}