{"thread":{"id":"18625","subject":"[PATCH] refs: add \"for_each_ref_in\" function to refactor \"for_each_*_ref\" functions","startedAt":"2009-03-30T03:07:15Z","lastAt":"2009-03-30T03:07:15Z","messageCount":1,"participants":["Christian Couder"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"109806","messageId":"20090330050715.7fa6f3ab.chriscool@tuxfamily.org","threadId":"18625","inReplyTo":null,"subject":"[PATCH] refs: add \"for_each_ref_in\" function to refactor \"for_each_*_ref\" functions","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2009-03-30T03:07:15Z","receivedAt":"2009-03-30T03:07:15Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"The \"for_each_{tag,branch,remote,replace,}_ref\" functions are\nredefined in terms of \"for_each_ref_in\" so that we can lose the\nhardcoded length of prefix strings from the code.\n\nAnd the \"for_each_bisect_ref\" as it is only used in \"bisect.c\" and\na call like 'for_each_ref_in(\"refs/bisect/\", register_ref, NULL)'\nis clear enough.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n bisect.c |    2 +-\n refs.c   |   18 +++++++++---------\n refs.h   |    2 +-\n 3 files changed, 11 insertions(+), 11 deletions(-)\n\n\tJunio wrote:\n\n\t> > +static int read_bisect_refs(void)\n\t> > +{\n\t> > +     return for_each_bisect_ref(register_ref, NULL);\n\t> > +}\n\t>\n\t> This is only a minor point, but I do not foresee anybody other than\n\t> bisect--helper (and later bisect) running for_each_bisect_ref().  It might\n\t> make sense to redo [01/10] to introduce\n\t>\n\t>        for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb)\n\t>\n\t> and change this call site to:\n\t>\n\t>        return for_each_ref_in(\"refs/bisect/\", register_ref, NULL);\n\t>\n\t> Needless to say, for_each_{ref,tag_ref,branch_ref,remote_ref}() can be\n\t> redefined in terms of for_each_ref_in() so that we can lose these\n\t> hardcoded length of prefix strings from the code.\n\n\tHere is a patch to do that, though \"for_each_ref\" is not redefined in terms\n\tof \"for_each_ref_in\", as it passes 0 as the length of the prefix string.\n\ndiff --git a/bisect.c b/bisect.c\nindex b4089ca..2e3d063 100644\n--- a/bisect.c\n+++ b/bisect.c\n@@ -422,7 +422,7 @@ static int register_ref(const char *refname, const unsigned char *sha1,\n \n static int read_bisect_refs(void)\n {\n-\treturn for_each_bisect_ref(register_ref, NULL);\n+\treturn for_each_ref_in(\"refs/bisect/\", register_ref, NULL);\n }\n \n void read_bisect_paths(void)\ndiff --git a/refs.c b/refs.c\nindex e512d4c..c52a758 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -647,29 +647,29 @@ int for_each_ref(each_ref_fn fn, void *cb_data)\n \treturn do_for_each_ref(\"refs/\", fn, 0, 0, cb_data);\n }\n \n-int for_each_tag_ref(each_ref_fn fn, void *cb_data)\n+int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)\n {\n-\treturn do_for_each_ref(\"refs/tags/\", fn, 10, 0, cb_data);\n+\treturn do_for_each_ref(prefix, fn, strlen(prefix), 0, cb_data);\n }\n \n-int for_each_branch_ref(each_ref_fn fn, void *cb_data)\n+int for_each_tag_ref(each_ref_fn fn, void *cb_data)\n {\n-\treturn do_for_each_ref(\"refs/heads/\", fn, 11, 0, cb_data);\n+\treturn for_each_ref_in(\"refs/tags/\", fn, cb_data);\n }\n \n-int for_each_remote_ref(each_ref_fn fn, void *cb_data)\n+int for_each_branch_ref(each_ref_fn fn, void *cb_data)\n {\n-\treturn do_for_each_ref(\"refs/remotes/\", fn, 13, 0, cb_data);\n+\treturn for_each_ref_in(\"refs/heads/\", fn, cb_data);\n }\n \n-int for_each_bisect_ref(each_ref_fn fn, void *cb_data)\n+int for_each_remote_ref(each_ref_fn fn, void *cb_data)\n {\n-\treturn do_for_each_ref(\"refs/bisect/\", fn, 12, 0, cb_data);\n+\treturn for_each_ref_in(\"refs/remotes/\", fn, cb_data);\n }\n \n int for_each_replace_ref(each_ref_fn fn, void *cb_data)\n {\n-\treturn do_for_each_ref(\"refs/replace/\", fn, 13, 0, cb_data);\n+\treturn for_each_ref_in(\"refs/replace/\", fn, cb_data);\n }\n \n int for_each_rawref(each_ref_fn fn, void *cb_data)\ndiff --git a/refs.h b/refs.h\nindex c76d96b..18649a7 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -20,10 +20,10 @@ struct ref_lock {\n typedef int each_ref_fn(const char *refname, const unsigned char *sha1, int flags, void *cb_data);\n extern int head_ref(each_ref_fn, void *);\n extern int for_each_ref(each_ref_fn, void *);\n+extern int for_each_ref_in(const char *, each_ref_fn, void *);\n extern int for_each_tag_ref(each_ref_fn, void *);\n extern int for_each_branch_ref(each_ref_fn, void *);\n extern int for_each_remote_ref(each_ref_fn, void *);\n-extern int for_each_bisect_ref(each_ref_fn, void *);\n extern int for_each_replace_ref(each_ref_fn, void *);\n \n /* can be used to learn about broken ref and symref */\n-- \n1.6.2.1.531.gbd5067.dirty\n"}]}