Skip to content

Commit bc5f220

Browse files
committed
diff: add option to report whether files are binary
When generating diff output, if either side of a filepair is detected as binary, Git omits the diff content and instead prints a "Binary files differ" message. In some cases, it can be useful to know which files were detected as binary and which were treated as text. Introduce a new `--report-binary-files` diff option that, when enabled, appends a line for each file indicating whether Git considered it binary or text. Signed-off-by: Justin Tobler <[email protected]>
1 parent 60f3f52 commit bc5f220

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

diff.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3534,6 +3534,15 @@ static int set_diff_algorithm(struct diff_options *opts,
35343534
return 0;
35353535
}
35363536

3537+
static void report_binary_file(struct strbuf *buf,
3538+
struct repository *repo,
3539+
struct diff_filespec *spec,
3540+
const char *path)
3541+
{
3542+
const char *type = diff_filespec_is_binary(repo, spec) ? "binary" : "text";
3543+
strbuf_addf(buf, "%s: %s\n", path, type);
3544+
}
3545+
35373546
static void builtin_diff(const char *name_a,
35383547
const char *name_b,
35393548
struct diff_filespec *one,
@@ -3675,6 +3684,10 @@ static void builtin_diff(const char *name_a,
36753684
header.buf, header.len, 0);
36763685
strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
36773686
diff_line_prefix(o), lbl[0], lbl[1]);
3687+
if (o->report_binary_files) {
3688+
report_binary_file(&sb, o->repo, one, lbl[0]);
3689+
report_binary_file(&sb, o->repo, two, lbl[1]);
3690+
}
36783691
emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
36793692
sb.buf, sb.len, 0);
36803693
strbuf_release(&sb);
@@ -3699,6 +3712,10 @@ static void builtin_diff(const char *name_a,
36993712
else {
37003713
strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
37013714
diff_line_prefix(o), lbl[0], lbl[1]);
3715+
if (o->report_binary_files) {
3716+
report_binary_file(&sb, o->repo, one, lbl[0]);
3717+
report_binary_file(&sb, o->repo, two, lbl[1]);
3718+
}
37023719
emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
37033720
sb.buf, sb.len, 0);
37043721
strbuf_release(&sb);
@@ -5741,6 +5758,8 @@ struct option *add_diff_options(const struct option *opts,
57415758
OPT_CALLBACK_F(0, "binary", options, NULL,
57425759
N_("output a binary diff that can be applied"),
57435760
PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_binary),
5761+
OPT_BOOL(0, "report-binary-files", &options->report_binary_files,
5762+
N_("report if pre- and post-image blobs are binary")),
57445763
OPT_BOOL(0, "full-index", &options->flags.full_index,
57455764
N_("show full pre- and post-image object names on the \"index\" lines")),
57465765
OPT_COLOR_FLAG(0, "color", &options->use_color,

diff.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,13 @@ struct diff_options {
369369
*/
370370
int skip_resolving_statuses;
371371

372+
/*
373+
* When generating patch diff output and a binary file is encountered,
374+
* after printing the "Binary files differ" message, append a line for
375+
* each file indicating whether Git considered it binary or text.
376+
*/
377+
int report_binary_files;
378+
372379
/* Callback which allows tweaking the options in diff_setup_done(). */
373380
void (*set_default)(struct diff_options *);
374381

0 commit comments

Comments
 (0)