git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/2] diff --no-index: support reading from pipes

From
Dennis Kaarsemaker <dennis@kaarsemaker.net>
Date
Nov 11, 2016, 20:19 UTC
Message-ID
<20161111201958.2175-3-dennis@kaarsemaker.net>
In-Reply-To
<20161111201958.2175-1-dennis@kaarsemaker.net>

diff <(command1) <(command2) provides useful output, let's make it possible for git to do the same.

Signed-off-by: Dennis Kaarsemaker <dennis@kaarsemaker.net>
---
 diff-no-index.c |  8 ++++++++
 diff.c          | 13 +++++++++++--
 2 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/diff-no-index.c b/diff-no-index.c
index 15811c2..487dbf5 100644
--- a/diff-no-index.c
+++ b/diff-no-index.c
@@ -83,6 +83,14 @@ static struct diff_filespec *noindex_filespec(const char *name, int mode)
 		name = "/dev/null";
 	s = alloc_filespec(name);
 	fill_filespec(s, null_sha1, 0, mode);
+	/*
+	 * In --no-index mode, we support reading from pipes. canon_mode, called by
+	 * fill_filespec, gets confused by this and thinks we now have subprojects.
+	 * Detect this and tell the rest of the diff machinery to treat pipes as
+	 * normal files.
+	 */
+	if (S_ISGITLINK(s->mode))
+		s->mode = S_IFREG | ce_permissions(mode);
 	if (name == file_from_standard_input)
 		populate_from_stdin(s);
 	return s;
diff --git a/diff.c b/diff.c
index 1eaf604..c599efb 100644
--- a/diff.c
+++ b/diff.c
@@ -2839,9 +2839,18 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
 		fd = open(s->path, O_RDONLY);
 		if (fd < 0)
 			goto err_empty;
-		s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
+		if (!S_ISREG(st.st_mode)) {
+			struct strbuf sb = STRBUF_INIT;
+			strbuf_read(&sb, fd, 0);
+			s->size = sb.len;
+			s->data = strbuf_detach(&sb, NULL);
+			s->should_free = 1;
+		}
+		else {
+			s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
+			s->should_munmap = 1;
+		}
 		close(fd);
-		s->should_munmap = 1;
 
 		/*
 		 * Convert from working tree format to canonical git format
-- 
2.10.1-449-gab0f84c
Previous: Dennis KaarsemakerNext: Dennis Kaarsemaker
Message 2 of 18 in “git diff <(command1) <(command2)”
  1. 0/2 git diff <(command1) <(command2)Dennis Kaarsemaker, Nov 11, 2016
  2. 2/2 diff --no-index: support reading from pipesDennis Kaarsemaker, Nov 11, 2016
  3. 1/2 diff --no-index: add option to follow symlinksDennis Kaarsemaker, Nov 11, 2016
  4. Junio C HamanoNov 11, 2016
  5. Jacob KellerNov 11, 2016
  6. Johannes SchindelinNov 12, 2016
  7. Junio C HamanoNov 14, 2016
  8. Michael J GruberNov 14, 2016
  9. Johannes SchindelinNov 14, 2016
  10. Junio C HamanoNov 14, 2016
  11. Michael J GruberNov 14, 2016
  12. Junio C HamanoNov 14, 2016
  13. Johannes SchindelinNov 16, 2016
  14. Junio C HamanoNov 16, 2016
  15. Junio C HamanoNov 16, 2016
  16. Dennis KaarsemakerNov 12, 2016
  17. Jeff KingNov 12, 2016
  18. Jacob KellerNov 11, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.