Skip to content

Commit 4981984

Browse files
phillipwoodgitster
authored andcommitted
diff --no-index: refuse to compare stdin to a directory
When the user runs git diff --no-index file directory we follow the behavior of POSIX diff and rewrite the arguments as git diff --no-index file directory/file Doing that when "file" is "-" (which means "read from stdin") does not make sense so we should error out if the user asks us to compare "-" to a directory. This matches the behavior of GNU diff and diff on *BSD. Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a9e066f commit 4981984

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

diff-no-index.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,13 @@ static void fixup_paths(const char **path, struct strbuf *replacement)
218218
{
219219
unsigned int isdir0, isdir1;
220220

221-
if (path[0] == file_from_standard_input ||
222-
path[1] == file_from_standard_input)
223-
return;
224-
isdir0 = is_directory(path[0]);
225-
isdir1 = is_directory(path[1]);
221+
isdir0 = path[0] != file_from_standard_input && is_directory(path[0]);
222+
isdir1 = path[1] != file_from_standard_input && is_directory(path[1]);
223+
224+
if ((path[0] == file_from_standard_input && isdir1) ||
225+
(isdir0 && path[1] == file_from_standard_input))
226+
die(_("cannot compare stdin to a directory"));
227+
226228
if (isdir0 == isdir1)
227229
return;
228230
if (isdir0) {

t/t4053-diff-no-index.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,9 @@ test_expect_success POSIXPERM,SYMLINKS 'diff --no-index normalizes: mode not lik
205205
test_cmp expected actual
206206
'
207207

208+
test_expect_success 'diff --no-index refuses to diff stdin and a directory' '
209+
test_must_fail git diff --no-index -- - a </dev/null 2>err &&
210+
grep "fatal: cannot compare stdin to a directory" err
211+
'
212+
208213
test_done

0 commit comments

Comments
 (0)