refs: move for_each_*ref* functions into common code

Make do_for_each_ref take a submodule as an argument instead of a ref_cache. Since all for_each_*ref* functions are defined in terms of do_for_each_ref, we can then move them into the common code. Later, we can simply make do_for_each_ref into a backend function. Signed-off-by: David Turner <dturner@twopensource.com> Reviewed-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

David Turner committed Apr 7, 2016 at 15:02 UTC 937705901b83737a35996f62dbfbfefd8e5c8d7f
3 files changed +66 -57
refs.c
+52
@@ -1103,3 +1103,55 @@ int head_ref(each_ref_fn fn, void *cb_data)
1103 {
1104 return head_ref_submodule(NULL, fn, cb_data);
1105 }
1106 +
1107 +int for_each_ref(each_ref_fn fn, void *cb_data)
1108 +{
1109 + return do_for_each_ref(NULL, "", fn, 0, 0, cb_data);
1110 +}
1111 +
1112 +int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1113 +{
1114 + return do_for_each_ref(submodule, "", fn, 0, 0, cb_data);
1115 +}
1116 +
1117 +int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
1118 +{
1119 + return do_for_each_ref(NULL, prefix, fn, strlen(prefix), 0, cb_data);
1120 +}
1121 +
1122 +int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken)
1123 +{
1124 + unsigned int flag = 0;
1125 +
1126 + if (broken)
1127 + flag = DO_FOR_EACH_INCLUDE_BROKEN;
1128 + return do_for_each_ref(NULL, prefix, fn, 0, flag, cb_data);
1129 +}
1130 +
1131 +int for_each_ref_in_submodule(const char *submodule, const char *prefix,
1132 + each_ref_fn fn, void *cb_data)
1133 +{
1134 + return do_for_each_ref(submodule, prefix, fn, strlen(prefix), 0, cb_data);
1135 +}
1136 +
1137 +int for_each_replace_ref(each_ref_fn fn, void *cb_data)
1138 +{
1139 + return do_for_each_ref(NULL, git_replace_ref_base, fn,
1140 + strlen(git_replace_ref_base), 0, cb_data);
1141 +}
1142 +
1143 +int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
1144 +{
1145 + struct strbuf buf = STRBUF_INIT;
1146 + int ret;
1147 + strbuf_addf(&buf, "%srefs/", get_git_namespace());
1148 + ret = do_for_each_ref(NULL, buf.buf, fn, 0, 0, cb_data);
1149 + strbuf_release(&buf);
1150 + return ret;
1151 +}
1152 +
1153 +int for_each_rawref(each_ref_fn fn, void *cb_data)
1154 +{
1155 + return do_for_each_ref(NULL, "", fn, 0,
1156 + DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
1157 +}
refs/files-backend.c
+5 -57
@@ -513,9 +513,6 @@ static void sort_ref_dir(struct ref_dir *dir)
513 dir->sorted = dir->nr = i;
514 }
515
516 -/* Include broken references in a do_for_each_ref*() iteration: */
517 -#define DO_FOR_EACH_INCLUDE_BROKEN 0x01
518 -
516 /*
517 * Return true iff the reference described by entry can be resolved to
518 * an object in the database. Emit a warning if the referred-to
@@ -1727,10 +1724,13 @@ static int do_for_each_entry(struct ref_cache *refs, const char *base,
1724 * value, stop the iteration and return that value; otherwise, return
1725 * 0.
1726 */
1730 -static int do_for_each_ref(struct ref_cache *refs, const char *base,
1731 - each_ref_fn fn, int trim, int flags, void *cb_data)
1727 +int do_for_each_ref(const char *submodule, const char *base,
1728 + each_ref_fn fn, int trim, int flags, void *cb_data)
1729 {
1730 struct ref_entry_cb data;
1731 + struct ref_cache *refs;
1732 +
1733 + refs = get_ref_cache(submodule);
1734 data.base = base;
1735 data.trim = trim;
1736 data.flags = flags;
@@ -1745,58 +1745,6 @@ static int do_for_each_ref(struct ref_cache *refs, const char *base,
1745 return do_for_each_entry(refs, base, do_one_ref, &data);
1746 }
1747
1748 -int for_each_ref(each_ref_fn fn, void *cb_data)
1749 -{
1750 - return do_for_each_ref(&ref_cache, "", fn, 0, 0, cb_data);
1751 -}
1752 -
1753 -int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1754 -{
1755 - return do_for_each_ref(get_ref_cache(submodule), "", fn, 0, 0, cb_data);
1756 -}
1757 -
1758 -int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
1759 -{
1760 - return do_for_each_ref(&ref_cache, prefix, fn, strlen(prefix), 0, cb_data);
1761 -}
1762 -
1763 -int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken)
1764 -{
1765 - unsigned int flag = 0;
1766 -
1767 - if (broken)
1768 - flag = DO_FOR_EACH_INCLUDE_BROKEN;
1769 - return do_for_each_ref(&ref_cache, prefix, fn, 0, flag, cb_data);
1770 -}
1771 -
1772 -int for_each_ref_in_submodule(const char *submodule, const char *prefix,
1773 - each_ref_fn fn, void *cb_data)
1774 -{
1775 - return do_for_each_ref(get_ref_cache(submodule), prefix, fn, strlen(prefix), 0, cb_data);
1776 -}
1777 -
1778 -int for_each_replace_ref(each_ref_fn fn, void *cb_data)
1779 -{
1780 - return do_for_each_ref(&ref_cache, git_replace_ref_base, fn,
1781 - strlen(git_replace_ref_base), 0, cb_data);
1782 -}
1783 -
1784 -int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
1785 -{
1786 - struct strbuf buf = STRBUF_INIT;
1787 - int ret;
1788 - strbuf_addf(&buf, "%srefs/", get_git_namespace());
1789 - ret = do_for_each_ref(&ref_cache, buf.buf, fn, 0, 0, cb_data);
1790 - strbuf_release(&buf);
1791 - return ret;
1792 -}
1793 -
1794 -int for_each_rawref(each_ref_fn fn, void *cb_data)
1795 -{
1796 - return do_for_each_ref(&ref_cache, "", fn, 0,
1797 - DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
1798 -}
1799 -
1748 static void unlock_ref(struct ref_lock *lock)
1749 {
1750 /* Do not free lock->lk -- atexit() still looks at them */
refs/refs-internal.h
+9
@@ -197,4 +197,13 @@ const char *find_descendant_ref(const char *dirname,
197
198 int rename_ref_available(const char *oldname, const char *newname);
199
200 +
201 +/* Include broken references in a do_for_each_ref*() iteration: */
202 +#define DO_FOR_EACH_INCLUDE_BROKEN 0x01
203 +
204 +/*
205 + * The common backend for the for_each_*ref* functions
206 + */
207 +int do_for_each_ref(const char *submodule, const char *base,
208 + each_ref_fn fn, int trim, int flags, void *cb_data);
209 #endif /* REFS_REFS_INTERNAL_H */