@matheusbsbs / matheus-bagliano / commits / da093d3750
Fork 0

improve fetch-pack's handling of kept packs

Since functions in fetch-clone.c were only used from fetch-pack.c, its content has been merged with fetch-pack.c. This allows for better coupling of features with much simpler implementations. One new thing is that the (abscence of) --thin also enforce it on index-pack now, such that index-pack will abort if a thin pack was _not_ asked for. The -k or --keep, when provided twice, now causes the fetched pack to be left as a kept pack just like receive-pack currently does. Eventually this will be used to close a race against concurrent repacking. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Nicolas Pitre committed Nov 1, 2006 at 17:06 UTC da093d37506f034bfb14e6de26cce9c14b1a1216
5 files changed +106 -100
Documentation/git-fetch-pack.txt
+2 -1
@@ -32,7 +32,8 @@ OPTIONS
32 -k::
33 Do not invoke 'git-unpack-objects' on received data, but
34 create a single packfile out of it instead, and store it
35 - in the object database.
35 + in the object database. If provided twice then the pack is
36 + locked against repacking.
37
38 --exec=<git-upload-pack>::
39 Use this to specify the path to 'git-upload-pack' on the
Makefile
+1 -1
@@ -260,7 +260,7 @@ LIB_OBJS = \
260 quote.o read-cache.o refs.o run-command.o dir.o object-refs.o \
261 server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
262 tag.o tree.o usage.o config.o environment.o ctype.o copy.o \
263 - fetch-clone.o revision.o pager.o tree-walk.o xdiff-interface.o \
263 + revision.o pager.o tree-walk.o xdiff-interface.o \
264 write_or_die.o trace.o list-objects.o grep.o \
265 alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \
266 color.o wt-status.o archive-zip.o archive-tar.o
cache.h
-4
@@ -416,10 +416,6 @@ extern int copy_fd(int ifd, int ofd);
416 extern void write_or_die(int fd, const void *buf, size_t count);
417 extern int write_or_whine(int fd, const void *buf, size_t count, const char *msg);
418
419 -/* Finish off pack transfer receiving end */
420 -extern int receive_unpack_pack(int fd[2], const char *me, int quiet, int);
421 -extern int receive_keep_pack(int fd[2], const char *me, int quiet, int);
422 -
419 /* pager.c */
420 extern void setup_pager(void);
421 extern int pager_in_use;
fetch-clone.c deleted
-87
@@ -1,87 +0,0 @@
1 -#include "cache.h"
2 -#include "exec_cmd.h"
3 -#include "pkt-line.h"
4 -#include "sideband.h"
5 -#include <sys/wait.h>
6 -
7 -static pid_t setup_sideband(int sideband, const char *me, int fd[2], int xd[2])
8 -{
9 - pid_t side_pid;
10 -
11 - if (!sideband) {
12 - fd[0] = xd[0];
13 - fd[1] = xd[1];
14 - return 0;
15 - }
16 - /* xd[] is talking with upload-pack; subprocess reads from
17 - * xd[0], spits out band#2 to stderr, and feeds us band#1
18 - * through our fd[0].
19 - */
20 - if (pipe(fd) < 0)
21 - die("%s: unable to set up pipe", me);
22 - side_pid = fork();
23 - if (side_pid < 0)
24 - die("%s: unable to fork off sideband demultiplexer", me);
25 - if (!side_pid) {
26 - /* subprocess */
27 - close(fd[0]);
28 - if (xd[0] != xd[1])
29 - close(xd[1]);
30 - if (recv_sideband(me, xd[0], fd[1], 2))
31 - exit(1);
32 - exit(0);
33 - }
34 - close(xd[0]);
35 - close(fd[1]);
36 - fd[1] = xd[1];
37 - return side_pid;
38 -}
39 -
40 -static int get_pack(int xd[2], const char *me, int sideband, const char **argv)
41 -{
42 - int status;
43 - pid_t pid, side_pid;
44 - int fd[2];
45 -
46 - side_pid = setup_sideband(sideband, me, fd, xd);
47 - pid = fork();
48 - if (pid < 0)
49 - die("%s: unable to fork off %s", me, argv[0]);
50 - if (!pid) {
51 - dup2(fd[0], 0);
52 - close(fd[0]);
53 - close(fd[1]);
54 - execv_git_cmd(argv);
55 - die("%s exec failed", argv[0]);
56 - }
57 - close(fd[0]);
58 - close(fd[1]);
59 - while (waitpid(pid, &status, 0) < 0) {
60 - if (errno != EINTR)
61 - die("waiting for %s: %s", argv[0], strerror(errno));
62 - }
63 - if (WIFEXITED(status)) {
64 - int code = WEXITSTATUS(status);
65 - if (code)
66 - die("%s died with error code %d", argv[0], code);
67 - return 0;
68 - }
69 - if (WIFSIGNALED(status)) {
70 - int sig = WTERMSIG(status);
71 - die("%s died of signal %d", argv[0], sig);
72 - }
73 - die("%s died of unnatural causes %d", argv[0], status);
74 -}
75 -
76 -int receive_unpack_pack(int xd[2], const char *me, int quiet, int sideband)
77 -{
78 - const char *argv[3] = { "unpack-objects", quiet ? "-q" : NULL, NULL };
79 - return get_pack(xd, me, sideband, argv);
80 -}
81 -
82 -int receive_keep_pack(int xd[2], const char *me, int quiet, int sideband)
83 -{
84 - const char *argv[5] = { "index-pack", "--stdin", "--fix-thin",
85 - quiet ? NULL : "-v", NULL };
86 - return get_pack(xd, me, sideband, argv);
87 -}
fetch-pack.c
+103 -7
@@ -3,6 +3,9 @@
3 #include "pkt-line.h"
4 #include "commit.h"
5 #include "tag.h"
6 +#include "exec_cmd.h"
7 +#include "sideband.h"
8 +#include <sys/wait.h>
9
10 static int keep_pack;
11 static int quiet;
@@ -416,6 +419,103 @@ static int everything_local(struct ref **refs, int nr_match, char **match)
419 return retval;
420 }
421
422 +static pid_t setup_sideband(int fd[2], int xd[2])
423 +{
424 + pid_t side_pid;
425 +
426 + if (!use_sideband) {
427 + fd[0] = xd[0];
428 + fd[1] = xd[1];
429 + return 0;
430 + }
431 + /* xd[] is talking with upload-pack; subprocess reads from
432 + * xd[0], spits out band#2 to stderr, and feeds us band#1
433 + * through our fd[0].
434 + */
435 + if (pipe(fd) < 0)
436 + die("fetch-pack: unable to set up pipe");
437 + side_pid = fork();
438 + if (side_pid < 0)
439 + die("fetch-pack: unable to fork off sideband demultiplexer");
440 + if (!side_pid) {
441 + /* subprocess */
442 + close(fd[0]);
443 + if (xd[0] != xd[1])
444 + close(xd[1]);
445 + if (recv_sideband("fetch-pack", xd[0], fd[1], 2))
446 + exit(1);
447 + exit(0);
448 + }
449 + close(xd[0]);
450 + close(fd[1]);
451 + fd[1] = xd[1];
452 + return side_pid;
453 +}
454 +
455 +static int get_pack(int xd[2], const char **argv)
456 +{
457 + int status;
458 + pid_t pid, side_pid;
459 + int fd[2];
460 +
461 + side_pid = setup_sideband(fd, xd);
462 + pid = fork();
463 + if (pid < 0)
464 + die("fetch-pack: unable to fork off %s", argv[0]);
465 + if (!pid) {
466 + dup2(fd[0], 0);
467 + close(fd[0]);
468 + close(fd[1]);
469 + execv_git_cmd(argv);
470 + die("%s exec failed", argv[0]);
471 + }
472 + close(fd[0]);
473 + close(fd[1]);
474 + while (waitpid(pid, &status, 0) < 0) {
475 + if (errno != EINTR)
476 + die("waiting for %s: %s", argv[0], strerror(errno));
477 + }
478 + if (WIFEXITED(status)) {
479 + int code = WEXITSTATUS(status);
480 + if (code)
481 + die("%s died with error code %d", argv[0], code);
482 + return 0;
483 + }
484 + if (WIFSIGNALED(status)) {
485 + int sig = WTERMSIG(status);
486 + die("%s died of signal %d", argv[0], sig);
487 + }
488 + die("%s died of unnatural causes %d", argv[0], status);
489 +}
490 +
491 +static int explode_rx_pack(int xd[2])
492 +{
493 + const char *argv[3] = { "unpack-objects", quiet ? "-q" : NULL, NULL };
494 + return get_pack(xd, argv);
495 +}
496 +
497 +static int keep_rx_pack(int xd[2])
498 +{
499 + const char *argv[6];
500 + char keep_arg[256];
501 + int n = 0;
502 +
503 + argv[n++] = "index-pack";
504 + argv[n++] = "--stdin";
505 + if (!quiet)
506 + argv[n++] = "-v";
507 + if (use_thin_pack)
508 + argv[n++] = "--fix-thin";
509 + if (keep_pack > 1) {
510 + int s = sprintf(keep_arg, "--keep=fetch-pack %i on ", getpid());
511 + if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
512 + strcpy(keep_arg + s, "localhost");
513 + argv[n++] = keep_arg;
514 + }
515 + argv[n] = NULL;
516 + return get_pack(xd, argv);
517 +}
518 +
519 static int fetch_pack(int fd[2], int nr_match, char **match)
520 {
521 struct ref *ref;
@@ -447,17 +547,13 @@ static int fetch_pack(int fd[2], int nr_match, char **match)
547 goto all_done;
548 }
549 if (find_common(fd, sha1, ref) < 0)
450 - if (!keep_pack)
550 + if (keep_pack != 1)
551 /* When cloning, it is not unusual to have
552 * no common commit.
553 */
554 fprintf(stderr, "warning: no common commits\n");
555
456 - if (keep_pack)
457 - status = receive_keep_pack(fd, "git-fetch-pack", quiet, use_sideband);
458 - else
459 - status = receive_unpack_pack(fd, "git-fetch-pack", quiet, use_sideband);
460 -
556 + status = (keep_pack) ? keep_rx_pack(fd) : explode_rx_pack(fd);
557 if (status)
558 die("git-fetch-pack: fetch failed.");
559
@@ -494,7 +590,7 @@ int main(int argc, char **argv)
590 continue;
591 }
592 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
497 - keep_pack = 1;
593 + keep_pack++;
594 continue;
595 }
596 if (!strcmp("--thin", arg)) {