From cba5d43039d5db5496fd013452bf151b878995c6 Mon Sep 17 00:00:00 2001 From: lisk77 Date: Sat, 20 Sep 2025 16:53:20 +0200 Subject: [PATCH] feat(cli): added proper display of modified files and the changes on the current branch for the status command --- src/main.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/src/main.c b/src/main.c index ca67488..90785a3 100644 --- a/src/main.c +++ b/src/main.c @@ -162,12 +162,61 @@ int main(int argc, char** argv) { StringBuffer* deleted_files = string_buffer_new(); StringBuffer* untracked_files = string_buffer_new(); + Changes* changes = calloc(1, sizeof(Changes)); + if (!changes) { + printf("ERROR: unable to allocate memory for changes!\n"); + return 1; + } + + changes->insertions = 0; + changes->deletions = 0; + for (size_t idx = 0; idx < files->len; idx++) { char* file_name = ((FileInfo*)files->items + idx)->name; if (base_file_buffer_search(tracked_list, file_name)) continue; string_buffer_push(untracked_files, file_name); } + for (size_t idx = 0; idx < files->len; idx++) { + char* file_name = ((FileInfo*)files->items + idx)->name; + if (base_file_buffer_search(tracked_list, file_name)) { + BaseFileInfo* base_file = base_file_buffer_search(tracked_list, file_name); + char base_file_hash[41]; + char id[2 + snprintf(NULL, 0, "%d", base_file->base_num) + strlen(file_name)]; + snprintf(id, sizeof(id), "%s %d", file_name, base_file->base_num); + object_hash(BaseFileObject, id, base_file_hash); + File* basefile = parse_object(base_file_hash, BaseFileObject, NULL, NULL); + if (!basefile) { + printf("ERROR: unable to parse base file %s!\n", file_name); + return 1; + } + + File* modified_file = new_file(file_name); + if (!modified_file) { + printf("ERROR: unable to read modified file %s!\n", file_name); + return 1; + } + + ActionList* diff = myers_diff(basefile, modified_file, 0, 0); + if (!diff) { + printf("ERROR: unable to compute diff for file %s!\n", file_name); + return 1; + } + + if (diff->len != 0) { + for (size_t idx = 0; idx < diff->len; idx++) { + Action action = diff->actions[idx]; + if (action.type == DELETE) { + changes->deletions++; + } else if (action.type == INSERT) { + changes->insertions++; + } + } + string_buffer_push(modified_files, file_name); + } + } + } + for (size_t idx = 0; idx < tracked_list->len; idx++) { char* file_name = ((BaseFileInfo*)tracked_list->items + idx)->name; if (file_info_buffer_search(files, file_name)) continue; @@ -178,19 +227,17 @@ int main(int argc, char** argv) { printf("\x1b[36;1mM\x1b[0m \x1b[36m%s\x1b[0m\n", modified_files->items[idx]); } - printf("\n"); - for (size_t idx = 0; idx < deleted_files->len; idx++) { printf("\x1b[31;4mD\x1b[0m \x1b[31;9m%s\x1b[0m\n", deleted_files->items[idx]); } - if (deleted_files->len != 0) printf("\n"); + if (modified_files->len != 0 || deleted_files->len != 0) printf("\n"); for (size_t idx = 0; idx < untracked_files->len; idx++) { printf("\x1b[31;1mU\x1b[0m \x1b[31m%s\x1b[0m\n", untracked_files->items[idx]); } - printf("\nOn branch \x1b[39;1;4m%s\x1b[0m with %d commits\n", branch, log->len); + printf("\n\x1b[32;1m%d+\x1b[0m \x1b[31;1m%d-\x1b[0m on branch \x1b[39;1;4m%s\x1b[0m with %d commits\n", changes->insertions, changes->deletions, branch, log->len); file_info_buffer_free(files); free(root);