{"thread":{"id":"54033","subject":"[PATCH] fast-export: factor out print_oid()","startedAt":"2020-08-13T11:11:36Z","lastAt":"2020-08-17T22:53:30Z","messageCount":10,"participants":["René Scharfe","Jeff King","Taylor Blau","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"403538","messageId":"1eb633f5-31ec-5d8d-69ac-35d0fb9772da@web.de","threadId":"54033","inReplyTo":null,"subject":"[PATCH] fast-export: factor out print_oid()","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2020-08-13T11:11:18Z","receivedAt":"2020-08-13T11:11:36Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Simplify the output code by splitting it up and reducing duplication.\nReuse the logic for printing object IDs -- anonymized if needed -- by\nmoving it to its own function, print_oid().\n\nSigned-off-by: René Scharfe <l.s.r@web.de>\n---\n builtin/fast-export.c | 26 +++++++++++++++-----------\n 1 file changed, 15 insertions(+), 11 deletions(-)\n\ndiff --git a/builtin/fast-export.c b/builtin/fast-export.c\nindex 9f37895d4cf..49bb50634ab 100644\n--- a/builtin/fast-export.c\n+++ b/builtin/fast-export.c\n@@ -420,6 +420,14 @@ static const char *anonymize_oid(const char *oid_hex)\n \treturn anonymize_str(&objs, generate_fake_oid, oid_hex, len, NULL);\n }\n\n+static void print_oid(const struct object_id *oid, int anonymize)\n+{\n+\tconst char *oid_hex = oid_to_hex(oid);\n+\tif (anonymize)\n+\t\toid_hex = anonymize_oid(oid_hex);\n+\tfputs(oid_hex, stdout);\n+}\n+\n static void show_filemodify(struct diff_queue_struct *q,\n \t\t\t    struct diff_options *options, void *data)\n {\n@@ -470,21 +478,19 @@ static void show_filemodify(struct diff_queue_struct *q,\n \t\tcase DIFF_STATUS_TYPE_CHANGED:\n \t\tcase DIFF_STATUS_MODIFIED:\n \t\tcase DIFF_STATUS_ADDED:\n+\t\t\tprintf(\"M %06o \", spec->mode);\n \t\t\t/*\n \t\t\t * Links refer to objects in another repositories;\n \t\t\t * output the SHA-1 verbatim.\n \t\t\t */\n \t\t\tif (no_data || S_ISGITLINK(spec->mode))\n-\t\t\t\tprintf(\"M %06o %s \", spec->mode,\n-\t\t\t\t       anonymize ?\n-\t\t\t\t       anonymize_oid(oid_to_hex(&spec->oid)) :\n-\t\t\t\t       oid_to_hex(&spec->oid));\n+\t\t\t\tprint_oid(&spec->oid, anonymize);\n \t\t\telse {\n \t\t\t\tstruct object *object = lookup_object(the_repository,\n \t\t\t\t\t\t\t\t      &spec->oid);\n-\t\t\t\tprintf(\"M %06o :%d \", spec->mode,\n-\t\t\t\t       get_object_mark(object));\n+\t\t\t\tprintf(\":%d\", get_object_mark(object));\n \t\t\t}\n+\t\t\tputchar(' ');\n \t\t\tprint_path(spec->path);\n \t\t\tstring_list_insert(changed, spec->path);\n \t\t\tputchar('\\n');\n@@ -724,12 +730,10 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,\n \t\telse\n \t\t\tprintf(\"merge \");\n \t\tif (mark)\n-\t\t\tprintf(\":%d\\n\", mark);\n+\t\t\tprintf(\":%d\", mark);\n \t\telse\n-\t\t\tprintf(\"%s\\n\",\n-\t\t\t       anonymize ?\n-\t\t\t       anonymize_oid(oid_to_hex(&obj->oid)) :\n-\t\t\t       oid_to_hex(&obj->oid));\n+\t\t\tprint_oid(&obj->oid, anonymize);\n+\t\tputchar('\\n');\n \t\ti++;\n \t}\n\n--\n2.28.0\n"},{"id":"403539","messageId":"20200813115815.GA3098633@coredump.intra.peff.net","threadId":"54033","inReplyTo":"1eb633f5-31ec-5d8d-69ac-35d0fb9772da@web.de","subject":"Re: [PATCH] fast-export: factor out print_oid()","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2020-08-13T11:58:15Z","receivedAt":"2020-08-13T12:00:13Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Aug 13, 2020 at 01:11:18PM +0200, René Scharfe wrote:\n\n> Simplify the output code by splitting it up and reducing duplication.\n> Reuse the logic for printing object IDs -- anonymized if needed -- by\n> moving it to its own function, print_oid().\n\nLooks sane overall, though somehow we added 4 extra lines while reducing\nduplication. ;)\n\n> +static void print_oid(const struct object_id *oid, int anonymize)\n> +{\n> +\tconst char *oid_hex = oid_to_hex(oid);\n> +\tif (anonymize)\n> +\t\toid_hex = anonymize_oid(oid_hex);\n> +\tfputs(oid_hex, stdout);\n> +}\n\nWould anyone ever pass anything except the global \"anonymize\" into this\nfunction (certainly neither of the new callers does). I get that it\ntakes us on a possible road towards moving the globals to locals, but in\nthe meantime, shadowing the global name just seems more confusing to me.\n\n> @@ -470,21 +478,19 @@ static void show_filemodify(struct diff_queue_struct *q,\n>  \t\tcase DIFF_STATUS_TYPE_CHANGED:\n>  \t\tcase DIFF_STATUS_MODIFIED:\n>  \t\tcase DIFF_STATUS_ADDED:\n> +\t\t\tprintf(\"M %06o \", spec->mode);\n\nThis makes the output a bit more lego-like (i.e., hard to see what the\nfull line will look like from the code), but it already was pretty\nbad because of using print_path(). I think that's fine.\n\n> @@ -724,12 +730,10 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,\n>  \t\telse\n>  \t\t\tprintf(\"merge \");\n>  \t\tif (mark)\n> -\t\t\tprintf(\":%d\\n\", mark);\n> +\t\t\tprintf(\":%d\", mark);\n\nThis line gets repeated, too. I guess we could have a print_mark(), but\nthere is really no logic here except \"put a colon in front of it\", so\nprobably not worthwhile.\n\n-Peff\n"},{"id":"403561","messageId":"20200813151856.GE2244@syl.lan","threadId":"54033","inReplyTo":"1eb633f5-31ec-5d8d-69ac-35d0fb9772da@web.de","subject":"Re: [PATCH] fast-export: factor out print_oid()","fromName":"Taylor Blau","fromEmail":"me@ttaylorr.com","sentAt":"2020-08-13T15:18:56Z","receivedAt":"2020-08-13T15:19:01Z","isPatch":true,"sender":{"key":"me@ttaylorr.com","avatar":"https://avatars.githubusercontent.com/u/301000140?v=4"},"body":"On Thu, Aug 13, 2020 at 01:11:18PM +0200, René Scharfe wrote:\n> Simplify the output code by splitting it up and reducing duplication.\n> Reuse the logic for printing object IDs -- anonymized if needed -- by\n> moving it to its own function, print_oid().\n>\n> Signed-off-by: René Scharfe <l.s.r@web.de>\n> ---\n>  builtin/fast-export.c | 26 +++++++++++++++-----------\n>  1 file changed, 15 insertions(+), 11 deletions(-)\n>\n> diff --git a/builtin/fast-export.c b/builtin/fast-export.c\n> index 9f37895d4cf..49bb50634ab 100644\n> --- a/builtin/fast-export.c\n> +++ b/builtin/fast-export.c\n> @@ -420,6 +420,14 @@ static const char *anonymize_oid(const char *oid_hex)\n>  \treturn anonymize_str(&objs, generate_fake_oid, oid_hex, len, NULL);\n>  }\n>\n> +static void print_oid(const struct object_id *oid, int anonymize)\n> +{\n> +\tconst char *oid_hex = oid_to_hex(oid);\n> +\tif (anonymize)\n> +\t\toid_hex = anonymize_oid(oid_hex);\n> +\tfputs(oid_hex, stdout);\n> +}\n> +\n\nThe fact that this calls fputs makes this patch (in my own opinion)\nnoisier than it needs to be. This is because of all of the factoring out\nof the other printfs. I'd expect that this looks something more like:\n\n  -\t\t\t\t       anonymize ?\n  -\t\t\t\t       anonymize_oid(oid_to_hex(&spec->oid)) :\n  -\t\t\t\t       oid_to_hex(&spec->oid));\n  +\t\t\t\t       anonymize_oid(anonymize, &spec->oid));\n\nwithout moving around all of the other printf code.\n\n>  static void show_filemodify(struct diff_queue_struct *q,\n>  \t\t\t    struct diff_options *options, void *data)\n>  {\n> @@ -470,21 +478,19 @@ static void show_filemodify(struct diff_queue_struct *q,\n>  \t\tcase DIFF_STATUS_TYPE_CHANGED:\n>  \t\tcase DIFF_STATUS_MODIFIED:\n>  \t\tcase DIFF_STATUS_ADDED:\n> +\t\t\tprintf(\"M %06o \", spec->mode);\n>  \t\t\t/*\n>  \t\t\t * Links refer to objects in another repositories;\n>  \t\t\t * output the SHA-1 verbatim.\n>  \t\t\t */\n>  \t\t\tif (no_data || S_ISGITLINK(spec->mode))\n> -\t\t\t\tprintf(\"M %06o %s \", spec->mode,\n> -\t\t\t\t       anonymize ?\n> -\t\t\t\t       anonymize_oid(oid_to_hex(&spec->oid)) :\n> -\t\t\t\t       oid_to_hex(&spec->oid));\n> +\t\t\t\tprint_oid(&spec->oid, anonymize);\n>  \t\t\telse {\n>  \t\t\t\tstruct object *object = lookup_object(the_repository,\n>  \t\t\t\t\t\t\t\t      &spec->oid);\n> -\t\t\t\tprintf(\"M %06o :%d \", spec->mode,\n> -\t\t\t\t       get_object_mark(object));\n> +\t\t\t\tprintf(\":%d\", get_object_mark(object));\n>  \t\t\t}\n> +\t\t\tputchar(' ');\n\n... That said, this transformation looks correct from a quick glance.\n\n>  \t\t\tprint_path(spec->path);\n>  \t\t\tstring_list_insert(changed, spec->path);\n>  \t\t\tputchar('\\n');\n> @@ -724,12 +730,10 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,\n>  \t\telse\n>  \t\t\tprintf(\"merge \");\n>  \t\tif (mark)\n> -\t\t\tprintf(\":%d\\n\", mark);\n> +\t\t\tprintf(\":%d\", mark);\n>  \t\telse\n> -\t\t\tprintf(\"%s\\n\",\n> -\t\t\t       anonymize ?\n> -\t\t\t       anonymize_oid(oid_to_hex(&obj->oid)) :\n> -\t\t\t       oid_to_hex(&obj->oid));\n> +\t\t\tprint_oid(&obj->oid, anonymize);\n> +\t\tputchar('\\n');\n\nAs does this one.\n\n>  \t\ti++;\n>  \t}\n>\n> --\n> 2.28.0\n\nSo, I guess what I'm trying to say is that this patch doesn't look\nwrong, other than that it seems more invasive than I would have expected\nit to be.\n\nThanks,\nTaylor\n"},{"id":"403571","messageId":"153a34a4-f0c9-6105-75d7-68f940a1cb39@web.de","threadId":"54033","inReplyTo":"20200813115815.GA3098633@coredump.intra.peff.net","subject":"Re: [PATCH] fast-export: factor out print_oid()","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2020-08-13T17:17:18Z","receivedAt":"2020-08-13T17:17:37Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Am 13.08.20 um 13:58 schrieb Jeff King:\n> On Thu, Aug 13, 2020 at 01:11:18PM +0200, René Scharfe wrote:\n>\n>> Simplify the output code by splitting it up and reducing duplication.\n>> Reuse the logic for printing object IDs -- anonymized if needed -- by\n>> moving it to its own function, print_oid().\n>\n> Looks sane overall, though somehow we added 4 extra lines while reducing\n> duplication. ;)\n\nYeah, but they are shorter. :)\n\n>\n>> +static void print_oid(const struct object_id *oid, int anonymize)\n>> +{\n>> +\tconst char *oid_hex = oid_to_hex(oid);\n>> +\tif (anonymize)\n>> +\t\toid_hex = anonymize_oid(oid_hex);\n>> +\tfputs(oid_hex, stdout);\n>> +}\n>\n> Would anyone ever pass anything except the global \"anonymize\" into this\n> function (certainly neither of the new callers does). I get that it\n> takes us on a possible road towards moving the globals to locals, but in\n> the meantime, shadowing the global name just seems more confusing to me.\n\nGood point.\n\n>\n>> @@ -470,21 +478,19 @@ static void show_filemodify(struct diff_queue_struct *q,\n>>  \t\tcase DIFF_STATUS_TYPE_CHANGED:\n>>  \t\tcase DIFF_STATUS_MODIFIED:\n>>  \t\tcase DIFF_STATUS_ADDED:\n>> +\t\t\tprintf(\"M %06o \", spec->mode);\n>\n> This makes the output a bit more lego-like (i.e., hard to see what the\n> full line will look like from the code), but it already was pretty\n> bad because of using print_path(). I think that's fine.\n\nYes, it was almost halfway to all-out lego style before, and the\npatch moves it further in that direction.\n\n>\n>> @@ -724,12 +730,10 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,\n>>  \t\telse\n>>  \t\t\tprintf(\"merge \");\n>>  \t\tif (mark)\n>> -\t\t\tprintf(\":%d\\n\", mark);\n>> +\t\t\tprintf(\":%d\", mark);\n>\n> This line gets repeated, too. I guess we could have a print_mark(), but\n> there is really no logic here except \"put a colon in front of it\", so\n> probably not worthwhile.\n\nRight.\n\nRené\n"},{"id":"403572","messageId":"3ddf0a14-77ac-7546-e03a-704432305bdb@web.de","threadId":"54033","inReplyTo":"20200813151856.GE2244@syl.lan","subject":"Re: [PATCH] fast-export: factor out print_oid()","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2020-08-13T17:17:20Z","receivedAt":"2020-08-13T17:17:37Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Am 13.08.20 um 17:18 schrieb Taylor Blau:\n> On Thu, Aug 13, 2020 at 01:11:18PM +0200, René Scharfe wrote:\n>> Simplify the output code by splitting it up and reducing duplication.\n>> Reuse the logic for printing object IDs -- anonymized if needed -- by\n>> moving it to its own function, print_oid().\n>>\n>> Signed-off-by: René Scharfe <l.s.r@web.de>\n>> ---\n>>  builtin/fast-export.c | 26 +++++++++++++++-----------\n>>  1 file changed, 15 insertions(+), 11 deletions(-)\n>>\n>> diff --git a/builtin/fast-export.c b/builtin/fast-export.c\n>> index 9f37895d4cf..49bb50634ab 100644\n>> --- a/builtin/fast-export.c\n>> +++ b/builtin/fast-export.c\n>> @@ -420,6 +420,14 @@ static const char *anonymize_oid(const char *oid_hex)\n>>  \treturn anonymize_str(&objs, generate_fake_oid, oid_hex, len, NULL);\n>>  }\n>>\n>> +static void print_oid(const struct object_id *oid, int anonymize)\n>> +{\n>> +\tconst char *oid_hex = oid_to_hex(oid);\n>> +\tif (anonymize)\n>> +\t\toid_hex = anonymize_oid(oid_hex);\n>> +\tfputs(oid_hex, stdout);\n>> +}\n>> +\n>\n> The fact that this calls fputs makes this patch (in my own opinion)\n> noisier than it needs to be. This is because of all of the factoring out\n> of the other printfs. I'd expect that this looks something more like:\n>\n>   -\t\t\t\t       anonymize ?\n>   -\t\t\t\t       anonymize_oid(oid_to_hex(&spec->oid)) :\n>   -\t\t\t\t       oid_to_hex(&spec->oid));\n>   +\t\t\t\t       anonymize_oid(anonymize, &spec->oid));\n>\n> without moving around all of the other printf code.\n\nMoving that part to anonymize_oid() would reduce the line count while\nstill getting rid of the duplication.  But the function would need a\nnew name.\n\n-- >8 --\nSubject: [PATCH v2] fast-export: deduplicate anonymization handling\n\nMove the code for converting an object_id to a hexadecimal string and\nfor handling of the default (not anonymizing) case from its callers to\nanonymize_oid() and consequently rename it to anonymize_oid_if_needed().\nThis reduces code duplication.\n\nSuggested-by: Taylor Blau <me@ttaylorr.com>\nHelped-by: Jeff King <peff@peff.net>\nSigned-off-by: René Scharfe <l.s.r@web.de>\n---\n builtin/fast-export.c | 14 ++++++--------\n 1 file changed, 6 insertions(+), 8 deletions(-)\n\ndiff --git a/builtin/fast-export.c b/builtin/fast-export.c\nindex 9f37895d4cf..fcc3208727f 100644\n--- a/builtin/fast-export.c\n+++ b/builtin/fast-export.c\n@@ -413,10 +413,13 @@ static char *generate_fake_oid(void *data)\n \treturn hash_to_hex_algop_r(hex, out, the_hash_algo);\n }\n\n-static const char *anonymize_oid(const char *oid_hex)\n+static const char *anonymize_oid_if_needed(const struct object_id *oid)\n {\n \tstatic struct hashmap objs;\n+\tconst char *oid_hex = oid_to_hex(oid);\n \tsize_t len = strlen(oid_hex);\n+\tif (!anonymize)\n+\t\treturn oid_hex;\n \treturn anonymize_str(&objs, generate_fake_oid, oid_hex, len, NULL);\n }\n\n@@ -476,9 +479,7 @@ static void show_filemodify(struct diff_queue_struct *q,\n \t\t\t */\n \t\t\tif (no_data || S_ISGITLINK(spec->mode))\n \t\t\t\tprintf(\"M %06o %s \", spec->mode,\n-\t\t\t\t       anonymize ?\n-\t\t\t\t       anonymize_oid(oid_to_hex(&spec->oid)) :\n-\t\t\t\t       oid_to_hex(&spec->oid));\n+\t\t\t\t       anonymize_oid_if_needed(&spec->oid));\n \t\t\telse {\n \t\t\t\tstruct object *object = lookup_object(the_repository,\n \t\t\t\t\t\t\t\t      &spec->oid);\n@@ -726,10 +727,7 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,\n \t\tif (mark)\n \t\t\tprintf(\":%d\\n\", mark);\n \t\telse\n-\t\t\tprintf(\"%s\\n\",\n-\t\t\t       anonymize ?\n-\t\t\t       anonymize_oid(oid_to_hex(&obj->oid)) :\n-\t\t\t       oid_to_hex(&obj->oid));\n+\t\t\tprintf(\"%s\\n\", anonymize_oid_if_needed(&obj->oid));\n \t\ti++;\n \t}\n\n--\n2.28.0\n"},{"id":"403574","messageId":"20200813172545.GB1597339@coredump.intra.peff.net","threadId":"54033","inReplyTo":"3ddf0a14-77ac-7546-e03a-704432305bdb@web.de","subject":"Re: [PATCH] fast-export: factor out print_oid()","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2020-08-13T17:25:45Z","receivedAt":"2020-08-13T17:25:52Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Aug 13, 2020 at 07:17:20PM +0200, René Scharfe wrote:\n\n> -- >8 --\n> Subject: [PATCH v2] fast-export: deduplicate anonymization handling\n> \n> Move the code for converting an object_id to a hexadecimal string and\n> for handling of the default (not anonymizing) case from its callers to\n> anonymize_oid() and consequently rename it to anonymize_oid_if_needed().\n> This reduces code duplication.\n\nI think this is a bad direction unless you're going to do it for all of\nthe other anonymize_*() functions, as well, for consistency. And there\nit gets tricky because the caller is able to use the anonymizing\nknowledge in more places.\n\nI actually liked your original version better.\n\n-Peff\n"},{"id":"403583","messageId":"xmqqmu2y2ysx.fsf@gitster.c.googlers.com","threadId":"54033","inReplyTo":"20200813151856.GE2244@syl.lan","subject":"Re: [PATCH] fast-export: factor out print_oid()","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2020-08-13T18:19:58Z","receivedAt":"2020-08-13T18:20:07Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Taylor Blau <me@ttaylorr.com> writes:\n\n>> +static void print_oid(const struct object_id *oid, int anonymize)\n>> +{\n>> +\tconst char *oid_hex = oid_to_hex(oid);\n>> +\tif (anonymize)\n>> +\t\toid_hex = anonymize_oid(oid_hex);\n>> +\tfputs(oid_hex, stdout);\n>> +}\n>> +\n>\n> The fact that this calls fputs makes this patch (in my own opinion)\n> noisier than it needs to be. This is because of all of the factoring out\n> of the other printfs. I'd expect that this looks something more like:\n>\n>   -\t\t\t\t       anonymize ?\n>   -\t\t\t\t       anonymize_oid(oid_to_hex(&spec->oid)) :\n>   -\t\t\t\t       oid_to_hex(&spec->oid));\n>   +\t\t\t\t       anonymize_oid(anonymize, &spec->oid));\n>\n> without moving around all of the other printf code.\n> ...\n> So, I guess what I'm trying to say is that this patch doesn't look\n> wrong, other than that it seems more invasive than I would have expected\n> it to be.\n\nYes, that matches my knee-jerk reaction.  I also shared Peff's\nreaction that the code to generate the message is now even more\nfragmented into pieces.\n\nJust for comparison purposes, I tried to fold anonymize_oid()'s body\ninto René's print_oid() and adjusted the calling sites, which did\nnot look too bad.\n\nSo, I dunno.\n\n builtin/fast-export.c | 22 +++++++++++-----------\n 1 file changed, 11 insertions(+), 11 deletions(-)\n\ndiff --git a/builtin/fast-export.c b/builtin/fast-export.c\nindex 9f37895d4c..bef2c01bd8 100644\n--- a/builtin/fast-export.c\n+++ b/builtin/fast-export.c\n@@ -413,11 +413,16 @@ static char *generate_fake_oid(void *data)\n \treturn hash_to_hex_algop_r(hex, out, the_hash_algo);\n }\n \n-static const char *anonymize_oid(const char *oid_hex)\n+static const char *anonymize_oid(const struct object_id *oid, int anonymize)\n {\n-\tstatic struct hashmap objs;\n-\tsize_t len = strlen(oid_hex);\n-\treturn anonymize_str(&objs, generate_fake_oid, oid_hex, len, NULL);\n+\tconst char *oid_hex = oid_to_hex(oid);\n+\tif (anonymize) {\n+\t\tstatic struct hashmap objs;\n+\t\tsize_t len = strlen(oid_hex);\n+\t\treturn anonymize_str(&objs, generate_fake_oid, oid_hex, len, NULL);\n+\t} else {\n+\t\treturn oid_hex;\n+\t}\n }\n \n static void show_filemodify(struct diff_queue_struct *q,\n@@ -476,9 +481,7 @@ static void show_filemodify(struct diff_queue_struct *q,\n \t\t\t */\n \t\t\tif (no_data || S_ISGITLINK(spec->mode))\n \t\t\t\tprintf(\"M %06o %s \", spec->mode,\n-\t\t\t\t       anonymize ?\n-\t\t\t\t       anonymize_oid(oid_to_hex(&spec->oid)) :\n-\t\t\t\t       oid_to_hex(&spec->oid));\n+\t\t\t\t       anonymize_oid(&spec->oid, anonymize));\n \t\t\telse {\n \t\t\t\tstruct object *object = lookup_object(the_repository,\n \t\t\t\t\t\t\t\t      &spec->oid);\n@@ -726,10 +729,7 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,\n \t\tif (mark)\n \t\t\tprintf(\":%d\\n\", mark);\n \t\telse\n-\t\t\tprintf(\"%s\\n\",\n-\t\t\t       anonymize ?\n-\t\t\t       anonymize_oid(oid_to_hex(&obj->oid)) :\n-\t\t\t       oid_to_hex(&obj->oid));\n+\t\t\tprintf(\"%s\\n\", anonymize_oid(&obj->oid, anonymize));\n \t\ti++;\n \t}\n \n"},{"id":"403784","messageId":"d25987e5-df7e-d153-bbe4-a4e391ed4454@web.de","threadId":"54033","inReplyTo":"20200813172545.GB1597339@coredump.intra.peff.net","subject":"Re: [PATCH] fast-export: factor out print_oid()","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2020-08-15T07:14:49Z","receivedAt":"2020-08-16T00:52:31Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Am 13.08.20 um 19:25 schrieb Jeff King:\n> On Thu, Aug 13, 2020 at 07:17:20PM +0200, René Scharfe wrote:\n>\n>> -- >8 --\n>> Subject: [PATCH v2] fast-export: deduplicate anonymization handling\n>>\n>> Move the code for converting an object_id to a hexadecimal string and\n>> for handling of the default (not anonymizing) case from its callers to\n>> anonymize_oid() and consequently rename it to anonymize_oid_if_needed().\n>> This reduces code duplication.\n>\n> I think this is a bad direction unless you're going to do it for all of\n> the other anonymize_*() functions, as well, for consistency. And there\n> it gets tricky because the caller is able to use the anonymizing\n> knowledge in more places.\n>\n> I actually liked your original version better.\n\nOK, how about embracing the static and do something like this?\n\n-- >8 --\nSubject: [PATCH] fast-export: add format_oid() and format_path()\n\nAdd functions that handle anonymization, quoting and formatting of paths\nand object IDs and return static buffers fit for use with printf().\nUse them to generate each output line containing these components with a\nsingle printf() format specification, which is easier to read.\n\nformat_oid() inherits the ability to be used for four different object\nIDs in parallel from oid_to_hex() -- but here we only need one anyway.\n\nformat_path() has two sets of static buffers, which is just enough for\nour purposes here.\n\nSigned-off-by: René Scharfe <l.s.r@web.de>\n---\n builtin/fast-export.c | 86 ++++++++++++++++++++++---------------------\n 1 file changed, 45 insertions(+), 41 deletions(-)\n\ndiff --git a/builtin/fast-export.c b/builtin/fast-export.c\nindex 9f37895d4cf..a9e36dccf9e 100644\n--- a/builtin/fast-export.c\n+++ b/builtin/fast-export.c\n@@ -368,17 +368,6 @@ static int depth_first(const void *a_, const void *b_)\n \treturn (a->status == 'R') - (b->status == 'R');\n }\n\n-static void print_path_1(const char *path)\n-{\n-\tint need_quote = quote_c_style(path, NULL, NULL, 0);\n-\tif (need_quote)\n-\t\tquote_c_style(path, NULL, stdout, 0);\n-\telse if (strchr(path, ' '))\n-\t\tprintf(\"\\\"%s\\\"\", path);\n-\telse\n-\t\tprintf(\"%s\", path);\n-}\n-\n static char *anonymize_path_component(void *data)\n {\n \tstatic int counter;\n@@ -387,18 +376,34 @@ static char *anonymize_path_component(void *data)\n \treturn strbuf_detach(&out, NULL);\n }\n\n-static void print_path(const char *path)\n+static const char *format_path(const char *path)\n {\n-\tif (!anonymize)\n-\t\tprint_path_1(path);\n-\telse {\n-\t\tstatic struct hashmap paths;\n-\t\tstatic struct strbuf anon = STRBUF_INIT;\n-\n-\t\tanonymize_path(&anon, path, &paths, anonymize_path_component);\n-\t\tprint_path_1(anon.buf);\n-\t\tstrbuf_reset(&anon);\n+\tstatic struct hashmap paths;\n+\tstatic struct strbuf anon_buffers[] = { STRBUF_INIT, STRBUF_INIT };\n+\tstatic struct strbuf quoted_buffers[] = { STRBUF_INIT, STRBUF_INIT };\n+\tstatic int which_buffer;\n+\tstruct strbuf *anon = &anon_buffers[which_buffer];\n+\tstruct strbuf *quoted = &quoted_buffers[which_buffer];\n+\n+\twhich_buffer++;\n+\twhich_buffer %= ARRAY_SIZE(anon_buffers) + BUILD_ASSERT_OR_ZERO(\n+\t\t\tARRAY_SIZE(anon_buffers) == ARRAY_SIZE(quoted_buffers));\n+\n+\tif (anonymize) {\n+\t\tstrbuf_reset(anon);\n+\t\tanonymize_path(anon, path, &paths, anonymize_path_component);\n+\t\tpath = anon->buf;\n+\t}\n+\n+\tstrbuf_reset(quoted);\n+\tif (quote_c_style(path, quoted, NULL, 0))\n+\t\treturn quoted->buf;\n+\tif (strchr(path, ' ')) {\n+\t\tstrbuf_reset(quoted);\n+\t\tstrbuf_addf(quoted, \"\\\"%s\\\"\", path);\n+\t\treturn quoted->buf;\n \t}\n+\treturn path;\n }\n\n static char *generate_fake_oid(void *data)\n@@ -420,6 +425,14 @@ static const char *anonymize_oid(const char *oid_hex)\n \treturn anonymize_str(&objs, generate_fake_oid, oid_hex, len, NULL);\n }\n\n+static const char *format_oid(const struct object_id *oid)\n+{\n+\tconst char *oid_hex = oid_to_hex(oid);\n+\tif (anonymize)\n+\t\toid_hex = anonymize_oid(oid_hex);\n+\treturn oid_hex;\n+}\n+\n static void show_filemodify(struct diff_queue_struct *q,\n \t\t\t    struct diff_options *options, void *data)\n {\n@@ -438,10 +451,8 @@ static void show_filemodify(struct diff_queue_struct *q,\n\n \t\tswitch (q->queue[i]->status) {\n \t\tcase DIFF_STATUS_DELETED:\n-\t\t\tprintf(\"D \");\n-\t\t\tprint_path(spec->path);\n+\t\t\tprintf(\"D %s\\n\", format_path(spec->path));\n \t\t\tstring_list_insert(changed, spec->path);\n-\t\t\tputchar('\\n');\n \t\t\tbreak;\n\n \t\tcase DIFF_STATUS_COPIED:\n@@ -454,12 +465,10 @@ static void show_filemodify(struct diff_queue_struct *q,\n \t\t\t * copy or rename only if there was no change observed.\n \t\t\t */\n \t\t\tif (!string_list_has_string(changed, ospec->path)) {\n-\t\t\t\tprintf(\"%c \", q->queue[i]->status);\n-\t\t\t\tprint_path(ospec->path);\n-\t\t\t\tputchar(' ');\n-\t\t\t\tprint_path(spec->path);\n+\t\t\t\tprintf(\"%c %s %s\\n\", q->queue[i]->status,\n+\t\t\t\t       format_path(ospec->path),\n+\t\t\t\t       format_path(spec->path));\n \t\t\t\tstring_list_insert(changed, spec->path);\n-\t\t\t\tputchar('\\n');\n\n \t\t\t\tif (oideq(&ospec->oid, &spec->oid) &&\n \t\t\t\t    ospec->mode == spec->mode)\n@@ -475,19 +484,17 @@ static void show_filemodify(struct diff_queue_struct *q,\n \t\t\t * output the SHA-1 verbatim.\n \t\t\t */\n \t\t\tif (no_data || S_ISGITLINK(spec->mode))\n-\t\t\t\tprintf(\"M %06o %s \", spec->mode,\n-\t\t\t\t       anonymize ?\n-\t\t\t\t       anonymize_oid(oid_to_hex(&spec->oid)) :\n-\t\t\t\t       oid_to_hex(&spec->oid));\n+\t\t\t\tprintf(\"M %06o %s %s\\n\", spec->mode,\n+\t\t\t\t       format_oid(&spec->oid),\n+\t\t\t\t       format_path(spec->path));\n \t\t\telse {\n \t\t\t\tstruct object *object = lookup_object(the_repository,\n \t\t\t\t\t\t\t\t      &spec->oid);\n-\t\t\t\tprintf(\"M %06o :%d \", spec->mode,\n-\t\t\t\t       get_object_mark(object));\n+\t\t\t\tprintf(\"M %06o :%d %s\\n\", spec->mode,\n+\t\t\t\t       get_object_mark(object),\n+\t\t\t\t       format_path(spec->path));\n \t\t\t}\n-\t\t\tprint_path(spec->path);\n \t\t\tstring_list_insert(changed, spec->path);\n-\t\t\tputchar('\\n');\n \t\t\tbreak;\n\n \t\tdefault:\n@@ -726,10 +733,7 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,\n \t\tif (mark)\n \t\t\tprintf(\":%d\\n\", mark);\n \t\telse\n-\t\t\tprintf(\"%s\\n\",\n-\t\t\t       anonymize ?\n-\t\t\t       anonymize_oid(oid_to_hex(&obj->oid)) :\n-\t\t\t       oid_to_hex(&obj->oid));\n+\t\t\tprintf(\"%s\\n\", format_oid(&obj->oid));\n \t\ti++;\n \t}\n\n--\n2.28.0\n"},{"id":"403899","messageId":"20200817220843.GA1858228@coredump.intra.peff.net","threadId":"54033","inReplyTo":"d25987e5-df7e-d153-bbe4-a4e391ed4454@web.de","subject":"Re: [PATCH] fast-export: factor out print_oid()","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2020-08-17T22:08:43Z","receivedAt":"2020-08-17T22:08:46Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Sat, Aug 15, 2020 at 09:14:49AM +0200, René Scharfe wrote:\n\n> > I think this is a bad direction unless you're going to do it for all of\n> > the other anonymize_*() functions, as well, for consistency. And there\n> > it gets tricky because the caller is able to use the anonymizing\n> > knowledge in more places.\n> >\n> > I actually liked your original version better.\n> \n> OK, how about embracing the static and do something like this?\n> \n> -- >8 --\n> Subject: [PATCH] fast-export: add format_oid() and format_path()\n\nTBH, I don't find it an improvement because of the extra buffer\nhandling. But I admit that I don't really care between any of the\nsolutions posted here. They all appear to be correct, and just trading\noff various properties so that none is definitively better than the\nother. (And none of them is so bad that I feel compelled to avoid it).\n\nSo I'd be OK with any of them (or leaving it as-is).\n\n-Peff\n"},{"id":"403907","messageId":"xmqqd03ozxy6.fsf@gitster.c.googlers.com","threadId":"54033","inReplyTo":"20200817220843.GA1858228@coredump.intra.peff.net","subject":"Re: [PATCH] fast-export: factor out print_oid()","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2020-08-17T22:53:21Z","receivedAt":"2020-08-17T22:53:30Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jeff King <peff@peff.net> writes:\n\n> On Sat, Aug 15, 2020 at 09:14:49AM +0200, René Scharfe wrote:\n>\n>> > I think this is a bad direction unless you're going to do it for all of\n>> > the other anonymize_*() functions, as well, for consistency. And there\n>> > it gets tricky because the caller is able to use the anonymizing\n>> > knowledge in more places.\n>> >\n>> > I actually liked your original version better.\n>> \n>> OK, how about embracing the static and do something like this?\n>> \n>> -- >8 --\n>> Subject: [PATCH] fast-export: add format_oid() and format_path()\n>\n> TBH, I don't find it an improvement because of the extra buffer\n> handling. But I admit that I don't really care between any of the\n> solutions posted here. They all appear to be correct, and just trading\n> off various properties so that none is definitively better than the\n> other. (And none of them is so bad that I feel compelled to avoid it).\n>\n> So I'd be OK with any of them (or leaving it as-is).\n>\n> -Peff\n\nI've marked it as \"retracted\" per\nhttps://lore.kernel.org/git/6e2d4472-8293-4f10-0ba6-82ae83f7a465@web.de/\n\nThanks.\n"}]}