11 #include "list-objects.h"
12 #include "run-command.h"
15 #include "string-list.h"
17 static const char upload_pack_usage
[] = "git upload-pack [--strict] [--timeout=<n>] <dir>";
19 /* bits #0..7 in revision.h, #8..10 in commit.c */
20 #define THEY_HAVE (1u << 11)
21 #define OUR_REF (1u << 12)
22 #define WANTED (1u << 13)
23 #define COMMON_KNOWN (1u << 14)
24 #define REACHABLE (1u << 15)
26 #define SHALLOW (1u << 16)
27 #define NOT_SHALLOW (1u << 17)
28 #define CLIENT_SHALLOW (1u << 18)
29 #define HIDDEN_REF (1u << 19)
31 static unsigned long oldest_have
;
35 static int use_thin_pack
, use_ofs_delta
, use_include_tag
;
36 static int no_progress
, daemon_mode
;
37 static int allow_tip_sha1_in_want
;
38 static int shallow_nr
;
39 static struct object_array have_obj
;
40 static struct object_array want_obj
;
41 static struct object_array extra_edge_obj
;
42 static unsigned int timeout
;
43 static int keepalive
= 5;
45 * otherwise maximum packet size (up to 65520 bytes).
47 static int use_sideband
;
48 static int advertise_refs
;
49 static int stateless_rpc
;
51 static void reset_timeout(void)
56 static ssize_t
send_client_data(int fd
, const char *data
, ssize_t sz
)
59 return send_sideband(1, fd
, data
, sz
, use_sideband
);
64 /* XXX: are we happy to lose stuff here? */
68 write_or_die(fd
, data
, sz
);
72 static FILE *pack_pipe
= NULL
;
73 static void show_commit(struct commit
*commit
, void *data
)
75 if (commit
->object
.flags
& BOUNDARY
)
76 fputc('-', pack_pipe
);
77 if (fputs(sha1_to_hex(commit
->object
.sha1
), pack_pipe
) < 0)
78 die("broken output pipe");
79 fputc('\n', pack_pipe
);
82 commit
->buffer
= NULL
;
85 static void show_object(struct object
*obj
,
86 const struct name_path
*path
, const char *component
,
89 show_object_with_name(pack_pipe
, obj
, path
, component
);
92 static void show_edge(struct commit
*commit
)
94 fprintf(pack_pipe
, "-%s\n", sha1_to_hex(commit
->object
.sha1
));
97 static int do_rev_list(int in
, int out
, void *user_data
)
100 struct rev_info revs
;
102 pack_pipe
= xfdopen(out
, "w");
103 init_revisions(&revs
, NULL
);
104 revs
.tag_objects
= 1;
105 revs
.tree_objects
= 1;
106 revs
.blob_objects
= 1;
110 for (i
= 0; i
< want_obj
.nr
; i
++) {
111 struct object
*o
= want_obj
.objects
[i
].item
;
113 o
->flags
&= ~UNINTERESTING
;
114 add_pending_object(&revs
, o
, NULL
);
116 for (i
= 0; i
< have_obj
.nr
; i
++) {
117 struct object
*o
= have_obj
.objects
[i
].item
;
118 o
->flags
|= UNINTERESTING
;
119 add_pending_object(&revs
, o
, NULL
);
121 setup_revisions(0, NULL
, &revs
, NULL
);
122 if (prepare_revision_walk(&revs
))
123 die("revision walk setup failed");
124 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
126 for (i
= 0; i
< extra_edge_obj
.nr
; i
++)
127 fprintf(pack_pipe
, "-%s\n", sha1_to_hex(
128 extra_edge_obj
.objects
[i
].item
->sha1
));
129 traverse_commit_list(&revs
, show_commit
, show_object
, NULL
);
135 static void create_pack_file(void)
137 struct async rev_list
;
138 struct child_process pack_objects
;
139 char data
[8193], progress
[128];
140 char abort_msg
[] = "aborting due to possible repository "
141 "corruption on the remote side.";
144 const char *argv
[10];
147 argv
[arg
++] = "pack-objects";
149 argv
[arg
++] = "--revs";
151 argv
[arg
++] = "--thin";
154 argv
[arg
++] = "--stdout";
156 argv
[arg
++] = "--progress";
158 argv
[arg
++] = "--delta-base-offset";
160 argv
[arg
++] = "--include-tag";
163 memset(&pack_objects
, 0, sizeof(pack_objects
));
164 pack_objects
.in
= -1;
165 pack_objects
.out
= -1;
166 pack_objects
.err
= -1;
167 pack_objects
.git_cmd
= 1;
168 pack_objects
.argv
= argv
;
170 if (start_command(&pack_objects
))
171 die("git upload-pack: unable to fork git-pack-objects");
174 memset(&rev_list
, 0, sizeof(rev_list
));
175 rev_list
.proc
= do_rev_list
;
176 rev_list
.out
= pack_objects
.in
;
177 if (start_async(&rev_list
))
178 die("git upload-pack: unable to fork git-rev-list");
181 FILE *pipe_fd
= xfdopen(pack_objects
.in
, "w");
184 for (i
= 0; i
< want_obj
.nr
; i
++)
185 fprintf(pipe_fd
, "%s\n",
186 sha1_to_hex(want_obj
.objects
[i
].item
->sha1
));
187 fprintf(pipe_fd
, "--not\n");
188 for (i
= 0; i
< have_obj
.nr
; i
++)
189 fprintf(pipe_fd
, "%s\n",
190 sha1_to_hex(have_obj
.objects
[i
].item
->sha1
));
191 fprintf(pipe_fd
, "\n");
197 /* We read from pack_objects.err to capture stderr output for
198 * progress bar, and pack_objects.out to capture the pack data.
202 struct pollfd pfd
[2];
203 int pe
, pu
, pollsize
;
211 if (0 <= pack_objects
.out
) {
212 pfd
[pollsize
].fd
= pack_objects
.out
;
213 pfd
[pollsize
].events
= POLLIN
;
217 if (0 <= pack_objects
.err
) {
218 pfd
[pollsize
].fd
= pack_objects
.err
;
219 pfd
[pollsize
].events
= POLLIN
;
227 ret
= poll(pfd
, pollsize
, 1000 * keepalive
);
229 if (errno
!= EINTR
) {
230 error("poll failed, resuming: %s",
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
= xread(pack_objects
.err
, progress
,
243 send_client_data(2, progress
, sz
);
245 close(pack_objects
.err
);
246 pack_objects
.err
= -1;
250 /* give priority to status messages */
253 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
254 /* Data ready; we keep the last byte to ourselves
255 * in case we detect broken rev-list, so that we
256 * can leave the stream corrupted. This is
257 * unfortunate -- unpack-objects would happily
258 * accept a valid packdata with trailing garbage,
259 * so appending garbage after we pass all the
260 * pack data is not good enough to signal
261 * breakage to downstream.
269 sz
= xread(pack_objects
.out
, cp
,
270 sizeof(data
) - outsz
);
274 close(pack_objects
.out
);
275 pack_objects
.out
= -1;
281 buffered
= data
[sz
-1] & 0xFF;
286 sz
= send_client_data(1, data
, sz
);
292 * We hit the keepalive timeout without saying anything; send
293 * an empty message on the data sideband just to let the other
294 * side know we're still working on it, but don't have any data
297 * If we don't have a sideband channel, there's no room in the
298 * protocol to say anything, so those clients are just out of
301 if (!ret
&& use_sideband
) {
302 static const char buf
[] = "0005\1";
303 write_or_die(1, buf
, 5);
307 if (finish_command(&pack_objects
)) {
308 error("git upload-pack: git-pack-objects died with error.");
311 if (shallow_nr
&& finish_async(&rev_list
))
312 goto fail
; /* error was already reported */
317 sz
= send_client_data(1, data
, 1);
320 fprintf(stderr
, "flushed.\n");
327 send_client_data(3, abort_msg
, sizeof(abort_msg
));
328 die("git upload-pack: %s", abort_msg
);
331 static int got_sha1(char *hex
, unsigned char *sha1
)
334 int we_knew_they_have
= 0;
336 if (get_sha1_hex(hex
, sha1
))
337 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
338 if (!has_sha1_file(sha1
))
341 o
= parse_object(sha1
);
343 die("oops (%s)", sha1_to_hex(sha1
));
344 if (o
->type
== OBJ_COMMIT
) {
345 struct commit_list
*parents
;
346 struct commit
*commit
= (struct commit
*)o
;
347 if (o
->flags
& THEY_HAVE
)
348 we_knew_they_have
= 1;
350 o
->flags
|= THEY_HAVE
;
351 if (!oldest_have
|| (commit
->date
< oldest_have
))
352 oldest_have
= commit
->date
;
353 for (parents
= commit
->parents
;
355 parents
= parents
->next
)
356 parents
->item
->object
.flags
|= THEY_HAVE
;
358 if (!we_knew_they_have
) {
359 add_object_array(o
, NULL
, &have_obj
);
365 static int reachable(struct commit
*want
)
367 struct commit_list
*work
= NULL
;
369 commit_list_insert_by_date(want
, &work
);
371 struct commit_list
*list
= work
->next
;
372 struct commit
*commit
= work
->item
;
376 if (commit
->object
.flags
& THEY_HAVE
) {
377 want
->object
.flags
|= COMMON_KNOWN
;
380 if (!commit
->object
.parsed
)
381 parse_object(commit
->object
.sha1
);
382 if (commit
->object
.flags
& REACHABLE
)
384 commit
->object
.flags
|= REACHABLE
;
385 if (commit
->date
< oldest_have
)
387 for (list
= commit
->parents
; list
; list
= list
->next
) {
388 struct commit
*parent
= list
->item
;
389 if (!(parent
->object
.flags
& REACHABLE
))
390 commit_list_insert_by_date(parent
, &work
);
393 want
->object
.flags
|= REACHABLE
;
394 clear_commit_marks(want
, REACHABLE
);
395 free_commit_list(work
);
396 return (want
->object
.flags
& COMMON_KNOWN
);
399 static int ok_to_give_up(void)
406 for (i
= 0; i
< want_obj
.nr
; i
++) {
407 struct object
*want
= want_obj
.objects
[i
].item
;
409 if (want
->flags
& COMMON_KNOWN
)
411 want
= deref_tag(want
, "a want line", 0);
412 if (!want
|| want
->type
!= OBJ_COMMIT
) {
413 /* no way to tell if this is reachable by
414 * looking at the ancestry chain alone, so
415 * leave a note to ourselves not to worry about
416 * this object anymore.
418 want_obj
.objects
[i
].item
->flags
|= COMMON_KNOWN
;
421 if (!reachable((struct commit
*)want
))
427 static int get_common_commits(void)
429 unsigned char sha1
[20];
435 save_commit_buffer
= 0;
438 char *line
= packet_read_line(0, NULL
);
442 if (multi_ack
== 2 && got_common
443 && !got_other
&& ok_to_give_up()) {
445 packet_write(1, "ACK %s ready\n", last_hex
);
447 if (have_obj
.nr
== 0 || multi_ack
)
448 packet_write(1, "NAK\n");
450 if (no_done
&& sent_ready
) {
451 packet_write(1, "ACK %s\n", last_hex
);
460 if (!prefixcmp(line
, "have ")) {
461 switch (got_sha1(line
+5, sha1
)) {
462 case -1: /* they have what we do not */
464 if (multi_ack
&& ok_to_give_up()) {
465 const char *hex
= sha1_to_hex(sha1
);
466 if (multi_ack
== 2) {
468 packet_write(1, "ACK %s ready\n", hex
);
470 packet_write(1, "ACK %s continue\n", hex
);
475 memcpy(last_hex
, sha1_to_hex(sha1
), 41);
477 packet_write(1, "ACK %s common\n", last_hex
);
479 packet_write(1, "ACK %s continue\n", last_hex
);
480 else if (have_obj
.nr
== 1)
481 packet_write(1, "ACK %s\n", last_hex
);
486 if (!strcmp(line
, "done")) {
487 if (have_obj
.nr
> 0) {
489 packet_write(1, "ACK %s\n", last_hex
);
492 packet_write(1, "NAK\n");
495 die("git upload-pack: expected SHA1 list, got '%s'", line
);
499 static int is_our_ref(struct object
*o
)
502 ((allow_tip_sha1_in_want
? HIDDEN_REF
: 0) | OUR_REF
);
505 static void check_non_tip(void)
507 static const char *argv
[] = {
508 "rev-list", "--stdin", NULL
,
510 static struct child_process cmd
;
512 char namebuf
[42]; /* ^ + SHA-1 + LF */
515 /* In the normal in-process case non-tip request can never happen */
525 if (start_command(&cmd
))
529 * If rev-list --stdin encounters an unknown commit, it
530 * terminates, which will cause SIGPIPE in the write loop
533 sigchain_push(SIGPIPE
, SIG_IGN
);
537 for (i
= get_max_object_index(); 0 < i
; ) {
538 o
= get_indexed_object(--i
);
543 memcpy(namebuf
+ 1, sha1_to_hex(o
->sha1
), 40);
544 if (write_in_full(cmd
.in
, namebuf
, 42) < 0)
548 for (i
= 0; i
< want_obj
.nr
; i
++) {
549 o
= want_obj
.objects
[i
].item
;
552 memcpy(namebuf
, sha1_to_hex(o
->sha1
), 40);
553 if (write_in_full(cmd
.in
, namebuf
, 41) < 0)
558 sigchain_pop(SIGPIPE
);
561 * The commits out of the rev-list are not ancestors of
564 i
= read_in_full(cmd
.out
, namebuf
, 1);
570 * rev-list may have died by encountering a bad commit
571 * in the history, in which case we do want to bail out
572 * even when it showed no commit.
574 if (finish_command(&cmd
))
577 /* All the non-tip ones are ancestors of what we advertised */
581 /* Pick one of them (we know there at least is one) */
582 for (i
= 0; i
< want_obj
.nr
; i
++) {
583 o
= want_obj
.objects
[i
].item
;
585 die("git upload-pack: not our ref %s",
586 sha1_to_hex(o
->sha1
));
590 static void receive_needs(void)
592 struct object_array shallows
= OBJECT_ARRAY_INIT
;
599 const char *features
;
600 unsigned char sha1_buf
[20];
601 char *line
= packet_read_line(0, NULL
);
606 if (!prefixcmp(line
, "shallow ")) {
607 unsigned char sha1
[20];
608 struct object
*object
;
609 if (get_sha1_hex(line
+ 8, sha1
))
610 die("invalid shallow line: %s", line
);
611 object
= parse_object(sha1
);
614 if (object
->type
!= OBJ_COMMIT
)
615 die("invalid shallow object %s", sha1_to_hex(sha1
));
616 if (!(object
->flags
& CLIENT_SHALLOW
)) {
617 object
->flags
|= CLIENT_SHALLOW
;
618 add_object_array(object
, NULL
, &shallows
);
622 if (!prefixcmp(line
, "deepen ")) {
624 depth
= strtol(line
+ 7, &end
, 0);
625 if (end
== line
+ 7 || depth
<= 0)
626 die("Invalid deepen: %s", line
);
629 if (prefixcmp(line
, "want ") ||
630 get_sha1_hex(line
+5, sha1_buf
))
631 die("git upload-pack: protocol error, "
632 "expected to get sha, not '%s'", line
);
634 features
= line
+ 45;
636 if (parse_feature_request(features
, "multi_ack_detailed"))
638 else if (parse_feature_request(features
, "multi_ack"))
640 if (parse_feature_request(features
, "no-done"))
642 if (parse_feature_request(features
, "thin-pack"))
644 if (parse_feature_request(features
, "ofs-delta"))
646 if (parse_feature_request(features
, "side-band-64k"))
647 use_sideband
= LARGE_PACKET_MAX
;
648 else if (parse_feature_request(features
, "side-band"))
649 use_sideband
= DEFAULT_PACKET_MAX
;
650 if (parse_feature_request(features
, "no-progress"))
652 if (parse_feature_request(features
, "include-tag"))
655 o
= parse_object(sha1_buf
);
657 die("git upload-pack: not our ref %s",
658 sha1_to_hex(sha1_buf
));
659 if (!(o
->flags
& WANTED
)) {
663 add_object_array(o
, NULL
, &want_obj
);
668 * We have sent all our refs already, and the other end
669 * should have chosen out of them. When we are operating
670 * in the stateless RPC mode, however, their choice may
671 * have been based on the set of older refs advertised
672 * by another process that handled the initial request.
677 if (!use_sideband
&& daemon_mode
)
680 if (depth
== 0 && shallows
.nr
== 0)
683 struct commit_list
*result
= NULL
, *backup
= NULL
;
685 if (depth
== INFINITE_DEPTH
)
686 for (i
= 0; i
< shallows
.nr
; i
++) {
687 struct object
*object
= shallows
.objects
[i
].item
;
688 object
->flags
|= NOT_SHALLOW
;
692 get_shallow_commits(&want_obj
, depth
,
693 SHALLOW
, NOT_SHALLOW
);
695 struct object
*object
= &result
->item
->object
;
696 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
697 packet_write(1, "shallow %s",
698 sha1_to_hex(object
->sha1
));
699 register_shallow(object
->sha1
);
702 result
= result
->next
;
704 free_commit_list(backup
);
705 for (i
= 0; i
< shallows
.nr
; i
++) {
706 struct object
*object
= shallows
.objects
[i
].item
;
707 if (object
->flags
& NOT_SHALLOW
) {
708 struct commit_list
*parents
;
709 packet_write(1, "unshallow %s",
710 sha1_to_hex(object
->sha1
));
711 object
->flags
&= ~CLIENT_SHALLOW
;
712 /* make sure the real parents are parsed */
713 unregister_shallow(object
->sha1
);
715 if (parse_commit((struct commit
*)object
))
716 die("invalid commit");
717 parents
= ((struct commit
*)object
)->parents
;
719 add_object_array(&parents
->item
->object
,
721 parents
= parents
->next
;
723 add_object_array(object
, NULL
, &extra_edge_obj
);
725 /* make sure commit traversal conforms to client */
726 register_shallow(object
->sha1
);
730 if (shallows
.nr
> 0) {
732 for (i
= 0; i
< shallows
.nr
; i
++)
733 register_shallow(shallows
.objects
[i
].item
->sha1
);
736 shallow_nr
+= shallows
.nr
;
737 free(shallows
.objects
);
740 /* return non-zero if the ref is hidden, otherwise 0 */
741 static int mark_our_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
743 struct object
*o
= lookup_unknown_object(sha1
);
745 if (ref_is_hidden(refname
)) {
746 o
->flags
|= HIDDEN_REF
;
750 die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1
));
755 static int send_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
757 static const char *capabilities
= "multi_ack thin-pack side-band"
758 " side-band-64k ofs-delta shallow no-progress"
759 " include-tag multi_ack_detailed";
760 const char *refname_nons
= strip_namespace(refname
);
761 unsigned char peeled
[20];
763 if (mark_our_ref(refname
, sha1
, flag
, cb_data
))
767 packet_write(1, "%s %s%c%s%s%s agent=%s\n",
768 sha1_to_hex(sha1
), refname_nons
,
770 allow_tip_sha1_in_want
? " allow-tip-sha1-in-want" : "",
771 stateless_rpc
? " no-done" : "",
772 git_user_agent_sanitized());
774 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), refname_nons
);
776 if (!peel_ref(refname
, peeled
))
777 packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled
), refname_nons
);
781 static void upload_pack(void)
783 if (advertise_refs
|| !stateless_rpc
) {
785 head_ref_namespaced(send_ref
, NULL
);
786 for_each_namespaced_ref(send_ref
, NULL
);
789 head_ref_namespaced(mark_our_ref
, NULL
);
790 for_each_namespaced_ref(mark_our_ref
, NULL
);
797 get_common_commits();
802 static int upload_pack_config(const char *var
, const char *value
, void *unused
)
804 if (!strcmp("uploadpack.allowtipsha1inwant", var
))
805 allow_tip_sha1_in_want
= git_config_bool(var
, value
);
806 else if (!strcmp("uploadpack.keepalive", var
)) {
807 keepalive
= git_config_int(var
, value
);
811 return parse_hide_refs_config(var
, value
, "uploadpack");
814 int main(int argc
, char **argv
)
822 packet_trace_identity("upload-pack");
823 git_extract_argv0_path(argv
[0]);
824 read_replace_refs
= 0;
826 for (i
= 1; i
< argc
; i
++) {
831 if (!strcmp(arg
, "--advertise-refs")) {
835 if (!strcmp(arg
, "--stateless-rpc")) {
839 if (!strcmp(arg
, "--strict")) {
843 if (!prefixcmp(arg
, "--timeout=")) {
844 timeout
= atoi(arg
+10);
848 if (!strcmp(arg
, "--")) {
855 usage(upload_pack_usage
);
861 if (!enter_repo(dir
, strict
))
862 die("'%s' does not appear to be a git repository", dir
);
863 if (is_repository_shallow())
864 die("attempt to fetch/clone from a shallow repository");
865 git_config(upload_pack_config
, NULL
);