11 #include "list-objects.h"
12 #include "run-command.h"
16 #include "string-list.h"
18 static const char upload_pack_usage
[] = "git upload-pack [--strict] [--timeout=<n>] <dir>";
20 /* Remember to update object flag allocation in object.h */
21 #define THEY_HAVE (1u << 11)
22 #define OUR_REF (1u << 12)
23 #define WANTED (1u << 13)
24 #define COMMON_KNOWN (1u << 14)
25 #define REACHABLE (1u << 15)
27 #define SHALLOW (1u << 16)
28 #define NOT_SHALLOW (1u << 17)
29 #define CLIENT_SHALLOW (1u << 18)
30 #define HIDDEN_REF (1u << 19)
32 static unsigned long oldest_have
;
36 static int use_thin_pack
, use_ofs_delta
, use_include_tag
;
37 static int no_progress
, daemon_mode
;
38 /* Allow specifying sha1 if it is a ref tip. */
39 #define ALLOW_TIP_SHA1 01
40 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
41 #define ALLOW_REACHABLE_SHA1 02
42 static unsigned int allow_unadvertised_object_request
;
43 static int shallow_nr
;
44 static struct object_array have_obj
;
45 static struct object_array want_obj
;
46 static struct object_array extra_edge_obj
;
47 static unsigned int timeout
;
48 static int keepalive
= 5;
50 * otherwise maximum packet size (up to 65520 bytes).
52 static int use_sideband
;
53 static int advertise_refs
;
54 static int stateless_rpc
;
56 static void reset_timeout(void)
61 static ssize_t
send_client_data(int fd
, const char *data
, ssize_t sz
)
64 return send_sideband(1, fd
, data
, sz
, use_sideband
);
69 /* XXX: are we happy to lose stuff here? */
73 write_or_die(fd
, data
, sz
);
77 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
80 if (graft
->nr_parent
== -1)
81 fprintf(fp
, "--shallow %s\n", oid_to_hex(&graft
->oid
));
85 static void create_pack_file(void)
87 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
88 char data
[8193], progress
[128];
89 char abort_msg
[] = "aborting due to possible repository "
90 "corruption on the remote side.";
98 argv
[arg
++] = "--shallow-file";
101 argv
[arg
++] = "pack-objects";
102 argv
[arg
++] = "--revs";
104 argv
[arg
++] = "--thin";
106 argv
[arg
++] = "--stdout";
108 argv
[arg
++] = "--shallow";
110 argv
[arg
++] = "--progress";
112 argv
[arg
++] = "--delta-base-offset";
114 argv
[arg
++] = "--include-tag";
117 pack_objects
.in
= -1;
118 pack_objects
.out
= -1;
119 pack_objects
.err
= -1;
120 pack_objects
.git_cmd
= 1;
121 pack_objects
.argv
= argv
;
123 if (start_command(&pack_objects
))
124 die("git upload-pack: unable to fork git-pack-objects");
126 pipe_fd
= xfdopen(pack_objects
.in
, "w");
129 for_each_commit_graft(write_one_shallow
, pipe_fd
);
131 for (i
= 0; i
< want_obj
.nr
; i
++)
132 fprintf(pipe_fd
, "%s\n",
133 sha1_to_hex(want_obj
.objects
[i
].item
->sha1
));
134 fprintf(pipe_fd
, "--not\n");
135 for (i
= 0; i
< have_obj
.nr
; i
++)
136 fprintf(pipe_fd
, "%s\n",
137 sha1_to_hex(have_obj
.objects
[i
].item
->sha1
));
138 for (i
= 0; i
< extra_edge_obj
.nr
; i
++)
139 fprintf(pipe_fd
, "%s\n",
140 sha1_to_hex(extra_edge_obj
.objects
[i
].item
->sha1
));
141 fprintf(pipe_fd
, "\n");
145 /* We read from pack_objects.err to capture stderr output for
146 * progress bar, and pack_objects.out to capture the pack data.
150 struct pollfd pfd
[2];
151 int pe
, pu
, pollsize
;
159 if (0 <= pack_objects
.out
) {
160 pfd
[pollsize
].fd
= pack_objects
.out
;
161 pfd
[pollsize
].events
= POLLIN
;
165 if (0 <= pack_objects
.err
) {
166 pfd
[pollsize
].fd
= pack_objects
.err
;
167 pfd
[pollsize
].events
= POLLIN
;
175 ret
= poll(pfd
, pollsize
,
176 keepalive
< 0 ? -1 : 1000 * keepalive
);
179 if (errno
!= EINTR
) {
180 error("poll failed, resuming: %s",
186 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
187 /* Status ready; we ship that in the side-band
188 * or dump to the standard error.
190 sz
= xread(pack_objects
.err
, progress
,
193 send_client_data(2, progress
, sz
);
195 close(pack_objects
.err
);
196 pack_objects
.err
= -1;
200 /* give priority to status messages */
203 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
204 /* Data ready; we keep the last byte to ourselves
205 * in case we detect broken rev-list, so that we
206 * can leave the stream corrupted. This is
207 * unfortunate -- unpack-objects would happily
208 * accept a valid packdata with trailing garbage,
209 * so appending garbage after we pass all the
210 * pack data is not good enough to signal
211 * breakage to downstream.
219 sz
= xread(pack_objects
.out
, cp
,
220 sizeof(data
) - outsz
);
224 close(pack_objects
.out
);
225 pack_objects
.out
= -1;
231 buffered
= data
[sz
-1] & 0xFF;
236 sz
= send_client_data(1, data
, sz
);
242 * We hit the keepalive timeout without saying anything; send
243 * an empty message on the data sideband just to let the other
244 * side know we're still working on it, but don't have any data
247 * If we don't have a sideband channel, there's no room in the
248 * protocol to say anything, so those clients are just out of
251 if (!ret
&& use_sideband
) {
252 static const char buf
[] = "0005\1";
253 write_or_die(1, buf
, 5);
257 if (finish_command(&pack_objects
)) {
258 error("git upload-pack: git-pack-objects died with error.");
265 sz
= send_client_data(1, data
, 1);
268 fprintf(stderr
, "flushed.\n");
275 send_client_data(3, abort_msg
, sizeof(abort_msg
));
276 die("git upload-pack: %s", abort_msg
);
279 static int got_sha1(char *hex
, unsigned char *sha1
)
282 int we_knew_they_have
= 0;
284 if (get_sha1_hex(hex
, sha1
))
285 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
286 if (!has_sha1_file(sha1
))
289 o
= parse_object(sha1
);
291 die("oops (%s)", sha1_to_hex(sha1
));
292 if (o
->type
== OBJ_COMMIT
) {
293 struct commit_list
*parents
;
294 struct commit
*commit
= (struct commit
*)o
;
295 if (o
->flags
& THEY_HAVE
)
296 we_knew_they_have
= 1;
298 o
->flags
|= THEY_HAVE
;
299 if (!oldest_have
|| (commit
->date
< oldest_have
))
300 oldest_have
= commit
->date
;
301 for (parents
= commit
->parents
;
303 parents
= parents
->next
)
304 parents
->item
->object
.flags
|= THEY_HAVE
;
306 if (!we_knew_they_have
) {
307 add_object_array(o
, NULL
, &have_obj
);
313 static int reachable(struct commit
*want
)
315 struct commit_list
*work
= NULL
;
317 commit_list_insert_by_date(want
, &work
);
319 struct commit_list
*list
= work
->next
;
320 struct commit
*commit
= work
->item
;
324 if (commit
->object
.flags
& THEY_HAVE
) {
325 want
->object
.flags
|= COMMON_KNOWN
;
328 if (!commit
->object
.parsed
)
329 parse_object(commit
->object
.sha1
);
330 if (commit
->object
.flags
& REACHABLE
)
332 commit
->object
.flags
|= REACHABLE
;
333 if (commit
->date
< oldest_have
)
335 for (list
= commit
->parents
; list
; list
= list
->next
) {
336 struct commit
*parent
= list
->item
;
337 if (!(parent
->object
.flags
& REACHABLE
))
338 commit_list_insert_by_date(parent
, &work
);
341 want
->object
.flags
|= REACHABLE
;
342 clear_commit_marks(want
, REACHABLE
);
343 free_commit_list(work
);
344 return (want
->object
.flags
& COMMON_KNOWN
);
347 static int ok_to_give_up(void)
354 for (i
= 0; i
< want_obj
.nr
; i
++) {
355 struct object
*want
= want_obj
.objects
[i
].item
;
357 if (want
->flags
& COMMON_KNOWN
)
359 want
= deref_tag(want
, "a want line", 0);
360 if (!want
|| want
->type
!= OBJ_COMMIT
) {
361 /* no way to tell if this is reachable by
362 * looking at the ancestry chain alone, so
363 * leave a note to ourselves not to worry about
364 * this object anymore.
366 want_obj
.objects
[i
].item
->flags
|= COMMON_KNOWN
;
369 if (!reachable((struct commit
*)want
))
375 static int get_common_commits(void)
377 unsigned char sha1
[20];
383 save_commit_buffer
= 0;
386 char *line
= packet_read_line(0, NULL
);
390 if (multi_ack
== 2 && got_common
391 && !got_other
&& ok_to_give_up()) {
393 packet_write(1, "ACK %s ready\n", last_hex
);
395 if (have_obj
.nr
== 0 || multi_ack
)
396 packet_write(1, "NAK\n");
398 if (no_done
&& sent_ready
) {
399 packet_write(1, "ACK %s\n", last_hex
);
408 if (starts_with(line
, "have ")) {
409 switch (got_sha1(line
+5, sha1
)) {
410 case -1: /* they have what we do not */
412 if (multi_ack
&& ok_to_give_up()) {
413 const char *hex
= sha1_to_hex(sha1
);
414 if (multi_ack
== 2) {
416 packet_write(1, "ACK %s ready\n", hex
);
418 packet_write(1, "ACK %s continue\n", hex
);
423 memcpy(last_hex
, sha1_to_hex(sha1
), 41);
425 packet_write(1, "ACK %s common\n", last_hex
);
427 packet_write(1, "ACK %s continue\n", last_hex
);
428 else if (have_obj
.nr
== 1)
429 packet_write(1, "ACK %s\n", last_hex
);
434 if (!strcmp(line
, "done")) {
435 if (have_obj
.nr
> 0) {
437 packet_write(1, "ACK %s\n", last_hex
);
440 packet_write(1, "NAK\n");
443 die("git upload-pack: expected SHA1 list, got '%s'", line
);
447 static int is_our_ref(struct object
*o
)
449 int allow_hidden_ref
= (allow_unadvertised_object_request
&
450 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
451 return o
->flags
& ((allow_hidden_ref
? HIDDEN_REF
: 0) | OUR_REF
);
454 static void check_non_tip(void)
456 static const char *argv
[] = {
457 "rev-list", "--stdin", NULL
,
459 static struct child_process cmd
= CHILD_PROCESS_INIT
;
461 char namebuf
[42]; /* ^ + SHA-1 + LF */
465 * In the normal in-process case without
466 * uploadpack.allowReachableSHA1InWant,
467 * non-tip requests can never happen.
469 if (!stateless_rpc
&& !(allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
))
478 if (start_command(&cmd
))
482 * If rev-list --stdin encounters an unknown commit, it
483 * terminates, which will cause SIGPIPE in the write loop
486 sigchain_push(SIGPIPE
, SIG_IGN
);
490 for (i
= get_max_object_index(); 0 < i
; ) {
491 o
= get_indexed_object(--i
);
496 memcpy(namebuf
+ 1, sha1_to_hex(o
->sha1
), 40);
497 if (write_in_full(cmd
.in
, namebuf
, 42) < 0)
501 for (i
= 0; i
< want_obj
.nr
; i
++) {
502 o
= want_obj
.objects
[i
].item
;
505 memcpy(namebuf
, sha1_to_hex(o
->sha1
), 40);
506 if (write_in_full(cmd
.in
, namebuf
, 41) < 0)
511 sigchain_pop(SIGPIPE
);
514 * The commits out of the rev-list are not ancestors of
517 i
= read_in_full(cmd
.out
, namebuf
, 1);
523 * rev-list may have died by encountering a bad commit
524 * in the history, in which case we do want to bail out
525 * even when it showed no commit.
527 if (finish_command(&cmd
))
530 /* All the non-tip ones are ancestors of what we advertised */
534 /* Pick one of them (we know there at least is one) */
535 for (i
= 0; i
< want_obj
.nr
; i
++) {
536 o
= want_obj
.objects
[i
].item
;
538 die("git upload-pack: not our ref %s",
539 sha1_to_hex(o
->sha1
));
543 static void receive_needs(void)
545 struct object_array shallows
= OBJECT_ARRAY_INIT
;
552 const char *features
;
553 unsigned char sha1_buf
[20];
554 char *line
= packet_read_line(0, NULL
);
559 if (starts_with(line
, "shallow ")) {
560 unsigned char sha1
[20];
561 struct object
*object
;
562 if (get_sha1_hex(line
+ 8, sha1
))
563 die("invalid shallow line: %s", line
);
564 object
= parse_object(sha1
);
567 if (object
->type
!= OBJ_COMMIT
)
568 die("invalid shallow object %s", sha1_to_hex(sha1
));
569 if (!(object
->flags
& CLIENT_SHALLOW
)) {
570 object
->flags
|= CLIENT_SHALLOW
;
571 add_object_array(object
, NULL
, &shallows
);
575 if (starts_with(line
, "deepen ")) {
577 depth
= strtol(line
+ 7, &end
, 0);
578 if (end
== line
+ 7 || depth
<= 0)
579 die("Invalid deepen: %s", line
);
582 if (!starts_with(line
, "want ") ||
583 get_sha1_hex(line
+5, sha1_buf
))
584 die("git upload-pack: protocol error, "
585 "expected to get sha, not '%s'", line
);
587 features
= line
+ 45;
589 if (parse_feature_request(features
, "multi_ack_detailed"))
591 else if (parse_feature_request(features
, "multi_ack"))
593 if (parse_feature_request(features
, "no-done"))
595 if (parse_feature_request(features
, "thin-pack"))
597 if (parse_feature_request(features
, "ofs-delta"))
599 if (parse_feature_request(features
, "side-band-64k"))
600 use_sideband
= LARGE_PACKET_MAX
;
601 else if (parse_feature_request(features
, "side-band"))
602 use_sideband
= DEFAULT_PACKET_MAX
;
603 if (parse_feature_request(features
, "no-progress"))
605 if (parse_feature_request(features
, "include-tag"))
608 o
= parse_object(sha1_buf
);
610 die("git upload-pack: not our ref %s",
611 sha1_to_hex(sha1_buf
));
612 if (!(o
->flags
& WANTED
)) {
616 add_object_array(o
, NULL
, &want_obj
);
621 * We have sent all our refs already, and the other end
622 * should have chosen out of them. When we are operating
623 * in the stateless RPC mode, however, their choice may
624 * have been based on the set of older refs advertised
625 * by another process that handled the initial request.
630 if (!use_sideband
&& daemon_mode
)
633 if (depth
== 0 && shallows
.nr
== 0)
636 struct commit_list
*result
= NULL
, *backup
= NULL
;
638 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow())
639 for (i
= 0; i
< shallows
.nr
; i
++) {
640 struct object
*object
= shallows
.objects
[i
].item
;
641 object
->flags
|= NOT_SHALLOW
;
645 get_shallow_commits(&want_obj
, depth
,
646 SHALLOW
, NOT_SHALLOW
);
648 struct object
*object
= &result
->item
->object
;
649 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
650 packet_write(1, "shallow %s",
651 sha1_to_hex(object
->sha1
));
652 register_shallow(object
->sha1
);
655 result
= result
->next
;
657 free_commit_list(backup
);
658 for (i
= 0; i
< shallows
.nr
; i
++) {
659 struct object
*object
= shallows
.objects
[i
].item
;
660 if (object
->flags
& NOT_SHALLOW
) {
661 struct commit_list
*parents
;
662 packet_write(1, "unshallow %s",
663 sha1_to_hex(object
->sha1
));
664 object
->flags
&= ~CLIENT_SHALLOW
;
665 /* make sure the real parents are parsed */
666 unregister_shallow(object
->sha1
);
668 parse_commit_or_die((struct commit
*)object
);
669 parents
= ((struct commit
*)object
)->parents
;
671 add_object_array(&parents
->item
->object
,
673 parents
= parents
->next
;
675 add_object_array(object
, NULL
, &extra_edge_obj
);
677 /* make sure commit traversal conforms to client */
678 register_shallow(object
->sha1
);
682 if (shallows
.nr
> 0) {
684 for (i
= 0; i
< shallows
.nr
; i
++)
685 register_shallow(shallows
.objects
[i
].item
->sha1
);
688 shallow_nr
+= shallows
.nr
;
689 free(shallows
.objects
);
692 /* return non-zero if the ref is hidden, otherwise 0 */
693 static int mark_our_ref(const char *refname
, const struct object_id
*oid
)
695 struct object
*o
= lookup_unknown_object(oid
->hash
);
697 if (ref_is_hidden(refname
)) {
698 o
->flags
|= HIDDEN_REF
;
705 static int check_ref(const char *refname
, const struct object_id
*oid
,
706 int flag
, void *cb_data
)
708 mark_our_ref(refname
, oid
);
712 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
714 struct string_list_item
*item
;
718 for_each_string_list_item(item
, symref
)
719 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
722 static int send_ref(const char *refname
, const struct object_id
*oid
,
723 int flag
, void *cb_data
)
725 static const char *capabilities
= "multi_ack thin-pack side-band"
726 " side-band-64k ofs-delta shallow no-progress"
727 " include-tag multi_ack_detailed";
728 const char *refname_nons
= strip_namespace(refname
);
729 struct object_id peeled
;
731 if (mark_our_ref(refname
, oid
))
735 struct strbuf symref_info
= STRBUF_INIT
;
737 format_symref_info(&symref_info
, cb_data
);
738 packet_write(1, "%s %s%c%s%s%s%s%s agent=%s\n",
739 oid_to_hex(oid
), refname_nons
,
741 (allow_unadvertised_object_request
& ALLOW_TIP_SHA1
) ?
742 " allow-tip-sha1-in-want" : "",
743 (allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
) ?
744 " allow-reachable-sha1-in-want" : "",
745 stateless_rpc
? " no-done" : "",
747 git_user_agent_sanitized());
748 strbuf_release(&symref_info
);
750 packet_write(1, "%s %s\n", oid_to_hex(oid
), refname_nons
);
753 if (!peel_ref(refname
, peeled
.hash
))
754 packet_write(1, "%s %s^{}\n", oid_to_hex(&peeled
), refname_nons
);
758 static int find_symref(const char *refname
, const struct object_id
*oid
,
759 int flag
, void *cb_data
)
761 const char *symref_target
;
762 struct string_list_item
*item
;
763 struct object_id unused
;
765 if ((flag
& REF_ISSYMREF
) == 0)
767 symref_target
= resolve_ref_unsafe(refname
, 0, unused
.hash
, &flag
);
768 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
769 die("'%s' is a symref but it is not?", refname
);
770 item
= string_list_append(cb_data
, refname
);
771 item
->util
= xstrdup(symref_target
);
775 static void upload_pack(void)
777 struct string_list symref
= STRING_LIST_INIT_DUP
;
779 head_ref_namespaced(find_symref
, &symref
);
781 if (advertise_refs
|| !stateless_rpc
) {
783 head_ref_namespaced(send_ref
, &symref
);
784 for_each_namespaced_ref(send_ref
, &symref
);
785 advertise_shallow_grafts(1);
788 head_ref_namespaced(check_ref
, NULL
);
789 for_each_namespaced_ref(check_ref
, NULL
);
791 string_list_clear(&symref
, 1);
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 if (git_config_bool(var
, value
))
806 allow_unadvertised_object_request
|= ALLOW_TIP_SHA1
;
808 allow_unadvertised_object_request
&= ~ALLOW_TIP_SHA1
;
809 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var
)) {
810 if (git_config_bool(var
, value
))
811 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
813 allow_unadvertised_object_request
&= ~ALLOW_REACHABLE_SHA1
;
814 } else if (!strcmp("uploadpack.keepalive", var
)) {
815 keepalive
= git_config_int(var
, value
);
819 return parse_hide_refs_config(var
, value
, "uploadpack");
822 int main(int argc
, char **argv
)
830 packet_trace_identity("upload-pack");
831 git_extract_argv0_path(argv
[0]);
832 check_replace_refs
= 0;
834 for (i
= 1; i
< argc
; i
++) {
839 if (!strcmp(arg
, "--advertise-refs")) {
843 if (!strcmp(arg
, "--stateless-rpc")) {
847 if (!strcmp(arg
, "--strict")) {
851 if (starts_with(arg
, "--timeout=")) {
852 timeout
= atoi(arg
+10);
856 if (!strcmp(arg
, "--")) {
863 usage(upload_pack_usage
);
869 if (!enter_repo(dir
, strict
))
870 die("'%s' does not appear to be a git repository", dir
);
872 git_config(upload_pack_config
, NULL
);