MCPcopy Index your code
hub / github.com/git/git / merge_hunks

Function merge_hunks

add-patch.c:888–1013  ·  view source on GitHub ↗

Coalesce hunks again that were split */

Source from the content-addressed store, hash-verified

886
887/* Coalesce hunks again that were split */
888static int merge_hunks(struct add_p_state *s, struct file_diff *file_diff,
889 size_t *hunk_index, int use_all, struct hunk *merged)
890{
891 size_t i = *hunk_index, delta;
892 struct hunk *hunk = file_diff->hunk + i;
893 /* `header` corresponds to the merged hunk */
894 struct hunk_header *header = &merged->header, *next;
895
896 if (!use_all && hunk->use != USE_HUNK)
897 return 0;
898
899 *merged = *hunk;
900 /* We simply skip the colored part (if any) when merging hunks */
901 merged->colored_start = merged->colored_end = 0;
902
903 for (; i + 1 < file_diff->hunk_nr; i++) {
904 hunk++;
905 next = &hunk->header;
906
907 /*
908 * Stop merging hunks when:
909 *
910 * - the hunk is not selected for use, or
911 * - the hunk does not overlap with the already-merged hunk(s)
912 */
913 if ((!use_all && hunk->use != USE_HUNK) ||
914 header->new_offset >= next->new_offset + merged->delta ||
915 header->new_offset + header->new_count
916 < next->new_offset + merged->delta)
917 break;
918
919 /*
920 * If the hunks were not edited, and overlap, we can simply
921 * extend the line range.
922 */
923 if (merged->start < hunk->start && merged->end > hunk->start) {
924 merged->end = hunk->end;
925 merged->colored_end = hunk->colored_end;
926 delta = 0;
927 } else {
928 const char *plain = s->plain.buf;
929 size_t overlapping_line_count = header->new_offset
930 + header->new_count - merged->delta
931 - next->new_offset;
932 size_t overlap_end = hunk->start;
933 size_t overlap_start = overlap_end;
934 size_t overlap_next, len, j;
935
936 /*
937 * One of the hunks was edited: the modified hunk was
938 * appended to the strbuf `s->plain`.
939 *
940 * Let's ensure that at least the last context line of
941 * the first hunk overlaps with the corresponding line
942 * of the second hunk, and then merge.
943 */
944 for (j = 0; j < overlapping_line_count; j++) {
945 overlap_next = find_next_line(&s->plain,

Callers 1

reassemble_patchFunction · 0.85

Calls 4

find_next_lineFunction · 0.85
normalize_markerFunction · 0.85
errorFunction · 0.85
strbuf_addFunction · 0.85

Tested by

no test coverage detected