git.con commit gitweb: Cleanup - chomp @lines in consistent style (0881d2d)
   1#include <stdio.h>
   2#include <sys/types.h>
   3#include <sys/stat.h>
   4#include <dirent.h>
   5#include <unistd.h>
   6#include <stdlib.h>
   7#include <string.h>
   8#include <errno.h>
   9#include <limits.h>
  10#include <stdarg.h>
  11#include "git-compat-util.h"
  12#include "exec_cmd.h"
  13#include "cache.h"
  14#include "quote.h"
  15
  16#include "builtin.h"
  17
  18static void prepend_to_path(const char *dir, int len)
  19{
  20        const char *old_path = getenv("PATH");
  21        char *path;
  22        int path_len = len;
  23
  24        if (!old_path)
  25                old_path = "/usr/local/bin:/usr/bin:/bin";
  26
  27        path_len = len + strlen(old_path) + 1;
  28
  29        path = malloc(path_len + 1);
  30
  31        memcpy(path, dir, len);
  32        path[len] = ':';
  33        memcpy(path + len + 1, old_path, path_len - len);
  34
  35        setenv("PATH", path, 1);
  36}
  37
  38static int handle_options(const char*** argv, int* argc)
  39{
  40        int handled = 0;
  41
  42        while (*argc > 0) {
  43                const char *cmd = (*argv)[0];
  44                if (cmd[0] != '-')
  45                        break;
  46
  47                /*
  48                 * For legacy reasons, the "version" and "help"
  49                 * commands can be written with "--" prepended
  50                 * to make them look like flags.
  51                 */
  52                if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
  53                        break;
  54
  55                /*
  56                 * Check remaining flags.
  57                 */
  58                if (!strncmp(cmd, "--exec-path", 11)) {
  59                        cmd += 11;
  60                        if (*cmd == '=')
  61                                git_set_exec_path(cmd + 1);
  62                        else {
  63                                puts(git_exec_path());
  64                                exit(0);
  65                        }
  66                } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
  67                        setup_pager();
  68                } else if (!strcmp(cmd, "--git-dir")) {
  69                        if (*argc < 1)
  70                                return -1;
  71                        setenv("GIT_DIR", (*argv)[1], 1);
  72                        (*argv)++;
  73                        (*argc)--;
  74                } else if (!strncmp(cmd, "--git-dir=", 10)) {
  75                        setenv("GIT_DIR", cmd + 10, 1);
  76                } else if (!strcmp(cmd, "--bare")) {
  77                        static char git_dir[1024];
  78                        setenv("GIT_DIR", getcwd(git_dir, 1024), 1);
  79                } else {
  80                        fprintf(stderr, "Unknown option: %s\n", cmd);
  81                        cmd_usage(0, NULL, NULL);
  82                }
  83
  84                (*argv)++;
  85                (*argc)--;
  86                handled++;
  87        }
  88        return handled;
  89}
  90
  91static const char *alias_command;
  92static char *alias_string = NULL;
  93
  94static int git_alias_config(const char *var, const char *value)
  95{
  96        if (!strncmp(var, "alias.", 6) && !strcmp(var + 6, alias_command)) {
  97                alias_string = strdup(value);
  98        }
  99        return 0;
 100}
 101
 102static int split_cmdline(char *cmdline, const char ***argv)
 103{
 104        int src, dst, count = 0, size = 16;
 105        char quoted = 0;
 106
 107        *argv = malloc(sizeof(char*) * size);
 108
 109        /* split alias_string */
 110        (*argv)[count++] = cmdline;
 111        for (src = dst = 0; cmdline[src];) {
 112                char c = cmdline[src];
 113                if (!quoted && isspace(c)) {
 114                        cmdline[dst++] = 0;
 115                        while (cmdline[++src]
 116                                        && isspace(cmdline[src]))
 117                                ; /* skip */
 118                        if (count >= size) {
 119                                size += 16;
 120                                *argv = realloc(*argv, sizeof(char*) * size);
 121                        }
 122                        (*argv)[count++] = cmdline + dst;
 123                } else if(!quoted && (c == '\'' || c == '"')) {
 124                        quoted = c;
 125                        src++;
 126                } else if (c == quoted) {
 127                        quoted = 0;
 128                        src++;
 129                } else {
 130                        if (c == '\\' && quoted != '\'') {
 131                                src++;
 132                                c = cmdline[src];
 133                                if (!c) {
 134                                        free(*argv);
 135                                        *argv = NULL;
 136                                        return error("cmdline ends with \\");
 137                                }
 138                        }
 139                        cmdline[dst++] = c;
 140                        src++;
 141                }
 142        }
 143
 144        cmdline[dst] = 0;
 145
 146        if (quoted) {
 147                free(*argv);
 148                *argv = NULL;
 149                return error("unclosed quote");
 150        }
 151
 152        return count;
 153}
 154
 155static int handle_alias(int *argcp, const char ***argv)
 156{
 157        int nongit = 0, ret = 0, saved_errno = errno;
 158        const char *subdir;
 159        int count, option_count;
 160        const char** new_argv;
 161
 162        subdir = setup_git_directory_gently(&nongit);
 163
 164        alias_command = (*argv)[0];
 165        git_config(git_alias_config);
 166        if (alias_string) {
 167                count = split_cmdline(alias_string, &new_argv);
 168                option_count = handle_options(&new_argv, &count);
 169                memmove(new_argv - option_count, new_argv,
 170                                count * sizeof(char *));
 171                new_argv -= option_count;
 172
 173                if (count < 1)
 174                        die("empty alias for %s", alias_command);
 175
 176                if (!strcmp(alias_command, new_argv[0]))
 177                        die("recursive alias: %s", alias_command);
 178
 179                if (getenv("GIT_TRACE")) {
 180                        int i;
 181                        fprintf(stderr, "trace: alias expansion: %s =>",
 182                                alias_command);
 183                        for (i = 0; i < count; ++i) {
 184                                fputc(' ', stderr);
 185                                sq_quote_print(stderr, new_argv[i]);
 186                        }
 187                        fputc('\n', stderr);
 188                        fflush(stderr);
 189                }
 190
 191                new_argv = realloc(new_argv, sizeof(char*) *
 192                                   (count + *argcp + 1));
 193                /* insert after command name */
 194                memcpy(new_argv + count, *argv + 1, sizeof(char*) * *argcp);
 195                new_argv[count+*argcp] = NULL;
 196
 197                *argv = new_argv;
 198                *argcp += count - 1;
 199
 200                ret = 1;
 201        }
 202
 203        if (subdir)
 204                chdir(subdir);
 205
 206        errno = saved_errno;
 207
 208        return ret;
 209}
 210
 211const char git_version_string[] = GIT_VERSION;
 212
 213#define NEEDS_PREFIX 1
 214
 215static void handle_internal_command(int argc, const char **argv, char **envp)
 216{
 217        const char *cmd = argv[0];
 218        static struct cmd_struct {
 219                const char *cmd;
 220                int (*fn)(int, const char **, const char *);
 221                int prefix;
 222        } commands[] = {
 223                { "version", cmd_version },
 224                { "help", cmd_help },
 225                { "log", cmd_log, NEEDS_PREFIX },
 226                { "whatchanged", cmd_whatchanged, NEEDS_PREFIX },
 227                { "show", cmd_show, NEEDS_PREFIX },
 228                { "push", cmd_push },
 229                { "format-patch", cmd_format_patch, NEEDS_PREFIX },
 230                { "count-objects", cmd_count_objects },
 231                { "diff", cmd_diff, NEEDS_PREFIX },
 232                { "grep", cmd_grep, NEEDS_PREFIX },
 233                { "rm", cmd_rm, NEEDS_PREFIX },
 234                { "add", cmd_add, NEEDS_PREFIX },
 235                { "rev-list", cmd_rev_list, NEEDS_PREFIX },
 236                { "init-db", cmd_init_db },
 237                { "get-tar-commit-id", cmd_get_tar_commit_id },
 238                { "upload-tar", cmd_upload_tar },
 239                { "check-ref-format", cmd_check_ref_format },
 240                { "ls-files", cmd_ls_files, NEEDS_PREFIX },
 241                { "ls-tree", cmd_ls_tree, NEEDS_PREFIX },
 242                { "tar-tree", cmd_tar_tree, NEEDS_PREFIX },
 243                { "read-tree", cmd_read_tree, NEEDS_PREFIX },
 244                { "commit-tree", cmd_commit_tree, NEEDS_PREFIX },
 245                { "apply", cmd_apply },
 246                { "show-branch", cmd_show_branch, NEEDS_PREFIX },
 247                { "diff-files", cmd_diff_files, NEEDS_PREFIX },
 248                { "diff-index", cmd_diff_index, NEEDS_PREFIX },
 249                { "diff-stages", cmd_diff_stages, NEEDS_PREFIX },
 250                { "diff-tree", cmd_diff_tree, NEEDS_PREFIX },
 251                { "cat-file", cmd_cat_file, NEEDS_PREFIX },
 252                { "rev-parse", cmd_rev_parse, NEEDS_PREFIX },
 253                { "write-tree", cmd_write_tree, NEEDS_PREFIX },
 254                { "mailsplit", cmd_mailsplit },
 255                { "mailinfo", cmd_mailinfo },
 256                { "stripspace", cmd_stripspace },
 257                { "update-index", cmd_update_index, NEEDS_PREFIX },
 258                { "update-ref", cmd_update_ref, NEEDS_PREFIX },
 259                { "fmt-merge-msg", cmd_fmt_merge_msg, NEEDS_PREFIX },
 260                { "prune", cmd_prune, NEEDS_PREFIX },
 261                { "mv", cmd_mv, NEEDS_PREFIX },
 262        };
 263        int i;
 264
 265        /* Turn "git cmd --help" into "git help cmd" */
 266        if (argc > 1 && !strcmp(argv[1], "--help")) {
 267                argv[1] = argv[0];
 268                argv[0] = cmd = "help";
 269        }
 270
 271        for (i = 0; i < ARRAY_SIZE(commands); i++) {
 272                struct cmd_struct *p = commands+i;
 273                const char *prefix;
 274                if (strcmp(p->cmd, cmd))
 275                        continue;
 276
 277                prefix = NULL;
 278                if (p->prefix)
 279                        prefix = setup_git_directory();
 280                if (getenv("GIT_TRACE")) {
 281                        int i;
 282                        fprintf(stderr, "trace: built-in: git");
 283                        for (i = 0; i < argc; ++i) {
 284                                fputc(' ', stderr);
 285                                sq_quote_print(stderr, argv[i]);
 286                        }
 287                        putc('\n', stderr);
 288                        fflush(stderr);
 289                }
 290
 291                exit(p->fn(argc, argv, prefix));
 292        }
 293}
 294
 295int main(int argc, const char **argv, char **envp)
 296{
 297        const char *cmd = argv[0];
 298        char *slash = strrchr(cmd, '/');
 299        const char *exec_path = NULL;
 300        int done_alias = 0;
 301
 302        /*
 303         * Take the basename of argv[0] as the command
 304         * name, and the dirname as the default exec_path
 305         * if it's an absolute path and we don't have
 306         * anything better.
 307         */
 308        if (slash) {
 309                *slash++ = 0;
 310                if (*cmd == '/')
 311                        exec_path = cmd;
 312                cmd = slash;
 313        }
 314
 315        /*
 316         * "git-xxxx" is the same as "git xxxx", but we obviously:
 317         *
 318         *  - cannot take flags in between the "git" and the "xxxx".
 319         *  - cannot execute it externally (since it would just do
 320         *    the same thing over again)
 321         *
 322         * So we just directly call the internal command handler, and
 323         * die if that one cannot handle it.
 324         */
 325        if (!strncmp(cmd, "git-", 4)) {
 326                cmd += 4;
 327                argv[0] = cmd;
 328                handle_internal_command(argc, argv, envp);
 329                die("cannot handle %s internally", cmd);
 330        }
 331
 332        /* Look for flags.. */
 333        argv++;
 334        argc--;
 335        handle_options(&argv, &argc);
 336        if (argc > 0) {
 337                if (!strncmp(argv[0], "--", 2))
 338                        argv[0] += 2;
 339        } else {
 340                /* Default command: "help" */
 341                argv[0] = "help";
 342                argc = 1;
 343        }
 344        cmd = argv[0];
 345
 346        /*
 347         * We search for git commands in the following order:
 348         *  - git_exec_path()
 349         *  - the path of the "git" command if we could find it
 350         *    in $0
 351         *  - the regular PATH.
 352         */
 353        if (exec_path)
 354                prepend_to_path(exec_path, strlen(exec_path));
 355        exec_path = git_exec_path();
 356        prepend_to_path(exec_path, strlen(exec_path));
 357
 358        while (1) {
 359                /* See if it's an internal command */
 360                handle_internal_command(argc, argv, envp);
 361
 362                /* .. then try the external ones */
 363                execv_git_cmd(argv);
 364
 365                /* It could be an alias -- this works around the insanity
 366                 * of overriding "git log" with "git show" by having
 367                 * alias.log = show
 368                 */
 369                if (done_alias || !handle_alias(&argc, &argv))
 370                        break;
 371                done_alias = 1;
 372        }
 373
 374        if (errno == ENOENT)
 375                cmd_usage(0, exec_path, "'%s' is not a git-command", cmd);
 376
 377        fprintf(stderr, "Failed to run command '%s': %s\n",
 378                cmd, strerror(errno));
 379
 380        return 1;
 381}