Skip to content

Commit 3ce8f08

Browse files
author
Junio C Hamano
committed
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>
1 parent 621c53c commit 3ce8f08

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

diff.c

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,16 +212,37 @@ static int fill_mmfile(mmfile_t *mf, const char *file)
212212
return 0;
213213
}
214214

215+
struct emit_callback {
216+
const char **label_path;
217+
};
218+
215219
static int fn_out(void *priv, mmbuffer_t *mb, int nbuf)
216220
{
217221
int i;
222+
struct emit_callback *ecbdata = priv;
218223

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+
}
219229
for (i = 0; i < nbuf; i++)
220230
if (!fwrite(mb[i].ptr, mb[i].size, 1, stdout))
221231
return -1;
222232
return 0;
223233
}
224234

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+
225246
static const char *builtin_diff(const char *name_a,
226247
const char *name_b,
227248
struct diff_tempfile *temp,
@@ -306,22 +327,32 @@ static const char *builtin_diff(const char *name_a,
306327
if (label_path[1][0] != '/')
307328
label_path[1] = quote_two("b/", name_b);
308329

309-
printf("--- %s\n", label_path[0]);
310-
printf("+++ %s\n", label_path[1]);
311-
312330
if (fill_mmfile(&mf1, temp[0].name) < 0 ||
313331
fill_mmfile(&mf2, temp[1].name) < 0)
314332
die("unable to read files to diff");
315333

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");
318340
xpparam_t xpp;
319341
xdemitconf_t xecfg;
320342
xdemitcb_t ecb;
343+
struct emit_callback ecbdata;
321344

345+
ecbdata.label_path = label_path;
322346
xpp.flags = XDF_NEED_MINIMAL;
323347
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);
324354
ecb.outf = fn_out;
355+
ecb.priv = &ecbdata;
325356
xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
326357
}
327358

t/t4001-diff-rename.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ rename from path0
4949
rename to path1
5050
--- a/path0
5151
+++ b/path1
52-
@@ -8,7 +8,7 @@ Line 7
52+
@@ -8,7 +8,7 @@
5353
Line 8
5454
Line 9
5555
Line 10

xdiff/xutils.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,20 +245,24 @@ int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, xdemitcb_t *ecb) {
245245

246246
nb += xdl_num_out(buf + nb, c1 ? s1: 0);
247247

248-
memcpy(buf + nb, ",", 1);
249-
nb += 1;
248+
if (c1 != 1) {
249+
memcpy(buf + nb, ",", 1);
250+
nb += 1;
250251

251-
nb += xdl_num_out(buf + nb, c1);
252+
nb += xdl_num_out(buf + nb, c1);
253+
}
252254

253255
memcpy(buf + nb, " +", 2);
254256
nb += 2;
255257

256258
nb += xdl_num_out(buf + nb, c2 ? s2: 0);
257259

258-
memcpy(buf + nb, ",", 1);
259-
nb += 1;
260+
if (c2 != 1) {
261+
memcpy(buf + nb, ",", 1);
262+
nb += 1;
260263

261-
nb += xdl_num_out(buf + nb, c2);
264+
nb += xdl_num_out(buf + nb, c2);
265+
}
262266

263267
memcpy(buf + nb, " @@\n", 4);
264268
nb += 4;

0 commit comments

Comments
 (0)