|
81 | 81 | // |
82 | 82 | // Fetch 1 or more objects. If a cache-server is configured, |
83 | 83 | // try it first. Optionally fallback to the main Git server. |
| 84 | +// |
84 | 85 | // Create 1 or more loose objects and/or packfiles in the |
85 | | -// requested shared-cache directory (given on the command |
86 | | -// line and which is reported at the beginning of the |
87 | | -// response). |
| 86 | +// shared-cache ODB. (The pathname of the selected ODB is |
| 87 | +// reported at the beginning of the response; this should |
| 88 | +// match the pathname given on the command line). |
88 | 89 | // |
89 | 90 | // git> get |
90 | 91 | // git> <oid> |
@@ -632,26 +633,88 @@ static int option_parse_cache_server_mode(const struct option *opt, |
632 | 633 | } |
633 | 634 |
|
634 | 635 | /* |
635 | | - * Let command line args override "gvfs.sharedcache" config setting. |
| 636 | + * Let command line args override "gvfs.sharedcache" config setting |
| 637 | + * and override the value set by git_default_config(). |
| 638 | + * |
| 639 | + * The command line is parsed *AFTER* the config is loaded, so |
| 640 | + * prepared_alt_odb() has already been called any default or inherited |
| 641 | + * shared-cache has already been set. |
636 | 642 | * |
637 | | - * It would be nice to move this to parse-options.c as an |
638 | | - * OPTION_PATHNAME handler. And maybe have flags for exists() |
639 | | - * and is_directory(). |
| 643 | + * We have a chance to override it here. |
640 | 644 | */ |
641 | 645 | static int option_parse_shared_cache_directory(const struct option *opt, |
642 | 646 | const char *arg, int unset) |
643 | 647 | { |
| 648 | + struct strbuf buf_arg = STRBUF_INIT; |
| 649 | + |
644 | 650 | if (unset) /* should not happen */ |
645 | 651 | return error(_("missing value for switch '%s'"), |
646 | 652 | opt->long_name); |
647 | 653 |
|
648 | | - if (!is_directory(arg)) |
649 | | - return error(_("value for switch '%s' is not a directory: '%s'"), |
650 | | - opt->long_name, arg); |
| 654 | + strbuf_addstr(&buf_arg, arg); |
| 655 | + if (strbuf_normalize_path(&buf_arg) < 0) { |
| 656 | + /* |
| 657 | + * Pretend command line wasn't given. Use whatever |
| 658 | + * settings we already have from the config. |
| 659 | + */ |
| 660 | + strbuf_release(&buf_arg); |
| 661 | + return 0; |
| 662 | + } |
| 663 | + strbuf_trim_trailing_dir_sep(&buf_arg); |
| 664 | + |
| 665 | + if (!strbuf_cmp(&buf_arg, &gvfs_shared_cache_pathname)) { |
| 666 | + /* |
| 667 | + * The command line argument matches what we got from |
| 668 | + * the config, so we're already setup correctly. (And |
| 669 | + * we have already verified that the directory exists |
| 670 | + * on disk.) |
| 671 | + */ |
| 672 | + strbuf_release(&buf_arg); |
| 673 | + return 0; |
| 674 | + } |
| 675 | + |
| 676 | + else if (!gvfs_shared_cache_pathname.len) { |
| 677 | + /* |
| 678 | + * A shared-cache was requested and we did not inherit one. |
| 679 | + * Try it, but let alt_odb_usabe() secretly disable it if |
| 680 | + * it cannot create the directory on disk. |
| 681 | + */ |
| 682 | + strbuf_addbuf(&gvfs_shared_cache_pathname, &buf_arg); |
651 | 683 |
|
652 | | - gvfs_shared_cache_pathname = arg; |
| 684 | + add_to_alternates_memory(buf_arg.buf); |
653 | 685 |
|
654 | | - return 0; |
| 686 | + strbuf_release(&buf_arg); |
| 687 | + return 0; |
| 688 | + } |
| 689 | + |
| 690 | + else { |
| 691 | + /* |
| 692 | + * The requested shared-cache is different from the one |
| 693 | + * we inherited. Replace the inherited value with this |
| 694 | + * one, but smartly fallback if necessary. |
| 695 | + */ |
| 696 | + struct strbuf buf_prev = STRBUF_INIT; |
| 697 | + |
| 698 | + strbuf_addbuf(&buf_prev, &gvfs_shared_cache_pathname); |
| 699 | + |
| 700 | + strbuf_setlen(&gvfs_shared_cache_pathname, 0); |
| 701 | + strbuf_addbuf(&gvfs_shared_cache_pathname, &buf_arg); |
| 702 | + |
| 703 | + add_to_alternates_memory(buf_arg.buf); |
| 704 | + |
| 705 | + /* |
| 706 | + * alt_odb_usabe() releases gvfs_shared_cache_pathname |
| 707 | + * if it cannot create the directory on disk, so fallback |
| 708 | + * to the previous choice when it fails. |
| 709 | + */ |
| 710 | + if (!gvfs_shared_cache_pathname.len) |
| 711 | + strbuf_addbuf(&gvfs_shared_cache_pathname, |
| 712 | + &buf_prev); |
| 713 | + |
| 714 | + strbuf_release(&buf_arg); |
| 715 | + strbuf_release(&buf_prev); |
| 716 | + return 0; |
| 717 | + } |
655 | 718 | } |
656 | 719 |
|
657 | 720 | /* |
@@ -949,24 +1012,20 @@ static void approve_cache_server_creds(void) |
949 | 1012 | } |
950 | 1013 |
|
951 | 1014 | /* |
952 | | - * Select the ODB directory where we will write objects that we |
953 | | - * download. If was given on the command line or define in the |
954 | | - * config, use the local ODB (in ".git/objects"). |
| 1015 | + * Get the pathname to the ODB where we write objects that we download. |
955 | 1016 | */ |
956 | 1017 | static void select_odb(void) |
957 | 1018 | { |
958 | | - const char *odb_path = NULL; |
| 1019 | + prepare_alt_odb(the_repository); |
959 | 1020 |
|
960 | 1021 | strbuf_init(&gh__global.buf_odb_path, 0); |
961 | 1022 |
|
962 | | - if (gvfs_shared_cache_pathname && *gvfs_shared_cache_pathname) |
963 | | - odb_path = gvfs_shared_cache_pathname; |
964 | | - else { |
965 | | - prepare_alt_odb(the_repository); |
966 | | - odb_path = the_repository->objects->odb->path; |
967 | | - } |
968 | | - |
969 | | - strbuf_addstr(&gh__global.buf_odb_path, odb_path); |
| 1023 | + if (gvfs_shared_cache_pathname.len) |
| 1024 | + strbuf_addbuf(&gh__global.buf_odb_path, |
| 1025 | + &gvfs_shared_cache_pathname); |
| 1026 | + else |
| 1027 | + strbuf_addstr(&gh__global.buf_odb_path, |
| 1028 | + the_repository->objects->odb->path); |
970 | 1029 | } |
971 | 1030 |
|
972 | 1031 | /* |
|
0 commit comments