@matheusbsbs / matheus-bagliano / commits / 3afaa72d7d
Fork 0

diff-ni: fix the diff with standard input

The earlier commit to read from stdin was full of problems, and this corrects them. - The mode bits should have been set to satisify S_ISREG(); we forgot to the S_IFREG bits and hardcoded 0644; - We did not give escape hatch to name a path whose name is really "-". Allow users to say "./-" for that; - Use of xread() was not prepared to see short read (e.g. reading from tty) nor handing read errors. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Mar 4, 2007 at 00:17 UTC 3afaa72d7d4e52d5fa1bc19abb68301f2b09aca6
2 files changed +40 -21
diff-lib.c
+11 -5
@@ -38,7 +38,7 @@ static int queue_diff(struct diff_options *o,
38
39 if (name1) {
40 if (!strcmp(name1, "-"))
41 - mode1 = 0644;
41 + mode1 = ntohl(create_ce_mode(0666));
42 else if (stat(name1, &st))
43 return error("Could not access '%s'", name1);
44 else
@@ -46,7 +46,7 @@ static int queue_diff(struct diff_options *o,
46 }
47 if (name2) {
48 if (!strcmp(name2, "-"))
49 - mode2 = 0644;
49 + mode2 = ntohl(create_ce_mode(0666));
50 else if (stat(name2, &st))
51 return error("Could not access '%s'", name2);
52 else
@@ -260,9 +260,15 @@ int setup_diff_no_index(struct rev_info *revs,
260
261 revs->diffopt.paths = xcalloc(2, sizeof(char*));
262 for (i = 0; i < 2; i++) {
263 - const char *p;
264 - p = prefix_filename(prefix, len, argv[argc - 2 + i]);
265 - revs->diffopt.paths[i] = xstrdup(p);
263 + const char *p = argv[argc - 2 + i];
264 + /*
265 + * stdin should be spelled as '-'; if you have
266 + * path that is '-', spell it as ./-.
267 + */
268 + p = (strcmp(p, "-")
269 + ? xstrdup(prefix_filename(prefix, len, p))
270 + : p);
271 + revs->diffopt.paths[i] = p;
272 }
273 }
274 else
diff.c
+29 -16
@@ -1364,6 +1364,32 @@ static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
1364 return e;
1365 }
1366
1367 +static int populate_from_stdin(struct diff_filespec *s)
1368 +{
1369 +#define INCREMENT 1024
1370 + char *buf;
1371 + unsigned long size;
1372 + int got;
1373 +
1374 + size = 0;
1375 + buf = NULL;
1376 + while (1) {
1377 + buf = xrealloc(buf, size + INCREMENT);
1378 + got = xread(0, buf + size, INCREMENT);
1379 + if (!got)
1380 + break; /* EOF */
1381 + if (got < 0)
1382 + return error("error while reading from stdin %s",
1383 + strerror(errno));
1384 + size += got;
1385 + }
1386 + s->should_munmap = 0;
1387 + s->data = buf;
1388 + s->size = size;
1389 + s->should_free = 1;
1390 + return 0;
1391 +}
1392 +
1393 /*
1394 * While doing rename detection and pickaxe operation, we may need to
1395 * grab the data for the blob (or file) for our own in-core comparison.
@@ -1389,22 +1415,9 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only)
1415 char *buf;
1416 unsigned long size;
1417
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 - }
1418 + if (!strcmp(s->path, "-"))
1419 + return populate_from_stdin(s);
1420 +
1421 if (lstat(s->path, &st) < 0) {
1422 if (errno == ENOENT) {
1423 err_empty: