From: Karthik Nayak <karthik.188@gmail.com>
To: git@vger.kernel.org
Cc: matthieu.moy@grenoble-inp.fr, christian.couder@gmail.com,
Karthik Nayak <karthik.188@gmail.com>
Subject: [PATCH 2/4] ref-filter: add ref-filter API
Date: Wed, 20 May 2015 18:48:22 +0530 [thread overview]
Message-ID: <1432127904-21070-2-git-send-email-karthik.188@gmail.com> (raw)
In-Reply-To: <555C88C2.8060902@gmail.com>
add a ref-filter API to provide functions to filter refs for listing.
This will act as a common library for commands like 'tag -l',
'branch -l' and 'for-each-ref'. ref-filter will enable each of these
commands to benefit from the features of the others.
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>
---
Makefile | 1 +
ref-filter.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ref-filter.h | 47 ++++++++++++++++++++++++++++++++++++++
3 files changed, 121 insertions(+)
create mode 100644 ref-filter.c
create mode 100644 ref-filter.h
diff --git a/Makefile b/Makefile
index 5f3987f..1e03bda 100644
--- a/Makefile
+++ b/Makefile
@@ -759,6 +759,7 @@ LIB_OBJS += reachable.o
LIB_OBJS += read-cache.o
LIB_OBJS += reflog-walk.o
LIB_OBJS += refs.o
+LIB_OBJS += ref-filter.o
LIB_OBJS += remote.o
LIB_OBJS += replace_object.o
LIB_OBJS += rerere.o
diff --git a/ref-filter.c b/ref-filter.c
new file mode 100644
index 0000000..df571a6
--- /dev/null
+++ b/ref-filter.c
@@ -0,0 +1,73 @@
+#include "builtin.h"
+#include "cache.h"
+#include "refs.h"
+#include "ref-filter.h"
+#include "wildmatch.h"
+#include "commit.h"
+
+static int match_name_as_path(const char **pattern, const char *refname)
+{
+ int namelen = strlen(refname);
+ for (; *pattern; pattern++) {
+ const char *p = *pattern;
+ int plen = strlen(p);
+
+ if ((plen <= namelen) &&
+ !strncmp(refname, p, plen) &&
+ (refname[plen] == '\0' ||
+ refname[plen] == '/' ||
+ p[plen-1] == '/'))
+ return 1;
+ if (!wildmatch(p, refname, WM_PATHNAME, NULL))
+ return 1;
+ }
+ return 0;
+}
+
+static struct ref_filter_item *new_ref_filter_item(const char *refname,
+ const unsigned char *sha1,
+ int flag)
+{
+ struct ref_filter_item *ref = xcalloc(1, sizeof(struct ref_filter_item));
+ ref->name = xstrdup(refname);
+ hashcpy(ref->sha1, sha1);
+ ref->flags = flag;
+
+ return ref;
+}
+
+/*
+ * A call-back given to for_each_ref(). Filter refs and keep them for
+ * later object processing.
+ */
+int ref_filter_add(const char *refname, const unsigned char *sha1, int flag, void *data)
+{
+ struct ref_filter *ref_list = data;
+ struct ref_filter_item *ref;
+
+ if (flag & REF_BAD_NAME) {
+ warning("ignoring ref with broken name %s", refname);
+ return 0;
+ }
+
+ if (*ref_list->name_patterns && !match_name_as_path(ref_list->name_patterns, refname))
+ return 0;
+
+ ref = new_ref_filter_item(refname, sha1, flag);
+
+ REALLOC_ARRAY(ref_list->items, ref_list->count + 1);
+ ref_list->items[ref_list->count++] = ref;
+
+ return 0;
+}
+
+void ref_filter_clear(struct ref_filter *refs)
+{
+ int i;
+
+ for (i = 0; i < refs->count; i++)
+ free(refs->items[i]);
+ free(refs->items);
+ refs->items = NULL;
+ refs->count = refs->alloc = 0;
+}
diff --git a/ref-filter.h b/ref-filter.h
new file mode 100644
index 0000000..3010d13
--- /dev/null
+++ b/ref-filter.h
@@ -0,0 +1,47 @@
+#ifndef REF_FILTER_H
+#define REF_FILTER_H
+
+#include "sha1-array.h"
+#include "refs.h"
+#include "commit.h"
+
+/*
+ * ref-filter is meant to act as a common provider of API's for
+ * 'tag -l', 'branch -l' and 'for-each-ref'. ref-filter is the attempt
+ * at unification of these three commands so that they ay benefit from
+ * the functionality of each other.
+ */
+
+/* An atom is a valid field atom used for sorting and formatting of refs.*/
+struct atom_value {
+ const char *s;
+ unsigned long ul; /* used for sorting when not FIELD_STR */
+};
+
+struct ref_sort {
+ struct ref_sort *next;
+ int atom; /* index into used_atom array */
+ unsigned reverse : 1;
+};
+
+/* ref_filter_item will hold the data pertaining to a particular ref. */
+struct ref_filter_item {
+ unsigned char sha1[20];
+ int flags;
+ const char *symref;
+ struct atom_value *value;
+ char *name;
+};
+
+/* ref_filter will hold data pertaining to a list of refs. */
+struct ref_filter {
+ int count, alloc;
+ struct ref_filter_item **items;
+ const char **name_patterns;
+};
+
+/* ref_filter_add is used to add refs to the ref_filter list. */
+int ref_filter_add(const char *refname, const unsigned char *sha1, int flags, void *data);
+void ref_filter_clear(struct ref_filter *refs);
+
+#endif /* REF_FILTER_H */
--
2.4.1
next prev parent reply other threads:[~2015-05-20 13:19 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-05-20 13:14 [WIP] [PATCH 0/4] Unifying git branch -l, git tag -l, and git for-each-ref karthik nayak
2015-05-20 13:18 ` [PATCH 1/4] for-each-ref: rename refinfo members to match similar structures Karthik Nayak
2015-05-20 16:57 ` Matthieu Moy
2015-05-21 6:27 ` karthik nayak
2015-05-20 13:18 ` Karthik Nayak [this message]
2015-05-20 19:07 ` [PATCH 2/4] ref-filter: add ref-filter API Eric Sunshine
2015-05-21 17:30 ` karthik nayak
2015-05-21 18:40 ` Eric Sunshine
2015-05-22 12:30 ` karthik nayak
2015-05-21 8:47 ` Matthieu Moy
2015-05-21 17:22 ` karthik nayak
2015-05-21 17:59 ` karthik nayak
2015-05-22 6:44 ` Matthieu Moy
2015-05-22 12:46 ` karthik nayak
2015-05-23 14:42 ` Matthieu Moy
2015-05-23 16:04 ` Christian Couder
2015-05-23 17:00 ` Matthieu Moy
2015-05-23 17:18 ` Junio C Hamano
2015-05-23 22:33 ` Matthieu Moy
2015-05-23 17:52 ` Karthik Nayak
2015-05-20 13:18 ` [PATCH 3/4] for-each-ref: convert to ref-filter Karthik Nayak
2015-05-20 23:50 ` Junio C Hamano
2015-05-21 6:51 ` karthik nayak
2015-05-20 13:18 ` [PATCH 4/4] ref-filter: move formatting/sorting options from 'for-each-ref' Karthik Nayak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1432127904-21070-2-git-send-email-karthik.188@gmail.com \
--to=karthik.188@gmail.com \
--cc=christian.couder@gmail.com \
--cc=git@vger.kernel.org \
--cc=matthieu.moy@grenoble-inp.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).