diff options
author | lhchavez <lhchavez@lhchavez.com> | 2021-01-05 06:24:26 -0800 |
---|---|---|
committer | lhchavez <lhchavez@lhchavez.com> | 2021-01-10 11:18:38 -0800 |
commit | 1a2f960907ebeeb703c85c5d2b61de8dbd69a1b5 (patch) | |
tree | f6e58d86a36d986d07d7e837676294147a69b3a2 /src/commit_graph.c | |
parent | 1f32ed25ee6f5ead60fff8cf5ba544ef2d567fe0 (diff) | |
download | libgit2-1a2f960907ebeeb703c85c5d2b61de8dbd69a1b5.tar.gz |
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: #5757
Diffstat (limited to 'src/commit_graph.c')
-rw-r--r-- | src/commit_graph.c | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/commit_graph.c b/src/commit_graph.c index 9740418e2..b301d3d49 100644 --- a/src/commit_graph.c +++ b/src/commit_graph.c @@ -333,6 +333,40 @@ static int git_commit_graph_entry_get_byindex( return 0; } +bool git_commit_graph_needs_refresh(const git_commit_graph_file *cgraph, const char *path) +{ + git_file fd = -1; + struct stat st; + ssize_t bytes_read; + git_oid cgraph_checksum = {{0}}; + + if (path == NULL) + path = git_buf_cstr(&cgraph->filename); + + /* TODO: properly open the file without access time using O_NOATIME */ + fd = git_futils_open_ro(path); + if (fd < 0) + return true; + + if (p_fstat(fd, &st) < 0) { + p_close(fd); + return true; + } + + if (!S_ISREG(st.st_mode) || !git__is_sizet(st.st_size) + || (size_t)st.st_size != cgraph->graph_map.len) { + p_close(fd); + return true; + } + + bytes_read = p_pread(fd, cgraph_checksum.id, GIT_OID_RAWSZ, st.st_size - GIT_OID_RAWSZ); + p_close(fd); + if (bytes_read != GIT_OID_RAWSZ) + return true; + + return !git_oid_equal(&cgraph_checksum, &cgraph->checksum); +} + int git_commit_graph_entry_find( git_commit_graph_entry *e, const git_commit_graph_file *cgraph, |