receive-pack.con commit Teach git-add and git-commit to handle filenames starting with '-'. (4bfe119)
   1#include "cache.h"
   2#include "refs.h"
   3#include "pkt-line.h"
   4#include "run-command.h"
   5#include <sys/wait.h>
   6
   7static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
   8
   9static const char unpacker[] = "git-unpack-objects";
  10
  11static int show_ref(const char *path, const unsigned char *sha1)
  12{
  13        packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
  14        return 0;
  15}
  16
  17static void write_head_info(void)
  18{
  19        for_each_ref(show_ref);
  20}
  21
  22struct command {
  23        struct command *next;
  24        unsigned char updated;
  25        unsigned char old_sha1[20];
  26        unsigned char new_sha1[20];
  27        char ref_name[0];
  28};
  29
  30static struct command *commands = NULL;
  31
  32static int is_all_zeroes(const char *hex)
  33{
  34        int i;
  35        for (i = 0; i < 40; i++)
  36                if (*hex++ != '0')
  37                        return 0;
  38        return 1;
  39}
  40
  41static int verify_old_ref(const char *name, char *hex_contents)
  42{
  43        int fd, ret;
  44        char buffer[60];
  45
  46        if (is_all_zeroes(hex_contents))
  47                return 0;
  48        fd = open(name, O_RDONLY);
  49        if (fd < 0)
  50                return -1;
  51        ret = read(fd, buffer, 40);
  52        close(fd);
  53        if (ret != 40)
  54                return -1;
  55        if (memcmp(buffer, hex_contents, 40))
  56                return -1;
  57        return 0;
  58}
  59
  60static char update_hook[] = "hooks/update";
  61
  62static int run_update_hook(const char *refname,
  63                           char *old_hex, char *new_hex)
  64{
  65        int code;
  66
  67        if (access(update_hook, X_OK) < 0)
  68                return 0;
  69        code = run_command(update_hook, refname, old_hex, new_hex, NULL);
  70        switch (code) {
  71        case 0:
  72                return 0;
  73        case -ERR_RUN_COMMAND_FORK:
  74                die("hook fork failed");
  75        case -ERR_RUN_COMMAND_EXEC:
  76                die("hook execute failed");
  77        case -ERR_RUN_COMMAND_WAITPID:
  78                die("waitpid failed");
  79        case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
  80                die("waitpid is confused");
  81        case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
  82                fprintf(stderr, "%s died of signal", update_hook);
  83                return -1;
  84        case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
  85                die("%s died strangely", update_hook);
  86        default:
  87                error("%s exited with error code %d", update_hook, -code);
  88                return -code;
  89        }
  90}
  91
  92static int update(const char *name,
  93                  unsigned char *old_sha1, unsigned char *new_sha1)
  94{
  95        char new_hex[60], *old_hex, *lock_name;
  96        int newfd, namelen, written;
  97
  98        if (!strncmp(name, "refs/", 5) && check_ref_format(name + 5))
  99                return error("refusing to create funny ref '%s' locally",
 100                             name);
 101
 102        namelen = strlen(name);
 103        lock_name = xmalloc(namelen + 10);
 104        memcpy(lock_name, name, namelen);
 105        memcpy(lock_name + namelen, ".lock", 6);
 106
 107        strcpy(new_hex, sha1_to_hex(new_sha1));
 108        old_hex = sha1_to_hex(old_sha1);
 109        if (!has_sha1_file(new_sha1))
 110                return error("unpack should have generated %s, "
 111                             "but I can't find it!", new_hex);
 112
 113        safe_create_leading_directories(lock_name);
 114
 115        newfd = open(lock_name, O_CREAT | O_EXCL | O_WRONLY, 0666);
 116        if (newfd < 0)
 117                return error("unable to create %s (%s)",
 118                             lock_name, strerror(errno));
 119
 120        /* Write the ref with an ending '\n' */
 121        new_hex[40] = '\n';
 122        new_hex[41] = 0;
 123        written = write(newfd, new_hex, 41);
 124        /* Remove the '\n' again */
 125        new_hex[40] = 0;
 126
 127        close(newfd);
 128        if (written != 41) {
 129                unlink(lock_name);
 130                return error("unable to write %s", lock_name);
 131        }
 132        if (verify_old_ref(name, old_hex) < 0) {
 133                unlink(lock_name);
 134                return error("%s changed during push", name);
 135        }
 136        if (run_update_hook(name, old_hex, new_hex)) {
 137                unlink(lock_name);
 138                return error("hook declined to update %s\n", name);
 139        }
 140        else if (rename(lock_name, name) < 0) {
 141                unlink(lock_name);
 142                return error("unable to replace %s", name);
 143        }
 144        else {
 145                fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
 146                return 0;
 147        }
 148}
 149
 150static char update_post_hook[] = "hooks/post-update";
 151
 152static void run_update_post_hook(struct command *cmd)
 153{
 154        struct command *cmd_p;
 155        int argc;
 156        char **argv;
 157
 158        if (access(update_post_hook, X_OK) < 0)
 159                return;
 160        for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
 161                if (!cmd_p->updated)
 162                        continue;
 163                argc++;
 164        }
 165        argv = xmalloc(sizeof(*argv) * (1 + argc));
 166        argv[0] = update_post_hook;
 167
 168        for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
 169                if (!cmd_p->updated)
 170                        continue;
 171                argv[argc] = xmalloc(strlen(cmd_p->ref_name) + 1);
 172                strcpy(argv[argc], cmd_p->ref_name);
 173                argc++;
 174        }
 175        argv[argc] = NULL;
 176        run_command_v(argc, argv);
 177}
 178
 179/*
 180 * This gets called after(if) we've successfully
 181 * unpacked the data payload.
 182 */
 183static void execute_commands(void)
 184{
 185        struct command *cmd = commands;
 186
 187        while (cmd) {
 188                cmd->updated = !update(cmd->ref_name,
 189                                       cmd->old_sha1, cmd->new_sha1);
 190                cmd = cmd->next;
 191        }
 192        run_update_post_hook(commands);
 193}
 194
 195static void read_head_info(void)
 196{
 197        struct command **p = &commands;
 198        for (;;) {
 199                static char line[1000];
 200                unsigned char old_sha1[20], new_sha1[20];
 201                struct command *cmd;
 202                int len;
 203
 204                len = packet_read_line(0, line, sizeof(line));
 205                if (!len)
 206                        break;
 207                if (line[len-1] == '\n')
 208                        line[--len] = 0;
 209                if (len < 83 ||
 210                    line[40] != ' ' ||
 211                    line[81] != ' ' ||
 212                    get_sha1_hex(line, old_sha1) ||
 213                    get_sha1_hex(line + 41, new_sha1))
 214                        die("protocol error: expected old/new/ref, got '%s'", line);
 215                cmd = xmalloc(sizeof(struct command) + len - 80);
 216                memcpy(cmd->old_sha1, old_sha1, 20);
 217                memcpy(cmd->new_sha1, new_sha1, 20);
 218                memcpy(cmd->ref_name, line + 82, len - 81);
 219                cmd->next = NULL;
 220                *p = cmd;
 221                p = &cmd->next;
 222        }
 223}
 224
 225static void unpack(void)
 226{
 227        int code = run_command(unpacker, NULL);
 228        switch (code) {
 229        case 0:
 230                return;
 231        case -ERR_RUN_COMMAND_FORK:
 232                die("unpack fork failed");
 233        case -ERR_RUN_COMMAND_EXEC:
 234                die("unpack execute failed");
 235        case -ERR_RUN_COMMAND_WAITPID:
 236                die("waitpid failed");
 237        case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
 238                die("waitpid is confused");
 239        case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
 240                die("%s died of signal", unpacker);
 241        case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
 242                die("%s died strangely", unpacker);
 243        default:
 244                die("%s exited with error code %d", unpacker, -code);
 245        }
 246}
 247
 248int main(int argc, char **argv)
 249{
 250        int i;
 251        const char *dir = NULL;
 252
 253        argv++;
 254        for (i = 1; i < argc; i++) {
 255                const char *arg = *argv++;
 256
 257                if (*arg == '-') {
 258                        /* Do flag handling here */
 259                        usage(receive_pack_usage);
 260                }
 261                if (dir)
 262                        usage(receive_pack_usage);
 263                dir = arg;
 264        }
 265        if (!dir)
 266                usage(receive_pack_usage);
 267
 268        /* chdir to the directory. If that fails, try appending ".git" */
 269        if (chdir(dir) < 0) {
 270                if (chdir(mkpath("%s.git", dir)) < 0)
 271                        die("unable to cd to %s", dir);
 272        }
 273
 274        /* If we have a ".git" directory, chdir to it */
 275        chdir(".git");
 276        putenv("GIT_DIR=.");
 277
 278        if (access("objects", X_OK) < 0 || access("refs/heads", X_OK) < 0)
 279                die("%s doesn't appear to be a git directory", dir);
 280        write_head_info();
 281
 282        /* EOF */
 283        packet_flush(1);
 284
 285        read_head_info();
 286        if (commands) {
 287                unpack();
 288                execute_commands();
 289        }
 290        return 0;
 291}