Re: [PATCH] fast-export: factor out print_oid()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Am 13.08.20 um 19:25 schrieb Jeff King:
> On Thu, Aug 13, 2020 at 07:17:20PM +0200, René Scharfe wrote:
>
>> -- >8 --
>> Subject: [PATCH v2] fast-export: deduplicate anonymization handling
>>
>> Move the code for converting an object_id to a hexadecimal string and
>> for handling of the default (not anonymizing) case from its callers to
>> anonymize_oid() and consequently rename it to anonymize_oid_if_needed().
>> This reduces code duplication.
>
> I think this is a bad direction unless you're going to do it for all of
> the other anonymize_*() functions, as well, for consistency. And there
> it gets tricky because the caller is able to use the anonymizing
> knowledge in more places.
>
> I actually liked your original version better.

OK, how about embracing the static and do something like this?

-- >8 --
Subject: [PATCH] fast-export: add format_oid() and format_path()

Add functions that handle anonymization, quoting and formatting of paths
and object IDs and return static buffers fit for use with printf().
Use them to generate each output line containing these components with a
single printf() format specification, which is easier to read.

format_oid() inherits the ability to be used for four different object
IDs in parallel from oid_to_hex() -- but here we only need one anyway.

format_path() has two sets of static buffers, which is just enough for
our purposes here.

Signed-off-by: René Scharfe <l.s.r@xxxxxx>
---
 builtin/fast-export.c | 86 ++++++++++++++++++++++---------------------
 1 file changed, 45 insertions(+), 41 deletions(-)

diff --git a/builtin/fast-export.c b/builtin/fast-export.c
index 9f37895d4cf..a9e36dccf9e 100644
--- a/builtin/fast-export.c
+++ b/builtin/fast-export.c
@@ -368,17 +368,6 @@ static int depth_first(const void *a_, const void *b_)
 	return (a->status == 'R') - (b->status == 'R');
 }

-static void print_path_1(const char *path)
-{
-	int need_quote = quote_c_style(path, NULL, NULL, 0);
-	if (need_quote)
-		quote_c_style(path, NULL, stdout, 0);
-	else if (strchr(path, ' '))
-		printf("\"%s\"", path);
-	else
-		printf("%s", path);
-}
-
 static char *anonymize_path_component(void *data)
 {
 	static int counter;
@@ -387,18 +376,34 @@ static char *anonymize_path_component(void *data)
 	return strbuf_detach(&out, NULL);
 }

-static void print_path(const char *path)
+static const char *format_path(const char *path)
 {
-	if (!anonymize)
-		print_path_1(path);
-	else {
-		static struct hashmap paths;
-		static struct strbuf anon = STRBUF_INIT;
-
-		anonymize_path(&anon, path, &paths, anonymize_path_component);
-		print_path_1(anon.buf);
-		strbuf_reset(&anon);
+	static struct hashmap paths;
+	static struct strbuf anon_buffers[] = { STRBUF_INIT, STRBUF_INIT };
+	static struct strbuf quoted_buffers[] = { STRBUF_INIT, STRBUF_INIT };
+	static int which_buffer;
+	struct strbuf *anon = &anon_buffers[which_buffer];
+	struct strbuf *quoted = &quoted_buffers[which_buffer];
+
+	which_buffer++;
+	which_buffer %= ARRAY_SIZE(anon_buffers) + BUILD_ASSERT_OR_ZERO(
+			ARRAY_SIZE(anon_buffers) == ARRAY_SIZE(quoted_buffers));
+
+	if (anonymize) {
+		strbuf_reset(anon);
+		anonymize_path(anon, path, &paths, anonymize_path_component);
+		path = anon->buf;
+	}
+
+	strbuf_reset(quoted);
+	if (quote_c_style(path, quoted, NULL, 0))
+		return quoted->buf;
+	if (strchr(path, ' ')) {
+		strbuf_reset(quoted);
+		strbuf_addf(quoted, "\"%s\"", path);
+		return quoted->buf;
 	}
+	return path;
 }

 static char *generate_fake_oid(void *data)
@@ -420,6 +425,14 @@ static const char *anonymize_oid(const char *oid_hex)
 	return anonymize_str(&objs, generate_fake_oid, oid_hex, len, NULL);
 }

+static const char *format_oid(const struct object_id *oid)
+{
+	const char *oid_hex = oid_to_hex(oid);
+	if (anonymize)
+		oid_hex = anonymize_oid(oid_hex);
+	return oid_hex;
+}
+
 static void show_filemodify(struct diff_queue_struct *q,
 			    struct diff_options *options, void *data)
 {
@@ -438,10 +451,8 @@ static void show_filemodify(struct diff_queue_struct *q,

 		switch (q->queue[i]->status) {
 		case DIFF_STATUS_DELETED:
-			printf("D ");
-			print_path(spec->path);
+			printf("D %s\n", format_path(spec->path));
 			string_list_insert(changed, spec->path);
-			putchar('\n');
 			break;

 		case DIFF_STATUS_COPIED:
@@ -454,12 +465,10 @@ static void show_filemodify(struct diff_queue_struct *q,
 			 * copy or rename only if there was no change observed.
 			 */
 			if (!string_list_has_string(changed, ospec->path)) {
-				printf("%c ", q->queue[i]->status);
-				print_path(ospec->path);
-				putchar(' ');
-				print_path(spec->path);
+				printf("%c %s %s\n", q->queue[i]->status,
+				       format_path(ospec->path),
+				       format_path(spec->path));
 				string_list_insert(changed, spec->path);
-				putchar('\n');

 				if (oideq(&ospec->oid, &spec->oid) &&
 				    ospec->mode == spec->mode)
@@ -475,19 +484,17 @@ static void show_filemodify(struct diff_queue_struct *q,
 			 * output the SHA-1 verbatim.
 			 */
 			if (no_data || S_ISGITLINK(spec->mode))
-				printf("M %06o %s ", spec->mode,
-				       anonymize ?
-				       anonymize_oid(oid_to_hex(&spec->oid)) :
-				       oid_to_hex(&spec->oid));
+				printf("M %06o %s %s\n", spec->mode,
+				       format_oid(&spec->oid),
+				       format_path(spec->path));
 			else {
 				struct object *object = lookup_object(the_repository,
 								      &spec->oid);
-				printf("M %06o :%d ", spec->mode,
-				       get_object_mark(object));
+				printf("M %06o :%d %s\n", spec->mode,
+				       get_object_mark(object),
+				       format_path(spec->path));
 			}
-			print_path(spec->path);
 			string_list_insert(changed, spec->path);
-			putchar('\n');
 			break;

 		default:
@@ -726,10 +733,7 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,
 		if (mark)
 			printf(":%d\n", mark);
 		else
-			printf("%s\n",
-			       anonymize ?
-			       anonymize_oid(oid_to_hex(&obj->oid)) :
-			       oid_to_hex(&obj->oid));
+			printf("%s\n", format_oid(&obj->oid));
 		i++;
 	}

--
2.28.0




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux