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