Skip to content

Commit 3769014

Browse files
lhchavezpersonalizedrefrigerator
authored andcommitted
commit-graph: Introduce git_commit_graph_needs_refresh()
This change introduces a function that allows the caller to know whether the `commit-graph` file has not been modified since it was parsed. Part of: libgit2#5757
1 parent 08aac8f commit 3769014

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

src/commit_graph.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,40 @@ static int git_commit_graph_entry_get_byindex(
333333
return 0;
334334
}
335335

336+
bool git_commit_graph_needs_refresh(const git_commit_graph_file *cgraph, const char *path)
337+
{
338+
git_file fd = -1;
339+
struct stat st;
340+
ssize_t bytes_read;
341+
git_oid cgraph_checksum = {{0}};
342+
343+
if (path == NULL)
344+
path = git_buf_cstr(&cgraph->filename);
345+
346+
/* TODO: properly open the file without access time using O_NOATIME */
347+
fd = git_futils_open_ro(path);
348+
if (fd < 0)
349+
return true;
350+
351+
if (p_fstat(fd, &st) < 0) {
352+
p_close(fd);
353+
return true;
354+
}
355+
356+
if (!S_ISREG(st.st_mode) || !git__is_sizet(st.st_size)
357+
|| (size_t)st.st_size != cgraph->graph_map.len) {
358+
p_close(fd);
359+
return true;
360+
}
361+
362+
bytes_read = p_pread(fd, cgraph_checksum.id, GIT_OID_RAWSZ, st.st_size - GIT_OID_RAWSZ);
363+
p_close(fd);
364+
if (bytes_read != GIT_OID_RAWSZ)
365+
return true;
366+
367+
return !git_oid_equal(&cgraph_checksum, &cgraph->checksum);
368+
}
369+
336370
int git_commit_graph_entry_find(
337371
git_commit_graph_entry *e,
338372
const git_commit_graph_file *cgraph,

src/commit_graph.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ typedef struct git_commit_graph_entry {
8989
} git_commit_graph_entry;
9090

9191
int git_commit_graph_open(git_commit_graph_file **cgraph_out, const char *path);
92+
93+
/*
94+
* Returns whether the commit_graph_file needs to be reloaded since the
95+
* contents of the commit-graph file have changed on disk. If `path` is NULL,
96+
* the filename stored in `cgraph` will be used.
97+
*/
98+
bool git_commit_graph_needs_refresh(const git_commit_graph_file *cgraph, const char *path);
99+
92100
int git_commit_graph_entry_find(
93101
git_commit_graph_entry *e,
94102
const git_commit_graph_file *cgraph,

tests/graph/commit_graph.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ void test_graph_commit_graph__parse(void)
1515
cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
1616
cl_git_pass(git_buf_joinpath(&commit_graph_path, git_repository_path(repo), "objects/info/commit-graph"));
1717
cl_git_pass(git_commit_graph_open(&cgraph, git_buf_cstr(&commit_graph_path)));
18+
cl_assert_equal_i(git_commit_graph_needs_refresh(cgraph, git_buf_cstr(&commit_graph_path)), 0);
1819

1920
cl_git_pass(git_oid_fromstr(&id, "5001298e0c09ad9c34e4249bc5801c75e9754fa5"));
2021
cl_git_pass(git_commit_graph_entry_find(&e, cgraph, &id, GIT_OID_HEXSZ));

0 commit comments

Comments
 (0)