[PATCH] refs: add "for_each_ref_in" function to refactor "for_each_*_ref" functions

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

 



The "for_each_{tag,branch,remote,replace,}_ref" functions are
redefined in terms of "for_each_ref_in" so that we can lose the
hardcoded length of prefix strings from the code.

And the "for_each_bisect_ref" as it is only used in "bisect.c" and
a call like 'for_each_ref_in("refs/bisect/", register_ref, NULL)'
is clear enough.

Signed-off-by: Christian Couder <chriscool@xxxxxxxxxxxxx>
---
 bisect.c |    2 +-
 refs.c   |   18 +++++++++---------
 refs.h   |    2 +-
 3 files changed, 11 insertions(+), 11 deletions(-)

	Junio wrote:

	> > +static int read_bisect_refs(void)
	> > +{
	> > +     return for_each_bisect_ref(register_ref, NULL);
	> > +}
	>
	> This is only a minor point, but I do not foresee anybody other than
	> bisect--helper (and later bisect) running for_each_bisect_ref().  It might
	> make sense to redo [01/10] to introduce
	>
	>        for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb)
	>
	> and change this call site to:
	>
	>        return for_each_ref_in("refs/bisect/", register_ref, NULL);
	>
	> Needless to say, for_each_{ref,tag_ref,branch_ref,remote_ref}() can be
	> redefined in terms of for_each_ref_in() so that we can lose these
	> hardcoded length of prefix strings from the code.

	Here is a patch to do that, though "for_each_ref" is not redefined in terms
	of "for_each_ref_in", as it passes 0 as the length of the prefix string.

diff --git a/bisect.c b/bisect.c
index b4089ca..2e3d063 100644
--- a/bisect.c
+++ b/bisect.c
@@ -422,7 +422,7 @@ static int register_ref(const char *refname, const unsigned char *sha1,
 
 static int read_bisect_refs(void)
 {
-	return for_each_bisect_ref(register_ref, NULL);
+	return for_each_ref_in("refs/bisect/", register_ref, NULL);
 }
 
 void read_bisect_paths(void)
diff --git a/refs.c b/refs.c
index e512d4c..c52a758 100644
--- a/refs.c
+++ b/refs.c
@@ -647,29 +647,29 @@ int for_each_ref(each_ref_fn fn, void *cb_data)
 	return do_for_each_ref("refs/", fn, 0, 0, cb_data);
 }
 
-int for_each_tag_ref(each_ref_fn fn, void *cb_data)
+int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
 {
-	return do_for_each_ref("refs/tags/", fn, 10, 0, cb_data);
+	return do_for_each_ref(prefix, fn, strlen(prefix), 0, cb_data);
 }
 
-int for_each_branch_ref(each_ref_fn fn, void *cb_data)
+int for_each_tag_ref(each_ref_fn fn, void *cb_data)
 {
-	return do_for_each_ref("refs/heads/", fn, 11, 0, cb_data);
+	return for_each_ref_in("refs/tags/", fn, cb_data);
 }
 
-int for_each_remote_ref(each_ref_fn fn, void *cb_data)
+int for_each_branch_ref(each_ref_fn fn, void *cb_data)
 {
-	return do_for_each_ref("refs/remotes/", fn, 13, 0, cb_data);
+	return for_each_ref_in("refs/heads/", fn, cb_data);
 }
 
-int for_each_bisect_ref(each_ref_fn fn, void *cb_data)
+int for_each_remote_ref(each_ref_fn fn, void *cb_data)
 {
-	return do_for_each_ref("refs/bisect/", fn, 12, 0, cb_data);
+	return for_each_ref_in("refs/remotes/", fn, cb_data);
 }
 
 int for_each_replace_ref(each_ref_fn fn, void *cb_data)
 {
-	return do_for_each_ref("refs/replace/", fn, 13, 0, cb_data);
+	return for_each_ref_in("refs/replace/", fn, cb_data);
 }
 
 int for_each_rawref(each_ref_fn fn, void *cb_data)
diff --git a/refs.h b/refs.h
index c76d96b..18649a7 100644
--- a/refs.h
+++ b/refs.h
@@ -20,10 +20,10 @@ struct ref_lock {
 typedef int each_ref_fn(const char *refname, const unsigned char *sha1, int flags, void *cb_data);
 extern int head_ref(each_ref_fn, void *);
 extern int for_each_ref(each_ref_fn, void *);
+extern int for_each_ref_in(const char *, each_ref_fn, void *);
 extern int for_each_tag_ref(each_ref_fn, void *);
 extern int for_each_branch_ref(each_ref_fn, void *);
 extern int for_each_remote_ref(each_ref_fn, void *);
-extern int for_each_bisect_ref(each_ref_fn, void *);
 extern int for_each_replace_ref(each_ref_fn, void *);
 
 /* can be used to learn about broken ref and symref */
-- 
1.6.2.1.531.gbd5067.dirty

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[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