fix(utilities): changed line_changed to line_original in apply_diffs delete case and sort deletions in descending order

This commit is contained in:
lisk77 2025-09-19 02:14:52 +02:00
parent 3e0a0d7057
commit abaa6e12fc

View file

@ -1102,6 +1102,8 @@ File* apply_diff(File* basefile, ActionList* diff) {
File* copy = copy_file(basefile); File* copy = copy_file(basefile);
if (!copy) return NULL; if (!copy) return NULL;
sort_action_list(diff);
for (size_t i = 0; i < diff->len; i++) { for (size_t i = 0; i < diff->len; i++) {
Action* action = &diff->actions[i]; Action* action = &diff->actions[i];
int success = 0; int success = 0;
@ -1117,9 +1119,9 @@ File* apply_diff(File* basefile, ActionList* diff) {
break; break;
case DELETE: case DELETE:
success = delete_line(copy, action->line_changed); success = delete_line(copy, action->line_original);
if (!success) { 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); free_file(copy);
return NULL; return NULL;
} }
@ -1143,9 +1145,18 @@ int compare_actions(const void* a, const void* b) {
return (action1->type == DELETE) ? -1 : 1; 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) { void sort_action_list(ActionList* actions) {
if (!actions || actions->len <= 1) return; if (!actions || actions->len <= 1) return;