upload-pack.con commit teach git-status about spaces in filenames (33bb218)
   1#include "cache.h"
   2#include "refs.h"
   3#include "pkt-line.h"
   4
   5static const char upload_pack_usage[] = "git-upload-pack <dir>";
   6
   7#define MAX_HAS (16)
   8#define MAX_NEEDS (256)
   9static int nr_has = 0, nr_needs = 0;
  10static unsigned char has_sha1[MAX_HAS][20];
  11static unsigned char needs_sha1[MAX_NEEDS][20];
  12
  13static int strip(char *line, int len)
  14{
  15        if (len && line[len-1] == '\n')
  16                line[--len] = 0;
  17        return len;
  18}
  19
  20static void create_pack_file(void)
  21{
  22        int fd[2];
  23        pid_t pid;
  24
  25        if (pipe(fd) < 0)
  26                die("git-upload-pack: unable to create pipe");
  27        pid = fork();
  28        if (pid < 0)
  29                die("git-upload-pack: unable to fork git-rev-list");
  30
  31        if (!pid) {
  32                int i;
  33                int args;
  34                char **argv;
  35                char *buf;
  36                char **p;
  37
  38                if (MAX_NEEDS <= nr_needs)
  39                        args = nr_has + 10;
  40                else
  41                        args = nr_has + nr_needs + 5;
  42                argv = xmalloc(args * sizeof(char *));
  43                buf = xmalloc(args * 45);
  44                p = argv;
  45
  46                dup2(fd[1], 1);
  47                close(0);
  48                close(fd[0]);
  49                close(fd[1]);
  50                *p++ = "git-rev-list";
  51                *p++ = "--objects";
  52                if (MAX_NEEDS <= nr_needs)
  53                        *p++ = "--all";
  54                else {
  55                        for (i = 0; i < nr_needs; i++) {
  56                                *p++ = buf;
  57                                memcpy(buf, sha1_to_hex(needs_sha1[i]), 41);
  58                                buf += 41;
  59                        }
  60                }
  61                for (i = 0; i < nr_has; i++) {
  62                        *p++ = buf;
  63                        *buf++ = '^';
  64                        memcpy(buf, sha1_to_hex(has_sha1[i]), 41);
  65                        buf += 41;
  66                }
  67                *p++ = NULL;
  68                execvp("git-rev-list", argv);
  69                die("git-upload-pack: unable to exec git-rev-list");
  70        }
  71        dup2(fd[0], 0);
  72        close(fd[0]);
  73        close(fd[1]);
  74        execlp("git-pack-objects", "git-pack-objects", "--stdout", NULL);
  75        die("git-upload-pack: unable to exec git-pack-objects");
  76}
  77
  78static int got_sha1(char *hex, unsigned char *sha1)
  79{
  80        int nr;
  81        if (get_sha1_hex(hex, sha1))
  82                die("git-upload-pack: expected SHA1 object, got '%s'", hex);
  83        if (!has_sha1_file(sha1))
  84                return 0;
  85        nr = nr_has;
  86        if (nr < MAX_HAS) {
  87                memcpy(has_sha1[nr], sha1, 20);
  88                nr_has = nr+1;
  89        }
  90        return 1;
  91}
  92
  93static int get_common_commits(void)
  94{
  95        static char line[1000];
  96        unsigned char sha1[20];
  97        int len;
  98
  99        for(;;) {
 100                len = packet_read_line(0, line, sizeof(line));
 101
 102                if (!len) {
 103                        packet_write(1, "NAK\n");
 104                        continue;
 105                }
 106                len = strip(line, len);
 107                if (!strncmp(line, "have ", 5)) {
 108                        if (got_sha1(line+5, sha1)) {
 109                                packet_write(1, "ACK %s\n", sha1_to_hex(sha1));
 110                                break;
 111                        }
 112                        continue;
 113                }
 114                if (!strcmp(line, "done")) {
 115                        packet_write(1, "NAK\n");
 116                        return -1;
 117                }
 118                die("git-upload-pack: expected SHA1 list, got '%s'", line);
 119        }
 120
 121        for (;;) {
 122                len = packet_read_line(0, line, sizeof(line));
 123                if (!len)
 124                        continue;
 125                len = strip(line, len);
 126                if (!strncmp(line, "have ", 5)) {
 127                        got_sha1(line+5, sha1);
 128                        continue;
 129                }
 130                if (!strcmp(line, "done"))
 131                        break;
 132                die("git-upload-pack: expected SHA1 list, got '%s'", line);
 133        }
 134        return 0;
 135}
 136
 137static int receive_needs(void)
 138{
 139        static char line[1000];
 140        int len, needs;
 141
 142        needs = 0;
 143        for (;;) {
 144                unsigned char dummy[20], *sha1_buf;
 145                len = packet_read_line(0, line, sizeof(line));
 146                if (!len)
 147                        return needs;
 148
 149                sha1_buf = dummy;
 150                if (needs == MAX_NEEDS) {
 151                        fprintf(stderr,
 152                                "warning: supporting only a max of %d requests. "
 153                                "sending everything instead.\n",
 154                                MAX_NEEDS);
 155                }
 156                else if (needs < MAX_NEEDS)
 157                        sha1_buf = needs_sha1[needs];
 158
 159                if (strncmp("want ", line, 5) || get_sha1_hex(line+5, sha1_buf))
 160                        die("git-upload-pack: protocol error, "
 161                            "expected to get sha, not '%s'", line);
 162                needs++;
 163        }
 164}
 165
 166static int send_ref(const char *refname, const unsigned char *sha1)
 167{
 168        packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
 169        return 0;
 170}
 171
 172static int upload_pack(void)
 173{
 174        head_ref(send_ref);
 175        for_each_ref(send_ref);
 176        packet_flush(1);
 177        nr_needs = receive_needs();
 178        if (!nr_needs)
 179                return 0;
 180        get_common_commits();
 181        create_pack_file();
 182        return 0;
 183}
 184
 185int main(int argc, char **argv)
 186{
 187        const char *dir;
 188        if (argc != 2)
 189                usage(upload_pack_usage);
 190        dir = argv[1];
 191
 192        /* chdir to the directory. If that fails, try appending ".git" */
 193        if (chdir(dir) < 0) {
 194                if (chdir(mkpath("%s.git", dir)) < 0)
 195                        die("git-upload-pack unable to chdir to %s", dir);
 196        }
 197        chdir(".git");
 198        if (access("objects", X_OK) || access("refs", X_OK))
 199                die("git-upload-pack: %s doesn't seem to be a git archive", dir);
 200        putenv("GIT_DIR=.");
 201        upload_pack();
 202        return 0;
 203}