14 #include "list-objects.h"
16 static const char upload_pack_usage
[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
18 /* bits #0..7 in revision.h, #8..10 in commit.c */
19 #define THEY_HAVE (1u << 11)
20 #define OUR_REF (1u << 12)
21 #define WANTED (1u << 13)
22 #define COMMON_KNOWN (1u << 14)
23 #define REACHABLE (1u << 15)
25 static unsigned long oldest_have
;
27 static int multi_ack
, nr_our_refs
;
28 static int use_thin_pack
, use_ofs_delta
;
29 static struct object_array have_obj
;
30 static struct object_array want_obj
;
31 static unsigned int timeout
;
33 * otherwise maximum packet size (up to 65520 bytes).
35 static int use_sideband
;
37 static void reset_timeout(void)
42 static int strip(char *line
, int len
)
44 if (len
&& line
[len
-1] == '\n')
49 static ssize_t
send_client_data(int fd
, const char *data
, ssize_t sz
)
52 return send_sideband(1, fd
, data
, sz
, use_sideband
);
60 return safe_write(fd
, data
, sz
);
63 FILE *pack_pipe
= NULL
;
64 static void show_commit(struct commit
*commit
)
66 if (commit
->object
.flags
& BOUNDARY
)
67 fputc('-', pack_pipe
);
68 if (fputs(sha1_to_hex(commit
->object
.sha1
), pack_pipe
) < 0)
69 die("broken output pipe");
70 fputc('\n', pack_pipe
);
73 commit
->buffer
= NULL
;
76 static void show_object(struct object_array_entry
*p
)
78 /* An object with name "foo\n0000000..." can be used to
79 * confuse downstream git-pack-objects very badly.
81 const char *ep
= strchr(p
->name
, '\n');
83 fprintf(pack_pipe
, "%s %.*s\n", sha1_to_hex(p
->item
->sha1
),
88 fprintf(pack_pipe
, "%s %s\n",
89 sha1_to_hex(p
->item
->sha1
), p
->name
);
92 static void show_edge(struct commit
*commit
)
94 fprintf(pack_pipe
, "-%s\n", sha1_to_hex(commit
->object
.sha1
));
97 static void create_pack_file(void)
99 /* Pipes between rev-list to pack-objects, pack-objects to us
100 * and pack-objects error stream for progress bar.
102 int lp_pipe
[2], pu_pipe
[2], pe_pipe
[2];
103 pid_t pid_rev_list
, pid_pack_objects
;
104 int create_full_pack
= (nr_our_refs
== want_obj
.nr
&& !have_obj
.nr
);
105 char data
[8193], progress
[128];
106 char abort_msg
[] = "aborting due to possible repository "
107 "corruption on the remote side.";
110 if (pipe(lp_pipe
) < 0)
111 die("git-upload-pack: unable to create pipe");
112 pid_rev_list
= fork();
113 if (pid_rev_list
< 0)
114 die("git-upload-pack: unable to fork git-rev-list");
118 struct rev_info revs
;
120 pack_pipe
= fdopen(lp_pipe
[1], "w");
122 if (create_full_pack
)
123 use_thin_pack
= 0; /* no point doing it */
124 init_revisions(&revs
, NULL
);
125 revs
.tag_objects
= 1;
126 revs
.tree_objects
= 1;
127 revs
.blob_objects
= 1;
131 if (create_full_pack
) {
132 const char *args
[] = {"rev-list", "--all", NULL
};
133 setup_revisions(2, args
, &revs
, NULL
);
135 for (i
= 0; i
< want_obj
.nr
; i
++) {
136 struct object
*o
= want_obj
.objects
[i
].item
;
137 add_pending_object(&revs
, o
, NULL
);
139 for (i
= 0; i
< have_obj
.nr
; i
++) {
140 struct object
*o
= have_obj
.objects
[i
].item
;
141 o
->flags
|= UNINTERESTING
;
142 add_pending_object(&revs
, o
, NULL
);
144 setup_revisions(0, NULL
, &revs
, NULL
);
146 prepare_revision_walk(&revs
);
147 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
148 traverse_commit_list(&revs
, show_commit
, show_object
);
152 if (pipe(pu_pipe
) < 0)
153 die("git-upload-pack: unable to create pipe");
154 if (pipe(pe_pipe
) < 0)
155 die("git-upload-pack: unable to create pipe");
156 pid_pack_objects
= fork();
157 if (pid_pack_objects
< 0) {
158 /* daemon sets things up to ignore TERM */
159 kill(pid_rev_list
, SIGKILL
);
160 die("git-upload-pack: unable to fork git-pack-objects");
162 if (!pid_pack_objects
) {
173 execl_git_cmd("pack-objects", "--stdout", "--progress",
174 use_ofs_delta
? "--delta-base-offset" : NULL
,
176 kill(pid_rev_list
, SIGKILL
);
177 die("git-upload-pack: unable to exec git-pack-objects");
183 /* We read from pe_pipe[0] to capture stderr output for
184 * progress bar, and pu_pipe[0] to capture the pack data.
191 struct pollfd pfd
[2];
195 int pe
, pu
, pollsize
;
202 if (0 <= pu_pipe
[0]) {
203 pfd
[pollsize
].fd
= pu_pipe
[0];
204 pfd
[pollsize
].events
= POLLIN
;
208 if (0 <= pe_pipe
[0]) {
209 pfd
[pollsize
].fd
= pe_pipe
[0];
210 pfd
[pollsize
].events
= POLLIN
;
216 if (poll(pfd
, pollsize
, -1) < 0) {
217 if (errno
!= EINTR
) {
218 error("poll failed, resuming: %s",
224 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
225 /* Data ready; we keep the last byte
226 * to ourselves in case we detect
227 * broken rev-list, so that we can
228 * leave the stream corrupted. This
229 * is unfortunate -- unpack-objects
230 * would happily accept a valid pack
231 * data with trailing garbage, so
232 * appending garbage after we pass all
233 * the pack data is not good enough to
234 * signal breakage to downstream.
242 sz
= read(pu_pipe
[0], cp
,
243 sizeof(data
) - outsz
);
254 buffered
= data
[sz
-1] & 0xFF;
259 sz
= send_client_data(1, data
, sz
);
263 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
264 /* Status ready; we ship that in the side-band
265 * or dump to the standard error.
267 sz
= read(pe_pipe
[0], progress
,
270 send_client_data(2, progress
, sz
);
280 /* See if the children are still there */
281 if (pid_rev_list
|| pid_pack_objects
) {
282 pid
= waitpid(-1, &status
, WNOHANG
);
285 who
= ((pid
== pid_rev_list
) ? "git-rev-list" :
286 (pid
== pid_pack_objects
) ? "git-pack-objects" :
290 error("git-upload-pack: %s",
294 error("git-upload-pack: we weren't "
295 "waiting for %d", pid
);
298 if (!WIFEXITED(status
) || WEXITSTATUS(status
) > 0) {
299 error("git-upload-pack: %s died with error.",
303 if (pid
== pid_rev_list
)
305 if (pid
== pid_pack_objects
)
306 pid_pack_objects
= 0;
307 if (pid_rev_list
|| pid_pack_objects
)
311 /* both died happily */
318 sz
= send_client_data(1, data
, 1);
321 fprintf(stderr
, "flushed.\n");
328 if (pid_pack_objects
)
329 kill(pid_pack_objects
, SIGKILL
);
331 kill(pid_rev_list
, SIGKILL
);
332 send_client_data(3, abort_msg
, sizeof(abort_msg
));
333 die("git-upload-pack: %s", abort_msg
);
336 static int got_sha1(char *hex
, unsigned char *sha1
)
339 int we_knew_they_have
= 0;
341 if (get_sha1_hex(hex
, sha1
))
342 die("git-upload-pack: expected SHA1 object, got '%s'", hex
);
343 if (!has_sha1_file(sha1
))
346 o
= lookup_object(sha1
);
347 if (!(o
&& o
->parsed
))
348 o
= parse_object(sha1
);
350 die("oops (%s)", sha1_to_hex(sha1
));
351 if (o
->type
== OBJ_COMMIT
) {
352 struct commit_list
*parents
;
353 struct commit
*commit
= (struct commit
*)o
;
354 if (o
->flags
& THEY_HAVE
)
355 we_knew_they_have
= 1;
357 o
->flags
|= THEY_HAVE
;
358 if (!oldest_have
|| (commit
->date
< oldest_have
))
359 oldest_have
= commit
->date
;
360 for (parents
= commit
->parents
;
362 parents
= parents
->next
)
363 parents
->item
->object
.flags
|= THEY_HAVE
;
365 if (!we_knew_they_have
) {
366 add_object_array(o
, NULL
, &have_obj
);
372 static int reachable(struct commit
*want
)
374 struct commit_list
*work
= NULL
;
376 insert_by_date(want
, &work
);
378 struct commit_list
*list
= work
->next
;
379 struct commit
*commit
= work
->item
;
383 if (commit
->object
.flags
& THEY_HAVE
) {
384 want
->object
.flags
|= COMMON_KNOWN
;
387 if (!commit
->object
.parsed
)
388 parse_object(commit
->object
.sha1
);
389 if (commit
->object
.flags
& REACHABLE
)
391 commit
->object
.flags
|= REACHABLE
;
392 if (commit
->date
< oldest_have
)
394 for (list
= commit
->parents
; list
; list
= list
->next
) {
395 struct commit
*parent
= list
->item
;
396 if (!(parent
->object
.flags
& REACHABLE
))
397 insert_by_date(parent
, &work
);
400 want
->object
.flags
|= REACHABLE
;
401 clear_commit_marks(want
, REACHABLE
);
402 free_commit_list(work
);
403 return (want
->object
.flags
& COMMON_KNOWN
);
406 static int ok_to_give_up(void)
413 for (i
= 0; i
< want_obj
.nr
; i
++) {
414 struct object
*want
= want_obj
.objects
[i
].item
;
416 if (want
->flags
& COMMON_KNOWN
)
418 want
= deref_tag(want
, "a want line", 0);
419 if (!want
|| want
->type
!= OBJ_COMMIT
) {
420 /* no way to tell if this is reachable by
421 * looking at the ancestry chain alone, so
422 * leave a note to ourselves not to worry about
423 * this object anymore.
425 want_obj
.objects
[i
].item
->flags
|= COMMON_KNOWN
;
428 if (!reachable((struct commit
*)want
))
434 static int get_common_commits(void)
436 static char line
[1000];
437 unsigned char sha1
[20];
438 char hex
[41], last_hex
[41];
441 track_object_refs
= 0;
442 save_commit_buffer
= 0;
445 len
= packet_read_line(0, line
, sizeof(line
));
449 if (have_obj
.nr
== 0 || multi_ack
)
450 packet_write(1, "NAK\n");
453 len
= strip(line
, len
);
454 if (!strncmp(line
, "have ", 5)) {
455 switch (got_sha1(line
+5, sha1
)) {
456 case -1: /* they have what we do not */
457 if (multi_ack
&& ok_to_give_up())
458 packet_write(1, "ACK %s continue\n",
462 memcpy(hex
, sha1_to_hex(sha1
), 41);
464 const char *msg
= "ACK %s continue\n";
465 packet_write(1, msg
, hex
);
466 memcpy(last_hex
, hex
, 41);
468 else if (have_obj
.nr
== 1)
469 packet_write(1, "ACK %s\n", hex
);
474 if (!strcmp(line
, "done")) {
475 if (have_obj
.nr
> 0) {
477 packet_write(1, "ACK %s\n", last_hex
);
480 packet_write(1, "NAK\n");
483 die("git-upload-pack: expected SHA1 list, got '%s'", line
);
487 static void receive_needs(void)
489 struct object_array shallows
= {0, 0, NULL
};
490 static char line
[1000];
495 unsigned char sha1_buf
[20];
496 len
= packet_read_line(0, line
, sizeof(line
));
501 if (!strncmp("shallow ", line
, 8)) {
502 unsigned char sha1
[20];
503 struct object
*object
;
504 if (get_sha1(line
+ 8, sha1
))
505 die("invalid shallow line: %s", line
);
506 object
= parse_object(sha1
);
508 die("did not find object for %s", line
);
509 add_object_array(object
, NULL
, &shallows
);
512 if (!strncmp("deepen ", line
, 7)) {
514 depth
= strtol(line
+ 7, &end
, 0);
515 if (end
== line
+ 7 || depth
<= 0)
516 die("Invalid deepen: %s", line
);
519 if (strncmp("want ", line
, 5) ||
520 get_sha1_hex(line
+5, sha1_buf
))
521 die("git-upload-pack: protocol error, "
522 "expected to get sha, not '%s'", line
);
523 if (strstr(line
+45, "multi_ack"))
525 if (strstr(line
+45, "thin-pack"))
527 if (strstr(line
+45, "ofs-delta"))
529 if (strstr(line
+45, "side-band-64k"))
530 use_sideband
= LARGE_PACKET_MAX
;
531 else if (strstr(line
+45, "side-band"))
532 use_sideband
= DEFAULT_PACKET_MAX
;
534 /* We have sent all our refs already, and the other end
535 * should have chosen out of them; otherwise they are
536 * asking for nonsense.
538 * Hmph. We may later want to allow "want" line that
539 * asks for something like "master~10" (symbolic)...
540 * would it make sense? I don't know.
542 o
= lookup_object(sha1_buf
);
543 if (!o
|| !(o
->flags
& OUR_REF
))
544 die("git-upload-pack: not our ref %s", line
+5);
545 if (!(o
->flags
& WANTED
)) {
547 add_object_array(o
, NULL
, &want_obj
);
551 struct commit_list
*result
, *backup
;
553 die("Deepening a shallow repository not yet supported");
554 backup
= result
= get_shallow_commits(&want_obj
, depth
);
556 packet_write(1, "shallow %s",
557 sha1_to_hex(result
->item
->object
.sha1
));
558 result
= result
->next
;
560 free_commit_list(backup
);
562 if (shallows
.nr
> 0) {
564 for (i
= 0; i
< shallows
.nr
; i
++)
565 register_shallow(shallows
.objects
[i
].item
->sha1
);
569 static int send_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
571 static const char *capabilities
= "multi_ack thin-pack side-band"
572 " side-band-64k ofs-delta shallow";
573 struct object
*o
= parse_object(sha1
);
576 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1
));
579 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1
), refname
,
582 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), refname
);
584 if (!(o
->flags
& OUR_REF
)) {
588 if (o
->type
== OBJ_TAG
) {
589 o
= deref_tag(o
, refname
, 0);
590 packet_write(1, "%s %s^{}\n", sha1_to_hex(o
->sha1
), refname
);
595 static void upload_pack(void)
598 head_ref(send_ref
, NULL
);
599 for_each_ref(send_ref
, NULL
);
603 get_common_commits();
608 int main(int argc
, char **argv
)
614 for (i
= 1; i
< argc
; i
++) {
619 if (!strcmp(arg
, "--strict")) {
623 if (!strncmp(arg
, "--timeout=", 10)) {
624 timeout
= atoi(arg
+10);
627 if (!strcmp(arg
, "--")) {
634 usage(upload_pack_usage
);
637 if (!enter_repo(dir
, strict
))
638 die("'%s': unable to chdir or not a git archive", dir
);