Skip to content

Commit e9dcf57

Browse files
committed
gc.c: extend vfs-cache-move to migrate loose objs
Extend the `vfs-cache-move` maintenance task to also migrate any loose objects in the local ODB directory to the shared volume cache for VFS for Git/Scalar (if set). Signed-off-by: Matthew John Cheetham <[email protected]>
1 parent e086b1a commit e9dcf57

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

Documentation/git-maintenance.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ vfs-cache-move::
163163
The `vfs-cache-move` task only operates on Scalar or VFS for Git
164164
repositories (cloned with either `scalar clone` or `gvfs clone`) that
165165
have the `gvfs.sharedCache` configuration setting present. This task
166-
migrates pack files from the repository's object directory in to the
167-
shared volume cache.
166+
migrates pack files and loose objects from the repository's object
167+
directory in to the shared volume cache.
168168

169169
OPTIONS
170170
-------

builtin/gc.c

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,16 +1475,34 @@ static void move_pack_to_vfs_cache(const char *full_path, size_t full_path_len,
14751475

14761476
srcdir = xstrndup(full_path, full_path_len - strlen(file_name) - 1);
14771477

1478-
/* Same source + destination means there's no work to do. */
1479-
if (!fspathcmp(srcdir, vfs_object_dir))
1480-
goto cleanup;
1481-
14821478
migrate_pack(srcdir, dstdir, file_name);
14831479

1484-
cleanup:
14851480
free(srcdir);
14861481
}
14871482

1483+
static int move_loose_object_to_vfs_cache(const struct object_id *oid,
1484+
const char *path,
1485+
UNUSED void *data)
1486+
{
1487+
struct stat st;
1488+
struct strbuf dst = STRBUF_INIT;
1489+
char *hex = oid_to_hex(oid);
1490+
1491+
strbuf_addf(&dst, "%s/%.2s/", vfs_object_dir, hex);
1492+
1493+
if (stat(dst.buf, &st)) {
1494+
if (mkdir(dst.buf, 0777))
1495+
die_errno(_("failed to create directory '%s'"), dst.buf);
1496+
} else if (!S_ISDIR(st.st_mode))
1497+
die(_("expected '%s' to be a directory"), dst.buf);
1498+
1499+
strbuf_addstr(&dst, hex+2);
1500+
rename_or_copy_or_die(path, dst.buf);
1501+
1502+
strbuf_release(&dst);
1503+
return 0;
1504+
}
1505+
14881506
static int maintenance_task_vfs_cache_move(UNUSED struct maintenance_run_opts *opts,
14891507
UNUSED struct gc_config *cfg)
14901508
{
@@ -1495,11 +1513,19 @@ static int maintenance_task_vfs_cache_move(UNUSED struct maintenance_run_opts *o
14951513
if (!vfs_object_dir)
14961514
return 0;
14971515

1516+
/* If the dest is the same as the local odb path then we do nothing. */
1517+
if (!fspathcmp(r->objects->odb->path, vfs_object_dir))
1518+
goto cleanup;
1519+
14981520
strbuf_addf(&dstdir, "%s/pack", vfs_object_dir);
14991521

15001522
for_each_file_in_pack_dir(r->objects->odb->path, move_pack_to_vfs_cache,
15011523
dstdir.buf);
15021524

1525+
for_each_loose_object(move_loose_object_to_vfs_cache, NULL,
1526+
FOR_EACH_OBJECT_LOCAL_ONLY);
1527+
1528+
cleanup:
15031529
strbuf_release(&dstdir);
15041530
return 0;
15051531
}

t/t7900-maintenance.sh

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ test_get_packdir_files() {
4747
fi
4848
}
4949

50+
test_get_loose_object_files () {
51+
find .git/objects -type f -path '.git/objects/??/*'
52+
}
53+
5054
test_expect_success 'help text' '
5155
test_expect_code 129 git maintenance -h >actual &&
5256
test_grep "usage: git maintenance <subcommand>" actual &&
@@ -1041,6 +1045,7 @@ test_expect_success 'vfs-cache-move task with no shared cache no op' '
10411045
test_import_packfile &&
10421046
test_get_packdir_files "*.pack" "*.idx" "*.keep" "*.rev" \
10431047
>files.txt &&
1048+
test_get_loose_object_files >>files.txt &&
10441049
10451050
git maintenance run &&
10461051
while IFS= read -r f; do
@@ -1049,21 +1054,22 @@ test_expect_success 'vfs-cache-move task with no shared cache no op' '
10491054
)
10501055
'
10511056

1052-
test_expect_success 'vfs-cache-move task cache path same as pack dir no op' '
1057+
test_expect_success 'vfs-cache-move task cache path same as local odb no op' '
10531058
test_when_finished "rm -rf repo" &&
10541059
git init repo &&
10551060
(
10561061
cd repo &&
10571062
10581063
test_commit something &&
1059-
git config set gvfs.sharedcache .git/objects/pack &&
1064+
git config set gvfs.sharedcache .git/objects &&
10601065
git config set maintenance.gc.enabled false &&
10611066
git config set maintenance.vfs-cache-move.enabled true &&
10621067
git config set maintenance.vfs-cache-move.auto 1 &&
10631068
10641069
test_import_packfile &&
10651070
test_get_packdir_files "*.pack" "*.idx" "*.keep" "*.rev" \
10661071
>files.txt &&
1072+
test_get_loose_object_files >>files.txt &&
10671073
10681074
git maintenance run &&
10691075
while IFS= read -r f; do
@@ -1087,10 +1093,11 @@ test_expect_success 'vfs-cache-move task no .rev or .keep' '
10871093
10881094
test_import_packfile &&
10891095
test_get_packdir_files "*.pack" "*.idx" >src.txt &&
1096+
test_get_loose_object_files >>src.txt &&
10901097
10911098
rm -f .git/objects/pack/*.rev .git/objects/pack/*.keep &&
10921099
1093-
sed "s/.*\\//..\\/cache\\/pack\\//" src.txt >dst.txt &&
1100+
sed "s/.git\\/objects\\//..\\/cache\\//" src.txt >dst.txt &&
10941101
10951102
git maintenance run &&
10961103
while IFS= read -r f; do
@@ -1119,8 +1126,9 @@ test_expect_success 'vfs-cache-move task success' '
11191126
test_import_packfile &&
11201127
test_get_packdir_files "*.pack" "*.idx" "*.keep" "*.rev" \
11211128
>src.txt &&
1129+
test_get_loose_object_files >>src.txt &&
11221130
1123-
sed "s/.*\\//..\\/cache\\/pack\\//" src.txt >dst.txt &&
1131+
sed "s/.git\\/objects\\//..\\/cache\\//" src.txt >dst.txt &&
11241132
11251133
git maintenance run &&
11261134
while IFS= read -r f; do

0 commit comments

Comments
 (0)