Skip to content

Commit c1d99ae

Browse files
mjcheethamdscho
authored andcommitted
maintenance: add new cache-local-objects maintenance task (#720)
Introduce a new maintenance task, `cache-local-objects`, that operates on Scalar or VFS for Git repositories with a per-volume, shared object cache (specified by `gvfs.sharedCache`) to migrate packfiles and loose objects from the repository object directory to the shared cache. Older versions of `microsoft/git` incorrectly placed packfiles in the repository object directory instead of the shared cache; this task will help clean up existing clones impacted by that issue. Fixes #716
2 parents 13d0122 + bcc01b2 commit c1d99ae

4 files changed

Lines changed: 330 additions & 0 deletions

File tree

Documentation/git-maintenance.adoc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ task:
6969
* `prefetch`: hourly.
7070
* `loose-objects`: daily.
7171
* `incremental-repack`: daily.
72+
* `cache-local-objects`: weekly.
7273
--
7374
+
7475
`git maintenance register` will also disable foreground maintenance by
@@ -174,6 +175,13 @@ worktree-prune::
174175
The `worktree-prune` task deletes stale or broken worktrees. See
175176
linkgit:git-worktree[1] for more information.
176177

178+
cache-local-objects::
179+
The `cache-local-objects` task only operates on Scalar or VFS for Git
180+
repositories (cloned with either `scalar clone` or `gvfs clone`) that
181+
have the `gvfs.sharedCache` configuration setting present. This task
182+
migrates pack files and loose objects from the repository's object
183+
directory in to the shared volume cache.
184+
177185
OPTIONS
178186
-------
179187
--auto::

builtin/gc.c

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
#define USE_THE_REPOSITORY_VARIABLE
1414
#define DISABLE_SIGN_COMPARE_WARNINGS
1515

16+
#include "git-compat-util.h"
1617
#include "builtin.h"
1718
#include "abspath.h"
19+
#include "copy.h"
1820
#include "date.h"
1921
#include "dir.h"
2022
#include "environment.h"
@@ -263,6 +265,7 @@ enum maintenance_task_label {
263265
TASK_REFLOG_EXPIRE,
264266
TASK_WORKTREE_PRUNE,
265267
TASK_RERERE_GC,
268+
TASK_CACHE_LOCAL_OBJS,
266269

267270
/* Leave as final value */
268271
TASK__COUNT
@@ -1696,6 +1699,186 @@ static int geometric_repack_auto_condition(struct gc_config *cfg UNUSED)
16961699
return ret;
16971700
}
16981701

1702+
static void link_or_copy_or_die(const char *src, const char *dst)
1703+
{
1704+
if (!link(src, dst))
1705+
return;
1706+
1707+
/* Use copy operation if src and dst are on different file systems. */
1708+
if (errno != EXDEV)
1709+
warning_errno(_("failed to link '%s' to '%s'"), src, dst);
1710+
1711+
if (copy_file(dst, src, 0444))
1712+
die_errno(_("failed to copy '%s' to '%s'"), src, dst);
1713+
}
1714+
1715+
static void rename_or_copy_or_die(const char *src, const char *dst)
1716+
{
1717+
if (!rename(src, dst))
1718+
return;
1719+
1720+
/* Use copy and delete if src and dst are on different file systems. */
1721+
if (errno != EXDEV)
1722+
warning_errno(_("failed to move '%s' to '%s'"), src, dst);
1723+
1724+
if (copy_file(dst, src, 0444))
1725+
die_errno(_("failed to copy '%s' to '%s'"), src, dst);
1726+
1727+
if (unlink(src))
1728+
die_errno(_("failed to delete '%s'"), src);
1729+
}
1730+
1731+
static void migrate_pack(const char *srcdir, const char *dstdir,
1732+
const char *pack_filename)
1733+
{
1734+
size_t basenamelen, srclen, dstlen;
1735+
struct strbuf src = STRBUF_INIT, dst = STRBUF_INIT;
1736+
struct {
1737+
const char *ext;
1738+
unsigned move:1;
1739+
} files[] = {
1740+
{".pack", 0},
1741+
{".keep", 0},
1742+
{".rev", 0},
1743+
{".idx", 1}, /* The index file must be atomically moved last. */
1744+
};
1745+
1746+
trace2_region_enter("maintenance", "migrate_pack", the_repository);
1747+
1748+
basenamelen = strlen(pack_filename) - 5; /* .pack */
1749+
strbuf_addstr(&src, srcdir);
1750+
strbuf_addch(&src, '/');
1751+
strbuf_add(&src, pack_filename, basenamelen);
1752+
strbuf_addstr(&src, ".idx");
1753+
1754+
/* A pack without an index file is not yet ready to be migrated. */
1755+
if (!file_exists(src.buf))
1756+
goto cleanup;
1757+
1758+
strbuf_setlen(&src, src.len - 4 /* .idx */);
1759+
strbuf_addstr(&dst, dstdir);
1760+
strbuf_addch(&dst, '/');
1761+
strbuf_add(&dst, pack_filename, basenamelen);
1762+
1763+
srclen = src.len;
1764+
dstlen = dst.len;
1765+
1766+
/* Move or copy files from the source directory to the destination. */
1767+
for (size_t i = 0; i < ARRAY_SIZE(files); i++) {
1768+
strbuf_setlen(&src, srclen);
1769+
strbuf_addstr(&src, files[i].ext);
1770+
1771+
if (!file_exists(src.buf))
1772+
continue;
1773+
1774+
strbuf_setlen(&dst, dstlen);
1775+
strbuf_addstr(&dst, files[i].ext);
1776+
1777+
if (files[i].move)
1778+
rename_or_copy_or_die(src.buf, dst.buf);
1779+
else
1780+
link_or_copy_or_die(src.buf, dst.buf);
1781+
}
1782+
1783+
/*
1784+
* Now the pack and all associated files exist at the destination we can
1785+
* now clean up the files in the source directory.
1786+
*/
1787+
for (size_t i = 0; i < ARRAY_SIZE(files); i++) {
1788+
/* Files that were moved rather than copied have no clean up. */
1789+
if (files[i].move)
1790+
continue;
1791+
1792+
strbuf_setlen(&src, srclen);
1793+
strbuf_addstr(&src, files[i].ext);
1794+
1795+
/* Files that never existed in originally have no clean up.*/
1796+
if (!file_exists(src.buf))
1797+
continue;
1798+
1799+
if (unlink(src.buf))
1800+
warning_errno(_("failed to delete '%s'"), src.buf);
1801+
}
1802+
1803+
cleanup:
1804+
strbuf_release(&src);
1805+
strbuf_release(&dst);
1806+
1807+
trace2_region_leave("maintenance", "migrate_pack", the_repository);
1808+
}
1809+
1810+
static void move_pack_to_shared_cache(const char *full_path, size_t full_path_len,
1811+
const char *file_name, void *data)
1812+
{
1813+
char *srcdir;
1814+
const char *dstdir = (const char *)data;
1815+
1816+
/* We only care about the actual pack files here.
1817+
* The associated .idx, .keep, .rev files will be copied in tandem
1818+
* with the pack file, with the index file being moved last.
1819+
* The original locations of the non-index files will only deleted
1820+
* once all other files have been copied/moved.
1821+
*/
1822+
if (!ends_with(file_name, ".pack"))
1823+
return;
1824+
1825+
srcdir = xstrndup(full_path, full_path_len - strlen(file_name) - 1);
1826+
1827+
migrate_pack(srcdir, dstdir, file_name);
1828+
1829+
free(srcdir);
1830+
}
1831+
1832+
static int move_loose_object_to_shared_cache(const struct object_id *oid,
1833+
const char *path,
1834+
UNUSED void *data)
1835+
{
1836+
struct stat st;
1837+
struct strbuf dst = STRBUF_INIT;
1838+
char *hex = oid_to_hex(oid);
1839+
1840+
strbuf_addf(&dst, "%s/%.2s/", shared_object_dir, hex);
1841+
1842+
if (stat(dst.buf, &st)) {
1843+
if (mkdir(dst.buf, 0777))
1844+
die_errno(_("failed to create directory '%s'"), dst.buf);
1845+
} else if (!S_ISDIR(st.st_mode))
1846+
die(_("expected '%s' to be a directory"), dst.buf);
1847+
1848+
strbuf_addstr(&dst, hex+2);
1849+
rename_or_copy_or_die(path, dst.buf);
1850+
1851+
strbuf_release(&dst);
1852+
return 0;
1853+
}
1854+
1855+
static int maintenance_task_cache_local_objs(UNUSED struct maintenance_run_opts *opts,
1856+
UNUSED struct gc_config *cfg)
1857+
{
1858+
struct strbuf dstdir = STRBUF_INIT;
1859+
struct repository *r = the_repository;
1860+
1861+
/* This task is only applicable with a VFS/Scalar shared cache. */
1862+
if (!shared_object_dir)
1863+
return 0;
1864+
1865+
/* If the dest is the same as the local odb path then we do nothing. */
1866+
if (!fspathcmp(r->objects->sources->path, shared_object_dir))
1867+
goto cleanup;
1868+
1869+
strbuf_addf(&dstdir, "%s/pack", shared_object_dir);
1870+
1871+
for_each_file_in_pack_dir(r->objects->sources->path, move_pack_to_shared_cache,
1872+
dstdir.buf);
1873+
1874+
for_each_loose_object(r->objects, move_loose_object_to_shared_cache, NULL,
1875+
FOR_EACH_OBJECT_LOCAL_ONLY);
1876+
1877+
cleanup:
1878+
strbuf_release(&dstdir);
1879+
return 0;
1880+
}
1881+
16991882
typedef int (*maintenance_task_fn)(struct maintenance_run_opts *opts,
17001883
struct gc_config *cfg);
17011884
typedef int (*maintenance_auto_fn)(struct gc_config *cfg);
@@ -1774,6 +1957,10 @@ static const struct maintenance_task tasks[] = {
17741957
.background = maintenance_task_rerere_gc,
17751958
.auto_condition = rerere_gc_condition,
17761959
},
1960+
[TASK_CACHE_LOCAL_OBJS] = {
1961+
"cache-local-objects",
1962+
maintenance_task_cache_local_objs,
1963+
},
17771964
};
17781965

17791966
enum task_phase {
@@ -1900,6 +2087,10 @@ static const struct maintenance_strategy incremental_strategy = {
19002087
.type = MAINTENANCE_TYPE_SCHEDULED,
19012088
.schedule = SCHEDULE_WEEKLY,
19022089
},
2090+
[TASK_CACHE_LOCAL_OBJS] = {
2091+
.type = MAINTENANCE_TYPE_SCHEDULED,
2092+
.schedule = SCHEDULE_WEEKLY,
2093+
},
19032094
/*
19042095
* Historically, the "incremental" strategy was only available
19052096
* in the context of scheduled maintenance when set up via

scalar.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,7 @@ static int cmd_run(int argc, const char **argv)
12441244
{ "fetch", "prefetch" },
12451245
{ "loose-objects", "loose-objects" },
12461246
{ "pack-files", "incremental-repack" },
1247+
{ "cache-local-objects", "cache-local-objects" },
12471248
{ NULL, NULL }
12481249
};
12491250
struct strbuf buf = STRBUF_INIT;

0 commit comments

Comments
 (0)