11 #include "list-objects.h"
12 #include "run-command.h"
16 #include "string-list.h"
17 #include "parse-options.h"
19 static const char * const upload_pack_usage
[] = {
20 N_("git upload-pack [<options>] <dir>"),
24 /* Remember to update object flag allocation in object.h */
25 #define THEY_HAVE (1u << 11)
26 #define OUR_REF (1u << 12)
27 #define WANTED (1u << 13)
28 #define COMMON_KNOWN (1u << 14)
29 #define REACHABLE (1u << 15)
31 #define SHALLOW (1u << 16)
32 #define NOT_SHALLOW (1u << 17)
33 #define CLIENT_SHALLOW (1u << 18)
34 #define HIDDEN_REF (1u << 19)
36 static unsigned long oldest_have
;
40 static int use_thin_pack
, use_ofs_delta
, use_include_tag
;
41 static int no_progress
, daemon_mode
;
42 /* Allow specifying sha1 if it is a ref tip. */
43 #define ALLOW_TIP_SHA1 01
44 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
45 #define ALLOW_REACHABLE_SHA1 02
46 static unsigned int allow_unadvertised_object_request
;
47 static int shallow_nr
;
48 static struct object_array have_obj
;
49 static struct object_array want_obj
;
50 static struct object_array extra_edge_obj
;
51 static unsigned int timeout
;
52 static int keepalive
= 5;
54 * otherwise maximum packet size (up to 65520 bytes).
56 static int use_sideband
;
57 static int advertise_refs
;
58 static int stateless_rpc
;
60 static void reset_timeout(void)
65 static ssize_t
send_client_data(int fd
, const char *data
, ssize_t sz
)
68 return send_sideband(1, fd
, data
, sz
, use_sideband
);
73 /* XXX: are we happy to lose stuff here? */
77 write_or_die(fd
, data
, sz
);
81 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
84 if (graft
->nr_parent
== -1)
85 fprintf(fp
, "--shallow %s\n", oid_to_hex(&graft
->oid
));
89 static void create_pack_file(void)
91 struct child_process pack_objects
= CHILD_PROCESS_INIT
;
92 char data
[8193], progress
[128];
93 char abort_msg
[] = "aborting due to possible repository "
94 "corruption on the remote side.";
101 argv_array_push(&pack_objects
.args
, "--shallow-file");
102 argv_array_push(&pack_objects
.args
, "");
104 argv_array_push(&pack_objects
.args
, "pack-objects");
105 argv_array_push(&pack_objects
.args
, "--revs");
107 argv_array_push(&pack_objects
.args
, "--thin");
109 argv_array_push(&pack_objects
.args
, "--stdout");
111 argv_array_push(&pack_objects
.args
, "--shallow");
113 argv_array_push(&pack_objects
.args
, "--progress");
115 argv_array_push(&pack_objects
.args
, "--delta-base-offset");
117 argv_array_push(&pack_objects
.args
, "--include-tag");
119 pack_objects
.in
= -1;
120 pack_objects
.out
= -1;
121 pack_objects
.err
= -1;
122 pack_objects
.git_cmd
= 1;
124 if (start_command(&pack_objects
))
125 die("git upload-pack: unable to fork git-pack-objects");
127 pipe_fd
= xfdopen(pack_objects
.in
, "w");
130 for_each_commit_graft(write_one_shallow
, pipe_fd
);
132 for (i
= 0; i
< want_obj
.nr
; i
++)
133 fprintf(pipe_fd
, "%s\n",
134 oid_to_hex(&want_obj
.objects
[i
].item
->oid
));
135 fprintf(pipe_fd
, "--not\n");
136 for (i
= 0; i
< have_obj
.nr
; i
++)
137 fprintf(pipe_fd
, "%s\n",
138 oid_to_hex(&have_obj
.objects
[i
].item
->oid
));
139 for (i
= 0; i
< extra_edge_obj
.nr
; i
++)
140 fprintf(pipe_fd
, "%s\n",
141 oid_to_hex(&extra_edge_obj
.objects
[i
].item
->oid
));
142 fprintf(pipe_fd
, "\n");
146 /* We read from pack_objects.err to capture stderr output for
147 * progress bar, and pack_objects.out to capture the pack data.
151 struct pollfd pfd
[2];
152 int pe
, pu
, pollsize
;
160 if (0 <= pack_objects
.out
) {
161 pfd
[pollsize
].fd
= pack_objects
.out
;
162 pfd
[pollsize
].events
= POLLIN
;
166 if (0 <= pack_objects
.err
) {
167 pfd
[pollsize
].fd
= pack_objects
.err
;
168 pfd
[pollsize
].events
= POLLIN
;
176 ret
= poll(pfd
, pollsize
,
177 keepalive
< 0 ? -1 : 1000 * keepalive
);
180 if (errno
!= EINTR
) {
181 error_errno("poll failed, resuming");
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
;
320 struct commit
*commit
= pop_commit(&work
);
322 if (commit
->object
.flags
& THEY_HAVE
) {
323 want
->object
.flags
|= COMMON_KNOWN
;
326 if (!commit
->object
.parsed
)
327 parse_object(commit
->object
.oid
.hash
);
328 if (commit
->object
.flags
& REACHABLE
)
330 commit
->object
.flags
|= REACHABLE
;
331 if (commit
->date
< oldest_have
)
333 for (list
= commit
->parents
; list
; list
= list
->next
) {
334 struct commit
*parent
= list
->item
;
335 if (!(parent
->object
.flags
& REACHABLE
))
336 commit_list_insert_by_date(parent
, &work
);
339 want
->object
.flags
|= REACHABLE
;
340 clear_commit_marks(want
, REACHABLE
);
341 free_commit_list(work
);
342 return (want
->object
.flags
& COMMON_KNOWN
);
345 static int ok_to_give_up(void)
352 for (i
= 0; i
< want_obj
.nr
; i
++) {
353 struct object
*want
= want_obj
.objects
[i
].item
;
355 if (want
->flags
& COMMON_KNOWN
)
357 want
= deref_tag(want
, "a want line", 0);
358 if (!want
|| want
->type
!= OBJ_COMMIT
) {
359 /* no way to tell if this is reachable by
360 * looking at the ancestry chain alone, so
361 * leave a note to ourselves not to worry about
362 * this object anymore.
364 want_obj
.objects
[i
].item
->flags
|= COMMON_KNOWN
;
367 if (!reachable((struct commit
*)want
))
373 static int get_common_commits(void)
375 unsigned char sha1
[20];
381 save_commit_buffer
= 0;
384 char *line
= packet_read_line(0, NULL
);
388 if (multi_ack
== 2 && got_common
389 && !got_other
&& ok_to_give_up()) {
391 packet_write(1, "ACK %s ready\n", last_hex
);
393 if (have_obj
.nr
== 0 || multi_ack
)
394 packet_write(1, "NAK\n");
396 if (no_done
&& sent_ready
) {
397 packet_write(1, "ACK %s\n", last_hex
);
406 if (starts_with(line
, "have ")) {
407 switch (got_sha1(line
+5, sha1
)) {
408 case -1: /* they have what we do not */
410 if (multi_ack
&& ok_to_give_up()) {
411 const char *hex
= sha1_to_hex(sha1
);
412 if (multi_ack
== 2) {
414 packet_write(1, "ACK %s ready\n", hex
);
416 packet_write(1, "ACK %s continue\n", hex
);
421 memcpy(last_hex
, sha1_to_hex(sha1
), 41);
423 packet_write(1, "ACK %s common\n", last_hex
);
425 packet_write(1, "ACK %s continue\n", last_hex
);
426 else if (have_obj
.nr
== 1)
427 packet_write(1, "ACK %s\n", last_hex
);
432 if (!strcmp(line
, "done")) {
433 if (have_obj
.nr
> 0) {
435 packet_write(1, "ACK %s\n", last_hex
);
438 packet_write(1, "NAK\n");
441 die("git upload-pack: expected SHA1 list, got '%s'", line
);
445 static int is_our_ref(struct object
*o
)
447 int allow_hidden_ref
= (allow_unadvertised_object_request
&
448 (ALLOW_TIP_SHA1
| ALLOW_REACHABLE_SHA1
));
449 return o
->flags
& ((allow_hidden_ref
? HIDDEN_REF
: 0) | OUR_REF
);
452 static void check_non_tip(void)
454 static const char *argv
[] = {
455 "rev-list", "--stdin", NULL
,
457 static struct child_process cmd
= CHILD_PROCESS_INIT
;
459 char namebuf
[42]; /* ^ + SHA-1 + LF */
463 * In the normal in-process case without
464 * uploadpack.allowReachableSHA1InWant,
465 * non-tip requests can never happen.
467 if (!stateless_rpc
&& !(allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
))
476 if (start_command(&cmd
))
480 * If rev-list --stdin encounters an unknown commit, it
481 * terminates, which will cause SIGPIPE in the write loop
484 sigchain_push(SIGPIPE
, SIG_IGN
);
488 for (i
= get_max_object_index(); 0 < i
; ) {
489 o
= get_indexed_object(--i
);
494 memcpy(namebuf
+ 1, oid_to_hex(&o
->oid
), GIT_SHA1_HEXSZ
);
495 if (write_in_full(cmd
.in
, namebuf
, 42) < 0)
499 for (i
= 0; i
< want_obj
.nr
; i
++) {
500 o
= want_obj
.objects
[i
].item
;
503 memcpy(namebuf
, oid_to_hex(&o
->oid
), GIT_SHA1_HEXSZ
);
504 if (write_in_full(cmd
.in
, namebuf
, 41) < 0)
509 sigchain_pop(SIGPIPE
);
512 * The commits out of the rev-list are not ancestors of
515 i
= read_in_full(cmd
.out
, namebuf
, 1);
521 * rev-list may have died by encountering a bad commit
522 * in the history, in which case we do want to bail out
523 * even when it showed no commit.
525 if (finish_command(&cmd
))
528 /* All the non-tip ones are ancestors of what we advertised */
532 /* Pick one of them (we know there at least is one) */
533 for (i
= 0; i
< want_obj
.nr
; i
++) {
534 o
= want_obj
.objects
[i
].item
;
536 die("git upload-pack: not our ref %s",
537 oid_to_hex(&o
->oid
));
541 static void receive_needs(void)
543 struct object_array shallows
= OBJECT_ARRAY_INIT
;
550 const char *features
;
551 unsigned char sha1_buf
[20];
552 char *line
= packet_read_line(0, NULL
);
557 if (starts_with(line
, "shallow ")) {
558 unsigned char sha1
[20];
559 struct object
*object
;
560 if (get_sha1_hex(line
+ 8, sha1
))
561 die("invalid shallow line: %s", line
);
562 object
= parse_object(sha1
);
565 if (object
->type
!= OBJ_COMMIT
)
566 die("invalid shallow object %s", sha1_to_hex(sha1
));
567 if (!(object
->flags
& CLIENT_SHALLOW
)) {
568 object
->flags
|= CLIENT_SHALLOW
;
569 add_object_array(object
, NULL
, &shallows
);
573 if (starts_with(line
, "deepen ")) {
575 depth
= strtol(line
+ 7, &end
, 0);
576 if (end
== line
+ 7 || depth
<= 0)
577 die("Invalid deepen: %s", line
);
580 if (!starts_with(line
, "want ") ||
581 get_sha1_hex(line
+5, sha1_buf
))
582 die("git upload-pack: protocol error, "
583 "expected to get sha, not '%s'", line
);
585 features
= line
+ 45;
587 if (parse_feature_request(features
, "multi_ack_detailed"))
589 else if (parse_feature_request(features
, "multi_ack"))
591 if (parse_feature_request(features
, "no-done"))
593 if (parse_feature_request(features
, "thin-pack"))
595 if (parse_feature_request(features
, "ofs-delta"))
597 if (parse_feature_request(features
, "side-band-64k"))
598 use_sideband
= LARGE_PACKET_MAX
;
599 else if (parse_feature_request(features
, "side-band"))
600 use_sideband
= DEFAULT_PACKET_MAX
;
601 if (parse_feature_request(features
, "no-progress"))
603 if (parse_feature_request(features
, "include-tag"))
606 o
= parse_object(sha1_buf
);
608 die("git upload-pack: not our ref %s",
609 sha1_to_hex(sha1_buf
));
610 if (!(o
->flags
& WANTED
)) {
614 add_object_array(o
, NULL
, &want_obj
);
619 * We have sent all our refs already, and the other end
620 * should have chosen out of them. When we are operating
621 * in the stateless RPC mode, however, their choice may
622 * have been based on the set of older refs advertised
623 * by another process that handled the initial request.
628 if (!use_sideband
&& daemon_mode
)
631 if (depth
== 0 && shallows
.nr
== 0)
634 struct commit_list
*result
= NULL
, *backup
= NULL
;
636 if (depth
== INFINITE_DEPTH
&& !is_repository_shallow())
637 for (i
= 0; i
< shallows
.nr
; i
++) {
638 struct object
*object
= shallows
.objects
[i
].item
;
639 object
->flags
|= NOT_SHALLOW
;
643 get_shallow_commits(&want_obj
, depth
,
644 SHALLOW
, NOT_SHALLOW
);
646 struct object
*object
= &result
->item
->object
;
647 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
648 packet_write(1, "shallow %s",
649 oid_to_hex(&object
->oid
));
650 register_shallow(object
->oid
.hash
);
653 result
= result
->next
;
655 free_commit_list(backup
);
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
;
660 packet_write(1, "unshallow %s",
661 oid_to_hex(&object
->oid
));
662 object
->flags
&= ~CLIENT_SHALLOW
;
663 /* make sure the real parents are parsed */
664 unregister_shallow(object
->oid
.hash
);
666 parse_commit_or_die((struct commit
*)object
);
667 parents
= ((struct commit
*)object
)->parents
;
669 add_object_array(&parents
->item
->object
,
671 parents
= parents
->next
;
673 add_object_array(object
, NULL
, &extra_edge_obj
);
675 /* make sure commit traversal conforms to client */
676 register_shallow(object
->oid
.hash
);
680 if (shallows
.nr
> 0) {
682 for (i
= 0; i
< shallows
.nr
; i
++)
683 register_shallow(shallows
.objects
[i
].item
->oid
.hash
);
686 shallow_nr
+= shallows
.nr
;
687 free(shallows
.objects
);
690 /* return non-zero if the ref is hidden, otherwise 0 */
691 static int mark_our_ref(const char *refname
, const char *refname_full
,
692 const struct object_id
*oid
)
694 struct object
*o
= lookup_unknown_object(oid
->hash
);
696 if (ref_is_hidden(refname
, refname_full
)) {
697 o
->flags
|= HIDDEN_REF
;
704 static int check_ref(const char *refname_full
, const struct object_id
*oid
,
705 int flag
, void *cb_data
)
707 const char *refname
= strip_namespace(refname_full
);
709 mark_our_ref(refname
, refname_full
, oid
);
713 static void format_symref_info(struct strbuf
*buf
, struct string_list
*symref
)
715 struct string_list_item
*item
;
719 for_each_string_list_item(item
, symref
)
720 strbuf_addf(buf
, " symref=%s:%s", item
->string
, (char *)item
->util
);
723 static int send_ref(const char *refname
, const struct object_id
*oid
,
724 int flag
, void *cb_data
)
726 static const char *capabilities
= "multi_ack thin-pack side-band"
727 " side-band-64k ofs-delta shallow no-progress"
728 " include-tag multi_ack_detailed";
729 const char *refname_nons
= strip_namespace(refname
);
730 struct object_id peeled
;
732 if (mark_our_ref(refname_nons
, refname
, oid
))
736 struct strbuf symref_info
= STRBUF_INIT
;
738 format_symref_info(&symref_info
, cb_data
);
739 packet_write(1, "%s %s%c%s%s%s%s%s agent=%s\n",
740 oid_to_hex(oid
), refname_nons
,
742 (allow_unadvertised_object_request
& ALLOW_TIP_SHA1
) ?
743 " allow-tip-sha1-in-want" : "",
744 (allow_unadvertised_object_request
& ALLOW_REACHABLE_SHA1
) ?
745 " allow-reachable-sha1-in-want" : "",
746 stateless_rpc
? " no-done" : "",
748 git_user_agent_sanitized());
749 strbuf_release(&symref_info
);
751 packet_write(1, "%s %s\n", oid_to_hex(oid
), refname_nons
);
754 if (!peel_ref(refname
, peeled
.hash
))
755 packet_write(1, "%s %s^{}\n", oid_to_hex(&peeled
), refname_nons
);
759 static int find_symref(const char *refname
, const struct object_id
*oid
,
760 int flag
, void *cb_data
)
762 const char *symref_target
;
763 struct string_list_item
*item
;
764 struct object_id unused
;
766 if ((flag
& REF_ISSYMREF
) == 0)
768 symref_target
= resolve_ref_unsafe(refname
, 0, unused
.hash
, &flag
);
769 if (!symref_target
|| (flag
& REF_ISSYMREF
) == 0)
770 die("'%s' is a symref but it is not?", refname
);
771 item
= string_list_append(cb_data
, refname
);
772 item
->util
= xstrdup(symref_target
);
776 static void upload_pack(void)
778 struct string_list symref
= STRING_LIST_INIT_DUP
;
780 head_ref_namespaced(find_symref
, &symref
);
782 if (advertise_refs
|| !stateless_rpc
) {
784 head_ref_namespaced(send_ref
, &symref
);
785 for_each_namespaced_ref(send_ref
, &symref
);
786 advertise_shallow_grafts(1);
789 head_ref_namespaced(check_ref
, NULL
);
790 for_each_namespaced_ref(check_ref
, NULL
);
792 string_list_clear(&symref
, 1);
798 get_common_commits();
803 static int upload_pack_config(const char *var
, const char *value
, void *unused
)
805 if (!strcmp("uploadpack.allowtipsha1inwant", var
)) {
806 if (git_config_bool(var
, value
))
807 allow_unadvertised_object_request
|= ALLOW_TIP_SHA1
;
809 allow_unadvertised_object_request
&= ~ALLOW_TIP_SHA1
;
810 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var
)) {
811 if (git_config_bool(var
, value
))
812 allow_unadvertised_object_request
|= ALLOW_REACHABLE_SHA1
;
814 allow_unadvertised_object_request
&= ~ALLOW_REACHABLE_SHA1
;
815 } else if (!strcmp("uploadpack.keepalive", var
)) {
816 keepalive
= git_config_int(var
, value
);
820 return parse_hide_refs_config(var
, value
, "uploadpack");
823 int main(int argc
, const char **argv
)
827 struct option options
[] = {
828 OPT_BOOL(0, "stateless-rpc", &stateless_rpc
,
829 N_("quit after a single request/response exchange")),
830 OPT_BOOL(0, "advertise-refs", &advertise_refs
,
831 N_("exit immediately after intial ref advertisement")),
832 OPT_BOOL(0, "strict", &strict
,
833 N_("do not try <directory>/.git/ if <directory> is no Git directory")),
834 OPT_INTEGER(0, "timeout", &timeout
,
835 N_("interrupt transfer after <n> seconds of inactivity")),
841 packet_trace_identity("upload-pack");
842 git_extract_argv0_path(argv
[0]);
843 check_replace_refs
= 0;
845 argc
= parse_options(argc
, argv
, NULL
, options
, upload_pack_usage
, 0);
848 usage_with_options(upload_pack_usage
, options
);
857 if (!enter_repo(dir
, strict
))
858 die("'%s' does not appear to be a git repository", dir
);
860 git_config(upload_pack_config
, NULL
);