From 5332b2af104180b8135e0b3528ace7596cb9ba09 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sun, 25 Feb 2007 23:36:10 +0100 Subject: 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 Signed-off-by: Junio C Hamano --- diff-lib.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'diff-lib.c') diff --git a/diff-lib.c b/diff-lib.c index 88e59b5794..226f09c72b 100644 --- a/diff-lib.c +++ b/diff-lib.c @@ -37,14 +37,20 @@ static int queue_diff(struct diff_options *o, int mode1 = 0, mode2 = 0; if (name1) { - if (stat(name1, &st)) + if (!strcmp(name1, "-")) + mode1 = 0644; + else if (stat(name1, &st)) return error("Could not access '%s'", name1); - mode1 = st.st_mode; + else + mode1 = st.st_mode; } if (name2) { - if (stat(name2, &st)) + if (!strcmp(name2, "-")) + mode2 = 0644; + else if (stat(name2, &st)) return error("Could not access '%s'", name2); - mode2 = st.st_mode; + else + mode2 = st.st_mode; } if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2)) @@ -224,7 +230,7 @@ int setup_diff_no_index(struct rev_info *revs, { int i; for (i = 1; i < argc; i++) - if (argv[i][0] != '-') + if (argv[i][0] != '-' || argv[i][1] == '\0') break; else if (!strcmp(argv[i], "--")) { i++; -- cgit v1.2.3 From 3afaa72d7d4e52d5fa1bc19abb68301f2b09aca6 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sun, 4 Mar 2007 00:17:27 -0800 Subject: 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 --- diff-lib.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'diff-lib.c') diff --git a/diff-lib.c b/diff-lib.c index 226f09c72b..470ad656a8 100644 --- a/diff-lib.c +++ b/diff-lib.c @@ -38,7 +38,7 @@ static int queue_diff(struct diff_options *o, if (name1) { if (!strcmp(name1, "-")) - mode1 = 0644; + mode1 = ntohl(create_ce_mode(0666)); else if (stat(name1, &st)) return error("Could not access '%s'", name1); else @@ -46,7 +46,7 @@ static int queue_diff(struct diff_options *o, } if (name2) { if (!strcmp(name2, "-")) - mode2 = 0644; + mode2 = ntohl(create_ce_mode(0666)); else if (stat(name2, &st)) return error("Could not access '%s'", name2); else @@ -260,9 +260,15 @@ int setup_diff_no_index(struct rev_info *revs, revs->diffopt.paths = xcalloc(2, sizeof(char*)); for (i = 0; i < 2; i++) { - const char *p; - p = prefix_filename(prefix, len, argv[argc - 2 + i]); - revs->diffopt.paths[i] = xstrdup(p); + const char *p = argv[argc - 2 + i]; + /* + * stdin should be spelled as '-'; if you have + * path that is '-', spell it as ./-. + */ + p = (strcmp(p, "-") + ? xstrdup(prefix_filename(prefix, len, p)) + : p); + revs->diffopt.paths[i] = p; } } else -- cgit v1.2.3 From 0c725f1bd938fa2b626e910773e622a385daced2 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sun, 25 Feb 2007 23:36:31 +0100 Subject: diff --no-index: support /dev/null as filename This allows us to create "new file" and "delete file" patches. It also cleans up the code. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- diff-lib.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'diff-lib.c') diff --git a/diff-lib.c b/diff-lib.c index 470ad656a8..5e6bca5906 100644 --- a/diff-lib.c +++ b/diff-lib.c @@ -30,28 +30,28 @@ static int read_directory(const char *path, struct path_list *list) return 0; } +static int get_mode(const char *path, int *mode) +{ + struct stat st; + + if (!path || !strcmp(path, "/dev/null")) + *mode = 0; + else if (!strcmp(path, "-")) + *mode = ntohl(create_ce_mode(0666)); + else if (stat(path, &st)) + return error("Could not access '%s'", path); + else + *mode = st.st_mode; + return 0; +} + static int queue_diff(struct diff_options *o, const char *name1, const char *name2) { - struct stat st; int mode1 = 0, mode2 = 0; - if (name1) { - if (!strcmp(name1, "-")) - mode1 = ntohl(create_ce_mode(0666)); - else if (stat(name1, &st)) - return error("Could not access '%s'", name1); - else - mode1 = st.st_mode; - } - if (name2) { - if (!strcmp(name2, "-")) - mode2 = ntohl(create_ce_mode(0666)); - else if (stat(name2, &st)) - return error("Could not access '%s'", name2); - else - mode2 = st.st_mode; - } + if (get_mode(name1, &mode1) || get_mode(name2, &mode2)) + return -1; if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2)) return error("file/directory conflict: %s, %s", name1, name2); -- cgit v1.2.3