From 6f4db505b5bedd78b96568fb955110a71ef5c1c1 Mon Sep 17 00:00:00 2001 From: Tim Waugh Date: Mon, 23 Feb 2026 11:09:10 +0000 Subject: [PATCH 1/3] Add test cases for binary file handling issues Two test cases added to reproduce GitHub issue #157, where binary files are incorrectly counted and filtered since commit bfab1d2. The lsdiff-binary-numbering test demonstrates that binary files get duplicate file numbers instead of sequential numbering. The filterdiff-binary-filtering test demonstrates that binary files leak through file number filters. Both tests currently fail and will pass once issue #157 is fixed. Assisted-by: Claude Code --- Makefile.am | 2 ++ tests/filterdiff-binary-filtering/run-test | 36 ++++++++++++++++++++++ tests/lsdiff-binary-numbering/run-test | 22 +++++++++++++ 3 files changed, 60 insertions(+) create mode 100755 tests/filterdiff-binary-filtering/run-test create mode 100755 tests/lsdiff-binary-numbering/run-test diff --git a/Makefile.am b/Makefile.am index 79b41f1c..e2657bb5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -209,6 +209,7 @@ TESTS = tests/newline1/run-test \ tests/lsdiff-exclusion-combined/run-test \ tests/lsdiff-verbose-levels/run-test \ tests/lsdiff-range-exclude/run-test \ + tests/lsdiff-binary-numbering/run-test \ tests/patchview1/run-test \ tests/patchview2/run-test \ tests/fuzz1/run-test \ @@ -306,6 +307,7 @@ TESTS = tests/newline1/run-test \ tests/patch-ignore-whitespace/run-test \ tests/whitespace-w/run-test \ tests/filterdiff-inplace1/run-test \ + tests/filterdiff-binary-filtering/run-test \ tests/rediff-inplace1/run-test \ tests/rediff-empty-hunk/run-test \ tests/git-rename-issue22/run-test \ diff --git a/tests/filterdiff-binary-filtering/run-test b/tests/filterdiff-binary-filtering/run-test new file mode 100755 index 00000000..2052aaa0 --- /dev/null +++ b/tests/filterdiff-binary-filtering/run-test @@ -0,0 +1,36 @@ +#!/bin/sh + +# GitHub issue #157: Binary files should be filtered by file number + +. ${top_srcdir-.}/tests/common.sh + +cat << 'EOF' > diff +diff --git a/file1.txt b/file1.txt +new file mode 100644 +--- /dev/null ++++ b/file1.txt +@@ -0,0 +1 @@ ++a +diff --git a/file1.bin b/file1.bin +new file mode 100644 +Binary files /dev/null and b/file1.bin differ +diff --git a/file2.txt b/file2.txt +new file mode 100644 +--- /dev/null ++++ b/file2.txt +@@ -0,0 +1 @@ ++b +diff --git a/file2.bin b/file2.bin +new file mode 100644 +Binary files /dev/null and b/file2.bin differ +EOF + +# Test -F2: should only show file #2 (file1.bin), NOT file2.bin +${FILTERDIFF} -F2 diff 2>errors >output || exit 1 +[ -s errors ] && exit 1 + +cat << 'EOF' | cmp - output || exit 1 +diff --git a/file1.bin b/file1.bin +new file mode 100644 +Binary files /dev/null and b/file1.bin differ +EOF diff --git a/tests/lsdiff-binary-numbering/run-test b/tests/lsdiff-binary-numbering/run-test new file mode 100755 index 00000000..30578878 --- /dev/null +++ b/tests/lsdiff-binary-numbering/run-test @@ -0,0 +1,22 @@ +#!/bin/sh + +# GitHub issue #157: Binary files should be numbered sequentially + +. ${top_srcdir-.}/tests/common.sh + +cat << 'EOF' > diff +diff --git a/file1.bin b/file1.bin +new file mode 100644 +Binary files /dev/null and b/file1.bin differ +diff --git a/file2.bin b/file2.bin +new file mode 100644 +Binary files /dev/null and b/file2.bin differ +EOF + +${LSDIFF} -N diff 2>errors >output || exit 1 +[ -s errors ] && exit 1 + +cat << 'EOF' | cmp - output || exit 1 +File #1 a/file1.bin +File #2 a/file2.bin +EOF From 05d0f710fdf18991bbaf6179daf94257ecdb4fdd Mon Sep 17 00:00:00 2001 From: Tim Waugh Date: Mon, 23 Feb 2026 11:21:10 +0000 Subject: [PATCH 2/3] Fix binary file numbering in lsdiff (issue #157) Binary files were not incrementing the file counter, causing them to display duplicate file numbers when using lsdiff -N. The issue occurs in git diffs without hunks (binary files, mode changes, pure renames). These files break out of header processing and are handled separately by do_git_diff_no_hunks(), bypassing the normal filecount++ that happens when processing +++ headers. This fix adds filecount++ in both code paths that handle git diffs without hunks: - EOF case: when git headers are found at end of file - Non-EOF case: when git headers are followed by binary content With this fix, binary files are numbered sequentially like text files. Assisted-by: Claude Code --- src/filterdiff.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/filterdiff.c b/src/filterdiff.c index 9e70c19d..5d18c5b1 100644 --- a/src/filterdiff.c +++ b/src/filterdiff.c @@ -1324,6 +1324,9 @@ static int filterdiff (FILE *f, const char *patchname) /* Extract filenames from git headers */ if (extract_git_filenames (header, num_headers, &git_old_name, &git_new_name, git_prefix_mode) == 0) { + /* Increment file count for this git diff without hunks */ + filecount++; + /* Use the best name for filtering */ char *names[2] = { git_old_name, git_new_name }; p = best_name (2, names); @@ -1400,6 +1403,9 @@ static int filterdiff (FILE *f, const char *patchname) goto flush_continue; } + /* Increment file count for this git diff without hunks */ + filecount++; + /* Use the best name for filtering */ char *names[2] = { git_old_name, git_new_name }; p = best_name (2, names); From d54663f2bae8084697c9ef211ab02b33dfd58c32 Mon Sep 17 00:00:00 2001 From: Tim Waugh Date: Mon, 23 Feb 2026 11:39:45 +0000 Subject: [PATCH 3/3] Fix binary file filtering with -F option (issue #157) Binary files were not respecting the -F file number filter, causing them to appear in output even when their file number didn't match the requested range. The issue occurs because git diffs without hunks (binary files, mode changes, pure renames) bypass the normal hunk processing where file_matches() is called. These files check pattern filters (-i/-x) but never check the file number filter (-F). This fix adds file_matches() checks in both code paths that handle git diffs without hunks: - EOF case: when git headers are found at end of file - Non-EOF case: when git headers are followed by binary content With this fix, binary files respect -F filtering just like text files. Assisted-by: Claude Code --- src/filterdiff.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/filterdiff.c b/src/filterdiff.c index 5d18c5b1..4f8a6956 100644 --- a/src/filterdiff.c +++ b/src/filterdiff.c @@ -1337,7 +1337,11 @@ static int filterdiff (FILE *f, const char *patchname) if (match && pat_include != NULL) match = patlist_match(pat_include, p_stripped); - /* Print filename if in list mode and matches */ + /* Apply file number filter */ + if (match) + match = file_matches(); + + /* Print filename if in list mode and matches */ if (match && !show_status && mode == mode_list) display_filename (start_linenum, status, p, patchname); @@ -1416,7 +1420,11 @@ static int filterdiff (FILE *f, const char *patchname) if (match && pat_include != NULL) match = patlist_match(pat_include, p_stripped); - /* Process the git diff (it will handle filename display) */ + /* Apply file number filter */ + if (match) + match = file_matches(); + + /* Process the git diff (it will handle filename display) */ result = do_git_diff_no_hunks (f, header, num_headers, match, &line, &linelen, &linenum, start_linenum, status, p, patchname,