fetch-pack: remove global (static) configuration variable "args"

This helps removes the hack in fetch_pack() that copies my_args to args. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Jeff King <peff@peff.net>

Nguyễn Thái Ngọc Duy committed Oct 26, 2012 at 22:53 UTC f8eb3036d0cc1838cc6865884b1648ff50a4f804
1 file changed +83 -77
builtin/fetch-pack.c
+83 -77
@@ -20,9 +20,6 @@ static int no_done;
20 static int fetch_fsck_objects = -1;
21 static int transfer_fsck_objects = -1;
22 static int agent_supported;
23 -static struct fetch_pack_args args = {
24 - /* .uploadpack = */ "git-upload-pack",
25 -};
23
24 static const char fetch_pack_usage[] =
25 "git fetch-pack [--all] [--stdin] [--quiet|-q] [--keep|-k] [--thin] "
@@ -173,9 +170,9 @@ enum ack_type {
170 ACK_ready
171 };
172
176 -static void consume_shallow_list(int fd)
173 +static void consume_shallow_list(struct fetch_pack_args *args, int fd)
174 {
178 - if (args.stateless_rpc && args.depth > 0) {
175 + if (args->stateless_rpc && args->depth > 0) {
176 /* If we sent a depth we will get back "duplicate"
177 * shallow and unshallow commands every time there
178 * is a block of have lines exchanged.
@@ -246,9 +243,10 @@ static enum ack_type get_ack(int fd, unsigned char *result_sha1)
243 die("git fetch_pack: expected ACK/NAK, got '%s'", line);
244 }
245
249 -static void send_request(int fd, struct strbuf *buf)
246 +static void send_request(struct fetch_pack_args *args,
247 + int fd, struct strbuf *buf)
248 {
251 - if (args.stateless_rpc) {
249 + if (args->stateless_rpc) {
250 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
251 packet_flush(fd);
252 } else
@@ -264,9 +262,9 @@ static void insert_one_alternate_ref(const struct ref *ref, void *unused)
262 #define PIPESAFE_FLUSH 32
263 #define LARGE_FLUSH 1024
264
267 -static int next_flush(int count)
265 +static int next_flush(struct fetch_pack_args *args, int count)
266 {
269 - int flush_limit = args.stateless_rpc ? LARGE_FLUSH : PIPESAFE_FLUSH;
267 + int flush_limit = args->stateless_rpc ? LARGE_FLUSH : PIPESAFE_FLUSH;
268
269 if (count < flush_limit)
270 count <<= 1;
@@ -275,7 +273,8 @@ static int next_flush(int count)
273 return count;
274 }
275
278 -static int find_common(int fd[2], unsigned char *result_sha1,
276 +static int find_common(struct fetch_pack_args *args,
277 + int fd[2], unsigned char *result_sha1,
278 struct ref *refs)
279 {
280 int fetching;
@@ -287,7 +286,7 @@ static int find_common(int fd[2], unsigned char *result_sha1,
286 struct strbuf req_buf = STRBUF_INIT;
287 size_t state_len = 0;
288
290 - if (args.stateless_rpc && multi_ack == 1)
289 + if (args->stateless_rpc && multi_ack == 1)
290 die("--stateless-rpc requires multi_ack_detailed");
291 if (marked)
292 for_each_ref(clear_marks, NULL);
@@ -325,9 +324,9 @@ static int find_common(int fd[2], unsigned char *result_sha1,
324 if (no_done) strbuf_addstr(&c, " no-done");
325 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
326 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
328 - if (args.use_thin_pack) strbuf_addstr(&c, " thin-pack");
329 - if (args.no_progress) strbuf_addstr(&c, " no-progress");
330 - if (args.include_tag) strbuf_addstr(&c, " include-tag");
327 + if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
328 + if (args->no_progress) strbuf_addstr(&c, " no-progress");
329 + if (args->include_tag) strbuf_addstr(&c, " include-tag");
330 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
331 if (agent_supported) strbuf_addf(&c, " agent=%s",
332 git_user_agent_sanitized());
@@ -346,16 +345,16 @@ static int find_common(int fd[2], unsigned char *result_sha1,
345
346 if (is_repository_shallow())
347 write_shallow_commits(&req_buf, 1);
349 - if (args.depth > 0)
350 - packet_buf_write(&req_buf, "deepen %d", args.depth);
348 + if (args->depth > 0)
349 + packet_buf_write(&req_buf, "deepen %d", args->depth);
350 packet_buf_flush(&req_buf);
351 state_len = req_buf.len;
352
354 - if (args.depth > 0) {
353 + if (args->depth > 0) {
354 char line[1024];
355 unsigned char sha1[20];
356
358 - send_request(fd[1], &req_buf);
357 + send_request(args, fd[1], &req_buf);
358 while (packet_read_line(fd[0], line, sizeof(line))) {
359 if (!prefixcmp(line, "shallow ")) {
360 if (get_sha1_hex(line + 8, sha1))
@@ -377,10 +376,10 @@ static int find_common(int fd[2], unsigned char *result_sha1,
376 }
377 die("expected shallow/unshallow, got %s", line);
378 }
380 - } else if (!args.stateless_rpc)
381 - send_request(fd[1], &req_buf);
379 + } else if (!args->stateless_rpc)
380 + send_request(args, fd[1], &req_buf);
381
383 - if (!args.stateless_rpc) {
382 + if (!args->stateless_rpc) {
383 /* If we aren't using the stateless-rpc interface
384 * we don't need to retain the headers.
385 */
@@ -392,29 +391,29 @@ static int find_common(int fd[2], unsigned char *result_sha1,
391 retval = -1;
392 while ((sha1 = get_rev())) {
393 packet_buf_write(&req_buf, "have %s\n", sha1_to_hex(sha1));
395 - if (args.verbose)
394 + if (args->verbose)
395 fprintf(stderr, "have %s\n", sha1_to_hex(sha1));
396 in_vain++;
397 if (flush_at <= ++count) {
398 int ack;
399
400 packet_buf_flush(&req_buf);
402 - send_request(fd[1], &req_buf);
401 + send_request(args, fd[1], &req_buf);
402 strbuf_setlen(&req_buf, state_len);
403 flushes++;
405 - flush_at = next_flush(count);
404 + flush_at = next_flush(args, count);
405
406 /*
407 * We keep one window "ahead" of the other side, and
408 * will wait for an ACK only on the next one
409 */
411 - if (!args.stateless_rpc && count == INITIAL_FLUSH)
410 + if (!args->stateless_rpc && count == INITIAL_FLUSH)
411 continue;
412
414 - consume_shallow_list(fd[0]);
413 + consume_shallow_list(args, fd[0]);
414 do {
415 ack = get_ack(fd[0], result_sha1);
417 - if (args.verbose && ack)
416 + if (args->verbose && ack)
417 fprintf(stderr, "got ack %d %s\n", ack,
418 sha1_to_hex(result_sha1));
419 switch (ack) {
@@ -430,7 +429,7 @@ static int find_common(int fd[2], unsigned char *result_sha1,
429 lookup_commit(result_sha1);
430 if (!commit)
431 die("invalid commit %s", sha1_to_hex(result_sha1));
433 - if (args.stateless_rpc
432 + if (args->stateless_rpc
433 && ack == ACK_common
434 && !(commit->object.flags & COMMON)) {
435 /* We need to replay the have for this object
@@ -455,7 +454,7 @@ static int find_common(int fd[2], unsigned char *result_sha1,
454 } while (ack);
455 flushes--;
456 if (got_continue && MAX_IN_VAIN < in_vain) {
458 - if (args.verbose)
457 + if (args->verbose)
458 fprintf(stderr, "giving up\n");
459 break; /* give up */
460 }
@@ -464,9 +463,9 @@ static int find_common(int fd[2], unsigned char *result_sha1,
463 done:
464 if (!got_ready || !no_done) {
465 packet_buf_write(&req_buf, "done\n");
467 - send_request(fd[1], &req_buf);
466 + send_request(args, fd[1], &req_buf);
467 }
469 - if (args.verbose)
468 + if (args->verbose)
469 fprintf(stderr, "done\n");
470 if (retval != 0) {
471 multi_ack = 0;
@@ -474,11 +473,11 @@ done:
473 }
474 strbuf_release(&req_buf);
475
477 - consume_shallow_list(fd[0]);
476 + consume_shallow_list(args, fd[0]);
477 while (flushes || multi_ack) {
478 int ack = get_ack(fd[0], result_sha1);
479 if (ack) {
481 - if (args.verbose)
480 + if (args->verbose)
481 fprintf(stderr, "got ack (%d) %s\n", ack,
482 sha1_to_hex(result_sha1));
483 if (ack == ACK)
@@ -515,10 +514,11 @@ static int mark_complete(const char *refname, const unsigned char *sha1, int fla
514 return 0;
515 }
516
518 -static void mark_recent_complete_commits(unsigned long cutoff)
517 +static void mark_recent_complete_commits(struct fetch_pack_args *args,
518 + unsigned long cutoff)
519 {
520 while (complete && cutoff <= complete->item->date) {
521 - if (args.verbose)
521 + if (args->verbose)
522 fprintf(stderr, "Marking %s as complete\n",
523 sha1_to_hex(complete->item->object.sha1));
524 pop_most_recent_commit(&complete, COMPLETE);
@@ -535,7 +535,8 @@ static int non_matching_ref(struct string_list_item *item, void *unused)
535 return 1;
536 }
537
538 -static void filter_refs(struct ref **refs, struct string_list *sought)
538 +static void filter_refs(struct fetch_pack_args *args,
539 + struct ref **refs, struct string_list *sought)
540 {
541 struct ref *newlist = NULL;
542 struct ref **newtail = &newlist;
@@ -564,8 +565,8 @@ static void filter_refs(struct ref **refs, struct string_list *sought)
565 }
566 }
567
567 - if (! keep && args.fetch_all &&
568 - (!args.depth || prefixcmp(ref->name, "refs/tags/")))
568 + if (! keep && args->fetch_all &&
569 + (!args->depth || prefixcmp(ref->name, "refs/tags/")))
570 keep = 1;
571
572 if (keep) {
@@ -586,7 +587,8 @@ static void mark_alternate_complete(const struct ref *ref, void *unused)
587 mark_complete(NULL, ref->old_sha1, 0, NULL);
588 }
589
589 -static int everything_local(struct ref **refs, struct string_list *sought)
590 +static int everything_local(struct fetch_pack_args *args,
591 + struct ref **refs, struct string_list *sought)
592 {
593 struct ref *ref;
594 int retval;
@@ -612,11 +614,11 @@ static int everything_local(struct ref **refs, struct string_list *sought)
614 }
615 }
616
615 - if (!args.depth) {
617 + if (!args->depth) {
618 for_each_ref(mark_complete, NULL);
619 for_each_alternate_ref(mark_alternate_complete, NULL);
620 if (cutoff)
619 - mark_recent_complete_commits(cutoff);
621 + mark_recent_complete_commits(args, cutoff);
622 }
623
624 /*
@@ -637,7 +639,7 @@ static int everything_local(struct ref **refs, struct string_list *sought)
639 }
640 }
641
640 - filter_refs(refs, sought);
642 + filter_refs(args, refs, sought);
643
644 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
645 const unsigned char *remote = ref->old_sha1;
@@ -647,7 +649,7 @@ static int everything_local(struct ref **refs, struct string_list *sought)
649 o = lookup_object(remote);
650 if (!o || !(o->flags & COMPLETE)) {
651 retval = 0;
650 - if (!args.verbose)
652 + if (!args->verbose)
653 continue;
654 fprintf(stderr,
655 "want %s (%s)\n", sha1_to_hex(remote),
@@ -656,7 +658,7 @@ static int everything_local(struct ref **refs, struct string_list *sought)
658 }
659
660 hashcpy(ref->new_sha1, local);
659 - if (!args.verbose)
661 + if (!args->verbose)
662 continue;
663 fprintf(stderr,
664 "already have %s (%s)\n", sha1_to_hex(remote),
@@ -674,14 +676,15 @@ static int sideband_demux(int in, int out, void *data)
676 return ret;
677 }
678
677 -static int get_pack(int xd[2], char **pack_lockfile)
679 +static int get_pack(struct fetch_pack_args *args,
680 + int xd[2], char **pack_lockfile)
681 {
682 struct async demux;
683 const char *argv[20];
684 char keep_arg[256];
685 char hdr_arg[256];
686 const char **av;
684 - int do_keep = args.keep_pack;
687 + int do_keep = args->keep_pack;
688 struct child_process cmd;
689
690 memset(&demux, 0, sizeof(demux));
@@ -704,7 +707,7 @@ static int get_pack(int xd[2], char **pack_lockfile)
707 cmd.argv = argv;
708 av = argv;
709 *hdr_arg = 0;
707 - if (!args.keep_pack && unpack_limit) {
710 + if (!args->keep_pack && unpack_limit) {
711 struct pack_header header;
712
713 if (read_pack_header(demux.out, &header))
@@ -723,11 +726,11 @@ static int get_pack(int xd[2], char **pack_lockfile)
726 cmd.out = -1;
727 *av++ = "index-pack";
728 *av++ = "--stdin";
726 - if (!args.quiet && !args.no_progress)
729 + if (!args->quiet && !args->no_progress)
730 *av++ = "-v";
728 - if (args.use_thin_pack)
731 + if (args->use_thin_pack)
732 *av++ = "--fix-thin";
730 - if (args.lock_pack || unpack_limit) {
733 + if (args->lock_pack || unpack_limit) {
734 int s = sprintf(keep_arg,
735 "--keep=fetch-pack %"PRIuMAX " on ", (uintmax_t) getpid());
736 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
@@ -737,7 +740,7 @@ static int get_pack(int xd[2], char **pack_lockfile)
740 }
741 else {
742 *av++ = "unpack-objects";
740 - if (args.quiet || args.no_progress)
743 + if (args->quiet || args->no_progress)
744 *av++ = "-q";
745 }
746 if (*hdr_arg)
@@ -766,10 +769,11 @@ static int get_pack(int xd[2], char **pack_lockfile)
769 return 0;
770 }
771
769 -static struct ref *do_fetch_pack(int fd[2],
770 - const struct ref *orig_ref,
771 - struct string_list *sought,
772 - char **pack_lockfile)
772 +static struct ref *do_fetch_pack(struct fetch_pack_args *args,
773 + int fd[2],
774 + const struct ref *orig_ref,
775 + struct string_list *sought,
776 + char **pack_lockfile)
777 {
778 struct ref *ref = copy_ref_list(orig_ref);
779 unsigned char sha1[20];
@@ -781,64 +785,64 @@ static struct ref *do_fetch_pack(int fd[2],
785 if (is_repository_shallow() && !server_supports("shallow"))
786 die("Server does not support shallow clients");
787 if (server_supports("multi_ack_detailed")) {
784 - if (args.verbose)
788 + if (args->verbose)
789 fprintf(stderr, "Server supports multi_ack_detailed\n");
790 multi_ack = 2;
791 if (server_supports("no-done")) {
788 - if (args.verbose)
792 + if (args->verbose)
793 fprintf(stderr, "Server supports no-done\n");
790 - if (args.stateless_rpc)
794 + if (args->stateless_rpc)
795 no_done = 1;
796 }
797 }
798 else if (server_supports("multi_ack")) {
795 - if (args.verbose)
799 + if (args->verbose)
800 fprintf(stderr, "Server supports multi_ack\n");
801 multi_ack = 1;
802 }
803 if (server_supports("side-band-64k")) {
800 - if (args.verbose)
804 + if (args->verbose)
805 fprintf(stderr, "Server supports side-band-64k\n");
806 use_sideband = 2;
807 }
808 else if (server_supports("side-band")) {
805 - if (args.verbose)
809 + if (args->verbose)
810 fprintf(stderr, "Server supports side-band\n");
811 use_sideband = 1;
812 }
813 if (!server_supports("thin-pack"))
810 - args.use_thin_pack = 0;
814 + args->use_thin_pack = 0;
815 if (!server_supports("no-progress"))
812 - args.no_progress = 0;
816 + args->no_progress = 0;
817 if (!server_supports("include-tag"))
814 - args.include_tag = 0;
818 + args->include_tag = 0;
819 if (server_supports("ofs-delta")) {
816 - if (args.verbose)
820 + if (args->verbose)
821 fprintf(stderr, "Server supports ofs-delta\n");
822 } else
823 prefer_ofs_delta = 0;
824
825 if ((agent_feature = server_feature_value("agent", &agent_len))) {
826 agent_supported = 1;
823 - if (args.verbose && agent_len)
827 + if (args->verbose && agent_len)
828 fprintf(stderr, "Server version is %.*s\n",
829 agent_len, agent_feature);
830 }
831
828 - if (everything_local(&ref, sought)) {
832 + if (everything_local(args, &ref, sought)) {
833 packet_flush(fd[1]);
834 goto all_done;
835 }
832 - if (find_common(fd, sha1, ref) < 0)
833 - if (!args.keep_pack)
836 + if (find_common(args, fd, sha1, ref) < 0)
837 + if (!args->keep_pack)
838 /* When cloning, it is not unusual to have
839 * no common commit.
840 */
841 warning("no common commits");
842
839 - if (args.stateless_rpc)
843 + if (args->stateless_rpc)
844 packet_flush(fd[1]);
841 - if (get_pack(fd, pack_lockfile))
845 + if (get_pack(args, fd, pack_lockfile))
846 die("git fetch-pack: fetch failed.");
847
848 all_done:
@@ -900,9 +904,13 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
904 char *pack_lockfile = NULL;
905 char **pack_lockfile_ptr = NULL;
906 struct child_process *conn;
907 + struct fetch_pack_args args;
908
909 packet_trace_identity("fetch-pack");
910
911 + memset(&args, 0, sizeof(args));
912 + args.uploadpack = "git-upload-pack";
913 +
914 for (i = 1; i < argc && *argv[i] == '-'; i++) {
915 const char *arg = argv[i];
916
@@ -1039,7 +1047,7 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
1047 return ret;
1048 }
1049
1042 -struct ref *fetch_pack(struct fetch_pack_args *my_args,
1050 +struct ref *fetch_pack(struct fetch_pack_args *args,
1051 int fd[], struct child_process *conn,
1052 const struct ref *ref,
1053 const char *dest,
@@ -1050,9 +1058,7 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args,
1058 struct ref *ref_cpy;
1059
1060 fetch_pack_setup();
1053 - if (&args != my_args)
1054 - memcpy(&args, my_args, sizeof(args));
1055 - if (args.depth > 0) {
1061 + if (args->depth > 0) {
1062 if (stat(git_path("shallow"), &st))
1063 st.st_mtime = 0;
1064 }
@@ -1066,9 +1072,9 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args,
1072 packet_flush(fd[1]);
1073 die("no matching remote head");
1074 }
1069 - ref_cpy = do_fetch_pack(fd, ref, sought, pack_lockfile);
1075 + ref_cpy = do_fetch_pack(args, fd, ref, sought, pack_lockfile);
1076
1071 - if (args.depth > 0) {
1077 + if (args->depth > 0) {
1078 struct cache_time mtime;
1079 struct strbuf sb = STRBUF_INIT;
1080 char *shallow = git_path("shallow");