All files / src/git zcl_abapgit_git_commit.clas.abap

98.8% Statements 330/334
75% Branches 6/8
100% Functions 2/2
98.8% Lines 330/334

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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 3351x 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 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 6x   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 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 5x 5x 5x 5x 5x 5x 5x 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 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  
CLASS zcl_abapgit_git_commit DEFINITION
  PUBLIC
  CREATE PUBLIC .
 
  PUBLIC SECTION.
    TYPES:
      BEGIN OF ty_pull_result,
        commits TYPE zif_abapgit_git_definitions=>ty_commit_tt,
        commit  TYPE zif_abapgit_git_definitions=>ty_sha1,
      END OF ty_pull_result .
 
    CLASS-METHODS get_by_branch
      IMPORTING
        !iv_branch_name       TYPE string
        !iv_repo_url          TYPE string
        !iv_deepen_level      TYPE i
        !iv_sorted            TYPE abap_bool DEFAULT abap_true
      RETURNING
        VALUE(rs_pull_result) TYPE ty_pull_result
      RAISING
        zcx_abapgit_exception .
    CLASS-METHODS get_by_commit
      IMPORTING
        !iv_commit_hash   TYPE zif_abapgit_git_definitions=>ty_sha1
        !iv_repo_url      TYPE string
        !iv_deepen_level  TYPE i
      RETURNING
        VALUE(rt_commits) TYPE zif_abapgit_git_definitions=>ty_commit_tt
      RAISING
        zcx_abapgit_exception .
    CLASS-METHODS parse_commits
      IMPORTING
        !it_objects       TYPE zif_abapgit_definitions=>ty_objects_tt
      RETURNING
        VALUE(rt_commits) TYPE zif_abapgit_git_definitions=>ty_commit_tt
      RAISING
        zcx_abapgit_exception .
    CLASS-METHODS sort_commits
      CHANGING
        !ct_commits TYPE zif_abapgit_git_definitions=>ty_commit_tt
      RAISING
        zcx_abapgit_exception .
    CLASS-METHODS reverse_sort_order
      CHANGING
        !ct_commits TYPE zif_abapgit_git_definitions=>ty_commit_tt .
    CLASS-METHODS clear_missing_parents CHANGING ct_commits TYPE zif_abapgit_git_definitions=>ty_commit_tt .
  PROTECTED SECTION.
  PRIVATE SECTION.
    TYPES: ty_sha1_range TYPE RANGE OF zif_abapgit_git_definitions=>ty_sha1 .
 
    CLASS-METHODS get_1st_child_commit
      IMPORTING
        it_commit_sha1s TYPE ty_sha1_range
      EXPORTING
        et_commit_sha1s TYPE ty_sha1_range
        es_1st_commit   TYPE zif_abapgit_git_definitions=>ty_commit
      CHANGING
        ct_commits      TYPE zif_abapgit_git_definitions=>ty_commit_tt .
 
    CLASS-METHODS is_missing
      IMPORTING
        it_commits       TYPE zif_abapgit_git_definitions=>ty_commit_tt
        iv_sha1          TYPE zif_abapgit_git_definitions=>ty_sha1
      RETURNING
        VALUE(rv_result) TYPE abap_bool.
 
    CLASS-METHODS extract_author_data
      IMPORTING
        !iv_author TYPE string
      EXPORTING
        !ev_author TYPE zif_abapgit_git_definitions=>ty_commit-author
        !ev_email  TYPE zif_abapgit_git_definitions=>ty_commit-email
        !ev_time   TYPE zif_abapgit_git_definitions=>ty_commit-time
      RAISING
        zcx_abapgit_exception .
ENDCLASS.
 
 
 
CLASS zcl_abapgit_git_commit IMPLEMENTATION.
 
  METHOD extract_author_data.
 
    " Unix time stamps are in same time zone, so ignore the zone
    FIND REGEX zif_abapgit_definitions=>c_author_regex IN iv_author
      SUBMATCHES
      ev_author
      ev_email
      ev_time.
 
    IF sy-subrc <> 0.
      zcx_abapgit_exception=>raise( |Error author regex value='{ iv_author }'| ).
    ENDIF.
 
  ENDMETHOD.
 
  METHOD clear_missing_parents.
 
    "Part of #4719 to handle cut commit sequences, todo
 
    FIELD-SYMBOLS: <ls_commit> TYPE zif_abapgit_git_definitions=>ty_commit.
 
    LOOP AT ct_commits ASSIGNING <ls_commit>.
 
      IF is_missing( it_commits = ct_commits
                     iv_sha1  = <ls_commit>-parent1 ) = abap_true.
        CLEAR <ls_commit>-parent1.
      ENDIF.
 
      IF is_missing( it_commits = ct_commits
                     iv_sha1  = <ls_commit>-parent2 ) = abap_true.
        CLEAR <ls_commit>-parent2.
      ENDIF.
 
    ENDLOOP.
 
  ENDMETHOD.
 
 
  METHOD get_1st_child_commit.
 
    DATA: lt_1stchild_commits TYPE zif_abapgit_git_definitions=>ty_commit_tt,
          ls_parent           LIKE LINE OF it_commit_sha1s,
          lt_commit_sha1s     LIKE it_commit_sha1s.
 
    FIELD-SYMBOLS: <ls_child_commit> TYPE zif_abapgit_git_definitions=>ty_commit.
 
    CLEAR: es_1st_commit.
 
* get all reachable next commits
    lt_commit_sha1s = it_commit_sha1s.
    LOOP AT ct_commits ASSIGNING <ls_child_commit> WHERE parent1 IN lt_commit_sha1s
                                                      OR parent2 IN lt_commit_sha1s.
      INSERT <ls_child_commit> INTO TABLE lt_1stchild_commits.
    ENDLOOP.
 
* return oldest one
    SORT lt_1stchild_commits BY time ASCENDING.
    READ TABLE lt_1stchild_commits INTO es_1st_commit INDEX 1.
 
* remove from available commits
    DELETE ct_commits WHERE sha1 = es_1st_commit-sha1.
 
* set relevant parent commit sha1s
    IF lines( lt_1stchild_commits ) = 1.
      CLEAR et_commit_sha1s.
    ELSE.
      et_commit_sha1s = it_commit_sha1s.
    ENDIF.
 
    ls_parent-sign   = 'I'.
    ls_parent-option = 'EQ'.
    ls_parent-low    = es_1st_commit-sha1.
    INSERT ls_parent INTO TABLE et_commit_sha1s.
 
  ENDMETHOD.
 
 
  METHOD get_by_branch.
 
    DATA: li_progress TYPE REF TO zif_abapgit_progress,
          lt_objects  TYPE zif_abapgit_definitions=>ty_objects_tt.
 
    li_progress = zcl_abapgit_progress=>get_instance( 1 ).
 
    li_progress->show(
      iv_current = 1
      iv_text    = |Get git commits { iv_repo_url }| ).
 
    zcl_abapgit_git_transport=>upload_pack_by_branch(
      EXPORTING
        iv_url          = iv_repo_url
        iv_branch_name  = iv_branch_name
        iv_deepen_level = iv_deepen_level
      IMPORTING
        ev_branch       = rs_pull_result-commit
        et_objects      = lt_objects ).
 
    DELETE lt_objects WHERE type <> zif_abapgit_git_definitions=>c_type-commit.
 
    rs_pull_result-commits = parse_commits( lt_objects ).
 
    IF iv_sorted = abap_true.
      sort_commits( CHANGING ct_commits = rs_pull_result-commits ).
    ENDIF.
 
  ENDMETHOD.
 
 
  METHOD get_by_commit.
 
    DATA: li_progress TYPE REF TO zif_abapgit_progress,
          lt_objects  TYPE zif_abapgit_definitions=>ty_objects_tt.
 
    li_progress = zcl_abapgit_progress=>get_instance( 1 ).
 
    li_progress->show(
      iv_current = 1
      iv_text    = |Get git commits { iv_repo_url }| ).
 
    zcl_abapgit_git_transport=>upload_pack_by_commit(
      EXPORTING
        iv_url          = iv_repo_url
        iv_deepen_level = iv_deepen_level
        iv_hash         = iv_commit_hash
      IMPORTING
        et_objects      = lt_objects ).
 
    DELETE lt_objects WHERE type <> zif_abapgit_git_definitions=>c_type-commit.
 
    rt_commits = parse_commits( lt_objects ).
    sort_commits( CHANGING ct_commits = rt_commits ).
 
  ENDMETHOD.
 
 
  METHOD is_missing.
 
    IF iv_sha1 IS NOT INITIAL.
 
      READ TABLE it_commits
        TRANSPORTING NO FIELDS
        WITH KEY sha1 = iv_sha1.
      rv_result = boolc( sy-subrc <> 0 ).
 
    ENDIF.
 
  ENDMETHOD.
 
 
  METHOD parse_commits.
 
    DATA: ls_commit TYPE zif_abapgit_git_definitions=>ty_commit,
          lt_body   TYPE STANDARD TABLE OF string WITH DEFAULT KEY,
          ls_raw    TYPE zcl_abapgit_git_pack=>ty_commit.
 
    FIELD-SYMBOLS: <ls_object> LIKE LINE OF it_objects,
                   <lv_body>   TYPE string.
 
 
    LOOP AT it_objects ASSIGNING <ls_object> USING KEY type
        WHERE type = zif_abapgit_git_definitions=>c_type-commit.
      ls_raw = zcl_abapgit_git_pack=>decode_commit( <ls_object>-data ).
 
      CLEAR ls_commit.
      ls_commit-sha1 = <ls_object>-sha1.
      ls_commit-parent1 = ls_raw-parent.
      ls_commit-parent2 = ls_raw-parent2.
 
      SPLIT ls_raw-body AT cl_abap_char_utilities=>newline INTO TABLE lt_body.
 
      READ TABLE lt_body WITH KEY table_line = ' -----END PGP SIGNATURE-----' TRANSPORTING NO FIELDS.
      IF sy-subrc = 0.
        DELETE lt_body TO sy-tabix.
        DELETE lt_body TO 2.
      ENDIF.
 
      READ TABLE lt_body INDEX 1 INTO ls_commit-message.  "#EC CI_SUBRC
      " The second line is always empty. Therefore we omit it.
      LOOP AT lt_body ASSIGNING <lv_body>
                      FROM 3.
        INSERT <lv_body> INTO TABLE ls_commit-body.
      ENDLOOP.
 
      extract_author_data(
        EXPORTING
          iv_author = ls_raw-author
        IMPORTING
          ev_author = ls_commit-author
          ev_email  = ls_commit-email
          ev_time   = ls_commit-time ).
 
      APPEND ls_commit TO rt_commits.
 
    ENDLOOP.
 
  ENDMETHOD.
 
 
  METHOD reverse_sort_order.
 
    DATA: lt_commits           TYPE zif_abapgit_git_definitions=>ty_commit_tt.
    FIELD-SYMBOLS: <ls_commit> TYPE zif_abapgit_git_definitions=>ty_commit.
 
    LOOP AT ct_commits ASSIGNING <ls_commit>.
      INSERT <ls_commit> INTO lt_commits INDEX 1.
    ENDLOOP.
    ct_commits = lt_commits.
    FREE lt_commits.
 
  ENDMETHOD.
 
 
  METHOD sort_commits.
 
    DATA: lt_sorted_commits TYPE zif_abapgit_git_definitions=>ty_commit_tt,
          ls_next_commit    TYPE zif_abapgit_git_definitions=>ty_commit,
          lt_parents        TYPE ty_sha1_range,
          ls_parent         LIKE LINE OF lt_parents.
 
    FIELD-SYMBOLS: <ls_initial_commit> TYPE zif_abapgit_git_definitions=>ty_commit.
 
    " find initial commit
    READ TABLE ct_commits ASSIGNING <ls_initial_commit> WITH KEY parent1 = space.
    IF sy-subrc <> 0.
      zcx_abapgit_exception=>raise( |Cannot find initial commit. Too many commits. Action not possible.| ).
    ENDIF.
 
    ls_parent-sign   = 'I'.
    ls_parent-option = 'EQ'.
    ls_parent-low    = <ls_initial_commit>-sha1.
    INSERT ls_parent INTO TABLE lt_parents.
 
    " first commit
    INSERT <ls_initial_commit> INTO TABLE lt_sorted_commits.
 
    " remove from available commits
    DELETE ct_commits WHERE sha1 = <ls_initial_commit>-sha1.
 
    DO.
      get_1st_child_commit( EXPORTING it_commit_sha1s = lt_parents
                            IMPORTING et_commit_sha1s = lt_parents
                                      es_1st_commit   = ls_next_commit
                            CHANGING  ct_commits      = ct_commits ).
      IF ls_next_commit IS INITIAL.
        EXIT. "DO
      ENDIF.
      INSERT ls_next_commit INTO TABLE lt_sorted_commits.
    ENDDO.
 
    ct_commits = lt_sorted_commits.
 
  ENDMETHOD.
ENDCLASS.