| 1049 | } |
| 1050 | |
| 1051 | static int split_hunk(struct add_p_state *s, struct file_diff *file_diff, |
| 1052 | size_t hunk_index) |
| 1053 | { |
| 1054 | int colored = !!s->colored.len, first = 1; |
| 1055 | struct hunk *hunk = file_diff->hunk + hunk_index; |
| 1056 | size_t splittable_into; |
| 1057 | size_t end, colored_end, current, colored_current = 0, context_line_count; |
| 1058 | struct hunk_header remaining, *header; |
| 1059 | char marker, ch; |
| 1060 | |
| 1061 | if (hunk_index >= file_diff->hunk_nr) |
| 1062 | BUG("invalid hunk index: %d (must be >= 0 and < %d)", |
| 1063 | (int)hunk_index, (int)file_diff->hunk_nr); |
| 1064 | |
| 1065 | if (hunk->splittable_into < 2) |
| 1066 | return 0; |
| 1067 | splittable_into = hunk->splittable_into; |
| 1068 | |
| 1069 | end = hunk->end; |
| 1070 | colored_end = hunk->colored_end; |
| 1071 | |
| 1072 | remaining = hunk->header; |
| 1073 | |
| 1074 | file_diff->hunk_nr += splittable_into - 1; |
| 1075 | ALLOC_GROW(file_diff->hunk, file_diff->hunk_nr, file_diff->hunk_alloc); |
| 1076 | if (hunk_index + splittable_into < file_diff->hunk_nr) |
| 1077 | memmove(file_diff->hunk + hunk_index + splittable_into, |
| 1078 | file_diff->hunk + hunk_index + 1, |
| 1079 | (file_diff->hunk_nr - hunk_index - splittable_into) |
| 1080 | * sizeof(*hunk)); |
| 1081 | hunk = file_diff->hunk + hunk_index; |
| 1082 | hunk->splittable_into = 1; |
| 1083 | hunk->use = UNDECIDED_HUNK; |
| 1084 | memset(hunk + 1, 0, (splittable_into - 1) * sizeof(*hunk)); |
| 1085 | |
| 1086 | header = &hunk->header; |
| 1087 | header->old_count = header->new_count = 0; |
| 1088 | |
| 1089 | current = hunk->start; |
| 1090 | if (colored) |
| 1091 | colored_current = hunk->colored_start; |
| 1092 | marker = '\0'; |
| 1093 | context_line_count = 0; |
| 1094 | |
| 1095 | while (splittable_into > 1) { |
| 1096 | ch = normalize_marker(&s->plain.buf[current]); |
| 1097 | |
| 1098 | if (!ch) |
| 1099 | BUG("buffer overrun while splitting hunks"); |
| 1100 | |
| 1101 | /* |
| 1102 | * Is this the first context line after a chain of +/- lines? |
| 1103 | * Then record the start of the next split hunk. |
| 1104 | */ |
| 1105 | if ((marker == '-' || marker == '+') && ch == ' ') { |
| 1106 | first = 0; |
| 1107 | hunk[1].start = current; |
| 1108 | if (colored) |
no test coverage detected