[PATCH] Do not call fetch() when we have it.
[git] / http-pull.c
1 #include <fcntl.h>
2 #include <unistd.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include "cache.h"
6 #include "commit.h"
7 #include <errno.h>
8 #include <stdio.h>
9
10 #include "pull.h"
11
12 #include <curl/curl.h>
13 #include <curl/easy.h>
14
15 static CURL *curl;
16
17 static char *base;
18
19 static SHA_CTX c;
20 static z_stream stream;
21
22 static int local;
23 static int zret;
24
25 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb, 
26                                void *data) {
27         char expn[4096];
28         size_t size = eltsize * nmemb;
29         int posn = 0;
30         do {
31                 ssize_t retval = write(local, ptr + posn, size - posn);
32                 if (retval < 0)
33                         return posn;
34                 posn += retval;
35         } while (posn < size);
36
37         stream.avail_in = size;
38         stream.next_in = ptr;
39         do {
40                 stream.next_out = expn;
41                 stream.avail_out = sizeof(expn);
42                 zret = inflate(&stream, Z_SYNC_FLUSH);
43                 SHA1_Update(&c, expn, sizeof(expn) - stream.avail_out);
44         } while (stream.avail_in && zret == Z_OK);
45         return size;
46 }
47
48 int fetch(unsigned char *sha1)
49 {
50         char *hex = sha1_to_hex(sha1);
51         char *filename = sha1_file_name(sha1);
52         char real_sha1[20];
53         char *url;
54         char *posn;
55
56         local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
57
58         if (local < 0)
59                 return error("Couldn't open %s\n", filename);
60
61         memset(&stream, 0, sizeof(stream));
62
63         inflateInit(&stream);
64
65         SHA1_Init(&c);
66
67         curl_easy_setopt(curl, CURLOPT_FILE, NULL);
68         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
69
70         url = xmalloc(strlen(base) + 50);
71         strcpy(url, base);
72         posn = url + strlen(base);
73         strcpy(posn, "objects/");
74         posn += 8;
75         memcpy(posn, hex, 2);
76         posn += 2;
77         *(posn++) = '/';
78         strcpy(posn, hex + 2);
79
80         curl_easy_setopt(curl, CURLOPT_URL, url);
81
82         /*printf("Getting %s\n", hex);*/
83
84         if (curl_easy_perform(curl))
85                 return error("Couldn't get %s for %s\n", url, hex);
86
87         close(local);
88         inflateEnd(&stream);
89         SHA1_Final(real_sha1, &c);
90         if (zret != Z_STREAM_END) {
91                 unlink(filename);
92                 return error("File %s (%s) corrupt\n", hex, url);
93         }
94         if (memcmp(sha1, real_sha1, 20)) {
95                 unlink(filename);
96                 return error("File %s has bad hash\n", hex);
97         }
98         
99         return 0;
100 }
101
102 int main(int argc, char **argv)
103 {
104         char *commit_id;
105         char *url;
106         int arg = 1;
107
108         while (arg < argc && argv[arg][0] == '-') {
109                 if (argv[arg][1] == 't') {
110                         get_tree = 1;
111                 } else if (argv[arg][1] == 'c') {
112                         get_history = 1;
113                 } else if (argv[arg][1] == 'a') {
114                         get_all = 1;
115                         get_tree = 1;
116                         get_history = 1;
117                 }
118                 arg++;
119         }
120         if (argc < arg + 2) {
121                 usage("http-pull [-c] [-t] [-a] commit-id url");
122                 return 1;
123         }
124         commit_id = argv[arg];
125         url = argv[arg + 1];
126
127         curl_global_init(CURL_GLOBAL_ALL);
128
129         curl = curl_easy_init();
130
131         base = url;
132
133         if (pull(commit_id))
134                 return 1;
135
136         curl_global_cleanup();
137         return 0;
138 }