Parse a single @@ hunk header line and emit a hunk entry if valid. * Returns true if a hunk was added. */
| 111 | /* Parse a single @@ hunk header line and emit a hunk entry if valid. |
| 112 | * Returns true if a hunk was added. */ |
| 113 | static bool parse_hunk_line(const char *line, size_t line_len, const char *current_file, |
| 114 | cbm_changed_hunk_t *out_h) { |
| 115 | const char *plus = memchr(line, '+', line_len); |
| 116 | if (!plus || plus <= line) { |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | const char *end_at = strstr(plus, " @@"); |
| 121 | size_t range_len; |
| 122 | if (end_at) { |
| 123 | range_len = (size_t)(end_at - plus - SKIP_ONE); |
| 124 | } else { |
| 125 | range_len = (size_t)(line + line_len - plus - SKIP_ONE); |
| 126 | } |
| 127 | |
| 128 | char range_str[CBM_SZ_64]; |
| 129 | if (range_len >= sizeof(range_str)) { |
| 130 | range_len = sizeof(range_str) - SKIP_ONE; |
| 131 | } |
| 132 | memcpy(range_str, plus + SKIP_ONE, range_len); |
| 133 | range_str[range_len] = '\0'; |
| 134 | |
| 135 | int start; |
| 136 | int cnt; |
| 137 | cbm_parse_range(range_str, &start, &cnt); |
| 138 | |
| 139 | if (start <= 0 || !cbm_is_trackable_file(current_file)) { |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | int end = start + cnt - SKIP_ONE; |
| 144 | if (end < start) { |
| 145 | end = start; |
| 146 | } |
| 147 | |
| 148 | snprintf(out_h->path, sizeof(out_h->path), "%s", current_file); |
| 149 | out_h->start_line = start; |
| 150 | out_h->end_line = end; |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | int cbm_parse_hunks(const char *output, cbm_changed_hunk_t *out, int max_out) { |
| 155 | if (!output || !out || max_out <= 0) { |
no test coverage detected