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