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

[PATCH] Add -r flag to show-diff for diff-cache/diff-tree like output.

From
Junio C Hamano <junkio@cox.net>
Date
Apr 26, 2005, 22:27 UTC
Message-ID
<7vy8b5mawy.fsf_-_@assigned-by-dhcp.cox.net>
In-Reply-To
<Pine.LNX.4.58.0504251832480.18901@ppc970.osdl.org>

This adds a new option -r (rational) to show-diff command, to produce diff-cache/diff-tree compatible output. When -r is specified, show-diff simply skips unmerged entries. This makes the following thee behave quite similarly:

    show-diff  -r -z
    diff-cache -r -z [--cached] <tree-or-commit>
    diff-tree  -r -z <tree-or-commit> <tree-or-commit>

When -z (machine readable) is used, it implies rational. In addition to terminating each output record with NUL instead of '\n', machine readable format also may output lines of the form:

    U filename <NUL>

to show that there are unmerged entries, once per path (this is to help scripts so that they do not have to run "sed | uniq" on output from "show-files --unmerged", just to get the list of unmerged files).

The earlier machine readable output format had another form:
    X filename <NUL>

to show deleted files. This has been removed; these paths now give the diff-cache compatible "-<mode> <sha1> <path>" output.

I resisted the temptation to change those tabs between columns into spaces. Linus, what do you think? I do not think Cogito cares and changing them into spaces would certainly make them more readable by humans.

Signed-off-by: Junio C Hamano <junkio@cox.net>
---

show-diff.c | 44 +++++++++++++++++++++++++++++++++----------- 1 files changed, 33 insertions(+), 11 deletions(-)

--- k/show-diff.c +++ l/show-diff.c

@@ -6,7 +6,8 @@
 #include "cache.h"
 #include "diff.h"
 
-static const char *show_diff_usage = "show-diff [-q] [-s] [-z] [paths...]";
+static const char *show_diff_usage =
+"show-diff [-q] [-s] [-r] [-z] [paths...]";
 
 static int matches_pathspec(struct cache_entry *ce, char **spec, int cnt)
 {
@@ -23,10 +24,17 @@ static int matches_pathspec(struct cache
 	return 0;
 }
 
+static void show_file(int pfx, struct cache_entry *ce, int NUL_terminate)
+{
+	printf("%c%o\t%s\t%s\t%s%c", pfx, ntohl(ce->ce_mode), "blob",
+	       sha1_to_hex(ce->sha1), ce->name, NUL_terminate ? 0 : '\n');
+}
+
 int main(int argc, char **argv)
 {
 	int silent = 0;
 	int silent_on_nonexisting_files = 0;
+	int rational = 0;
 	int machine_readable = 0;
 	int reverse = 0;
 	int entries = read_cache();
@@ -39,8 +47,12 @@ int main(int argc, char **argv)
 			silent_on_nonexisting_files = silent = 1;
 		else if (!strcmp(argv[1], "-q"))
 			silent_on_nonexisting_files = 1;
+		else if (!strcmp(argv[1], "-r"))
+			/* diff-cache and diff-tree compatible */
+			rational = 1;
 		else if (!strcmp(argv[1], "-z"))
-			machine_readable = 1;
+			/* machine readable implies rational */
+			rational = machine_readable = 1;
 		else
 			usage(show_diff_usage);
 		argv++; argc--;
@@ -64,11 +76,16 @@ int main(int argc, char **argv)
 			continue;
 
 		if (ce_stage(ce)) {
+			/* machine-readble == rational in most cases,
+			 * but rational does not care about unmerged.
+			 * In some cases we want the list of unmerged
+			 * files and running sort -u on show-files -z
+			 * --unmerged for that is a pain.
+			 */
 			if (machine_readable)
 				printf("U %s%c", ce->name, 0);
-			else
-				printf("%s: Unmerged\n",
-				       ce->name);
+			else if (!rational)
+				printf("%s: unmerged\n", ce->name);
 			while (i < entries &&
 			       !strcmp(ce->name, active_cache[i]->name))
 				i++;
@@ -79,8 +96,10 @@ int main(int argc, char **argv)
 		if (stat(ce->name, &st) < 0) {
 			if (errno == ENOENT && silent_on_nonexisting_files)
 				continue;
-			if (machine_readable)
-				printf("X %s%c", ce->name, 0);
+			if (rational) {
+				/* deleted */
+				show_file('-', ce, machine_readable);
+			}
 			else {
 				printf("%s: %s\n", ce->name, strerror(errno));
 				if (errno == ENOENT)
@@ -91,10 +110,13 @@ int main(int argc, char **argv)
 		changed = cache_match_stat(ce, &st);
 		if (!changed)
 			continue;
-		if (!machine_readable)
-			printf("%s: %s\n", ce->name, sha1_to_hex(ce->sha1));
-		else {
-			printf("%s %s%c", sha1_to_hex(ce->sha1), ce->name, 0);
+		if (rational) {
+			static char *no_sha1_hex = 
+				"0000000000000000000000000000000000000000";
+			printf("*%o->%o\t%s\t%s->%s\t%s%c",
+			       ntohl(ce->ce_mode), st.st_mode,
+			       "blob", sha1_to_hex(ce->sha1), no_sha1_hex,
+			       ce->name, (machine_readable ? 0 : '\n'));
 			continue;
 		}
 		if (silent)
Previous: Junio C HamanoNext: Linus Torvalds
Message 13 of 22 in “Re: [GIT PATCH] Selective diff-tree”
  1. Linus TorvaldsApr 24, 2005
  2. David WoodhouseApr 24, 2005
  3. Linus TorvaldsApr 24, 2005
  4. Junio C HamanoApr 24, 2005
  5. Linus TorvaldsApr 24, 2005
  6. 0/2 diff-tree/diff-cache helperJunio C Hamano, Apr 25, 2005
  7. 1/2 Split external diff command interface to a separate file.Junio C Hamano, Apr 25, 2005
  8. 2/2 Introduce diff-tree-helper.Junio C Hamano, Apr 25, 2005
  9. Linus TorvaldsApr 26, 2005
  10. Nicolas PitreApr 26, 2005
  11. Junio C HamanoApr 26, 2005
  12. Diff-tree-helper take two.Junio C Hamano, Apr 26, 2005
  13. Add -r flag to show-diff for diff-cache/diff-tree like output.Junio C Hamano, Apr 26, 2005
  14. Linus TorvaldsApr 26, 2005
  15. Junio C HamanoApr 26, 2005
  16. Linus TorvaldsApr 26, 2005
  17. Junio C HamanoApr 27, 2005
  18. Linus TorvaldsApr 27, 2005
  19. diff-cache/tree compatible output for show-diff (take 2).Junio C Hamano, Apr 26, 2005
  20. Linus TorvaldsApr 27, 2005
  21. Linus TorvaldsApr 27, 2005
  22. Linus TorvaldsApr 27, 2005

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.