diff --git a/include/utilities.h b/include/utilities.h index 27ef475..44e9ca6 100644 --- a/include/utilities.h +++ b/include/utilities.h @@ -38,6 +38,11 @@ typedef struct { typedef List StringBuffer; typedef List FileInfoBuffer; +typedef struct { + uint32_t insertions; + uint32_t deletions; +} Changes; + List* list_new(size_t); int list_push(List*, void*); int list_remove(List*, const void*, int (*compare)(const void*, const void*)); @@ -64,9 +69,9 @@ PathType get_path_type(const char*); char* get_file_content(char*); char* get_file_content_with_size(char*, size_t*); int create_default_config_file(char*); -int save_diff(ActionList*, char*, char*, size_t, char*, File*, int, char*); +int save_diff(ActionList*, char*, char*, size_t, char*, File*, int); int read_diff(char*, char*, size_t, char*, ActionList*); -int save_file_diff(char*, char*, size_t, char*, char*); +int save_file_diff(char*, char*, size_t, char*, Changes*); File* apply_diff(File*, ActionList*); void sort_action_list(ActionList*); diff --git a/src/utilities.c b/src/utilities.c index 67c43e0..b65c54e 100644 --- a/src/utilities.c +++ b/src/utilities.c @@ -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;