Skip to content

Commit f046226

Browse files
jeffhostetlerdscho
authored andcommitted
sha1-file: create shared-cache directory if it doesn't exist
The config variable `gvfs.sharedCache` contains the pathname to an alternate <odb> that will be used by `gvfs-helper` to store dynamically-fetched missing objects. If this directory does not exist on disk, `prepare_alt_odb()` omits this directory from the in-memory list of alternates. This causes `git` commands (and `gvfs-helper` in particular) to fall-back to `.git/objects` for storage of these objects. This disables the shared-cache and leads to poorer performance. Teach `alt_obj_usable()` and `prepare_alt_odb()`, match up the directory named in `gvfs.sharedCache` with an entry in `.git/objects/info/alternates` and force-create the `<odb>` root directory (and the associated `<odb>/pack` directory) if necessary. If the value of `gvfs.sharedCache` refers to a directory that is NOT listed as an alternate, create an in-memory alternate entry in the odb-list. (This is similar to how GIT_ALTERNATE_OBJECT_DIRECTORIES works.) This work happens the first time that `prepare_alt_odb()` is called. Furthermore, teach the `--shared-cache=<odb>` command line option in `gvfs-helper` (which is runs after the first call to `prepare_alt_odb()`) to override the inherited shared-cache (and again, create the ODB directory if necessary). Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com>
1 parent 317d0a7 commit f046226

File tree

6 files changed

+185
-33
lines changed

6 files changed

+185
-33
lines changed

Diff for: config.c

+5-7
Original file line numberDiff line numberDiff line change
@@ -1798,19 +1798,17 @@ static int git_default_gvfs_config(const char *var, const char *value)
17981798
}
17991799

18001800
if (!strcmp(var, "gvfs.sharedcache") && value && *value) {
1801-
struct strbuf buf = STRBUF_INIT;
1802-
strbuf_addstr(&buf, value);
1803-
if (strbuf_normalize_path(&buf) < 0) {
1801+
strbuf_setlen(&gvfs_shared_cache_pathname, 0);
1802+
strbuf_addstr(&gvfs_shared_cache_pathname, value);
1803+
if (strbuf_normalize_path(&gvfs_shared_cache_pathname) < 0) {
18041804
/*
18051805
* Pretend it wasn't set. This will cause us to
18061806
* fallback to ".git/objects" effectively.
18071807
*/
1808-
strbuf_release(&buf);
1808+
strbuf_release(&gvfs_shared_cache_pathname);
18091809
return 0;
18101810
}
1811-
strbuf_trim_trailing_dir_sep(&buf);
1812-
1813-
gvfs_shared_cache_pathname = strbuf_detach(&buf, NULL);
1811+
strbuf_trim_trailing_dir_sep(&gvfs_shared_cache_pathname);
18141812
return 0;
18151813
}
18161814

Diff for: environment.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ int protect_hfs = PROTECT_HFS_DEFAULT;
100100
int protect_ntfs = PROTECT_NTFS_DEFAULT;
101101
int core_use_gvfs_helper;
102102
char *gvfs_cache_server_url;
103-
const char *gvfs_shared_cache_pathname;
103+
struct strbuf gvfs_shared_cache_pathname = STRBUF_INIT;
104104

105105
/*
106106
* The character that begins a commented line in user-editable file

Diff for: environment.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ extern int protect_hfs;
177177
extern int protect_ntfs;
178178
extern int core_use_gvfs_helper;
179179
extern char *gvfs_cache_server_url;
180-
extern const char *gvfs_shared_cache_pathname;
180+
extern struct strbuf gvfs_shared_cache_pathname;
181181

182182
extern int core_apply_sparse_checkout;
183183
extern int core_sparse_checkout_cone;

Diff for: gvfs-helper-client.c

+20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#define USE_THE_REPOSITORY_VARIABLE
22
#include "git-compat-util.h"
3+
#include "environment.h"
34
#include "hex.h"
45
#include "strvec.h"
56
#include "trace2.h"
@@ -208,13 +209,32 @@ static int gh_client__get__receive_response(
208209
return err;
209210
}
210211

212+
/*
213+
* Select the preferred ODB for fetching missing objects.
214+
* This should be the alternate with the same directory
215+
* name as set in `gvfs.sharedCache`.
216+
*
217+
* Fallback to .git/objects if necessary.
218+
*/
211219
static void gh_client__choose_odb(void)
212220
{
221+
struct object_directory *odb;
222+
213223
if (gh_client__chosen_odb)
214224
return;
215225

216226
prepare_alt_odb(the_repository);
217227
gh_client__chosen_odb = the_repository->objects->odb;
228+
229+
if (!gvfs_shared_cache_pathname.len)
230+
return;
231+
232+
for (odb = the_repository->objects->odb->next; odb; odb = odb->next) {
233+
if (!fspathcmp(odb->path, gvfs_shared_cache_pathname.buf)) {
234+
gh_client__chosen_odb = odb;
235+
return;
236+
}
237+
}
218238
}
219239

220240
static int gh_client__get(enum gh_client__created *p_ghc)

Diff for: gvfs-helper.c

+83-24
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,11 @@
8181
//
8282
// Fetch 1 or more objects. If a cache-server is configured,
8383
// try it first. Optionally fallback to the main Git server.
84+
//
8485
// 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).
8889
//
8990
// git> get
9091
// git> <oid>
@@ -641,26 +642,88 @@ static int option_parse_cache_server_mode(const struct option *opt,
641642
}
642643

643644
/*
644-
* Let command line args override "gvfs.sharedcache" config setting.
645+
* Let command line args override "gvfs.sharedcache" config setting
646+
* and override the value set by git_default_config().
647+
*
648+
* The command line is parsed *AFTER* the config is loaded, so
649+
* prepared_alt_odb() has already been called any default or inherited
650+
* shared-cache has already been set.
645651
*
646-
* It would be nice to move this to parse-options.c as an
647-
* OPTION_PATHNAME handler. And maybe have flags for exists()
648-
* and is_directory().
652+
* We have a chance to override it here.
649653
*/
650654
static int option_parse_shared_cache_directory(const struct option *opt,
651655
const char *arg, int unset)
652656
{
657+
struct strbuf buf_arg = STRBUF_INIT;
658+
653659
if (unset) /* should not happen */
654660
return error(_("missing value for switch '%s'"),
655661
opt->long_name);
656662

657-
if (!is_directory(arg))
658-
return error(_("value for switch '%s' is not a directory: '%s'"),
659-
opt->long_name, arg);
663+
strbuf_addstr(&buf_arg, arg);
664+
if (strbuf_normalize_path(&buf_arg) < 0) {
665+
/*
666+
* Pretend command line wasn't given. Use whatever
667+
* settings we already have from the config.
668+
*/
669+
strbuf_release(&buf_arg);
670+
return 0;
671+
}
672+
strbuf_trim_trailing_dir_sep(&buf_arg);
673+
674+
if (!strbuf_cmp(&buf_arg, &gvfs_shared_cache_pathname)) {
675+
/*
676+
* The command line argument matches what we got from
677+
* the config, so we're already setup correctly. (And
678+
* we have already verified that the directory exists
679+
* on disk.)
680+
*/
681+
strbuf_release(&buf_arg);
682+
return 0;
683+
}
684+
685+
else if (!gvfs_shared_cache_pathname.len) {
686+
/*
687+
* A shared-cache was requested and we did not inherit one.
688+
* Try it, but let alt_odb_usable() secretly disable it if
689+
* it cannot create the directory on disk.
690+
*/
691+
strbuf_addbuf(&gvfs_shared_cache_pathname, &buf_arg);
660692

661-
gvfs_shared_cache_pathname = arg;
693+
add_to_alternates_memory(buf_arg.buf);
662694

663-
return 0;
695+
strbuf_release(&buf_arg);
696+
return 0;
697+
}
698+
699+
else {
700+
/*
701+
* The requested shared-cache is different from the one
702+
* we inherited. Replace the inherited value with this
703+
* one, but smartly fallback if necessary.
704+
*/
705+
struct strbuf buf_prev = STRBUF_INIT;
706+
707+
strbuf_addbuf(&buf_prev, &gvfs_shared_cache_pathname);
708+
709+
strbuf_setlen(&gvfs_shared_cache_pathname, 0);
710+
strbuf_addbuf(&gvfs_shared_cache_pathname, &buf_arg);
711+
712+
add_to_alternates_memory(buf_arg.buf);
713+
714+
/*
715+
* alt_odb_usable() releases gvfs_shared_cache_pathname
716+
* if it cannot create the directory on disk, so fallback
717+
* to the previous choice when it fails.
718+
*/
719+
if (!gvfs_shared_cache_pathname.len)
720+
strbuf_addbuf(&gvfs_shared_cache_pathname,
721+
&buf_prev);
722+
723+
strbuf_release(&buf_arg);
724+
strbuf_release(&buf_prev);
725+
return 0;
726+
}
664727
}
665728

666729
/*
@@ -961,24 +1024,20 @@ static void approve_cache_server_creds(void)
9611024
}
9621025

9631026
/*
964-
* Select the ODB directory where we will write objects that we
965-
* download. If was given on the command line or define in the
966-
* config, use the local ODB (in ".git/objects").
1027+
* Get the pathname to the ODB where we write objects that we download.
9671028
*/
9681029
static void select_odb(void)
9691030
{
970-
const char *odb_path = NULL;
1031+
prepare_alt_odb(the_repository);
9711032

9721033
strbuf_init(&gh__global.buf_odb_path, 0);
9731034

974-
if (gvfs_shared_cache_pathname && *gvfs_shared_cache_pathname)
975-
odb_path = gvfs_shared_cache_pathname;
976-
else {
977-
prepare_alt_odb(the_repository);
978-
odb_path = the_repository->objects->odb->path;
979-
}
980-
981-
strbuf_addstr(&gh__global.buf_odb_path, odb_path);
1035+
if (gvfs_shared_cache_pathname.len)
1036+
strbuf_addbuf(&gh__global.buf_odb_path,
1037+
&gvfs_shared_cache_pathname);
1038+
else
1039+
strbuf_addstr(&gh__global.buf_odb_path,
1040+
the_repository->objects->odb->path);
9821041
}
9831042

9841043
/*

Diff for: object-file.c

+75
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,8 @@ const char *loose_object_path(struct repository *r, struct strbuf *buf,
527527
return odb_loose_path(r->objects->odb, buf, oid);
528528
}
529529

530+
static int gvfs_matched_shared_cache_to_alternate;
531+
530532
/*
531533
* Return non-zero iff the path is usable as an alternate object database.
532534
*/
@@ -536,6 +538,52 @@ static int alt_odb_usable(struct raw_object_store *o,
536538
{
537539
int r;
538540

541+
if (!strbuf_cmp(path, &gvfs_shared_cache_pathname)) {
542+
/*
543+
* `gvfs.sharedCache` is the preferred alternate that we
544+
* will use with `gvfs-helper.exe` to dynamically fetch
545+
* missing objects. It is set during git_default_config().
546+
*
547+
* Make sure the directory exists on disk before we let the
548+
* stock code discredit it.
549+
*/
550+
struct strbuf buf_pack_foo = STRBUF_INIT;
551+
enum scld_error scld;
552+
553+
/*
554+
* Force create the "<odb>" and "<odb>/pack" directories, if
555+
* not present on disk. Append an extra bogus directory to
556+
* get safe_create_leading_directories() to see "<odb>/pack"
557+
* as a leading directory of something deeper (which it
558+
* won't create).
559+
*/
560+
strbuf_addf(&buf_pack_foo, "%s/pack/foo", path->buf);
561+
562+
scld = safe_create_leading_directories(buf_pack_foo.buf);
563+
if (scld != SCLD_OK && scld != SCLD_EXISTS) {
564+
error_errno(_("could not create shared-cache ODB '%s'"),
565+
gvfs_shared_cache_pathname.buf);
566+
567+
strbuf_release(&buf_pack_foo);
568+
569+
/*
570+
* Pretend no shared-cache was requested and
571+
* effectively fallback to ".git/objects" for
572+
* fetching missing objects.
573+
*/
574+
strbuf_release(&gvfs_shared_cache_pathname);
575+
return 0;
576+
}
577+
578+
/*
579+
* We know that there is an alternate (either from
580+
* .git/objects/info/alternates or from a memory-only
581+
* entry) associated with the shared-cache directory.
582+
*/
583+
gvfs_matched_shared_cache_to_alternate++;
584+
strbuf_release(&buf_pack_foo);
585+
}
586+
539587
/* Detect cases where alternate disappeared */
540588
if (!is_directory(path->buf)) {
541589
error(_("object directory %s does not exist; "
@@ -1019,6 +1067,33 @@ void prepare_alt_odb(struct repository *r)
10191067
link_alt_odb_entries(r, r->objects->alternate_db, PATH_SEP, NULL, 0);
10201068

10211069
read_info_alternates(r, r->objects->odb->path, 0);
1070+
1071+
if (gvfs_shared_cache_pathname.len &&
1072+
!gvfs_matched_shared_cache_to_alternate) {
1073+
/*
1074+
* There is no entry in .git/objects/info/alternates for
1075+
* the requested shared-cache directory. Therefore, the
1076+
* odb-list does not contain this directory.
1077+
*
1078+
* Force this directory into the odb-list as an in-memory
1079+
* alternate. Implicitly create the directory on disk, if
1080+
* necessary.
1081+
*
1082+
* See GIT_ALTERNATE_OBJECT_DIRECTORIES for another example
1083+
* of this kind of usage.
1084+
*
1085+
* Note: This has the net-effect of allowing Git to treat
1086+
* `gvfs.sharedCache` as an unofficial alternate. This
1087+
* usage should be discouraged for compatbility reasons
1088+
* with other tools in the overall Git ecosystem (that
1089+
* won't know about this trick). It would be much better
1090+
* for us to update .git/objects/info/alternates instead.
1091+
* The code here is considered a backstop.
1092+
*/
1093+
link_alt_odb_entries(r, gvfs_shared_cache_pathname.buf,
1094+
'\n', NULL, 0);
1095+
}
1096+
10221097
r->objects->loaded_alternates = 1;
10231098
}
10241099

0 commit comments

Comments
 (0)