upload-pack.con commit Commit first cut at "git-fetch-pack" (def88e9)
   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
   7static int got_sha1(char *hex, unsigned char *sha1)
   8{
   9        if (get_sha1_hex(hex, sha1))
  10                die("git-upload-pack: expected SHA1 object, got '%s'", hex);
  11        return has_sha1_file(sha1);
  12}
  13
  14static int get_common_commits(void)
  15{
  16        static char line[1000];
  17        unsigned char sha1[20];
  18        int len;
  19
  20        for(;;) {
  21                len = packet_read_line(0, line, sizeof(line));
  22
  23                if (!len) {
  24                        packet_write(1, "NAK\n");
  25                        continue;
  26                }
  27                if (line[len-1] == '\n')
  28                        line[--len] = 0;
  29                if (!strncmp(line, "have ", 5)) {
  30                        if (got_sha1(line+5, sha1)) {
  31                                packet_write(1, "ACK %s\n", sha1_to_hex(sha1));
  32                                break;
  33                        }
  34                        continue;
  35                }
  36                if (!strcmp(line, "done")) {
  37                        packet_write(1, "NAK\n");
  38                        return -1;
  39                }
  40                die("git-upload-pack: expected SHA1 list, got '%s'", line);
  41        }
  42
  43        for (;;) {
  44                len = packet_read_line(0, line, sizeof(line));
  45                if (!len)
  46                        break;
  47                if (!strncmp(line, "have ", 5)) {
  48                        got_sha1(line+5, sha1);
  49                        continue;
  50                }
  51                if (!strcmp(line, "done"))
  52                        break;
  53                die("git-upload-pack: expected SHA1 list, got '%s'", line);
  54        }
  55        return 0;
  56}
  57
  58static int send_ref(const char *refname, const unsigned char *sha1)
  59{
  60        packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
  61        return 0;
  62}
  63
  64static int upload_pack(void)
  65{
  66        for_each_ref(send_ref);
  67        packet_flush(1);
  68        get_common_commits();
  69        return 0;
  70}
  71
  72int main(int argc, char **argv)
  73{
  74        const char *dir;
  75        if (argc != 2)
  76                usage(upload_pack_usage);
  77        dir = argv[1];
  78        if (chdir(dir))
  79                die("git-upload-pack unable to chdir to %s", dir);
  80        chdir(".git");
  81        if (access("objects", X_OK) || access("refs", X_OK))
  82                die("git-upload-pack: %s doesn't seem to be a git archive", dir);
  83        setenv("GIT_DIR", ".", 1);
  84        upload_pack();
  85        return 0;
  86}