date handling: handle "AM"/"PM" on time
[git/git.git] / http-pull.c
CommitLineData
6eb7ed54
DB
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
4250a5e5
DB
10#include "pull.h"
11
6eb7ed54
DB
12#include <curl/curl.h>
13#include <curl/easy.h>
14
15static CURL *curl;
16
17static char *base;
18
6eb7ed54
DB
19static SHA_CTX c;
20static z_stream stream;
21
22static int local;
23static int zret;
24
25static 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
4250a5e5 48int fetch(unsigned char *sha1)
6eb7ed54
DB
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 if (has_sha1_file(sha1)) {
57 return 0;
58 }
59
60 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
61
62 if (local < 0)
63 return error("Couldn't open %s\n", filename);
64
65 memset(&stream, 0, sizeof(stream));
66
67 inflateInit(&stream);
68
69 SHA1_Init(&c);
70
71 curl_easy_setopt(curl, CURLOPT_FILE, NULL);
72 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
73
812666c8 74 url = xmalloc(strlen(base) + 50);
6eb7ed54
DB
75 strcpy(url, base);
76 posn = url + strlen(base);
77 strcpy(posn, "objects/");
78 posn += 8;
79 memcpy(posn, hex, 2);
80 posn += 2;
81 *(posn++) = '/';
82 strcpy(posn, hex + 2);
83
84 curl_easy_setopt(curl, CURLOPT_URL, url);
85
86 /*printf("Getting %s\n", hex);*/
87
88 if (curl_easy_perform(curl))
89 return error("Couldn't get %s for %s\n", url, hex);
90
91 close(local);
92 inflateEnd(&stream);
93 SHA1_Final(real_sha1, &c);
94 if (zret != Z_STREAM_END) {
95 unlink(filename);
96 return error("File %s (%s) corrupt\n", hex, url);
97 }
98 if (memcmp(sha1, real_sha1, 20)) {
99 unlink(filename);
100 return error("File %s has bad hash\n", hex);
101 }
102
103 return 0;
104}
105
6eb7ed54
DB
106int main(int argc, char **argv)
107{
108 char *commit_id;
109 char *url;
110 int arg = 1;
6eb7ed54
DB
111
112 while (arg < argc && argv[arg][0] == '-') {
113 if (argv[arg][1] == 't') {
4250a5e5 114 get_tree = 1;
6eb7ed54 115 } else if (argv[arg][1] == 'c') {
4250a5e5 116 get_history = 1;
6eb7ed54 117 } else if (argv[arg][1] == 'a') {
4250a5e5
DB
118 get_all = 1;
119 get_tree = 1;
120 get_history = 1;
6eb7ed54
DB
121 }
122 arg++;
123 }
124 if (argc < arg + 2) {
125 usage("http-pull [-c] [-t] [-a] commit-id url");
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}