* [PATCH v11 06/13] ref-filter: add option to filter out tags, branches and remotes
@ 2015-08-15 18:00 Karthik Nayak
2015-08-17 4:42 ` Eric Sunshine
0 siblings, 1 reply; 4+ messages in thread
From: Karthik Nayak @ 2015-08-15 18:00 UTC (permalink / raw
To: git; +Cc: christian.couder, Matthieu.Moy, gitster, Karthik Nayak
From: Karthik Nayak <karthik.188@gmail.com>
Add a function called 'for_each_reftype_fullpath()' 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 | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
ref-filter.h | 12 ++++++++++--
refs.c | 7 +++++++
refs.h | 1 +
4 files changed, 72 insertions(+), 7 deletions(-)
diff --git a/ref-filter.c b/ref-filter.c
index eac99d0..abcd235 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -1098,6 +1098,36 @@ 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)
+ return FILTER_REFS_BRANCHES;
+ else if (filter->kind == FILTER_REFS_REMOTES)
+ return FILTER_REFS_REMOTES;
+ else if (filter->kind == FILTER_REFS_TAGS)
+ return FILTER_REFS_TAGS;
+ 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.
@@ -1108,6 +1138,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);
@@ -1119,6 +1150,10 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
return 0;
}
+ 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;
@@ -1150,6 +1185,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;
}
@@ -1226,16 +1262,29 @@ 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;
/* 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 (type & FILTER_REFS_INCLUDE_BROKEN) {
+ type -= FILTER_REFS_INCLUDE_BROKEN;
+ broken = 1;
+ }
+
+ filter->kind = type;
+ if (type == FILTER_REFS_BRANCHES)
+ ret = for_each_reftype_fullpath(ref_filter_handler, "refs/heads/", broken, &ref_cbdata);
+ else if (type == FILTER_REFS_REMOTES)
+ ret = for_each_reftype_fullpath(ref_filter_handler, "refs/remotes/", broken, &ref_cbdata);
+ else if (type == FILTER_REFS_TAGS)
+ ret = for_each_reftype_fullpath(ref_filter_handler, "refs/tags/", broken, &ref_cbdata);
+ else if (type & FILTER_REFS_ALL) {
+ ret = for_each_reftype_fullpath(ref_filter_handler, "", broken, &ref_cbdata);
+ if (type & FILTER_REFS_DETACHED_HEAD)
+ head_ref(ref_filter_handler, &ref_cbdata);
+ } else
die("filter_refs: invalid type");
/* Filters that need revision walking */
diff --git a/ref-filter.h b/ref-filter.h
index 144a633..64fedd3 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -14,8 +14,14 @@
#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
struct atom_value;
@@ -28,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;
@@ -52,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 2db2975..0f18c34 100644
--- a/refs.c
+++ b/refs.c
@@ -2145,6 +2145,13 @@ int for_each_replace_ref(each_ref_fn fn, void *cb_data)
strlen(git_replace_ref_base), 0, cb_data);
}
+int for_each_reftype_fullpath(each_ref_fn fn, char *type, unsigned int broken, void *cb_data)
+{
+ if (broken)
+ broken = DO_FOR_EACH_INCLUDE_BROKEN;
+ return do_for_each_ref(&ref_cache, type, fn, 0, broken, cb_data);
+}
+
int head_ref_namespaced(each_ref_fn fn, void *cb_data)
{
struct strbuf buf = STRBUF_INIT;
diff --git a/refs.h b/refs.h
index 6a3fa6d..7f91a72 100644
--- a/refs.h
+++ b/refs.h
@@ -179,6 +179,7 @@ extern int for_each_remote_ref(each_ref_fn fn, void *cb_data);
extern int for_each_replace_ref(each_ref_fn fn, void *cb_data);
extern int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data);
extern int for_each_glob_ref_in(each_ref_fn fn, const char *pattern, const char *prefix, void *cb_data);
+extern int for_each_reftype_fullpath(each_ref_fn fn, char *type, unsigned int broken, void *cb_data);
extern int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
extern int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
--
2.5.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v11 06/13] ref-filter: add option to filter out tags, branches and remotes
2015-08-15 18:00 [PATCH v11 06/13] ref-filter: add option to filter out tags, branches and remotes Karthik Nayak
@ 2015-08-17 4:42 ` Eric Sunshine
2015-08-17 5:14 ` Eric Sunshine
2015-08-17 14:46 ` Karthik Nayak
0 siblings, 2 replies; 4+ messages in thread
From: Eric Sunshine @ 2015-08-17 4:42 UTC (permalink / raw
To: Karthik Nayak; +Cc: Git List, Christian Couder, Matthieu Moy, Junio C Hamano
On Sat, Aug 15, 2015 at 2:00 PM, Karthik Nayak <karthik.188@gmail.com> wrote:
> Add a function called 'for_each_reftype_fullpath()' 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.
>
> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
> ---
> diff --git a/ref-filter.c b/ref-filter.c
> index eac99d0..abcd235 100644
> --- a/ref-filter.c
> +++ b/ref-filter.c
> @@ -1226,16 +1262,29 @@ 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;
>
> /* 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 (type & FILTER_REFS_INCLUDE_BROKEN) {
> + type -= FILTER_REFS_INCLUDE_BROKEN;
The above is a somewhat unusual way to say the more idiomatic:
type &= ~FILTER_REFS_INCLUDE_BROKEN;
when dealing with bit flags. Is there precedence elsewhere in the
project for choosing '-' over '~'?
> + broken = 1;
> + }
> +
> + filter->kind = type;
> + if (type == FILTER_REFS_BRANCHES)
> + ret = for_each_reftype_fullpath(ref_filter_handler, "refs/heads/", broken, &ref_cbdata);
> + else if (type == FILTER_REFS_REMOTES)
> + ret = for_each_reftype_fullpath(ref_filter_handler, "refs/remotes/", broken, &ref_cbdata);
> + else if (type == FILTER_REFS_TAGS)
> + ret = for_each_reftype_fullpath(ref_filter_handler, "refs/tags/", broken, &ref_cbdata);
> + else if (type & FILTER_REFS_ALL) {
> + ret = for_each_reftype_fullpath(ref_filter_handler, "", broken, &ref_cbdata);
These cases are all the same except for the (string) second argument,
aren't they? This might be less noisy and easier to follow if you
assign the appropriate string to a variable first, and then invoke
for_each_reftype_fullpath() once with the string variable as an
argument.
> + if (type & FILTER_REFS_DETACHED_HEAD)
> + head_ref(ref_filter_handler, &ref_cbdata);
> + } else
> die("filter_refs: invalid type");
>
> /* Filters that need revision walking */
> diff --git a/ref-filter.h b/ref-filter.h
> index 144a633..64fedd3 100644
> --- a/ref-filter.h
> +++ b/ref-filter.h
> @@ -14,8 +14,14 @@
> -#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
I suppose there's some good reason that FILTER_REFS_DETACHED_HEAD is
not a member of FILTER_REFS_ALL? Perhaps add a comment explaining it?
> diff --git a/refs.c b/refs.c
> index 2db2975..0f18c34 100644
> --- a/refs.c
> +++ b/refs.c
> @@ -2145,6 +2145,13 @@ int for_each_replace_ref(each_ref_fn fn, void *cb_data)
> strlen(git_replace_ref_base), 0, cb_data);
> }
>
> +int for_each_reftype_fullpath(each_ref_fn fn, char *type, unsigned int broken, void *cb_data)
> +{
> + if (broken)
> + broken = DO_FOR_EACH_INCLUDE_BROKEN;
It's a bit ugly and confusing to have the same variable, 'broken', act
as both a boolean input argument and as a bit flag argument to the
called function.
> + return do_for_each_ref(&ref_cache, type, fn, 0, broken, cb_data);
> +}
> +
> int head_ref_namespaced(each_ref_fn fn, void *cb_data)
> {
> struct strbuf buf = STRBUF_INIT;
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v11 06/13] ref-filter: add option to filter out tags, branches and remotes
2015-08-17 4:42 ` Eric Sunshine
@ 2015-08-17 5:14 ` Eric Sunshine
2015-08-17 14:46 ` Karthik Nayak
1 sibling, 0 replies; 4+ messages in thread
From: Eric Sunshine @ 2015-08-17 5:14 UTC (permalink / raw
To: Karthik Nayak; +Cc: Git List, Christian Couder, Matthieu Moy, Junio C Hamano
On Mon, Aug 17, 2015 at 12:42 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Sat, Aug 15, 2015 at 2:00 PM, Karthik Nayak <karthik.188@gmail.com> wrote:
>> - 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 (type & FILTER_REFS_INCLUDE_BROKEN) {
>> + type -= FILTER_REFS_INCLUDE_BROKEN;
>
> The above is a somewhat unusual way to say the more idiomatic:
>
> type &= ~FILTER_REFS_INCLUDE_BROKEN;
>
> when dealing with bit flags. Is there precedence elsewhere in the
> project for choosing '-' over '~'?
s/precedence/precedent/
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v11 06/13] ref-filter: add option to filter out tags, branches and remotes
2015-08-17 4:42 ` Eric Sunshine
2015-08-17 5:14 ` Eric Sunshine
@ 2015-08-17 14:46 ` Karthik Nayak
1 sibling, 0 replies; 4+ messages in thread
From: Karthik Nayak @ 2015-08-17 14:46 UTC (permalink / raw
To: Eric Sunshine; +Cc: Git List, Christian Couder, Matthieu Moy, Junio C Hamano
On Mon, Aug 17, 2015 at 10:12 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Sat, Aug 15, 2015 at 2:00 PM, Karthik Nayak <karthik.188@gmail.com> wrote:
>> Add a function called 'for_each_reftype_fullpath()' 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.
>>
>> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
>> ---
>> diff --git a/ref-filter.c b/ref-filter.c
>> index eac99d0..abcd235 100644
>> --- a/ref-filter.c
>> +++ b/ref-filter.c
>> @@ -1226,16 +1262,29 @@ 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;
>>
>> /* 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 (type & FILTER_REFS_INCLUDE_BROKEN) {
>> + type -= FILTER_REFS_INCLUDE_BROKEN;
>
> The above is a somewhat unusual way to say the more idiomatic:
>
> type &= ~FILTER_REFS_INCLUDE_BROKEN;
>
> when dealing with bit flags. Is there precedence elsewhere in the
> project for choosing '-' over '~'?
>
I just preferred it this way, but will change it.
>> + broken = 1;
>> + }
>> +
>> + filter->kind = type;
>> + if (type == FILTER_REFS_BRANCHES)
>> + ret = for_each_reftype_fullpath(ref_filter_handler, "refs/heads/", broken, &ref_cbdata);
>> + else if (type == FILTER_REFS_REMOTES)
>> + ret = for_each_reftype_fullpath(ref_filter_handler, "refs/remotes/", broken, &ref_cbdata);
>> + else if (type == FILTER_REFS_TAGS)
>> + ret = for_each_reftype_fullpath(ref_filter_handler, "refs/tags/", broken, &ref_cbdata);
>> + else if (type & FILTER_REFS_ALL) {
>> + e ret = for_each_reftype_fullpath(ref_filter_handler, "", broken, &ref_cbdata);
>
> These cases are all the same except for the (string) second argument,
> aren't they? This might be less noisy and easier to follow if you
> assign the appropriate string to a variable first, and then invoke
> for_each_reftype_fullpath() once with the string variable as an
> argument.
>
The problem is when we have FILTER_REFS_DETACHED_HEAD we need
to call head_ref() after collecting all other refs, this is because in
branch.c we
need to print the detached head first, hence it's inserted to the end of the
ref_array. Doing it the way you suggested would require to check FILTER_REFS_ALL
again after the first time for setting the string variable. I'd find
that a bit more
confusing.
>> + if (type & FILTER_REFS_DETACHED_HEAD)
>> + head_ref(ref_filter_handler, &ref_cbdata);
>> + } else
>> die("filter_refs: invalid type");
>>
>> /* Filters that need revision walking */
>> diff --git a/ref-filter.h b/ref-filter.h
>> index 144a633..64fedd3 100644
>> --- a/ref-filter.h
>> +++ b/ref-filter.h
>> @@ -14,8 +14,14 @@
>> -#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
>
> I suppose there's some good reason that FILTER_REFS_DETACHED_HEAD is
> not a member of FILTER_REFS_ALL? Perhaps add a comment explaining it?
>
>> diff --git a/refs.c b/refs.c
>> index 2db2975..0f18c34 100644
>> --- a/refs.c
>> +++ b/refs.c
>> @@ -2145,6 +2145,13 @@ int for_each_replace_ref(each_ref_fn fn, void *cb_data)
>> strlen(git_replace_ref_base), 0, cb_data);
>> }
>>
>> +int for_each_reftype_fullpath(each_ref_fn fn, char *type, unsigned int broken, void *cb_data)
>> +{
>> + if (broken)
>> + broken = DO_FOR_EACH_INCLUDE_BROKEN;
>
> It's a bit ugly and confusing to have the same variable, 'broken', act
> as both a boolean input argument and as a bit flag argument to the
> called function.
>
Ill change that :)
--
Regards,
Karthik Nayak
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2015-08-17 14:47 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-08-15 18:00 [PATCH v11 06/13] ref-filter: add option to filter out tags, branches and remotes Karthik Nayak
2015-08-17 4:42 ` Eric Sunshine
2015-08-17 5:14 ` Eric Sunshine
2015-08-17 14:46 ` Karthik Nayak
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).