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

[PATCHv2 04/25] diff.c: introduce emit_diff_symbol

From
Stefan Beller <sbeller@google.com>
Date
Jun 23, 2017, 01:28 UTC
Message-ID
<20170623012919.28693-5-sbeller@google.com>
In-Reply-To
<20170623012919.28693-1-sbeller@google.com>

In a later patch we want to buffer all output before emitting it as a new feature ("markup moved lines") conceptually cannot be implemented in a single pass over the output.

There are different approaches to buffer all output such as:
* Buffering on the char level, i.e. we'd have a char[] which would
  grow at approximately 80 characters a line. This would keep the
  output completely unstructured, but might be very easy to implement,
  such as redirecting all output to a temporary file and working off
  that. The later passes over the buffer are quite complicated though,
  because we have to parse back any output and then decide if it should
  be modified.
* Buffer on a line level. As the output is mostly line oriented already,
  this would make sense, but it still is a bit awkward as we'd have to
  make sense of it again by looking at the first characters of a line
  to decide what part of a diff a line is.
* Buffer semantically. Imagine there is a formal grammar for the diff
  output and we'd keep the symbols of this grammar around. This keeps
  the highest level of structure in the buffered data, such that the
  actual memory requirements are less than say the first option. Instead
  of buffering the characters of the line, we'll buffer what we intend
  to do plus additional information for the specifics. An output of
    diff --git a/new.txt b/new.txt
    index fa69b07..412428c 100644
    Binary files a/new.txt and b/new.txt differ
  could be buffered as
     DIFF_SYMBOL_DIFF_START + new.txt
     DIFF_SYMBOL_INDEX_MODE + fa69b07 412428c "non-executable" flag
     DIFF_SYMBOL_BINARY_FILES + new.txt

This and the following patches introduce the third option of buffering by first moving any output to emit_diff_symbol, and then introducing the buffering in this function.

Signed-off-by: Stefan Beller <sbeller@google.com>
---
 diff.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
diff --git a/diff.c b/diff.c
index 2f9722b382..2257d44e2c 100644
--- a/diff.c
+++ b/diff.c
@@ -559,6 +559,24 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset
 	emit_line_0(o, set, reset, line[0], line+1, len-1);
 }
 
+enum diff_symbol {
+	DIFF_SYMBOL_SEPARATOR
+};
+
+static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
+			     const char *line, int len)
+{
+	switch (s) {
+	case DIFF_SYMBOL_SEPARATOR:
+		fprintf(o->file, "%s%c",
+			diff_line_prefix(o),
+			o->line_termination);
+		break;
+	default:
+		die("BUG: unknown diff symbol");
+	}
+}
+
 static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len)
 {
 	if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
@@ -4833,9 +4851,7 @@ void diff_flush(struct diff_options *options)
 
 	if (output_format & DIFF_FORMAT_PATCH) {
 		if (separator) {
-			fprintf(options->file, "%s%c",
-				diff_line_prefix(options),
-				options->line_termination);
+			emit_diff_symbol(options, DIFF_SYMBOL_SEPARATOR, NULL, 0);
 			if (options->stat_sep) {
 				/* attach patch instead of inline */
 				fputs(options->stat_sep, options->file);
-- 
2.12.2.575.gb14f27f917
Previous: Stefan BellerNext: Junio C Hamano
Message 100 of 125 in “[PATCHv5 00/17] Diff machine: highlight moved lines.”
  1. Stefan BellerMay 24, 2017
  2. 01/17 diff: readability fixStefan Beller, May 24, 2017
  3. 03/17 diff.c: factor out diff_flush_patch_all_file_pairsStefan Beller, May 24, 2017
  4. 09/17 submodule.c: convert show_submodule_summary to use emit_line_fmtStefan Beller, May 24, 2017
  5. 04/17 diff: introduce more flexible emit functionStefan Beller, May 24, 2017
  6. Jonathan TanJun 13, 2017
  7. Stefan BellerJun 13, 2017
  8. Jonathan TanJun 13, 2017
  9. 05/17 diff.c: convert fn_out_consume to use emit_lineStefan Beller, May 24, 2017
  10. 02/17 diff: move line ending check into emit_hunk_headerStefan Beller, May 24, 2017
  11. 07/17 diff.c: convert emit_rewrite_diff to use emit_line_*Stefan Beller, May 24, 2017
  12. 15/17 diff.c: emit_line includes whitespace highlightingStefan Beller, May 24, 2017
  13. 11/17 diff.c: convert show_stats to use emit_line_*Stefan Beller, May 24, 2017
  14. 17/17 diff.c: color moved lines differentlyStefan Beller, May 24, 2017
  15. Junio C HamanoMay 25, 2017
  16. Stefan BellerMay 25, 2017
  17. 10/17 diff.c: convert emit_binary_diff_body to use emit_line_*Stefan Beller, May 24, 2017
  18. 08/17 diff.c: convert emit_rewrite_lines to use emit_line_*Stefan Beller, May 24, 2017
  19. 16/17 diff: buffer all output if asked toStefan Beller, May 24, 2017
  20. Junio C HamanoMay 25, 2017
  21. Stefan BellerMay 25, 2017
  22. Junio C HamanoMay 26, 2017
  23. Jonathan TanJun 13, 2017
  24. Stefan BellerJun 14, 2017
  25. 12/17 diff.c: convert word diffing to use emit_line_*Stefan Beller, May 24, 2017
  26. 14/17 diff.c: convert diff_summary to use emit_line_*Stefan Beller, May 24, 2017
  27. 13/17 diff.c: convert diff_flush to use emit_line_*Stefan Beller, May 24, 2017
  28. 06/17 diff.c: convert builtin_diff to use emit_line_*Stefan Beller, May 24, 2017
  29. Junio C HamanoMay 25, 2017
  30. Stefan BellerMay 25, 2017
  31. Junio C HamanoMay 26, 2017
  32. Stefan BellerMay 26, 2017
  33. 0/1 Diff machine: highlight moved lines.Stefan Beller, May 27, 2017
  34. 1/1 diff.c: color moved lines differentlyStefan Beller, May 27, 2017
  35. Philip OakleyMay 27, 2017
  36. Stefan BellerMay 30, 2017
  37. diff.c: color moved lines differentlyStefan Beller, Jun 1, 2017
  38. Jonathan TanJun 13, 2017
  39. Stefan BellerJun 14, 2017
  40. 00/26 reroll of sb/diff-color-movedStefan Beller, Jun 20, 2017
  41. 02/26 diff.c: move line ending check into emit_hunk_headerStefan Beller, Jun 20, 2017
  42. 03/26 diff.c: factor out diff_flush_patch_all_file_pairsStefan Beller, Jun 20, 2017
  43. 06/26 diff.c: emit_diff_symbol learns DIFF_SYMBOL_CONTEXT_FRAGINFOStefan Beller, Jun 20, 2017
  44. 07/26 diff.c: emit_diff_symbol learns DIFF_SYMBOL_NO_LF_EOFStefan Beller, Jun 20, 2017
  45. 08/26 diff.c: migrate emit_line_checked to use emit_diff_symbolStefan Beller, Jun 20, 2017
  46. Junio C HamanoJun 21, 2017
  47. Stefan BellerJun 22, 2017
  48. Stefan BellerJun 22, 2017
  49. Junio C HamanoJun 23, 2017
  50. 11/26 diff.c: emit_diff_symbol learns DIFF_SYMBOL_FILEPAIRStefan Beller, Jun 20, 2017
  51. Jonathan TanJun 20, 2017
  52. Junio C HamanoJun 21, 2017
  53. Stefan BellerJun 22, 2017
  54. 04/26 diff.c: introduce emit_diff_symbolStefan Beller, Jun 20, 2017
  55. Junio C HamanoJun 21, 2017
  56. Stefan BellerJun 21, 2017
  57. Junio C HamanoJun 21, 2017
  58. Junio C HamanoJun 21, 2017
  59. Stefan BellerJun 21, 2017
  60. Junio C HamanoJun 21, 2017
  61. Stefan BellerJun 21, 2017
  62. Brandon WilliamsJun 21, 2017
  63. Junio C HamanoJun 21, 2017
  64. Brandon WilliamsJun 21, 2017
  65. 15/26 submodule.c: migrate diff output to use emit_diff_symbolStefan Beller, Jun 20, 2017
  66. Jonathan TanJun 20, 2017
  67. 19/26 diff.c: emit_diff_symbol learns about DIFF_SYMBOL_STAT_SEPStefan Beller, Jun 20, 2017
  68. 12/26 diff.c: emit_diff_symbol learns DIFF_SYMBOL_HEADERStefan Beller, Jun 20, 2017
  69. 17/26 diff.c: convert show_stats to use emit_diff_symbolStefan Beller, Jun 20, 2017
  70. Brandon WilliamsJun 21, 2017
  71. Stefan BellerJun 21, 2017
  72. 18/26 diff.c: convert word diffing to use emit_diff_symbolStefan Beller, Jun 20, 2017
  73. 25/26 diff: document the new --color-moved settingStefan Beller, Jun 20, 2017
  74. 14/26 diff.c: emit_diff_symbol learns DIFF_SYMBOL_REWRITE_DIFFStefan Beller, Jun 20, 2017
  75. 26/26 diff.c: have a "machine parseable" move coloringStefan Beller, Jun 20, 2017
  76. Stefan BellerJun 20, 2017
  77. Ævar Arnfjörð BjarmasonJun 23, 2017
  78. Brandon WilliamsJun 21, 2017
  79. Junio C HamanoJun 21, 2017
  80. Stefan BellerJun 21, 2017
  81. 20/26 diff.c: emit_diff_symbol learns about DIFF_SYMBOL_SUMMARYStefan Beller, Jun 20, 2017
  82. 21/26 diff.c: buffer all output if asked toStefan Beller, Jun 20, 2017
  83. 24/26 diff.c: add dimming to moved line detectionStefan Beller, Jun 20, 2017
  84. Junio C HamanoJun 21, 2017
  85. 16/26 diff.c: convert emit_binary_diff_body to use emit_diff_symbolStefan Beller, Jun 20, 2017
  86. Junio C HamanoJun 21, 2017
  87. 23/26 diff.c: color moved lines differently, plain modeStefan Beller, Jun 20, 2017
  88. 09/26 diff.c: emit_diff_symbol learns DIFF_SYMBOL_WORDS{_PORCELAIN}Stefan Beller, Jun 20, 2017
  89. 22/26 diff.c: color moved lines differentlyStefan Beller, Jun 20, 2017
  90. Jonathan TanJun 20, 2017
  91. Stefan BellerJun 20, 2017
  92. 05/26 diff.c: emit_diff_symbol learns DIFF_SYMBOL_CONTEXT_MARKERStefan Beller, Jun 20, 2017
  93. 10/26 diff.c: emit_diff_symbol learns DIFF_SYMBOL_CONTEXT_INCOMPLETEStefan Beller, Jun 20, 2017
  94. 13/26 diff.c: emit_diff_symbol learns about DIFF_SYMBOL_BINARY_FILESStefan Beller, Jun 20, 2017
  95. Junio C HamanoJun 21, 2017
  96. Stefan BellerJun 21, 2017
  97. 01/26 diff.c: readability fixStefan Beller, Jun 20, 2017
  98. 00/25 reroll of sb/diff-color-movedStefan Beller, Jun 23, 2017
  99. 02/25 diff.c: move line ending check into emit_hunk_headerStefan Beller, Jun 23, 2017
  100. 04/25 diff.c: introduce emit_diff_symbolStefan Beller, Jun 23, 2017
  101. Junio C HamanoJun 23, 2017
  102. Stefan BellerJun 23, 2017
  103. 06/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_CONTEXT_FRAGINFOStefan Beller, Jun 23, 2017
  104. 03/25 diff.c: factor out diff_flush_patch_all_file_pairsStefan Beller, Jun 23, 2017
  105. 05/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_CONTEXT_MARKERStefan Beller, Jun 23, 2017
  106. 09/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_WORDS[_PORCELAIN]Stefan Beller, Jun 23, 2017
  107. 07/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_NO_LF_EOFStefan Beller, Jun 23, 2017
  108. 13/25 diff.c: emit_diff_symbol learns about DIFF_SYMBOL_BINARY_FILESStefan Beller, Jun 23, 2017
  109. 12/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_HEADERStefan Beller, Jun 23, 2017
  110. 14/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_REWRITE_DIFFStefan Beller, Jun 23, 2017
  111. 23/25 diff.c: color moved lines differently, plain modeStefan Beller, Jun 23, 2017
  112. 22/25 diff.c: color moved lines differentlyStefan Beller, Jun 23, 2017
  113. 15/25 submodule.c: migrate diff output to use emit_diff_symbolStefan Beller, Jun 23, 2017
  114. 25/25 diff: document the new --color-moved settingStefan Beller, Jun 23, 2017
  115. 11/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_FILEPAIR_{PLUS, MINUS}Stefan Beller, Jun 23, 2017
  116. 24/25 diff.c: add dimming to moved line detectionStefan Beller, Jun 23, 2017
  117. 01/25 diff.c: readability fixStefan Beller, Jun 23, 2017
  118. 10/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_CONTEXT_INCOMPLETEStefan Beller, Jun 23, 2017
  119. 21/25 diff.c: buffer all output if asked toStefan Beller, Jun 23, 2017
  120. 08/25 diff.c: migrate emit_line_checked to use emit_diff_symbolStefan Beller, Jun 23, 2017
  121. 18/25 diff.c: convert word diffing to use emit_diff_symbolStefan Beller, Jun 23, 2017
  122. 17/25 diff.c: convert show_stats to use emit_diff_symbolStefan Beller, Jun 23, 2017
  123. 19/25 diff.c: emit_diff_symbol learns about DIFF_SYMBOL_STAT_SEPStefan Beller, Jun 23, 2017
  124. 20/25 diff.c: emit_diff_symbol learns about DIFF_SYMBOL_SUMMARYStefan Beller, Jun 23, 2017
  125. 16/25 diff.c: convert emit_binary_diff_body to use emit_diff_symbolStefan Beller, Jun 23, 2017

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.