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 /* bits #0..7 in revision.h, #8..10 in commit.c */
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 static int allow_tip_sha1_in_want
;
39 static int shallow_nr
;
40 static struct object_array have_obj
;
41 static struct object_array want_obj
;
42 static struct object_array extra_edge_obj
;
43 static unsigned int timeout
;
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 void create_pack_file(void)
74 struct child_process pack_objects
;
75 char data
[8193], progress
[128];
76 char abort_msg
[] = "aborting due to possible repository "
77 "corruption on the remote side.";
83 char *shallow_file
= NULL
;
86 shallow_file
= setup_temporary_shallow();
87 argv
[arg
++] = "--shallow-file";
88 argv
[arg
++] = shallow_file
;
90 argv
[arg
++] = "pack-objects";
91 argv
[arg
++] = "--revs";
93 argv
[arg
++] = "--thin";
95 argv
[arg
++] = "--stdout";
97 argv
[arg
++] = "--progress";
99 argv
[arg
++] = "--delta-base-offset";
101 argv
[arg
++] = "--include-tag";
104 memset(&pack_objects
, 0, sizeof(pack_objects
));
105 pack_objects
.in
= -1;
106 pack_objects
.out
= -1;
107 pack_objects
.err
= -1;
108 pack_objects
.git_cmd
= 1;
109 pack_objects
.argv
= argv
;
111 if (start_command(&pack_objects
))
112 die("git upload-pack: unable to fork git-pack-objects");
114 pipe_fd
= xfdopen(pack_objects
.in
, "w");
116 for (i
= 0; i
< want_obj
.nr
; i
++)
117 fprintf(pipe_fd
, "%s\n",
118 sha1_to_hex(want_obj
.objects
[i
].item
->sha1
));
119 fprintf(pipe_fd
, "--not\n");
120 for (i
= 0; i
< have_obj
.nr
; i
++)
121 fprintf(pipe_fd
, "%s\n",
122 sha1_to_hex(have_obj
.objects
[i
].item
->sha1
));
123 for (i
= 0; i
< extra_edge_obj
.nr
; i
++)
124 fprintf(pipe_fd
, "%s\n",
125 sha1_to_hex(extra_edge_obj
.objects
[i
].item
->sha1
));
126 fprintf(pipe_fd
, "\n");
130 /* We read from pack_objects.err to capture stderr output for
131 * progress bar, and pack_objects.out to capture the pack data.
135 struct pollfd pfd
[2];
136 int pe
, pu
, pollsize
;
143 if (0 <= pack_objects
.out
) {
144 pfd
[pollsize
].fd
= pack_objects
.out
;
145 pfd
[pollsize
].events
= POLLIN
;
149 if (0 <= pack_objects
.err
) {
150 pfd
[pollsize
].fd
= pack_objects
.err
;
151 pfd
[pollsize
].events
= POLLIN
;
159 if (poll(pfd
, pollsize
, -1) < 0) {
160 if (errno
!= EINTR
) {
161 error("poll failed, resuming: %s",
167 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
168 /* Status ready; we ship that in the side-band
169 * or dump to the standard error.
171 sz
= xread(pack_objects
.err
, progress
,
174 send_client_data(2, progress
, sz
);
176 close(pack_objects
.err
);
177 pack_objects
.err
= -1;
181 /* give priority to status messages */
184 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
185 /* Data ready; we keep the last byte to ourselves
186 * in case we detect broken rev-list, so that we
187 * can leave the stream corrupted. This is
188 * unfortunate -- unpack-objects would happily
189 * accept a valid packdata with trailing garbage,
190 * so appending garbage after we pass all the
191 * pack data is not good enough to signal
192 * breakage to downstream.
200 sz
= xread(pack_objects
.out
, cp
,
201 sizeof(data
) - outsz
);
205 close(pack_objects
.out
);
206 pack_objects
.out
= -1;
212 buffered
= data
[sz
-1] & 0xFF;
217 sz
= send_client_data(1, data
, sz
);
223 if (finish_command(&pack_objects
)) {
224 error("git upload-pack: git-pack-objects died with error.");
229 unlink(shallow_file
);
236 sz
= send_client_data(1, data
, 1);
239 fprintf(stderr
, "flushed.\n");
246 send_client_data(3, abort_msg
, sizeof(abort_msg
));
247 die("git upload-pack: %s", abort_msg
);
250 static int got_sha1(char *hex
, unsigned char *sha1
)
253 int we_knew_they_have
= 0;
255 if (get_sha1_hex(hex
, sha1
))
256 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
257 if (!has_sha1_file(sha1
))
260 o
= parse_object(sha1
);
262 die("oops (%s)", sha1_to_hex(sha1
));
263 if (o
->type
== OBJ_COMMIT
) {
264 struct commit_list
*parents
;
265 struct commit
*commit
= (struct commit
*)o
;
266 if (o
->flags
& THEY_HAVE
)
267 we_knew_they_have
= 1;
269 o
->flags
|= THEY_HAVE
;
270 if (!oldest_have
|| (commit
->date
< oldest_have
))
271 oldest_have
= commit
->date
;
272 for (parents
= commit
->parents
;
274 parents
= parents
->next
)
275 parents
->item
->object
.flags
|= THEY_HAVE
;
277 if (!we_knew_they_have
) {
278 add_object_array(o
, NULL
, &have_obj
);
284 static int reachable(struct commit
*want
)
286 struct commit_list
*work
= NULL
;
288 commit_list_insert_by_date(want
, &work
);
290 struct commit_list
*list
= work
->next
;
291 struct commit
*commit
= work
->item
;
295 if (commit
->object
.flags
& THEY_HAVE
) {
296 want
->object
.flags
|= COMMON_KNOWN
;
299 if (!commit
->object
.parsed
)
300 parse_object(commit
->object
.sha1
);
301 if (commit
->object
.flags
& REACHABLE
)
303 commit
->object
.flags
|= REACHABLE
;
304 if (commit
->date
< oldest_have
)
306 for (list
= commit
->parents
; list
; list
= list
->next
) {
307 struct commit
*parent
= list
->item
;
308 if (!(parent
->object
.flags
& REACHABLE
))
309 commit_list_insert_by_date(parent
, &work
);
312 want
->object
.flags
|= REACHABLE
;
313 clear_commit_marks(want
, REACHABLE
);
314 free_commit_list(work
);
315 return (want
->object
.flags
& COMMON_KNOWN
);
318 static int ok_to_give_up(void)
325 for (i
= 0; i
< want_obj
.nr
; i
++) {
326 struct object
*want
= want_obj
.objects
[i
].item
;
328 if (want
->flags
& COMMON_KNOWN
)
330 want
= deref_tag(want
, "a want line", 0);
331 if (!want
|| want
->type
!= OBJ_COMMIT
) {
332 /* no way to tell if this is reachable by
333 * looking at the ancestry chain alone, so
334 * leave a note to ourselves not to worry about
335 * this object anymore.
337 want_obj
.objects
[i
].item
->flags
|= COMMON_KNOWN
;
340 if (!reachable((struct commit
*)want
))
346 static int get_common_commits(void)
348 unsigned char sha1
[20];
354 save_commit_buffer
= 0;
357 char *line
= packet_read_line(0, NULL
);
361 if (multi_ack
== 2 && got_common
362 && !got_other
&& ok_to_give_up()) {
364 packet_write(1, "ACK %s ready\n", last_hex
);
366 if (have_obj
.nr
== 0 || multi_ack
)
367 packet_write(1, "NAK\n");
369 if (no_done
&& sent_ready
) {
370 packet_write(1, "ACK %s\n", last_hex
);
379 if (!prefixcmp(line
, "have ")) {
380 switch (got_sha1(line
+5, sha1
)) {
381 case -1: /* they have what we do not */
383 if (multi_ack
&& ok_to_give_up()) {
384 const char *hex
= sha1_to_hex(sha1
);
385 if (multi_ack
== 2) {
387 packet_write(1, "ACK %s ready\n", hex
);
389 packet_write(1, "ACK %s continue\n", hex
);
394 memcpy(last_hex
, sha1_to_hex(sha1
), 41);
396 packet_write(1, "ACK %s common\n", last_hex
);
398 packet_write(1, "ACK %s continue\n", last_hex
);
399 else if (have_obj
.nr
== 1)
400 packet_write(1, "ACK %s\n", last_hex
);
405 if (!strcmp(line
, "done")) {
406 if (have_obj
.nr
> 0) {
408 packet_write(1, "ACK %s\n", last_hex
);
411 packet_write(1, "NAK\n");
414 die("git upload-pack: expected SHA1 list, got '%s'", line
);
418 static int is_our_ref(struct object
*o
)
421 ((allow_tip_sha1_in_want
? HIDDEN_REF
: 0) | OUR_REF
);
424 static void check_non_tip(void)
426 static const char *argv
[] = {
427 "rev-list", "--stdin", NULL
,
429 static struct child_process cmd
;
431 char namebuf
[42]; /* ^ + SHA-1 + LF */
434 /* In the normal in-process case non-tip request can never happen */
444 if (start_command(&cmd
))
448 * If rev-list --stdin encounters an unknown commit, it
449 * terminates, which will cause SIGPIPE in the write loop
452 sigchain_push(SIGPIPE
, SIG_IGN
);
456 for (i
= get_max_object_index(); 0 < i
; ) {
457 o
= get_indexed_object(--i
);
462 memcpy(namebuf
+ 1, sha1_to_hex(o
->sha1
), 40);
463 if (write_in_full(cmd
.in
, namebuf
, 42) < 0)
467 for (i
= 0; i
< want_obj
.nr
; i
++) {
468 o
= want_obj
.objects
[i
].item
;
471 memcpy(namebuf
, sha1_to_hex(o
->sha1
), 40);
472 if (write_in_full(cmd
.in
, namebuf
, 41) < 0)
477 sigchain_pop(SIGPIPE
);
480 * The commits out of the rev-list are not ancestors of
483 i
= read_in_full(cmd
.out
, namebuf
, 1);
489 * rev-list may have died by encountering a bad commit
490 * in the history, in which case we do want to bail out
491 * even when it showed no commit.
493 if (finish_command(&cmd
))
496 /* All the non-tip ones are ancestors of what we advertised */
500 /* Pick one of them (we know there at least is one) */
501 for (i
= 0; i
< want_obj
.nr
; i
++) {
502 o
= want_obj
.objects
[i
].item
;
504 die("git upload-pack: not our ref %s",
505 sha1_to_hex(o
->sha1
));
509 static void receive_needs(void)
511 struct object_array shallows
= OBJECT_ARRAY_INIT
;
518 const char *features
;
519 unsigned char sha1_buf
[20];
520 char *line
= packet_read_line(0, NULL
);
525 if (!prefixcmp(line
, "shallow ")) {
526 unsigned char sha1
[20];
527 struct object
*object
;
528 if (get_sha1_hex(line
+ 8, sha1
))
529 die("invalid shallow line: %s", line
);
530 object
= parse_object(sha1
);
533 if (object
->type
!= OBJ_COMMIT
)
534 die("invalid shallow object %s", sha1_to_hex(sha1
));
535 if (!(object
->flags
& CLIENT_SHALLOW
)) {
536 object
->flags
|= CLIENT_SHALLOW
;
537 add_object_array(object
, NULL
, &shallows
);
541 if (!prefixcmp(line
, "deepen ")) {
543 depth
= strtol(line
+ 7, &end
, 0);
544 if (end
== line
+ 7 || depth
<= 0)
545 die("Invalid deepen: %s", line
);
548 if (prefixcmp(line
, "want ") ||
549 get_sha1_hex(line
+5, sha1_buf
))
550 die("git upload-pack: protocol error, "
551 "expected to get sha, not '%s'", line
);
553 features
= line
+ 45;
555 if (parse_feature_request(features
, "multi_ack_detailed"))
557 else if (parse_feature_request(features
, "multi_ack"))
559 if (parse_feature_request(features
, "no-done"))
561 if (parse_feature_request(features
, "thin-pack"))
563 if (parse_feature_request(features
, "ofs-delta"))
565 if (parse_feature_request(features
, "side-band-64k"))
566 use_sideband
= LARGE_PACKET_MAX
;
567 else if (parse_feature_request(features
, "side-band"))
568 use_sideband
= DEFAULT_PACKET_MAX
;
569 if (parse_feature_request(features
, "no-progress"))
571 if (parse_feature_request(features
, "include-tag"))
574 o
= parse_object(sha1_buf
);
576 die("git upload-pack: not our ref %s",
577 sha1_to_hex(sha1_buf
));
578 if (!(o
->flags
& WANTED
)) {
582 add_object_array(o
, NULL
, &want_obj
);
587 * We have sent all our refs already, and the other end
588 * should have chosen out of them. When we are operating
589 * in the stateless RPC mode, however, their choice may
590 * have been based on the set of older refs advertised
591 * by another process that handled the initial request.
596 if (!use_sideband
&& daemon_mode
)
599 if (depth
== 0 && shallows
.nr
== 0)
602 struct commit_list
*result
= NULL
, *backup
= NULL
;
604 if (depth
== INFINITE_DEPTH
)
605 for (i
= 0; i
< shallows
.nr
; i
++) {
606 struct object
*object
= shallows
.objects
[i
].item
;
607 object
->flags
|= NOT_SHALLOW
;
611 get_shallow_commits(&want_obj
, depth
,
612 SHALLOW
, NOT_SHALLOW
);
614 struct object
*object
= &result
->item
->object
;
615 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
616 packet_write(1, "shallow %s",
617 sha1_to_hex(object
->sha1
));
618 register_shallow(object
->sha1
);
621 result
= result
->next
;
623 free_commit_list(backup
);
624 for (i
= 0; i
< shallows
.nr
; i
++) {
625 struct object
*object
= shallows
.objects
[i
].item
;
626 if (object
->flags
& NOT_SHALLOW
) {
627 struct commit_list
*parents
;
628 packet_write(1, "unshallow %s",
629 sha1_to_hex(object
->sha1
));
630 object
->flags
&= ~CLIENT_SHALLOW
;
631 /* make sure the real parents are parsed */
632 unregister_shallow(object
->sha1
);
634 if (parse_commit((struct commit
*)object
))
635 die("invalid commit");
636 parents
= ((struct commit
*)object
)->parents
;
638 add_object_array(&parents
->item
->object
,
640 parents
= parents
->next
;
642 add_object_array(object
, NULL
, &extra_edge_obj
);
644 /* make sure commit traversal conforms to client */
645 register_shallow(object
->sha1
);
649 if (shallows
.nr
> 0) {
651 for (i
= 0; i
< shallows
.nr
; i
++)
652 register_shallow(shallows
.objects
[i
].item
->sha1
);
655 shallow_nr
+= shallows
.nr
;
656 free(shallows
.objects
);
659 /* return non-zero if the ref is hidden, otherwise 0 */
660 static int mark_our_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
662 struct object
*o
= lookup_unknown_object(sha1
);
664 if (ref_is_hidden(refname
)) {
665 o
->flags
|= HIDDEN_REF
;
669 die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1
));
674 static int send_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
676 static const char *capabilities
= "multi_ack thin-pack side-band"
677 " side-band-64k ofs-delta shallow no-progress"
678 " include-tag multi_ack_detailed";
679 const char *refname_nons
= strip_namespace(refname
);
680 unsigned char peeled
[20];
682 if (mark_our_ref(refname
, sha1
, flag
, cb_data
))
686 packet_write(1, "%s %s%c%s%s%s agent=%s\n",
687 sha1_to_hex(sha1
), refname_nons
,
689 allow_tip_sha1_in_want
? " allow-tip-sha1-in-want" : "",
690 stateless_rpc
? " no-done" : "",
691 git_user_agent_sanitized());
693 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), refname_nons
);
695 if (!peel_ref(refname
, peeled
))
696 packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled
), refname_nons
);
700 static void upload_pack(void)
702 if (advertise_refs
|| !stateless_rpc
) {
704 head_ref_namespaced(send_ref
, NULL
);
705 for_each_namespaced_ref(send_ref
, NULL
);
708 head_ref_namespaced(mark_our_ref
, NULL
);
709 for_each_namespaced_ref(mark_our_ref
, NULL
);
716 get_common_commits();
721 static int upload_pack_config(const char *var
, const char *value
, void *unused
)
723 if (!strcmp("uploadpack.allowtipsha1inwant", var
))
724 allow_tip_sha1_in_want
= git_config_bool(var
, value
);
725 return parse_hide_refs_config(var
, value
, "uploadpack");
728 int main(int argc
, char **argv
)
736 packet_trace_identity("upload-pack");
737 git_extract_argv0_path(argv
[0]);
738 read_replace_refs
= 0;
740 for (i
= 1; i
< argc
; i
++) {
745 if (!strcmp(arg
, "--advertise-refs")) {
749 if (!strcmp(arg
, "--stateless-rpc")) {
753 if (!strcmp(arg
, "--strict")) {
757 if (!prefixcmp(arg
, "--timeout=")) {
758 timeout
= atoi(arg
+10);
762 if (!strcmp(arg
, "--")) {
769 usage(upload_pack_usage
);
775 if (!enter_repo(dir
, strict
))
776 die("'%s' does not appear to be a git repository", dir
);
777 if (is_repository_shallow())
778 die("attempt to fetch/clone from a shallow repository");
779 git_config(upload_pack_config
, NULL
);