fix(utilities): made save diff accept a changes struct to count deletions and insertions

This commit is contained in:
lisk77 2025-09-18 23:59:54 +02:00
parent 550b47d568
commit 8831b267e5
2 changed files with 21 additions and 9 deletions

View file

@ -591,7 +591,7 @@ int create_default_config_file(char* config_path) {
return 0;
}
int save_diff(ActionList* diff, char* path, char* root, size_t diff_id, char* basefile_hash, File* modified_file, int tree, char* hash) {
int save_diff(ActionList* diff, char* path, char* root, size_t diff_id, char* basefile_hash, File* modified_file, int tree) {
size_t buffer_size = 41;
buffer_size += 1 + snprintf(NULL, 0, "%d", diff->len);
@ -645,6 +645,7 @@ int save_diff(ActionList* diff, char* path, char* root, size_t diff_id, char* ba
char id[2+snprintf(NULL, 0, "%d", diff_id)+strlen(path)];
snprintf(id, sizeof(id), "%s %d", path, diff_id);
char hash[41];
if (tree) object_hash(TreeDiffObject, id, hash);
else object_hash(FileDiffObject, id, hash);
@ -664,9 +665,6 @@ int save_diff(ActionList* diff, char* path, char* root, size_t diff_id, char* ba
return 0;
}
// Check if the file already exists
// Ensure the directory structure exists
if (mkdir_recursive(dir_path, 0755) < 0 && errno != EEXIST) {
perror("ERROR: failed to create directory structure in save_diff!");
@ -1012,7 +1010,7 @@ int read_diff(char* path, char* root, size_t diff_id, char* basefile_hash, Actio
}
int save_file_diff(char* path, char* root, size_t diff_id, char* basefile_hash, char* hash) {
int save_file_diff(char* path, char* root, size_t diff_id, char* basefile_hash, Changes* changes) {
char basefile_location[2+strlen(root)+strlen("/.merk/objects/")+strlen(basefile_hash)];
snprintf(basefile_location, sizeof(basefile_location), "%s/.merk/objects/%.2s/%s", root, basefile_hash, basefile_hash+2);
@ -1078,12 +1076,21 @@ int save_file_diff(char* path, char* root, size_t diff_id, char* basefile_hash,
ActionList* diff = myers_diff(basefile, modified_file, 0, 0);
if (diff->len == 0) {
hash = NULL;
free(basefile_content);
return 1;
}
save_diff(diff, path, root, diff_id, basefile_hash, modified_file, 0, hash);
for (size_t idx = 0; idx < diff->len; idx++) {
Action action = diff->actions[idx];
if (action.type == INSERT) {
changes->insertions += 1;
} else if (action.type == DELETE) {
changes->deletions += 1;
}
}
save_diff(diff, path, root, diff_id, basefile_hash, modified_file, 0);
free(basefile_content);
return 1;