git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v14 05/13] ref-filter: add option to filter out tags, branches and remotes

From
Karthik Nayak <karthik.188@gmail.com>
Date
Aug 29, 2015, 14:12 UTC
Message-ID
<1440857537-13968-6-git-send-email-Karthik.188@gmail.com>
In-Reply-To
<1440857537-13968-1-git-send-email-Karthik.188@gmail.com>
From: Karthik Nayak <karthik.188@gmail.com>

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>
---
 ref-filter.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 ref-filter.h | 13 +++++++++++--
 refs.c       |  9 +++++++++
 refs.h       |  1 +
 4 files changed, 79 insertions(+), 7 deletions(-)
diff --git a/ref-filter.c b/ref-filter.c
index 21c8b5f..5d4f93d 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -1160,6 +1160,34 @@ static struct ref_array_item *new_ref_array_item(const char *refname,
 	return ref;
 }
 
+static int filter_ref_kind(struct ref_filter *filter, const char *refname)
+{
+	unsigned int i;
+
+	static struct {
+		const char *prefix;
+		unsigned int kind;
+	} ref_kind[] = {
+		{ "refs/heads/" , FILTER_REFS_BRANCHES },
+		{ "refs/remotes/" , FILTER_REFS_REMOTES },
+		{ "refs/tags/", FILTER_REFS_TAGS}
+	};
+
+	if (filter->kind == FILTER_REFS_BRANCHES ||
+	    filter->kind == FILTER_REFS_REMOTES ||
+	    filter->kind == FILTER_REFS_TAGS)
+		return filter->kind;
+	else if (!strcmp(refname, "HEAD"))
+		return FILTER_REFS_DETACHED_HEAD;
+
+	for (i = 0; i < ARRAY_SIZE(ref_kind); i++) {
+		if (starts_with(refname, ref_kind[i].prefix))
+			return ref_kind[i].kind;
+	}
+
+	return FILTER_REFS_OTHERS;
+}
+
 /*
  * A call-back given to for_each_ref().  Filter refs and keep them for
  * later object processing.
@@ -1170,6 +1198,7 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
 	struct ref_filter *filter = ref_cbdata->filter;
 	struct ref_array_item *ref;
 	struct commit *commit = NULL;
+	unsigned int kind;
 
 	if (flag & REF_BAD_NAME) {
 		warning("ignoring ref with broken name %s", refname);
@@ -1181,6 +1210,15 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
 		return 0;
 	}
 
+	/*
+	 * Get the current ref kind. If we're filtering tags, remotes or local branches
+	 * only then the current ref-kind is nothing but filter->kind and filter_ref_kind()
+	 * will only return that value.
+	 */
+	kind = filter_ref_kind(filter, refname);
+	if (!(kind & filter->kind))
+		return 0;
+
 	if (*filter->name_patterns && !match_name_as_path(filter->name_patterns, refname))
 		return 0;
 
@@ -1212,6 +1250,7 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
 
 	REALLOC_ARRAY(ref_cbdata->array->items, ref_cbdata->array->nr + 1);
 	ref_cbdata->array->items[ref_cbdata->array->nr++] = ref;
+	ref->kind = kind;
 	return 0;
 }
 
@@ -1288,17 +1327,31 @@ int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int
 {
 	struct ref_filter_cbdata ref_cbdata;
 	int ret = 0;
+	unsigned int broken = 0;
 
 	ref_cbdata.array = array;
 	ref_cbdata.filter = filter;
 
+	if (type & FILTER_REFS_INCLUDE_BROKEN)
+		broken = 1;
+	filter->kind = type & FILTER_REFS_KIND_MASK;
+
 	/*  Simple per-ref filtering */
-	if (type & (FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN))
-		ret = for_each_rawref(ref_filter_handler, &ref_cbdata);
-	else if (type & FILTER_REFS_ALL)
-		ret = for_each_ref(ref_filter_handler, &ref_cbdata);
-	else if (type)
+	if (!filter->kind)
 		die("filter_refs: invalid type");
+	else {
+		if (filter->kind == FILTER_REFS_BRANCHES)
+			ret = for_each_fullref_in("refs/heads/", ref_filter_handler, &ref_cbdata, broken);
+		else if (filter->kind == FILTER_REFS_REMOTES)
+			ret = for_each_fullref_in("refs/remotes/", ref_filter_handler, &ref_cbdata, broken);
+		else if (filter->kind == FILTER_REFS_TAGS)
+			ret = for_each_fullref_in("refs/tags/", ref_filter_handler, &ref_cbdata, broken);
+		else if (filter->kind & FILTER_REFS_ALL)
+			ret = for_each_fullref_in("", ref_filter_handler, &ref_cbdata, broken);
+		if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD))
+			head_ref(ref_filter_handler, &ref_cbdata);
+	}
+
 
 	/*  Filters that need revision walking */
 	if (filter->merge_commit)
diff --git a/ref-filter.h b/ref-filter.h
index 45026d0..0913ba9 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -13,8 +13,15 @@
 #define QUOTE_PYTHON 4
 #define QUOTE_TCL 8
 
-#define FILTER_REFS_INCLUDE_BROKEN 0x1
-#define FILTER_REFS_ALL 0x2
+#define FILTER_REFS_INCLUDE_BROKEN 0x0001
+#define FILTER_REFS_TAGS           0x0002
+#define FILTER_REFS_BRANCHES       0x0004
+#define FILTER_REFS_REMOTES        0x0008
+#define FILTER_REFS_OTHERS         0x0010
+#define FILTER_REFS_ALL            (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \
+				    FILTER_REFS_REMOTES | FILTER_REFS_OTHERS)
+#define FILTER_REFS_DETACHED_HEAD  0x0020
+#define FILTER_REFS_KIND_MASK      (FILTER_REFS_ALL | FILTER_REFS_DETACHED_HEAD)
 
 struct atom_value;
 
@@ -27,6 +34,7 @@ struct ref_sorting {
 struct ref_array_item {
 	unsigned char objectname[20];
 	int flag;
+	unsigned int kind;
 	const char *symref;
 	struct commit *commit;
 	struct atom_value *value;
@@ -51,6 +59,7 @@ struct ref_filter {
 	struct commit *merge_commit;
 
 	unsigned int with_commit_tag_algo : 1;
+	unsigned int kind;
 };
 
 struct ref_filter_cbdata {
diff --git a/refs.c b/refs.c
index 4e15f60..a9469c2 100644
--- a/refs.c
+++ b/refs.c
@@ -2108,6 +2108,15 @@ int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
 	return do_for_each_ref(&ref_cache, prefix, fn, strlen(prefix), 0, cb_data);
 }
 
+int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken)
+{
+	unsigned int flag = 0;
+
+	if (broken)
+		flag = DO_FOR_EACH_INCLUDE_BROKEN;
+	return do_for_each_ref(&ref_cache, prefix, fn, 0, flag, cb_data);
+}
+
 int for_each_ref_in_submodule(const char *submodule, const char *prefix,
 		each_ref_fn fn, void *cb_data)
 {
diff --git a/refs.h b/refs.h
index e9a5f32..6d30c98 100644
--- a/refs.h
+++ b/refs.h
@@ -173,6 +173,7 @@ typedef int each_ref_fn(const char *refname,
 extern int head_ref(each_ref_fn fn, void *cb_data);
 extern int for_each_ref(each_ref_fn fn, void *cb_data);
 extern int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data);
+extern int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken);
 extern int for_each_tag_ref(each_ref_fn fn, void *cb_data);
 extern int for_each_branch_ref(each_ref_fn fn, void *cb_data);
 extern int for_each_remote_ref(each_ref_fn fn, void *cb_data);
-- 
2.5.0
Previous: Karthik NayakNext: Eric Sunshine
Message 27 of 52 in “Port tag.c to use ref-filter.c”
  1. 00/13 Port tag.c to use ref-filter.cKarthik Nayak, Aug 29, 2015
  2. 01/13 ref-filter: move `struct atom_value` to ref-filter.cKarthik Nayak, Aug 29, 2015
  3. 02/13 ref-filter: introduce ref_formatting_state and ref_formatting_stackKarthik Nayak, Aug 29, 2015
  4. 03/13 utf8: add function to align a string into given strbufKarthik Nayak, Aug 29, 2015
  5. Torsten BögershausenAug 29, 2015
  6. Karthik NayakAug 29, 2015
  7. 04/13 ref-filter: implement an `align` atomKarthik Nayak, Aug 29, 2015
  8. Eric SunshineAug 30, 2015
  9. Karthik NayakAug 30, 2015
  10. Eric SunshineAug 30, 2015
  11. Karthik NayakAug 31, 2015
  12. Eric SunshineAug 31, 2015
  13. Matthieu MoyAug 31, 2015
  14. Eric SunshineAug 31, 2015
  15. Karthik NayakSep 1, 2015
  16. Matthieu MoySep 1, 2015
  17. Karthik NayakSep 1, 2015
  18. Karthik NayakAug 30, 2015
  19. Eric SunshineAug 30, 2015
  20. Karthik NayakAug 31, 2015
  21. Junio C HamanoAug 30, 2015
  22. Eric SunshineAug 30, 2015
  23. Karthik NayakAug 31, 2015
  24. Karthik NayakAug 31, 2015
  25. Matthieu MoyAug 31, 2015
  26. Karthik NayakAug 31, 2015
  27. 05/13 ref-filter: add option to filter out tags, branches and remotesKarthik Nayak, Aug 29, 2015
  28. Eric SunshineAug 30, 2015
  29. Karthik NayakAug 30, 2015
  30. Eric SunshineAug 30, 2015
  31. 06/13 ref-filter: introduce format_ref_array_item()Karthik Nayak, Aug 29, 2015
  32. Eric SunshineAug 30, 2015
  33. Karthik NayakAug 30, 2015
  34. Karthik NayakAug 30, 2015
  35. 07/13 ref-filter: add support for %(contents:lines=X)Karthik Nayak, Aug 29, 2015
  36. Eric SunshineAug 30, 2015
  37. Karthik NayakAug 30, 2015
  38. Eric SunshineAug 30, 2015
  39. Karthik NayakAug 30, 2015
  40. Eric SunshineAug 30, 2015
  41. Karthik NayakAug 31, 2015
  42. 08/13 ref-filter: add support to sort by versionKarthik Nayak, Aug 29, 2015
  43. 09/13 ref-filter: add option to match literal patternKarthik Nayak, Aug 29, 2015
  44. 10/13 tag.c: use 'ref-filter' data structuresKarthik Nayak, Aug 29, 2015
  45. 11/13 tag.c: use 'ref-filter' APIsKarthik Nayak, Aug 29, 2015
  46. 12/13 tag.c: implement '--format' optionKarthik Nayak, Aug 29, 2015
  47. 13/13 tag.c: implement '--merged' and '--no-merged' optionsKarthik Nayak, Aug 29, 2015
  48. Matthieu MoyAug 31, 2015
  49. Karthik NayakAug 31, 2015
  50. Matthieu MoyAug 31, 2015
  51. Karthik NayakAug 31, 2015
  52. Karthik NayakSep 1, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.