diff --git a/src/utilities.c b/src/utilities.c index b65c54e..3e759ab 100644 --- a/src/utilities.c +++ b/src/utilities.c @@ -1102,6 +1102,8 @@ File* apply_diff(File* basefile, ActionList* diff) { File* copy = copy_file(basefile); if (!copy) return NULL; + sort_action_list(diff); + for (size_t i = 0; i < diff->len; i++) { Action* action = &diff->actions[i]; int success = 0; @@ -1117,9 +1119,9 @@ File* apply_diff(File* basefile, ActionList* diff) { break; case DELETE: - success = delete_line(copy, action->line_changed); + success = delete_line(copy, action->line_original); if (!success) { - fprintf(stderr, "ERROR: Failed to delete line at index %zu\n", action->line_changed); + fprintf(stderr, "ERROR: Failed to delete line at index %zu\n", action->line_original); free_file(copy); return NULL; } @@ -1143,9 +1145,18 @@ int compare_actions(const void* a, const void* b) { return (action1->type == DELETE) ? -1 : 1; } - return 0; + if (action1->type == DELETE) { + if (action1->line_original < action2->line_original) return 1; + if (action1->line_original > action2->line_original) return -1; + return 0; + } else { + if (action1->line_changed < action2->line_changed) return -1; + if (action1->line_changed > action2->line_changed) return 1; + return 0; + } } + void sort_action_list(ActionList* actions) { if (!actions || actions->len <= 1) return;