From 0e0d1fa1efdb84bc005cbba20a98c8f5a832da3b Mon Sep 17 00:00:00 2001 From: lisk77 Date: Sun, 21 Sep 2025 00:13:39 +0200 Subject: [PATCH] feat(cli): added a visual branch for the log --- src/main.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index 90785a3..9340dda 100644 --- a/src/main.c +++ b/src/main.c @@ -197,7 +197,27 @@ int main(int argc, char** argv) { return 1; } - ActionList* diff = myers_diff(basefile, modified_file, 0, 0); + File* comparison_file = basefile; + + // If there are previous diffs, we need to compare against the last committed version + if (base_file->diff_num > 0) { + size_t prev_diff = base_file->diff_num - 1; + char prev_diff_hash[41]; + char id2[2 + snprintf(NULL, 0, "%zu", prev_diff) + strlen(file_name)]; + snprintf(id2, sizeof(id2), "%s %zu", file_name, prev_diff); + object_hash(FileDiffObject, id2, prev_diff_hash); + char hash[41]; + ActionList* last_diff = parse_object(prev_diff_hash, FileDiffObject, NULL, hash); + if (last_diff) { + File* last_version = apply_diff(basefile, last_diff); + if (last_version) { + comparison_file = last_version; + } + free_action_list(last_diff); + } + } + + ActionList* diff = myers_diff(comparison_file, modified_file, 0, 0); if (!diff) { printf("ERROR: unable to compute diff for file %s!\n", file_name); return 1; @@ -214,6 +234,13 @@ int main(int argc, char** argv) { } string_buffer_push(modified_files, file_name); } + + free_action_list(diff); + free_file(modified_file); + if (comparison_file != basefile) { + free_file(comparison_file); + } + free_file(basefile); } } @@ -728,13 +755,30 @@ int main(int argc, char** argv) { read_commit_log(log, log_path); + char ref_path[PATH_MAX]; + snprintf(ref_path, sizeof(ref_path), "%s/.merk/refs/branches/%s", root, branch); + + int ref = open(ref_path, O_RDONLY); + if (ref < 0) { + free(branch); + free(root); + commit_log_free(log); + return -1; + } + char head[41] = ""; + read(ref, head, 40); + close(ref); + for (size_t i = log->len; i != 0; i--) { Commit* commit = (Commit*)log->items + i - 1; char human_readable_time[32]; time_t timestamp = atol(commit->committer.timestamp); strftime(human_readable_time, sizeof(human_readable_time), "%Y-%m-%d %H:%M:%S %z", localtime(×tamp)); - printf("\x1b[33;1m%.7s\x1b[0m: %s\n%s <\x1b[38;5;240m\x1b]8;;mailto:%s\x1b\\%s\x1b]8;;\x1b\\\x1b[0m> (\x1b[36m%s\x1b[0m)\n\n", + char* marker = (strcmp(commit->hash, head) == 0) ? "\x1b[32m@\x1b[0m" : "\x1b[34m◯\x1b[0m"; + + printf("%s \x1b[33;1m%.7s\x1b[0m: %s\n│ %s <\x1b[38;5;240m\x1b]8;;mailto:%s\x1b\\%s\x1b]8;;\x1b\\\x1b[0m> (\x1b[36m%s\x1b[0m)\n│\n", + marker, commit->hash, commit->message, commit->committer.name, @@ -743,6 +787,7 @@ int main(int argc, char** argv) { human_readable_time ); } + if (log->len != 0) printf("┴\n"); free(branch); free(root);