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