ref-filter: add option to filter out tags, branches and remotes

Add a function called 'for_each_fullref_in()' to refs.{c,h} which iterates through each ref for the given path without trimming the path and also accounting for broken refs, if mentioned. Add 'filter_ref_kind()' in ref-filter.c to check the kind of ref being handled and return the kind to 'ref_filter_handler()', where we discard refs which we do not need and assign the kind to needed refs. Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Sep 10, 2015 at 21:18 UTC 5b4f28510f36062134390ad8818721c1d4a2760f
4 files changed +81 -7
ref-filter.c
+60 -5
@@ -1190,6 +1190,34 @@ static struct ref_array_item *new_ref_array_item(const char *refname,
1190 return ref;
1191 }
1192
1193 +static int filter_ref_kind(struct ref_filter *filter, const char *refname)
1194 +{
1195 + unsigned int i;
1196 +
1197 + static struct {
1198 + const char *prefix;
1199 + unsigned int kind;
1200 + } ref_kind[] = {
1201 + { "refs/heads/" , FILTER_REFS_BRANCHES },
1202 + { "refs/remotes/" , FILTER_REFS_REMOTES },
1203 + { "refs/tags/", FILTER_REFS_TAGS}
1204 + };
1205 +
1206 + if (filter->kind == FILTER_REFS_BRANCHES ||
1207 + filter->kind == FILTER_REFS_REMOTES ||
1208 + filter->kind == FILTER_REFS_TAGS)
1209 + return filter->kind;
1210 + else if (!strcmp(refname, "HEAD"))
1211 + return FILTER_REFS_DETACHED_HEAD;
1212 +
1213 + for (i = 0; i < ARRAY_SIZE(ref_kind); i++) {
1214 + if (starts_with(refname, ref_kind[i].prefix))
1215 + return ref_kind[i].kind;
1216 + }
1217 +
1218 + return FILTER_REFS_OTHERS;
1219 +}
1220 +
1221 /*
1222 * A call-back given to for_each_ref(). Filter refs and keep them for
1223 * later object processing.
@@ -1200,6 +1228,7 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
1228 struct ref_filter *filter = ref_cbdata->filter;
1229 struct ref_array_item *ref;
1230 struct commit *commit = NULL;
1231 + unsigned int kind;
1232
1233 if (flag & REF_BAD_NAME) {
1234 warning("ignoring ref with broken name %s", refname);
@@ -1211,6 +1240,11 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
1240 return 0;
1241 }
1242
1243 + /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
1244 + kind = filter_ref_kind(filter, refname);
1245 + if (!(kind & filter->kind))
1246 + return 0;
1247 +
1248 if (*filter->name_patterns && !match_name_as_path(filter->name_patterns, refname))
1249 return 0;
1250
@@ -1242,6 +1276,7 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
1276
1277 REALLOC_ARRAY(ref_cbdata->array->items, ref_cbdata->array->nr + 1);
1278 ref_cbdata->array->items[ref_cbdata->array->nr++] = ref;
1279 + ref->kind = kind;
1280 return 0;
1281 }
1282
@@ -1318,17 +1353,37 @@ int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int
1353 {
1354 struct ref_filter_cbdata ref_cbdata;
1355 int ret = 0;
1356 + unsigned int broken = 0;
1357
1358 ref_cbdata.array = array;
1359 ref_cbdata.filter = filter;
1360
1361 + if (type & FILTER_REFS_INCLUDE_BROKEN)
1362 + broken = 1;
1363 + filter->kind = type & FILTER_REFS_KIND_MASK;
1364 +
1365 /* Simple per-ref filtering */
1326 - if (type & (FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN))
1327 - ret = for_each_rawref(ref_filter_handler, &ref_cbdata);
1328 - else if (type & FILTER_REFS_ALL)
1329 - ret = for_each_ref(ref_filter_handler, &ref_cbdata);
1330 - else if (type)
1366 + if (!filter->kind)
1367 die("filter_refs: invalid type");
1368 + else {
1369 + /*
1370 + * For common cases where we need only branches or remotes or tags,
1371 + * we only iterate through those refs. If a mix of refs is needed,
1372 + * we iterate over all refs and filter out required refs with the help
1373 + * of filter_ref_kind().
1374 + */
1375 + if (filter->kind == FILTER_REFS_BRANCHES)
1376 + ret = for_each_fullref_in("refs/heads/", ref_filter_handler, &ref_cbdata, broken);
1377 + else if (filter->kind == FILTER_REFS_REMOTES)
1378 + ret = for_each_fullref_in("refs/remotes/", ref_filter_handler, &ref_cbdata, broken);
1379 + else if (filter->kind == FILTER_REFS_TAGS)
1380 + ret = for_each_fullref_in("refs/tags/", ref_filter_handler, &ref_cbdata, broken);
1381 + else if (filter->kind & FILTER_REFS_ALL)
1382 + ret = for_each_fullref_in("", ref_filter_handler, &ref_cbdata, broken);
1383 + if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD))
1384 + head_ref(ref_filter_handler, &ref_cbdata);
1385 + }
1386 +
1387
1388 /* Filters that need revision walking */
1389 if (filter->merge_commit)
ref-filter.h
+11 -2
@@ -13,8 +13,15 @@
13 #define QUOTE_PYTHON 4
14 #define QUOTE_TCL 8
15
16 -#define FILTER_REFS_INCLUDE_BROKEN 0x1
17 -#define FILTER_REFS_ALL 0x2
16 +#define FILTER_REFS_INCLUDE_BROKEN 0x0001
17 +#define FILTER_REFS_TAGS 0x0002
18 +#define FILTER_REFS_BRANCHES 0x0004
19 +#define FILTER_REFS_REMOTES 0x0008
20 +#define FILTER_REFS_OTHERS 0x0010
21 +#define FILTER_REFS_ALL (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \
22 + FILTER_REFS_REMOTES | FILTER_REFS_OTHERS)
23 +#define FILTER_REFS_DETACHED_HEAD 0x0020
24 +#define FILTER_REFS_KIND_MASK (FILTER_REFS_ALL | FILTER_REFS_DETACHED_HEAD)
25
26 struct atom_value;
27
@@ -27,6 +34,7 @@ struct ref_sorting {
34 struct ref_array_item {
35 unsigned char objectname[20];
36 int flag;
37 + unsigned int kind;
38 const char *symref;
39 struct commit *commit;
40 struct atom_value *value;
@@ -51,6 +59,7 @@ struct ref_filter {
59 struct commit *merge_commit;
60
61 unsigned int with_commit_tag_algo : 1;
62 + unsigned int kind;
63 };
64
65 struct ref_filter_cbdata {
refs.c
+9
@@ -2108,6 +2108,15 @@ int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
2108 return do_for_each_ref(&ref_cache, prefix, fn, strlen(prefix), 0, cb_data);
2109 }
2110
2111 +int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken)
2112 +{
2113 + unsigned int flag = 0;
2114 +
2115 + if (broken)
2116 + flag = DO_FOR_EACH_INCLUDE_BROKEN;
2117 + return do_for_each_ref(&ref_cache, prefix, fn, 0, flag, cb_data);
2118 +}
2119 +
2120 int for_each_ref_in_submodule(const char *submodule, const char *prefix,
2121 each_ref_fn fn, void *cb_data)
2122 {
refs.h
+1
@@ -173,6 +173,7 @@ typedef int each_ref_fn(const char *refname,
173 extern int head_ref(each_ref_fn fn, void *cb_data);
174 extern int for_each_ref(each_ref_fn fn, void *cb_data);
175 extern int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data);
176 +extern int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken);
177 extern int for_each_tag_ref(each_ref_fn fn, void *cb_data);
178 extern int for_each_branch_ref(each_ref_fn fn, void *cb_data);
179 extern int for_each_remote_ref(each_ref_fn fn, void *cb_data);