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

[PATCH RFC 14/20] cat-file: move print_object_or_die to ref-filter

From
Olga Telezhnaya <olyatelezhnaya@gmail.com>
Date
Feb 22, 2019, 16:05 UTC
Message-ID
<0102016915f49a66-7e179c2f-b7d4-4d4d-935f-ff1431f86a77-000000@eu-west-1.amazonses.com>
In-Reply-To
<0102016915f499b8-5813fc52-230b-469e-b939-a1244e83a2b9-000000@eu-west-1.amazonses.com>

Move printing function to ref-filter, it is logical because we move all formatting/printing logic to ref-filter. It could be much better if we embed this logic into current flows in ref-filter, but it looks like the task for another patch.

Signed-off-by: Olga Telezhnaia <olyatelezhnaya@gmail.com>
---
 builtin/cat-file.c | 51 ---------------------------------------------
 ref-filter.c       | 52 ++++++++++++++++++++++++++++++++++++++++++++++
 ref-filter.h       |  3 +++
 3 files changed, 55 insertions(+), 51 deletions(-)
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index 2066ff1e697e4..6c0cbf71f0f0c 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -226,57 +226,6 @@ static size_t expand_format(struct strbuf *sb, const char *start, void *data)
 	return end - start + 1;
 }
 
-static void print_object_or_die(struct expand_data *data, int cmdmode,
-				int buffered, const char *rest)
-{
-	const struct object_id *oid = &data->oid;
-	unsigned long size;
-	char *contents;
-
-	assert(data->info.typep);
-
-	if (data->type != OBJ_BLOB) {
-		enum object_type type;
-		contents = read_object_file(oid, &type, &size);
-		if (!contents)
-			die("object %s disappeared", oid_to_hex(oid));
-		if (type != data->type)
-			die("object %s changed type!?", oid_to_hex(oid));
-		if (data->info.sizep && size != data->size)
-			die("object %s changed size!?", oid_to_hex(oid));
-
-		write_or_die(1, contents, size);
-		free(contents);
-		return;
-	}
-
-	if (buffered)
-		fflush(stdout);
-	if (!cmdmode) {
-		if (stream_blob_to_fd(1, oid, NULL, 0))
-			die("unable to stream %s to stdout", oid_to_hex(oid));
-		return;
-	}
-
-	if (!rest)
-		die("missing path for '%s'", oid_to_hex(oid));
-
-	if (cmdmode == 'w') {
-		if (filter_object(rest, 0100644, oid, &contents, &size))
-			die("could not convert '%s' %s", oid_to_hex(oid), rest);
-	} else if (cmdmode == 'c') {
-		enum object_type type;
-		if (!textconv_object(the_repository, rest, 0100644, oid, 1,
-				     &contents, &size))
-			contents = read_object_file(oid, &type, &size);
-		if (!contents)
-			die("could not convert '%s' %s", oid_to_hex(oid), rest);
-	} else
-		BUG("invalid cmdmode: %c", cmdmode);
-	write_or_die(1, contents, size);
-	free(contents);
-}
-
 static void batch_object_write(const char *obj_name,
 			       struct strbuf *scratch,
 			       struct batch_options *opt,
diff --git a/ref-filter.c b/ref-filter.c
index 65b94ea21e54f..68d9741a56468 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -20,6 +20,7 @@
 #include "commit-slab.h"
 #include "commit-graph.h"
 #include "commit-reach.h"
+#include "streaming.h"
 
 static struct ref_msg {
 	const char *gone;
@@ -2366,3 +2367,54 @@ int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
 
 	return 0;
 }
+
+void print_object_or_die(struct expand_data *data, int cmdmode,
+			 int buffered, const char *rest)
+{
+	const struct object_id *oid = &data->oid;
+	unsigned long size;
+	char *contents;
+
+	assert(data->info.typep);
+
+	if (data->type != OBJ_BLOB) {
+		enum object_type type;
+		contents = read_object_file(oid, &type, &size);
+		if (!contents)
+			die("object %s disappeared", oid_to_hex(oid));
+		if (type != data->type)
+			die("object %s changed type!?", oid_to_hex(oid));
+		if (data->info.sizep && size != data->size)
+			die("object %s changed size!?", oid_to_hex(oid));
+
+		write_or_die(1, contents, size);
+		free(contents);
+		return;
+	}
+
+	if (buffered)
+		fflush(stdout);
+	if (!cmdmode) {
+		if (stream_blob_to_fd(1, oid, NULL, 0))
+			die("unable to stream %s to stdout", oid_to_hex(oid));
+		return;
+	}
+
+	if (!rest)
+		die("missing path for '%s'", oid_to_hex(oid));
+
+	if (cmdmode == 'w') {
+		if (filter_object(rest, 0100644, oid, &contents, &size))
+			die("could not convert '%s' %s", oid_to_hex(oid), rest);
+	} else if (cmdmode == 'c') {
+		enum object_type type;
+		if (!textconv_object(the_repository, rest, 0100644, oid, 1,
+				     &contents, &size))
+			contents = read_object_file(oid, &type, &size);
+		if (!contents)
+			die("could not convert '%s' %s", oid_to_hex(oid), rest);
+	} else
+		BUG("invalid cmdmode: %c", cmdmode);
+	write_or_die(1, contents, size);
+	free(contents);
+}
diff --git a/ref-filter.h b/ref-filter.h
index fc61457d4d660..3422f39e64b5b 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -157,4 +157,7 @@ struct ref_array_item *ref_array_push(struct ref_array *array,
 				      const char *refname,
 				      const struct object_id *oid);
 
+void print_object_or_die(struct expand_data *data, int cmdmode,
+			 int buffered, const char *rest);
+
 #endif /*  REF_FILTER_H  */

--
https://github.com/git/git/pull/568
Previous: Olga TelezhnayaNext: Olga Telezhnaya
Message 2 of 32 in “cat-file: reuse struct ref_format”
  1. 01/20 cat-file: reuse struct ref_formatOlga Telezhnaya, Feb 22, 2019
  2. 14/20 cat-file: move print_object_or_die to ref-filterOlga Telezhnaya, Feb 22, 2019
  3. 18/20 cat-file: get rid of expand_dataOlga Telezhnaya, Feb 22, 2019
  4. 08/20 cat-file: remove rest from expand_dataOlga Telezhnaya, Feb 22, 2019
  5. Jeff KingFeb 28, 2019
  6. 19/20 cat-file: tests for new atoms addedOlga Telezhnaya, Feb 22, 2019
  7. 17/20 cat-file: reuse ref-filter formatting logicOlga Telezhnaya, Feb 22, 2019
  8. 16/20 for-each-ref: tests for new atom %(raw) addedOlga Telezhnaya, Feb 22, 2019
  9. 20/20 cat-file: update docsOlga Telezhnaya, Feb 22, 2019
  10. 06/20 cat-file: remove mark_query from expand_dataOlga Telezhnaya, Feb 22, 2019
  11. Jeff KingFeb 28, 2019
  12. Christian CouderMar 3, 2019
  13. 03/20 ref-filter: add rest formatting optionOlga Telezhnaya, Feb 22, 2019
  14. Jeff KingFeb 28, 2019
  15. 13/20 cat-file: rewrite print_object_or_dieOlga Telezhnaya, Feb 22, 2019
  16. 09/20 ref-filter: make expand_data globalOlga Telezhnaya, Feb 22, 2019
  17. Jeff KingFeb 28, 2019
  18. 04/20 for-each-ref: tests for new atom %(rest) addedOlga Telezhnaya, Feb 22, 2019
  19. Jeff KingFeb 28, 2019
  20. Olga TelezhnayaMar 1, 2019
  21. 11/20 cat-file: move filter_object to diff.cOlga Telezhnaya, Feb 22, 2019
  22. 10/20 cat-file: inline stream_blobOlga Telezhnaya, Feb 22, 2019
  23. Jeff KingFeb 28, 2019
  24. 15/20 ref-filter: add raw formatting optionOlga Telezhnaya, Feb 22, 2019
  25. 07/20 cat-file: remove skip_object_infoOlga Telezhnaya, Feb 22, 2019
  26. Jeff KingFeb 28, 2019
  27. 05/20 cat-file: remove split_on_whitespaceOlga Telezhnaya, Feb 22, 2019
  28. Jeff KingFeb 28, 2019
  29. 02/20 ref-filter: rename field in ref_array_item stuctOlga Telezhnaya, Feb 22, 2019
  30. Jeff KingFeb 28, 2019
  31. 12/20 cat-file: remove batch_write functionOlga Telezhnaya, Feb 22, 2019
  32. Jeff KingFeb 28, 2019

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.