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

Always obtain fetch-pack arguments from struct fetch_pack_args

Copying the arguments from a fetch_pack_args into static globals within the builtin-fetch-pack module is error-prone and may lead rise to cases where arguments supplied via the struct from the new fetch_pack() API may not be honored by the implementation. Here we reorganize all of the static globals into a single static struct fetch_pack_args instance and use memcpy() to move the data from the caller supplied structure into the globals before we execute our pack fetching implementation. This strategy is more robust to additions and deletions of properties. As keep_pack is a single bit we have also introduced lock_pack to mean not only download and store the packfile via index-pack but also to lock it against repacking by creating a .keep file when the packfile itself is stored. The caller must remove the .keep file when it is safe to do so. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Shawn O. Pearce committed Sep 19, 2007 at 00:49 UTC fa74052922cf39e5a39ad7178d1b13c2da9b4519
3 files changed +55 -74
builtin-fetch-pack.c
+46 -65
@@ -8,15 +8,11 @@
8 #include "sideband.h"
9 #include "fetch-pack.h"
10
11 -static int keep_pack;
11 static int transfer_unpack_limit = -1;
12 static int fetch_unpack_limit = -1;
13 static int unpack_limit = 100;
15 -static int quiet;
16 -static int verbose;
17 -static int fetch_all;
18 -static int depth;
19 -static int no_progress;
14 +static struct fetch_pack_args args;
15 +
16 static const char fetch_pack_usage[] =
17 "git-fetch-pack [--all] [--quiet|-q] [--keep|-k] [--thin] [--upload-pack=<git-upload-pack>] [--depth=<n>] [--no-progress] [-v] [<host>:]<directory> [<refs>...]";
18 static const char *uploadpack = "git-upload-pack";
@@ -181,7 +177,7 @@ static int find_common(int fd[2], unsigned char *result_sha1,
177 (use_sideband == 2 ? " side-band-64k" : ""),
178 (use_sideband == 1 ? " side-band" : ""),
179 (use_thin_pack ? " thin-pack" : ""),
184 - (no_progress ? " no-progress" : ""),
180 + (args.no_progress ? " no-progress" : ""),
181 " ofs-delta");
182 else
183 packet_write(fd[1], "want %s\n", sha1_to_hex(remote));
@@ -189,13 +185,13 @@ static int find_common(int fd[2], unsigned char *result_sha1,
185 }
186 if (is_repository_shallow())
187 write_shallow_commits(fd[1], 1);
192 - if (depth > 0)
193 - packet_write(fd[1], "deepen %d", depth);
188 + if (args.depth > 0)
189 + packet_write(fd[1], "deepen %d", args.depth);
190 packet_flush(fd[1]);
191 if (!fetching)
192 return 1;
193
198 - if (depth > 0) {
194 + if (args.depth > 0) {
195 char line[1024];
196 unsigned char sha1[20];
197 int len;
@@ -226,7 +222,7 @@ static int find_common(int fd[2], unsigned char *result_sha1,
222 retval = -1;
223 while ((sha1 = get_rev())) {
224 packet_write(fd[1], "have %s\n", sha1_to_hex(sha1));
229 - if (verbose)
225 + if (args.verbose)
226 fprintf(stderr, "have %s\n", sha1_to_hex(sha1));
227 in_vain++;
228 if (!(31 & ++count)) {
@@ -244,7 +240,7 @@ static int find_common(int fd[2], unsigned char *result_sha1,
240
241 do {
242 ack = get_ack(fd[0], result_sha1);
247 - if (verbose && ack)
243 + if (args.verbose && ack)
244 fprintf(stderr, "got ack %d %s\n", ack,
245 sha1_to_hex(result_sha1));
246 if (ack == 1) {
@@ -263,7 +259,7 @@ static int find_common(int fd[2], unsigned char *result_sha1,
259 } while (ack);
260 flushes--;
261 if (got_continue && MAX_IN_VAIN < in_vain) {
266 - if (verbose)
262 + if (args.verbose)
263 fprintf(stderr, "giving up\n");
264 break; /* give up */
265 }
@@ -271,7 +267,7 @@ static int find_common(int fd[2], unsigned char *result_sha1,
267 }
268 done:
269 packet_write(fd[1], "done\n");
274 - if (verbose)
270 + if (args.verbose)
271 fprintf(stderr, "done\n");
272 if (retval != 0) {
273 multi_ack = 0;
@@ -280,7 +276,7 @@ done:
276 while (flushes || multi_ack) {
277 int ack = get_ack(fd[0], result_sha1);
278 if (ack) {
283 - if (verbose)
279 + if (args.verbose)
280 fprintf(stderr, "got ack (%d) %s\n", ack,
281 sha1_to_hex(result_sha1));
282 if (ack == 1)
@@ -317,7 +313,7 @@ static int mark_complete(const char *path, const unsigned char *sha1, int flag,
313 static void mark_recent_complete_commits(unsigned long cutoff)
314 {
315 while (complete && cutoff <= complete->item->date) {
320 - if (verbose)
316 + if (args.verbose)
317 fprintf(stderr, "Marking %s as complete\n",
318 sha1_to_hex(complete->item->object.sha1));
319 pop_most_recent_commit(&complete, COMPLETE);
@@ -332,7 +328,7 @@ static void filter_refs(struct ref **refs, int nr_match, char **match)
328 struct ref *ref, *next;
329 struct ref *fastarray[32];
330
335 - if (nr_match && !fetch_all) {
331 + if (nr_match && !args.fetch_all) {
332 if (ARRAY_SIZE(fastarray) < nr_match)
333 return_refs = xcalloc(nr_match, sizeof(struct ref *));
334 else {
@@ -348,8 +344,8 @@ static void filter_refs(struct ref **refs, int nr_match, char **match)
344 if (!memcmp(ref->name, "refs/", 5) &&
345 check_ref_format(ref->name + 5))
346 ; /* trash */
351 - else if (fetch_all &&
352 - (!depth || prefixcmp(ref->name, "refs/tags/") )) {
347 + else if (args.fetch_all &&
348 + (!args.depth || prefixcmp(ref->name, "refs/tags/") )) {
349 *newtail = ref;
350 ref->next = NULL;
351 newtail = &ref->next;
@@ -365,7 +361,7 @@ static void filter_refs(struct ref **refs, int nr_match, char **match)
361 free(ref);
362 }
363
368 - if (!fetch_all) {
364 + if (!args.fetch_all) {
365 int i;
366 for (i = 0; i < nr_match; i++) {
367 ref = return_refs[i];
@@ -408,7 +404,7 @@ static int everything_local(struct ref **refs, int nr_match, char **match)
404 }
405 }
406
411 - if (!depth) {
407 + if (!args.depth) {
408 for_each_ref(mark_complete, NULL);
409 if (cutoff)
410 mark_recent_complete_commits(cutoff);
@@ -442,7 +438,7 @@ static int everything_local(struct ref **refs, int nr_match, char **match)
438 o = lookup_object(remote);
439 if (!o || !(o->flags & COMPLETE)) {
440 retval = 0;
445 - if (!verbose)
441 + if (!args.verbose)
442 continue;
443 fprintf(stderr,
444 "want %s (%s)\n", sha1_to_hex(remote),
@@ -451,7 +447,7 @@ static int everything_local(struct ref **refs, int nr_match, char **match)
447 }
448
449 hashcpy(ref->new_sha1, local);
454 - if (!verbose)
450 + if (!args.verbose)
451 continue;
452 fprintf(stderr,
453 "already have %s (%s)\n", sha1_to_hex(remote),
@@ -502,14 +498,14 @@ static int get_pack(int xd[2], char **pack_lockfile)
498 char keep_arg[256];
499 char hdr_arg[256];
500 const char **av;
505 - int do_keep = keep_pack;
501 + int do_keep = args.keep_pack;
502 int keep_pipe[2];
503
504 side_pid = setup_sideband(fd, xd);
505
506 av = argv;
507 *hdr_arg = 0;
512 - if (unpack_limit) {
508 + if (!args.keep_pack && unpack_limit) {
509 struct pack_header header;
510
511 if (read_pack_header(fd[0], &header))
@@ -527,11 +523,11 @@ static int get_pack(int xd[2], char **pack_lockfile)
523 die("fetch-pack: pipe setup failure: %s", strerror(errno));
524 *av++ = "index-pack";
525 *av++ = "--stdin";
530 - if (!quiet && !no_progress)
526 + if (!args.quiet && !args.no_progress)
527 *av++ = "-v";
532 - if (use_thin_pack)
528 + if (args.use_thin_pack)
529 *av++ = "--fix-thin";
534 - if (keep_pack > 1 || unpack_limit) {
530 + if (args.lock_pack || unpack_limit) {
531 int s = sprintf(keep_arg,
532 "--keep=fetch-pack %d on ", getpid());
533 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
@@ -541,7 +537,7 @@ static int get_pack(int xd[2], char **pack_lockfile)
537 }
538 else {
539 *av++ = "unpack-objects";
544 - if (quiet)
540 + if (args.quiet)
541 *av++ = "-q";
542 }
543 if (*hdr_arg)
@@ -599,17 +595,17 @@ static struct ref *do_fetch_pack(int fd[2],
595 if (is_repository_shallow() && !server_supports("shallow"))
596 die("Server does not support shallow clients");
597 if (server_supports("multi_ack")) {
602 - if (verbose)
598 + if (args.verbose)
599 fprintf(stderr, "Server supports multi_ack\n");
600 multi_ack = 1;
601 }
602 if (server_supports("side-band-64k")) {
607 - if (verbose)
603 + if (args.verbose)
604 fprintf(stderr, "Server supports side-band-64k\n");
605 use_sideband = 2;
606 }
607 else if (server_supports("side-band")) {
612 - if (verbose)
608 + if (args.verbose)
609 fprintf(stderr, "Server supports side-band\n");
610 use_sideband = 1;
611 }
@@ -622,7 +618,7 @@ static struct ref *do_fetch_pack(int fd[2],
618 goto all_done;
619 }
620 if (find_common(fd, sha1, ref) < 0)
625 - if (keep_pack != 1)
621 + if (!args.keep_pack)
622 /* When cloning, it is not unusual to have
623 * no common commit.
624 */
@@ -674,22 +670,6 @@ static int fetch_pack_config(const char *var, const char *value)
670
671 static struct lock_file lock;
672
677 -void setup_fetch_pack(struct fetch_pack_args *args)
678 -{
679 - uploadpack = args->uploadpack;
680 - quiet = args->quiet;
681 - keep_pack = args->keep_pack;
682 - if (args->unpacklimit >= 0)
683 - unpack_limit = args->unpacklimit;
684 - if (args->keep_pack)
685 - unpack_limit = 0;
686 - use_thin_pack = args->use_thin_pack;
687 - fetch_all = args->fetch_all;
688 - verbose = args->verbose;
689 - depth = args->depth;
690 - no_progress = args->no_progress;
691 -}
692 -
673 int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
674 {
675 int i, ret, nr_heads;
@@ -710,40 +690,40 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
690
691 if (*arg == '-') {
692 if (!prefixcmp(arg, "--upload-pack=")) {
713 - uploadpack = arg + 14;
693 + args.uploadpack = arg + 14;
694 continue;
695 }
696 if (!prefixcmp(arg, "--exec=")) {
717 - uploadpack = arg + 7;
697 + args.uploadpack = arg + 7;
698 continue;
699 }
700 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
721 - quiet = 1;
701 + args.quiet = 1;
702 continue;
703 }
704 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
725 - keep_pack++;
726 - unpack_limit = 0;
705 + args.lock_pack = args.keep_pack;
706 + args.keep_pack = 1;
707 continue;
708 }
709 if (!strcmp("--thin", arg)) {
730 - use_thin_pack = 1;
710 + args.use_thin_pack = 1;
711 continue;
712 }
713 if (!strcmp("--all", arg)) {
734 - fetch_all = 1;
714 + args.fetch_all = 1;
715 continue;
716 }
717 if (!strcmp("-v", arg)) {
738 - verbose = 1;
718 + args.verbose = 1;
719 continue;
720 }
721 if (!prefixcmp(arg, "--depth=")) {
742 - depth = strtol(arg + 8, NULL, 0);
722 + args.depth = strtol(arg + 8, NULL, 0);
723 continue;
724 }
725 if (!strcmp("--no-progress", arg)) {
746 - no_progress = 1;
726 + args.no_progress = 1;
727 continue;
728 }
729 usage(fetch_pack_usage);
@@ -756,8 +736,7 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
736 if (!dest)
737 usage(fetch_pack_usage);
738
759 - ref = fetch_pack(dest, nr_heads, heads, NULL);
760 -
739 + ref = fetch_pack(&args, dest, nr_heads, heads, NULL);
740 ret = !ref;
741
742 while (ref) {
@@ -769,7 +748,8 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
748 return ret;
749 }
750
772 -struct ref *fetch_pack(const char *dest,
751 +struct ref *fetch_pack(struct fetch_pack_args *my_args,
752 + const char *dest,
753 int nr_heads,
754 char **heads,
755 char **pack_lockfile)
@@ -780,13 +760,14 @@ struct ref *fetch_pack(const char *dest,
760 struct ref *ref;
761 struct stat st;
762
783 - if (depth > 0) {
763 + memcpy(&args, my_args, sizeof(args));
764 + if (args.depth > 0) {
765 if (stat(git_path("shallow"), &st))
766 st.st_mtime = 0;
767 }
768
769 pid = git_connect(fd, (char *)dest, uploadpack,
789 - verbose ? CONNECT_VERBOSE : 0);
770 + args.verbose ? CONNECT_VERBOSE : 0);
771 if (pid < 0)
772 return NULL;
773 if (heads && nr_heads)
@@ -809,7 +790,7 @@ struct ref *fetch_pack(const char *dest,
790 }
791 }
792
812 - if (!ret && depth > 0) {
793 + if (!ret && args.depth > 0) {
794 struct cache_time mtime;
795 char *shallow = git_path("shallow");
796 int fd;
fetch-pack.h
+6 -3
@@ -8,14 +8,17 @@ struct fetch_pack_args
8 int depth;
9 unsigned quiet:1,
10 keep_pack:1,
11 + lock_pack:1,
12 use_thin_pack:1,
13 fetch_all:1,
14 verbose:1,
15 no_progress:1;
16 };
17
17 -void setup_fetch_pack(struct fetch_pack_args *args);
18 -
19 -struct ref *fetch_pack(const char *dest, int nr_heads, char **heads, char **pack_lockfile);
18 +struct ref *fetch_pack(struct fetch_pack_args *args,
19 + const char *dest,
20 + int nr_heads,
21 + char **heads,
22 + char **pack_lockfile);
23
24 #endif
transport.c
+3 -6
@@ -314,21 +314,18 @@ static int fetch_refs_via_pack(struct transport *transport,
314 struct fetch_pack_args args;
315 int i;
316
317 + memset(&args, 0, sizeof(args));
318 args.uploadpack = data->uploadpack;
318 - args.quiet = 0;
319 args.keep_pack = data->keep;
320 + args.lock_pack = 1;
321 args.unpacklimit = data->unpacklimit;
322 args.use_thin_pack = data->thin;
322 - args.fetch_all = 0;
323 args.verbose = transport->verbose;
324 args.depth = data->depth;
325 - args.no_progress = 0;
326 -
327 - setup_fetch_pack(&args);
325
326 for (i = 0; i < nr_heads; i++)
327 origh[i] = heads[i] = xstrdup(to_fetch[i]->name);
331 - refs = fetch_pack(dest, nr_heads, heads, &transport->pack_lockfile);
328 + refs = fetch_pack(&args, dest, nr_heads, heads, &transport->pack_lockfile);
329
330 for (i = 0; i < nr_heads; i++)
331 free(origh[i]);