38047e695b8dfabbdbe824486b1fae9ff5664ea4
   1#include "cache.h"
   2#include "commit.h"
   3
   4#include "pull.h"
   5
   6#include <curl/curl.h>
   7#include <curl/easy.h>
   8
   9#if LIBCURL_VERSION_NUM < 0x070704
  10#define curl_global_cleanup() do { /* nothing */ } while(0)
  11#endif
  12#if LIBCURL_VERSION_NUM < 0x070800
  13#define curl_global_init(a) do { /* nothing */ } while(0)
  14#endif
  15
  16static CURL *curl;
  17
  18static char *base;
  19
  20static SHA_CTX c;
  21static z_stream stream;
  22
  23static int local;
  24static int zret;
  25
  26static int curl_ssl_verify;
  27
  28struct buffer
  29{
  30        size_t posn;
  31        size_t size;
  32        void *buffer;
  33};
  34
  35static size_t fwrite_buffer(void *ptr, size_t eltsize, size_t nmemb,
  36                            struct buffer *buffer) {
  37        size_t size = eltsize * nmemb;
  38        if (size > buffer->size - buffer->posn)
  39                size = buffer->size - buffer->posn;
  40        memcpy(buffer->buffer + buffer->posn, ptr, size);
  41        buffer->posn += size;
  42        return size;
  43}
  44
  45static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb, 
  46                               void *data) {
  47        unsigned char expn[4096];
  48        size_t size = eltsize * nmemb;
  49        int posn = 0;
  50        do {
  51                ssize_t retval = write(local, ptr + posn, size - posn);
  52                if (retval < 0)
  53                        return posn;
  54                posn += retval;
  55        } while (posn < size);
  56
  57        stream.avail_in = size;
  58        stream.next_in = ptr;
  59        do {
  60                stream.next_out = expn;
  61                stream.avail_out = sizeof(expn);
  62                zret = inflate(&stream, Z_SYNC_FLUSH);
  63                SHA1_Update(&c, expn, sizeof(expn) - stream.avail_out);
  64        } while (stream.avail_in && zret == Z_OK);
  65        return size;
  66}
  67
  68int fetch(unsigned char *sha1)
  69{
  70        char *hex = sha1_to_hex(sha1);
  71        char *filename = sha1_file_name(sha1);
  72        unsigned char real_sha1[20];
  73        char *url;
  74        char *posn;
  75
  76        local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
  77
  78        if (local < 0)
  79                return error("Couldn't open %s\n", filename);
  80
  81        memset(&stream, 0, sizeof(stream));
  82
  83        inflateInit(&stream);
  84
  85        SHA1_Init(&c);
  86
  87        curl_easy_setopt(curl, CURLOPT_FILE, NULL);
  88        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
  89
  90        url = xmalloc(strlen(base) + 50);
  91        strcpy(url, base);
  92        posn = url + strlen(base);
  93        strcpy(posn, "objects/");
  94        posn += 8;
  95        memcpy(posn, hex, 2);
  96        posn += 2;
  97        *(posn++) = '/';
  98        strcpy(posn, hex + 2);
  99
 100        curl_easy_setopt(curl, CURLOPT_URL, url);
 101
 102        if (curl_easy_perform(curl))
 103                return error("Couldn't get %s for %s\n", url, hex);
 104
 105        close(local);
 106        inflateEnd(&stream);
 107        SHA1_Final(real_sha1, &c);
 108        if (zret != Z_STREAM_END) {
 109                unlink(filename);
 110                return error("File %s (%s) corrupt\n", hex, url);
 111        }
 112        if (memcmp(sha1, real_sha1, 20)) {
 113                unlink(filename);
 114                return error("File %s has bad hash\n", hex);
 115        }
 116        
 117        pull_say("got %s\n", hex);
 118        return 0;
 119}
 120
 121int fetch_ref(char *ref, unsigned char *sha1)
 122{
 123        char *url, *posn;
 124        char hex[42];
 125        struct buffer buffer;
 126        buffer.size = 41;
 127        buffer.posn = 0;
 128        buffer.buffer = hex;
 129        hex[41] = '\0';
 130        
 131        curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
 132        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
 133
 134        url = xmalloc(strlen(base) + 6 + strlen(ref));
 135        strcpy(url, base);
 136        posn = url + strlen(base);
 137        strcpy(posn, "refs/");
 138        posn += 5;
 139        strcpy(posn, ref);
 140
 141        curl_easy_setopt(curl, CURLOPT_URL, url);
 142
 143        if (curl_easy_perform(curl))
 144                return error("Couldn't get %s for %s\n", url, ref);
 145
 146        hex[40] = '\0';
 147        get_sha1_hex(hex, sha1);
 148        return 0;
 149}
 150
 151int main(int argc, char **argv)
 152{
 153        char *commit_id;
 154        char *url;
 155        int arg = 1;
 156
 157        while (arg < argc && argv[arg][0] == '-') {
 158                if (argv[arg][1] == 't') {
 159                        get_tree = 1;
 160                } else if (argv[arg][1] == 'c') {
 161                        get_history = 1;
 162                } else if (argv[arg][1] == 'a') {
 163                        get_all = 1;
 164                        get_tree = 1;
 165                        get_history = 1;
 166                } else if (argv[arg][1] == 'v') {
 167                        get_verbosely = 1;
 168                } else if (argv[arg][1] == 'w') {
 169                        write_ref = argv[arg + 1];
 170                        arg++;
 171                }
 172                arg++;
 173        }
 174        if (argc < arg + 2) {
 175                usage("git-http-pull [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
 176                return 1;
 177        }
 178        commit_id = argv[arg];
 179        url = argv[arg + 1];
 180
 181        curl_global_init(CURL_GLOBAL_ALL);
 182
 183        curl = curl_easy_init();
 184
 185        curl_ssl_verify = gitenv("GIT_SSL_NO_VERIFY") ? 0 : 1;
 186        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
 187#if LIBCURL_VERSION_NUM >= 0x070907
 188        curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
 189#endif
 190
 191        base = url;
 192
 193        if (pull(commit_id))
 194                return 1;
 195
 196        curl_global_cleanup();
 197        return 0;
 198}