Show diffs in case of trailing spaces (#7062)
Some checks failed
main-build / build-merged (push) Has been cancelled
main-build / auto-tag (push) Has been cancelled
main-build / coverage (push) Has been cancelled
main-build / auto-tag-artifact (push) Has been cancelled

Co-authored-by: Lars Hvam <larshp@hotmail.com>
This commit is contained in:
Marc Bernard 2024-10-29 00:40:08 -04:00 committed by GitHub
parent e4bb6ec3ef
commit 57d785ac98
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 67 additions and 22 deletions

View File

@ -75,7 +75,13 @@ CLASS zcl_abapgit_diff DEFINITION
METHODS create_regex_set
RETURNING
VALUE(rt_regex_set) TYPE ty_regexset_tt.
METHODS compute_and_render
METHODS compute_diff
IMPORTING
!it_new TYPE rswsourcet
!it_old TYPE rswsourcet
RETURNING
VALUE(rt_diff) TYPE zif_abapgit_definitions=>ty_diffs_tt.
METHODS compute_diff_extra
IMPORTING
!it_new TYPE rswsourcet
!it_old TYPE rswsourcet
@ -187,7 +193,7 @@ CLASS zcl_abapgit_diff IMPLEMENTATION.
ENDMETHOD.
METHOD compute_and_render.
METHOD compute_diff.
DATA:
lv_i TYPE i,
@ -195,8 +201,6 @@ CLASS zcl_abapgit_diff IMPLEMENTATION.
lt_delta TYPE STANDARD TABLE OF rsedcresul WITH DEFAULT KEY.
FIELD-SYMBOLS:
<ls_old> LIKE LINE OF it_old,
<ls_new> LIKE LINE OF it_new,
<ls_delta> LIKE LINE OF lt_delta.
" Note: Ignore case is for keywords, variables, types etc, but not for literals
@ -245,22 +249,9 @@ CLASS zcl_abapgit_diff IMPLEMENTATION.
APPEND ls_diff TO rt_diff.
ENDLOOP.
ELSEIF sy-subrc = 2.
" Copy input... but it might not be identical
LOOP AT it_old ASSIGNING <ls_old>.
CLEAR ls_diff.
ls_diff-old_num = sy-tabix.
ls_diff-old = <ls_old>.
READ TABLE it_new ASSIGNING <ls_new> INDEX sy-tabix.
ASSERT sy-subrc = 0.
ls_diff-new_num = sy-tabix.
ls_diff-new = <ls_new>.
" SAP function ignores lines that contain only whitespace so we compare directly
IF ( mv_compare_mode = 1 OR mv_compare_mode = 3 ) AND <ls_old> <> <ls_new> AND
( strlen( condense( <ls_old> ) ) = 0 OR strlen( condense( <ls_new> ) ) = 0 ).
ls_diff-result = zif_abapgit_definitions=>c_diff-update.
ENDIF.
APPEND ls_diff TO rt_diff.
ENDLOOP.
" The function doesn't find all diffs...
rt_diff = compute_diff_extra( it_new = it_new
it_old = it_old ).
ELSE.
ASSERT 0 = 1. " incorrect function call
ENDIF.
@ -268,6 +259,53 @@ CLASS zcl_abapgit_diff IMPLEMENTATION.
ENDMETHOD.
METHOD compute_diff_extra.
DATA:
lv_last_new TYPE c LENGTH 1,
lv_last_old TYPE c LENGTH 1,
ls_diff LIKE LINE OF rt_diff.
FIELD-SYMBOLS:
<ls_old> LIKE LINE OF it_old,
<ls_new> LIKE LINE OF it_new.
LOOP AT it_old ASSIGNING <ls_old>.
CLEAR ls_diff.
ls_diff-old_num = sy-tabix.
ls_diff-old = <ls_old>.
READ TABLE it_new ASSIGNING <ls_new> INDEX sy-tabix.
IF sy-subrc <> 0.
EXIT.
ENDIF.
ls_diff-new_num = sy-tabix.
ls_diff-new = <ls_new>.
" SAP function ignores lines that contain only whitespace so we compare directly
" Also check if one line has trailing space(s)
IF ( mv_compare_mode = 1 OR mv_compare_mode = 3 ) AND <ls_old> <> <ls_new>.
IF strlen( condense( <ls_old> ) ) = 0 OR strlen( condense( <ls_new> ) ) = 0.
ls_diff-result = zif_abapgit_definitions=>c_diff-update.
ELSEIF strlen( <ls_old> ) > 0 AND strlen( <ls_new> ) > 0.
lv_last_new = substring( val = <ls_new>
off = strlen( <ls_new> ) - 1 ).
lv_last_old = substring( val = <ls_old>
off = strlen( <ls_old> ) - 1 ).
IF lv_last_new = space OR lv_last_old = space.
ls_diff-result = zif_abapgit_definitions=>c_diff-update.
ENDIF.
ENDIF.
ENDIF.
APPEND ls_diff TO rt_diff.
ENDLOOP.
ENDMETHOD.
METHOD constructor.
DATA: lt_new TYPE rswsourcet,
@ -287,8 +325,8 @@ CLASS zcl_abapgit_diff IMPLEMENTATION.
IMPORTING et_new = lt_new
et_old = lt_old ).
mt_diff = compute_and_render( it_new = lt_new
it_old = lt_old ).
mt_diff = compute_diff( it_new = lt_new
it_old = lt_old ).
adjust_diff( ).

View File

@ -528,12 +528,14 @@ CLASS ltcl_diff IMPLEMENTATION.
add_new( ` ` ). " one space
add_new( ` ` ). " some spaces
add_new( 'E' ).
add_new( 'X' ). " no trailing space
add_old( 'A' ).
add_old( ` ` ). " some spaces
add_old( ` ` ). " two spaces
add_old( `` ). " empty line
add_old( 'E' ).
add_old( `X ` ). " some trailing space
add_expected( iv_new_num = ' 1'
iv_new = 'A'
@ -560,6 +562,11 @@ CLASS ltcl_diff IMPLEMENTATION.
iv_result = zif_abapgit_definitions=>c_diff-unchanged
iv_old_num = ' 5'
iv_old = 'E' ).
add_expected( iv_new_num = ' 6'
iv_new = 'X'
iv_result = zif_abapgit_definitions=>c_diff-update
iv_old_num = ' 6'
iv_old = `X ` ).
test( ).