When adding a new function to the end of a file, it's enough to know
that 1) the addition is at the end of the file; and 2) there is a
function _somewhere_ in there.
If we had simply been changing the end of an existing function, then we
would also be deleting something from the old version.
This fixes the case where we add e.g.
// Begin of dummy
static int dummy(void)
{
}
to the end of the file.
Cc: René Scharfe <redacted>
Signed-off-by: Vegard Nossum <redacted>
---
xdiff/xemit.c | 14 ++++++--------
1 file changed, 6 insertions(+), 8 deletions(-)
When using -W to include the whole function in the diff context, you
are typically doing this to be able to review the change in its entirety
within the context of the function. It is therefore almost always
desirable to include any comments that immediately precede the function.
This also the fixes the case for C where the declaration is split across
multiple lines (where the first line of the declaration would not be
included in the output), e.g.:
void
dummy(void)
{
...
}
We can include these lines by simply scanning upwards from the place of
the detected function start until we hit the first non-blank line.
Cc: René Scharfe <redacted>
Signed-off-by: Vegard Nossum <redacted>
---
xdiff/xemit.c | 4 ++++
1 file changed, 4 insertions(+)
From: René Scharfe <hidden> Date: 2017-01-13 18:20:14
Am 13.01.2017 um 17:15 schrieb Vegard Nossum:
When using -W to include the whole function in the diff context, you
are typically doing this to be able to review the change in its entirety
within the context of the function. It is therefore almost always
desirable to include any comments that immediately precede the function.
This also the fixes the case for C where the declaration is split across
multiple lines (where the first line of the declaration would not be
included in the output), e.g.:
void
dummy(void)
{
...
}
That's true, but I'm not sure "non-empty line before function line" is
good enough a definition for desirable lines. It wouldn't work for
people who don't believe in empty lines. Or for those that put a blank
line between comment and function. (I have an opinion on such habits,
but git diff should probably stay neutral.) And that's just for C code;
I have no idea how this heuristic would hold up for other file types
like HTML.
We can identify function lines with arbitrary precision (with a
xfuncname regex, if needed), but there is no accurate way to classify
lines as comments, or as the end of functions. Adding optional regexes
for single- and multi-line comments would help, at least for C.
René
From: Stefan Beller <hidden> Date: 2017-01-13 18:44:08
On Fri, Jan 13, 2017 at 10:19 AM, René Scharfe [off-list ref] wrote:
Am 13.01.2017 um 17:15 schrieb Vegard Nossum:
quoted
When using -W to include the whole function in the diff context, you
are typically doing this to be able to review the change in its entirety
within the context of the function. It is therefore almost always
desirable to include any comments that immediately precede the function.
Do we need a small comment in the actual code to hint at why we count
upwards there?
quoted
This also the fixes the case for C where the declaration is split across
multiple lines (where the first line of the declaration would not be
included in the output), e.g.:
void
dummy(void)
{
...
}
That's true, but I'm not sure "non-empty line before function line" is good
enough a definition for desirable lines. It wouldn't work for people who
don't believe in empty lines. Or for those that put a blank line between
comment and function. (I have an opinion on such habits, but git diff
should probably stay neutral.) And that's just for C code; I have no idea
how this heuristic would hold up for other file types like HTML.
I think empty lines are "good as a first approach", see e.g.
433860f3d0beb0c6 the "compaction" heuristic for a similar
thing (the compaction was introduced at d634d61ed), and then
we can build a more elaborate thing on top.
We can identify function lines with arbitrary precision (with a xfuncname
regex, if needed), but there is no accurate way to classify lines as
comments, or as the end of functions. Adding optional regexes for single-
and multi-line comments would help, at least for C.
That would cover Java and whole lot of other C like languages. So a good
start as well IMHO.
We now include non-empty lines immediately before (and after) a function
as belonging to the function.
We can test this new functionality by moving the "// Begin" markers on
each function to the previous line.
This commit is intentionally not part of the previous commits in order
to show that the tests do not break even when changing the behaviour of
'diff -W' in the previous commits.
Cc: René Scharfe <redacted>
Signed-off-by: Vegard Nossum <redacted>
---
t/t4051-diff-function-context.sh | 2 +-
t/t4051/appended1.c | 3 ++-
t/t4051/dummy.c | 3 ++-
t/t4051/hello.c | 3 ++-
4 files changed, 7 insertions(+), 4 deletions(-)
@@ -72,7 +72,7 @@ test_expect_success 'setup' '# overlap function context of 1st change and -u context of 2nd changegrep-v"delete me from hello"<"$dir/hello.c">file.c&&-sed2p<"$dir/dummy.c">>file.c&&+sed3p<"$dir/dummy.c">>file.c&&commit_and_tagchanged_hello_dummyfile.c&&gitcheckoutinitial&&
From: René Scharfe <hidden> Date: 2017-01-13 17:57:04
Am 13.01.2017 um 17:15 schrieb Vegard Nossum:
When adding a new function to the end of a file, it's enough to know
that 1) the addition is at the end of the file; and 2) there is a
function _somewhere_ in there.
If we had simply been changing the end of an existing function, then we
would also be deleting something from the old version.
That makes sense, thanks.
This fixes the case where we add e.g.
// Begin of dummy
static int dummy(void)
{
}
to the end of the file.
Without this patch the unchanged function before the added lines is
shown in its entirety as (uncalled for) context.
Nit: I don't like the indentation here. Giving "dummy" its own line is
also not exactly pretty, but at least would allow the parameters to be
aligned on the opening parenthesis.
+ goto post_context_calculation;
i2++;
- if (i2 < xe->xdf2.nrec &&
- match_func_rec(&xe->xdf2, xecfg, i2,
- dummy, sizeof(dummy)) >= 0)
- goto post_context_calculation;
+ }
/*
* Otherwise get more context from the