Move most of the code from 'for-each-ref' to 'ref-filter' to make
it publically available to other commands, this is to unify the code
of 'tag -l', 'branch -l' and 'for-each-ref' so that they can share
their implementations with each other.
Mentored-by: Christian Couder christian.cou...@gmail.com
Mentored-by: Matthieu Moy matthieu@grenoble-inp.fr
Signed-off-by: Karthik Nayak karthik@gmail.com
---
Makefile |1 +
builtin/for-each-ref.c | 1089 +---
ref-filter.c | 1051 ++
ref-filter.h | 66 +++
4 files changed, 1119 insertions(+), 1088 deletions(-)
create mode 100644 ref-filter.c
create mode 100644 ref-filter.h
diff --git a/Makefile b/Makefile
index 36655d5..068d313 100644
--- a/Makefile
+++ b/Makefile
@@ -761,6 +761,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/builtin/for-each-ref.c b/builtin/for-each-ref.c
index f045def..2f11c01 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -1,1095 +1,8 @@
#include builtin.h
#include cache.h
#include refs.h
-#include object.h
-#include tag.h
-#include commit.h
-#include tree.h
-#include blob.h
-#include quote.h
#include parse-options.h
-#include remote.h
-#include color.h
-
-/* Quoting styles */
-#define QUOTE_NONE 0
-#define QUOTE_SHELL 1
-#define QUOTE_PERL 2
-#define QUOTE_PYTHON 4
-#define QUOTE_TCL 8
-
-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 ref_array_item {
- unsigned char objectname[20];
- int flag;
- const char *symref;
- struct atom_value *value;
- char *refname;
-};
-
-struct ref_array {
- int nr, alloc;
- struct ref_array_item **items;
-};
-
-struct ref_filter {
- const char **name_patterns;
-};
-
-struct ref_filter_cbdata {
- struct ref_array array;
- struct ref_filter filter;
-};
-
-static struct {
- const char *name;
- cmp_type cmp_type;
-} valid_atom[] = {
- { refname },
- { objecttype },
- { objectsize, FIELD_ULONG },
- { objectname },
- { tree },
- { parent },
- { numparent, FIELD_ULONG },
- { object },
- { type },
- { tag },
- { author },
- { authorname },
- { authoremail },
- { authordate, FIELD_TIME },
- { committer },
- { committername },
- { committeremail },
- { committerdate, FIELD_TIME },
- { tagger },
- { taggername },
- { taggeremail },
- { taggerdate, FIELD_TIME },
- { creator },
- { creatordate, FIELD_TIME },
- { subject },
- { body },
- { contents },
- { contents:subject },
- { contents:body },
- { contents:signature },
- { upstream },
- { symref },
- { flag },
- { HEAD },
- { color },
-};
-
-/*
- * An atom is a valid field atom listed above, possibly prefixed with
- * 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. ref_array_item
- * structure will hold an array of values extracted that can be
- * indexed with the atom number, which is an index into this
- * array.
- */
-static const char **used_atom;
-static cmp_type *used_atom_type;
-static int used_atom_cnt, need_tagged, need_symref;
-static int need_color_reset_at_eol;
-
-/*
- * Used to parse format string and sort specifiers
- */
-int parse_ref_filter_atom(const char *atom, const char *ep)
-{
- const char *sp;
- int i, at;
-
- sp = atom;
- if (*sp == '*' sp ep)
- sp++; /* deref */
- if (ep = sp)
- die(malformed field name: %.*s, (int)(ep-atom), atom);
-
- /* Do we have the atom already used elsewhere? */
- for (i = 0; i used_atom_cnt; i++) {
- int len = strlen(used_atom[i]);
- if (len == ep - atom !memcmp(used_atom[i], atom, len))
- return i;
- }
-
- /* Is the atom a valid one? */
- for (i = 0; i ARRAY_SIZE(valid_atom); i++) {
- int len = strlen(valid_atom[i].name);
- /*
-* If the atom name has a colon, strip it and everything after
-* it off - it specifies the format for this entry, and
-* shouldn't be used for checking against the valid_atom
-* table.
-*/
- const char *formatp = strchr(sp, ':');
- if (!formatp ||