diff: support reading a file from stdin via "-"
This allows you to say echo Hello World | git diff x - to compare the contents of file "x" with the line "Hello World". This automatically switches to --no-index mode. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>
Johannes Schindelin committed
Feb 25, 2007 at 23:36 UTC
5332b2af104180b8135e0b3528ace7596cb9ba09
2 files changed
+31
-5
diff-lib.c
+11
-5
@@ -37,14 +37,20 @@ static int queue_diff(struct diff_options *o,
37
int mode1 = 0, mode2 = 0;
38
39
if (name1) {
40
- if (stat(name1, &st))
40
+ if (!strcmp(name1, "-"))
41
+ mode1 = 0644;
42
+ else if (stat(name1, &st))
43
return error("Could not access '%s'", name1);
42
- mode1 = st.st_mode;
44
+ else
45
+ mode1 = st.st_mode;
46
}
47
if (name2) {
45
- if (stat(name2, &st))
48
+ if (!strcmp(name2, "-"))
49
+ mode2 = 0644;
50
+ else if (stat(name2, &st))
51
return error("Could not access '%s'", name2);
47
- mode2 = st.st_mode;
52
+ else
53
+ mode2 = st.st_mode;
54
}
55
56
if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2))
@@ -224,7 +230,7 @@ int setup_diff_no_index(struct rev_info *revs,
230
{
231
int i;
232
for (i = 1; i < argc; i++)
227
- if (argv[i][0] != '-')
233
+ if (argv[i][0] != '-' || argv[i][1] == '\0')
234
break;
235
else if (!strcmp(argv[i], "--")) {
236
i++;
diff.c
+20
@@ -1389,6 +1389,22 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only)
1389
char *buf;
1390
unsigned long size;
1391
1392
+ if (!strcmp(s->path, "-")) {
1393
+#define INCREMENT 1024
1394
+ int i = INCREMENT;
1395
+ size = 0;
1396
+ buf = NULL;
1397
+ while (i == INCREMENT) {
1398
+ buf = xrealloc(buf, size + INCREMENT);
1399
+ i = xread(0, buf + size, INCREMENT);
1400
+ size += i;
1401
+ }
1402
+ s->should_munmap = 0;
1403
+ s->data = buf;
1404
+ s->size = size;
1405
+ s->should_free = 1;
1406
+ return 0;
1407
+ }
1408
if (lstat(s->path, &st) < 0) {
1409
if (errno == ENOENT) {
1410
err_empty:
@@ -1689,6 +1705,10 @@ static void diff_fill_sha1_info(struct diff_filespec *one)
1705
if (DIFF_FILE_VALID(one)) {
1706
if (!one->sha1_valid) {
1707
struct stat st;
1708
+ if (!strcmp(one->path, "-")) {
1709
+ hashcpy(one->sha1, null_sha1);
1710
+ return;
1711
+ }
1712
if (lstat(one->path, &st) < 0)
1713
die("stat %s", one->path);
1714
if (index_path(one->sha1, one->path, &st, 0))