Skip to content

Commit 041bbba

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 <[email protected]>
1 parent 52ae1cd commit 041bbba

File tree

5 files changed

+188
-35
lines changed

5 files changed

+188
-35
lines changed

environment.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ int protect_hfs = PROTECT_HFS_DEFAULT;
132132
int protect_ntfs = PROTECT_NTFS_DEFAULT;
133133
int core_use_gvfs_helper;
134134
char *gvfs_cache_server_url;
135-
const char *gvfs_shared_cache_pathname;
135+
struct strbuf gvfs_shared_cache_pathname = STRBUF_INIT;
136136

137137
/*
138138
* The character that begins a commented line in user-editable file
@@ -714,19 +714,17 @@ static int git_default_gvfs_config(const char *var, const char *value)
714714
}
715715

716716
if (!strcmp(var, "gvfs.sharedcache") && value && *value) {
717-
struct strbuf buf = STRBUF_INIT;
718-
strbuf_addstr(&buf, value);
719-
if (strbuf_normalize_path(&buf) < 0) {
717+
strbuf_setlen(&gvfs_shared_cache_pathname, 0);
718+
strbuf_addstr(&gvfs_shared_cache_pathname, value);
719+
if (strbuf_normalize_path(&gvfs_shared_cache_pathname) < 0) {
720720
/*
721721
* Pretend it wasn't set. This will cause us to
722722
* fallback to ".git/objects" effectively.
723723
*/
724-
strbuf_release(&buf);
724+
strbuf_release(&gvfs_shared_cache_pathname);
725725
return 0;
726726
}
727-
strbuf_trim_trailing_dir_sep(&buf);
728-
729-
gvfs_shared_cache_pathname = strbuf_detach(&buf, NULL);
727+
strbuf_trim_trailing_dir_sep(&gvfs_shared_cache_pathname);
730728
return 0;
731729
}
732730

environment.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ extern int protect_hfs;
166166
extern int protect_ntfs;
167167
extern int core_use_gvfs_helper;
168168
extern char *gvfs_cache_server_url;
169-
extern const char *gvfs_shared_cache_pathname;
169+
extern struct strbuf gvfs_shared_cache_pathname;
170170

171171
extern int core_apply_sparse_checkout;
172172
extern int core_sparse_checkout_cone;

gvfs-helper-client.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#define USE_THE_REPOSITORY_VARIABLE
22
#include "git-compat-util.h"
3+
#include "dir.h"
4+
#include "environment.h"
35
#include "gvfs-helper-client.h"
46
#include "hex.h"
57
#include "object-file.h"
@@ -208,13 +210,32 @@ static int gh_client__get__receive_response(
208210
return err;
209211
}
210212

213+
/*
214+
* Select the preferred ODB for fetching missing objects.
215+
* This should be the alternate with the same directory
216+
* name as set in `gvfs.sharedCache`.
217+
*
218+
* Fallback to .git/objects if necessary.
219+
*/
211220
static void gh_client__choose_odb(void)
212221
{
222+
struct odb_source *odb;
223+
213224
if (gh_client__chosen_odb)
214225
return;
215226

216227
odb_prepare_alternates(the_repository->objects);
217228
gh_client__chosen_odb = the_repository->objects->sources;
229+
230+
if (!gvfs_shared_cache_pathname.len)
231+
return;
232+
233+
for (odb = the_repository->objects->sources->next; odb; odb = odb->next) {
234+
if (!fspathcmp(odb->path, gvfs_shared_cache_pathname.buf)) {
235+
gh_client__chosen_odb = odb;
236+
return;
237+
}
238+
}
218239
}
219240

220241
static int gh_client__get(enum gh_client__created *p_ghc)

gvfs-helper.c

Lines changed: 83 additions & 24 deletions
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+
odb_prepare_alternates(the_repository->objects);
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-
odb_prepare_alternates(the_repository->objects);
978-
odb_path = the_repository->objects->sources->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
/*

odb.c

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ int odb_mkstemp(struct object_database *odb,
9191
return xmkstemp_mode(temp_filename->buf, mode);
9292
}
9393

94+
static int gvfs_matched_shared_cache_to_alternate;
95+
9496
/*
9597
* Return non-zero iff the path is usable as an alternate object database.
9698
*/
@@ -100,6 +102,52 @@ static int alt_odb_usable(struct object_database *o,
100102
{
101103
int r;
102104

105+
if (!strbuf_cmp(path, &gvfs_shared_cache_pathname)) {
106+
/*
107+
* `gvfs.sharedCache` is the preferred alternate that we
108+
* will use with `gvfs-helper.exe` to dynamically fetch
109+
* missing objects. It is set during git_default_config().
110+
*
111+
* Make sure the directory exists on disk before we let the
112+
* stock code discredit it.
113+
*/
114+
struct strbuf buf_pack_foo = STRBUF_INIT;
115+
enum scld_error scld;
116+
117+
/*
118+
* Force create the "<odb>" and "<odb>/pack" directories, if
119+
* not present on disk. Append an extra bogus directory to
120+
* get safe_create_leading_directories() to see "<odb>/pack"
121+
* as a leading directory of something deeper (which it
122+
* won't create).
123+
*/
124+
strbuf_addf(&buf_pack_foo, "%s/pack/foo", path->buf);
125+
126+
scld = safe_create_leading_directories(the_repository, buf_pack_foo.buf);
127+
if (scld != SCLD_OK && scld != SCLD_EXISTS) {
128+
error_errno(_("could not create shared-cache ODB '%s'"),
129+
gvfs_shared_cache_pathname.buf);
130+
131+
strbuf_release(&buf_pack_foo);
132+
133+
/*
134+
* Pretend no shared-cache was requested and
135+
* effectively fallback to ".git/objects" for
136+
* fetching missing objects.
137+
*/
138+
strbuf_release(&gvfs_shared_cache_pathname);
139+
return 0;
140+
}
141+
142+
/*
143+
* We know that there is an alternate (either from
144+
* .git/objects/info/alternates or from a memory-only
145+
* entry) associated with the shared-cache directory.
146+
*/
147+
gvfs_matched_shared_cache_to_alternate++;
148+
strbuf_release(&buf_pack_foo);
149+
}
150+
103151
/* Detect cases where alternate disappeared */
104152
if (!is_directory(path->buf)) {
105153
error(_("object directory %s does not exist; "
@@ -605,8 +653,35 @@ void odb_prepare_alternates(struct object_database *odb)
605653

606654
link_alt_odb_entries(odb, odb->alternate_db, PATH_SEP, NULL, 0);
607655

608-
read_info_alternates(odb, odb->sources->path, 0);
609-
odb->loaded_alternates = 1;
656+
read_info_alternates(r, r->objects->odb->path, 0);
657+
658+
if (gvfs_shared_cache_pathname.len &&
659+
!gvfs_matched_shared_cache_to_alternate) {
660+
/*
661+
* There is no entry in .git/objects/info/alternates for
662+
* the requested shared-cache directory. Therefore, the
663+
* odb-list does not contain this directory.
664+
*
665+
* Force this directory into the odb-list as an in-memory
666+
* alternate. Implicitly create the directory on disk, if
667+
* necessary.
668+
*
669+
* See GIT_ALTERNATE_OBJECT_DIRECTORIES for another example
670+
* of this kind of usage.
671+
*
672+
* Note: This has the net-effect of allowing Git to treat
673+
* `gvfs.sharedCache` as an unofficial alternate. This
674+
* usage should be discouraged for compatbility reasons
675+
* with other tools in the overall Git ecosystem (that
676+
* won't know about this trick). It would be much better
677+
* for us to update .git/objects/info/alternates instead.
678+
* The code here is considered a backstop.
679+
*/
680+
link_alt_odb_entries(r, gvfs_shared_cache_pathname.buf,
681+
'\n', NULL, 0);
682+
}
683+
684+
r->objects->loaded_alternates = 1;
610685
}
611686

612687
int odb_has_alternates(struct object_database *odb)

0 commit comments

Comments
 (0)