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

[PATCH/RFC] Do not show "diff --git" metainfo with --no-prefix

From
Junio C Hamano <gitster@pobox.com>
Date
Jan 16, 2008, 00:58 UTC
Message-ID
<7v4pded1rk.fsf_-_@gitster.siamese.dyndns.org>
In-Reply-To
<7vhched3kw.fsf@gitster.siamese.dyndns.org>

If a non-standard prefix is used by --no-prefix, --src-prefix, or --dst-prefix options, the resulting diff becomes something git-apply would not grok. In such a case, we should not trigger the more strict check git-apply does for "diff --git" format.

This checks the prefix specified when generating diff and if src and dst prefix are not one-level of directory name followed by a slash (i.e. the standard "diff --git a/foo b/foo" is fine, a custom "diff --git l/foo k/foo" is Ok, but "diff --git foo foo" is NOT Ok).

---
 diff.c |   52 ++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 40 insertions(+), 12 deletions(-)
diff --git a/diff.c b/diff.c
index b18c140..8126a74 100644
--- a/diff.c
+++ b/diff.c
@@ -1233,6 +1233,18 @@ static const char *diff_funcname_pattern(struct diff_filespec *one)
 	return NULL;
 }
 
+static int with_standard_prefix(struct diff_options *o)
+{
+	const char *slash;
+	slash = strchr(o->a_prefix, '/');
+	if (!slash || slash[1])
+		return 0;
+	slash = strchr(o->b_prefix, '/');
+	if (!slash || slash[1])
+		return 0;
+	return 1;
+}
+
 static void builtin_diff(const char *name_a,
 			 const char *name_b,
 			 struct diff_filespec *one,
@@ -1246,30 +1258,46 @@ static void builtin_diff(const char *name_a,
 	char *a_one, *b_two;
 	const char *set = diff_get_color_opt(o, DIFF_METAINFO);
 	const char *reset = diff_get_color_opt(o, DIFF_RESET);
+	int is_git_diff = with_standard_prefix(o);
 
 	a_one = quote_two(o->a_prefix, name_a + (*name_a == '/'));
 	b_two = quote_two(o->b_prefix, name_b + (*name_b == '/'));
 	lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
 	lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
-	printf("%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
+
+	if (!is_git_diff)
+		printf("%sIndex: %s%s\n", set, b_two, reset);
+	else
+		printf("%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
+
 	if (lbl[0][0] == '/') {
 		/* /dev/null */
-		printf("%snew file mode %06o%s\n", set, two->mode, reset);
-		if (xfrm_msg && xfrm_msg[0])
-			printf("%s%s%s\n", set, xfrm_msg, reset);
+		if (is_git_diff) {
+			printf("%snew file mode %06o%s\n",
+			       set, two->mode, reset);
+			if (xfrm_msg && xfrm_msg[0])
+				printf("%s%s%s\n", set, xfrm_msg, reset);
+		}
 	}
 	else if (lbl[1][0] == '/') {
-		printf("%sdeleted file mode %06o%s\n", set, one->mode, reset);
-		if (xfrm_msg && xfrm_msg[0])
-			printf("%s%s%s\n", set, xfrm_msg, reset);
+		if (is_git_diff) {
+			printf("%sdeleted file mode %06o%s\n",
+			       set, one->mode, reset);
+			if (xfrm_msg && xfrm_msg[0])
+				printf("%s%s%s\n", set, xfrm_msg, reset);
+		}
 	}
 	else {
-		if (one->mode != two->mode) {
-			printf("%sold mode %06o%s\n", set, one->mode, reset);
-			printf("%snew mode %06o%s\n", set, two->mode, reset);
+		if (is_git_diff) {
+			if (one->mode != two->mode) {
+				printf("%sold mode %06o%s\n",
+				       set, one->mode, reset);
+				printf("%snew mode %06o%s\n",
+				       set, two->mode, reset);
+			}
+			if (xfrm_msg && xfrm_msg[0])
+				printf("%s%s%s\n", set, xfrm_msg, reset);
 		}
-		if (xfrm_msg && xfrm_msg[0])
-			printf("%s%s%s\n", set, xfrm_msg, reset);
 		/*
 		 * we do not run diff between different kind
 		 * of objects.
Previous: Junio C HamanoNext: Johannes Schindelin
Message 16 of 53 in “[FEATURE REQUEST] git-svn format-patch”
  1. Chris OrtmanJan 15, 2008
  2. Johannes SchindelinJan 15, 2008
  3. Chris OrtmanJan 15, 2008
  4. Johannes SchindelinJan 15, 2008
  5. Chris OrtmanJan 15, 2008
  6. Johannes SchindelinJan 15, 2008
  7. Chris OrtmanJan 15, 2008
  8. Johannes SchindelinJan 15, 2008
  9. Chris OrtmanJan 15, 2008
  10. Jan HudecJan 15, 2008
  11. Miles BaderJan 16, 2008
  12. Shawn O. PearceJan 16, 2008
  13. Pascal ObryJan 15, 2008
  14. Daniel BarkalowJan 15, 2008
  15. Junio C HamanoJan 16, 2008
  16. Do not show "diff --git" metainfo with --no-prefixJunio C Hamano, Jan 16, 2008
  17. Johannes SchindelinJan 16, 2008
  18. Junio C HamanoJan 16, 2008
  19. Johannes SchindelinJan 16, 2008
  20. Daniel BarkalowJan 16, 2008
  21. Junio C HamanoJan 16, 2008
  22. Do not show "diff --git" metainfo with --no-prefixJunio C Hamano, Jan 16, 2008
  23. Linus TorvaldsJan 16, 2008
  24. Linus TorvaldsJan 16, 2008
  25. Daniel BarkalowJan 16, 2008
  26. Linus TorvaldsJan 16, 2008
  27. Junio C HamanoJan 16, 2008
  28. Daniel BarkalowJan 16, 2008
  29. Junio C HamanoJan 17, 2008
  30. Johannes SchindelinJan 17, 2008
  31. Junio C HamanoJan 17, 2008
  32. Johannes SchindelinJan 17, 2008
  33. Junio C HamanoJan 17, 2008
  34. Junio C HamanoJan 17, 2008
  35. Johannes SchindelinJan 17, 2008
  36. Junio C HamanoJan 17, 2008
  37. Johannes SchindelinJan 17, 2008
  38. Jeff KingJan 17, 2008
  39. Jeff KingJan 17, 2008
  40. Johannes SchindelinJan 17, 2008
  41. Jeff KingJan 17, 2008
  42. Junio C HamanoJan 18, 2008
  43. Junio C HamanoJan 16, 2008
  44. Daniel BarkalowJan 16, 2008
  45. Jan HudecJan 19, 2008
  46. Junio C HamanoJan 16, 2008
  47. Chris OrtmanJan 16, 2008
  48. Jean-Luc HerrenJan 15, 2008
  49. Chris OrtmanJan 15, 2008
  50. Daniel BarkalowJan 16, 2008
  51. Nigel MagnayMar 11, 2008
  52. Jan HudecMar 11, 2008
  53. Daniel BarkalowMar 12, 2008

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.