]>
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 #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 and pack-objects to us. */
40 int lp_pipe
[2], pu_pipe
[2];
41 pid_t pid_rev_list
, pid_pack_objects
;
42 int create_full_pack
= (nr_our_refs
== nr_needs
&& !nr_has
);
46 if (pipe(lp_pipe
) < 0)
47 die("git-upload-pack: unable to create pipe");
48 pid_rev_list
= fork();
50 die("git-upload-pack: unable to fork git-rev-list");
59 if (create_full_pack
) {
61 use_thin_pack
= 0; /* no point doing it */
64 args
= nr_has
+ nr_needs
+ 5;
65 p
= xmalloc(args
* sizeof(char *));
66 argv
= (const char **) p
;
67 buf
= xmalloc(args
* 45);
74 *p
++ = use_thin_pack
? "--objects-edge" : "--objects";
75 if (create_full_pack
|| MAX_NEEDS
<= nr_needs
)
78 for (i
= 0; i
< nr_needs
; i
++) {
80 memcpy(buf
, sha1_to_hex(needs_sha1
[i
]), 41);
84 if (!create_full_pack
)
85 for (i
= 0; i
< nr_has
; i
++) {
88 memcpy(buf
, sha1_to_hex(has_sha1
[i
]), 41);
93 die("git-upload-pack: unable to exec git-rev-list");
96 if (pipe(pu_pipe
) < 0)
97 die("git-upload-pack: unable to create pipe");
98 pid_pack_objects
= fork();
99 if (pid_pack_objects
< 0) {
100 /* daemon sets things up to ignore TERM */
101 kill(pid_rev_list
, SIGKILL
);
102 die("git-upload-pack: unable to fork git-pack-objects");
104 if (!pid_pack_objects
) {
112 execl_git_cmd("pack-objects", "--stdout", NULL
);
113 kill(pid_rev_list
, SIGKILL
);
114 die("git-upload-pack: unable to exec git-pack-objects");
120 /* We read from pu_pipe[0] to capture the pack data.
126 struct pollfd pfd
[2];
135 if (0 <= pu_pipe
[0]) {
136 pfd
[pollsize
].fd
= pu_pipe
[0];
137 pfd
[pollsize
].events
= POLLIN
;
143 if (poll(pfd
, pollsize
, -1) < 0) {
144 if (errno
!= EINTR
) {
145 error("poll failed, resuming: %s",
151 if (0 <= pu
&& (pfd
[pu
].revents
& (POLLIN
|POLLHUP
))) {
152 /* Data ready; we keep the last byte
153 * to ourselves in case we detect
154 * broken rev-list, so that we can
155 * leave the stream corrupted. This
156 * is unfortunate -- unpack-objects
157 * would happily accept a valid pack
158 * data with trailing garbage, so
159 * appending garbage after we pass all
160 * the pack data is not good enough to
161 * signal breakage to downstream.
169 sz
= read(pu_pipe
[0], cp
,
170 sizeof(data
) - outsz
);
181 buffered
= data
[sz
-1] & 0xFF;
186 sz
= xwrite(1, data
, sz
);
192 /* See if the children are still there */
193 if (pid_rev_list
|| pid_pack_objects
) {
194 pid
= waitpid(-1, &status
, WNOHANG
);
197 who
= ((pid
== pid_rev_list
) ? "git-rev-list" :
198 (pid
== pid_pack_objects
) ? "git-pack-objects" :
202 error("git-upload-pack: %s",
206 error("git-upload-pack: we weren't "
207 "waiting for %d", pid
);
210 if (!WIFEXITED(status
) || WEXITSTATUS(status
) > 0) {
211 error("git-upload-pack: %s died with error.",
215 if (pid
== pid_rev_list
)
217 if (pid
== pid_pack_objects
)
218 pid_pack_objects
= 0;
219 if (pid_rev_list
|| pid_pack_objects
)
223 /* both died happily */
230 sz
= xwrite(1, data
, 1);
233 fprintf(stderr
, "flushed.\n");
238 if (pid_pack_objects
)
239 kill(pid_pack_objects
, SIGKILL
);
241 kill(pid_rev_list
, SIGKILL
);
242 die("git-upload-pack: aborting due to possible repository corruption on the remote side.");
245 static int got_sha1(char *hex
, unsigned char *sha1
)
247 if (get_sha1_hex(hex
, sha1
))
248 die("git-upload-pack: expected SHA1 object, got '%s'", hex
);
249 if (!has_sha1_file(sha1
))
251 if (nr_has
< MAX_HAS
) {
252 struct object
*o
= lookup_object(sha1
);
253 if (!(o
&& o
->parsed
))
254 o
= parse_object(sha1
);
256 die("oops (%s)", sha1_to_hex(sha1
));
257 if (o
->type
== TYPE_COMMIT
) {
258 struct commit_list
*parents
;
259 if (o
->flags
& THEY_HAVE
)
261 o
->flags
|= THEY_HAVE
;
262 for (parents
= ((struct commit
*)o
)->parents
;
264 parents
= parents
->next
)
265 parents
->item
->object
.flags
|= THEY_HAVE
;
267 memcpy(has_sha1
[nr_has
++], sha1
, 20);
272 static int get_common_commits(void)
274 static char line
[1000];
275 unsigned char sha1
[20], last_sha1
[20];
278 track_object_refs
= 0;
279 save_commit_buffer
= 0;
282 len
= packet_read_line(0, line
, sizeof(line
));
286 if (nr_has
== 0 || multi_ack
)
287 packet_write(1, "NAK\n");
290 len
= strip(line
, len
);
291 if (!strncmp(line
, "have ", 5)) {
292 if (got_sha1(line
+5, sha1
) &&
293 (multi_ack
|| nr_has
== 1)) {
294 if (nr_has
>= MAX_HAS
)
296 packet_write(1, "ACK %s%s\n",
298 multi_ack
? " continue" : "");
300 memcpy(last_sha1
, sha1
, 20);
304 if (!strcmp(line
, "done")) {
307 packet_write(1, "ACK %s\n",
308 sha1_to_hex(last_sha1
));
311 packet_write(1, "NAK\n");
314 die("git-upload-pack: expected SHA1 list, got '%s'", line
);
318 static int receive_needs(void)
320 static char line
[1000];
326 unsigned char dummy
[20], *sha1_buf
;
327 len
= packet_read_line(0, line
, sizeof(line
));
333 if (needs
== MAX_NEEDS
) {
335 "warning: supporting only a max of %d requests. "
336 "sending everything instead.\n",
339 else if (needs
< MAX_NEEDS
)
340 sha1_buf
= needs_sha1
[needs
];
342 if (strncmp("want ", line
, 5) || get_sha1_hex(line
+5, sha1_buf
))
343 die("git-upload-pack: protocol error, "
344 "expected to get sha, not '%s'", line
);
345 if (strstr(line
+45, "multi_ack"))
347 if (strstr(line
+45, "thin-pack"))
350 /* We have sent all our refs already, and the other end
351 * should have chosen out of them; otherwise they are
352 * asking for nonsense.
354 * Hmph. We may later want to allow "want" line that
355 * asks for something like "master~10" (symbolic)...
356 * would it make sense? I don't know.
358 o
= lookup_object(sha1_buf
);
359 if (!o
|| !(o
->flags
& OUR_REF
))
360 die("git-upload-pack: not our ref %s", line
+5);
361 if (!(o
->flags
& WANTED
)) {
368 static int send_ref(const char *refname
, const unsigned char *sha1
)
370 static char *capabilities
= "multi_ack thin-pack";
371 struct object
*o
= parse_object(sha1
);
374 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1
));
377 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1
), refname
,
380 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), refname
);
382 if (!(o
->flags
& OUR_REF
)) {
386 if (o
->type
== TYPE_TAG
) {
387 o
= deref_tag(o
, refname
, 0);
388 packet_write(1, "%s %s^{}\n", sha1_to_hex(o
->sha1
), refname
);
393 static int upload_pack(void)
397 for_each_ref(send_ref
);
399 nr_needs
= receive_needs();
402 get_common_commits();
407 int main(int argc
, char **argv
)
413 for (i
= 1; i
< argc
; i
++) {
418 if (!strcmp(arg
, "--strict")) {
422 if (!strncmp(arg
, "--timeout=", 10)) {
423 timeout
= atoi(arg
+10);
426 if (!strcmp(arg
, "--")) {
433 usage(upload_pack_usage
);
436 if (!enter_repo(dir
, strict
))
437 die("'%s': unable to chdir or not a git archive", dir
);