ab1981c72ae8d908e5844ea0b8f4b6e74e572bbc
   1#include "cache.h"
   2#include "refs.h"
   3#include "pkt-line.h"
   4#include "tag.h"
   5#include "object.h"
   6#include "commit.h"
   7
   8static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
   9
  10#define THEY_HAVE (1U << 0)
  11#define MAX_HAS 256
  12#define MAX_NEEDS 256
  13static int nr_has = 0, nr_needs = 0;
  14static unsigned char has_sha1[MAX_HAS][20];
  15static unsigned char needs_sha1[MAX_NEEDS][20];
  16static unsigned int timeout = 0;
  17
  18static void reset_timeout(void)
  19{
  20        alarm(timeout);
  21}
  22
  23static int strip(char *line, int len)
  24{
  25        if (len && line[len-1] == '\n')
  26                line[--len] = 0;
  27        return len;
  28}
  29
  30static void create_pack_file(void)
  31{
  32        int fd[2];
  33        pid_t pid;
  34
  35        if (pipe(fd) < 0)
  36                die("git-upload-pack: unable to create pipe");
  37        pid = fork();
  38        if (pid < 0)
  39                die("git-upload-pack: unable to fork git-rev-list");
  40
  41        if (!pid) {
  42                int i;
  43                int args;
  44                char **argv;
  45                char *buf;
  46                char **p;
  47
  48                if (MAX_NEEDS <= nr_needs)
  49                        args = nr_has + 10;
  50                else
  51                        args = nr_has + nr_needs + 5;
  52                argv = xmalloc(args * sizeof(char *));
  53                buf = xmalloc(args * 45);
  54                p = argv;
  55
  56                dup2(fd[1], 1);
  57                close(0);
  58                close(fd[0]);
  59                close(fd[1]);
  60                *p++ = "git-rev-list";
  61                *p++ = "--objects";
  62                if (MAX_NEEDS <= nr_needs)
  63                        *p++ = "--all";
  64                else {
  65                        for (i = 0; i < nr_needs; i++) {
  66                                *p++ = buf;
  67                                memcpy(buf, sha1_to_hex(needs_sha1[i]), 41);
  68                                buf += 41;
  69                        }
  70                }
  71                for (i = 0; i < nr_has; i++) {
  72                        *p++ = buf;
  73                        *buf++ = '^';
  74                        memcpy(buf, sha1_to_hex(has_sha1[i]), 41);
  75                        buf += 41;
  76                }
  77                *p++ = NULL;
  78                execvp("git-rev-list", argv);
  79                die("git-upload-pack: unable to exec git-rev-list");
  80        }
  81        dup2(fd[0], 0);
  82        close(fd[0]);
  83        close(fd[1]);
  84        execlp("git-pack-objects", "git-pack-objects", "--stdout", NULL);
  85        die("git-upload-pack: unable to exec git-pack-objects");
  86}
  87
  88static int got_sha1(char *hex, unsigned char *sha1)
  89{
  90        if (get_sha1_hex(hex, sha1))
  91                die("git-upload-pack: expected SHA1 object, got '%s'", hex);
  92        if (!has_sha1_file(sha1))
  93                return 0;
  94        if (nr_has < MAX_HAS) {
  95                struct object *o = lookup_object(sha1);
  96                if (!o || (!o->parsed && !parse_object(sha1)))
  97                        die("oops (%s)", sha1_to_hex(sha1));
  98                if (o->type == commit_type) {
  99                        struct commit_list *parents;
 100                        if (o->flags & THEY_HAVE)
 101                                return 0;
 102                        o->flags |= THEY_HAVE;
 103                        for (parents = ((struct commit*)o)->parents;
 104                             parents;
 105                             parents = parents->next)
 106                                parents->item->object.flags |= THEY_HAVE;
 107                }
 108                memcpy(has_sha1[nr_has++], sha1, 20);
 109        }
 110        return 1;
 111}
 112
 113static int get_common_commits(void)
 114{
 115        static char line[1000];
 116        unsigned char sha1[20];
 117        int len;
 118
 119        track_object_refs = 0;
 120        save_commit_buffer = 0;
 121
 122        for(;;) {
 123                len = packet_read_line(0, line, sizeof(line));
 124                reset_timeout();
 125
 126                if (!len) {
 127                        packet_write(1, "NAK\n");
 128                        continue;
 129                }
 130                len = strip(line, len);
 131                if (!strncmp(line, "have ", 5)) {
 132                        if (got_sha1(line+5, sha1)) {
 133                                packet_write(1, "ACK %s\n", sha1_to_hex(sha1));
 134                                break;
 135                        }
 136                        continue;
 137                }
 138                if (!strcmp(line, "done")) {
 139                        packet_write(1, "NAK\n");
 140                        return -1;
 141                }
 142                die("git-upload-pack: expected SHA1 list, got '%s'", line);
 143        }
 144
 145        for (;;) {
 146                len = packet_read_line(0, line, sizeof(line));
 147                reset_timeout();
 148                if (!len)
 149                        continue;
 150                len = strip(line, len);
 151                if (!strncmp(line, "have ", 5)) {
 152                        got_sha1(line+5, sha1);
 153                        continue;
 154                }
 155                if (!strcmp(line, "done"))
 156                        break;
 157                die("git-upload-pack: expected SHA1 list, got '%s'", line);
 158        }
 159        return 0;
 160}
 161
 162static int receive_needs(void)
 163{
 164        static char line[1000];
 165        int len, needs;
 166
 167        needs = 0;
 168        for (;;) {
 169                unsigned char dummy[20], *sha1_buf;
 170                len = packet_read_line(0, line, sizeof(line));
 171                reset_timeout();
 172                if (!len)
 173                        return needs;
 174
 175                sha1_buf = dummy;
 176                if (needs == MAX_NEEDS) {
 177                        fprintf(stderr,
 178                                "warning: supporting only a max of %d requests. "
 179                                "sending everything instead.\n",
 180                                MAX_NEEDS);
 181                }
 182                else if (needs < MAX_NEEDS)
 183                        sha1_buf = needs_sha1[needs];
 184
 185                if (strncmp("want ", line, 5) || get_sha1_hex(line+5, sha1_buf))
 186                        die("git-upload-pack: protocol error, "
 187                            "expected to get sha, not '%s'", line);
 188                needs++;
 189        }
 190}
 191
 192static int send_ref(const char *refname, const unsigned char *sha1)
 193{
 194        struct object *o = parse_object(sha1);
 195
 196        packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
 197        if (o->type == tag_type) {
 198                o = deref_tag(o);
 199                packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
 200        }
 201        return 0;
 202}
 203
 204static int upload_pack(void)
 205{
 206        reset_timeout();
 207        head_ref(send_ref);
 208        for_each_ref(send_ref);
 209        packet_flush(1);
 210        nr_needs = receive_needs();
 211        if (!nr_needs)
 212                return 0;
 213        get_common_commits();
 214        create_pack_file();
 215        return 0;
 216}
 217
 218int main(int argc, char **argv)
 219{
 220        const char *dir;
 221        int i;
 222        int strict = 0;
 223
 224        for (i = 1; i < argc; i++) {
 225                char *arg = argv[i];
 226
 227                if (arg[0] != '-')
 228                        break;
 229                if (!strcmp(arg, "--strict")) {
 230                        strict = 1;
 231                        continue;
 232                }
 233                if (!strncmp(arg, "--timeout=", 10)) {
 234                        timeout = atoi(arg+10);
 235                        continue;
 236                }
 237                if (!strcmp(arg, "--")) {
 238                        i++;
 239                        break;
 240                }
 241        }
 242        
 243        if (i != argc-1)
 244                usage(upload_pack_usage);
 245        dir = argv[i];
 246
 247        /* chdir to the directory. If that fails, try appending ".git" */
 248        if (chdir(dir) < 0) {
 249                if (strict || chdir(mkpath("%s.git", dir)) < 0)
 250                        die("git-upload-pack unable to chdir to %s", dir);
 251        }
 252        if (!strict)
 253                chdir(".git");
 254
 255        if (access("objects", X_OK) || access("refs", X_OK))
 256                die("git-upload-pack: %s doesn't seem to be a git archive", dir);
 257
 258        putenv("GIT_DIR=.");
 259        upload_pack();
 260        return 0;
 261}