11 #include "list-objects.h"
12 #include "run-command.h"
14 static const char upload_pack_usage
[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
16 /* bits #0..7 in revision.h, #8..10 in commit.c */
17 #define THEY_HAVE (1u << 11)
18 #define OUR_REF (1u << 12)
19 #define WANTED (1u << 13)
20 #define COMMON_KNOWN (1u << 14)
21 #define REACHABLE (1u << 15)
23 #define SHALLOW (1u << 16)
24 #define NOT_SHALLOW (1u << 17)
25 #define CLIENT_SHALLOW (1u << 18)
27 static unsigned long oldest_have
;
29 static int multi_ack
, nr_our_refs
;
30 static int use_thin_pack
, use_ofs_delta
, use_include_tag
;
31 static int no_progress
;
32 static struct object_array have_obj
;
33 static struct object_array want_obj
;
34 static unsigned int timeout
;
36 * otherwise maximum packet size (up to 65520 bytes).
38 static int use_sideband
;
41 static void reset_timeout(void)
46 static int strip(char *line
, int len
)
48 if (len
&& line
[len
-1] == '\n')
53 static ssize_t
send_client_data(int fd
, const char *data
, ssize_t sz
)
56 return send_sideband(1, fd
, data
, sz
, use_sideband
);
61 /* XXX: are we happy to lose stuff here? */
65 return safe_write(fd
, data
, sz
);
68 static FILE *pack_pipe
= NULL
;
69 static void show_commit(struct commit
*commit
)
71 if (commit
->object
.flags
& BOUNDARY
)
72 fputc('-', pack_pipe
);
73 if (fputs(sha1_to_hex(commit
->object
.sha1
), pack_pipe
) < 0)
74 die("broken output pipe");
75 fputc('\n', pack_pipe
);
78 commit
->buffer
= NULL
;
81 static void show_object(struct object_array_entry
*p
)
83 /* An object with name "foo\n0000000..." can be used to
84 * confuse downstream git-pack-objects very badly.
86 const char *ep
= strchr(p
->name
, '\n');
88 fprintf(pack_pipe
, "%s %.*s\n", sha1_to_hex(p
->item
->sha1
),
93 fprintf(pack_pipe
, "%s %s\n",
94 sha1_to_hex(p
->item
->sha1
), p
->name
);
97 static void show_edge(struct commit
*commit
)
99 fprintf(pack_pipe
, "-%s\n", sha1_to_hex(commit
->object
.sha1
));
102 static int do_rev_list(int fd
, void *create_full_pack
)
105 struct rev_info revs
;
107 pack_pipe
= fdopen(fd
, "w");
108 if (create_full_pack
)
109 use_thin_pack
= 0; /* no point doing it */
110 init_revisions(&revs
, NULL
);
111 revs
.tag_objects
= 1;
112 revs
.tree_objects
= 1;
113 revs
.blob_objects
= 1;
117 if (create_full_pack
) {
118 const char *args
[] = {"rev-list", "--all", NULL
};
119 setup_revisions(2, args
, &revs
, NULL
);
121 for (i
= 0; i
< want_obj
.nr
; i
++) {
122 struct object
*o
= want_obj
.objects
[i
].item
;
124 o
->flags
&= ~UNINTERESTING
;
125 add_pending_object(&revs
, o
, NULL
);
127 for (i
= 0; i
< have_obj
.nr
; i
++) {
128 struct object
*o
= have_obj
.objects
[i
].item
;
129 o
->flags
|= UNINTERESTING
;
130 add_pending_object(&revs
, o
, NULL
);
132 setup_revisions(0, NULL
, &revs
, NULL
);
134 if (prepare_revision_walk(&revs
))
135 die("revision walk setup failed");
136 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
137 traverse_commit_list(&revs
, show_commit
, show_object
);
143 static void create_pack_file(void)
145 struct async rev_list
;
146 struct child_process pack_objects
;
147 int create_full_pack
= (nr_our_refs
== want_obj
.nr
&& !have_obj
.nr
);
148 char data
[8193], progress
[128];
149 char abort_msg
[] = "aborting due to possible repository "
150 "corruption on the remote side.";
153 const char *argv
[10];
156 rev_list
.proc
= do_rev_list
;
157 /* .data is just a boolean: any non-NULL value will do */
158 rev_list
.data
= create_full_pack
? &rev_list
: NULL
;
159 if (start_async(&rev_list
))
160 die("git upload-pack: unable to fork git-rev-list");
162 argv
[arg
++] = "pack-objects";
163 argv
[arg
++] = "--stdout";
165 argv
[arg
++] = "--progress";
167 argv
[arg
++] = "--delta-base-offset";
169 argv
[arg
++] = "--include-tag";
172 memset(&pack_objects
, 0, sizeof(pack_objects
));
173 pack_objects
.in
= rev_list
.out
; /* start_command closes it */
174 pack_objects
.out
= -1;
175 pack_objects
.err
= -1;
176 pack_objects
.git_cmd
= 1;
177 pack_objects
.argv
= argv
;
179 if (start_command(&pack_objects
))
180 die("git upload-pack: unable to fork git-pack-objects");
182 /* We read from pack_objects.err to capture stderr output for
183 * progress bar, and pack_objects.out to capture the pack data.
187 struct pollfd pfd
[2];
188 int pe
, pu
, pollsize
;
195 if (0 <= pack_objects
.out
) {
196 pfd
[pollsize
].fd
= pack_objects
.out
;
197 pfd
[pollsize
].events
= POLLIN
;
201 if (0 <= pack_objects
.err
) {
202 pfd
[pollsize
].fd
= pack_objects
.err
;
203 pfd
[pollsize
].events
= POLLIN
;
211 if (poll(pfd
, pollsize
, -1) < 0) {
212 if (errno
!= EINTR
) {
213 error("poll failed, resuming: %s",
219 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
220 /* Data ready; we keep the last byte to ourselves
221 * in case we detect broken rev-list, so that we
222 * can leave the stream corrupted. This is
223 * unfortunate -- unpack-objects would happily
224 * accept a valid packdata with trailing garbage,
225 * so appending garbage after we pass all the
226 * pack data is not good enough to signal
227 * breakage to downstream.
235 sz
= xread(pack_objects
.out
, cp
,
236 sizeof(data
) - outsz
);
240 close(pack_objects
.out
);
241 pack_objects
.out
= -1;
247 buffered
= data
[sz
-1] & 0xFF;
252 sz
= send_client_data(1, data
, sz
);
256 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
257 /* Status ready; we ship that in the side-band
258 * or dump to the standard error.
260 sz
= xread(pack_objects
.err
, progress
,
263 send_client_data(2, progress
, sz
);
265 close(pack_objects
.err
);
266 pack_objects
.err
= -1;
273 if (finish_command(&pack_objects
)) {
274 error("git upload-pack: git-pack-objects died with error.");
277 if (finish_async(&rev_list
))
278 goto fail
; /* error was already reported */
283 sz
= send_client_data(1, data
, 1);
286 fprintf(stderr
, "flushed.\n");
293 send_client_data(3, abort_msg
, sizeof(abort_msg
));
294 die("git upload-pack: %s", abort_msg
);
297 static int got_sha1(char *hex
, unsigned char *sha1
)
300 int we_knew_they_have
= 0;
302 if (get_sha1_hex(hex
, sha1
))
303 die("git upload-pack: expected SHA1 object, got '%s'", hex
);
304 if (!has_sha1_file(sha1
))
307 o
= lookup_object(sha1
);
308 if (!(o
&& o
->parsed
))
309 o
= parse_object(sha1
);
311 die("oops (%s)", sha1_to_hex(sha1
));
312 if (o
->type
== OBJ_COMMIT
) {
313 struct commit_list
*parents
;
314 struct commit
*commit
= (struct commit
*)o
;
315 if (o
->flags
& THEY_HAVE
)
316 we_knew_they_have
= 1;
318 o
->flags
|= THEY_HAVE
;
319 if (!oldest_have
|| (commit
->date
< oldest_have
))
320 oldest_have
= commit
->date
;
321 for (parents
= commit
->parents
;
323 parents
= parents
->next
)
324 parents
->item
->object
.flags
|= THEY_HAVE
;
326 if (!we_knew_they_have
) {
327 add_object_array(o
, NULL
, &have_obj
);
333 static int reachable(struct commit
*want
)
335 struct commit_list
*work
= NULL
;
337 insert_by_date(want
, &work
);
339 struct commit_list
*list
= work
->next
;
340 struct commit
*commit
= work
->item
;
344 if (commit
->object
.flags
& THEY_HAVE
) {
345 want
->object
.flags
|= COMMON_KNOWN
;
348 if (!commit
->object
.parsed
)
349 parse_object(commit
->object
.sha1
);
350 if (commit
->object
.flags
& REACHABLE
)
352 commit
->object
.flags
|= REACHABLE
;
353 if (commit
->date
< oldest_have
)
355 for (list
= commit
->parents
; list
; list
= list
->next
) {
356 struct commit
*parent
= list
->item
;
357 if (!(parent
->object
.flags
& REACHABLE
))
358 insert_by_date(parent
, &work
);
361 want
->object
.flags
|= REACHABLE
;
362 clear_commit_marks(want
, REACHABLE
);
363 free_commit_list(work
);
364 return (want
->object
.flags
& COMMON_KNOWN
);
367 static int ok_to_give_up(void)
374 for (i
= 0; i
< want_obj
.nr
; i
++) {
375 struct object
*want
= want_obj
.objects
[i
].item
;
377 if (want
->flags
& COMMON_KNOWN
)
379 want
= deref_tag(want
, "a want line", 0);
380 if (!want
|| want
->type
!= OBJ_COMMIT
) {
381 /* no way to tell if this is reachable by
382 * looking at the ancestry chain alone, so
383 * leave a note to ourselves not to worry about
384 * this object anymore.
386 want_obj
.objects
[i
].item
->flags
|= COMMON_KNOWN
;
389 if (!reachable((struct commit
*)want
))
395 static int get_common_commits(void)
397 static char line
[1000];
398 unsigned char sha1
[20];
399 char hex
[41], last_hex
[41];
402 save_commit_buffer
= 0;
405 len
= packet_read_line(0, line
, sizeof(line
));
409 if (have_obj
.nr
== 0 || multi_ack
)
410 packet_write(1, "NAK\n");
413 len
= strip(line
, len
);
414 if (!prefixcmp(line
, "have ")) {
415 switch (got_sha1(line
+5, sha1
)) {
416 case -1: /* they have what we do not */
417 if (multi_ack
&& ok_to_give_up())
418 packet_write(1, "ACK %s continue\n",
422 memcpy(hex
, sha1_to_hex(sha1
), 41);
424 const char *msg
= "ACK %s continue\n";
425 packet_write(1, msg
, hex
);
426 memcpy(last_hex
, hex
, 41);
428 else if (have_obj
.nr
== 1)
429 packet_write(1, "ACK %s\n", 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 void receive_needs(void)
449 struct object_array shallows
= {0, 0, NULL
};
450 static char line
[1000];
454 write_in_full(debug_fd
, "#S\n", 3);
457 unsigned char sha1_buf
[20];
458 len
= packet_read_line(0, line
, sizeof(line
));
463 write_in_full(debug_fd
, line
, len
);
465 if (!prefixcmp(line
, "shallow ")) {
466 unsigned char sha1
[20];
467 struct object
*object
;
469 if (get_sha1(line
+ 8, sha1
))
470 die("invalid shallow line: %s", line
);
471 object
= parse_object(sha1
);
473 die("did not find object for %s", line
);
474 object
->flags
|= CLIENT_SHALLOW
;
475 add_object_array(object
, NULL
, &shallows
);
478 if (!prefixcmp(line
, "deepen ")) {
481 depth
= strtol(line
+ 7, &end
, 0);
482 if (end
== line
+ 7 || depth
<= 0)
483 die("Invalid deepen: %s", line
);
486 if (prefixcmp(line
, "want ") ||
487 get_sha1_hex(line
+5, sha1_buf
))
488 die("git upload-pack: protocol error, "
489 "expected to get sha, not '%s'", line
);
490 if (strstr(line
+45, "multi_ack"))
492 if (strstr(line
+45, "thin-pack"))
494 if (strstr(line
+45, "ofs-delta"))
496 if (strstr(line
+45, "side-band-64k"))
497 use_sideband
= LARGE_PACKET_MAX
;
498 else if (strstr(line
+45, "side-band"))
499 use_sideband
= DEFAULT_PACKET_MAX
;
500 if (strstr(line
+45, "no-progress"))
502 if (strstr(line
+45, "include-tag"))
505 /* We have sent all our refs already, and the other end
506 * should have chosen out of them; otherwise they are
507 * asking for nonsense.
509 * Hmph. We may later want to allow "want" line that
510 * asks for something like "master~10" (symbolic)...
511 * would it make sense? I don't know.
513 o
= lookup_object(sha1_buf
);
514 if (!o
|| !(o
->flags
& OUR_REF
))
515 die("git upload-pack: not our ref %s", line
+5);
516 if (!(o
->flags
& WANTED
)) {
518 add_object_array(o
, NULL
, &want_obj
);
522 write_in_full(debug_fd
, "#E\n", 3);
523 if (depth
== 0 && shallows
.nr
== 0)
526 struct commit_list
*result
, *backup
;
528 backup
= result
= get_shallow_commits(&want_obj
, depth
,
529 SHALLOW
, NOT_SHALLOW
);
531 struct object
*object
= &result
->item
->object
;
532 if (!(object
->flags
& (CLIENT_SHALLOW
|NOT_SHALLOW
))) {
533 packet_write(1, "shallow %s",
534 sha1_to_hex(object
->sha1
));
535 register_shallow(object
->sha1
);
537 result
= result
->next
;
539 free_commit_list(backup
);
540 for (i
= 0; i
< shallows
.nr
; i
++) {
541 struct object
*object
= shallows
.objects
[i
].item
;
542 if (object
->flags
& NOT_SHALLOW
) {
543 struct commit_list
*parents
;
544 packet_write(1, "unshallow %s",
545 sha1_to_hex(object
->sha1
));
546 object
->flags
&= ~CLIENT_SHALLOW
;
547 /* make sure the real parents are parsed */
548 unregister_shallow(object
->sha1
);
550 if (parse_commit((struct commit
*)object
))
551 die("invalid commit");
552 parents
= ((struct commit
*)object
)->parents
;
554 add_object_array(&parents
->item
->object
,
556 parents
= parents
->next
;
559 /* make sure commit traversal conforms to client */
560 register_shallow(object
->sha1
);
564 if (shallows
.nr
> 0) {
566 for (i
= 0; i
< shallows
.nr
; i
++)
567 register_shallow(shallows
.objects
[i
].item
->sha1
);
569 free(shallows
.objects
);
572 static int send_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
574 static const char *capabilities
= "multi_ack thin-pack side-band"
575 " side-band-64k ofs-delta shallow no-progress"
577 struct object
*o
= parse_object(sha1
);
580 die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1
));
583 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1
), refname
,
586 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), refname
);
588 if (!(o
->flags
& OUR_REF
)) {
592 if (o
->type
== OBJ_TAG
) {
593 o
= deref_tag(o
, refname
, 0);
595 packet_write(1, "%s %s^{}\n", sha1_to_hex(o
->sha1
), refname
);
600 static void upload_pack(void)
603 head_ref(send_ref
, NULL
);
604 for_each_ref(send_ref
, NULL
);
608 get_common_commits();
613 int main(int argc
, char **argv
)
619 for (i
= 1; i
< argc
; i
++) {
624 if (!strcmp(arg
, "--strict")) {
628 if (!prefixcmp(arg
, "--timeout=")) {
629 timeout
= atoi(arg
+10);
632 if (!strcmp(arg
, "--")) {
639 usage(upload_pack_usage
);
645 if (!enter_repo(dir
, strict
))
646 die("'%s': unable to chdir or not a git archive", dir
);
647 if (is_repository_shallow())
648 die("attempt to fetch/clone from a shallow repository");
649 if (getenv("GIT_DEBUG_SEND_PACK"))
650 debug_fd
= atoi(getenv("GIT_DEBUG_SEND_PACK"));