Abstract test_create_repo out for use in tests.
[git/git.git] / upload-pack.c
CommitLineData
def88e9a
LT
1#include "cache.h"
2#include "refs.h"
3#include "pkt-line.h"
f6b42a81
JH
4#include "tag.h"
5#include "object.h"
f0243f26 6#include "commit.h"
77cb17e9 7#include "exec_cmd.h"
def88e9a 8
960deccb 9static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
def88e9a 10
f0243f26 11#define THEY_HAVE (1U << 0)
565ebbf7
JH
12#define OUR_REF (1U << 1)
13#define WANTED (1U << 2)
6b32884a
JH
14#define MAX_HAS 256
15#define MAX_NEEDS 256
1bd8c8f0 16static int nr_has = 0, nr_needs = 0, multi_ack = 0, nr_our_refs = 0;
fb9040cc
LT
17static unsigned char has_sha1[MAX_HAS][20];
18static unsigned char needs_sha1[MAX_NEEDS][20];
960deccb
PA
19static unsigned int timeout = 0;
20
21static void reset_timeout(void)
22{
23 alarm(timeout);
24}
fb9040cc 25
75bfc6c2
LT
26static int strip(char *line, int len)
27{
28 if (len && line[len-1] == '\n')
29 line[--len] = 0;
30 return len;
31}
32
fb9040cc
LT
33static void create_pack_file(void)
34{
75bfc6c2
LT
35 int fd[2];
36 pid_t pid;
565ebbf7 37 int create_full_pack = (nr_our_refs == nr_needs && !nr_has);
75bfc6c2
LT
38
39 if (pipe(fd) < 0)
40 die("git-upload-pack: unable to create pipe");
41 pid = fork();
42 if (pid < 0)
43 die("git-upload-pack: unable to fork git-rev-list");
44
45 if (!pid) {
46 int i;
e091eb93
JH
47 int args;
48 char **argv;
49 char *buf;
50 char **p;
51
565ebbf7
JH
52 if (create_full_pack)
53 args = 10;
e091eb93
JH
54 else
55 args = nr_has + nr_needs + 5;
56 argv = xmalloc(args * sizeof(char *));
57 buf = xmalloc(args * 45);
58 p = argv;
75bfc6c2
LT
59
60 dup2(fd[1], 1);
61 close(0);
62 close(fd[0]);
63 close(fd[1]);
77cb17e9 64 *p++ = "rev-list";
75bfc6c2 65 *p++ = "--objects";
b5c367f7 66 if (create_full_pack || MAX_NEEDS <= nr_needs)
e091eb93
JH
67 *p++ = "--all";
68 else {
69 for (i = 0; i < nr_needs; i++) {
70 *p++ = buf;
71 memcpy(buf, sha1_to_hex(needs_sha1[i]), 41);
72 buf += 41;
73 }
75bfc6c2 74 }
b5c367f7
JS
75 if (!create_full_pack)
76 for (i = 0; i < nr_has; i++) {
77 *p++ = buf;
78 *buf++ = '^';
79 memcpy(buf, sha1_to_hex(has_sha1[i]), 41);
80 buf += 41;
81 }
75bfc6c2 82 *p++ = NULL;
77cb17e9 83 execv_git_cmd(argv);
75bfc6c2
LT
84 die("git-upload-pack: unable to exec git-rev-list");
85 }
86 dup2(fd[0], 0);
87 close(fd[0]);
88 close(fd[1]);
77cb17e9 89 execl_git_cmd("pack-objects", "--stdout", NULL);
75bfc6c2 90 die("git-upload-pack: unable to exec git-pack-objects");
fb9040cc
LT
91}
92
def88e9a
LT
93static int got_sha1(char *hex, unsigned char *sha1)
94{
95 if (get_sha1_hex(hex, sha1))
96 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
fb9040cc
LT
97 if (!has_sha1_file(sha1))
98 return 0;
f0243f26
JS
99 if (nr_has < MAX_HAS) {
100 struct object *o = lookup_object(sha1);
101 if (!(o && o->parsed))
102 o = parse_object(sha1);
103 if (!o)
104 die("oops (%s)", sha1_to_hex(sha1));
105 if (o->type == commit_type) {
106 struct commit_list *parents;
107 if (o->flags & THEY_HAVE)
108 return 0;
109 o->flags |= THEY_HAVE;
110 for (parents = ((struct commit*)o)->parents;
111 parents;
112 parents = parents->next)
113 parents->item->object.flags |= THEY_HAVE;
114 }
115 memcpy(has_sha1[nr_has++], sha1, 20);
fb9040cc
LT
116 }
117 return 1;
def88e9a
LT
118}
119
120static int get_common_commits(void)
121{
122 static char line[1000];
1bd8c8f0 123 unsigned char sha1[20], last_sha1[20];
def88e9a
LT
124 int len;
125
f0243f26
JS
126 track_object_refs = 0;
127 save_commit_buffer = 0;
128
def88e9a
LT
129 for(;;) {
130 len = packet_read_line(0, line, sizeof(line));
960deccb 131 reset_timeout();
def88e9a
LT
132
133 if (!len) {
1bd8c8f0
JS
134 if (nr_has == 0 || multi_ack)
135 packet_write(1, "NAK\n");
def88e9a
LT
136 continue;
137 }
75bfc6c2 138 len = strip(line, len);
def88e9a 139 if (!strncmp(line, "have ", 5)) {
1bd8c8f0
JS
140 if (got_sha1(line+5, sha1) &&
141 (multi_ack || nr_has == 1)) {
142 if (nr_has >= MAX_HAS)
143 multi_ack = 0;
144 packet_write(1, "ACK %s%s\n",
145 sha1_to_hex(sha1),
146 multi_ack ? " continue" : "");
147 if (multi_ack)
148 memcpy(last_sha1, sha1, 20);
af2d3aa4 149 }
def88e9a
LT
150 continue;
151 }
152 if (!strcmp(line, "done")) {
1bd8c8f0
JS
153 if (nr_has > 0) {
154 if (multi_ack)
155 packet_write(1, "ACK %s\n",
156 sha1_to_hex(last_sha1));
157 return 0;
158 }
def88e9a
LT
159 packet_write(1, "NAK\n");
160 return -1;
161 }
162 die("git-upload-pack: expected SHA1 list, got '%s'", line);
163 }
def88e9a
LT
164}
165
fb9040cc
LT
166static int receive_needs(void)
167{
168 static char line[1000];
169 int len, needs;
170
171 needs = 0;
172 for (;;) {
565ebbf7 173 struct object *o;
e091eb93 174 unsigned char dummy[20], *sha1_buf;
fb9040cc 175 len = packet_read_line(0, line, sizeof(line));
960deccb 176 reset_timeout();
fb9040cc
LT
177 if (!len)
178 return needs;
179
e091eb93
JH
180 sha1_buf = dummy;
181 if (needs == MAX_NEEDS) {
182 fprintf(stderr,
183 "warning: supporting only a max of %d requests. "
184 "sending everything instead.\n",
185 MAX_NEEDS);
186 }
187 else if (needs < MAX_NEEDS)
188 sha1_buf = needs_sha1[needs];
189
190 if (strncmp("want ", line, 5) || get_sha1_hex(line+5, sha1_buf))
191 die("git-upload-pack: protocol error, "
192 "expected to get sha, not '%s'", line);
1bd8c8f0
JS
193 if (strstr(line+45, "multi_ack"))
194 multi_ack = 1;
565ebbf7
JH
195
196 /* We have sent all our refs already, and the other end
197 * should have chosen out of them; otherwise they are
198 * asking for nonsense.
199 *
200 * Hmph. We may later want to allow "want" line that
201 * asks for something like "master~10" (symbolic)...
202 * would it make sense? I don't know.
203 */
204 o = lookup_object(sha1_buf);
205 if (!o || !(o->flags & OUR_REF))
206 die("git-upload-pack: not our ref %s", line+5);
207 if (!(o->flags & WANTED)) {
208 o->flags |= WANTED;
209 needs++;
210 }
fb9040cc
LT
211 }
212}
213
def88e9a
LT
214static int send_ref(const char *refname, const unsigned char *sha1)
215{
1f5881bb 216 static char *capabilities = "multi_ack";
f6b42a81
JH
217 struct object *o = parse_object(sha1);
218
1f5881bb
JS
219 if (capabilities)
220 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1), refname,
221 0, capabilities);
222 else
223 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
224 capabilities = NULL;
565ebbf7
JH
225 if (!(o->flags & OUR_REF)) {
226 o->flags |= OUR_REF;
227 nr_our_refs++;
228 }
f6b42a81 229 if (o->type == tag_type) {
9534f40b 230 o = deref_tag(o, refname, 0);
f6b42a81
JH
231 packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
232 }
def88e9a
LT
233 return 0;
234}
235
236static int upload_pack(void)
237{
960deccb 238 reset_timeout();
723c31fe 239 head_ref(send_ref);
def88e9a
LT
240 for_each_ref(send_ref);
241 packet_flush(1);
fb9040cc
LT
242 nr_needs = receive_needs();
243 if (!nr_needs)
244 return 0;
def88e9a 245 get_common_commits();
fb9040cc 246 create_pack_file();
def88e9a
LT
247 return 0;
248}
249
250int main(int argc, char **argv)
251{
8d630132 252 char *dir;
960deccb
PA
253 int i;
254 int strict = 0;
255
256 for (i = 1; i < argc; i++) {
257 char *arg = argv[i];
258
259 if (arg[0] != '-')
260 break;
261 if (!strcmp(arg, "--strict")) {
262 strict = 1;
263 continue;
264 }
265 if (!strncmp(arg, "--timeout=", 10)) {
266 timeout = atoi(arg+10);
267 continue;
268 }
269 if (!strcmp(arg, "--")) {
270 i++;
271 break;
272 }
273 }
274
275 if (i != argc-1)
def88e9a 276 usage(upload_pack_usage);
960deccb 277 dir = argv[i];
113b9475 278
8d630132
AE
279 if (!enter_repo(dir, strict))
280 die("'%s': unable to chdir or not a git archive", dir);
960deccb 281
def88e9a
LT
282 upload_pack();
283 return 0;
284}