]>
Commit | Line | Data |
---|---|---|
def88e9a LT |
1 | #include "cache.h" |
2 | #include "refs.h" | |
3 | #include "pkt-line.h" | |
958c24b1 | 4 | #include "sideband.h" |
f6b42a81 JH |
5 | #include "tag.h" |
6 | #include "object.h" | |
f0243f26 | 7 | #include "commit.h" |
77cb17e9 | 8 | #include "exec_cmd.h" |
9b8dc263 JS |
9 | #include "diff.h" |
10 | #include "revision.h" | |
11 | #include "list-objects.h" | |
cc41fa8d | 12 | #include "run-command.h" |
47a59185 | 13 | #include "connect.h" |
051e4005 | 14 | #include "sigchain.h" |
ff5effdf | 15 | #include "version.h" |
daebaa78 | 16 | #include "string-list.h" |
9812f213 | 17 | #include "parse-options.h" |
569e554b | 18 | #include "argv-array.h" |
5411b10c | 19 | #include "prio-queue.h" |
def88e9a | 20 | |
9812f213 AQ |
21 | static const char * const upload_pack_usage[] = { |
22 | N_("git upload-pack [<options>] <dir>"), | |
23 | NULL | |
24 | }; | |
def88e9a | 25 | |
208acbfb | 26 | /* Remember to update object flag allocation in object.h */ |
937a515a JH |
27 | #define THEY_HAVE (1u << 11) |
28 | #define OUR_REF (1u << 12) | |
29 | #define WANTED (1u << 13) | |
30 | #define COMMON_KNOWN (1u << 14) | |
31 | #define REACHABLE (1u << 15) | |
32 | ||
f53514bc JS |
33 | #define SHALLOW (1u << 16) |
34 | #define NOT_SHALLOW (1u << 17) | |
35 | #define CLIENT_SHALLOW (1u << 18) | |
390eb36b | 36 | #define HIDDEN_REF (1u << 19) |
f53514bc | 37 | |
3fbe2d54 | 38 | static unsigned long oldest_have; |
937a515a | 39 | |
cccf74e2 | 40 | static int deepen_relative; |
3f1da57f | 41 | static int multi_ack; |
4e10cf9a | 42 | static int no_done; |
348e390b | 43 | static int use_thin_pack, use_ofs_delta, use_include_tag; |
9462e3f5 | 44 | static int no_progress, daemon_mode; |
7199c093 FM |
45 | /* Allow specifying sha1 if it is a ref tip. */ |
46 | #define ALLOW_TIP_SHA1 01 | |
68ee6289 FM |
47 | /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */ |
48 | #define ALLOW_REACHABLE_SHA1 02 | |
f8edeaa0 DT |
49 | /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */ |
50 | #define ALLOW_ANY_SHA1 07 | |
7199c093 | 51 | static unsigned int allow_unadvertised_object_request; |
f0cea83f | 52 | static int shallow_nr; |
b1e9fff7 JH |
53 | static struct object_array have_obj; |
54 | static struct object_array want_obj; | |
6523078b | 55 | static struct object_array extra_edge_obj; |
96f1e58f | 56 | static unsigned int timeout; |
115dedd7 | 57 | static int keepalive = 5; |
d47f3db7 JH |
58 | /* 0 for no sideband, |
59 | * otherwise maximum packet size (up to 65520 bytes). | |
60 | */ | |
96f1e58f | 61 | static int use_sideband; |
42526b47 SP |
62 | static int advertise_refs; |
63 | static int stateless_rpc; | |
20b20a22 | 64 | static const char *pack_objects_hook; |
960deccb PA |
65 | |
66 | static void reset_timeout(void) | |
67 | { | |
68 | alarm(timeout); | |
69 | } | |
fb9040cc | 70 | |
fcf0fe9e | 71 | static void send_client_data(int fd, const char *data, ssize_t sz) |
583b7ea3 | 72 | { |
4c4b7d1d LF |
73 | if (use_sideband) { |
74 | send_sideband(1, fd, data, sz, use_sideband); | |
fcf0fe9e | 75 | return; |
4c4b7d1d | 76 | } |
958c24b1 JH |
77 | if (fd == 3) |
78 | /* emergency quit */ | |
79 | fd = 2; | |
80 | if (fd == 2) { | |
93822c22 | 81 | /* XXX: are we happy to lose stuff here? */ |
958c24b1 | 82 | xwrite(fd, data, sz); |
fcf0fe9e | 83 | return; |
583b7ea3 | 84 | } |
cdf4fb8e | 85 | write_or_die(fd, data, sz); |
583b7ea3 JH |
86 | } |
87 | ||
b790e0f6 NTND |
88 | static int write_one_shallow(const struct commit_graft *graft, void *cb_data) |
89 | { | |
90 | FILE *fp = cb_data; | |
91 | if (graft->nr_parent == -1) | |
7683e2e6 | 92 | fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid)); |
b790e0f6 NTND |
93 | return 0; |
94 | } | |
95 | ||
fb9040cc LT |
96 | static void create_pack_file(void) |
97 | { | |
d3180279 | 98 | struct child_process pack_objects = CHILD_PROCESS_INIT; |
363b7817 | 99 | char data[8193], progress[128]; |
583b7ea3 JH |
100 | char abort_msg[] = "aborting due to possible repository " |
101 | "corruption on the remote side."; | |
b1c71b72 | 102 | int buffered = -1; |
1456b043 | 103 | ssize_t sz; |
65a3629e | 104 | int i; |
cdab4858 | 105 | FILE *pipe_fd; |
75bfc6c2 | 106 | |
20b20a22 JK |
107 | if (!pack_objects_hook) |
108 | pack_objects.git_cmd = 1; | |
109 | else { | |
110 | argv_array_push(&pack_objects.args, pack_objects_hook); | |
111 | argv_array_push(&pack_objects.args, "git"); | |
112 | pack_objects.use_shell = 1; | |
113 | } | |
114 | ||
cdab4858 | 115 | if (shallow_nr) { |
65a3629e MP |
116 | argv_array_push(&pack_objects.args, "--shallow-file"); |
117 | argv_array_push(&pack_objects.args, ""); | |
f0cea83f | 118 | } |
65a3629e MP |
119 | argv_array_push(&pack_objects.args, "pack-objects"); |
120 | argv_array_push(&pack_objects.args, "--revs"); | |
cdab4858 | 121 | if (use_thin_pack) |
65a3629e | 122 | argv_array_push(&pack_objects.args, "--thin"); |
75bfc6c2 | 123 | |
65a3629e | 124 | argv_array_push(&pack_objects.args, "--stdout"); |
2dacf26d | 125 | if (shallow_nr) |
65a3629e | 126 | argv_array_push(&pack_objects.args, "--shallow"); |
cc41fa8d | 127 | if (!no_progress) |
65a3629e | 128 | argv_array_push(&pack_objects.args, "--progress"); |
cc41fa8d | 129 | if (use_ofs_delta) |
65a3629e | 130 | argv_array_push(&pack_objects.args, "--delta-base-offset"); |
348e390b | 131 | if (use_include_tag) |
65a3629e | 132 | argv_array_push(&pack_objects.args, "--include-tag"); |
cc41fa8d | 133 | |
b9612197 | 134 | pack_objects.in = -1; |
cc41fa8d JS |
135 | pack_objects.out = -1; |
136 | pack_objects.err = -1; | |
21edd3f1 | 137 | |
4c324c00 | 138 | if (start_command(&pack_objects)) |
7e44c935 | 139 | die("git upload-pack: unable to fork git-pack-objects"); |
b1c71b72 | 140 | |
cdab4858 NTND |
141 | pipe_fd = xfdopen(pack_objects.in, "w"); |
142 | ||
b790e0f6 NTND |
143 | if (shallow_nr) |
144 | for_each_commit_graft(write_one_shallow, pipe_fd); | |
145 | ||
cdab4858 NTND |
146 | for (i = 0; i < want_obj.nr; i++) |
147 | fprintf(pipe_fd, "%s\n", | |
f2fd0760 | 148 | oid_to_hex(&want_obj.objects[i].item->oid)); |
cdab4858 NTND |
149 | fprintf(pipe_fd, "--not\n"); |
150 | for (i = 0; i < have_obj.nr; i++) | |
151 | fprintf(pipe_fd, "%s\n", | |
f2fd0760 | 152 | oid_to_hex(&have_obj.objects[i].item->oid)); |
cdab4858 NTND |
153 | for (i = 0; i < extra_edge_obj.nr; i++) |
154 | fprintf(pipe_fd, "%s\n", | |
f2fd0760 | 155 | oid_to_hex(&extra_edge_obj.objects[i].item->oid)); |
cdab4858 NTND |
156 | fprintf(pipe_fd, "\n"); |
157 | fflush(pipe_fd); | |
158 | fclose(pipe_fd); | |
f0cea83f | 159 | |
cc41fa8d JS |
160 | /* We read from pack_objects.err to capture stderr output for |
161 | * progress bar, and pack_objects.out to capture the pack data. | |
b1c71b72 | 162 | */ |
b1c71b72 JH |
163 | |
164 | while (1) { | |
b1c71b72 | 165 | struct pollfd pfd[2]; |
363b7817 | 166 | int pe, pu, pollsize; |
05e95155 | 167 | int ret; |
b1c71b72 | 168 | |
0d516ada ML |
169 | reset_timeout(); |
170 | ||
b1c71b72 | 171 | pollsize = 0; |
363b7817 | 172 | pe = pu = -1; |
b1c71b72 | 173 | |
cc41fa8d JS |
174 | if (0 <= pack_objects.out) { |
175 | pfd[pollsize].fd = pack_objects.out; | |
b1c71b72 JH |
176 | pfd[pollsize].events = POLLIN; |
177 | pu = pollsize; | |
178 | pollsize++; | |
179 | } | |
cc41fa8d JS |
180 | if (0 <= pack_objects.err) { |
181 | pfd[pollsize].fd = pack_objects.err; | |
363b7817 JH |
182 | pfd[pollsize].events = POLLIN; |
183 | pe = pollsize; | |
184 | pollsize++; | |
185 | } | |
b1c71b72 | 186 | |
4c324c00 JS |
187 | if (!pollsize) |
188 | break; | |
189 | ||
6c71f8b0 ET |
190 | ret = poll(pfd, pollsize, |
191 | keepalive < 0 ? -1 : 1000 * keepalive); | |
192 | ||
05e95155 | 193 | if (ret < 0) { |
4c324c00 | 194 | if (errno != EINTR) { |
d2b6afa2 | 195 | error_errno("poll failed, resuming"); |
4c324c00 | 196 | sleep(1); |
b1c71b72 | 197 | } |
4c324c00 JS |
198 | continue; |
199 | } | |
6b59f51b NP |
200 | if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) { |
201 | /* Status ready; we ship that in the side-band | |
202 | * or dump to the standard error. | |
203 | */ | |
204 | sz = xread(pack_objects.err, progress, | |
205 | sizeof(progress)); | |
206 | if (0 < sz) | |
207 | send_client_data(2, progress, sz); | |
208 | else if (sz == 0) { | |
209 | close(pack_objects.err); | |
210 | pack_objects.err = -1; | |
211 | } | |
212 | else | |
213 | goto fail; | |
214 | /* give priority to status messages */ | |
215 | continue; | |
216 | } | |
4c324c00 JS |
217 | if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) { |
218 | /* Data ready; we keep the last byte to ourselves | |
219 | * in case we detect broken rev-list, so that we | |
220 | * can leave the stream corrupted. This is | |
221 | * unfortunate -- unpack-objects would happily | |
222 | * accept a valid packdata with trailing garbage, | |
223 | * so appending garbage after we pass all the | |
224 | * pack data is not good enough to signal | |
225 | * breakage to downstream. | |
226 | */ | |
227 | char *cp = data; | |
228 | ssize_t outsz = 0; | |
229 | if (0 <= buffered) { | |
230 | *cp++ = buffered; | |
231 | outsz++; | |
b1c71b72 | 232 | } |
4c324c00 JS |
233 | sz = xread(pack_objects.out, cp, |
234 | sizeof(data) - outsz); | |
235 | if (0 < sz) | |
1456b043 | 236 | ; |
4c324c00 JS |
237 | else if (sz == 0) { |
238 | close(pack_objects.out); | |
239 | pack_objects.out = -1; | |
363b7817 | 240 | } |
4c324c00 JS |
241 | else |
242 | goto fail; | |
243 | sz += outsz; | |
244 | if (1 < sz) { | |
245 | buffered = data[sz-1] & 0xFF; | |
246 | sz--; | |
b1c71b72 | 247 | } |
4c324c00 JS |
248 | else |
249 | buffered = -1; | |
fcf0fe9e | 250 | send_client_data(1, data, sz); |
4c324c00 | 251 | } |
05e95155 JK |
252 | |
253 | /* | |
254 | * We hit the keepalive timeout without saying anything; send | |
255 | * an empty message on the data sideband just to let the other | |
256 | * side know we're still working on it, but don't have any data | |
257 | * yet. | |
258 | * | |
259 | * If we don't have a sideband channel, there's no room in the | |
260 | * protocol to say anything, so those clients are just out of | |
261 | * luck. | |
262 | */ | |
263 | if (!ret && use_sideband) { | |
264 | static const char buf[] = "0005\1"; | |
265 | write_or_die(1, buf, 5); | |
266 | } | |
4c324c00 | 267 | } |
b1c71b72 | 268 | |
4c324c00 | 269 | if (finish_command(&pack_objects)) { |
7e44c935 | 270 | error("git upload-pack: git-pack-objects died with error."); |
4c324c00 JS |
271 | goto fail; |
272 | } | |
b1c71b72 | 273 | |
4c324c00 JS |
274 | /* flush the data */ |
275 | if (0 <= buffered) { | |
276 | data[0] = buffered; | |
fcf0fe9e | 277 | send_client_data(1, data, 1); |
4c324c00 | 278 | fprintf(stderr, "flushed.\n"); |
b1c71b72 | 279 | } |
4c324c00 JS |
280 | if (use_sideband) |
281 | packet_flush(1); | |
282 | return; | |
283 | ||
b1c71b72 | 284 | fail: |
583b7ea3 | 285 | send_client_data(3, abort_msg, sizeof(abort_msg)); |
7e44c935 | 286 | die("git upload-pack: %s", abort_msg); |
fb9040cc LT |
287 | } |
288 | ||
8bf3b758 | 289 | static int got_sha1(const char *hex, unsigned char *sha1) |
def88e9a | 290 | { |
b1e9fff7 | 291 | struct object *o; |
937a515a | 292 | int we_knew_they_have = 0; |
b1e9fff7 | 293 | |
def88e9a | 294 | if (get_sha1_hex(hex, sha1)) |
7e44c935 | 295 | die("git upload-pack: expected SHA1 object, got '%s'", hex); |
fb9040cc | 296 | if (!has_sha1_file(sha1)) |
937a515a | 297 | return -1; |
b1e9fff7 | 298 | |
a6eec126 | 299 | o = parse_object(sha1); |
b1e9fff7 JH |
300 | if (!o) |
301 | die("oops (%s)", sha1_to_hex(sha1)); | |
182a8dab | 302 | if (o->type == OBJ_COMMIT) { |
b1e9fff7 | 303 | struct commit_list *parents; |
937a515a | 304 | struct commit *commit = (struct commit *)o; |
b1e9fff7 | 305 | if (o->flags & THEY_HAVE) |
937a515a JH |
306 | we_knew_they_have = 1; |
307 | else | |
308 | o->flags |= THEY_HAVE; | |
309 | if (!oldest_have || (commit->date < oldest_have)) | |
310 | oldest_have = commit->date; | |
311 | for (parents = commit->parents; | |
b1e9fff7 JH |
312 | parents; |
313 | parents = parents->next) | |
314 | parents->item->object.flags |= THEY_HAVE; | |
fb9040cc | 315 | } |
937a515a JH |
316 | if (!we_knew_they_have) { |
317 | add_object_array(o, NULL, &have_obj); | |
318 | return 1; | |
319 | } | |
320 | return 0; | |
321 | } | |
322 | ||
323 | static int reachable(struct commit *want) | |
324 | { | |
5411b10c | 325 | struct prio_queue work = { compare_commits_by_commit_date }; |
937a515a | 326 | |
5411b10c JK |
327 | prio_queue_put(&work, want); |
328 | while (work.nr) { | |
e510ab89 | 329 | struct commit_list *list; |
5411b10c | 330 | struct commit *commit = prio_queue_get(&work); |
937a515a JH |
331 | |
332 | if (commit->object.flags & THEY_HAVE) { | |
333 | want->object.flags |= COMMON_KNOWN; | |
334 | break; | |
335 | } | |
336 | if (!commit->object.parsed) | |
ed1c9977 | 337 | parse_object(commit->object.oid.hash); |
937a515a JH |
338 | if (commit->object.flags & REACHABLE) |
339 | continue; | |
340 | commit->object.flags |= REACHABLE; | |
341 | if (commit->date < oldest_have) | |
342 | continue; | |
343 | for (list = commit->parents; list; list = list->next) { | |
344 | struct commit *parent = list->item; | |
345 | if (!(parent->object.flags & REACHABLE)) | |
5411b10c | 346 | prio_queue_put(&work, parent); |
937a515a JH |
347 | } |
348 | } | |
349 | want->object.flags |= REACHABLE; | |
350 | clear_commit_marks(want, REACHABLE); | |
5411b10c | 351 | clear_prio_queue(&work); |
937a515a JH |
352 | return (want->object.flags & COMMON_KNOWN); |
353 | } | |
354 | ||
355 | static int ok_to_give_up(void) | |
356 | { | |
357 | int i; | |
358 | ||
359 | if (!have_obj.nr) | |
360 | return 0; | |
361 | ||
362 | for (i = 0; i < want_obj.nr; i++) { | |
363 | struct object *want = want_obj.objects[i].item; | |
364 | ||
365 | if (want->flags & COMMON_KNOWN) | |
366 | continue; | |
367 | want = deref_tag(want, "a want line", 0); | |
368 | if (!want || want->type != OBJ_COMMIT) { | |
369 | /* no way to tell if this is reachable by | |
370 | * looking at the ancestry chain alone, so | |
371 | * leave a note to ourselves not to worry about | |
372 | * this object anymore. | |
373 | */ | |
374 | want_obj.objects[i].item->flags |= COMMON_KNOWN; | |
375 | continue; | |
376 | } | |
377 | if (!reachable((struct commit *)want)) | |
378 | return 0; | |
379 | } | |
fb9040cc | 380 | return 1; |
def88e9a LT |
381 | } |
382 | ||
383 | static int get_common_commits(void) | |
384 | { | |
c04c4e57 | 385 | unsigned char sha1[20]; |
78affc49 | 386 | char last_hex[41]; |
49bee717 SP |
387 | int got_common = 0; |
388 | int got_other = 0; | |
4e10cf9a | 389 | int sent_ready = 0; |
def88e9a | 390 | |
f0243f26 JS |
391 | save_commit_buffer = 0; |
392 | ||
eeefa7c9 | 393 | for (;;) { |
74543a04 | 394 | char *line = packet_read_line(0, NULL); |
8bf3b758 NTND |
395 | const char *arg; |
396 | ||
960deccb | 397 | reset_timeout(); |
def88e9a | 398 | |
74543a04 | 399 | if (!line) { |
49bee717 | 400 | if (multi_ack == 2 && got_common |
4e10cf9a JH |
401 | && !got_other && ok_to_give_up()) { |
402 | sent_ready = 1; | |
81c634e9 | 403 | packet_write_fmt(1, "ACK %s ready\n", last_hex); |
4e10cf9a | 404 | } |
b1e9fff7 | 405 | if (have_obj.nr == 0 || multi_ack) |
81c634e9 | 406 | packet_write_fmt(1, "NAK\n"); |
4e10cf9a JH |
407 | |
408 | if (no_done && sent_ready) { | |
81c634e9 | 409 | packet_write_fmt(1, "ACK %s\n", last_hex); |
4e10cf9a JH |
410 | return 0; |
411 | } | |
42526b47 SP |
412 | if (stateless_rpc) |
413 | exit(0); | |
49bee717 SP |
414 | got_common = 0; |
415 | got_other = 0; | |
def88e9a LT |
416 | continue; |
417 | } | |
8bf3b758 NTND |
418 | if (skip_prefix(line, "have ", &arg)) { |
419 | switch (got_sha1(arg, sha1)) { | |
937a515a | 420 | case -1: /* they have what we do not */ |
49bee717 | 421 | got_other = 1; |
78affc49 SP |
422 | if (multi_ack && ok_to_give_up()) { |
423 | const char *hex = sha1_to_hex(sha1); | |
4e10cf9a JH |
424 | if (multi_ack == 2) { |
425 | sent_ready = 1; | |
81c634e9 | 426 | packet_write_fmt(1, "ACK %s ready\n", hex); |
4e10cf9a | 427 | } else |
81c634e9 | 428 | packet_write_fmt(1, "ACK %s continue\n", hex); |
78affc49 | 429 | } |
937a515a JH |
430 | break; |
431 | default: | |
49bee717 | 432 | got_common = 1; |
78affc49 SP |
433 | memcpy(last_hex, sha1_to_hex(sha1), 41); |
434 | if (multi_ack == 2) | |
81c634e9 | 435 | packet_write_fmt(1, "ACK %s common\n", last_hex); |
78affc49 | 436 | else if (multi_ack) |
81c634e9 | 437 | packet_write_fmt(1, "ACK %s continue\n", last_hex); |
c04c4e57 | 438 | else if (have_obj.nr == 1) |
81c634e9 | 439 | packet_write_fmt(1, "ACK %s\n", last_hex); |
937a515a | 440 | break; |
af2d3aa4 | 441 | } |
def88e9a LT |
442 | continue; |
443 | } | |
444 | if (!strcmp(line, "done")) { | |
b1e9fff7 | 445 | if (have_obj.nr > 0) { |
1bd8c8f0 | 446 | if (multi_ack) |
81c634e9 | 447 | packet_write_fmt(1, "ACK %s\n", last_hex); |
1bd8c8f0 JS |
448 | return 0; |
449 | } | |
81c634e9 | 450 | packet_write_fmt(1, "NAK\n"); |
def88e9a LT |
451 | return -1; |
452 | } | |
7e44c935 | 453 | die("git upload-pack: expected SHA1 list, got '%s'", line); |
def88e9a | 454 | } |
def88e9a LT |
455 | } |
456 | ||
390eb36b JH |
457 | static int is_our_ref(struct object *o) |
458 | { | |
68ee6289 FM |
459 | int allow_hidden_ref = (allow_unadvertised_object_request & |
460 | (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)); | |
7199c093 | 461 | return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF); |
390eb36b JH |
462 | } |
463 | ||
2997178e NTND |
464 | /* |
465 | * on successful case, it's up to the caller to close cmd->out | |
466 | */ | |
467 | static int do_reachable_revlist(struct child_process *cmd, | |
079aa97e NTND |
468 | struct object_array *src, |
469 | struct object_array *reachable) | |
051e4005 JH |
470 | { |
471 | static const char *argv[] = { | |
472 | "rev-list", "--stdin", NULL, | |
473 | }; | |
051e4005 JH |
474 | struct object *o; |
475 | char namebuf[42]; /* ^ + SHA-1 + LF */ | |
476 | int i; | |
477 | ||
2997178e NTND |
478 | cmd->argv = argv; |
479 | cmd->git_cmd = 1; | |
480 | cmd->no_stderr = 1; | |
481 | cmd->in = -1; | |
482 | cmd->out = -1; | |
051e4005 JH |
483 | |
484 | /* | |
7fcbd37f NTND |
485 | * If the next rev-list --stdin encounters an unknown commit, |
486 | * it terminates, which will cause SIGPIPE in the write loop | |
051e4005 JH |
487 | * below. |
488 | */ | |
489 | sigchain_push(SIGPIPE, SIG_IGN); | |
490 | ||
2997178e | 491 | if (start_command(cmd)) |
7fcbd37f NTND |
492 | goto error; |
493 | ||
051e4005 JH |
494 | namebuf[0] = '^'; |
495 | namebuf[41] = '\n'; | |
496 | for (i = get_max_object_index(); 0 < i; ) { | |
497 | o = get_indexed_object(--i); | |
2a745324 BH |
498 | if (!o) |
499 | continue; | |
079aa97e NTND |
500 | if (reachable && o->type == OBJ_COMMIT) |
501 | o->flags &= ~TMP_MARK; | |
390eb36b | 502 | if (!is_our_ref(o)) |
051e4005 | 503 | continue; |
f2fd0760 | 504 | memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ); |
2997178e | 505 | if (write_in_full(cmd->in, namebuf, 42) < 0) |
051e4005 JH |
506 | goto error; |
507 | } | |
508 | namebuf[40] = '\n'; | |
3f0f6624 NTND |
509 | for (i = 0; i < src->nr; i++) { |
510 | o = src->objects[i].item; | |
079aa97e NTND |
511 | if (is_our_ref(o)) { |
512 | if (reachable) | |
513 | add_object_array(o, NULL, reachable); | |
051e4005 | 514 | continue; |
079aa97e NTND |
515 | } |
516 | if (reachable && o->type == OBJ_COMMIT) | |
517 | o->flags |= TMP_MARK; | |
f2fd0760 | 518 | memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ); |
2997178e | 519 | if (write_in_full(cmd->in, namebuf, 41) < 0) |
051e4005 JH |
520 | goto error; |
521 | } | |
2997178e NTND |
522 | close(cmd->in); |
523 | cmd->in = -1; | |
524 | sigchain_pop(SIGPIPE); | |
051e4005 | 525 | |
2997178e NTND |
526 | return 0; |
527 | ||
528 | error: | |
051e4005 JH |
529 | sigchain_pop(SIGPIPE); |
530 | ||
2997178e NTND |
531 | if (cmd->in >= 0) |
532 | close(cmd->in); | |
533 | if (cmd->out >= 0) | |
534 | close(cmd->out); | |
535 | return -1; | |
536 | } | |
537 | ||
079aa97e NTND |
538 | static int get_reachable_list(struct object_array *src, |
539 | struct object_array *reachable) | |
540 | { | |
541 | struct child_process cmd = CHILD_PROCESS_INIT; | |
542 | int i; | |
543 | struct object *o; | |
544 | char namebuf[42]; /* ^ + SHA-1 + LF */ | |
545 | ||
546 | if (do_reachable_revlist(&cmd, src, reachable) < 0) | |
547 | return -1; | |
548 | ||
549 | while ((i = read_in_full(cmd.out, namebuf, 41)) == 41) { | |
550 | struct object_id sha1; | |
551 | ||
552 | if (namebuf[40] != '\n' || get_oid_hex(namebuf, &sha1)) | |
553 | break; | |
554 | ||
555 | o = lookup_object(sha1.hash); | |
556 | if (o && o->type == OBJ_COMMIT) { | |
557 | o->flags &= ~TMP_MARK; | |
558 | } | |
559 | } | |
560 | for (i = get_max_object_index(); 0 < i; i--) { | |
561 | o = get_indexed_object(i - 1); | |
562 | if (o && o->type == OBJ_COMMIT && | |
563 | (o->flags & TMP_MARK)) { | |
564 | add_object_array(o, NULL, reachable); | |
565 | o->flags &= ~TMP_MARK; | |
566 | } | |
567 | } | |
568 | close(cmd.out); | |
569 | ||
570 | if (finish_command(&cmd)) | |
571 | return -1; | |
572 | ||
573 | return 0; | |
574 | } | |
575 | ||
2997178e NTND |
576 | static int has_unreachable(struct object_array *src) |
577 | { | |
578 | struct child_process cmd = CHILD_PROCESS_INIT; | |
579 | char buf[1]; | |
580 | int i; | |
581 | ||
079aa97e | 582 | if (do_reachable_revlist(&cmd, src, NULL) < 0) |
2997178e | 583 | return 1; |
051e4005 JH |
584 | |
585 | /* | |
586 | * The commits out of the rev-list are not ancestors of | |
587 | * our ref. | |
588 | */ | |
2997178e | 589 | i = read_in_full(cmd.out, buf, 1); |
051e4005 JH |
590 | if (i) |
591 | goto error; | |
592 | close(cmd.out); | |
7fcbd37f | 593 | cmd.out = -1; |
051e4005 JH |
594 | |
595 | /* | |
596 | * rev-list may have died by encountering a bad commit | |
597 | * in the history, in which case we do want to bail out | |
598 | * even when it showed no commit. | |
599 | */ | |
600 | if (finish_command(&cmd)) | |
601 | goto error; | |
602 | ||
603 | /* All the non-tip ones are ancestors of what we advertised */ | |
3f0f6624 | 604 | return 0; |
051e4005 JH |
605 | |
606 | error: | |
7fcbd37f | 607 | sigchain_pop(SIGPIPE); |
7fcbd37f NTND |
608 | if (cmd.out >= 0) |
609 | close(cmd.out); | |
3f0f6624 NTND |
610 | return 1; |
611 | } | |
7fcbd37f | 612 | |
3f0f6624 NTND |
613 | static void check_non_tip(void) |
614 | { | |
615 | int i; | |
616 | ||
617 | /* | |
618 | * In the normal in-process case without | |
619 | * uploadpack.allowReachableSHA1InWant, | |
620 | * non-tip requests can never happen. | |
621 | */ | |
622 | if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1)) | |
623 | goto error; | |
624 | if (!has_unreachable(&want_obj)) | |
625 | /* All the non-tip ones are ancestors of what we advertised */ | |
626 | return; | |
051e4005 JH |
627 | |
628 | error: | |
629 | /* Pick one of them (we know there at least is one) */ | |
630 | for (i = 0; i < want_obj.nr; i++) { | |
3f0f6624 | 631 | struct object *o = want_obj.objects[i].item; |
390eb36b | 632 | if (!is_our_ref(o)) |
051e4005 | 633 | die("git upload-pack: not our ref %s", |
f2fd0760 | 634 | oid_to_hex(&o->oid)); |
051e4005 JH |
635 | } |
636 | } | |
637 | ||
5c24cdea NTND |
638 | static void send_shallow(struct commit_list *result) |
639 | { | |
640 | while (result) { | |
641 | struct object *object = &result->item->object; | |
642 | if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) { | |
dbaa6bdc JH |
643 | packet_write_fmt(1, "shallow %s", |
644 | oid_to_hex(&object->oid)); | |
5c24cdea NTND |
645 | register_shallow(object->oid.hash); |
646 | shallow_nr++; | |
647 | } | |
648 | result = result->next; | |
649 | } | |
650 | } | |
651 | ||
873700c9 | 652 | static void send_unshallow(const struct object_array *shallows) |
e8e44de7 | 653 | { |
e8e44de7 | 654 | int i; |
873700c9 | 655 | |
e8e44de7 NTND |
656 | for (i = 0; i < shallows->nr; i++) { |
657 | struct object *object = shallows->objects[i].item; | |
658 | if (object->flags & NOT_SHALLOW) { | |
659 | struct commit_list *parents; | |
dbaa6bdc JH |
660 | packet_write_fmt(1, "unshallow %s", |
661 | oid_to_hex(&object->oid)); | |
e8e44de7 | 662 | object->flags &= ~CLIENT_SHALLOW; |
873700c9 NTND |
663 | /* |
664 | * We want to _register_ "object" as shallow, but we | |
665 | * also need to traverse object's parents to deepen a | |
666 | * shallow clone. Unregister it for now so we can | |
667 | * parse and add the parents to the want list, then | |
668 | * re-register it. | |
669 | */ | |
e8e44de7 NTND |
670 | unregister_shallow(object->oid.hash); |
671 | object->parsed = 0; | |
672 | parse_commit_or_die((struct commit *)object); | |
673 | parents = ((struct commit *)object)->parents; | |
674 | while (parents) { | |
675 | add_object_array(&parents->item->object, | |
676 | NULL, &want_obj); | |
677 | parents = parents->next; | |
678 | } | |
679 | add_object_array(object, NULL, &extra_edge_obj); | |
680 | } | |
681 | /* make sure commit traversal conforms to client */ | |
682 | register_shallow(object->oid.hash); | |
683 | } | |
873700c9 NTND |
684 | } |
685 | ||
cccf74e2 NTND |
686 | static void deepen(int depth, int deepen_relative, |
687 | struct object_array *shallows) | |
873700c9 NTND |
688 | { |
689 | if (depth == INFINITE_DEPTH && !is_repository_shallow()) { | |
690 | int i; | |
691 | ||
692 | for (i = 0; i < shallows->nr; i++) { | |
693 | struct object *object = shallows->objects[i].item; | |
694 | object->flags |= NOT_SHALLOW; | |
695 | } | |
cccf74e2 NTND |
696 | } else if (deepen_relative) { |
697 | struct object_array reachable_shallows = OBJECT_ARRAY_INIT; | |
698 | struct commit_list *result; | |
699 | ||
700 | get_reachable_list(shallows, &reachable_shallows); | |
701 | result = get_shallow_commits(&reachable_shallows, | |
702 | depth + 1, | |
703 | SHALLOW, NOT_SHALLOW); | |
704 | send_shallow(result); | |
705 | free_commit_list(result); | |
706 | object_array_clear(&reachable_shallows); | |
873700c9 NTND |
707 | } else { |
708 | struct commit_list *result; | |
709 | ||
710 | result = get_shallow_commits(&want_obj, depth, | |
711 | SHALLOW, NOT_SHALLOW); | |
712 | send_shallow(result); | |
713 | free_commit_list(result); | |
714 | } | |
715 | ||
716 | send_unshallow(shallows); | |
e8e44de7 NTND |
717 | packet_flush(1); |
718 | } | |
719 | ||
569e554b NTND |
720 | static void deepen_by_rev_list(int ac, const char **av, |
721 | struct object_array *shallows) | |
722 | { | |
723 | struct commit_list *result; | |
724 | ||
725 | result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW); | |
726 | send_shallow(result); | |
727 | free_commit_list(result); | |
728 | send_unshallow(shallows); | |
729 | packet_flush(1); | |
730 | } | |
731 | ||
b1e9fff7 | 732 | static void receive_needs(void) |
fb9040cc | 733 | { |
3cd47459 | 734 | struct object_array shallows = OBJECT_ARRAY_INIT; |
269a7a83 | 735 | struct string_list deepen_not = STRING_LIST_INIT_DUP; |
74543a04 | 736 | int depth = 0; |
051e4005 | 737 | int has_non_tip = 0; |
569e554b NTND |
738 | unsigned long deepen_since = 0; |
739 | int deepen_rev_list = 0; | |
fb9040cc | 740 | |
f0cea83f | 741 | shallow_nr = 0; |
fb9040cc | 742 | for (;;) { |
565ebbf7 | 743 | struct object *o; |
f47182c8 | 744 | const char *features; |
6ece0d30 | 745 | unsigned char sha1_buf[20]; |
74543a04 | 746 | char *line = packet_read_line(0, NULL); |
8bf3b758 NTND |
747 | const char *arg; |
748 | ||
960deccb | 749 | reset_timeout(); |
74543a04 | 750 | if (!line) |
ed09aef0 | 751 | break; |
e091eb93 | 752 | |
8bf3b758 | 753 | if (skip_prefix(line, "shallow ", &arg)) { |
ed09aef0 JS |
754 | unsigned char sha1[20]; |
755 | struct object *object; | |
8bf3b758 | 756 | if (get_sha1_hex(arg, sha1)) |
ed09aef0 JS |
757 | die("invalid shallow line: %s", line); |
758 | object = parse_object(sha1); | |
759 | if (!object) | |
af04fa2a | 760 | continue; |
6293ded3 NTND |
761 | if (object->type != OBJ_COMMIT) |
762 | die("invalid shallow object %s", sha1_to_hex(sha1)); | |
e58e57e4 JK |
763 | if (!(object->flags & CLIENT_SHALLOW)) { |
764 | object->flags |= CLIENT_SHALLOW; | |
765 | add_object_array(object, NULL, &shallows); | |
766 | } | |
ed09aef0 JS |
767 | continue; |
768 | } | |
8bf3b758 | 769 | if (skip_prefix(line, "deepen ", &arg)) { |
6e414e30 | 770 | char *end = NULL; |
8bf3b758 | 771 | depth = strtol(arg, &end, 0); |
6e414e30 | 772 | if (!end || *end || depth <= 0) |
016e6ccb JS |
773 | die("Invalid deepen: %s", line); |
774 | continue; | |
775 | } | |
569e554b NTND |
776 | if (skip_prefix(line, "deepen-since ", &arg)) { |
777 | char *end = NULL; | |
778 | deepen_since = strtoul(arg, &end, 0); | |
779 | if (!end || *end || !deepen_since || | |
780 | /* revisions.c's max_age -1 is special */ | |
781 | deepen_since == -1) | |
782 | die("Invalid deepen-since: %s", line); | |
783 | deepen_rev_list = 1; | |
784 | continue; | |
785 | } | |
269a7a83 NTND |
786 | if (skip_prefix(line, "deepen-not ", &arg)) { |
787 | char *ref = NULL; | |
788 | unsigned char sha1[20]; | |
789 | if (expand_ref(arg, strlen(arg), sha1, &ref) != 1) | |
790 | die("git upload-pack: ambiguous deepen-not: %s", line); | |
791 | string_list_append(&deepen_not, ref); | |
792 | free(ref); | |
793 | deepen_rev_list = 1; | |
794 | continue; | |
795 | } | |
8bf3b758 NTND |
796 | if (!skip_prefix(line, "want ", &arg) || |
797 | get_sha1_hex(arg, sha1_buf)) | |
7e44c935 | 798 | die("git upload-pack: protocol error, " |
e091eb93 | 799 | "expected to get sha, not '%s'", line); |
f47182c8 | 800 | |
8bf3b758 | 801 | features = arg + 40; |
f47182c8 | 802 | |
cccf74e2 NTND |
803 | if (parse_feature_request(features, "deepen-relative")) |
804 | deepen_relative = 1; | |
f47182c8 | 805 | if (parse_feature_request(features, "multi_ack_detailed")) |
78affc49 | 806 | multi_ack = 2; |
f47182c8 | 807 | else if (parse_feature_request(features, "multi_ack")) |
1bd8c8f0 | 808 | multi_ack = 1; |
f47182c8 | 809 | if (parse_feature_request(features, "no-done")) |
4e10cf9a | 810 | no_done = 1; |
f47182c8 | 811 | if (parse_feature_request(features, "thin-pack")) |
b19696c2 | 812 | use_thin_pack = 1; |
f47182c8 | 813 | if (parse_feature_request(features, "ofs-delta")) |
e4fe4b8e | 814 | use_ofs_delta = 1; |
f47182c8 | 815 | if (parse_feature_request(features, "side-band-64k")) |
d47f3db7 | 816 | use_sideband = LARGE_PACKET_MAX; |
f47182c8 | 817 | else if (parse_feature_request(features, "side-band")) |
d47f3db7 | 818 | use_sideband = DEFAULT_PACKET_MAX; |
f47182c8 | 819 | if (parse_feature_request(features, "no-progress")) |
b0e90897 | 820 | no_progress = 1; |
f47182c8 | 821 | if (parse_feature_request(features, "include-tag")) |
348e390b | 822 | use_include_tag = 1; |
565ebbf7 | 823 | |
f59de5d1 | 824 | o = parse_object(sha1_buf); |
051e4005 | 825 | if (!o) |
9f9aa761 EN |
826 | die("git upload-pack: not our ref %s", |
827 | sha1_to_hex(sha1_buf)); | |
565ebbf7 JH |
828 | if (!(o->flags & WANTED)) { |
829 | o->flags |= WANTED; | |
f8edeaa0 DT |
830 | if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1 |
831 | || is_our_ref(o))) | |
051e4005 | 832 | has_non_tip = 1; |
b1e9fff7 | 833 | add_object_array(o, NULL, &want_obj); |
565ebbf7 | 834 | } |
fb9040cc | 835 | } |
9462e3f5 | 836 | |
051e4005 JH |
837 | /* |
838 | * We have sent all our refs already, and the other end | |
839 | * should have chosen out of them. When we are operating | |
840 | * in the stateless RPC mode, however, their choice may | |
841 | * have been based on the set of older refs advertised | |
842 | * by another process that handled the initial request. | |
843 | */ | |
844 | if (has_non_tip) | |
845 | check_non_tip(); | |
846 | ||
9462e3f5 JS |
847 | if (!use_sideband && daemon_mode) |
848 | no_progress = 1; | |
849 | ||
569e554b | 850 | if (depth == 0 && !deepen_rev_list && shallows.nr == 0) |
f53514bc | 851 | return; |
569e554b | 852 | if (depth > 0 && deepen_rev_list) |
269a7a83 | 853 | die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together"); |
e8e44de7 | 854 | if (depth > 0) |
cccf74e2 | 855 | deepen(depth, deepen_relative, &shallows); |
569e554b NTND |
856 | else if (deepen_rev_list) { |
857 | struct argv_array av = ARGV_ARRAY_INIT; | |
f53514bc | 858 | int i; |
569e554b NTND |
859 | |
860 | argv_array_push(&av, "rev-list"); | |
861 | if (deepen_since) | |
862 | argv_array_pushf(&av, "--max-age=%lu", deepen_since); | |
269a7a83 NTND |
863 | if (deepen_not.nr) { |
864 | argv_array_push(&av, "--not"); | |
865 | for (i = 0; i < deepen_not.nr; i++) { | |
866 | struct string_list_item *s = deepen_not.items + i; | |
867 | argv_array_push(&av, s->string); | |
f53514bc | 868 | } |
269a7a83 | 869 | argv_array_push(&av, "--not"); |
016e6ccb | 870 | } |
569e554b NTND |
871 | for (i = 0; i < want_obj.nr; i++) { |
872 | struct object *o = want_obj.objects[i].item; | |
873 | argv_array_push(&av, oid_to_hex(&o->oid)); | |
f53514bc | 874 | } |
569e554b NTND |
875 | deepen_by_rev_list(av.argc, av.argv, &shallows); |
876 | argv_array_clear(&av); | |
877 | } | |
e8e44de7 | 878 | else |
f53514bc JS |
879 | if (shallows.nr > 0) { |
880 | int i; | |
881 | for (i = 0; i < shallows.nr; i++) | |
ed1c9977 | 882 | register_shallow(shallows.objects[i].item->oid.hash); |
f53514bc | 883 | } |
f0cea83f NE |
884 | |
885 | shallow_nr += shallows.nr; | |
f53514bc | 886 | free(shallows.objects); |
fb9040cc LT |
887 | } |
888 | ||
daebaa78 | 889 | /* return non-zero if the ref is hidden, otherwise 0 */ |
78a766ab LF |
890 | static int mark_our_ref(const char *refname, const char *refname_full, |
891 | const struct object_id *oid) | |
cbbe50db | 892 | { |
363e98bf | 893 | struct object *o = lookup_unknown_object(oid->hash); |
daebaa78 | 894 | |
78a766ab | 895 | if (ref_is_hidden(refname, refname_full)) { |
390eb36b | 896 | o->flags |= HIDDEN_REF; |
daebaa78 | 897 | return 1; |
390eb36b | 898 | } |
3f1da57f | 899 | o->flags |= OUR_REF; |
cbbe50db JH |
900 | return 0; |
901 | } | |
902 | ||
78a766ab | 903 | static int check_ref(const char *refname_full, const struct object_id *oid, |
363e98bf | 904 | int flag, void *cb_data) |
e172755b | 905 | { |
78a766ab LF |
906 | const char *refname = strip_namespace(refname_full); |
907 | ||
908 | mark_our_ref(refname, refname_full, oid); | |
e172755b JK |
909 | return 0; |
910 | } | |
911 | ||
7171d8c1 JH |
912 | static void format_symref_info(struct strbuf *buf, struct string_list *symref) |
913 | { | |
914 | struct string_list_item *item; | |
915 | ||
916 | if (!symref->nr) | |
917 | return; | |
918 | for_each_string_list_item(item, symref) | |
919 | strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util); | |
920 | } | |
921 | ||
363e98bf MH |
922 | static int send_ref(const char *refname, const struct object_id *oid, |
923 | int flag, void *cb_data) | |
def88e9a | 924 | { |
ed09aef0 | 925 | static const char *capabilities = "multi_ack thin-pack side-band" |
cccf74e2 NTND |
926 | " side-band-64k ofs-delta shallow deepen-since deepen-not" |
927 | " deepen-relative no-progress include-tag multi_ack_detailed"; | |
6b01ecfe | 928 | const char *refname_nons = strip_namespace(refname); |
21758aff | 929 | struct object_id peeled; |
b5b16990 | 930 | |
78a766ab | 931 | if (mark_our_ref(refname_nons, refname, oid)) |
daebaa78 | 932 | return 0; |
cbbe50db | 933 | |
7171d8c1 JH |
934 | if (capabilities) { |
935 | struct strbuf symref_info = STRBUF_INIT; | |
936 | ||
937 | format_symref_info(&symref_info, cb_data); | |
81c634e9 | 938 | packet_write_fmt(1, "%s %s%c%s%s%s%s%s agent=%s\n", |
363e98bf | 939 | oid_to_hex(oid), refname_nons, |
cf2ad8e6 | 940 | 0, capabilities, |
7199c093 FM |
941 | (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ? |
942 | " allow-tip-sha1-in-want" : "", | |
68ee6289 FM |
943 | (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ? |
944 | " allow-reachable-sha1-in-want" : "", | |
ff5effdf | 945 | stateless_rpc ? " no-done" : "", |
7171d8c1 | 946 | symref_info.buf, |
ff5effdf | 947 | git_user_agent_sanitized()); |
7171d8c1 JH |
948 | strbuf_release(&symref_info); |
949 | } else { | |
81c634e9 | 950 | packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons); |
7171d8c1 | 951 | } |
1f5881bb | 952 | capabilities = NULL; |
21758aff | 953 | if (!peel_ref(refname, peeled.hash)) |
81c634e9 | 954 | packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons); |
def88e9a LT |
955 | return 0; |
956 | } | |
957 | ||
7dabd056 MH |
958 | static int find_symref(const char *refname, const struct object_id *oid, |
959 | int flag, void *cb_data) | |
7171d8c1 JH |
960 | { |
961 | const char *symref_target; | |
962 | struct string_list_item *item; | |
e45a4949 | 963 | struct object_id unused; |
7171d8c1 JH |
964 | |
965 | if ((flag & REF_ISSYMREF) == 0) | |
966 | return 0; | |
e45a4949 | 967 | symref_target = resolve_ref_unsafe(refname, 0, unused.hash, &flag); |
7171d8c1 JH |
968 | if (!symref_target || (flag & REF_ISSYMREF) == 0) |
969 | die("'%s' is a symref but it is not?", refname); | |
970 | item = string_list_append(cb_data, refname); | |
971 | item->util = xstrdup(symref_target); | |
972 | return 0; | |
973 | } | |
974 | ||
59076eba | 975 | static void upload_pack(void) |
def88e9a | 976 | { |
7171d8c1 JH |
977 | struct string_list symref = STRING_LIST_INIT_DUP; |
978 | ||
979 | head_ref_namespaced(find_symref, &symref); | |
980 | ||
42526b47 SP |
981 | if (advertise_refs || !stateless_rpc) { |
982 | reset_timeout(); | |
7171d8c1 JH |
983 | head_ref_namespaced(send_ref, &symref); |
984 | for_each_namespaced_ref(send_ref, &symref); | |
ad491366 | 985 | advertise_shallow_grafts(1); |
42526b47 SP |
986 | packet_flush(1); |
987 | } else { | |
e172755b JK |
988 | head_ref_namespaced(check_ref, NULL); |
989 | for_each_namespaced_ref(check_ref, NULL); | |
42526b47 | 990 | } |
7171d8c1 | 991 | string_list_clear(&symref, 1); |
42526b47 SP |
992 | if (advertise_refs) |
993 | return; | |
994 | ||
b1e9fff7 | 995 | receive_needs(); |
59076eba DR |
996 | if (want_obj.nr) { |
997 | get_common_commits(); | |
998 | create_pack_file(); | |
999 | } | |
def88e9a LT |
1000 | } |
1001 | ||
daebaa78 JH |
1002 | static int upload_pack_config(const char *var, const char *value, void *unused) |
1003 | { | |
7199c093 FM |
1004 | if (!strcmp("uploadpack.allowtipsha1inwant", var)) { |
1005 | if (git_config_bool(var, value)) | |
1006 | allow_unadvertised_object_request |= ALLOW_TIP_SHA1; | |
1007 | else | |
1008 | allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1; | |
68ee6289 FM |
1009 | } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) { |
1010 | if (git_config_bool(var, value)) | |
1011 | allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1; | |
1012 | else | |
1013 | allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1; | |
f8edeaa0 DT |
1014 | } else if (!strcmp("uploadpack.allowanysha1inwant", var)) { |
1015 | if (git_config_bool(var, value)) | |
1016 | allow_unadvertised_object_request |= ALLOW_ANY_SHA1; | |
1017 | else | |
1018 | allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1; | |
7199c093 | 1019 | } else if (!strcmp("uploadpack.keepalive", var)) { |
05e95155 JK |
1020 | keepalive = git_config_int(var, value); |
1021 | if (!keepalive) | |
1022 | keepalive = -1; | |
20b20a22 JK |
1023 | } else if (current_config_scope() != CONFIG_SCOPE_REPO) { |
1024 | if (!strcmp("uploadpack.packobjectshook", var)) | |
1025 | return git_config_string(&pack_objects_hook, var, value); | |
05e95155 | 1026 | } |
daebaa78 JH |
1027 | return parse_hide_refs_config(var, value, "uploadpack"); |
1028 | } | |
1029 | ||
3f2e2297 | 1030 | int cmd_main(int argc, const char **argv) |
def88e9a | 1031 | { |
9812f213 | 1032 | const char *dir; |
960deccb | 1033 | int strict = 0; |
9812f213 AQ |
1034 | struct option options[] = { |
1035 | OPT_BOOL(0, "stateless-rpc", &stateless_rpc, | |
1036 | N_("quit after a single request/response exchange")), | |
1037 | OPT_BOOL(0, "advertise-refs", &advertise_refs, | |
2e3a16b2 | 1038 | N_("exit immediately after initial ref advertisement")), |
9812f213 AQ |
1039 | OPT_BOOL(0, "strict", &strict, |
1040 | N_("do not try <directory>/.git/ if <directory> is no Git directory")), | |
1041 | OPT_INTEGER(0, "timeout", &timeout, | |
1042 | N_("interrupt transfer after <n> seconds of inactivity")), | |
1043 | OPT_END() | |
1044 | }; | |
960deccb | 1045 | |
bbc30f99 | 1046 | packet_trace_identity("upload-pack"); |
afc711b8 | 1047 | check_replace_refs = 0; |
2fb3f6db | 1048 | |
9812f213 | 1049 | argc = parse_options(argc, argv, NULL, options, upload_pack_usage, 0); |
960deccb | 1050 | |
9812f213 AQ |
1051 | if (argc != 1) |
1052 | usage_with_options(upload_pack_usage, options); | |
a6080a0a | 1053 | |
9812f213 AQ |
1054 | if (timeout) |
1055 | daemon_mode = 1; | |
04b33055 | 1056 | |
e1464ca7 | 1057 | setup_path(); |
04b33055 | 1058 | |
9812f213 | 1059 | dir = argv[0]; |
113b9475 | 1060 | |
8d630132 | 1061 | if (!enter_repo(dir, strict)) |
05ac6b34 | 1062 | die("'%s' does not appear to be a git repository", dir); |
ad491366 | 1063 | |
daebaa78 | 1064 | git_config(upload_pack_config, NULL); |
def88e9a LT |
1065 | upload_pack(); |
1066 | return 0; | |
1067 | } |