diff --git a/contrib/libdiff/lib/diff_internal.h b/contrib/libdiff/lib/diff_internal.h index 46bbadf3cb64..16ee6776a71f 100644 --- a/contrib/libdiff/lib/diff_internal.h +++ b/contrib/libdiff/lib/diff_internal.h @@ -1,157 +1,157 @@ /* Generic infrastructure to implement various diff algorithms. */ /* * Copyright (c) 2020 Neels Hofmeyr * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #ifndef MAX #define MAX(A,B) ((A)>(B)?(A):(B)) #endif #ifndef MIN #define MIN(A,B) ((A)<(B)?(A):(B)) #endif static inline bool diff_range_empty(const struct diff_range *r) { return r->start == r->end; } static inline bool diff_ranges_touch(const struct diff_range *a, const struct diff_range *b) { return (a->end >= b->start) && (a->start <= b->end); } static inline void diff_ranges_merge(struct diff_range *a, const struct diff_range *b) { *a = (struct diff_range){ .start = MIN(a->start, b->start), .end = MAX(a->end, b->end), }; } static inline int diff_range_len(const struct diff_range *r) { if (!r) return 0; return r->end - r->start; } /* Indicate whether two given diff atoms match. */ int diff_atom_same(bool *same, const struct diff_atom *left, const struct diff_atom *right); /* A diff chunk represents a set of atoms on the left and/or a set of atoms on * the right. * * If solved == false: * The diff algorithm has divided the source file, and this is a chunk that the * inner_algo should run on next. * The lines on the left should be diffed against the lines on the right. * (If there are no left lines or no right lines, it implies solved == true, * because there is nothing to diff.) * * If solved == true: * If there are only left atoms, it is a chunk removing atoms from the left ("a * minus chunk"). * If there are only right atoms, it is a chunk adding atoms from the right ("a * plus chunk"). * If there are both left and right lines, it is a chunk of equal content on * both sides, and left_count == right_count: * * - foo } * - bar }-- diff_chunk{ left_start = &left.atoms.head[0], left_count = 3, * - baz } right_start = NULL, right_count = 0 } * moo } * goo }-- diff_chunk{ left_start = &left.atoms.head[3], left_count = 3, * zoo } right_start = &right.atoms.head[0], right_count = 3 } * +loo } * +roo }-- diff_chunk{ left_start = NULL, left_count = 0, * +too } right_start = &right.atoms.head[3], right_count = 3 } * */ struct diff_chunk { bool solved; struct diff_atom *left_start; unsigned int left_count; struct diff_atom *right_start; unsigned int right_count; }; #define DIFF_RESULT_ALLOC_BLOCKSIZE 128 struct diff_chunk_context; bool diff_chunk_context_empty(const struct diff_chunk_context *cc); bool diff_chunk_contexts_touch(const struct diff_chunk_context *cc, const struct diff_chunk_context *other); void diff_chunk_contexts_merge(struct diff_chunk_context *cc, const struct diff_chunk_context *other); struct diff_state { /* The final result passed to the original diff caller. */ struct diff_result *result; /* The root diff_data is in result->left,right, these are (possibly) * subsections of the root data. */ struct diff_data left; struct diff_data right; unsigned int recursion_depth_left; /* Remaining chunks from one diff algorithm pass, if any solved == false * chunks came up. */ diff_chunk_arraylist_t temp_result; /* State buffer used by Myers algorithm. */ int *kd_buf; size_t kd_buf_size; /* in units of sizeof(int), not bytes */ }; struct diff_chunk *diff_state_add_chunk(struct diff_state *state, bool solved, struct diff_atom *left_start, unsigned int left_count, struct diff_atom *right_start, unsigned int right_count); struct diff_output_info; int diff_output_lines(struct diff_output_info *output_info, FILE *dest, const char *prefix, struct diff_atom *start_atom, unsigned int count); int diff_output_trailing_newline_msg(struct diff_output_info *outinfo, FILE *dest, const struct diff_chunk *c); #define DIFF_FUNCTION_CONTEXT_SIZE 55 int diff_output_match_function_prototype(char *prototype, size_t prototype_size, int *last_prototype_idx, const struct diff_result *result, - const struct diff_chunk_context *cc); + int chunk_start_line); struct diff_output_info *diff_output_info_alloc(void); void diff_data_init_subsection(struct diff_data *d, struct diff_data *parent, struct diff_atom *from_atom, unsigned int atoms_count); diff --git a/contrib/libdiff/lib/diff_output.c b/contrib/libdiff/lib/diff_output.c index 7ac63bb6c433..78d9b8942077 100644 --- a/contrib/libdiff/lib/diff_output.c +++ b/contrib/libdiff/lib/diff_output.c @@ -1,371 +1,372 @@ /* Common parts for printing diff output */ /* * Copyright (c) 2020 Neels Hofmeyr * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include #include #include #include #include #include #include #include #include #include #include #include "diff_internal.h" static int get_atom_byte(int *ch, struct diff_atom *atom, off_t off) { off_t cur; if (atom->at != NULL) { *ch = atom->at[off]; return 0; } cur = ftello(atom->root->f); if (cur == -1) return errno; if (cur != atom->pos + off && fseeko(atom->root->f, atom->pos + off, SEEK_SET) == -1) return errno; *ch = fgetc(atom->root->f); if (*ch == EOF && ferror(atom->root->f)) return errno; return 0; } #define DIFF_OUTPUT_BUF_SIZE 512 int diff_output_lines(struct diff_output_info *outinfo, FILE *dest, const char *prefix, struct diff_atom *start_atom, unsigned int count) { struct diff_atom *atom; off_t outoff = 0, *offp; uint8_t *typep; int rc; if (outinfo && outinfo->line_offsets.len > 0) { unsigned int idx = outinfo->line_offsets.len - 1; outoff = outinfo->line_offsets.head[idx]; } foreach_diff_atom(atom, start_atom, count) { off_t outlen = 0; int i, ch, nbuf = 0; unsigned int len = atom->len; unsigned char buf[DIFF_OUTPUT_BUF_SIZE + 1 /* '\n' */]; size_t n; n = strlcpy(buf, prefix, sizeof(buf)); if (n >= DIFF_OUTPUT_BUF_SIZE) /* leave room for '\n' */ return ENOBUFS; nbuf += n; if (len) { rc = get_atom_byte(&ch, atom, len - 1); if (rc) return rc; if (ch == '\n') len--; } for (i = 0; i < len; i++) { rc = get_atom_byte(&ch, atom, i); if (rc) return rc; if (nbuf >= DIFF_OUTPUT_BUF_SIZE) { rc = fwrite(buf, 1, nbuf, dest); if (rc != nbuf) return errno; outlen += rc; nbuf = 0; } buf[nbuf++] = ch; } buf[nbuf++] = '\n'; rc = fwrite(buf, 1, nbuf, dest); if (rc != nbuf) return errno; outlen += rc; if (outinfo) { ARRAYLIST_ADD(offp, outinfo->line_offsets); if (offp == NULL) return ENOMEM; outoff += outlen; *offp = outoff; ARRAYLIST_ADD(typep, outinfo->line_types); if (typep == NULL) return ENOMEM; *typep = *prefix == ' ' ? DIFF_LINE_CONTEXT : *prefix == '-' ? DIFF_LINE_MINUS : *prefix == '+' ? DIFF_LINE_PLUS : DIFF_LINE_NONE; } } return DIFF_RC_OK; } int diff_output_chunk_left_version(struct diff_output_info **output_info, FILE *dest, const struct diff_input_info *info, const struct diff_result *result, const struct diff_chunk_context *cc) { int rc, c_idx; struct diff_output_info *outinfo = NULL; if (diff_range_empty(&cc->left)) return DIFF_RC_OK; if (output_info) { *output_info = diff_output_info_alloc(); if (*output_info == NULL) return ENOMEM; outinfo = *output_info; } /* Write out all chunks on the left side. */ for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) { const struct diff_chunk *c = &result->chunks.head[c_idx]; if (c->left_count) { rc = diff_output_lines(outinfo, dest, "", c->left_start, c->left_count); if (rc) return rc; } } return DIFF_RC_OK; } int diff_output_chunk_right_version(struct diff_output_info **output_info, FILE *dest, const struct diff_input_info *info, const struct diff_result *result, const struct diff_chunk_context *cc) { int rc, c_idx; struct diff_output_info *outinfo = NULL; if (diff_range_empty(&cc->right)) return DIFF_RC_OK; if (output_info) { *output_info = diff_output_info_alloc(); if (*output_info == NULL) return ENOMEM; outinfo = *output_info; } /* Write out all chunks on the right side. */ for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) { const struct diff_chunk *c = &result->chunks.head[c_idx]; if (c->right_count) { rc = diff_output_lines(outinfo, dest, "", c->right_start, c->right_count); if (rc) return rc; } } return DIFF_RC_OK; } int diff_output_trailing_newline_msg(struct diff_output_info *outinfo, FILE *dest, const struct diff_chunk *c) { enum diff_chunk_type chunk_type = diff_chunk_type(c); struct diff_atom *atom, *start_atom; unsigned int atom_count; int rc, ch; off_t outoff = 0, *offp; uint8_t *typep; if (chunk_type == CHUNK_MINUS || chunk_type == CHUNK_SAME) { start_atom = c->left_start; atom_count = c->left_count; } else if (chunk_type == CHUNK_PLUS) { start_atom = c->right_start; atom_count = c->right_count; } else return EINVAL; /* Locate the last atom. */ if (atom_count == 0) return EINVAL; atom = &start_atom[atom_count - 1]; rc = get_atom_byte(&ch, atom, atom->len - 1); if (rc != DIFF_RC_OK) return rc; if (ch != '\n') { if (outinfo && outinfo->line_offsets.len > 0) { unsigned int idx = outinfo->line_offsets.len - 1; outoff = outinfo->line_offsets.head[idx]; } rc = fprintf(dest, "\\ No newline at end of file\n"); if (rc < 0) return errno; if (outinfo) { ARRAYLIST_ADD(offp, outinfo->line_offsets); if (offp == NULL) return ENOMEM; outoff += rc; *offp = outoff; ARRAYLIST_ADD(typep, outinfo->line_types); if (typep == NULL) return ENOMEM; *typep = DIFF_LINE_NONE; } } return DIFF_RC_OK; } static bool is_function_prototype(unsigned char ch) { - return (isalpha((unsigned char)ch) || ch == '_' || ch == '$'); + return (isalpha((unsigned char)ch) || ch == '_' || ch == '$' || + ch == '-' || ch == '+'); } #define begins_with(s, pre) (strncmp(s, pre, sizeof(pre)-1) == 0) int diff_output_match_function_prototype(char *prototype, size_t prototype_size, int *last_prototype_idx, const struct diff_result *result, - const struct diff_chunk_context *cc) + int chunk_start_line) { struct diff_atom *start_atom, *atom; const struct diff_data *data; unsigned char buf[DIFF_FUNCTION_CONTEXT_SIZE]; const char *state = NULL; int rc, i, ch; - if (result->left->atoms.len > 0 && cc->left.start > 0) { + if (result->left->atoms.len > 0 && chunk_start_line > 0) { data = result->left; - start_atom = &data->atoms.head[cc->left.start - 1]; + start_atom = &data->atoms.head[chunk_start_line - 1]; } else return DIFF_RC_OK; diff_data_foreach_atom_backwards_from(start_atom, atom, data) { int atom_idx = diff_atom_root_idx(data, atom); if (atom_idx < *last_prototype_idx) break; rc = get_atom_byte(&ch, atom, 0); if (rc) return rc; buf[0] = (unsigned char)ch; if (!is_function_prototype(buf[0])) continue; for (i = 1; i < atom->len && i < sizeof(buf) - 1; i++) { rc = get_atom_byte(&ch, atom, i); if (rc) return rc; if (ch == '\n') break; buf[i] = (unsigned char)ch; } buf[i] = '\0'; if (begins_with(buf, "private:")) { if (!state) state = " (private)"; } else if (begins_with(buf, "protected:")) { if (!state) state = " (protected)"; } else if (begins_with(buf, "public:")) { if (!state) state = " (public)"; } else { if (state) /* don't care about truncation */ strlcat(buf, state, sizeof(buf)); strlcpy(prototype, buf, prototype_size); break; } } *last_prototype_idx = diff_atom_root_idx(data, start_atom); return DIFF_RC_OK; } struct diff_output_info * diff_output_info_alloc(void) { struct diff_output_info *output_info; off_t *offp; uint8_t *typep; output_info = malloc(sizeof(*output_info)); if (output_info != NULL) { ARRAYLIST_INIT(output_info->line_offsets, 128); ARRAYLIST_ADD(offp, output_info->line_offsets); if (offp == NULL) { diff_output_info_free(output_info); return NULL; } *offp = 0; ARRAYLIST_INIT(output_info->line_types, 128); ARRAYLIST_ADD(typep, output_info->line_types); if (typep == NULL) { diff_output_info_free(output_info); return NULL; } *typep = DIFF_LINE_NONE; } return output_info; } void diff_output_info_free(struct diff_output_info *output_info) { ARRAYLIST_FREE(output_info->line_offsets); ARRAYLIST_FREE(output_info->line_types); free(output_info); } const char * diff_output_get_label_left(const struct diff_input_info *info) { if (info->flags & DIFF_INPUT_LEFT_NONEXISTENT) return "/dev/null"; return info->left_path ? info->left_path : "a"; } const char * diff_output_get_label_right(const struct diff_input_info *info) { if (info->flags & DIFF_INPUT_RIGHT_NONEXISTENT) return "/dev/null"; return info->right_path ? info->right_path : "b"; } diff --git a/contrib/libdiff/lib/diff_output_unidiff.c b/contrib/libdiff/lib/diff_output_unidiff.c index d480a022a9a7..88c98c663c00 100644 --- a/contrib/libdiff/lib/diff_output_unidiff.c +++ b/contrib/libdiff/lib/diff_output_unidiff.c @@ -1,602 +1,602 @@ /* Produce a unidiff output from a diff_result. */ /* * Copyright (c) 2020 Neels Hofmeyr * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include #include #include #include #include #include #include #include #include #include #include "diff_internal.h" #include "diff_debug.h" off_t diff_chunk_get_left_start_pos(const struct diff_chunk *c) { return c->left_start->pos; } off_t diff_chunk_get_right_start_pos(const struct diff_chunk *c) { return c->right_start->pos; } bool diff_chunk_context_empty(const struct diff_chunk_context *cc) { return diff_range_empty(&cc->chunk); } int diff_chunk_get_left_start(const struct diff_chunk *c, const struct diff_result *r, int context_lines) { int left_start = diff_atom_root_idx(r->left, c->left_start); return MAX(0, left_start - context_lines); } int diff_chunk_get_left_end(const struct diff_chunk *c, const struct diff_result *r, int context_lines) { int left_start = diff_chunk_get_left_start(c, r, 0); return MIN(r->left->atoms.len, left_start + c->left_count + context_lines); } int diff_chunk_get_right_start(const struct diff_chunk *c, const struct diff_result *r, int context_lines) { int right_start = diff_atom_root_idx(r->right, c->right_start); return MAX(0, right_start - context_lines); } int diff_chunk_get_right_end(const struct diff_chunk *c, const struct diff_result *r, int context_lines) { int right_start = diff_chunk_get_right_start(c, r, 0); return MIN(r->right->atoms.len, right_start + c->right_count + context_lines); } struct diff_chunk * diff_chunk_get(const struct diff_result *r, int chunk_idx) { return &r->chunks.head[chunk_idx]; } int diff_chunk_get_left_count(struct diff_chunk *c) { return c->left_count; } int diff_chunk_get_right_count(struct diff_chunk *c) { return c->right_count; } void diff_chunk_context_get(struct diff_chunk_context *cc, const struct diff_result *r, int chunk_idx, int context_lines) { const struct diff_chunk *c = &r->chunks.head[chunk_idx]; int left_start = diff_chunk_get_left_start(c, r, context_lines); int left_end = diff_chunk_get_left_end(c, r, context_lines); int right_start = diff_chunk_get_right_start(c, r, context_lines); int right_end = diff_chunk_get_right_end(c, r, context_lines); *cc = (struct diff_chunk_context){ .chunk = { .start = chunk_idx, .end = chunk_idx + 1, }, .left = { .start = left_start, .end = left_end, }, .right = { .start = right_start, .end = right_end, }, }; } bool diff_chunk_contexts_touch(const struct diff_chunk_context *cc, const struct diff_chunk_context *other) { return diff_ranges_touch(&cc->chunk, &other->chunk) || diff_ranges_touch(&cc->left, &other->left) || diff_ranges_touch(&cc->right, &other->right); } void diff_chunk_contexts_merge(struct diff_chunk_context *cc, const struct diff_chunk_context *other) { diff_ranges_merge(&cc->chunk, &other->chunk); diff_ranges_merge(&cc->left, &other->left); diff_ranges_merge(&cc->right, &other->right); } void diff_chunk_context_load_change(struct diff_chunk_context *cc, int *nchunks_used, struct diff_result *result, int start_chunk_idx, int context_lines) { int i; int seen_minus = 0, seen_plus = 0; if (nchunks_used) *nchunks_used = 0; for (i = start_chunk_idx; i < result->chunks.len; i++) { struct diff_chunk *chunk = &result->chunks.head[i]; enum diff_chunk_type t = diff_chunk_type(chunk); struct diff_chunk_context next; if (t != CHUNK_MINUS && t != CHUNK_PLUS) { if (nchunks_used) (*nchunks_used)++; if (seen_minus || seen_plus) break; else continue; } else if (t == CHUNK_MINUS) seen_minus = 1; else if (t == CHUNK_PLUS) seen_plus = 1; if (diff_chunk_context_empty(cc)) { /* Note down the start point, any number of subsequent * chunks may be joined up to this chunk by being * directly adjacent. */ diff_chunk_context_get(cc, result, i, context_lines); if (nchunks_used) (*nchunks_used)++; continue; } /* There already is a previous chunk noted down for being * printed. Does it join up with this one? */ diff_chunk_context_get(&next, result, i, context_lines); if (diff_chunk_contexts_touch(cc, &next)) { /* This next context touches or overlaps the previous * one, join. */ diff_chunk_contexts_merge(cc, &next); if (nchunks_used) (*nchunks_used)++; continue; } else break; } } struct diff_output_unidiff_state { bool header_printed; char prototype[DIFF_FUNCTION_CONTEXT_SIZE]; int last_prototype_idx; }; struct diff_output_unidiff_state * diff_output_unidiff_state_alloc(void) { struct diff_output_unidiff_state *state; state = calloc(1, sizeof(struct diff_output_unidiff_state)); if (state != NULL) diff_output_unidiff_state_reset(state); return state; } void diff_output_unidiff_state_reset(struct diff_output_unidiff_state *state) { state->header_printed = false; memset(state->prototype, 0, sizeof(state->prototype)); state->last_prototype_idx = 0; } void diff_output_unidiff_state_free(struct diff_output_unidiff_state *state) { free(state); } static int output_unidiff_chunk(struct diff_output_info *outinfo, FILE *dest, struct diff_output_unidiff_state *state, const struct diff_input_info *info, const struct diff_result *result, bool print_header, bool show_function_prototypes, const struct diff_chunk_context *cc) { int rc, left_start, left_len, right_start, right_len; off_t outoff = 0, *offp; uint8_t *typep; if (diff_range_empty(&cc->left) && diff_range_empty(&cc->right)) return DIFF_RC_OK; if (outinfo && outinfo->line_offsets.len > 0) { unsigned int idx = outinfo->line_offsets.len - 1; outoff = outinfo->line_offsets.head[idx]; } if (print_header && !(state->header_printed)) { rc = fprintf(dest, "--- %s\n", diff_output_get_label_left(info)); if (rc < 0) return errno; if (outinfo) { ARRAYLIST_ADD(offp, outinfo->line_offsets); if (offp == NULL) return ENOMEM; outoff += rc; *offp = outoff; ARRAYLIST_ADD(typep, outinfo->line_types); if (typep == NULL) return ENOMEM; *typep = DIFF_LINE_MINUS; } rc = fprintf(dest, "+++ %s\n", diff_output_get_label_right(info)); if (rc < 0) return errno; if (outinfo) { ARRAYLIST_ADD(offp, outinfo->line_offsets); if (offp == NULL) return ENOMEM; outoff += rc; *offp = outoff; ARRAYLIST_ADD(typep, outinfo->line_types); if (typep == NULL) return ENOMEM; *typep = DIFF_LINE_PLUS; } state->header_printed = true; } left_len = cc->left.end - cc->left.start; if (result->left->atoms.len == 0) left_start = 0; else if (left_len == 0 && cc->left.start > 0) left_start = cc->left.start; else left_start = cc->left.start + 1; right_len = cc->right.end - cc->right.start; if (result->right->atoms.len == 0) right_start = 0; else if (right_len == 0 && cc->right.start > 0) right_start = cc->right.start; else right_start = cc->right.start + 1; + /* Got the absolute line numbers where to start printing, and the index + * of the interesting (non-context) chunk. + * To print context lines above the interesting chunk, nipping on the + * previous chunk index may be necessary. + * It is guaranteed to be only context lines where left == right, so it + * suffices to look on the left. */ + const struct diff_chunk *first_chunk; + int chunk_start_line; + first_chunk = &result->chunks.head[cc->chunk.start]; + chunk_start_line = diff_atom_root_idx(result->left, + first_chunk->left_start); if (show_function_prototypes) { rc = diff_output_match_function_prototype(state->prototype, sizeof(state->prototype), &state->last_prototype_idx, - result, cc); + result, chunk_start_line); if (rc) return rc; } if (left_len == 1 && right_len == 1) { rc = fprintf(dest, "@@ -%d +%d @@%s%s\n", left_start, right_start, state->prototype[0] ? " " : "", state->prototype[0] ? state->prototype : ""); } else if (left_len == 1 && right_len != 1) { rc = fprintf(dest, "@@ -%d +%d,%d @@%s%s\n", left_start, right_start, right_len, state->prototype[0] ? " " : "", state->prototype[0] ? state->prototype : ""); } else if (left_len != 1 && right_len == 1) { rc = fprintf(dest, "@@ -%d,%d +%d @@%s%s\n", left_start, left_len, right_start, state->prototype[0] ? " " : "", state->prototype[0] ? state->prototype : ""); } else { rc = fprintf(dest, "@@ -%d,%d +%d,%d @@%s%s\n", left_start, left_len, right_start, right_len, state->prototype[0] ? " " : "", state->prototype[0] ? state->prototype : ""); } if (rc < 0) return errno; if (outinfo) { ARRAYLIST_ADD(offp, outinfo->line_offsets); if (offp == NULL) return ENOMEM; outoff += rc; *offp = outoff; ARRAYLIST_ADD(typep, outinfo->line_types); if (typep == NULL) return ENOMEM; *typep = DIFF_LINE_HUNK; } - /* Got the absolute line numbers where to start printing, and the index - * of the interesting (non-context) chunk. - * To print context lines above the interesting chunk, nipping on the - * previous chunk index may be necessary. - * It is guaranteed to be only context lines where left == right, so it - * suffices to look on the left. */ - const struct diff_chunk *first_chunk; - int chunk_start_line; - first_chunk = &result->chunks.head[cc->chunk.start]; - chunk_start_line = diff_atom_root_idx(result->left, - first_chunk->left_start); if (cc->left.start < chunk_start_line) { rc = diff_output_lines(outinfo, dest, " ", &result->left->atoms.head[cc->left.start], chunk_start_line - cc->left.start); if (rc) return rc; } /* Now write out all the joined chunks and contexts between them */ int c_idx; for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) { const struct diff_chunk *c = &result->chunks.head[c_idx]; if (c->left_count && c->right_count) rc = diff_output_lines(outinfo, dest, c->solved ? " " : "?", c->left_start, c->left_count); else if (c->left_count && !c->right_count) rc = diff_output_lines(outinfo, dest, c->solved ? "-" : "?", c->left_start, c->left_count); else if (c->right_count && !c->left_count) rc = diff_output_lines(outinfo, dest, c->solved ? "+" : "?", c->right_start, c->right_count); if (rc) return rc; if (cc->chunk.end == result->chunks.len) { rc = diff_output_trailing_newline_msg(outinfo, dest, c); if (rc != DIFF_RC_OK) return rc; } } /* Trailing context? */ const struct diff_chunk *last_chunk; int chunk_end_line; last_chunk = &result->chunks.head[cc->chunk.end - 1]; chunk_end_line = diff_atom_root_idx(result->left, last_chunk->left_start + last_chunk->left_count); if (cc->left.end > chunk_end_line) { rc = diff_output_lines(outinfo, dest, " ", &result->left->atoms.head[chunk_end_line], cc->left.end - chunk_end_line); if (rc) return rc; if (cc->left.end == result->left->atoms.len) { rc = diff_output_trailing_newline_msg(outinfo, dest, &result->chunks.head[result->chunks.len - 1]); if (rc != DIFF_RC_OK) return rc; } } return DIFF_RC_OK; } int diff_output_unidiff_chunk(struct diff_output_info **output_info, FILE *dest, struct diff_output_unidiff_state *state, const struct diff_input_info *info, const struct diff_result *result, const struct diff_chunk_context *cc) { struct diff_output_info *outinfo = NULL; int flags = (result->left->root->diff_flags | result->right->root->diff_flags); bool show_function_prototypes = (flags & DIFF_FLAG_SHOW_PROTOTYPES); if (output_info) { *output_info = diff_output_info_alloc(); if (*output_info == NULL) return ENOMEM; outinfo = *output_info; } return output_unidiff_chunk(outinfo, dest, state, info, result, false, show_function_prototypes, cc); } int diff_output_unidiff(struct diff_output_info **output_info, FILE *dest, const struct diff_input_info *info, const struct diff_result *result, unsigned int context_lines) { struct diff_output_unidiff_state *state; struct diff_chunk_context cc = {}; struct diff_output_info *outinfo = NULL; int atomizer_flags = (result->left->atomizer_flags| result->right->atomizer_flags); int flags = (result->left->root->diff_flags | result->right->root->diff_flags); bool show_function_prototypes = (flags & DIFF_FLAG_SHOW_PROTOTYPES); bool force_text = (flags & DIFF_FLAG_FORCE_TEXT_DATA); bool have_binary = (atomizer_flags & DIFF_ATOMIZER_FOUND_BINARY_DATA); off_t outoff = 0, *offp; uint8_t *typep; int rc, i; if (!result) return EINVAL; if (result->rc != DIFF_RC_OK) return result->rc; if (output_info) { *output_info = diff_output_info_alloc(); if (*output_info == NULL) return ENOMEM; outinfo = *output_info; } if (have_binary && !force_text) { for (i = 0; i < result->chunks.len; i++) { struct diff_chunk *c = &result->chunks.head[i]; enum diff_chunk_type t = diff_chunk_type(c); if (t != CHUNK_MINUS && t != CHUNK_PLUS) continue; if (outinfo && outinfo->line_offsets.len > 0) { unsigned int idx = outinfo->line_offsets.len - 1; outoff = outinfo->line_offsets.head[idx]; } rc = fprintf(dest, "Binary files %s and %s differ\n", diff_output_get_label_left(info), diff_output_get_label_right(info)); if (outinfo) { ARRAYLIST_ADD(offp, outinfo->line_offsets); if (offp == NULL) return ENOMEM; outoff += rc; *offp = outoff; ARRAYLIST_ADD(typep, outinfo->line_types); if (typep == NULL) return ENOMEM; *typep = DIFF_LINE_NONE; } break; } return DIFF_RC_OK; } state = diff_output_unidiff_state_alloc(); if (state == NULL) { if (output_info) { diff_output_info_free(*output_info); *output_info = NULL; } return ENOMEM; } #if DEBUG unsigned int check_left_pos, check_right_pos; check_left_pos = 0; check_right_pos = 0; for (i = 0; i < result->chunks.len; i++) { struct diff_chunk *c = &result->chunks.head[i]; enum diff_chunk_type t = diff_chunk_type(c); debug("[%d] %s lines L%d R%d @L %d @R %d\n", i, (t == CHUNK_MINUS ? "minus" : (t == CHUNK_PLUS ? "plus" : (t == CHUNK_SAME ? "same" : "?"))), c->left_count, c->right_count, c->left_start ? diff_atom_root_idx(result->left, c->left_start) : -1, c->right_start ? diff_atom_root_idx(result->right, c->right_start) : -1); assert(check_left_pos == diff_atom_root_idx(result->left, c->left_start)); assert(check_right_pos == diff_atom_root_idx(result->right, c->right_start)); check_left_pos += c->left_count; check_right_pos += c->right_count; } assert(check_left_pos == result->left->atoms.len); assert(check_right_pos == result->right->atoms.len); #endif for (i = 0; i < result->chunks.len; i++) { struct diff_chunk *c = &result->chunks.head[i]; enum diff_chunk_type t = diff_chunk_type(c); struct diff_chunk_context next; if (t != CHUNK_MINUS && t != CHUNK_PLUS) continue; if (diff_chunk_context_empty(&cc)) { /* These are the first lines being printed. * Note down the start point, any number of subsequent * chunks may be joined up to this unidiff chunk by * context lines or by being directly adjacent. */ diff_chunk_context_get(&cc, result, i, context_lines); debug("new chunk to be printed:" " chunk %d-%d left %d-%d right %d-%d\n", cc.chunk.start, cc.chunk.end, cc.left.start, cc.left.end, cc.right.start, cc.right.end); continue; } /* There already is a previous chunk noted down for being * printed. Does it join up with this one? */ diff_chunk_context_get(&next, result, i, context_lines); debug("new chunk to be printed:" " chunk %d-%d left %d-%d right %d-%d\n", next.chunk.start, next.chunk.end, next.left.start, next.left.end, next.right.start, next.right.end); if (diff_chunk_contexts_touch(&cc, &next)) { /* This next context touches or overlaps the previous * one, join. */ diff_chunk_contexts_merge(&cc, &next); debug("new chunk to be printed touches previous chunk," " now: left %d-%d right %d-%d\n", cc.left.start, cc.left.end, cc.right.start, cc.right.end); continue; } /* No touching, so the previous context is complete with a gap * between it and this next one. Print the previous one and * start fresh here. */ debug("new chunk to be printed does not touch previous chunk;" " print left %d-%d right %d-%d\n", cc.left.start, cc.left.end, cc.right.start, cc.right.end); output_unidiff_chunk(outinfo, dest, state, info, result, true, show_function_prototypes, &cc); cc = next; debug("new unprinted chunk is left %d-%d right %d-%d\n", cc.left.start, cc.left.end, cc.right.start, cc.right.end); } if (!diff_chunk_context_empty(&cc)) output_unidiff_chunk(outinfo, dest, state, info, result, true, show_function_prototypes, &cc); diff_output_unidiff_state_free(state); return DIFF_RC_OK; }