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

[PATCH v4 3/6] diff: allow --patch & cie to override -s/--no-patch

From
Matthieu Moy <matthieu.moy@imag.fr>
Date
Jul 16, 2013, 08:05 UTC
Message-ID
<1373961940-31614-4-git-send-email-Matthieu.Moy@imag.fr>
In-Reply-To
<1373961940-31614-1-git-send-email-Matthieu.Moy@imag.fr>
All options that trigger a patch output now override --no-patch.

The case of --binary deserves extra attention: the name may suggest that it turns a normal patch into a binary patch, but it actually already enables patch output when normally disabled (e.g. "git log --binary" displays a patch), hence it makes sense for "git show --no-patch --binary" to display the binary patch.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
---
 diff.c                 | 28 +++++++++++++++++-----------
 t/t4000-diff-format.sh | 18 ++++++++++++++++++
 2 files changed, 35 insertions(+), 11 deletions(-)
diff --git a/diff.c b/diff.c
index 6bd821d..66ab714 100644
--- a/diff.c
+++ b/diff.c
@@ -3508,6 +3508,11 @@ static int parse_submodule_opt(struct diff_options *options, const char *value)
 	return 1;
 }
 
+static void enable_patch_output(int *fmt) {
+	*fmt &= ~DIFF_FORMAT_NO_OUTPUT;
+	*fmt |= DIFF_FORMAT_PATCH;
+}
+
 int diff_opt_parse(struct diff_options *options, const char **av, int ac)
 {
 	const char *arg = av[0];
@@ -3515,15 +3520,15 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
 	int argcount;
 
 	/* Output format options */
-	if (!strcmp(arg, "-p") || !strcmp(arg, "-u") || !strcmp(arg, "--patch"))
-		options->output_format |= DIFF_FORMAT_PATCH;
-	else if (opt_arg(arg, 'U', "unified", &options->context))
-		options->output_format |= DIFF_FORMAT_PATCH;
+	if (!strcmp(arg, "-p") || !strcmp(arg, "-u") || !strcmp(arg, "--patch")
+	    || opt_arg(arg, 'U', "unified", &options->context))
+		enable_patch_output(&options->output_format);
 	else if (!strcmp(arg, "--raw"))
 		options->output_format |= DIFF_FORMAT_RAW;
-	else if (!strcmp(arg, "--patch-with-raw"))
-		options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW;
-	else if (!strcmp(arg, "--numstat"))
+	else if (!strcmp(arg, "--patch-with-raw")) {
+		enable_patch_output(&options->output_format);
+		options->output_format |= DIFF_FORMAT_RAW;
+	} else if (!strcmp(arg, "--numstat"))
 		options->output_format |= DIFF_FORMAT_NUMSTAT;
 	else if (!strcmp(arg, "--shortstat"))
 		options->output_format |= DIFF_FORMAT_SHORTSTAT;
@@ -3545,9 +3550,10 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
 		options->output_format |= DIFF_FORMAT_CHECKDIFF;
 	else if (!strcmp(arg, "--summary"))
 		options->output_format |= DIFF_FORMAT_SUMMARY;
-	else if (!strcmp(arg, "--patch-with-stat"))
-		options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT;
-	else if (!strcmp(arg, "--name-only"))
+	else if (!strcmp(arg, "--patch-with-stat")) {
+		enable_patch_output(&options->output_format);
+		options->output_format |= DIFF_FORMAT_DIFFSTAT;
+	} else if (!strcmp(arg, "--name-only"))
 		options->output_format |= DIFF_FORMAT_NAME;
 	else if (!strcmp(arg, "--name-status"))
 		options->output_format |= DIFF_FORMAT_NAME_STATUS;
@@ -3624,7 +3630,7 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
 
 	/* flags options */
 	else if (!strcmp(arg, "--binary")) {
-		options->output_format |= DIFF_FORMAT_PATCH;
+		enable_patch_output(&options->output_format);
 		DIFF_OPT_SET(options, BINARY);
 	}
 	else if (!strcmp(arg, "--full-index"))
diff --git a/t/t4000-diff-format.sh b/t/t4000-diff-format.sh
index 3b9a9ae..8de36b7 100755
--- a/t/t4000-diff-format.sh
+++ b/t/t4000-diff-format.sh
@@ -71,4 +71,22 @@ test_expect_success 'git diff-files --no-patch as synonym for -s' '
 	test_must_be_empty err
 '
 
+test_expect_success 'git diff-files --no-patch --patch shows the patch' '
+	git diff-files --no-patch --patch >actual &&
+	compare_diff_patch expected actual
+'
+
+test_expect_success 'git diff-files --no-patch --patch-with-raw shows the patch and raw data' '
+	git diff-files --no-patch --patch-with-raw >actual &&
+	grep -q "^:100644 100755 .* 0000000000000000000000000000000000000000 M	path0\$" actual &&
+	tail -n +4 actual >actual-patch &&
+	compare_diff_patch expected actual-patch
+'
+
+test_expect_success 'git diff-files --patch --no-patch does not show the patch' '
+	git diff-files --patch --no-patch >actual 2>err &&
+	test_must_be_empty actual &&
+	test_must_be_empty err
+'
+
 test_done
-- 
1.8.3.1.495.g13f33cf.dirty
Previous: Matthieu MoyNext: Matthieu Moy
Message 24 of 43 in “Bug in "git show"?”
  1. Stefan BellerJul 15, 2013
  2. Matthieu MoyJul 15, 2013
  3. Stefan BellerJul 15, 2013
  4. 0/5 Make "git show -s" easier to discover for usersMatthieu Moy, Jul 15, 2013
  5. 1/5 diff: allow --no-patch as synonym for -sMatthieu Moy, Jul 15, 2013
  6. 2/5 diff: allow --patch to override -s/--no-patchMatthieu Moy, Jul 15, 2013
  7. Junio C HamanoJul 15, 2013
  8. Matthieu MoyJul 15, 2013
  9. Junio C HamanoJul 15, 2013
  10. 0/5 Make "git show -s" easier to discover for usersMatthieu Moy, Jul 15, 2013
  11. 1/5 diff: allow --no-patch as synonym for -sMatthieu Moy, Jul 15, 2013
  12. Jonathan NiederJul 15, 2013
  13. 0/6 Make "git show -s" easier to discover for usersMatthieu Moy, Jul 15, 2013
  14. 1/6 t4000-diff-format.sh: modernize styleMatthieu Moy, Jul 15, 2013
  15. Jonathan NiederJul 15, 2013
  16. 2/6 diff: allow --no-patch as synonym for -sMatthieu Moy, Jul 15, 2013
  17. 3/6 diff: allow --patch & cie to override -s/--no-patchMatthieu Moy, Jul 15, 2013
  18. Jonathan NiederJul 15, 2013
  19. Matthieu MoyJul 15, 2013
  20. Jonathan NiederJul 15, 2013
  21. 0/6 Make "git show -s" easier to discover for usersMatthieu Moy, Jul 16, 2013
  22. 1/6 t4000-diff-format.sh: modernize styleMatthieu Moy, Jul 16, 2013
  23. 2/6 diff: allow --no-patch as synonym for -sMatthieu Moy, Jul 16, 2013
  24. 3/6 diff: allow --patch & cie to override -s/--no-patchMatthieu Moy, Jul 16, 2013
  25. 4/6 Documentation/git-show.txt: include common diff options, like git-log.txtMatthieu Moy, Jul 16, 2013
  26. 5/6 Documentation: move description of -s, --no-patch to diff-options.txtMatthieu Moy, Jul 16, 2013
  27. 6/6 Documentation/git-log.txt: capitalize section namesMatthieu Moy, Jul 16, 2013
  28. Jonathan NiederJul 16, 2013
  29. Stefan BellerJul 16, 2013
  30. Junio C HamanoJul 18, 2013
  31. 4/6 Documentation/git-show.txt: include common diff options, like git-log.txtMatthieu Moy, Jul 15, 2013
  32. 5/6 Documentation: move description of -s, --no-patch to diff-options.txtMatthieu Moy, Jul 15, 2013
  33. 6/6 Documentation/git-log.txt: capitalize section namesMatthieu Moy, Jul 15, 2013
  34. Jonathan NiederJul 15, 2013
  35. 2/5 diff: allow --patch & cie to override -s/--no-patchMatthieu Moy, Jul 15, 2013
  36. 3/5 Documentation/git-show.txt: include common diff options, like git-log.txtMatthieu Moy, Jul 15, 2013
  37. 4/5 Documentation: move description of -s, --no-patch to diff-options.txtMatthieu Moy, Jul 15, 2013
  38. 5/5 Documentation/git-log.txt: capitalize section namesMatthieu Moy, Jul 15, 2013
  39. 3/5 Documentation/git-show.txt: include common diff options, like git-log.txtMatthieu Moy, Jul 15, 2013
  40. Junio C HamanoJul 15, 2013
  41. Junio C HamanoJul 15, 2013
  42. 4/5 Documentation: move description of -s, --no-patch to diff-options.txtMatthieu Moy, Jul 15, 2013
  43. 5/5 Documentation/git-log.txt: capitalize section namesMatthieu Moy, Jul 15, 2013

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.