13 static const char upload_pack_usage
[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
15 /* bits #0..7 in revision.h, #8..10 in commit.c */
16 #define THEY_HAVE (1u << 11)
17 #define OUR_REF (1u << 12)
18 #define WANTED (1u << 13)
19 #define COMMON_KNOWN (1u << 14)
20 #define REACHABLE (1u << 15)
22 static unsigned long oldest_have
;
24 static int multi_ack
, nr_our_refs
;
25 static int use_thin_pack
, use_ofs_delta
;
26 static struct object_array have_obj
;
27 static struct object_array want_obj
;
28 static unsigned int timeout
;
30 * otherwise maximum packet size (up to 65520 bytes).
32 static int use_sideband
;
34 static void reset_timeout(void)
39 static int strip(char *line
, int len
)
41 if (len
&& line
[len
-1] == '\n')
46 static ssize_t
send_client_data(int fd
, const char *data
, ssize_t sz
)
49 return send_sideband(1, fd
, data
, sz
, use_sideband
);
57 return safe_write(fd
, data
, sz
);
60 static void create_pack_file(void)
62 /* Pipes between rev-list to pack-objects, pack-objects to us
63 * and pack-objects error stream for progress bar.
65 int lp_pipe
[2], pu_pipe
[2], pe_pipe
[2];
66 pid_t pid_rev_list
, pid_pack_objects
;
67 int create_full_pack
= (nr_our_refs
== want_obj
.nr
&& !have_obj
.nr
);
68 char data
[8193], progress
[128];
69 char abort_msg
[] = "aborting due to possible repository "
70 "corruption on the remote side.";
73 if (pipe(lp_pipe
) < 0)
74 die("git-upload-pack: unable to create pipe");
75 pid_rev_list
= fork();
77 die("git-upload-pack: unable to fork git-rev-list");
86 if (create_full_pack
) {
88 use_thin_pack
= 0; /* no point doing it */
91 args
= have_obj
.nr
+ want_obj
.nr
+ 5;
92 p
= xmalloc(args
* sizeof(char *));
93 argv
= (const char **) p
;
94 buf
= xmalloc(args
* 45);
101 *p
++ = use_thin_pack
? "--objects-edge" : "--objects";
102 if (create_full_pack
)
105 for (i
= 0; i
< want_obj
.nr
; i
++) {
106 struct object
*o
= want_obj
.objects
[i
].item
;
108 memcpy(buf
, sha1_to_hex(o
->sha1
), 41);
112 if (!create_full_pack
)
113 for (i
= 0; i
< have_obj
.nr
; i
++) {
114 struct object
*o
= have_obj
.objects
[i
].item
;
117 memcpy(buf
, sha1_to_hex(o
->sha1
), 41);
122 die("git-upload-pack: unable to exec git-rev-list");
125 if (pipe(pu_pipe
) < 0)
126 die("git-upload-pack: unable to create pipe");
127 if (pipe(pe_pipe
) < 0)
128 die("git-upload-pack: unable to create pipe");
129 pid_pack_objects
= fork();
130 if (pid_pack_objects
< 0) {
131 /* daemon sets things up to ignore TERM */
132 kill(pid_rev_list
, SIGKILL
);
133 die("git-upload-pack: unable to fork git-pack-objects");
135 if (!pid_pack_objects
) {
146 execl_git_cmd("pack-objects", "--stdout", "--progress",
147 use_ofs_delta
? "--delta-base-offset" : NULL
,
149 kill(pid_rev_list
, SIGKILL
);
150 die("git-upload-pack: unable to exec git-pack-objects");
156 /* We read from pe_pipe[0] to capture stderr output for
157 * progress bar, and pu_pipe[0] to capture the pack data.
164 struct pollfd pfd
[2];
168 int pe
, pu
, pollsize
;
175 if (0 <= pu_pipe
[0]) {
176 pfd
[pollsize
].fd
= pu_pipe
[0];
177 pfd
[pollsize
].events
= POLLIN
;
181 if (0 <= pe_pipe
[0]) {
182 pfd
[pollsize
].fd
= pe_pipe
[0];
183 pfd
[pollsize
].events
= POLLIN
;
189 if (poll(pfd
, pollsize
, -1) < 0) {
190 if (errno
!= EINTR
) {
191 error("poll failed, resuming: %s",
197 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
198 /* Data ready; we keep the last byte
199 * to ourselves in case we detect
200 * broken rev-list, so that we can
201 * leave the stream corrupted. This
202 * is unfortunate -- unpack-objects
203 * would happily accept a valid pack
204 * data with trailing garbage, so
205 * appending garbage after we pass all
206 * the pack data is not good enough to
207 * signal breakage to downstream.
215 sz
= read(pu_pipe
[0], cp
,
216 sizeof(data
) - outsz
);
227 buffered
= data
[sz
-1] & 0xFF;
232 sz
= send_client_data(1, data
, sz
);
236 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
237 /* Status ready; we ship that in the side-band
238 * or dump to the standard error.
240 sz
= read(pe_pipe
[0], progress
,
243 send_client_data(2, progress
, sz
);
253 /* See if the children are still there */
254 if (pid_rev_list
|| pid_pack_objects
) {
255 pid
= waitpid(-1, &status
, WNOHANG
);
258 who
= ((pid
== pid_rev_list
) ? "git-rev-list" :
259 (pid
== pid_pack_objects
) ? "git-pack-objects" :
263 error("git-upload-pack: %s",
267 error("git-upload-pack: we weren't "
268 "waiting for %d", pid
);
271 if (!WIFEXITED(status
) || WEXITSTATUS(status
) > 0) {
272 error("git-upload-pack: %s died with error.",
276 if (pid
== pid_rev_list
)
278 if (pid
== pid_pack_objects
)
279 pid_pack_objects
= 0;
280 if (pid_rev_list
|| pid_pack_objects
)
284 /* both died happily */
291 sz
= send_client_data(1, data
, 1);
294 fprintf(stderr
, "flushed.\n");
301 if (pid_pack_objects
)
302 kill(pid_pack_objects
, SIGKILL
);
304 kill(pid_rev_list
, SIGKILL
);
305 send_client_data(3, abort_msg
, sizeof(abort_msg
));
306 die("git-upload-pack: %s", abort_msg
);
309 static int got_sha1(char *hex
, unsigned char *sha1
)
312 int we_knew_they_have
= 0;
314 if (get_sha1_hex(hex
, sha1
))
315 die("git-upload-pack: expected SHA1 object, got '%s'", hex
);
316 if (!has_sha1_file(sha1
))
319 o
= lookup_object(sha1
);
320 if (!(o
&& o
->parsed
))
321 o
= parse_object(sha1
);
323 die("oops (%s)", sha1_to_hex(sha1
));
324 if (o
->type
== OBJ_COMMIT
) {
325 struct commit_list
*parents
;
326 struct commit
*commit
= (struct commit
*)o
;
327 if (o
->flags
& THEY_HAVE
)
328 we_knew_they_have
= 1;
330 o
->flags
|= THEY_HAVE
;
331 if (!oldest_have
|| (commit
->date
< oldest_have
))
332 oldest_have
= commit
->date
;
333 for (parents
= commit
->parents
;
335 parents
= parents
->next
)
336 parents
->item
->object
.flags
|= THEY_HAVE
;
338 if (!we_knew_they_have
) {
339 add_object_array(o
, NULL
, &have_obj
);
345 static int reachable(struct commit
*want
)
347 struct commit_list
*work
= NULL
;
349 insert_by_date(want
, &work
);
351 struct commit_list
*list
= work
->next
;
352 struct commit
*commit
= work
->item
;
356 if (commit
->object
.flags
& THEY_HAVE
) {
357 want
->object
.flags
|= COMMON_KNOWN
;
360 if (!commit
->object
.parsed
)
361 parse_object(commit
->object
.sha1
);
362 if (commit
->object
.flags
& REACHABLE
)
364 commit
->object
.flags
|= REACHABLE
;
365 if (commit
->date
< oldest_have
)
367 for (list
= commit
->parents
; list
; list
= list
->next
) {
368 struct commit
*parent
= list
->item
;
369 if (!(parent
->object
.flags
& REACHABLE
))
370 insert_by_date(parent
, &work
);
373 want
->object
.flags
|= REACHABLE
;
374 clear_commit_marks(want
, REACHABLE
);
375 free_commit_list(work
);
376 return (want
->object
.flags
& COMMON_KNOWN
);
379 static int ok_to_give_up(void)
386 for (i
= 0; i
< want_obj
.nr
; i
++) {
387 struct object
*want
= want_obj
.objects
[i
].item
;
389 if (want
->flags
& COMMON_KNOWN
)
391 want
= deref_tag(want
, "a want line", 0);
392 if (!want
|| want
->type
!= OBJ_COMMIT
) {
393 /* no way to tell if this is reachable by
394 * looking at the ancestry chain alone, so
395 * leave a note to ourselves not to worry about
396 * this object anymore.
398 want_obj
.objects
[i
].item
->flags
|= COMMON_KNOWN
;
401 if (!reachable((struct commit
*)want
))
407 static int get_common_commits(void)
409 static char line
[1000];
410 unsigned char sha1
[20];
411 char hex
[41], last_hex
[41];
414 track_object_refs
= 0;
415 save_commit_buffer
= 0;
418 len
= packet_read_line(0, line
, sizeof(line
));
422 if (have_obj
.nr
== 0 || multi_ack
)
423 packet_write(1, "NAK\n");
426 len
= strip(line
, len
);
427 if (!strncmp(line
, "have ", 5)) {
428 switch (got_sha1(line
+5, sha1
)) {
429 case -1: /* they have what we do not */
430 if (multi_ack
&& ok_to_give_up())
431 packet_write(1, "ACK %s continue\n",
435 memcpy(hex
, sha1_to_hex(sha1
), 41);
437 const char *msg
= "ACK %s continue\n";
438 packet_write(1, msg
, hex
);
439 memcpy(last_hex
, hex
, 41);
441 else if (have_obj
.nr
== 1)
442 packet_write(1, "ACK %s\n", hex
);
447 if (!strcmp(line
, "done")) {
448 if (have_obj
.nr
> 0) {
450 packet_write(1, "ACK %s\n", last_hex
);
453 packet_write(1, "NAK\n");
456 die("git-upload-pack: expected SHA1 list, got '%s'", line
);
460 static void receive_needs(void)
462 static char line
[1000];
467 unsigned char sha1_buf
[20];
468 len
= packet_read_line(0, line
, sizeof(line
));
473 if (strncmp("want ", line
, 5) ||
474 get_sha1_hex(line
+5, sha1_buf
))
475 die("git-upload-pack: protocol error, "
476 "expected to get sha, not '%s'", line
);
477 if (strstr(line
+45, "multi_ack"))
479 if (strstr(line
+45, "thin-pack"))
481 if (strstr(line
+45, "ofs-delta"))
483 if (strstr(line
+45, "side-band-64k"))
484 use_sideband
= LARGE_PACKET_MAX
;
485 else if (strstr(line
+45, "side-band"))
486 use_sideband
= DEFAULT_PACKET_MAX
;
488 /* We have sent all our refs already, and the other end
489 * should have chosen out of them; otherwise they are
490 * asking for nonsense.
492 * Hmph. We may later want to allow "want" line that
493 * asks for something like "master~10" (symbolic)...
494 * would it make sense? I don't know.
496 o
= lookup_object(sha1_buf
);
497 if (!o
|| !(o
->flags
& OUR_REF
))
498 die("git-upload-pack: not our ref %s", line
+5);
499 if (!(o
->flags
& WANTED
)) {
501 add_object_array(o
, NULL
, &want_obj
);
506 static int send_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
508 static const char *capabilities
= "multi_ack thin-pack side-band side-band-64k ofs-delta";
509 struct object
*o
= parse_object(sha1
);
512 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1
));
515 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1
), refname
,
518 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), refname
);
520 if (!(o
->flags
& OUR_REF
)) {
524 if (o
->type
== OBJ_TAG
) {
525 o
= deref_tag(o
, refname
, 0);
526 packet_write(1, "%s %s^{}\n", sha1_to_hex(o
->sha1
), refname
);
531 static void upload_pack(void)
534 head_ref(send_ref
, NULL
);
535 for_each_ref(send_ref
, NULL
);
539 get_common_commits();
544 int main(int argc
, char **argv
)
550 for (i
= 1; i
< argc
; i
++) {
555 if (!strcmp(arg
, "--strict")) {
559 if (!strncmp(arg
, "--timeout=", 10)) {
560 timeout
= atoi(arg
+10);
563 if (!strcmp(arg
, "--")) {
570 usage(upload_pack_usage
);
573 if (!enter_repo(dir
, strict
))
574 die("'%s': unable to chdir or not a git archive", dir
);