git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "René Scharfe" <l.s.r@web.de>
To: Jeff King <peff@peff.net>
Cc: Taylor Blau <me@ttaylorr.com>,
	Git Mailing List <git@vger.kernel.org>,
	Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH] fast-export: factor out print_oid()
Date: Sat, 15 Aug 2020 09:14:49 +0200	[thread overview]
Message-ID: <d25987e5-df7e-d153-bbe4-a4e391ed4454@web.de> (raw)
In-Reply-To: <20200813172545.GB1597339@coredump.intra.peff.net>

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@web.de>
---
 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

  reply	other threads:[~2020-08-16  0:58 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-13 11:11 [PATCH] fast-export: factor out print_oid() René Scharfe
2020-08-13 11:58 ` Jeff King
2020-08-13 17:17   ` René Scharfe
2020-08-13 15:18 ` Taylor Blau
2020-08-13 17:17   ` René Scharfe
2020-08-13 17:25     ` Jeff King
2020-08-15  7:14       ` René Scharfe [this message]
2020-08-17 22:08         ` Jeff King
2020-08-17 22:53           ` Junio C Hamano
2020-08-13 18:19   ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d25987e5-df7e-d153-bbe4-a4e391ed4454@web.de \
    --to=l.s.r@web.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=me@ttaylorr.com \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).