commit dc306c6bd88271ab911e205539974da98be82d17 from: Stefan Sperling date: Tue Aug 29 13:18:21 2023 UTC back out "improve accuracy of function prototype in hunk headers" which was commit 8993f42562696079866fc2dec7191877b6cc1f18 This commit introduced a regression where hunk headers would contain lines taken from below or within a change. Reported by tb@ and sthen@. There is some confusion around how the -p feature was documented: -p With unified and context diffs, show with each change the first 40 characters of the last line before the context beginning with a letter, an underscore or a dollar sign. For C source code following standard layout conventions, this will show the prototype of the function the change applies to. The original behaviour matches "the last line before the context beginning with a letter ...". The new (reverted) behaviour sometimes matches "will show the prototype of the function the change applies to". This did always happen as intended, as seen when some .c files in the OpenBSD src tree were changed. The new behaviour was also worse for Makefiles in the OpenBSD ports tree. Let's revert to the traditional -p behaviour until a better fix is found. Details: https://marc.gameoftrees.org/mail/1692696001.69872_0.html https://marc.info/?l=gameoftrees&m=169269624516330&w=2 ok tb@ commit - f46fa9b5e78156c0360f508336dbe3dfa04b503f commit + dc306c6bd88271ab911e205539974da98be82d17 blob - 6855889d9612f734106a574e7b7ff947b56823d5 blob + 46bbadf3cb6493dd4afd224d56cf96436b9194d6 --- lib/diff_internal.h +++ lib/diff_internal.h @@ -148,8 +148,7 @@ int diff_output_trailing_newline_msg(struct diff_outpu 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, - unsigned int ncontext); + const struct diff_chunk_context *cc); struct diff_output_info *diff_output_info_alloc(void); blob - f44e799638f771a0588c11e3d5b6b1200b5c64f1 blob + 7ac63bb6c433b17070e64af301c8039c4a1aefbe --- lib/diff_output.c +++ lib/diff_output.c @@ -263,19 +263,17 @@ is_function_prototype(unsigned char ch) 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, unsigned int ncontext) + const struct diff_chunk_context *cc) { 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, idx; - - idx = MIN(cc->left.start + (ncontext ? ncontext : 0), cc->left.end - 1); + int rc, i, ch; if (result->left->atoms.len > 0 && cc->left.start > 0) { data = result->left; - start_atom = &data->atoms.head[idx]; + start_atom = &data->atoms.head[cc->left.start - 1]; } else return DIFF_RC_OK; blob - 4757099644a9c1eec21e1af9102ba13ba417d609 blob + 9b8e02dc9f20f06d14deba7d2ac739a0a2f8f20a --- lib/diff_output_unidiff.c +++ lib/diff_output_unidiff.c @@ -237,7 +237,7 @@ output_unidiff_chunk(struct diff_output_info *outinfo, const struct diff_input_info *info, const struct diff_result *result, bool print_header, bool show_function_prototypes, - const struct diff_chunk_context *cc, unsigned int ncontext) + const struct diff_chunk_context *cc) { int rc, left_start, left_len, right_start, right_len; off_t outoff = 0, *offp; @@ -304,7 +304,7 @@ output_unidiff_chunk(struct diff_output_info *outinfo, if (show_function_prototypes) { rc = diff_output_match_function_prototype(state->prototype, sizeof(state->prototype), &state->last_prototype_idx, - result, cc, ncontext); + result, cc); if (rc) return rc; } @@ -433,7 +433,7 @@ diff_output_unidiff_chunk(struct diff_output_info **ou } return output_unidiff_chunk(outinfo, dest, state, info, - result, false, show_function_prototypes, cc, 0); + result, false, show_function_prototypes, cc); } int @@ -586,7 +586,7 @@ diff_output_unidiff(struct diff_output_info **output_i " 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, context_lines); + 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); @@ -594,7 +594,7 @@ diff_output_unidiff(struct diff_output_info **output_i if (!diff_chunk_context_empty(&cc)) output_unidiff_chunk(outinfo, dest, state, info, result, - true, show_function_prototypes, &cc, context_lines); + true, show_function_prototypes, &cc); diff_output_unidiff_state_free(state); return DIFF_RC_OK; } blob - 2ae051a0a1e04a0e219eb0a362d5d74f2039a76f blob + 82713c206aff6bb75421e50f6e9f3ee6a9ca4239 --- test/expect124.diff +++ test/expect124.diff @@ -1,6 +1,6 @@ --- test124.left-p.txt +++ test124.right-p.txt -@@ -11,5 +11,5 @@ return_test(int test) { +@@ -11,5 +11,5 @@ doSomethingThenPrintHello(int test) struct testfile * return_test(int test) {