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 3/4] for-each-ref: convert to ref-filter
Date: Wed, 20 May 2015 18:48:23 +0530 [thread overview]
Message-ID: <1432127904-21070-3-git-send-email-karthik.188@gmail.com> (raw)
In-Reply-To: <555C88C2.8060902@gmail.com>
convert 'for-each-ref' to use the common API provided by 'ref-filter'.
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>
---
builtin/for-each-ref.c | 118 +++++++++----------------------------------------
1 file changed, 20 insertions(+), 98 deletions(-)
diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index 2721228..c9875d1 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -10,6 +10,7 @@
#include "parse-options.h"
#include "remote.h"
#include "color.h"
+#include "ref-filter.h"
/* Quoting styles */
#define QUOTE_NONE 0
@@ -20,25 +21,6 @@
typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
-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;
-};
-
-struct refinfo {
- char *name;
- unsigned char sha1[20];
- int flags;
- const char *symref;
- struct atom_value *value;
-};
-
static struct {
const char *name;
cmp_type cmp_type;
@@ -85,7 +67,7 @@ static struct {
* a "*" to denote deref_tag().
*
* We parse given format string and sort specifiers, and make a list
- * of properties that we need to extract out of objects. refinfo
+ * of properties that we need to extract out of objects. ref_filter_item
* structure will hold an array of values extracted that can be
* indexed with the "atom number", which is an index into this
* array.
@@ -622,7 +604,7 @@ static inline char *copy_advance(char *dst, const char *src)
/*
* Parse the object referred by ref, and grab needed value.
*/
-static void populate_value(struct refinfo *ref)
+static void populate_value(struct ref_filter_item *ref)
{
void *buf;
struct object *obj;
@@ -821,7 +803,7 @@ static void populate_value(struct refinfo *ref)
* Given a ref, return the value for the atom. This lazily gets value
* out of the object by calling populate value.
*/
-static void get_value(struct refinfo *ref, int atom, struct atom_value **v)
+static void get_value(struct ref_filter_item *ref, int atom, struct atom_value **v)
{
if (!ref->value) {
populate_value(ref);
@@ -830,65 +812,7 @@ static void get_value(struct refinfo *ref, int atom, struct atom_value **v)
*v = &ref->value[atom];
}
-struct grab_ref_cbdata {
- struct refinfo **grab_array;
- const char **grab_pattern;
- int grab_cnt;
-};
-
-/*
- * A call-back given to for_each_ref(). Filter refs and keep them for
- * later object processing.
- */
-static int grab_single_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
-{
- struct grab_ref_cbdata *cb = cb_data;
- struct refinfo *ref;
- int cnt;
-
- if (flag & REF_BAD_NAME) {
- warning("ignoring ref with broken name %s", refname);
- return 0;
- }
-
- if (*cb->grab_pattern) {
- const char **pattern;
- int namelen = strlen(refname);
- for (pattern = cb->grab_pattern; *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] == '/'))
- break;
- if (!wildmatch(p, refname, WM_PATHNAME, NULL))
- break;
- }
- if (!*pattern)
- return 0;
- }
-
- /*
- * We do not open the object yet; sort may only need refname
- * to do its job and the resulting list may yet to be pruned
- * by maxcount logic.
- */
- ref = xcalloc(1, sizeof(*ref));
- ref->name = xstrdup(refname);
- hashcpy(ref->sha1, sha1);
- ref->flags = flag;
-
- cnt = cb->grab_cnt;
- REALLOC_ARRAY(cb->grab_array, cnt + 1);
- cb->grab_array[cnt++] = ref;
- cb->grab_cnt = cnt;
- return 0;
-}
-
-static int cmp_ref_sort(struct ref_sort *s, struct refinfo *a, struct refinfo *b)
+static int cmp_ref_sort(struct ref_sort *s, struct ref_filter_item *a, struct ref_filter_item *b)
{
struct atom_value *va, *vb;
int cmp;
@@ -915,8 +839,8 @@ static int cmp_ref_sort(struct ref_sort *s, struct refinfo *a, struct refinfo *b
static struct ref_sort *ref_sort;
static int compare_refs(const void *a_, const void *b_)
{
- struct refinfo *a = *((struct refinfo **)a_);
- struct refinfo *b = *((struct refinfo **)b_);
+ struct ref_filter_item *a = *((struct ref_filter_item **)a_);
+ struct ref_filter_item *b = *((struct ref_filter_item **)b_);
struct ref_sort *s;
for (s = ref_sort; s; s = s->next) {
@@ -927,10 +851,10 @@ static int compare_refs(const void *a_, const void *b_)
return 0;
}
-static void sort_refs(struct ref_sort *sort, struct refinfo **refs, int num_refs)
+static void sort_refs(struct ref_sort *sort, struct ref_filter *refs)
{
ref_sort = sort;
- qsort(refs, num_refs, sizeof(struct refinfo *), compare_refs);
+ qsort(refs->items, refs->count, sizeof(struct ref_filter_item *), compare_refs);
}
static void print_value(struct atom_value *v, int quote_style)
@@ -997,7 +921,7 @@ static void emit(const char *cp, const char *ep)
}
}
-static void show_ref(struct refinfo *info, const char *format, int quote_style)
+static void show_ref(struct ref_filter_item *info, const char *format, int quote_style)
{
const char *cp, *sp, *ep;
@@ -1066,12 +990,12 @@ static char const * const for_each_ref_usage[] = {
int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
{
- int i, num_refs;
+ int i;
const char *format = "%(objectname) %(objecttype)\t%(refname)";
struct ref_sort *sort = NULL, **sort_tail = &sort;
int maxcount = 0, quote_style = 0;
- struct refinfo **refs;
- struct grab_ref_cbdata cbdata;
+ struct ref_filter refs;
+ memset(&refs, 0, sizeof(refs));
struct option opts[] = {
OPT_BIT('s', "shell", "e_style,
@@ -1109,17 +1033,15 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
/* for warn_ambiguous_refs */
git_config(git_default_config, NULL);
- memset(&cbdata, 0, sizeof(cbdata));
- cbdata.grab_pattern = argv;
- for_each_rawref(grab_single_ref, &cbdata);
- refs = cbdata.grab_array;
- num_refs = cbdata.grab_cnt;
+ refs.name_patterns = argv;
+ for_each_rawref(ref_filter_add, &refs);
- sort_refs(sort, refs, num_refs);
+ sort_refs(sort, &refs);
- if (!maxcount || num_refs < maxcount)
- maxcount = num_refs;
+ if (!maxcount || refs.count < maxcount)
+ maxcount = refs.count;
for (i = 0; i < maxcount; i++)
- show_ref(refs[i], format, quote_style);
+ show_ref(refs.items[i], format, quote_style);
+ ref_filter_clear(&refs);
return 0;
}
--
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 ` [PATCH 2/4] ref-filter: add ref-filter API Karthik Nayak
2015-05-20 19:07 ` 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 ` Karthik Nayak [this message]
2015-05-20 23:50 ` [PATCH 3/4] for-each-ref: convert to ref-filter 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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1432127904-21070-3-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.
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 NNTP newsgroup(s).