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)
19 static int nr_has
= 0, nr_needs
= 0, multi_ack
= 0, nr_our_refs
= 0;
20 static int use_thin_pack
= 0;
21 static unsigned char has_sha1
[MAX_HAS
][20];
22 static unsigned char needs_sha1
[MAX_NEEDS
][20];
23 static unsigned int timeout
= 0;
25 static void reset_timeout(void)
30 static int strip(char *line
, int len
)
32 if (len
&& line
[len
-1] == '\n')
37 static void create_pack_file(void)
39 /* Pipes between rev-list to pack-objects, pack-objects to us
40 * and pack-objects error stream for progress bar.
42 int lp_pipe
[2], pu_pipe
[2], pe_pipe
[2];
43 pid_t pid_rev_list
, pid_pack_objects
;
44 int create_full_pack
= (nr_our_refs
== nr_needs
&& !nr_has
);
45 char data
[8193], progress
[128];
48 if (pipe(lp_pipe
) < 0)
49 die("git-upload-pack: unable to create pipe");
50 pid_rev_list
= fork();
52 die("git-upload-pack: unable to fork git-rev-list");
61 if (create_full_pack
) {
63 use_thin_pack
= 0; /* no point doing it */
66 args
= nr_has
+ nr_needs
+ 5;
67 p
= xmalloc(args
* sizeof(char *));
68 argv
= (const char **) p
;
69 buf
= xmalloc(args
* 45);
76 *p
++ = use_thin_pack
? "--objects-edge" : "--objects";
77 if (create_full_pack
|| MAX_NEEDS
<= nr_needs
)
80 for (i
= 0; i
< nr_needs
; i
++) {
82 memcpy(buf
, sha1_to_hex(needs_sha1
[i
]), 41);
86 if (!create_full_pack
)
87 for (i
= 0; i
< nr_has
; i
++) {
90 memcpy(buf
, sha1_to_hex(has_sha1
[i
]), 41);
95 die("git-upload-pack: unable to exec git-rev-list");
98 if (pipe(pu_pipe
) < 0)
99 die("git-upload-pack: unable to create pipe");
100 if (pipe(pe_pipe
) < 0)
101 die("git-upload-pack: unable to create pipe");
102 pid_pack_objects
= fork();
103 if (pid_pack_objects
< 0) {
104 /* daemon sets things up to ignore TERM */
105 kill(pid_rev_list
, SIGKILL
);
106 die("git-upload-pack: unable to fork git-pack-objects");
108 if (!pid_pack_objects
) {
119 execl_git_cmd("pack-objects", "--stdout", "--progress", NULL
);
120 kill(pid_rev_list
, SIGKILL
);
121 die("git-upload-pack: unable to exec git-pack-objects");
127 /* We read from pe_pipe[0] to capture stderr output for
128 * progress bar, and pu_pipe[0] to capture the pack data.
136 struct pollfd pfd
[2];
140 int pe
, pu
, pollsize
;
145 if (0 <= pu_pipe
[0]) {
146 pfd
[pollsize
].fd
= pu_pipe
[0];
147 pfd
[pollsize
].events
= POLLIN
;
151 if (0 <= pe_pipe
[0]) {
152 pfd
[pollsize
].fd
= pe_pipe
[0];
153 pfd
[pollsize
].events
= POLLIN
;
159 if (poll(pfd
, pollsize
, -1) < 0) {
160 if (errno
!= EINTR
) {
161 error("poll failed, resuming: %s",
167 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
168 /* Data ready; we keep the last byte
169 * to ourselves in case we detect
170 * broken rev-list, so that we can
171 * leave the stream corrupted. This
172 * is unfortunate -- unpack-objects
173 * would happily accept a valid pack
174 * data with trailing garbage, so
175 * appending garbage after we pass all
176 * the pack data is not good enough to
177 * signal breakage to downstream.
185 sz
= read(pu_pipe
[0], cp
,
186 sizeof(data
) - outsz
);
197 buffered
= data
[sz
-1] & 0xFF;
202 sz
= xwrite(1, data
, sz
);
206 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
207 /* Status ready; we do not use it for now,
208 * but later we will add side-band to send it
211 sz
= read(pe_pipe
[0], progress
,
214 write(2, progress
, sz
);
224 /* See if the children are still there */
225 if (pid_rev_list
|| pid_pack_objects
) {
226 pid
= waitpid(-1, &status
, WNOHANG
);
229 who
= ((pid
== pid_rev_list
) ? "git-rev-list" :
230 (pid
== pid_pack_objects
) ? "git-pack-objects" :
234 error("git-upload-pack: %s",
238 error("git-upload-pack: we weren't "
239 "waiting for %d", pid
);
242 if (!WIFEXITED(status
) || WEXITSTATUS(status
) > 0) {
243 error("git-upload-pack: %s died with error.",
247 if (pid
== pid_rev_list
)
249 if (pid
== pid_pack_objects
)
250 pid_pack_objects
= 0;
251 if (pid_rev_list
|| pid_pack_objects
)
255 /* both died happily */
262 sz
= xwrite(1, data
, 1);
265 fprintf(stderr
, "flushed.\n");
270 if (pid_pack_objects
)
271 kill(pid_pack_objects
, SIGKILL
);
273 kill(pid_rev_list
, SIGKILL
);
274 die("git-upload-pack: aborting due to possible repository corruption on the remote side.");
277 static int got_sha1(char *hex
, unsigned char *sha1
)
279 if (get_sha1_hex(hex
, sha1
))
280 die("git-upload-pack: expected SHA1 object, got '%s'", hex
);
281 if (!has_sha1_file(sha1
))
283 if (nr_has
< MAX_HAS
) {
284 struct object
*o
= lookup_object(sha1
);
285 if (!(o
&& o
->parsed
))
286 o
= parse_object(sha1
);
288 die("oops (%s)", sha1_to_hex(sha1
));
289 if (o
->type
== TYPE_COMMIT
) {
290 struct commit_list
*parents
;
291 if (o
->flags
& THEY_HAVE
)
293 o
->flags
|= THEY_HAVE
;
294 for (parents
= ((struct commit
*)o
)->parents
;
296 parents
= parents
->next
)
297 parents
->item
->object
.flags
|= THEY_HAVE
;
299 memcpy(has_sha1
[nr_has
++], sha1
, 20);
304 static int get_common_commits(void)
306 static char line
[1000];
307 unsigned char sha1
[20], last_sha1
[20];
310 track_object_refs
= 0;
311 save_commit_buffer
= 0;
314 len
= packet_read_line(0, line
, sizeof(line
));
318 if (nr_has
== 0 || multi_ack
)
319 packet_write(1, "NAK\n");
322 len
= strip(line
, len
);
323 if (!strncmp(line
, "have ", 5)) {
324 if (got_sha1(line
+5, sha1
) &&
325 (multi_ack
|| nr_has
== 1)) {
326 if (nr_has
>= MAX_HAS
)
328 packet_write(1, "ACK %s%s\n",
330 multi_ack
? " continue" : "");
332 memcpy(last_sha1
, sha1
, 20);
336 if (!strcmp(line
, "done")) {
339 packet_write(1, "ACK %s\n",
340 sha1_to_hex(last_sha1
));
343 packet_write(1, "NAK\n");
346 die("git-upload-pack: expected SHA1 list, got '%s'", line
);
350 static int receive_needs(void)
352 static char line
[1000];
358 unsigned char dummy
[20], *sha1_buf
;
359 len
= packet_read_line(0, line
, sizeof(line
));
365 if (needs
== MAX_NEEDS
) {
367 "warning: supporting only a max of %d requests. "
368 "sending everything instead.\n",
371 else if (needs
< MAX_NEEDS
)
372 sha1_buf
= needs_sha1
[needs
];
374 if (strncmp("want ", line
, 5) || get_sha1_hex(line
+5, sha1_buf
))
375 die("git-upload-pack: protocol error, "
376 "expected to get sha, not '%s'", line
);
377 if (strstr(line
+45, "multi_ack"))
379 if (strstr(line
+45, "thin-pack"))
382 /* We have sent all our refs already, and the other end
383 * should have chosen out of them; otherwise they are
384 * asking for nonsense.
386 * Hmph. We may later want to allow "want" line that
387 * asks for something like "master~10" (symbolic)...
388 * would it make sense? I don't know.
390 o
= lookup_object(sha1_buf
);
391 if (!o
|| !(o
->flags
& OUR_REF
))
392 die("git-upload-pack: not our ref %s", line
+5);
393 if (!(o
->flags
& WANTED
)) {
400 static int send_ref(const char *refname
, const unsigned char *sha1
)
402 static char *capabilities
= "multi_ack thin-pack";
403 struct object
*o
= parse_object(sha1
);
406 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1
));
409 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1
), refname
,
412 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), refname
);
414 if (!(o
->flags
& OUR_REF
)) {
418 if (o
->type
== TYPE_TAG
) {
419 o
= deref_tag(o
, refname
, 0);
420 packet_write(1, "%s %s^{}\n", sha1_to_hex(o
->sha1
), refname
);
425 static int upload_pack(void)
429 for_each_ref(send_ref
);
431 nr_needs
= receive_needs();
434 get_common_commits();
439 int main(int argc
, char **argv
)
445 for (i
= 1; i
< argc
; i
++) {
450 if (!strcmp(arg
, "--strict")) {
454 if (!strncmp(arg
, "--timeout=", 10)) {
455 timeout
= atoi(arg
+10);
458 if (!strcmp(arg
, "--")) {
465 usage(upload_pack_usage
);
468 if (!enter_repo(dir
, strict
))
469 die("'%s': unable to chdir or not a git archive", dir
);