12 static const char upload_pack_usage
[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
14 #define THEY_HAVE (1U << 0)
15 #define OUR_REF (1U << 1)
16 #define WANTED (1U << 2)
17 static int multi_ack
, nr_our_refs
;
18 static int use_thin_pack
;
19 static struct object_array have_obj
;
20 static struct object_array want_obj
;
21 static unsigned int timeout
;
22 static int use_sideband
;
24 static void reset_timeout(void)
29 static int strip(char *line
, int len
)
31 if (len
&& line
[len
-1] == '\n')
36 #define PACKET_MAX 1000
37 static ssize_t
send_client_data(int fd
, const char *data
, ssize_t sz
)
56 return safe_write(fd
, data
, sz
);
65 if (PACKET_MAX
- 5 < n
)
67 sprintf(hdr
, "%04x", n
+ 5);
69 safe_write(1, hdr
, 5);
77 static void create_pack_file(void)
79 /* Pipes between rev-list to pack-objects, pack-objects to us
80 * and pack-objects error stream for progress bar.
82 int lp_pipe
[2], pu_pipe
[2], pe_pipe
[2];
83 pid_t pid_rev_list
, pid_pack_objects
;
84 int create_full_pack
= (nr_our_refs
== want_obj
.nr
&& !have_obj
.nr
);
85 char data
[8193], progress
[128];
86 char abort_msg
[] = "aborting due to possible repository "
87 "corruption on the remote side.";
90 if (pipe(lp_pipe
) < 0)
91 die("git-upload-pack: unable to create pipe");
92 pid_rev_list
= fork();
94 die("git-upload-pack: unable to fork git-rev-list");
103 if (create_full_pack
) {
105 use_thin_pack
= 0; /* no point doing it */
108 args
= have_obj
.nr
+ want_obj
.nr
+ 5;
109 p
= xmalloc(args
* sizeof(char *));
110 argv
= (const char **) p
;
111 buf
= xmalloc(args
* 45);
118 *p
++ = use_thin_pack
? "--objects-edge" : "--objects";
119 if (create_full_pack
)
122 for (i
= 0; i
< want_obj
.nr
; i
++) {
123 struct object
*o
= want_obj
.objects
[i
].item
;
125 memcpy(buf
, sha1_to_hex(o
->sha1
), 41);
129 if (!create_full_pack
)
130 for (i
= 0; i
< have_obj
.nr
; i
++) {
131 struct object
*o
= have_obj
.objects
[i
].item
;
134 memcpy(buf
, sha1_to_hex(o
->sha1
), 41);
139 die("git-upload-pack: unable to exec git-rev-list");
142 if (pipe(pu_pipe
) < 0)
143 die("git-upload-pack: unable to create pipe");
144 if (pipe(pe_pipe
) < 0)
145 die("git-upload-pack: unable to create pipe");
146 pid_pack_objects
= fork();
147 if (pid_pack_objects
< 0) {
148 /* daemon sets things up to ignore TERM */
149 kill(pid_rev_list
, SIGKILL
);
150 die("git-upload-pack: unable to fork git-pack-objects");
152 if (!pid_pack_objects
) {
163 execl_git_cmd("pack-objects", "--stdout", "--progress", NULL
);
164 kill(pid_rev_list
, SIGKILL
);
165 die("git-upload-pack: unable to exec git-pack-objects");
171 /* We read from pe_pipe[0] to capture stderr output for
172 * progress bar, and pu_pipe[0] to capture the pack data.
179 struct pollfd pfd
[2];
183 int pe
, pu
, pollsize
;
190 if (0 <= pu_pipe
[0]) {
191 pfd
[pollsize
].fd
= pu_pipe
[0];
192 pfd
[pollsize
].events
= POLLIN
;
196 if (0 <= pe_pipe
[0]) {
197 pfd
[pollsize
].fd
= pe_pipe
[0];
198 pfd
[pollsize
].events
= POLLIN
;
204 if (poll(pfd
, pollsize
, -1) < 0) {
205 if (errno
!= EINTR
) {
206 error("poll failed, resuming: %s",
212 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
213 /* Data ready; we keep the last byte
214 * to ourselves in case we detect
215 * broken rev-list, so that we can
216 * leave the stream corrupted. This
217 * is unfortunate -- unpack-objects
218 * would happily accept a valid pack
219 * data with trailing garbage, so
220 * appending garbage after we pass all
221 * the pack data is not good enough to
222 * signal breakage to downstream.
230 sz
= read(pu_pipe
[0], cp
,
231 sizeof(data
) - outsz
);
242 buffered
= data
[sz
-1] & 0xFF;
247 sz
= send_client_data(1, data
, sz
);
251 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
252 /* Status ready; we ship that in the side-band
253 * or dump to the standard error.
255 sz
= read(pe_pipe
[0], progress
,
258 send_client_data(2, progress
, sz
);
268 /* See if the children are still there */
269 if (pid_rev_list
|| pid_pack_objects
) {
270 pid
= waitpid(-1, &status
, WNOHANG
);
273 who
= ((pid
== pid_rev_list
) ? "git-rev-list" :
274 (pid
== pid_pack_objects
) ? "git-pack-objects" :
278 error("git-upload-pack: %s",
282 error("git-upload-pack: we weren't "
283 "waiting for %d", pid
);
286 if (!WIFEXITED(status
) || WEXITSTATUS(status
) > 0) {
287 error("git-upload-pack: %s died with error.",
291 if (pid
== pid_rev_list
)
293 if (pid
== pid_pack_objects
)
294 pid_pack_objects
= 0;
295 if (pid_rev_list
|| pid_pack_objects
)
299 /* both died happily */
306 sz
= send_client_data(1, data
, 1);
309 fprintf(stderr
, "flushed.\n");
311 send_client_data(1, NULL
, 0);
315 if (pid_pack_objects
)
316 kill(pid_pack_objects
, SIGKILL
);
318 kill(pid_rev_list
, SIGKILL
);
319 send_client_data(3, abort_msg
, sizeof(abort_msg
));
320 die("git-upload-pack: %s", abort_msg
);
323 static int got_sha1(char *hex
, unsigned char *sha1
)
327 if (get_sha1_hex(hex
, sha1
))
328 die("git-upload-pack: expected SHA1 object, got '%s'", hex
);
329 if (!has_sha1_file(sha1
))
332 o
= lookup_object(sha1
);
333 if (!(o
&& o
->parsed
))
334 o
= parse_object(sha1
);
336 die("oops (%s)", sha1_to_hex(sha1
));
337 if (o
->type
== OBJ_COMMIT
) {
338 struct commit_list
*parents
;
339 if (o
->flags
& THEY_HAVE
)
341 o
->flags
|= THEY_HAVE
;
342 for (parents
= ((struct commit
*)o
)->parents
;
344 parents
= parents
->next
)
345 parents
->item
->object
.flags
|= THEY_HAVE
;
347 add_object_array(o
, NULL
, &have_obj
);
351 static int get_common_commits(void)
353 static char line
[1000];
354 unsigned char sha1
[20], last_sha1
[20];
357 track_object_refs
= 0;
358 save_commit_buffer
= 0;
361 len
= packet_read_line(0, line
, sizeof(line
));
365 if (have_obj
.nr
== 0 || multi_ack
)
366 packet_write(1, "NAK\n");
369 len
= strip(line
, len
);
370 if (!strncmp(line
, "have ", 5)) {
371 if (got_sha1(line
+5, sha1
) &&
372 (multi_ack
|| have_obj
.nr
== 1)) {
373 packet_write(1, "ACK %s%s\n",
375 multi_ack
? " continue" : "");
377 hashcpy(last_sha1
, sha1
);
381 if (!strcmp(line
, "done")) {
382 if (have_obj
.nr
> 0) {
384 packet_write(1, "ACK %s\n",
385 sha1_to_hex(last_sha1
));
388 packet_write(1, "NAK\n");
391 die("git-upload-pack: expected SHA1 list, got '%s'", line
);
395 static void receive_needs(void)
397 static char line
[1000];
402 unsigned char sha1_buf
[20];
403 len
= packet_read_line(0, line
, sizeof(line
));
408 if (strncmp("want ", line
, 5) ||
409 get_sha1_hex(line
+5, sha1_buf
))
410 die("git-upload-pack: protocol error, "
411 "expected to get sha, not '%s'", line
);
412 if (strstr(line
+45, "multi_ack"))
414 if (strstr(line
+45, "thin-pack"))
416 if (strstr(line
+45, "side-band"))
419 /* We have sent all our refs already, and the other end
420 * should have chosen out of them; otherwise they are
421 * asking for nonsense.
423 * Hmph. We may later want to allow "want" line that
424 * asks for something like "master~10" (symbolic)...
425 * would it make sense? I don't know.
427 o
= lookup_object(sha1_buf
);
428 if (!o
|| !(o
->flags
& OUR_REF
))
429 die("git-upload-pack: not our ref %s", line
+5);
430 if (!(o
->flags
& WANTED
)) {
432 add_object_array(o
, NULL
, &want_obj
);
437 static int send_ref(const char *refname
, const unsigned char *sha1
)
439 static const char *capabilities
= "multi_ack thin-pack side-band";
440 struct object
*o
= parse_object(sha1
);
443 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1
));
446 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1
), refname
,
449 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), refname
);
451 if (!(o
->flags
& OUR_REF
)) {
455 if (o
->type
== OBJ_TAG
) {
456 o
= deref_tag(o
, refname
, 0);
457 packet_write(1, "%s %s^{}\n", sha1_to_hex(o
->sha1
), refname
);
462 static void upload_pack(void)
466 for_each_ref(send_ref
);
470 get_common_commits();
475 int main(int argc
, char **argv
)
481 for (i
= 1; i
< argc
; i
++) {
486 if (!strcmp(arg
, "--strict")) {
490 if (!strncmp(arg
, "--timeout=", 10)) {
491 timeout
= atoi(arg
+10);
494 if (!strcmp(arg
, "--")) {
501 usage(upload_pack_usage
);
504 if (!enter_repo(dir
, strict
))
505 die("'%s': unable to chdir or not a git archive", dir
);