Implement a new caching system to make repeated runs much much faster.

This commit is contained in:
Squibid 2025-11-14 04:02:16 -05:00
parent 4a008a82b0
commit 5e0e140b09
Signed by: squibid
GPG key ID: BECE5684D3C4005D
8 changed files with 252 additions and 44 deletions

132
hash.c Normal file
View file

@ -0,0 +1,132 @@
#include <git2/repository.h>
#include <git2/types.h>
#include <linux/limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <stdint.h>
#include <errno.h>
#include "hash.h"
#include "helpers.h"
#ifdef GITHASH
static uint64_t
murmur64(uint64_t k)
{
k ^= k >> 33;
k *= 0xff51afd7ed558ccdLLU;
k ^= k >> 33;
k *= 0xc4ceb9fe1a85ec53LLU;
k ^= k >> 33;
return k;
}
uint64_t
generate_hash(git_repository *repo)
{
struct stat dir, index;
char path[PATH_MAX] = { 0 };
const char *gitpath = git_repository_path(repo);
if (strlen(gitpath) + strlen("/..") > PATH_MAX - 1) {
L("strlen: %s", strerror(errno));
return -1;
}
strcat(path, gitpath);
if (stat(path, &index) < 0) {
return -1;
}
strcat(path, "/..");
if (stat(path, &dir) < 0) {
return -1;
}
return murmur64(dir.st_mtim.tv_nsec ^ index.st_mtim.tv_nsec);
}
repohash
*read_hash(git_repository *repo)
{
FILE *f;
uint64_t data;
uint8_t changes;
repohash *hash = malloc(sizeof(repohash));
char path[PATH_MAX] = { 0 };
const char *gitpath = git_repository_path(repo);
if (strlen(gitpath) + strlen(XD_HASH_PATH) > PATH_MAX - 1) {
L("strlen: %s", strerror(errno));
return NULL;
}
strcat(path, gitpath);
strcat(path, XD_HASH_PATH);
f = fopen(path, "r");
if (!f) {
L("fopen: %s", strerror(errno));
return NULL;
}
if (fread(&data, sizeof(uint64_t), 1, f) != 1) {
L("fread: %s", strerror(errno));
return NULL;
}
if (fseek(f, sizeof(uint64_t), SEEK_SET) < 0) {
L("fseek: %s", strerror(errno));
return NULL;
}
if (fread(&changes, sizeof(uint8_t), 1, f) != 1) {
L("fread: %s", strerror(errno));
return NULL;
}
fclose(f);
hash->hash = data;
hash->changes = changes;
return hash;
}
int
write_hash(git_repository *repo, repohash hash)
{
FILE *f;
char path[PATH_MAX] = { 0 };
const char *gitpath = git_repository_path(repo);
if (strlen(gitpath) + strlen(XD_HASH_PATH) > PATH_MAX - 1) {
L("strlen: %s", strerror(errno));
return 1;
}
strcat(path, gitpath);
strcat(path, XD_HASH_PATH);
f = fopen(path, "wb");
if (!f) {
L("fopen: %s", strerror(errno));
return 1;
}
if (fwrite(&hash.hash, sizeof(uint64_t), 1, f) != 1) {
L("fwrite: %s", strerror(errno));
return 1;
}
if (fseek(f, sizeof(uint64_t), SEEK_SET) < 0) {
L("fseek: %s", strerror(errno));
return 1;
}
if (fwrite(&hash.changes, sizeof(uint8_t), 1, f) != 1) {
L("fwrite: %s", strerror(errno));
return 1;
}
fclose(f);
return 0;
}
#endif