13 static const char upload_pack_usage
[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
15 #define THEY_HAVE (1U << 0)
16 #define OUR_REF (1U << 1)
17 #define WANTED (1U << 2)
18 static int multi_ack
, nr_our_refs
;
19 static int use_thin_pack
, use_ofs_delta
;
20 static struct object_array have_obj
;
21 static struct object_array want_obj
;
22 static unsigned int timeout
;
24 * otherwise maximum packet size (up to 65520 bytes).
26 static int use_sideband
;
28 static void reset_timeout(void)
33 static int strip(char *line
, int len
)
35 if (len
&& line
[len
-1] == '\n')
40 static ssize_t
send_client_data(int fd
, const char *data
, ssize_t sz
)
43 return send_sideband(1, fd
, data
, sz
, use_sideband
);
51 return safe_write(fd
, data
, sz
);
54 static void create_pack_file(void)
56 /* Pipes between rev-list to pack-objects, pack-objects to us
57 * and pack-objects error stream for progress bar.
59 int lp_pipe
[2], pu_pipe
[2], pe_pipe
[2];
60 pid_t pid_rev_list
, pid_pack_objects
;
61 int create_full_pack
= (nr_our_refs
== want_obj
.nr
&& !have_obj
.nr
);
62 char data
[8193], progress
[128];
63 char abort_msg
[] = "aborting due to possible repository "
64 "corruption on the remote side.";
67 if (pipe(lp_pipe
) < 0)
68 die("git-upload-pack: unable to create pipe");
69 pid_rev_list
= fork();
71 die("git-upload-pack: unable to fork git-rev-list");
80 if (create_full_pack
) {
82 use_thin_pack
= 0; /* no point doing it */
85 args
= have_obj
.nr
+ want_obj
.nr
+ 5;
86 p
= xmalloc(args
* sizeof(char *));
87 argv
= (const char **) p
;
88 buf
= xmalloc(args
* 45);
95 *p
++ = use_thin_pack
? "--objects-edge" : "--objects";
99 for (i
= 0; i
< want_obj
.nr
; i
++) {
100 struct object
*o
= want_obj
.objects
[i
].item
;
102 memcpy(buf
, sha1_to_hex(o
->sha1
), 41);
106 if (!create_full_pack
)
107 for (i
= 0; i
< have_obj
.nr
; i
++) {
108 struct object
*o
= have_obj
.objects
[i
].item
;
111 memcpy(buf
, sha1_to_hex(o
->sha1
), 41);
116 die("git-upload-pack: unable to exec git-rev-list");
119 if (pipe(pu_pipe
) < 0)
120 die("git-upload-pack: unable to create pipe");
121 if (pipe(pe_pipe
) < 0)
122 die("git-upload-pack: unable to create pipe");
123 pid_pack_objects
= fork();
124 if (pid_pack_objects
< 0) {
125 /* daemon sets things up to ignore TERM */
126 kill(pid_rev_list
, SIGKILL
);
127 die("git-upload-pack: unable to fork git-pack-objects");
129 if (!pid_pack_objects
) {
140 execl_git_cmd("pack-objects", "--stdout", "--progress",
141 use_ofs_delta
? "--delta-base-offset" : NULL
,
143 kill(pid_rev_list
, SIGKILL
);
144 die("git-upload-pack: unable to exec git-pack-objects");
150 /* We read from pe_pipe[0] to capture stderr output for
151 * progress bar, and pu_pipe[0] to capture the pack data.
158 struct pollfd pfd
[2];
162 int pe
, pu
, pollsize
;
169 if (0 <= pu_pipe
[0]) {
170 pfd
[pollsize
].fd
= pu_pipe
[0];
171 pfd
[pollsize
].events
= POLLIN
;
175 if (0 <= pe_pipe
[0]) {
176 pfd
[pollsize
].fd
= pe_pipe
[0];
177 pfd
[pollsize
].events
= POLLIN
;
183 if (poll(pfd
, pollsize
, -1) < 0) {
184 if (errno
!= EINTR
) {
185 error("poll failed, resuming: %s",
191 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
192 /* Data ready; we keep the last byte
193 * to ourselves in case we detect
194 * broken rev-list, so that we can
195 * leave the stream corrupted. This
196 * is unfortunate -- unpack-objects
197 * would happily accept a valid pack
198 * data with trailing garbage, so
199 * appending garbage after we pass all
200 * the pack data is not good enough to
201 * signal breakage to downstream.
209 sz
= read(pu_pipe
[0], cp
,
210 sizeof(data
) - outsz
);
221 buffered
= data
[sz
-1] & 0xFF;
226 sz
= send_client_data(1, data
, sz
);
230 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
231 /* Status ready; we ship that in the side-band
232 * or dump to the standard error.
234 sz
= read(pe_pipe
[0], progress
,
237 send_client_data(2, progress
, sz
);
247 /* See if the children are still there */
248 if (pid_rev_list
|| pid_pack_objects
) {
249 pid
= waitpid(-1, &status
, WNOHANG
);
252 who
= ((pid
== pid_rev_list
) ? "git-rev-list" :
253 (pid
== pid_pack_objects
) ? "git-pack-objects" :
257 error("git-upload-pack: %s",
261 error("git-upload-pack: we weren't "
262 "waiting for %d", pid
);
265 if (!WIFEXITED(status
) || WEXITSTATUS(status
) > 0) {
266 error("git-upload-pack: %s died with error.",
270 if (pid
== pid_rev_list
)
272 if (pid
== pid_pack_objects
)
273 pid_pack_objects
= 0;
274 if (pid_rev_list
|| pid_pack_objects
)
278 /* both died happily */
285 sz
= send_client_data(1, data
, 1);
288 fprintf(stderr
, "flushed.\n");
295 if (pid_pack_objects
)
296 kill(pid_pack_objects
, SIGKILL
);
298 kill(pid_rev_list
, SIGKILL
);
299 send_client_data(3, abort_msg
, sizeof(abort_msg
));
300 die("git-upload-pack: %s", abort_msg
);
303 static int got_sha1(char *hex
, unsigned char *sha1
)
307 if (get_sha1_hex(hex
, sha1
))
308 die("git-upload-pack: expected SHA1 object, got '%s'", hex
);
309 if (!has_sha1_file(sha1
))
312 o
= lookup_object(sha1
);
313 if (!(o
&& o
->parsed
))
314 o
= parse_object(sha1
);
316 die("oops (%s)", sha1_to_hex(sha1
));
317 if (o
->type
== OBJ_COMMIT
) {
318 struct commit_list
*parents
;
319 if (o
->flags
& THEY_HAVE
)
321 o
->flags
|= THEY_HAVE
;
322 for (parents
= ((struct commit
*)o
)->parents
;
324 parents
= parents
->next
)
325 parents
->item
->object
.flags
|= THEY_HAVE
;
327 add_object_array(o
, NULL
, &have_obj
);
331 static int get_common_commits(void)
333 static char line
[1000];
334 unsigned char sha1
[20];
335 char hex
[41], last_hex
[41];
338 track_object_refs
= 0;
339 save_commit_buffer
= 0;
342 len
= packet_read_line(0, line
, sizeof(line
));
346 if (have_obj
.nr
== 0 || multi_ack
)
347 packet_write(1, "NAK\n");
350 len
= strip(line
, len
);
351 if (!strncmp(line
, "have ", 5)) {
352 if (got_sha1(line
+5, sha1
)) {
353 memcpy(hex
, sha1_to_hex(sha1
), 41);
355 const char *msg
= "ACK %s continue\n";
356 packet_write(1, msg
, hex
);
357 memcpy(last_hex
, hex
, 41);
359 else if (have_obj
.nr
== 1)
360 packet_write(1, "ACK %s\n", hex
);
364 if (!strcmp(line
, "done")) {
365 if (have_obj
.nr
> 0) {
367 packet_write(1, "ACK %s\n", last_hex
);
370 packet_write(1, "NAK\n");
373 die("git-upload-pack: expected SHA1 list, got '%s'", line
);
377 static void receive_needs(void)
379 static char line
[1000];
384 unsigned char sha1_buf
[20];
385 len
= packet_read_line(0, line
, sizeof(line
));
390 if (strncmp("want ", line
, 5) ||
391 get_sha1_hex(line
+5, sha1_buf
))
392 die("git-upload-pack: protocol error, "
393 "expected to get sha, not '%s'", line
);
394 if (strstr(line
+45, "multi_ack"))
396 if (strstr(line
+45, "thin-pack"))
398 if (strstr(line
+45, "ofs-delta"))
400 if (strstr(line
+45, "side-band-64k"))
401 use_sideband
= LARGE_PACKET_MAX
;
402 else if (strstr(line
+45, "side-band"))
403 use_sideband
= DEFAULT_PACKET_MAX
;
405 /* We have sent all our refs already, and the other end
406 * should have chosen out of them; otherwise they are
407 * asking for nonsense.
409 * Hmph. We may later want to allow "want" line that
410 * asks for something like "master~10" (symbolic)...
411 * would it make sense? I don't know.
413 o
= lookup_object(sha1_buf
);
414 if (!o
|| !(o
->flags
& OUR_REF
))
415 die("git-upload-pack: not our ref %s", line
+5);
416 if (!(o
->flags
& WANTED
)) {
418 add_object_array(o
, NULL
, &want_obj
);
423 static int send_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
425 static const char *capabilities
= "multi_ack thin-pack side-band side-band-64k ofs-delta";
426 struct object
*o
= parse_object(sha1
);
429 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1
));
432 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1
), refname
,
435 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), refname
);
437 if (!(o
->flags
& OUR_REF
)) {
441 if (o
->type
== OBJ_TAG
) {
442 o
= deref_tag(o
, refname
, 0);
443 packet_write(1, "%s %s^{}\n", sha1_to_hex(o
->sha1
), refname
);
448 static void upload_pack(void)
451 head_ref(send_ref
, NULL
);
452 for_each_ref(send_ref
, NULL
);
456 get_common_commits();
461 int main(int argc
, char **argv
)
467 for (i
= 1; i
< argc
; i
++) {
472 if (!strcmp(arg
, "--strict")) {
476 if (!strncmp(arg
, "--timeout=", 10)) {
477 timeout
= atoi(arg
+10);
480 if (!strcmp(arg
, "--")) {
487 usage(upload_pack_usage
);
490 if (!enter_repo(dir
, strict
))
491 die("'%s': unable to chdir or not a git archive", dir
);