@matheusbsbs / matheus-bagliano / commits / 3ce8f08944
Fork 0

built-in diff: minimum tweaks

This fixes up a couple of minor issues with the real built-in diff to be more usable: - Omit ---/+++ header unless we emit diff output; - Detect and punt binary diff like GNU does; - Honor GIT_DIFF_OPTS minimally (only -u<number> and --unified=<number> are currently supported); - Omit line count of 1 from "@@ -l,k +m,n @@" hunk header (i.e. when k == 1 or n == 1) - Adjust testsuite for the lack of -p support. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Mar 25, 2006 at 12:16 UTC 3ce8f08944e614813c7dfb212a88b7c4e3b443bc
3 files changed +47 -12
diff.c
+36 -5
@@ -212,16 +212,37 @@ static int fill_mmfile(mmfile_t *mf, const char *file)
212 return 0;
213 }
214
215 +struct emit_callback {
216 + const char **label_path;
217 +};
218 +
219 static int fn_out(void *priv, mmbuffer_t *mb, int nbuf)
220 {
221 int i;
222 + struct emit_callback *ecbdata = priv;
223
224 + if (ecbdata->label_path[0]) {
225 + printf("--- %s\n", ecbdata->label_path[0]);
226 + printf("+++ %s\n", ecbdata->label_path[1]);
227 + ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
228 + }
229 for (i = 0; i < nbuf; i++)
230 if (!fwrite(mb[i].ptr, mb[i].size, 1, stdout))
231 return -1;
232 return 0;
233 }
234
235 +#define FIRST_FEW_BYTES 8000
236 +static int mmfile_is_binary(mmfile_t *mf)
237 +{
238 + long sz = mf->size;
239 + if (FIRST_FEW_BYTES < sz)
240 + sz = FIRST_FEW_BYTES;
241 + if (memchr(mf->ptr, 0, sz))
242 + return 1;
243 + return 0;
244 +}
245 +
246 static const char *builtin_diff(const char *name_a,
247 const char *name_b,
248 struct diff_tempfile *temp,
@@ -306,22 +327,32 @@ static const char *builtin_diff(const char *name_a,
327 if (label_path[1][0] != '/')
328 label_path[1] = quote_two("b/", name_b);
329
309 - printf("--- %s\n", label_path[0]);
310 - printf("+++ %s\n", label_path[1]);
311 -
330 if (fill_mmfile(&mf1, temp[0].name) < 0 ||
331 fill_mmfile(&mf2, temp[1].name) < 0)
332 die("unable to read files to diff");
333
316 - /* Crazy xdl interfaces.. */
317 - {
334 + if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))
335 + printf("Binary files %s and %s differ\n",
336 + label_path[0], label_path[1]);
337 + else {
338 + /* Crazy xdl interfaces.. */
339 + const char *diffopts = getenv("GIT_DIFF_OPTS");
340 xpparam_t xpp;
341 xdemitconf_t xecfg;
342 xdemitcb_t ecb;
343 + struct emit_callback ecbdata;
344
345 + ecbdata.label_path = label_path;
346 xpp.flags = XDF_NEED_MINIMAL;
347 xecfg.ctxlen = 3;
348 + if (!diffopts)
349 + ;
350 + else if (!strncmp(diffopts, "--unified=", 10))
351 + xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
352 + else if (!strncmp(diffopts, "-u", 2))
353 + xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
354 ecb.outf = fn_out;
355 + ecb.priv = &ecbdata;
356 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
357 }
358
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 @@ Line 7
52 +@@ -8,7 +8,7 @@
53 Line 8
54 Line 9
55 Line 10
xdiff/xutils.c
+10 -6
@@ -245,20 +245,24 @@ int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, xdemitcb_t *ecb) {
245
246 nb += xdl_num_out(buf + nb, c1 ? s1: 0);
247
248 - memcpy(buf + nb, ",", 1);
249 - nb += 1;
248 + if (c1 != 1) {
249 + memcpy(buf + nb, ",", 1);
250 + nb += 1;
251
251 - nb += xdl_num_out(buf + nb, c1);
252 + nb += xdl_num_out(buf + nb, c1);
253 + }
254
255 memcpy(buf + nb, " +", 2);
256 nb += 2;
257
258 nb += xdl_num_out(buf + nb, c2 ? s2: 0);
259
258 - memcpy(buf + nb, ",", 1);
259 - nb += 1;
260 + if (c2 != 1) {
261 + memcpy(buf + nb, ",", 1);
262 + nb += 1;
263
261 - nb += xdl_num_out(buf + nb, c2);
264 + nb += xdl_num_out(buf + nb, c2);
265 + }
266
267 memcpy(buf + nb, " @@\n", 4);
268 nb += 4;