Skip to content

Commit 9187b27

Browse files
committed
Merge branch 'pw/diff-no-index-from-named-pipes'
"git diff --no-index" learned to read from named pipes as if they were regular files, to allow "git diff <(process) <(substitution)" some shells support. * pw/diff-no-index-from-named-pipes: diff --no-index: support reading from named pipes t4054: test diff --no-index with stdin diff --no-index: die on error reading stdin diff --no-index: refuse to compare stdin to a directory
2 parents 830b4a0 + 1e3f265 commit 9187b27

File tree

2 files changed

+153
-31
lines changed

2 files changed

+153
-31
lines changed

diff-no-index.c

Lines changed: 89 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,75 +41,119 @@ static int read_directory_contents(const char *path, struct string_list *list)
4141
*/
4242
static const char file_from_standard_input[] = "-";
4343

44-
static int get_mode(const char *path, int *mode)
44+
/*
45+
* For paths given on the command-line we treat "-" as stdin and named
46+
* pipes and symbolic links to named pipes specially.
47+
*/
48+
enum special {
49+
SPECIAL_NONE,
50+
SPECIAL_STDIN,
51+
SPECIAL_PIPE,
52+
};
53+
54+
static int get_mode(const char *path, int *mode, enum special *special)
4555
{
4656
struct stat st;
4757

48-
if (!path || !strcmp(path, "/dev/null"))
58+
if (!path || !strcmp(path, "/dev/null")) {
4959
*mode = 0;
5060
#ifdef GIT_WINDOWS_NATIVE
51-
else if (!strcasecmp(path, "nul"))
61+
} else if (!strcasecmp(path, "nul")) {
5262
*mode = 0;
5363
#endif
54-
else if (path == file_from_standard_input)
64+
} else if (path == file_from_standard_input) {
5565
*mode = create_ce_mode(0666);
56-
else if (lstat(path, &st))
66+
*special = SPECIAL_STDIN;
67+
} else if (lstat(path, &st)) {
5768
return error("Could not access '%s'", path);
58-
else
69+
} else {
5970
*mode = st.st_mode;
71+
}
72+
/*
73+
* For paths on the command-line treat named pipes and symbolic
74+
* links that resolve to a named pipe specially.
75+
*/
76+
if (special &&
77+
(S_ISFIFO(*mode) ||
78+
(S_ISLNK(*mode) && !stat(path, &st) && S_ISFIFO(st.st_mode)))) {
79+
*mode = create_ce_mode(0666);
80+
*special = SPECIAL_PIPE;
81+
}
82+
6083
return 0;
6184
}
6285

63-
static int populate_from_stdin(struct diff_filespec *s)
86+
static void populate_common(struct diff_filespec *s, struct strbuf *buf)
6487
{
65-
struct strbuf buf = STRBUF_INIT;
6688
size_t size = 0;
6789

68-
if (strbuf_read(&buf, 0, 0) < 0)
69-
return error_errno("error while reading from stdin");
70-
7190
s->should_munmap = 0;
72-
s->data = strbuf_detach(&buf, &size);
91+
s->data = strbuf_detach(buf, &size);
7392
s->size = size;
7493
s->should_free = 1;
7594
s->is_stdin = 1;
76-
return 0;
7795
}
7896

79-
static struct diff_filespec *noindex_filespec(const char *name, int mode)
97+
static void populate_from_pipe(struct diff_filespec *s)
98+
{
99+
struct strbuf buf = STRBUF_INIT;
100+
int fd = xopen(s->path, O_RDONLY);
101+
102+
if (strbuf_read(&buf, fd, 0) < 0)
103+
die_errno("error while reading from '%s'", s->path);
104+
close(fd);
105+
populate_common(s, &buf);
106+
}
107+
108+
static void populate_from_stdin(struct diff_filespec *s)
109+
{
110+
struct strbuf buf = STRBUF_INIT;
111+
112+
if (strbuf_read(&buf, 0, 0) < 0)
113+
die_errno("error while reading from stdin");
114+
populate_common(s, &buf);
115+
}
116+
117+
static struct diff_filespec *noindex_filespec(const char *name, int mode,
118+
enum special special)
80119
{
81120
struct diff_filespec *s;
82121

83122
if (!name)
84123
name = "/dev/null";
85124
s = alloc_filespec(name);
86125
fill_filespec(s, null_oid(), 0, mode);
87-
if (name == file_from_standard_input)
126+
if (special == SPECIAL_STDIN)
88127
populate_from_stdin(s);
128+
else if (special == SPECIAL_PIPE)
129+
populate_from_pipe(s);
89130
return s;
90131
}
91132

92133
static int queue_diff(struct diff_options *o,
93-
const char *name1, const char *name2)
134+
const char *name1, const char *name2, int recursing)
94135
{
95136
int mode1 = 0, mode2 = 0;
137+
enum special special1 = SPECIAL_NONE, special2 = SPECIAL_NONE;
96138

97-
if (get_mode(name1, &mode1) || get_mode(name2, &mode2))
139+
/* Paths can only be special if we're not recursing. */
140+
if (get_mode(name1, &mode1, recursing ? NULL : &special1) ||
141+
get_mode(name2, &mode2, recursing ? NULL : &special2))
98142
return -1;
99143

100144
if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2)) {
101145
struct diff_filespec *d1, *d2;
102146

103147
if (S_ISDIR(mode1)) {
104148
/* 2 is file that is created */
105-
d1 = noindex_filespec(NULL, 0);
106-
d2 = noindex_filespec(name2, mode2);
149+
d1 = noindex_filespec(NULL, 0, SPECIAL_NONE);
150+
d2 = noindex_filespec(name2, mode2, special2);
107151
name2 = NULL;
108152
mode2 = 0;
109153
} else {
110154
/* 1 is file that is deleted */
111-
d1 = noindex_filespec(name1, mode1);
112-
d2 = noindex_filespec(NULL, 0);
155+
d1 = noindex_filespec(name1, mode1, special1);
156+
d2 = noindex_filespec(NULL, 0, SPECIAL_NONE);
113157
name1 = NULL;
114158
mode1 = 0;
115159
}
@@ -174,7 +218,7 @@ static int queue_diff(struct diff_options *o,
174218
n2 = buffer2.buf;
175219
}
176220

177-
ret = queue_diff(o, n1, n2);
221+
ret = queue_diff(o, n1, n2, 1);
178222
}
179223
string_list_clear(&p1, 0);
180224
string_list_clear(&p2, 0);
@@ -190,8 +234,8 @@ static int queue_diff(struct diff_options *o,
190234
SWAP(name1, name2);
191235
}
192236

193-
d1 = noindex_filespec(name1, mode1);
194-
d2 = noindex_filespec(name2, mode2);
237+
d1 = noindex_filespec(name1, mode1, special1);
238+
d2 = noindex_filespec(name2, mode2, special2);
195239
diff_queue(&diff_queued_diff, d1, d2);
196240
return 0;
197241
}
@@ -216,13 +260,27 @@ static void append_basename(struct strbuf *path, const char *dir, const char *fi
216260
*/
217261
static void fixup_paths(const char **path, struct strbuf *replacement)
218262
{
219-
unsigned int isdir0, isdir1;
263+
struct stat st;
264+
unsigned int isdir0 = 0, isdir1 = 0;
265+
unsigned int ispipe0 = 0, ispipe1 = 0;
266+
267+
if (path[0] != file_from_standard_input && !stat(path[0], &st)) {
268+
isdir0 = S_ISDIR(st.st_mode);
269+
ispipe0 = S_ISFIFO(st.st_mode);
270+
}
271+
272+
if (path[1] != file_from_standard_input && !stat(path[1], &st)) {
273+
isdir1 = S_ISDIR(st.st_mode);
274+
ispipe1 = S_ISFIFO(st.st_mode);
275+
}
276+
277+
if ((path[0] == file_from_standard_input && isdir1) ||
278+
(isdir0 && path[1] == file_from_standard_input))
279+
die(_("cannot compare stdin to a directory"));
280+
281+
if ((isdir0 && ispipe1) || (ispipe0 && isdir1))
282+
die(_("cannot compare a named pipe to a directory"));
220283

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]);
226284
if (isdir0 == isdir1)
227285
return;
228286
if (isdir0) {
@@ -296,7 +354,7 @@ int diff_no_index(struct rev_info *revs,
296354
setup_diff_pager(&revs->diffopt);
297355
revs->diffopt.flags.exit_with_status = 1;
298356

299-
if (queue_diff(&revs->diffopt, paths[0], paths[1]))
357+
if (queue_diff(&revs->diffopt, paths[0], paths[1], 0))
300358
goto out;
301359
diff_set_mnemonic_prefix(&revs->diffopt, "1/", "2/");
302360
diffcore_std(&revs->diffopt);

t/t4053-diff-no-index.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,68 @@ 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 treats '-' as stdin" '
209+
cat >expect <<-EOF &&
210+
diff --git a/- b/a/1
211+
index $ZERO_OID..$(git hash-object --stdin <a/1) 100644
212+
--- a/-
213+
+++ b/a/1
214+
@@ -1 +1 @@
215+
-x
216+
+1
217+
EOF
218+
219+
test_write_lines x | test_expect_code 1 \
220+
git -c core.abbrev=no diff --no-index -- - a/1 >actual &&
221+
test_cmp expect actual &&
222+
223+
test_write_lines 1 | git diff --no-index -- a/1 - >actual &&
224+
test_must_be_empty actual
225+
'
226+
227+
test_expect_success 'diff --no-index refuses to diff stdin and a directory' '
228+
test_must_fail git diff --no-index -- - a </dev/null 2>err &&
229+
grep "fatal: cannot compare stdin to a directory" err
230+
'
231+
232+
test_expect_success PIPE 'diff --no-index refuses to diff a named pipe and a directory' '
233+
test_when_finished "rm -f pipe" &&
234+
mkfifo pipe &&
235+
{
236+
(>pipe) &
237+
} &&
238+
test_when_finished "kill $!" &&
239+
test_must_fail git diff --no-index -- pipe a 2>err &&
240+
grep "fatal: cannot compare a named pipe to a directory" err
241+
'
242+
243+
test_expect_success PIPE,SYMLINKS 'diff --no-index reads from pipes' '
244+
test_when_finished "rm -f old new new-link" &&
245+
mkfifo old &&
246+
mkfifo new &&
247+
ln -s new new-link &&
248+
{
249+
(test_write_lines a b c >old) &
250+
} &&
251+
test_when_finished "! kill $!" &&
252+
{
253+
(test_write_lines a x c >new) &
254+
} &&
255+
test_when_finished "! kill $!" &&
256+
257+
cat >expect <<-EOF &&
258+
diff --git a/old b/new-link
259+
--- a/old
260+
+++ b/new-link
261+
@@ -1,3 +1,3 @@
262+
a
263+
-b
264+
+x
265+
c
266+
EOF
267+
268+
test_expect_code 1 git diff --no-index old new-link >actual &&
269+
test_cmp expect actual
270+
'
271+
208272
test_done

0 commit comments

Comments
 (0)