@matheusbsbs / matheus-bagliano / commits / acb7257729
Fork 0

xdiff: Show function names in hunk headers.

The speed of the built-in diff generator is nice; but the function names shown by `diff -p' are /really/ nice. And I hate having to choose. So, we hack xdiff to find the function names and print them. xdiff has grown a flag to say whether to dig up the function names. The builtin_diff function passes this flag unconditionally. I suppose it could parse GIT_DIFF_OPTS, but it doesn't at the moment. I've also reintroduced the `function name' into the test suite, from which it was removed in commit 3ce8f089. The function names are parsed by a particularly stupid algorithm at the moment: it just tries to find a line in the `old' file, from before the start of the hunk, whose first character looks plausible. Still, it's most definitely a start. Signed-off-by: Mark Wooding <mdw@distorted.org.uk> Signed-off-by: Junio C Hamano <junkio@cox.net>

Mark Wooding committed Mar 28, 2006 at 03:23 UTC acb725772964ee11656543a28c303e9aa6d092c5
7 files changed +60 -6
diff.c
+1
@@ -267,6 +267,7 @@ static void builtin_diff(const char *name_a,
267 ecbdata.label_path = lbl;
268 xpp.flags = XDF_NEED_MINIMAL;
269 xecfg.ctxlen = 3;
270 + xecfg.flags = XDL_EMIT_FUNCNAMES;
271 if (!diffopts)
272 ;
273 else if (!strncmp(diffopts, "--unified=", 10))
t/t4001-diff-rename.sh
+1 -1
@@ -49,7 +49,7 @@ rename from path0
49 rename to path1
50 --- a/path0
51 +++ b/path1
52 -@@ -8,7 +8,7 @@
52 +@@ -8,7 +8,7 @@ Line 7
53 Line 8
54 Line 9
55 Line 10
xdiff/xdiff.h
+3
@@ -35,6 +35,8 @@ extern "C" {
35 #define XDL_PATCH_MODEMASK ((1 << 8) - 1)
36 #define XDL_PATCH_IGNOREBSPACE (1 << 8)
37
38 +#define XDL_EMIT_FUNCNAMES (1 << 0)
39 +
40 #define XDL_MMB_READONLY (1 << 0)
41
42 #define XDL_MMF_ATOMIC (1 << 0)
@@ -65,6 +67,7 @@ typedef struct s_xdemitcb {
67
68 typedef struct s_xdemitconf {
69 long ctxlen;
70 + unsigned long flags;
71 } xdemitconf_t;
72
73 typedef struct s_bdiffparam {
xdiff/xemit.c
+40 -1
@@ -69,10 +69,43 @@ static xdchange_t *xdl_get_hunk(xdchange_t *xscr, xdemitconf_t const *xecfg) {
69 }
70
71
72 +static void xdl_find_func(xdfile_t *xf, long i, char *buf, long sz, long *ll) {
73 +
74 + /*
75 + * Be quite stupid about this for now. Find a line in the old file
76 + * before the start of the hunk (and context) which starts with a
77 + * plausible character.
78 + */
79 +
80 + const char *rec;
81 + long len;
82 +
83 + *ll = 0;
84 + while (i-- > 0) {
85 + len = xdl_get_rec(xf, i, &rec);
86 + if (len > 0 &&
87 + (isalpha((unsigned char)*rec) || /* identifier? */
88 + *rec == '_' || /* also identifier? */
89 + *rec == '(' || /* lisp defun? */
90 + *rec == '#')) { /* #define? */
91 + if (len > sz)
92 + len = sz;
93 + if (len && rec[len - 1] == '\n')
94 + len--;
95 + memcpy(buf, rec, len);
96 + *ll = len;
97 + return;
98 + }
99 + }
100 +}
101 +
102 +
103 int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
104 xdemitconf_t const *xecfg) {
105 long s1, s2, e1, e2, lctx;
106 xdchange_t *xch, *xche;
107 + char funcbuf[40];
108 + long funclen = 0;
109
110 for (xch = xche = xscr; xch; xch = xche->next) {
111 xche = xdl_get_hunk(xch, xecfg);
@@ -90,7 +123,13 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
123 /*
124 * Emit current hunk header.
125 */
93 - if (xdl_emit_hunk_hdr(s1 + 1, e1 - s1, s2 + 1, e2 - s2, ecb) < 0)
126 +
127 + if (xecfg->flags & XDL_EMIT_FUNCNAMES) {
128 + xdl_find_func(&xe->xdf1, s1, funcbuf,
129 + sizeof(funcbuf), &funclen);
130 + }
131 + if (xdl_emit_hunk_hdr(s1 + 1, e1 - s1, s2 + 1, e2 - s2,
132 + funcbuf, funclen, ecb) < 0)
133 return -1;
134
135 /*
xdiff/xinclude.h
+1
@@ -23,6 +23,7 @@
23 #if !defined(XINCLUDE_H)
24 #define XINCLUDE_H
25
26 +#include <ctype.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
xdiff/xutils.c
+12 -3
@@ -235,7 +235,8 @@ long xdl_atol(char const *str, char const **next) {
235 }
236
237
238 -int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, xdemitcb_t *ecb) {
238 +int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2,
239 + const char *func, long funclen, xdemitcb_t *ecb) {
240 int nb = 0;
241 mmbuffer_t mb;
242 char buf[128];
@@ -264,8 +265,16 @@ int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, xdemitcb_t *ecb) {
265 nb += xdl_num_out(buf + nb, c2);
266 }
267
267 - memcpy(buf + nb, " @@\n", 4);
268 - nb += 4;
268 + memcpy(buf + nb, " @@", 3);
269 + nb += 3;
270 + if (func && funclen) {
271 + buf[nb++] = ' ';
272 + if (funclen > sizeof(buf) - nb - 1)
273 + funclen = sizeof(buf) - nb - 1;
274 + memcpy(buf + nb, func, funclen);
275 + nb += funclen;
276 + }
277 + buf[nb++] = '\n';
278
279 mb.ptr = buf;
280 mb.size = nb;
xdiff/xutils.h
+2 -1
@@ -36,7 +36,8 @@ unsigned long xdl_hash_record(char const **data, char const *top);
36 unsigned int xdl_hashbits(unsigned int size);
37 int xdl_num_out(char *out, long val);
38 long xdl_atol(char const *str, char const **next);
39 -int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, xdemitcb_t *ecb);
39 +int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2,
40 + const char *func, long funclen, xdemitcb_t *ecb);
41
42
43