Commit 64453f7e55c4c69b6cd08229929ed8227b789ff8

Stefan Sperling 2020-11-21T13:35:00

handle binary files in got/tog diff commands; add -a options to force text

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
diff --git a/got/got.1 b/got/got.1
index 2af328d..c44524c 100644
--- a/got/got.1
+++ b/got/got.1
@@ -785,7 +785,7 @@ This option has no effect if the specified
 .Ar commit
 is never traversed.
 .El
-.It Cm diff Oo Fl C Ar number Oc Oo Fl r Ar repository-path Oc Oo Fl s Oc Oo Fl w Oc Op Ar object1 Ar object2 | Ar path
+.It Cm diff Oo Fl a Oc Oo Fl C Ar number Oc Oo Fl r Ar repository-path Oc Oo Fl s Oc Oo Fl w Oc Op Ar object1 Ar object2 | Ar path
 When invoked within a work tree with less than two arguments, display
 local changes in the work tree.
 If a
@@ -803,6 +803,8 @@ The options for
 .Cm got diff
 are as follows:
 .Bl -tag -width Ds
+.It Fl a
+Treat file contents as ASCII text even if binary data is detected.
 .It Fl C Ar number
 Set the number of context lines shown in the diff.
 By default, 3 lines of context are shown.
diff --git a/got/got.c b/got/got.c
index d471800..3b8ba65 100644
--- a/got/got.c
+++ b/got/got.c
@@ -3124,7 +3124,7 @@ done:
 static const struct got_error *
 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
     const char *path, int diff_context, int ignore_whitespace,
-    struct got_repository *repo)
+    int force_text_diff, struct got_repository *repo)
 {
 	const struct got_error *err = NULL;
 	struct got_blob_object *blob1 = NULL, *blob2 = NULL;
@@ -3142,7 +3142,7 @@ diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
 	while (path[0] == '/')
 		path++;
 	err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
-	    diff_context, ignore_whitespace, stdout);
+	    diff_context, ignore_whitespace, force_text_diff, stdout);
 done:
 	if (blob1)
 		got_object_blob_close(blob1);
@@ -3153,7 +3153,7 @@ done:
 static const struct got_error *
 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
     const char *path, int diff_context, int ignore_whitespace,
-    struct got_repository *repo)
+    int force_text_diff, struct got_repository *repo)
 {
 	const struct got_error *err = NULL;
 	struct got_tree_object *tree1 = NULL, *tree2 = NULL;
@@ -3171,6 +3171,7 @@ diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
 
 	arg.diff_context = diff_context;
 	arg.ignore_whitespace = ignore_whitespace;
+	arg.force_text_diff = force_text_diff;
 	arg.outfile = stdout;
 	arg.line_offsets = NULL;
 	arg.nlines = 0;
@@ -3282,11 +3283,11 @@ print_patch(struct got_commit_object *commit, struct got_object_id *id,
 		switch (obj_type) {
 		case GOT_OBJ_TYPE_BLOB:
 			err = diff_blobs(obj_id1, obj_id2, path, diff_context,
-			    0, repo);
+			    0, 0, repo);
 			break;
 		case GOT_OBJ_TYPE_TREE:
 			err = diff_trees(obj_id1, obj_id2, path, diff_context,
-			    0, repo);
+			    0, 0, repo);
 			break;
 		default:
 			err = got_error(GOT_ERR_OBJ_TYPE);
@@ -3305,8 +3306,10 @@ print_patch(struct got_commit_object *commit, struct got_object_id *id,
 			if (err)
 				goto done;
 		}
-		printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
-		err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
+		printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
+		    id_str2);
+		err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
+		    repo);
 	}
 done:
 	free(id_str1);
@@ -3887,8 +3890,8 @@ done:
 __dead static void
 usage_diff(void)
 {
-	fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
-	    "[-w] [object1 object2 | path]\n", getprogname());
+	fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
+	    "[-s] [-w] [object1 object2 | path]\n", getprogname());
 	exit(1);
 }
 
@@ -3900,6 +3903,7 @@ struct print_diff_arg {
 	int header_shown;
 	int diff_staged;
 	int ignore_whitespace;
+	int force_text_diff;
 };
 
 /*
@@ -4004,7 +4008,7 @@ print_diff(void *arg, unsigned char status, unsigned char staged_status,
 		}
 		return got_diff_objects_as_blobs(NULL, NULL, blob_id,
 		    staged_blob_id, label1, label2, a->diff_context,
-		    a->ignore_whitespace, a->repo, stdout);
+		    a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
 	}
 
 	if (staged_status == GOT_STATUS_ADD ||
@@ -4077,7 +4081,7 @@ print_diff(void *arg, unsigned char status, unsigned char staged_status,
 		sb.st_size = 0;
 
 	err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
-	    a->diff_context, a->ignore_whitespace, stdout);
+	    a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
 done:
 	if (blob1)
 		got_object_blob_close(blob1);
@@ -4101,6 +4105,7 @@ cmd_diff(int argc, char *argv[])
 	char *label1 = NULL, *label2 = NULL;
 	int type1, type2;
 	int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
+	int force_text_diff = 0;
 	const char *errstr;
 	char *path = NULL;
 
@@ -4110,8 +4115,11 @@ cmd_diff(int argc, char *argv[])
 		err(1, "pledge");
 #endif
 
-	while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
+	while ((ch = getopt(argc, argv, "aC:r:sw")) != -1) {
 		switch (ch) {
+		case 'a':
+			force_text_diff = 1;
+			break;
 		case 'C':
 			diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
 			    &errstr);
@@ -4229,6 +4237,7 @@ cmd_diff(int argc, char *argv[])
 		arg.header_shown = 0;
 		arg.diff_staged = diff_staged;
 		arg.ignore_whitespace = ignore_whitespace;
+		arg.force_text_diff = force_text_diff;
 
 		error = got_pathlist_append(&paths, path, NULL);
 		if (error)
@@ -4267,17 +4276,19 @@ cmd_diff(int argc, char *argv[])
 	switch (type1) {
 	case GOT_OBJ_TYPE_BLOB:
 		error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
-		    NULL, NULL, diff_context, ignore_whitespace, repo,
-		    stdout);
+		    NULL, NULL, diff_context, ignore_whitespace,
+		    force_text_diff, repo, stdout);
 		break;
 	case GOT_OBJ_TYPE_TREE:
 		error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
-		    "", "", diff_context, ignore_whitespace, repo, stdout);
+		    "", "", diff_context, ignore_whitespace, force_text_diff,
+		    repo, stdout);
 		break;
 	case GOT_OBJ_TYPE_COMMIT:
 		printf("diff %s %s\n", label1, label2);
 		error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
-		    diff_context, ignore_whitespace, repo, stdout);
+		    diff_context, ignore_whitespace, force_text_diff, repo,
+		    stdout);
 		break;
 	default:
 		error = got_error(GOT_ERR_OBJ_TYPE);
diff --git a/include/got_diff.h b/include/got_diff.h
index 6e5a0c4..2e56321 100644
--- a/include/got_diff.h
+++ b/include/got_diff.h
@@ -26,7 +26,7 @@
  */
 const struct got_error *got_diff_blob(off_t **, size_t *,
     struct got_blob_object *, struct got_blob_object *,
-    const char *, const char *, int, int, FILE *);
+    const char *, const char *, int, int, int, FILE *);
 
 /*
  * Compute the differences between a blob and a file and write unified diff
@@ -37,7 +37,7 @@ const struct got_error *got_diff_blob(off_t **, size_t *,
  * Whitespace differences may optionally be ignored.
  */
 const struct got_error *got_diff_blob_file(struct got_blob_object *,
-    const char *, FILE *, size_t, const char *, int, int, FILE *);
+    const char *, FILE *, size_t, const char *, int, int, int, FILE *);
 
 /*
  * A callback function invoked to handle the differences between two blobs
@@ -64,6 +64,7 @@ struct got_diff_blob_output_unidiff_arg {
 	FILE *outfile;		/* Unidiff text will be written here. */
 	int diff_context;	/* Sets the number of context lines. */
 	int ignore_whitespace;	/* Ignore whitespace differences. */
+	int force_text_diff;	/* Assume text even if binary data detected. */
 
 	/*
 	 * The number of lines contained in produced unidiff text output,
@@ -128,7 +129,7 @@ const struct got_error *got_diff_tree_collect_changed_paths(void *,
  */
 const struct got_error *got_diff_objects_as_blobs(off_t **, size_t *,
     struct got_object_id *, struct got_object_id *,
-    const char *, const char *, int, int,
+    const char *, const char *, int, int, int,
     struct got_repository *, FILE *);
 
 /*
@@ -142,7 +143,7 @@ const struct got_error *got_diff_objects_as_blobs(off_t **, size_t *,
  */
 const struct got_error *got_diff_objects_as_trees(off_t **, size_t *,
     struct got_object_id *, struct got_object_id *, char *, char *,
-    int, int, struct got_repository *, FILE *);
+    int, int, int, struct got_repository *, FILE *);
 
 /*
  * Diff two objects, assuming both objects are commits.
@@ -152,7 +153,7 @@ const struct got_error *got_diff_objects_as_trees(off_t **, size_t *,
  * array of line offsets for, and the number of lines in, the unidiff text.
  */
 const struct got_error *got_diff_objects_as_commits(off_t **, size_t *,
-    struct got_object_id *, struct got_object_id *, int, int,
+    struct got_object_id *, struct got_object_id *, int, int, int,
     struct got_repository *, FILE *);
 
 #define GOT_DIFF_MAX_CONTEXT	64
diff --git a/lib/diff.c b/lib/diff.c
index 1b319f3..f48f80f 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -57,7 +57,7 @@ diff_blobs(off_t **line_offsets, size_t *nlines,
     struct got_diffreg_result **resultp, struct got_blob_object *blob1,
     struct got_blob_object *blob2,
     const char *label1, const char *label2, mode_t mode1, mode_t mode2,
-    int diff_context, int ignore_whitespace, FILE *outfile)
+    int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile)
 {
 	const struct got_error *err = NULL, *free_err;
 	FILE *f1 = NULL, *f2 = NULL;
@@ -161,7 +161,7 @@ diff_blobs(off_t **line_offsets, size_t *nlines,
 		free(modestr2);
 	}
 	err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
-	    ignore_whitespace);
+	    ignore_whitespace, force_text_diff);
 	if (err)
 		goto done;
 
@@ -199,23 +199,25 @@ got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
 
 	return diff_blobs(&a->line_offsets, &a->nlines, NULL,
 	    blob1, blob2, label1, label2, mode1, mode2, a->diff_context,
-	    a->ignore_whitespace, a->outfile);
+	    a->ignore_whitespace, a->force_text_diff, a->outfile);
 }
 
 const struct got_error *
 got_diff_blob(off_t **line_offsets, size_t *nlines,
     struct got_blob_object *blob1, struct got_blob_object *blob2,
     const char *label1, const char *label2, int diff_context,
-    int ignore_whitespace, FILE *outfile)
+    int ignore_whitespace, int force_text_diff, FILE *outfile)
 {
 	return diff_blobs(line_offsets, nlines, NULL, blob1, blob2,
-	    label1, label2, 0, 0, diff_context, ignore_whitespace, outfile);
+	    label1, label2, 0, 0, diff_context, ignore_whitespace,
+	    force_text_diff, outfile);
 }
 
 static const struct got_error *
 diff_blob_file(struct got_diffreg_result **resultp,
     struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
-    const char *label2, int diff_context, int ignore_whitespace, FILE *outfile)
+    const char *label2, int diff_context, int ignore_whitespace,
+    int force_text_diff, FILE *outfile)
 {
 	const struct got_error *err = NULL, *free_err;
 	FILE *f1 = NULL;
@@ -248,7 +250,7 @@ diff_blob_file(struct got_diffreg_result **resultp,
 	}
 
 	err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE, 
-	    ignore_whitespace);
+	    ignore_whitespace, force_text_diff);
 	if (err)
 		goto done;
 
@@ -276,10 +278,10 @@ done:
 const struct got_error *
 got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
     FILE *f2, size_t size2, const char *label2, int diff_context,
-    int ignore_whitespace, FILE *outfile)
+    int ignore_whitespace, int force_text_diff, FILE *outfile)
 {
 	return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
-	    diff_context, ignore_whitespace, outfile);
+	    diff_context, ignore_whitespace, force_text_diff, outfile);
 }
 
 static const struct got_error *
@@ -720,7 +722,8 @@ const struct got_error *
 got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
     struct got_object_id *id1, struct got_object_id *id2,
     const char *label1, const char *label2, int diff_context,
-    int ignore_whitespace, struct got_repository *repo, FILE *outfile)
+    int ignore_whitespace, int force_text_diff,
+    struct got_repository *repo, FILE *outfile)
 {
 	const struct got_error *err;
 	struct got_blob_object *blob1 = NULL, *blob2 = NULL;
@@ -739,7 +742,8 @@ got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
 			goto done;
 	}
 	err = got_diff_blob(line_offsets, nlines, blob1, blob2,
-	    label1, label2, diff_context, ignore_whitespace, outfile);
+	    label1, label2, diff_context, ignore_whitespace, force_text_diff,
+	    outfile);
 done:
 	if (blob1)
 		got_object_blob_close(blob1);
@@ -752,7 +756,7 @@ const struct got_error *
 got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
     struct got_object_id *id1, struct got_object_id *id2,
     char *label1, char *label2, int diff_context, int ignore_whitespace,
-    struct got_repository *repo, FILE *outfile)
+    int force_text_diff, struct got_repository *repo, FILE *outfile)
 {
 	const struct got_error *err;
 	struct got_tree_object *tree1 = NULL, *tree2 = NULL;
@@ -774,6 +778,7 @@ got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
 	}
 	arg.diff_context = diff_context;
 	arg.ignore_whitespace = ignore_whitespace;
+	arg.force_text_diff = force_text_diff;
 	arg.outfile = outfile;
 	if (want_lineoffsets) {
 		arg.line_offsets = *line_offsets;
@@ -800,7 +805,7 @@ done:
 const struct got_error *
 got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
     struct got_object_id *id1, struct got_object_id *id2,
-    int diff_context, int ignore_whitespace,
+    int diff_context, int ignore_whitespace, int force_text_diff,
     struct got_repository *repo, FILE *outfile)
 {
 	const struct got_error *err;
@@ -822,7 +827,7 @@ got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
 	err = got_diff_objects_as_trees(line_offsets, nlines,
 	    commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
 	    got_object_commit_get_tree_id(commit2), "", "", diff_context,
-	    ignore_whitespace, repo, outfile);
+	    ignore_whitespace, force_text_diff, repo, outfile);
 done:
 	if (commit1)
 		got_object_commit_close(commit1);
@@ -834,7 +839,8 @@ done:
 const struct got_error *
 got_diff_files(struct got_diffreg_result **resultp,
     FILE *f1, const char *label1, FILE *f2, const char *label2,
-    int diff_context, int ignore_whitespace, FILE *outfile)
+    int diff_context, int ignore_whitespace, int force_text_diff,
+    FILE *outfile)
 {
 	const struct got_error *err = NULL;
 	struct got_diffreg_result *diffreg_result = NULL;
@@ -850,7 +856,7 @@ got_diff_files(struct got_diffreg_result **resultp,
 	}
 
 	err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
-	    ignore_whitespace);
+	    ignore_whitespace, force_text_diff);
 	if (err)
 		goto done;
 
diff --git a/lib/diff3.c b/lib/diff3.c
index d685f9e..7b8449a 100644
--- a/lib/diff3.c
+++ b/lib/diff3.c
@@ -223,7 +223,7 @@ diffreg(BUF **d, const char *path1, const char *path2)
 		goto done;
 
 	err = got_diffreg(&diffreg_result, f1, f2,
-	    GOT_DIFF_ALGORITHM_PATIENCE, 0);
+	    GOT_DIFF_ALGORITHM_PATIENCE, 0, 1);
 	if (err)
 		goto done;
 
diff --git a/lib/diffreg.c b/lib/diffreg.c
index e22e81b..8683b99 100644
--- a/lib/diffreg.c
+++ b/lib/diffreg.c
@@ -139,7 +139,7 @@ got_diff_get_config(struct diff_config **cfg,
 const struct got_error *
 got_diff_prepare_file(FILE *f, char **p, size_t *size,
     struct diff_data *diff_data, const struct diff_config *cfg,
-    int ignore_whitespace)
+    int ignore_whitespace, int force_text_diff)
 {
 	const struct got_error *err = NULL;
 	struct stat st;
@@ -150,6 +150,8 @@ got_diff_prepare_file(FILE *f, char **p, size_t *size,
 	diff_flags |= DIFF_FLAG_SHOW_PROTOTYPES;
 	if (ignore_whitespace)
 		diff_flags |= DIFF_FLAG_IGNORE_WHITESPACE;
+	if (force_text_diff)
+		diff_flags |= DIFF_FLAG_FORCE_TEXT_DATA;
 
 	if (fstat(fileno(f), &st) == -1) {
 		err = got_error_from_errno("fstat");
@@ -177,7 +179,8 @@ done:
 
 const struct got_error *
 got_diffreg(struct got_diffreg_result **diffreg_result, FILE *f1, FILE *f2,
-    enum got_diff_algorithm algorithm, int ignore_whitespace)
+    enum got_diff_algorithm algorithm, int ignore_whitespace,
+    int force_text_diff)
 {
 	const struct got_error *err = NULL;
 	struct diff_config *cfg = NULL;
@@ -223,12 +226,12 @@ got_diffreg(struct got_diffreg_result **diffreg_result, FILE *f1, FILE *f2,
 	}
 
 	err = got_diff_prepare_file(f1, &p1, &size1, left, cfg,
-	    ignore_whitespace);
+	    ignore_whitespace, force_text_diff);
 	if (err)
 		goto done;
 
 	err = got_diff_prepare_file(f2, &p2, &size2, right, cfg,
-	    ignore_whitespace);
+	    ignore_whitespace, force_text_diff);
 	if (err)
 		goto done;
 
diff --git a/lib/got_lib_diff.h b/lib/got_lib_diff.h
index 3a995c4..f3e58cc 100644
--- a/lib/got_lib_diff.h
+++ b/lib/got_lib_diff.h
@@ -48,9 +48,9 @@ struct got_diffreg_result {
 const struct got_error *got_diff_get_config(struct diff_config **,
     enum got_diff_algorithm, diff_atomize_func_t, void *);
 const struct got_error *got_diff_prepare_file(FILE *, char **, size_t *,
-    struct diff_data *, const struct diff_config *, int); 
-const struct got_error *got_diffreg(struct got_diffreg_result **, FILE *, FILE *,
-    enum got_diff_algorithm, int);
+    struct diff_data *, const struct diff_config *, int, int); 
+const struct got_error *got_diffreg(struct got_diffreg_result **, FILE *,
+    FILE *, enum got_diff_algorithm, int, int);
 const struct got_error *got_diffreg_output(off_t **, size_t *,
     struct got_diffreg_result *, FILE *, FILE *, const char *, const char *,
     enum got_diff_output_format, int, FILE *);
@@ -66,4 +66,4 @@ const struct got_error *got_merge_diff3(int *, int, const char *, const char *,
     const char *, const char *, const char *, const char *);
 
 const struct got_error *got_diff_files(struct got_diffreg_result **, FILE *,
-    const char *, FILE *, const char *, int, int, FILE *);
+    const char *, FILE *, const char *, int, int, int, FILE *);
diff --git a/lib/worktree.c b/lib/worktree.c
index b012446..971955f 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -4324,7 +4324,7 @@ create_patched_content(char **path_outfile, int reverse_patch,
 	if (err)
 		goto done;
 
-	err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0,
+	err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
 	    NULL);
 	if (err)
 		goto done;
@@ -7450,7 +7450,7 @@ create_unstaged_content(char **path_unstaged_content,
 		goto done;
 
 	err = got_diff_files(&diffreg_result, f1, label1, f2,
-	    path2, 3, 0, NULL);
+	    path2, 3, 0, 1, NULL);
 	if (err)
 		goto done;
 
diff --git a/regress/cmdline/diff.sh b/regress/cmdline/diff.sh
index 40f48b0..92cd8f3 100755
--- a/regress/cmdline/diff.sh
+++ b/regress/cmdline/diff.sh
@@ -600,13 +600,27 @@ test_diff_binary_files() {
 	echo "diff $head_rev $testroot/wt" > $testroot/stdout.expected
 	echo 'blob - /dev/null' >> $testroot/stdout.expected
 	echo 'file + foo' >> $testroot/stdout.expected
+	echo "Binary files foo and foo differ" >> $testroot/stdout.expected
+
+	(cd $testroot/wt && got diff > $testroot/stdout)
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -a -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "diff $head_rev $testroot/wt" > $testroot/stdout.expected
+	echo 'blob - /dev/null' >> $testroot/stdout.expected
+	echo 'file + foo' >> $testroot/stdout.expected
 	echo '--- foo' >> $testroot/stdout.expected
 	echo '+++ foo' >> $testroot/stdout.expected
 	echo '@@ -0,0 +1 @@' >> $testroot/stdout.expected
 	printf '+\377\377\0\0\377\377\0\0\n' >> $testroot/stdout.expected
 	echo '\\ No newline at end of file' >> $testroot/stdout.expected
 
-	(cd $testroot/wt && got diff > $testroot/stdout)
+	(cd $testroot/wt && got diff -a > $testroot/stdout)
 	cmp -s $testroot/stdout.expected $testroot/stdout
 	ret="$?"
 	if [ "$ret" != "0" ]; then
@@ -633,7 +647,7 @@ test_diff_binary_files() {
 	printf '+\377\200\0\0\377\200\0\0\n' >> $testroot/stdout.expected
 	echo '\\ No newline at end of file' >> $testroot/stdout.expected
 
-	(cd $testroot/wt && got diff > $testroot/stdout)
+	(cd $testroot/wt && got diff -a > $testroot/stdout)
 	cmp -s $testroot/stdout.expected $testroot/stdout
 	ret="$?"
 	if [ "$ret" != "0" ]; then
diff --git a/tog/tog.1 b/tog/tog.1
index c6cc6c9..2998a02 100644
--- a/tog/tog.1
+++ b/tog/tog.1
@@ -175,7 +175,7 @@ If this directory is a
 .Xr got 1
 work tree, use the repository path associated with this work tree.
 .El
-.It Cm diff Oo Fl r Ar repository-path Oc Ar object1 object2
+.It Cm diff Oo Fl a Oc Oo Fl r Ar repository-path Oc Ar object1 object2
 Display the differences between two objects in the repository.
 Each
 .Ar object
@@ -188,6 +188,9 @@ The key bindings for
 .Cm tog diff
 are as follows:
 .Bl -tag -width Ds
+.It Cm a
+Toggle treatment of file contents as ASCII text even if binary data was
+detected.
 .It Cm Down-arrow, j
 Scroll down.
 .It Cm Up-arrow, k
@@ -220,6 +223,8 @@ The options for
 .Cm tog diff
 are as follows:
 .Bl -tag -width Ds
+.It Fl a
+Treat file contents as ASCII text even if binary data is detected.
 .It Fl r Ar repository-path
 Use the repository at the specified path.
 If not specified, assume the repository is located at or above the current
diff --git a/tog/tog.c b/tog/tog.c
index 54c37e9..e4b6922 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -249,6 +249,7 @@ struct tog_diff_view_state {
 	int last_displayed_line;
 	int eof;
 	int diff_context;
+	int force_text_diff;
 	struct got_repository *repo;
 	struct got_reflist_head *refs;
 	struct tog_colors colors;
@@ -445,7 +446,7 @@ struct tog_view {
 };
 
 static const struct got_error *open_diff_view(struct tog_view *,
-    struct got_object_id *, struct got_object_id *, struct tog_view *,
+    struct got_object_id *, struct got_object_id *, int, struct tog_view *,
     struct got_reflist_head *, struct got_repository *);
 static const struct got_error *show_diff_view(struct tog_view *);
 static const struct got_error *input_diff_view(struct tog_view **,
@@ -1768,7 +1769,7 @@ open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
 
 	parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
 	err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
-	    commit_id, log_view, refs, repo);
+	    commit_id, 0, log_view, refs, repo);
 	if (err == NULL)
 		*new_view = diff_view;
 	return err;
@@ -2757,8 +2758,8 @@ __dead static void
 usage_diff(void)
 {
 	endwin();
-	fprintf(stderr, "usage: %s diff [-r repository-path] object1 object2\n",
-	    getprogname());
+	fprintf(stderr, "usage: %s diff [-a] [-r repository-path] "
+	    "object1 object2\n", getprogname());
 	exit(1);
 }
 
@@ -3217,11 +3218,12 @@ create_diff(struct tog_diff_view_state *s)
 	case GOT_OBJ_TYPE_BLOB:
 		err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
 		    s->id1, s->id2, NULL, NULL, s->diff_context, 0,
-		    s->repo, s->f);
+		    s->force_text_diff, s->repo, s->f);
 		break;
 	case GOT_OBJ_TYPE_TREE:
 		err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
-		    s->id1, s->id2, "", "", s->diff_context, 0, s->repo, s->f);
+		    s->id1, s->id2, "", "", s->diff_context, 0,
+		    s->force_text_diff, s->repo, s->f);
 		break;
 	case GOT_OBJ_TYPE_COMMIT: {
 		const struct got_object_id_queue *parent_ids;
@@ -3253,7 +3255,8 @@ create_diff(struct tog_diff_view_state *s)
 		got_object_commit_close(commit2);
 
 		err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
-		    s->id1, s->id2, s->diff_context, 0, s->repo, s->f);
+		    s->id1, s->id2, s->diff_context, 0, s->force_text_diff,
+		    s->repo, s->f);
 		break;
 	}
 	default:
@@ -3357,7 +3360,7 @@ search_next_diff_view(struct tog_view *view)
 
 static const struct got_error *
 open_diff_view(struct tog_view *view, struct got_object_id *id1,
-    struct got_object_id *id2, struct tog_view *log_view,
+    struct got_object_id *id2, int force_text_diff, struct tog_view *log_view,
     struct got_reflist_head *refs, struct got_repository *repo)
 {
 	const struct got_error *err;
@@ -3400,6 +3403,7 @@ open_diff_view(struct tog_view *view, struct got_object_id *id1,
 	s->first_displayed_line = 1;
 	s->last_displayed_line = view->nlines;
 	s->diff_context = 3;
+	s->force_text_diff = force_text_diff;
 	s->log_view = log_view;
 	s->repo = repo;
 	s->refs = refs;
@@ -3563,6 +3567,14 @@ input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
 	int i;
 
 	switch (ch) {
+	case 'a':
+		s->force_text_diff = !s->force_text_diff;
+		wclear(view->window);
+		s->first_displayed_line = 1;
+		s->last_displayed_line = view->nlines;
+		diff_view_indicate_progress(view);
+		err = create_diff(s);
+		break;
 	case 'k':
 	case KEY_UP:
 		if (s->first_displayed_line > 1)
@@ -3687,7 +3699,7 @@ cmd_diff(int argc, char *argv[])
 	struct got_object_id *id1 = NULL, *id2 = NULL;
 	char *repo_path = NULL, *cwd = NULL;
 	char *id_str1 = NULL, *id_str2 = NULL;
-	int ch;
+	int ch, force_text_diff = 0;
 	struct tog_view *view;
 
 	SIMPLEQ_INIT(&refs);
@@ -3698,8 +3710,11 @@ cmd_diff(int argc, char *argv[])
 		err(1, "pledge");
 #endif
 
-	while ((ch = getopt(argc, argv, "r:")) != -1) {
+	while ((ch = getopt(argc, argv, "ar:")) != -1) {
 		switch (ch) {
+		case 'a':
+			force_text_diff = 1;
+			break;
 		case 'r':
 			repo_path = realpath(optarg, NULL);
 			if (repo_path == NULL)
@@ -3772,7 +3787,8 @@ cmd_diff(int argc, char *argv[])
 		error = got_error_from_errno("view_open");
 		goto done;
 	}
-	error = open_diff_view(view, id1, id2, NULL, &refs, repo);
+	error = open_diff_view(view, id1, id2, force_text_diff, NULL, &refs,
+	    repo);
 	if (error)
 		goto done;
 	error = view_loop(view);
@@ -4533,7 +4549,7 @@ input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
 			break;
 		}
 		err = open_diff_view(diff_view, pid ? pid->id : NULL,
-		    id, NULL, s->refs, s->repo);
+		    id, 0, NULL, s->refs, s->repo);
 		got_object_commit_close(commit);
 		if (err) {
 			view_close(diff_view);