All files / src/repo zcl_abapgit_repo_checksums.clas.abap

96.41% Statements 215/223
88.88% Branches 8/9
85.71% Functions 6/7
96.41% Lines 215/223

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 2241x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x     2x 2x 2x 2x 2x 2x 1x 1x 1x             1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
CLASS zcl_abapgit_repo_checksums DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .
 
  PUBLIC SECTION.
 
    INTERFACES zif_abapgit_repo_checksums.
 
    METHODS constructor
      IMPORTING
        !iv_repo_key TYPE zif_abapgit_persistence=>ty_repo-key
      RAISING
        zcx_abapgit_exception.
 
    METHODS force_write
      IMPORTING
        it_checksums TYPE zif_abapgit_persistence=>ty_local_checksum_tt
      RAISING
        zcx_abapgit_exception.
 
  PROTECTED SECTION.
  PRIVATE SECTION.
 
    TYPES:
      ty_local_files_by_item_tt TYPE SORTED TABLE OF zif_abapgit_definitions=>ty_file_item WITH NON-UNIQUE KEY item.
 
    DATA mv_repo_key TYPE zif_abapgit_persistence=>ty_repo-key.
    DATA mi_repo TYPE REF TO zif_abapgit_repo.
 
    METHODS remove_non_code_related_files
      CHANGING
        !ct_local_files TYPE ty_local_files_by_item_tt.
 
    METHODS build_checksums_from_files
      IMPORTING
        it_local            TYPE ty_local_files_by_item_tt
      RETURNING
        VALUE(rt_checksums) TYPE zif_abapgit_persistence=>ty_local_checksum_tt.
 
    METHODS save_checksums
      IMPORTING
        it_checksums TYPE zif_abapgit_persistence=>ty_local_checksum_tt
      RAISING
        zcx_abapgit_exception.
 
    METHODS add_meta
      CHANGING
        cv_cs_blob TYPE string
      RAISING
        zcx_abapgit_exception.
 
    METHODS extract_meta
      CHANGING
*        co_string_map - return string map with meta when it is needed
        cv_cs_blob TYPE string.
ENDCLASS.
 
 
 
CLASS ZCL_ABAPGIT_REPO_CHECKSUMS IMPLEMENTATION.
 
 
  METHOD add_meta.
 
    DATA lv_meta_str TYPE string.
 
    lv_meta_str = |#repo_name#{ mi_repo->get_name( ) }|.
 
    cv_cs_blob = lv_meta_str && |\n| && cv_cs_blob.
 
  ENDMETHOD.
 
 
  METHOD build_checksums_from_files.
 
    DATA ls_last_item TYPE zif_abapgit_definitions=>ty_item.
 
    FIELD-SYMBOLS:
      <ls_checksum>    LIKE LINE OF rt_checksums,
      <ls_local>       LIKE LINE OF it_local,
      <ls_cs_file_sig> LIKE LINE OF <ls_checksum>-files.
 
    " This method is run at repo creation moment or manually by user
    " In the first case it assumes that the local state is the CURRENT state
    " Thus the idea is to copy local state to checksums
    " The second case is an exception, when we acknowledge that the state is unknown
    " Thus copying the local to checksums is the "best guess"
 
    LOOP AT it_local ASSIGNING <ls_local>.
      IF ls_last_item <> <ls_local>-item OR sy-tabix = 1. " First or New item reached ?
        APPEND INITIAL LINE TO rt_checksums ASSIGNING <ls_checksum>.
        MOVE-CORRESPONDING <ls_local>-item TO <ls_checksum>-item.
        ls_last_item       = <ls_local>-item.
      ENDIF.
 
      APPEND INITIAL LINE TO <ls_checksum>-files ASSIGNING <ls_cs_file_sig>.
      MOVE-CORRESPONDING <ls_local>-file TO <ls_cs_file_sig>.
 
    ENDLOOP.
 
  ENDMETHOD.
 
 
  METHOD constructor.
    ASSERT iv_repo_key IS NOT INITIAL.
    mv_repo_key = iv_repo_key.
    mi_repo = zcl_abapgit_repo_srv=>get_instance( )->get( mv_repo_key ).
    " Should be safe as repo_srv is supposed to be single source of repo instances
  ENDMETHOD.
 
 
  METHOD extract_meta.
 
    DATA lv_meta_str TYPE string.
 
    IF cv_cs_blob+0(1) <> '#'.
      RETURN. " No meta ? just ignore it
    ENDIF.
 
    SPLIT cv_cs_blob AT |\n| INTO lv_meta_str cv_cs_blob.
    " Just remove the header meta string - this is OK for now.
    " There is just repo name for the moment - needed to for DB util and potential debug
 
  ENDMETHOD.
 
 
  METHOD force_write.

    " for migration only for the moment

    save_checksums( it_checksums ).

  ENDMETHOD.
 
 
  METHOD remove_non_code_related_files.
 
    DELETE ct_local_files
      WHERE item IS INITIAL
      AND NOT ( file-path = zif_abapgit_definitions=>c_root_dir
      AND file-filename = zif_abapgit_definitions=>c_dot_abapgit ).
 
  ENDMETHOD.
 
 
  METHOD save_checksums.
 
    DATA lv_cs_blob TYPE string.
 
    lv_cs_blob = lcl_checksum_serializer=>serialize( it_checksums ).
    add_meta( CHANGING cv_cs_blob = lv_cs_blob ).
    zcl_abapgit_persist_factory=>get_repo_cs( )->update(
      iv_key     = mv_repo_key
      iv_cs_blob = lv_cs_blob ).
 
  ENDMETHOD.
 
 
  METHOD zif_abapgit_repo_checksums~get.
 
    DATA lv_cs_blob TYPE string.
 
    TRY.
        lv_cs_blob = zcl_abapgit_persist_factory=>get_repo_cs( )->read( mv_repo_key ).
      CATCH zcx_abapgit_exception zcx_abapgit_not_found.
        " Ignore currently, it's not critical for execution, just return empty
        RETURN.
    ENDTRY.
 
    IF lv_cs_blob IS NOT INITIAL.
      extract_meta( CHANGING cv_cs_blob = lv_cs_blob ).
      rt_checksums = lcl_checksum_serializer=>deserialize( lv_cs_blob ).
    ENDIF.
 
  ENDMETHOD.
 
 
  METHOD zif_abapgit_repo_checksums~get_checksums_per_file.
 
    DATA lt_checksums   TYPE zif_abapgit_persistence=>ty_local_checksum_tt.
    FIELD-SYMBOLS <ls_object> LIKE LINE OF lt_checksums.
 
    lt_checksums = zif_abapgit_repo_checksums~get( ).
 
    LOOP AT lt_checksums ASSIGNING <ls_object>.
      APPEND LINES OF <ls_object>-files TO rt_checksums.
    ENDLOOP.
 
  ENDMETHOD.
 
 
  METHOD zif_abapgit_repo_checksums~rebuild.
 
    DATA lt_local     TYPE ty_local_files_by_item_tt.
    DATA lt_checksums TYPE zif_abapgit_persistence=>ty_local_checksum_tt.
 
    lt_local  = mi_repo->get_files_local( ).
    remove_non_code_related_files( CHANGING ct_local_files = lt_local ).
 
    lt_checksums = build_checksums_from_files( lt_local ).
    save_checksums( lt_checksums ).
 
  ENDMETHOD.
 
 
  METHOD zif_abapgit_repo_checksums~update.
 
    DATA lt_checksums   TYPE zif_abapgit_persistence=>ty_local_checksum_tt.
    DATA lt_local_files TYPE zif_abapgit_definitions=>ty_files_item_tt.
 
    lt_checksums   = zif_abapgit_repo_checksums~get( ).
    lt_local_files = mi_repo->get_files_local( ).
 
    lt_checksums = lcl_update_calculator=>calculate_updated(
      it_current_checksums = lt_checksums
      it_local_files       = lt_local_files
      it_updated_files     = it_updated_files ).
 
    save_checksums( lt_checksums ).
 
  ENDMETHOD.
ENDCLASS.