Skip to content

Commit 7197335

Browse files
committed
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 e20ae3e commit 7197335

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

src/commit_graph.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,37 @@ 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+
/* TODO: properly open the file without access time using O_NOATIME */
344+
fd = git_futils_open_ro(path);
345+
if (fd < 0)
346+
return true;
347+
348+
if (p_fstat(fd, &st) < 0) {
349+
p_close(fd);
350+
return true;
351+
}
352+
353+
if (!S_ISREG(st.st_mode) || !git__is_sizet(st.st_size)
354+
|| (size_t)st.st_size != cgraph->graph_map.len) {
355+
p_close(fd);
356+
return true;
357+
}
358+
359+
bytes_read = p_pread(fd, cgraph_checksum.id, GIT_OID_RAWSZ, st.st_size - GIT_OID_RAWSZ);
360+
p_close(fd);
361+
if (bytes_read != GIT_OID_RAWSZ)
362+
return true;
363+
364+
return !git_oid_equal(&cgraph_checksum, &cgraph->checksum);
365+
}
366+
336367
int git_commit_graph_entry_find(
337368
git_commit_graph_entry *e,
338369
const git_commit_graph_file *cgraph,

src/commit_graph.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ typedef struct git_commit_graph_entry {
8888
} git_commit_graph_entry;
8989

9090
int git_commit_graph_open(git_commit_graph_file **cgraph_out, const char *path);
91+
bool git_commit_graph_needs_refresh(const git_commit_graph_file *cgraph, const char *path);
9192
int git_commit_graph_entry_find(
9293
git_commit_graph_entry *e,
9394
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)