]>
git.ipfire.org Git - thirdparty/git.git/blob - upload-pack.c
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
;
20 static struct object_array have_obj
;
21 static struct object_array want_obj
;
22 static unsigned int timeout
;
23 static int use_sideband
;
25 static void reset_timeout(void)
30 static int strip(char *line
, int len
)
32 if (len
&& line
[len
-1] == '\n')
37 static ssize_t
send_client_data(int fd
, const char *data
, ssize_t sz
)
40 return send_sideband(1, fd
, data
, sz
, DEFAULT_PACKET_MAX
);
49 return safe_write(fd
, data
, sz
);
52 static void create_pack_file(void)
54 /* Pipes between rev-list to pack-objects, pack-objects to us
55 * and pack-objects error stream for progress bar.
57 int lp_pipe
[2], pu_pipe
[2], pe_pipe
[2];
58 pid_t pid_rev_list
, pid_pack_objects
;
59 int create_full_pack
= (nr_our_refs
== want_obj
.nr
&& !have_obj
.nr
);
60 char data
[8193], progress
[128];
61 char abort_msg
[] = "aborting due to possible repository "
62 "corruption on the remote side.";
65 if (pipe(lp_pipe
) < 0)
66 die("git-upload-pack: unable to create pipe");
67 pid_rev_list
= fork();
69 die("git-upload-pack: unable to fork git-rev-list");
78 if (create_full_pack
) {
80 use_thin_pack
= 0; /* no point doing it */
83 args
= have_obj
.nr
+ want_obj
.nr
+ 5;
84 p
= xmalloc(args
* sizeof(char *));
85 argv
= (const char **) p
;
86 buf
= xmalloc(args
* 45);
93 *p
++ = use_thin_pack
? "--objects-edge" : "--objects";
97 for (i
= 0; i
< want_obj
.nr
; i
++) {
98 struct object
*o
= want_obj
.objects
[i
].item
;
100 memcpy(buf
, sha1_to_hex(o
->sha1
), 41);
104 if (!create_full_pack
)
105 for (i
= 0; i
< have_obj
.nr
; i
++) {
106 struct object
*o
= have_obj
.objects
[i
].item
;
109 memcpy(buf
, sha1_to_hex(o
->sha1
), 41);
114 die("git-upload-pack: unable to exec git-rev-list");
117 if (pipe(pu_pipe
) < 0)
118 die("git-upload-pack: unable to create pipe");
119 if (pipe(pe_pipe
) < 0)
120 die("git-upload-pack: unable to create pipe");
121 pid_pack_objects
= fork();
122 if (pid_pack_objects
< 0) {
123 /* daemon sets things up to ignore TERM */
124 kill(pid_rev_list
, SIGKILL
);
125 die("git-upload-pack: unable to fork git-pack-objects");
127 if (!pid_pack_objects
) {
138 execl_git_cmd("pack-objects", "--stdout", "--progress", NULL
);
139 kill(pid_rev_list
, SIGKILL
);
140 die("git-upload-pack: unable to exec git-pack-objects");
146 /* We read from pe_pipe[0] to capture stderr output for
147 * progress bar, and pu_pipe[0] to capture the pack data.
154 struct pollfd pfd
[2];
158 int pe
, pu
, pollsize
;
165 if (0 <= pu_pipe
[0]) {
166 pfd
[pollsize
].fd
= pu_pipe
[0];
167 pfd
[pollsize
].events
= POLLIN
;
171 if (0 <= pe_pipe
[0]) {
172 pfd
[pollsize
].fd
= pe_pipe
[0];
173 pfd
[pollsize
].events
= POLLIN
;
179 if (poll(pfd
, pollsize
, -1) < 0) {
180 if (errno
!= EINTR
) {
181 error("poll failed, resuming: %s",
187 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
188 /* Data ready; we keep the last byte
189 * to ourselves in case we detect
190 * broken rev-list, so that we can
191 * leave the stream corrupted. This
192 * is unfortunate -- unpack-objects
193 * would happily accept a valid pack
194 * data with trailing garbage, so
195 * appending garbage after we pass all
196 * the pack data is not good enough to
197 * signal breakage to downstream.
205 sz
= read(pu_pipe
[0], cp
,
206 sizeof(data
) - outsz
);
217 buffered
= data
[sz
-1] & 0xFF;
222 sz
= send_client_data(1, data
, sz
);
226 if (0 <= pe
&& (pfd
[pe
].revents
& (POLLIN
|POLLHUP
))) {
227 /* Status ready; we ship that in the side-band
228 * or dump to the standard error.
230 sz
= read(pe_pipe
[0], progress
,
233 send_client_data(2, progress
, sz
);
243 /* See if the children are still there */
244 if (pid_rev_list
|| pid_pack_objects
) {
245 pid
= waitpid(-1, &status
, WNOHANG
);
248 who
= ((pid
== pid_rev_list
) ? "git-rev-list" :
249 (pid
== pid_pack_objects
) ? "git-pack-objects" :
253 error("git-upload-pack: %s",
257 error("git-upload-pack: we weren't "
258 "waiting for %d", pid
);
261 if (!WIFEXITED(status
) || WEXITSTATUS(status
) > 0) {
262 error("git-upload-pack: %s died with error.",
266 if (pid
== pid_rev_list
)
268 if (pid
== pid_pack_objects
)
269 pid_pack_objects
= 0;
270 if (pid_rev_list
|| pid_pack_objects
)
274 /* both died happily */
281 sz
= send_client_data(1, data
, 1);
284 fprintf(stderr
, "flushed.\n");
291 if (pid_pack_objects
)
292 kill(pid_pack_objects
, SIGKILL
);
294 kill(pid_rev_list
, SIGKILL
);
295 send_client_data(3, abort_msg
, sizeof(abort_msg
));
296 die("git-upload-pack: %s", abort_msg
);
299 static int got_sha1(char *hex
, unsigned char *sha1
)
303 if (get_sha1_hex(hex
, sha1
))
304 die("git-upload-pack: expected SHA1 object, got '%s'", hex
);
305 if (!has_sha1_file(sha1
))
308 o
= lookup_object(sha1
);
309 if (!(o
&& o
->parsed
))
310 o
= parse_object(sha1
);
312 die("oops (%s)", sha1_to_hex(sha1
));
313 if (o
->type
== OBJ_COMMIT
) {
314 struct commit_list
*parents
;
315 if (o
->flags
& THEY_HAVE
)
317 o
->flags
|= THEY_HAVE
;
318 for (parents
= ((struct commit
*)o
)->parents
;
320 parents
= parents
->next
)
321 parents
->item
->object
.flags
|= THEY_HAVE
;
323 add_object_array(o
, NULL
, &have_obj
);
327 static int get_common_commits(void)
329 static char line
[1000];
330 unsigned char sha1
[20], last_sha1
[20];
333 track_object_refs
= 0;
334 save_commit_buffer
= 0;
337 len
= packet_read_line(0, line
, sizeof(line
));
341 if (have_obj
.nr
== 0 || multi_ack
)
342 packet_write(1, "NAK\n");
345 len
= strip(line
, len
);
346 if (!strncmp(line
, "have ", 5)) {
347 if (got_sha1(line
+5, sha1
) &&
348 (multi_ack
|| have_obj
.nr
== 1)) {
349 packet_write(1, "ACK %s%s\n",
351 multi_ack
? " continue" : "");
353 hashcpy(last_sha1
, sha1
);
357 if (!strcmp(line
, "done")) {
358 if (have_obj
.nr
> 0) {
360 packet_write(1, "ACK %s\n",
361 sha1_to_hex(last_sha1
));
364 packet_write(1, "NAK\n");
367 die("git-upload-pack: expected SHA1 list, got '%s'", line
);
371 static void receive_needs(void)
373 static char line
[1000];
378 unsigned char sha1_buf
[20];
379 len
= packet_read_line(0, line
, sizeof(line
));
384 if (strncmp("want ", line
, 5) ||
385 get_sha1_hex(line
+5, sha1_buf
))
386 die("git-upload-pack: protocol error, "
387 "expected to get sha, not '%s'", line
);
388 if (strstr(line
+45, "multi_ack"))
390 if (strstr(line
+45, "thin-pack"))
392 if (strstr(line
+45, "side-band"))
395 /* We have sent all our refs already, and the other end
396 * should have chosen out of them; otherwise they are
397 * asking for nonsense.
399 * Hmph. We may later want to allow "want" line that
400 * asks for something like "master~10" (symbolic)...
401 * would it make sense? I don't know.
403 o
= lookup_object(sha1_buf
);
404 if (!o
|| !(o
->flags
& OUR_REF
))
405 die("git-upload-pack: not our ref %s", line
+5);
406 if (!(o
->flags
& WANTED
)) {
408 add_object_array(o
, NULL
, &want_obj
);
413 static int send_ref(const char *refname
, const unsigned char *sha1
)
415 static const char *capabilities
= "multi_ack thin-pack side-band";
416 struct object
*o
= parse_object(sha1
);
419 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1
));
422 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1
), refname
,
425 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), refname
);
427 if (!(o
->flags
& OUR_REF
)) {
431 if (o
->type
== OBJ_TAG
) {
432 o
= deref_tag(o
, refname
, 0);
433 packet_write(1, "%s %s^{}\n", sha1_to_hex(o
->sha1
), refname
);
438 static void upload_pack(void)
442 for_each_ref(send_ref
);
446 get_common_commits();
451 int main(int argc
, char **argv
)
457 for (i
= 1; i
< argc
; i
++) {
462 if (!strcmp(arg
, "--strict")) {
466 if (!strncmp(arg
, "--timeout=", 10)) {
467 timeout
= atoi(arg
+10);
470 if (!strcmp(arg
, "--")) {
477 usage(upload_pack_usage
);
480 if (!enter_repo(dir
, strict
))
481 die("'%s': unable to chdir or not a git archive", dir
);