]>
git.ipfire.org Git - thirdparty/git.git/blob - upload-pack.c
12 static const char upload_pack_usage
[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
14 /* bits #0..7 in revision.h, #8..10 in commit.c */
15 #define THEY_HAVE (1u << 11)
16 #define OUR_REF (1u << 12)
17 #define WANTED (1u << 13)
18 #define COMMON_KNOWN (1u << 14)
19 #define REACHABLE (1u << 15)
21 static unsigned long oldest_have
= 0;
23 static int multi_ack
= 0, nr_our_refs
= 0;
24 static int use_thin_pack
= 0;
25 static struct object_array have_obj
;
26 static struct object_array want_obj
;
27 static unsigned int timeout
= 0;
28 static int use_sideband
= 0;
30 static void reset_timeout(void)
35 static int strip(char *line
, int len
)
37 if (len
&& line
[len
-1] == '\n')
42 #define PACKET_MAX 1000
43 static ssize_t
send_client_data(int fd
, const char *data
, ssize_t sz
)
62 return safe_write(fd
, data
, sz
);
71 if (PACKET_MAX
- 5 < n
)
73 sprintf(hdr
, "%04x", n
+ 5);
75 safe_write(1, hdr
, 5);
83 static void create_pack_file(void)
85 /* Pipes between rev-list to pack-objects, pack-objects to us
86 * and pack-objects error stream for progress bar.
88 int lp_pipe
[2], pu_pipe
[2], pe_pipe
[2];
89 pid_t pid_rev_list
, pid_pack_objects
;
90 int create_full_pack
= (nr_our_refs
== want_obj
.nr
&& !have_obj
.nr
);
91 char data
[8193], progress
[128];
92 char abort_msg
[] = "aborting due to possible repository "
93 "corruption on the remote side.";
96 if (pipe(lp_pipe
) < 0)
97 die("git-upload-pack: unable to create pipe");
98 pid_rev_list
= fork();
100 die("git-upload-pack: unable to fork git-rev-list");
109 if (create_full_pack
) {
111 use_thin_pack
= 0; /* no point doing it */
114 args
= have_obj
.nr
+ want_obj
.nr
+ 5;
115 p
= xmalloc(args
* sizeof(char *));
116 argv
= (const char **) p
;
117 buf
= xmalloc(args
* 45);
124 *p
++ = use_thin_pack
? "--objects-edge" : "--objects";
125 if (create_full_pack
)
128 for (i
= 0; i
< want_obj
.nr
; i
++) {
129 struct object
*o
= want_obj
.objects
[i
].item
;
131 memcpy(buf
, sha1_to_hex(o
->sha1
), 41);
135 if (!create_full_pack
)
136 for (i
= 0; i
< have_obj
.nr
; i
++) {
137 struct object
*o
= have_obj
.objects
[i
].item
;
140 memcpy(buf
, sha1_to_hex(o
->sha1
), 41);
145 die("git-upload-pack: unable to exec git-rev-list");
148 if (pipe(pu_pipe
) < 0)
149 die("git-upload-pack: unable to create pipe");
150 if (pipe(pe_pipe
) < 0)
151 die("git-upload-pack: unable to create pipe");
152 pid_pack_objects
= fork();
153 if (pid_pack_objects
< 0) {
154 /* daemon sets things up to ignore TERM */
155 kill(pid_rev_list
, SIGKILL
);
156 die("git-upload-pack: unable to fork git-pack-objects");
158 if (!pid_pack_objects
) {
169 execl_git_cmd("pack-objects", "--stdout", "--progress", NULL
);
170 kill(pid_rev_list
, SIGKILL
);
171 die("git-upload-pack: unable to exec git-pack-objects");
177 /* We read from pe_pipe[0] to capture stderr output for
178 * progress bar, and pu_pipe[0] to capture the pack data.
185 struct pollfd pfd
[2];
189 int pe
, pu
, pollsize
;
196 if (0 <= pu_pipe
[0]) {
197 pfd
[pollsize
].fd
= pu_pipe
[0];
198 pfd
[pollsize
].events
= POLLIN
;
202 if (0 <= pe_pipe
[0]) {
203 pfd
[pollsize
].fd
= pe_pipe
[0];
204 pfd
[pollsize
].events
= POLLIN
;
210 if (poll(pfd
, pollsize
, -1) < 0) {
211 if (errno
!= EINTR
) {
212 error("poll failed, resuming: %s",
218 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
219 /* Data ready; we keep the last byte
220 * to ourselves in case we detect
221 * broken rev-list, so that we can
222 * leave the stream corrupted. This
223 * is unfortunate -- unpack-objects
224 * would happily accept a valid pack
225 * data with trailing garbage, so
226 * appending garbage after we pass all
227 * the pack data is not good enough to
228 * signal breakage to downstream.
236 sz
= read(pu_pipe
[0], cp
,
237 sizeof(data
) - outsz
);
248 buffered
= data
[sz
-1] & 0xFF;
253 sz
= send_client_data(1, data
, sz
);
257 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
258 /* Status ready; we ship that in the side-band
259 * or dump to the standard error.
261 sz
= read(pe_pipe
[0], progress
,
264 send_client_data(2, progress
, sz
);
274 /* See if the children are still there */
275 if (pid_rev_list
|| pid_pack_objects
) {
276 pid
= waitpid(-1, &status
, WNOHANG
);
279 who
= ((pid
== pid_rev_list
) ? "git-rev-list" :
280 (pid
== pid_pack_objects
) ? "git-pack-objects" :
284 error("git-upload-pack: %s",
288 error("git-upload-pack: we weren't "
289 "waiting for %d", pid
);
292 if (!WIFEXITED(status
) || WEXITSTATUS(status
) > 0) {
293 error("git-upload-pack: %s died with error.",
297 if (pid
== pid_rev_list
)
299 if (pid
== pid_pack_objects
)
300 pid_pack_objects
= 0;
301 if (pid_rev_list
|| pid_pack_objects
)
305 /* both died happily */
312 sz
= send_client_data(1, data
, 1);
315 fprintf(stderr
, "flushed.\n");
317 send_client_data(1, NULL
, 0);
321 if (pid_pack_objects
)
322 kill(pid_pack_objects
, SIGKILL
);
324 kill(pid_rev_list
, SIGKILL
);
325 send_client_data(3, abort_msg
, sizeof(abort_msg
));
326 die("git-upload-pack: %s", abort_msg
);
329 static int got_sha1(char *hex
, unsigned char *sha1
)
332 int we_knew_they_have
= 0;
334 if (get_sha1_hex(hex
, sha1
))
335 die("git-upload-pack: expected SHA1 object, got '%s'", hex
);
336 if (!has_sha1_file(sha1
))
339 o
= lookup_object(sha1
);
340 if (!(o
&& o
->parsed
))
341 o
= parse_object(sha1
);
343 die("oops (%s)", sha1_to_hex(sha1
));
344 if (o
->type
== OBJ_COMMIT
) {
345 struct commit_list
*parents
;
346 struct commit
*commit
= (struct commit
*)o
;
347 if (o
->flags
& THEY_HAVE
)
348 we_knew_they_have
= 1;
350 o
->flags
|= THEY_HAVE
;
351 if (!oldest_have
|| (commit
->date
< oldest_have
))
352 oldest_have
= commit
->date
;
353 for (parents
= commit
->parents
;
355 parents
= parents
->next
)
356 parents
->item
->object
.flags
|= THEY_HAVE
;
358 if (!we_knew_they_have
) {
359 add_object_array(o
, NULL
, &have_obj
);
365 static int reachable(struct commit
*want
)
367 struct commit_list
*work
= NULL
;
369 insert_by_date(want
, &work
);
371 struct commit_list
*list
= work
->next
;
372 struct commit
*commit
= work
->item
;
376 if (commit
->object
.flags
& THEY_HAVE
) {
377 want
->object
.flags
|= COMMON_KNOWN
;
380 if (!commit
->object
.parsed
)
381 parse_object(commit
->object
.sha1
);
382 if (commit
->object
.flags
& REACHABLE
)
384 commit
->object
.flags
|= REACHABLE
;
385 if (commit
->date
< oldest_have
)
387 for (list
= commit
->parents
; list
; list
= list
->next
) {
388 struct commit
*parent
= list
->item
;
389 if (!(parent
->object
.flags
& REACHABLE
))
390 insert_by_date(parent
, &work
);
393 want
->object
.flags
|= REACHABLE
;
394 clear_commit_marks(want
, REACHABLE
);
395 free_commit_list(work
);
396 return (want
->object
.flags
& COMMON_KNOWN
);
399 static int ok_to_give_up(void)
406 for (i
= 0; i
< want_obj
.nr
; i
++) {
407 struct object
*want
= want_obj
.objects
[i
].item
;
409 if (want
->flags
& COMMON_KNOWN
)
411 want
= deref_tag(want
, "a want line", 0);
412 if (!want
|| want
->type
!= OBJ_COMMIT
) {
413 /* no way to tell if this is reachable by
414 * looking at the ancestry chain alone, so
415 * leave a note to ourselves not to worry about
416 * this object anymore.
418 want_obj
.objects
[i
].item
->flags
|= COMMON_KNOWN
;
421 if (!reachable((struct commit
*)want
))
427 static int get_common_commits(void)
429 static char line
[1000];
430 unsigned char sha1
[20];
431 char hex
[41], last_hex
[41];
434 track_object_refs
= 0;
435 save_commit_buffer
= 0;
438 len
= packet_read_line(0, line
, sizeof(line
));
442 if (have_obj
.nr
== 0 || multi_ack
)
443 packet_write(1, "NAK\n");
446 len
= strip(line
, len
);
447 if (!strncmp(line
, "have ", 5)) {
448 switch (got_sha1(line
+5, sha1
)) {
449 case -1: /* they have what we do not */
450 if (multi_ack
&& ok_to_give_up())
451 packet_write(1, "ACK %s continue\n",
455 memcpy(hex
, sha1_to_hex(sha1
), 41);
457 const char *msg
= "ACK %s continue\n";
458 packet_write(1, msg
, hex
);
459 memcpy(last_hex
, hex
, 41);
461 else if (have_obj
.nr
== 1)
462 packet_write(1, "ACK %s\n", hex
);
467 if (!strcmp(line
, "done")) {
468 if (have_obj
.nr
> 0) {
470 packet_write(1, "ACK %s\n", last_hex
);
473 packet_write(1, "NAK\n");
476 die("git-upload-pack: expected SHA1 list, got '%s'", line
);
480 static void receive_needs(void)
482 static char line
[1000];
487 unsigned char sha1_buf
[20];
488 len
= packet_read_line(0, line
, sizeof(line
));
493 if (strncmp("want ", line
, 5) ||
494 get_sha1_hex(line
+5, sha1_buf
))
495 die("git-upload-pack: protocol error, "
496 "expected to get sha, not '%s'", line
);
497 if (strstr(line
+45, "multi_ack"))
499 if (strstr(line
+45, "thin-pack"))
501 if (strstr(line
+45, "side-band"))
504 /* We have sent all our refs already, and the other end
505 * should have chosen out of them; otherwise they are
506 * asking for nonsense.
508 * Hmph. We may later want to allow "want" line that
509 * asks for something like "master~10" (symbolic)...
510 * would it make sense? I don't know.
512 o
= lookup_object(sha1_buf
);
513 if (!o
|| !(o
->flags
& OUR_REF
))
514 die("git-upload-pack: not our ref %s", line
+5);
515 if (!(o
->flags
& WANTED
)) {
517 add_object_array(o
, NULL
, &want_obj
);
522 static int send_ref(const char *refname
, const unsigned char *sha1
)
524 static const char *capabilities
= "multi_ack thin-pack side-band";
525 struct object
*o
= parse_object(sha1
);
528 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1
));
531 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1
), refname
,
534 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), refname
);
536 if (!(o
->flags
& OUR_REF
)) {
540 if (o
->type
== OBJ_TAG
) {
541 o
= deref_tag(o
, refname
, 0);
542 packet_write(1, "%s %s^{}\n", sha1_to_hex(o
->sha1
), refname
);
547 static int upload_pack(void)
551 for_each_ref(send_ref
);
556 get_common_commits();
561 int main(int argc
, char **argv
)
567 for (i
= 1; i
< argc
; i
++) {
572 if (!strcmp(arg
, "--strict")) {
576 if (!strncmp(arg
, "--timeout=", 10)) {
577 timeout
= atoi(arg
+10);
580 if (!strcmp(arg
, "--")) {
587 usage(upload_pack_usage
);
590 if (!enter_repo(dir
, strict
))
591 die("'%s': unable to chdir or not a git archive", dir
);