11 #include "list-objects.h"
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 #define SHALLOW (1u << 16)
23 #define NOT_SHALLOW (1u << 17)
24 #define CLIENT_SHALLOW (1u << 18)
26 static unsigned long oldest_have
;
28 static int multi_ack
, nr_our_refs
;
29 static int use_thin_pack
, use_ofs_delta
;
30 static struct object_array have_obj
;
31 static struct object_array want_obj
;
32 static unsigned int timeout
;
34 * otherwise maximum packet size (up to 65520 bytes).
36 static int use_sideband
;
38 static void reset_timeout(void)
43 static int strip(char *line
, int len
)
45 if (len
&& line
[len
-1] == '\n')
50 static ssize_t
send_client_data(int fd
, const char *data
, ssize_t sz
)
53 return send_sideband(1, fd
, data
, sz
, use_sideband
);
58 /* XXX: are we happy to lose stuff here? */
62 return safe_write(fd
, data
, sz
);
65 FILE *pack_pipe
= NULL
;
66 static void show_commit(struct commit
*commit
)
68 if (commit
->object
.flags
& BOUNDARY
)
69 fputc('-', pack_pipe
);
70 if (fputs(sha1_to_hex(commit
->object
.sha1
), pack_pipe
) < 0)
71 die("broken output pipe");
72 fputc('\n', pack_pipe
);
75 commit
->buffer
= NULL
;
78 static void show_object(struct object_array_entry
*p
)
80 /* An object with name "foo\n0000000..." can be used to
81 * confuse downstream git-pack-objects very badly.
83 const char *ep
= strchr(p
->name
, '\n');
85 fprintf(pack_pipe
, "%s %.*s\n", sha1_to_hex(p
->item
->sha1
),
90 fprintf(pack_pipe
, "%s %s\n",
91 sha1_to_hex(p
->item
->sha1
), p
->name
);
94 static void show_edge(struct commit
*commit
)
96 fprintf(pack_pipe
, "-%s\n", sha1_to_hex(commit
->object
.sha1
));
99 static void do_rev_list(int create_full_pack
)
102 struct rev_info revs
;
104 if (create_full_pack
)
105 use_thin_pack
= 0; /* no point doing it */
106 init_revisions(&revs
, NULL
);
107 revs
.tag_objects
= 1;
108 revs
.tree_objects
= 1;
109 revs
.blob_objects
= 1;
113 if (create_full_pack
) {
114 const char *args
[] = {"rev-list", "--all", NULL
};
115 setup_revisions(2, args
, &revs
, NULL
);
117 for (i
= 0; i
< want_obj
.nr
; i
++) {
118 struct object
*o
= want_obj
.objects
[i
].item
;
120 o
->flags
&= ~UNINTERESTING
;
121 add_pending_object(&revs
, o
, NULL
);
123 for (i
= 0; i
< have_obj
.nr
; i
++) {
124 struct object
*o
= have_obj
.objects
[i
].item
;
125 o
->flags
|= UNINTERESTING
;
126 add_pending_object(&revs
, o
, NULL
);
128 setup_revisions(0, NULL
, &revs
, NULL
);
130 prepare_revision_walk(&revs
);
131 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
132 traverse_commit_list(&revs
, show_commit
, show_object
);
135 static void create_pack_file(void)
137 /* Pipes between rev-list to pack-objects, pack-objects to us
138 * and pack-objects error stream for progress bar.
140 int lp_pipe
[2], pu_pipe
[2], pe_pipe
[2];
141 pid_t pid_rev_list
, pid_pack_objects
;
142 int create_full_pack
= (nr_our_refs
== want_obj
.nr
&& !have_obj
.nr
);
143 char data
[8193], progress
[128];
144 char abort_msg
[] = "aborting due to possible repository "
145 "corruption on the remote side.";
148 if (pipe(lp_pipe
) < 0)
149 die("git-upload-pack: unable to create pipe");
150 pid_rev_list
= fork();
151 if (pid_rev_list
< 0)
152 die("git-upload-pack: unable to fork git-rev-list");
155 pack_pipe
= fdopen(lp_pipe
[1], "w");
156 do_rev_list(create_full_pack
);
160 if (pipe(pu_pipe
) < 0)
161 die("git-upload-pack: unable to create pipe");
162 if (pipe(pe_pipe
) < 0)
163 die("git-upload-pack: unable to create pipe");
164 pid_pack_objects
= fork();
165 if (pid_pack_objects
< 0) {
166 /* daemon sets things up to ignore TERM */
167 kill(pid_rev_list
, SIGKILL
);
168 die("git-upload-pack: unable to fork git-pack-objects");
170 if (!pid_pack_objects
) {
181 execl_git_cmd("pack-objects", "--stdout", "--progress",
182 use_ofs_delta
? "--delta-base-offset" : NULL
,
184 kill(pid_rev_list
, SIGKILL
);
185 die("git-upload-pack: unable to exec git-pack-objects");
191 /* We read from pe_pipe[0] to capture stderr output for
192 * progress bar, and pu_pipe[0] to capture the pack data.
199 struct pollfd pfd
[2];
203 int pe
, pu
, pollsize
;
210 if (0 <= pu_pipe
[0]) {
211 pfd
[pollsize
].fd
= pu_pipe
[0];
212 pfd
[pollsize
].events
= POLLIN
;
216 if (0 <= pe_pipe
[0]) {
217 pfd
[pollsize
].fd
= pe_pipe
[0];
218 pfd
[pollsize
].events
= POLLIN
;
224 if (poll(pfd
, pollsize
, -1) < 0) {
225 if (errno
!= EINTR
) {
226 error("poll failed, resuming: %s",
232 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
233 /* Data ready; we keep the last byte
234 * to ourselves in case we detect
235 * broken rev-list, so that we can
236 * leave the stream corrupted. This
237 * is unfortunate -- unpack-objects
238 * would happily accept a valid pack
239 * data with trailing garbage, so
240 * appending garbage after we pass all
241 * the pack data is not good enough to
242 * signal breakage to downstream.
250 sz
= xread(pu_pipe
[0], cp
,
251 sizeof(data
) - outsz
);
262 buffered
= data
[sz
-1] & 0xFF;
267 sz
= send_client_data(1, data
, sz
);
271 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
272 /* Status ready; we ship that in the side-band
273 * or dump to the standard error.
275 sz
= xread(pe_pipe
[0], progress
,
278 send_client_data(2, progress
, sz
);
288 /* See if the children are still there */
289 if (pid_rev_list
|| pid_pack_objects
) {
290 pid
= waitpid(-1, &status
, WNOHANG
);
293 who
= ((pid
== pid_rev_list
) ? "git-rev-list" :
294 (pid
== pid_pack_objects
) ? "git-pack-objects" :
298 error("git-upload-pack: %s",
302 error("git-upload-pack: we weren't "
303 "waiting for %d", pid
);
306 if (!WIFEXITED(status
) || WEXITSTATUS(status
) > 0) {
307 error("git-upload-pack: %s died with error.",
311 if (pid
== pid_rev_list
)
313 if (pid
== pid_pack_objects
)
314 pid_pack_objects
= 0;
315 if (pid_rev_list
|| pid_pack_objects
)
319 /* both died happily */
326 sz
= send_client_data(1, data
, 1);
329 fprintf(stderr
, "flushed.\n");
336 if (pid_pack_objects
)
337 kill(pid_pack_objects
, SIGKILL
);
339 kill(pid_rev_list
, SIGKILL
);
340 send_client_data(3, abort_msg
, sizeof(abort_msg
));
341 die("git-upload-pack: %s", abort_msg
);
344 static int got_sha1(char *hex
, unsigned char *sha1
)
347 int we_knew_they_have
= 0;
349 if (get_sha1_hex(hex
, sha1
))
350 die("git-upload-pack: expected SHA1 object, got '%s'", hex
);
351 if (!has_sha1_file(sha1
))
354 o
= lookup_object(sha1
);
355 if (!(o
&& o
->parsed
))
356 o
= parse_object(sha1
);
358 die("oops (%s)", sha1_to_hex(sha1
));
359 if (o
->type
== OBJ_COMMIT
) {
360 struct commit_list
*parents
;
361 struct commit
*commit
= (struct commit
*)o
;
362 if (o
->flags
& THEY_HAVE
)
363 we_knew_they_have
= 1;
365 o
->flags
|= THEY_HAVE
;
366 if (!oldest_have
|| (commit
->date
< oldest_have
))
367 oldest_have
= commit
->date
;
368 for (parents
= commit
->parents
;
370 parents
= parents
->next
)
371 parents
->item
->object
.flags
|= THEY_HAVE
;
373 if (!we_knew_they_have
) {
374 add_object_array(o
, NULL
, &have_obj
);
380 static int reachable(struct commit
*want
)
382 struct commit_list
*work
= NULL
;
384 insert_by_date(want
, &work
);
386 struct commit_list
*list
= work
->next
;
387 struct commit
*commit
= work
->item
;
391 if (commit
->object
.flags
& THEY_HAVE
) {
392 want
->object
.flags
|= COMMON_KNOWN
;
395 if (!commit
->object
.parsed
)
396 parse_object(commit
->object
.sha1
);
397 if (commit
->object
.flags
& REACHABLE
)
399 commit
->object
.flags
|= REACHABLE
;
400 if (commit
->date
< oldest_have
)
402 for (list
= commit
->parents
; list
; list
= list
->next
) {
403 struct commit
*parent
= list
->item
;
404 if (!(parent
->object
.flags
& REACHABLE
))
405 insert_by_date(parent
, &work
);
408 want
->object
.flags
|= REACHABLE
;
409 clear_commit_marks(want
, REACHABLE
);
410 free_commit_list(work
);
411 return (want
->object
.flags
& COMMON_KNOWN
);
414 static int ok_to_give_up(void)
421 for (i
= 0; i
< want_obj
.nr
; i
++) {
422 struct object
*want
= want_obj
.objects
[i
].item
;
424 if (want
->flags
& COMMON_KNOWN
)
426 want
= deref_tag(want
, "a want line", 0);
427 if (!want
|| want
->type
!= OBJ_COMMIT
) {
428 /* no way to tell if this is reachable by
429 * looking at the ancestry chain alone, so
430 * leave a note to ourselves not to worry about
431 * this object anymore.
433 want_obj
.objects
[i
].item
->flags
|= COMMON_KNOWN
;
436 if (!reachable((struct commit
*)want
))
442 static int get_common_commits(void)
444 static char line
[1000];
445 unsigned char sha1
[20];
446 char hex
[41], last_hex
[41];
449 track_object_refs
= 0;
450 save_commit_buffer
= 0;
453 len
= packet_read_line(0, line
, sizeof(line
));
457 if (have_obj
.nr
== 0 || multi_ack
)
458 packet_write(1, "NAK\n");
461 len
= strip(line
, len
);
462 if (!strncmp(line
, "have ", 5)) {
463 switch (got_sha1(line
+5, sha1
)) {
464 case -1: /* they have what we do not */
465 if (multi_ack
&& ok_to_give_up())
466 packet_write(1, "ACK %s continue\n",
470 memcpy(hex
, sha1_to_hex(sha1
), 41);
472 const char *msg
= "ACK %s continue\n";
473 packet_write(1, msg
, hex
);
474 memcpy(last_hex
, hex
, 41);
476 else if (have_obj
.nr
== 1)
477 packet_write(1, "ACK %s\n", hex
);
482 if (!strcmp(line
, "done")) {
483 if (have_obj
.nr
> 0) {
485 packet_write(1, "ACK %s\n", last_hex
);
488 packet_write(1, "NAK\n");
491 die("git-upload-pack: expected SHA1 list, got '%s'", line
);
495 static void receive_needs(void)
497 struct object_array shallows
= {0, 0, NULL
};
498 static char line
[1000];
503 unsigned char sha1_buf
[20];
504 len
= packet_read_line(0, line
, sizeof(line
));
509 if (!strncmp("shallow ", line
, 8)) {
510 unsigned char sha1
[20];
511 struct object
*object
;
513 if (get_sha1(line
+ 8, sha1
))
514 die("invalid shallow line: %s", line
);
515 object
= parse_object(sha1
);
517 die("did not find object for %s", line
);
518 object
->flags
|= CLIENT_SHALLOW
;
519 add_object_array(object
, NULL
, &shallows
);
522 if (!strncmp("deepen ", line
, 7)) {
525 depth
= strtol(line
+ 7, &end
, 0);
526 if (end
== line
+ 7 || depth
<= 0)
527 die("Invalid deepen: %s", line
);
530 if (strncmp("want ", line
, 5) ||
531 get_sha1_hex(line
+5, sha1_buf
))
532 die("git-upload-pack: protocol error, "
533 "expected to get sha, not '%s'", line
);
534 if (strstr(line
+45, "multi_ack"))
536 if (strstr(line
+45, "thin-pack"))
538 if (strstr(line
+45, "ofs-delta"))
540 if (strstr(line
+45, "side-band-64k"))
541 use_sideband
= LARGE_PACKET_MAX
;
542 else if (strstr(line
+45, "side-band"))
543 use_sideband
= DEFAULT_PACKET_MAX
;
545 /* We have sent all our refs already, and the other end
546 * should have chosen out of them; otherwise they are
547 * asking for nonsense.
549 * Hmph. We may later want to allow "want" line that
550 * asks for something like "master~10" (symbolic)...
551 * would it make sense? I don't know.
553 o
= lookup_object(sha1_buf
);
554 if (!o
|| !(o
->flags
& OUR_REF
))
555 die("git-upload-pack: not our ref %s", line
+5);
556 if (!(o
->flags
& WANTED
)) {
558 add_object_array(o
, NULL
, &want_obj
);
561 if (depth
== 0 && shallows
.nr
== 0)
564 struct commit_list
*result
, *backup
;
566 backup
= result
= get_shallow_commits(&want_obj
, depth
,
567 SHALLOW
, NOT_SHALLOW
);
569 struct object
*object
= &result
->item
->object
;
570 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
571 packet_write(1, "shallow %s",
572 sha1_to_hex(object
->sha1
));
573 register_shallow(object
->sha1
);
575 result
= result
->next
;
577 free_commit_list(backup
);
578 for (i
= 0; i
< shallows
.nr
; i
++) {
579 struct object
*object
= shallows
.objects
[i
].item
;
580 if (object
->flags
& NOT_SHALLOW
) {
581 struct commit_list
*parents
;
582 packet_write(1, "unshallow %s",
583 sha1_to_hex(object
->sha1
));
584 object
->flags
&= ~CLIENT_SHALLOW
;
585 /* make sure the real parents are parsed */
586 unregister_shallow(object
->sha1
);
588 parse_commit((struct commit
*)object
);
589 parents
= ((struct commit
*)object
)->parents
;
591 add_object_array(&parents
->item
->object
,
593 parents
= parents
->next
;
596 /* make sure commit traversal conforms to client */
597 register_shallow(object
->sha1
);
601 if (shallows
.nr
> 0) {
603 for (i
= 0; i
< shallows
.nr
; i
++)
604 register_shallow(shallows
.objects
[i
].item
->sha1
);
606 free(shallows
.objects
);
609 static int send_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
611 static const char *capabilities
= "multi_ack thin-pack side-band"
612 " side-band-64k ofs-delta shallow";
613 struct object
*o
= parse_object(sha1
);
616 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1
));
619 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1
), refname
,
622 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), refname
);
624 if (!(o
->flags
& OUR_REF
)) {
628 if (o
->type
== OBJ_TAG
) {
629 o
= deref_tag(o
, refname
, 0);
630 packet_write(1, "%s %s^{}\n", sha1_to_hex(o
->sha1
), refname
);
635 static void upload_pack(void)
638 head_ref(send_ref
, NULL
);
639 for_each_ref(send_ref
, NULL
);
643 get_common_commits();
648 int main(int argc
, char **argv
)
654 for (i
= 1; i
< argc
; i
++) {
659 if (!strcmp(arg
, "--strict")) {
663 if (!strncmp(arg
, "--timeout=", 10)) {
664 timeout
= atoi(arg
+10);
667 if (!strcmp(arg
, "--")) {
674 usage(upload_pack_usage
);
677 if (!enter_repo(dir
, strict
))
678 die("'%s': unable to chdir or not a git archive", dir
);