From fae8489871b036fd617ce869480bed99b83349ab Mon Sep 17 00:00:00 2001 From: lisk77 Date: Mon, 24 Nov 2025 18:21:09 +0100 Subject: [PATCH] fix(commit): make object hashes branch aware and thus dont error status out --- include/file.h | 2 +- include/object.h | 2 +- src/commands.c | 28 ++++++++++++++++++++++------ src/file.c | 16 ++++++++++++---- src/object.c | 12 +++++++++--- 5 files changed, 45 insertions(+), 15 deletions(-) diff --git a/include/file.h b/include/file.h index b4011e4..ede5628 100644 --- a/include/file.h +++ b/include/file.h @@ -55,7 +55,7 @@ File* slice_file(File*, uint64_t, uint64_t); File* copy_file(File* original); int insert_line(File*, char*, size_t); int delete_line(File*, size_t); -int snapshot_file(char*, char*, size_t, char*); +int snapshot_file(char*, char*, size_t, char*, char*); void free_file(File*); FileInfoBuffer* file_info_buffer_new(); int file_info_buffer_push(FileInfoBuffer*, FileInfo); diff --git a/include/object.h b/include/object.h index 7e92fa4..0af3e8a 100644 --- a/include/object.h +++ b/include/object.h @@ -28,7 +28,7 @@ char* get_object(char*, size_t*); void* parse_object(char*, ObjectType, size_t*, char*); -int save_diff(ActionList*, char*, char*, size_t, char*); +int save_diff(ActionList*, char*, char*, size_t, char*, char*); int read_diff(char*, char*, ActionList*); int save_file_diff(char*, char*, size_t, char*, ActionList*); File* apply_diff(File*, ActionList*); diff --git a/src/commands.c b/src/commands.c index f05c059..c6df995 100644 --- a/src/commands.c +++ b/src/commands.c @@ -420,7 +420,7 @@ int commit(int argc, char** argv) { changes->insertions += modified_file->lines; char file_hash[41]; - snapshot_file(files->items[idx], root, 0, file_hash); + snapshot_file(files->items[idx], root, 0, branch, file_hash); flat_map_put(file_hash_map, files->items[idx], file_hash); continue; } @@ -545,7 +545,7 @@ int commit(int argc, char** argv) { if (diff->len > 200) { char new_base_hash[41]; - snapshot_file(files->items[idx], root, base_file->base_num+1, new_base_hash); + snapshot_file(files->items[idx], root, base_file->base_num+1, branch, new_base_hash); flat_map_put(file_hash_map, files->items[idx], new_base_hash); @@ -559,12 +559,28 @@ int commit(int argc, char** argv) { continue; } else { - save_diff(diff, files->items[idx], root, base_file->diff_num, base_file_hash); + save_diff(diff, files->items[idx], root, base_file->diff_num, base_file_hash, branch); char diff_hash[41]; - char id[3 + snprintf(NULL, 0, "%lu", base_file->diff_num) + strlen(files->items[idx]) + strlen(branch)]; - snprintf(id, sizeof(id), "%s %lu %s", (char*)files->items[idx], base_file->diff_num, branch); - object_hash(BaseFileObject, id, diff_hash); + size_t id_len = snprintf(NULL, 0, "%s %zu %s", (char*)files->items[idx], base_file->diff_num, branch) + 1; + char* id = calloc(id_len, sizeof(char)); + if (!id) { + free_action_list(diff); + free_file(modified_file); + free_file(basefile); + free(commit_message); + list_free(files); + base_file_buffer_free(base_files); + free(branch); + free(root); + free_config(&config); + free(changes); + return 1; + } + + snprintf(id, id_len, "%s %zu %s", (char*)files->items[idx], base_file->diff_num, branch); + object_hash(FileDiffObject, id, diff_hash); + free(id); flat_map_put(file_hash_map, files->items[idx], diff_hash); diff --git a/src/file.c b/src/file.c index d98e964..d41589e 100644 --- a/src/file.c +++ b/src/file.c @@ -181,7 +181,7 @@ int delete_line(File* file, size_t idx) { return 1; } -int snapshot_file(char* path, char* root, size_t basefile_id, char* hash) { +int snapshot_file(char* path, char* root, size_t basefile_id, char* branch, char* hash) { File* file = new_file(path); if (!file) return 1; @@ -221,9 +221,17 @@ int snapshot_file(char* path, char* root, size_t basefile_id, char* hash) { free(concat_file); concat_file = NULL; - char id[2 + snprintf(NULL, 0, "%d", basefile_id) + strlen(path)]; - snprintf(id, sizeof(id), "%s %d", path, basefile_id); + size_t id_len = snprintf(NULL, 0, "%s %zu %s", path, basefile_id, branch) + 1; + char* id = calloc(id_len, sizeof(char)); + if (!id) { + free(final_content); + free_file(file); + return 0; + } + + snprintf(id, id_len, "%s %zu %s", path, basefile_id, branch); object_hash(BaseFileObject, id, hash); + free(id); char dir_path[PATH_MAX]; char file_path[PATH_MAX]; @@ -323,4 +331,4 @@ FileInfo* file_info_buffer_search(FileInfoBuffer* buffer, const char* filename) FileInfo search_key = {.mode = 0, .name = (char*)filename}; return (FileInfo*)list_binary_search(buffer, &search_key, compare_file_info); -} \ No newline at end of file +} diff --git a/src/object.c b/src/object.c index e4ed47a..2284d4a 100644 --- a/src/object.c +++ b/src/object.c @@ -152,7 +152,7 @@ static int mkdir_recursive(const char *path, mode_t mode) { return 0; } -int save_diff(ActionList* diff, char* path, char* root, size_t diff_id, char* basefile_hash) { +int save_diff(ActionList* diff, char* path, char* root, size_t diff_id, char* basefile_hash, char* branch) { size_t buffer_size = 41; buffer_size += 1 + snprintf(NULL, 0, "%d", diff->len); @@ -204,10 +204,16 @@ 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); + size_t id_len = snprintf(NULL, 0, "%s %zu %s", path, diff_id, branch) + 1; + char* id = calloc(id_len, sizeof(char)); + if (!id) { + return 0; + } + + snprintf(id, id_len, "%s %zu %s", path, diff_id, branch); char hash[41]; object_hash(FileDiffObject, id, hash); + free(id); char dir_path[PATH_MAX]; char file_path[PATH_MAX];