Skip to content

Commit 18d612b

Browse files
committed
commit-graph: expire commit-graph files
As we merge commit-graph files in a commit-graph chain, we should clean up the files that are no longer used. This change introduces an 'expiry_window' value to the context, which is always zero (for now). We then check the modified time of each graph-{hash}.graph file in the $OBJDIR/info/commit-graphs folder and unlink the files that are older than the expiry_window. Since this is always zero, this immediately clears all unused graph files. We will update the value to match a config setting in a future change. Signed-off-by: Derrick Stolee <[email protected]>
1 parent 5cfd653 commit 18d612b

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

Documentation/technical/commit-graph.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,21 @@ The merge strategy values (2 for the size multiple, 64,000 for the maximum
266266
number of commits) could be extracted into config settings for full
267267
flexibility.
268268

269+
## Deleting graph-{hash} files
270+
271+
After a new tip file is written, some `graph-{hash}` files may no longer
272+
be part of a chain. It is important to remove these files from disk, eventually.
273+
The main reason to delay removal is that another process could read the
274+
`commit-graph-chain` file before it is rewritten, but then look for the
275+
`graph-{hash}` files after they are deleted.
276+
277+
To allow holding old split commit-graphs for a while after they are unreferenced,
278+
we update the modified times of the files when they become unreferenced. Then,
279+
we scan the `$OBJDIR/info/commit-graphs/` directory for `graph-{hash}`
280+
files whose modified times are older than a given expiry window. This window
281+
defaults to zero, but can be changed using command-line arguments or a config
282+
setting.
283+
269284
## Chains across multiple object directories
270285

271286
In a repo with alternates, we look for the `commit-graph-chain` file starting

commit-graph.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,6 +1637,70 @@ static void merge_commit_graphs(struct write_commit_graph_context *ctx)
16371637
deduplicate_commits(ctx);
16381638
}
16391639

1640+
static void mark_commit_graphs(struct write_commit_graph_context *ctx)
1641+
{
1642+
uint32_t i;
1643+
time_t now = time(NULL);
1644+
1645+
for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
1646+
struct stat st;
1647+
struct utimbuf updated_time;
1648+
1649+
stat(ctx->commit_graph_filenames_before[i], &st);
1650+
1651+
updated_time.actime = st.st_atime;
1652+
updated_time.modtime = now;
1653+
utime(ctx->commit_graph_filenames_before[i], &updated_time);
1654+
}
1655+
}
1656+
1657+
static void expire_commit_graphs(struct write_commit_graph_context *ctx)
1658+
{
1659+
struct strbuf path = STRBUF_INIT;
1660+
DIR *dir;
1661+
struct dirent *de;
1662+
size_t dirnamelen;
1663+
time_t expire_time = time(NULL);
1664+
1665+
strbuf_addstr(&path, ctx->obj_dir);
1666+
strbuf_addstr(&path, "/info/commit-graphs");
1667+
dir = opendir(path.buf);
1668+
1669+
if (!dir) {
1670+
strbuf_release(&path);
1671+
return;
1672+
}
1673+
1674+
strbuf_addch(&path, '/');
1675+
dirnamelen = path.len;
1676+
while ((de = readdir(dir)) != NULL) {
1677+
struct stat st;
1678+
uint32_t i, found = 0;
1679+
1680+
strbuf_setlen(&path, dirnamelen);
1681+
strbuf_addstr(&path, de->d_name);
1682+
1683+
stat(path.buf, &st);
1684+
1685+
if (st.st_mtime > expire_time)
1686+
continue;
1687+
if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
1688+
continue;
1689+
1690+
for (i = 0; i < ctx->num_commit_graphs_after; i++) {
1691+
if (!strcmp(ctx->commit_graph_filenames_after[i],
1692+
path.buf)) {
1693+
found = 1;
1694+
break;
1695+
}
1696+
}
1697+
1698+
if (!found)
1699+
unlink(path.buf);
1700+
1701+
}
1702+
}
1703+
16401704
int write_commit_graph(const char *obj_dir,
16411705
struct string_list *pack_indexes,
16421706
struct string_list *commit_hex,
@@ -1749,6 +1813,11 @@ int write_commit_graph(const char *obj_dir,
17491813

17501814
res = write_commit_graph_file(ctx);
17511815

1816+
if (ctx->split) {
1817+
mark_commit_graphs(ctx);
1818+
expire_commit_graphs(ctx);
1819+
}
1820+
17521821
cleanup:
17531822
free(ctx->graph_name);
17541823
free(ctx->commits.list);

t/t5323-split-commit-graph.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ test_expect_success 'add one commit, write a merged graph' '
141141
test_path_is_file $graphdir/commit-graph-chain &&
142142
test_line_count = 2 $graphdir/commit-graph-chain &&
143143
ls $graphdir/graph-*.graph >graph-files &&
144-
test_line_count = 4 graph-files &&
144+
test_line_count = 2 graph-files &&
145145
verify_chain_files_exist $graphdir
146146
'
147147

0 commit comments

Comments
 (0)