@@ -2,7 +2,7 @@ module conflict_parser |
| 2 | 2 | implicit none |
| 3 | 3 | private |
| 4 | 4 | |
| 5 | | - public :: conflict_t, parse_conflict_file, conflict_count, get_file_view |
| 5 | + public :: conflict_t, parse_conflict_file, conflict_count, get_file_view, get_full_preview, get_side_view |
| 6 | 6 | |
| 7 | 7 | ! Type to hold a single conflict |
| 8 | 8 | type :: conflict_t |
@@ -281,4 +281,189 @@ contains |
| 281 | 281 | |
| 282 | 282 | end subroutine get_file_view |
| 283 | 283 | |
| 284 | + ! Get a full preview with ALL conflicts resolved |
| 285 | + subroutine get_full_preview(conflicts, n_conflicts, current_idx, file_view, n_lines, conflict_start, conflict_end) |
| 286 | + type(conflict_t), intent(in) :: conflicts(:) |
| 287 | + integer, intent(in) :: n_conflicts, current_idx |
| 288 | + character(len=:), allocatable, dimension(:), intent(out) :: file_view |
| 289 | + integer, intent(out) :: n_lines |
| 290 | + integer, intent(out) :: conflict_start, conflict_end |
| 291 | + integer :: i, j, view_idx, line_num, n_res_lines |
| 292 | + character(len=:), allocatable, dimension(:) :: resolution_lines |
| 293 | + logical :: in_conflict |
| 294 | + integer :: conflict_idx |
| 295 | + |
| 296 | + if (n_conflicts == 0 .or. .not. allocated(conflicts(1)%full_file)) return |
| 297 | + |
| 298 | + ! Allocate output |
| 299 | + allocate(character(len=1024) :: file_view(conflicts(1)%total_file_lines * 2)) |
| 300 | + view_idx = 0 |
| 301 | + conflict_start = 0 |
| 302 | + conflict_end = 0 |
| 303 | + |
| 304 | + ! Walk through the file line by line |
| 305 | + line_num = 1 |
| 306 | + do while (line_num <= conflicts(1)%total_file_lines) |
| 307 | + ! Check if this line starts a conflict |
| 308 | + in_conflict = .false. |
| 309 | + do i = 1, n_conflicts |
| 310 | + if (line_num == conflicts(i)%start_line) then |
| 311 | + in_conflict = .true. |
| 312 | + conflict_idx = i |
| 313 | + |
| 314 | + ! Mark if this is the current conflict |
| 315 | + if (i == current_idx) then |
| 316 | + conflict_start = view_idx + 1 |
| 317 | + end if |
| 318 | + |
| 319 | + ! Get resolution for this conflict |
| 320 | + select case (conflicts(i)%choice) |
| 321 | + case (1) ! Incoming |
| 322 | + resolution_lines = conflicts(i)%incoming_lines |
| 323 | + case (2) ! Local |
| 324 | + resolution_lines = conflicts(i)%local_lines |
| 325 | + case (3) ! Both |
| 326 | + if (allocated(conflicts(i)%incoming_lines) .and. allocated(conflicts(i)%local_lines)) then |
| 327 | + n_res_lines = size(conflicts(i)%incoming_lines) + size(conflicts(i)%local_lines) |
| 328 | + allocate(character(len=1024) :: resolution_lines(n_res_lines)) |
| 329 | + do j = 1, size(conflicts(i)%incoming_lines) |
| 330 | + resolution_lines(j) = conflicts(i)%incoming_lines(j) |
| 331 | + end do |
| 332 | + do j = 1, size(conflicts(i)%local_lines) |
| 333 | + resolution_lines(size(conflicts(i)%incoming_lines) + j) = conflicts(i)%local_lines(j) |
| 334 | + end do |
| 335 | + end if |
| 336 | + case default ! No choice yet - show incoming |
| 337 | + resolution_lines = conflicts(i)%incoming_lines |
| 338 | + end select |
| 339 | + |
| 340 | + ! Add resolution lines |
| 341 | + if (allocated(resolution_lines)) then |
| 342 | + do j = 1, size(resolution_lines) |
| 343 | + view_idx = view_idx + 1 |
| 344 | + file_view(view_idx) = resolution_lines(j) |
| 345 | + end do |
| 346 | + deallocate(resolution_lines) |
| 347 | + end if |
| 348 | + |
| 349 | + ! Mark end if this is the current conflict |
| 350 | + if (i == current_idx) then |
| 351 | + conflict_end = view_idx |
| 352 | + end if |
| 353 | + |
| 354 | + ! Skip to end of conflict |
| 355 | + line_num = conflicts(i)%end_line + 1 |
| 356 | + exit |
| 357 | + end if |
| 358 | + end do |
| 359 | + |
| 360 | + ! If not in conflict, copy the line |
| 361 | + if (.not. in_conflict) then |
| 362 | + view_idx = view_idx + 1 |
| 363 | + file_view(view_idx) = conflicts(1)%full_file(line_num) |
| 364 | + line_num = line_num + 1 |
| 365 | + end if |
| 366 | + end do |
| 367 | + |
| 368 | + n_lines = view_idx |
| 369 | + |
| 370 | + end subroutine get_full_preview |
| 371 | + |
| 372 | + ! Get a view showing a specific side (incoming/local) for current conflict, all others resolved |
| 373 | + subroutine get_side_view(conflicts, n_conflicts, current_idx, side, file_view, n_lines, conflict_start, conflict_end) |
| 374 | + type(conflict_t), intent(in) :: conflicts(:) |
| 375 | + integer, intent(in) :: n_conflicts, current_idx |
| 376 | + integer, intent(in) :: side ! 1=incoming, 2=local |
| 377 | + character(len=:), allocatable, dimension(:), intent(out) :: file_view |
| 378 | + integer, intent(out) :: n_lines |
| 379 | + integer, intent(out) :: conflict_start, conflict_end |
| 380 | + integer :: i, j, view_idx, line_num, n_res_lines |
| 381 | + character(len=:), allocatable, dimension(:) :: resolution_lines |
| 382 | + logical :: in_conflict |
| 383 | + |
| 384 | + if (n_conflicts == 0 .or. .not. allocated(conflicts(1)%full_file)) return |
| 385 | + |
| 386 | + ! Allocate output |
| 387 | + allocate(character(len=1024) :: file_view(conflicts(1)%total_file_lines * 2)) |
| 388 | + view_idx = 0 |
| 389 | + conflict_start = 0 |
| 390 | + conflict_end = 0 |
| 391 | + |
| 392 | + ! Walk through the file line by line |
| 393 | + line_num = 1 |
| 394 | + do while (line_num <= conflicts(1)%total_file_lines) |
| 395 | + ! Check if this line starts a conflict |
| 396 | + in_conflict = .false. |
| 397 | + do i = 1, n_conflicts |
| 398 | + if (line_num == conflicts(i)%start_line) then |
| 399 | + in_conflict = .true. |
| 400 | + |
| 401 | + ! Mark if this is the current conflict |
| 402 | + if (i == current_idx) then |
| 403 | + conflict_start = view_idx + 1 |
| 404 | + end if |
| 405 | + |
| 406 | + ! Get resolution for this conflict |
| 407 | + if (i == current_idx) then |
| 408 | + ! For current conflict, use specified side |
| 409 | + if (side == 1) then |
| 410 | + resolution_lines = conflicts(i)%incoming_lines |
| 411 | + else |
| 412 | + resolution_lines = conflicts(i)%local_lines |
| 413 | + end if |
| 414 | + else |
| 415 | + ! For other conflicts, use their choice (or incoming if no choice) |
| 416 | + select case (conflicts(i)%choice) |
| 417 | + case (1) |
| 418 | + resolution_lines = conflicts(i)%incoming_lines |
| 419 | + case (2) |
| 420 | + resolution_lines = conflicts(i)%local_lines |
| 421 | + case (3) ! Both |
| 422 | + if (allocated(conflicts(i)%incoming_lines) .and. allocated(conflicts(i)%local_lines)) then |
| 423 | + n_res_lines = size(conflicts(i)%incoming_lines) + size(conflicts(i)%local_lines) |
| 424 | + allocate(character(len=1024) :: resolution_lines(n_res_lines)) |
| 425 | + do j = 1, size(conflicts(i)%incoming_lines) |
| 426 | + resolution_lines(j) = conflicts(i)%incoming_lines(j) |
| 427 | + end do |
| 428 | + do j = 1, size(conflicts(i)%local_lines) |
| 429 | + resolution_lines(size(conflicts(i)%incoming_lines) + j) = conflicts(i)%local_lines(j) |
| 430 | + end do |
| 431 | + end if |
| 432 | + case default ! No choice yet - show incoming |
| 433 | + resolution_lines = conflicts(i)%incoming_lines |
| 434 | + end select |
| 435 | + end if |
| 436 | + |
| 437 | + ! Add resolution lines |
| 438 | + if (allocated(resolution_lines)) then |
| 439 | + do j = 1, size(resolution_lines) |
| 440 | + view_idx = view_idx + 1 |
| 441 | + file_view(view_idx) = resolution_lines(j) |
| 442 | + end do |
| 443 | + deallocate(resolution_lines) |
| 444 | + end if |
| 445 | + |
| 446 | + ! Mark end if this is the current conflict |
| 447 | + if (i == current_idx) then |
| 448 | + conflict_end = view_idx |
| 449 | + end if |
| 450 | + |
| 451 | + ! Skip to end of conflict |
| 452 | + line_num = conflicts(i)%end_line + 1 |
| 453 | + exit |
| 454 | + end if |
| 455 | + end do |
| 456 | + |
| 457 | + ! If not in conflict, copy the line |
| 458 | + if (.not. in_conflict) then |
| 459 | + view_idx = view_idx + 1 |
| 460 | + file_view(view_idx) = conflicts(1)%full_file(line_num) |
| 461 | + line_num = line_num + 1 |
| 462 | + end if |
| 463 | + end do |
| 464 | + |
| 465 | + n_lines = view_idx |
| 466 | + |
| 467 | + end subroutine get_side_view |
| 468 | + |
| 284 | 469 | end module conflict_parser |