fast-export: factor out print_oid()
PATCH 1/1 fast-export: factor out print_oid()
Simplify the output code by splitting it up and reducing duplication. Reuse the logic for printing object IDs -- anonymized if needed -- by moving it to its own function, print_oid().
--- builtin/fast-export.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-)
builtin/fast-export.c +15 -11
@@ -420,6 +420,14 @@ static const char *anonymize_oid(const char *oid_hex)return anonymize_str (&objs, generate_fake_oid, oid_hex, len,NULL ); } +static void print_oid (const struct object_id *oid,int anonymize) +{ +const char *oid_hex =oid_to_hex (oid); +if (anonymize) + oid_hex =anonymize_oid (oid_hex); +fputs (oid_hex, stdout); +} +static void show_filemodify (struct diff_queue_struct *q,struct diff_options *options,void *data) {
@@ -470,21 +478,19 @@ static void show_filemodify(struct diff_queue_struct *q,case DIFF_STATUS_TYPE_CHANGED :case DIFF_STATUS_MODIFIED :case DIFF_STATUS_ADDED : +printf ("M %06o " , spec->mode);/* * Links refer to objects in another repositories; * 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)); +print_oid (&spec->oid, anonymize);else {struct object *object =lookup_object (the_repository, &spec->oid); -printf ("M %06o :%d " , spec->mode, -get_object_mark (object)); +printf (":%d" ,get_object_mark (object)); } +putchar (' ' );print_path (spec->path);string_list_insert (changed, spec->path);putchar ('\n' );
@@ -724,12 +730,10 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,else printf ("merge " );if (mark) -printf (":%d\n" , mark); +printf (":%d" , mark);else -printf ("%s\n" , - anonymize ? -anonymize_oid (oid_to_hex (&obj->oid)) : -oid_to_hex (&obj->oid)); +print_oid (&obj->oid, anonymize); +putchar ('\n' ); i++; }
--
2.28.0
Quoted 3 lines · René Scharfe
Simplify the output code by splitting it up and reducing duplication. Reuse the logic for printing object IDs -- anonymized if needed -- by moving it to its own function,
print_oid().
Looks sane overall, though somehow we added 4 extra lines while reducing duplication. ;)
builtin/fast-export.c static const char *anonymize_oid(const char *oid_hex) · line 429
Quoted 7 lines
builtin/fast-export.c +7
-0+static void print_oid (const struct object_id *oid,int anonymize) +{ +const char *oid_hex =oid_to_hex (oid); +if (anonymize) + oid_hex =anonymize_oid (oid_hex); +fputs (oid_hex, stdout); +}
Would anyone ever pass anything except the global "anonymize" into this function (certainly neither of the new callers does). I get that it takes us on a possible road towards moving the globals to locals, but in the meantime, shadowing the global name just seems more confusing to me.
builtin/fast-export.c static void show_filemodify(struct diff_queue_struct *q, · line 481
Quoted 5 lines
builtin/fast-export.c +1
-0@@ -470,21 +478,19 @@ static void show_filemodify(struct diff_queue_struct *q,case DIFF_STATUS_TYPE_CHANGED :case DIFF_STATUS_MODIFIED :case DIFF_STATUS_ADDED : +printf ("M %06o " , spec->mode);
This makes the output a bit more lego-like (i.e., hard to see what the full line will look like from the code), but it already was pretty bad because of using print_path(). I think that's fine.
builtin/fast-export.c static void handle_commit(struct commit *commit, struct rev_info *rev, · line 733
Quoted 6 lines
builtin/fast-export.c +1
-1@@ -724,12 +730,10 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,else printf ("merge " );if (mark) -printf (":%d\n" , mark); +printf (":%d" , mark);
This line gets repeated, too. I guess we could have a print_mark(), but there is really no logic here except "put a colon in front of it", so probably not worthwhile.
-Peff
Am 13.08.20 um 13:58 schrieb Jeff King:
Quoted 8 lines · René Scharfe
Quoted 4 lines
Simplify the output code by splitting it up and reducing duplication. Reuse the logic for printing object IDs -- anonymized if needed -- by moving it to its own function,
print_oid().Looks sane overall, though somehow we added 4 extra lines while reducing duplication. ;)
Yeah, but they are shorter. :)
Quoted 13 lines
builtin/fast-export.c static const char *anonymize_oid(const char *oid_hex) · line 429
Quoted 8 lines
builtin/fast-export.c +7
-0+static void print_oid (const struct object_id *oid,int anonymize) +{ +const char *oid_hex =oid_to_hex (oid); +if (anonymize) + oid_hex =anonymize_oid (oid_hex); +fputs (oid_hex, stdout); +}Would anyone ever pass anything except the global "anonymize" into this function (certainly neither of the new callers does). I get that it takes us on a possible road towards moving the globals to locals, but in the meantime, shadowing the global name just seems more confusing to me.
Good point.
Quoted 10 lines
builtin/fast-export.c static void show_filemodify(struct diff_queue_struct *q, · line 481
Quoted 6 lines
builtin/fast-export.c +1
-0@@ -470,21 +478,19 @@ static void show_filemodify(struct diff_queue_struct *q,case DIFF_STATUS_TYPE_CHANGED :case DIFF_STATUS_MODIFIED :case DIFF_STATUS_ADDED : +printf ("M %06o " , spec->mode);This makes the output a bit more lego-like (i.e., hard to see what the full line will look like from the code), but it already was pretty bad because of using
print_path(). I think that's fine.
Yes, it was almost halfway to all-out lego style before, and the patch moves it further in that direction.
Quoted 11 lines
builtin/fast-export.c static void handle_commit(struct commit *commit, struct rev_info *rev, · line 733
Quoted 7 lines
builtin/fast-export.c +1
-1@@ -724,12 +730,10 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,else printf ("merge " );if (mark) -printf (":%d\n" , mark); +printf (":%d" , mark);This line gets repeated, too. I guess we could have a
print_mark(), but there is really no logic here except "put a colon in front of it", so probably not worthwhile.
Right.
René
Quoted 15 lines · René Scharfe
Simplify the output code by splitting it up and reducing duplication. Reuse the logic for printing object IDs -- anonymized if needed -- by moving it to its own function,
print_oid().Signed-off-by René Scharfe <[email protected]>--- builtin/fast-export.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-)builtin/fast-export.c +0
-0@@ -420,6 +420,14 @@ static const char *anonymize_oid(const char *oid_hex)return anonymize_str (&objs, generate_fake_oid, oid_hex, len,NULL );
builtin/fast-export.c static const char *anonymize_oid(const char *oid_hex) · line 430
Quoted 10 lines
builtin/fast-export.c +8
-0} +static void print_oid (const struct object_id *oid,int anonymize) +{ +const char *oid_hex =oid_to_hex (oid); +if (anonymize) + oid_hex =anonymize_oid (oid_hex); +fputs (oid_hex, stdout); +} +
The fact that this calls fputs makes this patch (in my own opinion) noisier than it needs to be. This is because of all of the factoring out of the other printfs. I'd expect that this looks something more like:
- anonymize ? - anonymize_oid(oid_to_hex(&spec->oid)) : - oid_to_hex(&spec->oid)); + anonymize_oid(anonymize, &spec->oid));
without moving around all of the other printf code.
Quoted 16 lines
builtin/fast-export.c +1
-3static void show_filemodify (struct diff_queue_struct *q,struct diff_options *options,void *data) {@@ -470,21 +478,19 @@ static void show_filemodify(struct diff_queue_struct *q,case DIFF_STATUS_TYPE_CHANGED :case DIFF_STATUS_MODIFIED :case DIFF_STATUS_ADDED : +printf ("M %06o " , spec->mode);/* * Links refer to objects in another repositories; * 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)) :
builtin/fast-export.c static void show_filemodify(struct diff_queue_struct *q, · line 493
Quoted 10 lines
builtin/fast-export.c +3
-3- oid_to_hex(&spec->oid)); +print_oid (&spec->oid, anonymize); else {struct object *object =lookup_object (the_repository, &spec->oid); -printf ("M %06o :%d " , spec->mode, -get_object_mark (object)); +printf (":%d" ,get_object_mark (object)); } +putchar (' ' );
... That said, this transformation looks correct from a quick glance.
Quoted 6 lines
builtin/fast-export.c +0
-0print_path (spec->path);string_list_insert (changed, spec->path);putchar ('\n' );@@ -724,12 +730,10 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,else printf ("merge " );
builtin/fast-export.c static void handle_commit(struct commit *commit, struct rev_info *rev, · line 736
Quoted 10 lines
builtin/fast-export.c +3
-5if (mark) -printf (":%d\n" , mark); +printf (":%d" , mark);else -printf ("%s\n" , - anonymize ? -anonymize_oid (oid_to_hex (&obj->oid)) : -oid_to_hex (&obj->oid)); +print_oid (&obj->oid, anonymize); +putchar ('\n' );
As does this one.
Quoted 5 lines
builtin/fast-export.c +0
-1i++; } --2.28.0
So, I guess what I'm trying to say is that this patch doesn't look wrong, other than that it seems more invasive than I would have expected it to be.
Thanks,
Taylor
Am 13.08.20 um 17:18 schrieb Taylor Blau:
Quoted 31 lines · René Scharfe
Quoted 15 lines
Simplify the output code by splitting it up and reducing duplication. Reuse the logic for printing object IDs -- anonymized if needed -- by moving it to its own function,
print_oid().Signed-off-by René Scharfe <[email protected]>--- builtin/fast-export.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-)builtin/fast-export.c +0
-0@@ -420,6 +420,14 @@ static const char *anonymize_oid(const char *oid_hex)return anonymize_str (&objs, generate_fake_oid, oid_hex, len,NULL );builtin/fast-export.c static const char *anonymize_oid(const char *oid_hex) · line 430
Quoted 10 lines
builtin/fast-export.c +8
-0} +static void print_oid (const struct object_id *oid,int anonymize) +{ +const char *oid_hex =oid_to_hex (oid); +if (anonymize) + oid_hex =anonymize_oid (oid_hex); +fputs (oid_hex, stdout); +} +The fact that this calls fputs makes this patch (in my own opinion) noisier than it needs to be. This is because of all of the factoring out of the other printfs. I'd expect that this looks something more like:
Quoted 6 lines
- anonymize ? - anonymize_oid(oid_to_hex(&spec->oid)) : - oid_to_hex(&spec->oid)); + anonymize_oid(anonymize, &spec->oid));without moving around all of the other printf code.
Moving that part to anonymize_oid() would reduce the line count while still getting rid of the duplication. But the function would need a new name.
-- >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.
--- builtin/fast-export.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)
builtin/fast-export.c +6 -8
@@ -413,10 +413,13 @@ static char *generate_fake_oid(void *data)return hash_to_hex_algop_r (hex, out, the_hash_algo); } -static const char *anonymize_oid (const char *oid_hex) +static const char *anonymize_oid_if_needed (const struct object_id *oid) {static struct hashmap objs; +const char *oid_hex =oid_to_hex (oid);size_t len =strlen (oid_hex); +if (!anonymize) +return oid_hex;return anonymize_str (&objs, generate_fake_oid, oid_hex, len,NULL ); }
@@ -476,9 +479,7 @@ static void show_filemodify(struct diff_queue_struct *q,*/ 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)); +anonymize_oid_if_needed (&spec->oid));else {struct object *object =lookup_object (the_repository, &spec->oid);
@@ -726,10 +727,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" ,anonymize_oid_if_needed (&obj->oid)); i++; }
--
2.28.0
Quoted 7 lines · René Scharfe
-- >8 --
Subject: [PATCH v2] fast-export: deduplicate anonymization handlingMove the code for converting an
object_idto a hexadecimal string and for handling of the default (not anonymizing) case from its callers toanonymize_oid()and consequently rename it toanonymize_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.
-Peff
Am 13.08.20 um 19:25 schrieb Jeff King:
Quoted 10 lines · René Scharfe
Quoted 8 lines
-- >8 --
Subject: [PATCH v2] fast-export: deduplicate anonymization handlingMove the code for converting an
object_idto a hexadecimal string and for handling of the default (not anonymizing) case from its callers toanonymize_oid()and consequently rename it toanonymize_oid_if_needed(). This reduces code duplication.
Quoted 6 lines
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.
--- builtin/fast-export.c | 86 ++++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 41 deletions(-)
builtin/fast-export.c +45 -41
@@ -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 = "ed_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
Quoted 11 lines · René Scharfe
Quoted 6 lines
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: addformat_oid()andformat_path()
TBH, I don't find it an improvement because of the extra buffer handling. But I admit that I don't really care between any of the solutions posted here. They all appear to be correct, and just trading off various properties so that none is definitively better than the other. (And none of them is so bad that I feel compelled to avoid it).
So I'd be OK with any of them (or leaving it as-is).
-Peff
Quoted 20 lines · Jeff King
Quoted 11 lines · René Scharfe
Quoted 6 lines
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: addformat_oid()andformat_path()TBH, I don't find it an improvement because of the extra buffer handling. But I admit that I don't really care between any of the solutions posted here. They all appear to be correct, and just trading off various properties so that none is definitively better than the other. (And none of them is so bad that I feel compelled to avoid it).
Quoted 3 lines
So I'd be OK with any of them (or leaving it as-is).
-Peff
I've marked it as "retracted" per
https://lore.kernel.org/git/[email protected]/
Thanks.
Quoted 18 lines · Taylor Blau
Review
Quoted 8 lines
(quoted diff) +8
-0+static void print_oid(const struct object_id *oid, int anonymize) +{ + const char *oid_hex = oid_to_hex(oid); + if (anonymize) + oid_hex = anonymize_oid(oid_hex); + fputs(oid_hex, stdout); +} +The fact that this calls fputs makes this patch (in my own opinion) noisier than it needs to be. This is because of all of the factoring out of the other printfs. I'd expect that this looks something more like:
- anonymize ? - anonymize_oid(oid_to_hex(&spec->oid)) : - oid_to_hex(&spec->oid)); + anonymize_oid(anonymize, &spec->oid));
Quoted 5 lines
without moving around all of the other printf code.
...
So, I guess what I'm trying to say is that this patch doesn't look wrong, other than that it seems more invasive than I would have expected it to be.
Yes, that matches my knee-jerk reaction. I also shared Peff's reaction that the code to generate the message is now even more fragmented into pieces.
Just for comparison purposes, I tried to fold anonymize_oid()'s body into René's print_oid() and adjusted the calling sites, which did not look too bad.
So, I dunno.
builtin/fast-export.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
builtin/fast-export.c +11 -11
@@ -413,11 +413,16 @@ static char *generate_fake_oid(void *data)return hash_to_hex_algop_r (hex, out, the_hash_algo); } -static const char *anonymize_oid (const char *oid_hex) +static const char *anonymize_oid (const struct object_id *oid,int anonymize) { -static struct hashmap objs; -size_t len =strlen (oid_hex); -return anonymize_str (&objs, generate_fake_oid, oid_hex, len,NULL ); +const char *oid_hex =oid_to_hex (oid); +if (anonymize) { +static struct hashmap objs; +size_t len =strlen (oid_hex); +return anonymize_str (&objs, generate_fake_oid, oid_hex, len,NULL ); + }else { +return oid_hex; + } } static void show_filemodify(struct diff_queue_struct *q,
@@ -476,9 +481,7 @@ static void show_filemodify(struct diff_queue_struct *q,*/ 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)); +anonymize_oid (&spec->oid, anonymize));else {struct object *object =lookup_object (the_repository, &spec->oid);
@@ -726,10 +729,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" ,anonymize_oid (&obj->oid, anonymize)); i++; }