* [WIP/PATCH v4 1/8] for-each-ref: extract helper functions out of grab_single_ref() [not found] <5569EF77.4010300@gmail.com> @ 2015-05-30 17:53 ` Karthik Nayak 2015-05-31 2:58 ` Eric Sunshine 2015-05-31 17:34 ` Junio C Hamano 2015-05-30 17:53 ` [WIP/PATCH v4 2/8] for-each-ref: simplify code Karthik Nayak ` (6 subsequent siblings) 7 siblings, 2 replies; 44+ messages in thread From: Karthik Nayak @ 2015-05-30 17:53 UTC (permalink / raw) To: git; +Cc: christian.couder, Matthieu.Moy, Karthik Nayak Extract two helper functions out of grab_single_ref(). Firstly, new_refinfo() which is used to allocate memory for a new refinfo structure and copy the objectname, refname and flag to it. Secondly, match_name_as_path() which when given an array of patterns and the refname checks if the refname matches any of the patterns given while the pattern is a pathname, also while supporting wild characters. This is a preperatory patch for restructuring 'for-each-ref' and eventually moving most of it to 'ref-filter' to provide the functionality to similar commands via public API's. Helped-by: Junio C Hamano <gitster@pobox.com> 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 | 69 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 28 deletions(-) diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c index 83f9cf9..b33e2de 100644 --- a/builtin/for-each-ref.c +++ b/builtin/for-each-ref.c @@ -837,6 +837,43 @@ struct grab_ref_cbdata { }; /* + * Given a refname, return 1 if the refname matches with one of the patterns + * while the pattern is a pathname like 'refs/tags' or 'refs/heads/master' + * and so on, else return 0. Supports use of wild characters. + */ +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; +} + +/* Allocate space for a new refinfo and copy the objectname and flag to it */ +static struct refinfo *new_refinfo(const char *refname, + const unsigned char *objectname, + int flag) +{ + struct refinfo *ref = xcalloc(1, sizeof(struct refinfo)); + ref->refname = xstrdup(refname); + hashcpy(ref->objectname, objectname); + ref->flag = flag; + + return ref; +} + +/* * A call-back given to for_each_ref(). Filter refs and keep them for * later object processing. */ @@ -851,40 +888,16 @@ static int grab_single_ref(const char *refname, const unsigned char *sha1, int f 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; - } + if (*cb->grab_pattern && !match_name_as_path(cb->grab_pattern, refname)) + 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->refname = xstrdup(refname); - hashcpy(ref->objectname, sha1); - ref->flag = flag; + ref = new_refinfo(refname, sha1, flag); cnt = cb->grab_cnt; REALLOC_ARRAY(cb->grab_array, cnt + 1); cb->grab_array[cnt++] = ref; cb->grab_cnt = cnt; + return 0; } -- 2.4.2 ^ permalink raw reply related [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 1/8] for-each-ref: extract helper functions out of grab_single_ref() 2015-05-30 17:53 ` [WIP/PATCH v4 1/8] for-each-ref: extract helper functions out of grab_single_ref() Karthik Nayak @ 2015-05-31 2:58 ` Eric Sunshine 2015-05-31 8:11 ` Karthik Nayak 2015-05-31 17:34 ` Junio C Hamano 1 sibling, 1 reply; 44+ messages in thread From: Eric Sunshine @ 2015-05-31 2:58 UTC (permalink / raw) To: Karthik Nayak; +Cc: Git List, Christian Couder, Matthieu Moy On Sat, May 30, 2015 at 1:53 PM, Karthik Nayak <karthik.188@gmail.com> wrote: > Extract two helper functions out of grab_single_ref(). Firstly, > new_refinfo() which is used to allocate memory for a new refinfo > structure and copy the objectname, refname and flag to it. > Secondly, match_name_as_path() which when given an array of patterns > and the refname checks if the refname matches any of the patterns > given while the pattern is a pathname, also while supporting wild > characters. > > This is a preperatory patch for restructuring 'for-each-ref' and > eventually moving most of it to 'ref-filter' to provide the > functionality to similar commands via public API's. > > Helped-by: Junio C Hamano <gitster@pobox.com> > 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> > --- > diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c > index 83f9cf9..b33e2de 100644 > --- a/builtin/for-each-ref.c > +++ b/builtin/for-each-ref.c > @@ -837,6 +837,43 @@ struct grab_ref_cbdata { > }; > > /* > + * Given a refname, return 1 if the refname matches with one of the patterns > + * while the pattern is a pathname like 'refs/tags' or 'refs/heads/master' > + * and so on, else return 0. Supports use of wild characters. > + */ > +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; > +} > + > +/* Allocate space for a new refinfo and copy the objectname and flag to it */ > +static struct refinfo *new_refinfo(const char *refname, > + const unsigned char *objectname, > + int flag) > +{ > + struct refinfo *ref = xcalloc(1, sizeof(struct refinfo)); > + ref->refname = xstrdup(refname); > + hashcpy(ref->objectname, objectname); > + ref->flag = flag; > + > + return ref; > +} > + > +/* > * A call-back given to for_each_ref(). Filter refs and keep them for > * later object processing. > */ > @@ -851,40 +888,16 @@ static int grab_single_ref(const char *refname, const unsigned char *sha1, int f > 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; > - } > + if (*cb->grab_pattern && !match_name_as_path(cb->grab_pattern, refname)) > + 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. > - */ Why did this comment get removed? Isn't it still meaningful to the overall logic of this function? > - ref = xcalloc(1, sizeof(*ref)); > - ref->refname = xstrdup(refname); > - hashcpy(ref->objectname, sha1); > - ref->flag = flag; > + ref = new_refinfo(refname, sha1, flag); > > cnt = cb->grab_cnt; > REALLOC_ARRAY(cb->grab_array, cnt + 1); > cb->grab_array[cnt++] = ref; > cb->grab_cnt = cnt; > + Sneaking in whitespace change? > return 0; > } ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 1/8] for-each-ref: extract helper functions out of grab_single_ref() 2015-05-31 2:58 ` Eric Sunshine @ 2015-05-31 8:11 ` Karthik Nayak 0 siblings, 0 replies; 44+ messages in thread From: Karthik Nayak @ 2015-05-31 8:11 UTC (permalink / raw) To: Eric Sunshine; +Cc: Git List, Christian Couder, Matthieu Moy > > Why did this comment get removed? Isn't it still meaningful to the > overall logic of this function? > Wasn't intentional, will put that back. > > Sneaking in whitespace change? > Noted. thanks -- Regards, Karthik ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 1/8] for-each-ref: extract helper functions out of grab_single_ref() 2015-05-30 17:53 ` [WIP/PATCH v4 1/8] for-each-ref: extract helper functions out of grab_single_ref() Karthik Nayak 2015-05-31 2:58 ` Eric Sunshine @ 2015-05-31 17:34 ` Junio C Hamano 2015-05-31 17:48 ` Karthik Nayak 2015-05-31 20:39 ` Matthieu Moy 1 sibling, 2 replies; 44+ messages in thread From: Junio C Hamano @ 2015-05-31 17:34 UTC (permalink / raw) To: Karthik Nayak; +Cc: git, christian.couder, Matthieu.Moy Karthik Nayak <karthik.188@gmail.com> writes: > /* > + * Given a refname, return 1 if the refname matches with one of the patterns > + * while the pattern is a pathname like 'refs/tags' or 'refs/heads/master' > + * and so on, else return 0. Supports use of wild characters. > + */ > +static int match_name_as_path(const char **pattern, const char *refname) > +{ I wonder why this is not "match_refname", in other words, what the significance of "as path" part of the name? If you later are going to introuce match_name_as_something_else() and that name may not be a refname, then this naming is perfectly sane. If such a function you will later introduce will still deal with names that are only refnames, then match_refname_as_path() would be sensible. Otherwise this name seems overly long (i.e. "as_path" may not be adding value) while not being desctiptive enough (i.e. is meant to be limited to refnames but just says "name"). Just being curious. > @@ -851,40 +888,16 @@ static int grab_single_ref(const char *refname, const unsigned char *sha1, int f > return 0; > } > > - if (*cb->grab_pattern) { > -... > - } > + if (*cb->grab_pattern && !match_name_as_path(cb->grab_pattern, refname)) > + 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)); The comment still is a good reminder for those who will later touch this grab_single_ref() function to make them think twice. Thanks. ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 1/8] for-each-ref: extract helper functions out of grab_single_ref() 2015-05-31 17:34 ` Junio C Hamano @ 2015-05-31 17:48 ` Karthik Nayak 2015-05-31 20:39 ` Matthieu Moy 1 sibling, 0 replies; 44+ messages in thread From: Karthik Nayak @ 2015-05-31 17:48 UTC (permalink / raw) To: Junio C Hamano; +Cc: git, christian.couder, Matthieu.Moy On 05/31/2015 11:04 PM, Junio C Hamano wrote: > Karthik Nayak <karthik.188@gmail.com> writes: > >> /* >> + * Given a refname, return 1 if the refname matches with one of the patterns >> + * while the pattern is a pathname like 'refs/tags' or 'refs/heads/master' >> + * and so on, else return 0. Supports use of wild characters. >> + */ >> +static int match_name_as_path(const char **pattern, const char *refname) >> +{ > > I wonder why this is not "match_refname", in other words, what the > significance of "as path" part of the name? If you later are going > to introuce match_name_as_something_else() and that name may not be > a refname, then this naming is perfectly sane. If such a function > you will later introduce will still deal with names that are only > refnames, then match_refname_as_path() would be sensible. Otherwise > this name seems overly long (i.e. "as_path" may not be adding value) > while not being desctiptive enough (i.e. is meant to be limited to > refnames but just says "name"). > > Just being curious. > Good Question, This is because in the future I'll eventually add pattern matching for 'tag -l' and 'branch -l' which wont match as path, but would be a regular string match (with wild character support). Hence keeping that in mind I included 'as_path()' in the function name. > > The comment still is a good reminder for those who will later touch > this grab_single_ref() function to make them think twice. > > Thanks. > Yes! It was removed it by mistake. -- Regards, Karthik ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 1/8] for-each-ref: extract helper functions out of grab_single_ref() 2015-05-31 17:34 ` Junio C Hamano 2015-05-31 17:48 ` Karthik Nayak @ 2015-05-31 20:39 ` Matthieu Moy 2015-06-01 14:39 ` Junio C Hamano 1 sibling, 1 reply; 44+ messages in thread From: Matthieu Moy @ 2015-05-31 20:39 UTC (permalink / raw) To: Junio C Hamano; +Cc: Karthik Nayak, git, christian.couder Junio C Hamano <gitster@pobox.com> writes: > Karthik Nayak <karthik.188@gmail.com> writes: > >> /* >> + * Given a refname, return 1 if the refname matches with one of the patterns >> + * while the pattern is a pathname like 'refs/tags' or 'refs/heads/master' >> + * and so on, else return 0. Supports use of wild characters. >> + */ >> +static int match_name_as_path(const char **pattern, const char *refname) >> +{ > > I wonder why this is not "match_refname", in other words, what the > significance of "as path" part of the name? Just match_refname may not carry all the semantics of the function, which matches a prefix up to the end of string, or up to a / (but you can't just be a prefix stopping in the middle of a word). To me, the "_as_path" helped understanding the overall behavior of the function. -- Matthieu Moy http://www-verimag.imag.fr/~moy/ ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 1/8] for-each-ref: extract helper functions out of grab_single_ref() 2015-05-31 20:39 ` Matthieu Moy @ 2015-06-01 14:39 ` Junio C Hamano 0 siblings, 0 replies; 44+ messages in thread From: Junio C Hamano @ 2015-06-01 14:39 UTC (permalink / raw) To: Matthieu Moy; +Cc: Karthik Nayak, git, christian.couder Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes: > Just match_refname may not carry all the semantics of the function, > which matches a prefix up to the end of string, or up to a / (but you > can't just be a prefix stopping in the middle of a word). To me, the > "_as_path" helped understanding the overall behavior of the function. Yeah, Karthik explained that the plan is to add different kinds of matching later, and in that context _as_path is a good way to tell them apart. Thanks. ^ permalink raw reply [flat|nested] 44+ messages in thread
* [WIP/PATCH v4 2/8] for-each-ref: simplify code [not found] <5569EF77.4010300@gmail.com> 2015-05-30 17:53 ` [WIP/PATCH v4 1/8] for-each-ref: extract helper functions out of grab_single_ref() Karthik Nayak @ 2015-05-30 17:53 ` Karthik Nayak 2015-05-30 17:53 ` [WIP/PATCH v4 3/8] for-each-ref: rename 'refinfo' to 'ref_array_item' Karthik Nayak ` (5 subsequent siblings) 7 siblings, 0 replies; 44+ messages in thread From: Karthik Nayak @ 2015-05-30 17:53 UTC (permalink / raw) To: git; +Cc: christian.couder, Matthieu.Moy, Karthik Nayak In 'grab_single_ref()' remove the extra count variable 'cnt' and use the variable 'grab_cnt' of structure 'grab_ref_cbdata' directly instead. 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 | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c index b33e2de..919d45e 100644 --- a/builtin/for-each-ref.c +++ b/builtin/for-each-ref.c @@ -881,7 +881,6 @@ static int grab_single_ref(const char *refname, const unsigned char *sha1, int f { 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); @@ -893,10 +892,8 @@ static int grab_single_ref(const char *refname, const unsigned char *sha1, int f ref = new_refinfo(refname, sha1, flag); - cnt = cb->grab_cnt; - REALLOC_ARRAY(cb->grab_array, cnt + 1); - cb->grab_array[cnt++] = ref; - cb->grab_cnt = cnt; + REALLOC_ARRAY(cb->grab_array, cb->grab_cnt + 1); + cb->grab_array[cb->grab_cnt++] = ref; return 0; } -- 2.4.2 ^ permalink raw reply related [flat|nested] 44+ messages in thread
* [WIP/PATCH v4 3/8] for-each-ref: rename 'refinfo' to 'ref_array_item' [not found] <5569EF77.4010300@gmail.com> 2015-05-30 17:53 ` [WIP/PATCH v4 1/8] for-each-ref: extract helper functions out of grab_single_ref() Karthik Nayak 2015-05-30 17:53 ` [WIP/PATCH v4 2/8] for-each-ref: simplify code Karthik Nayak @ 2015-05-30 17:53 ` Karthik Nayak 2015-05-31 17:37 ` Junio C Hamano 2015-05-30 17:53 ` [WIP/PATCH v4 4/8] for-each-ref: introduce new structures for better organisation Karthik Nayak ` (4 subsequent siblings) 7 siblings, 1 reply; 44+ messages in thread From: Karthik Nayak @ 2015-05-30 17:53 UTC (permalink / raw) To: git; +Cc: christian.couder, Matthieu.Moy, Karthik Nayak Rename 'refinfo' to 'ref_array_item' as a preparatory step for introduction of new structures in the forthcoming patch. Re-order the fields in 'ref_array_item' so that refname can be eventually converted to a FLEX_ARRAY. 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 | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c index 919d45e..e634fd2 100644 --- a/builtin/for-each-ref.c +++ b/builtin/for-each-ref.c @@ -31,12 +31,12 @@ struct ref_sort { unsigned reverse : 1; }; -struct refinfo { - char *refname; +struct ref_array_item { unsigned char objectname[20]; int flag; const char *symref; struct atom_value *value; + char *refname; }; static struct { @@ -85,7 +85,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_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. @@ -622,7 +622,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_array_item *ref) { void *buf; struct object *obj; @@ -821,7 +821,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_array_item *ref, int atom, struct atom_value **v) { if (!ref->value) { populate_value(ref); @@ -831,7 +831,7 @@ static void get_value(struct refinfo *ref, int atom, struct atom_value **v) } struct grab_ref_cbdata { - struct refinfo **grab_array; + struct ref_array_item **grab_array; const char **grab_pattern; int grab_cnt; }; @@ -860,12 +860,12 @@ static int match_name_as_path(const char **pattern, const char *refname) return 0; } -/* Allocate space for a new refinfo and copy the objectname and flag to it */ -static struct refinfo *new_refinfo(const char *refname, - const unsigned char *objectname, - int flag) +/* Allocate space for a new ref_array_item and copy the objectname and flag to it */ +static struct ref_array_item *new_ref_array_item(const char *refname, + const unsigned char *objectname, + int flag) { - struct refinfo *ref = xcalloc(1, sizeof(struct refinfo)); + struct ref_array_item *ref = xcalloc(1, sizeof(struct ref_array_item)); ref->refname = xstrdup(refname); hashcpy(ref->objectname, objectname); ref->flag = flag; @@ -880,7 +880,7 @@ static struct refinfo *new_refinfo(const char *refname, 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; + struct ref_array_item *ref; if (flag & REF_BAD_NAME) { warning("ignoring ref with broken name %s", refname); @@ -890,7 +890,7 @@ static int grab_single_ref(const char *refname, const unsigned char *sha1, int f if (*cb->grab_pattern && !match_name_as_path(cb->grab_pattern, refname)) return 0; - ref = new_refinfo(refname, sha1, flag); + ref = new_ref_array_item(refname, sha1, flag); REALLOC_ARRAY(cb->grab_array, cb->grab_cnt + 1); cb->grab_array[cb->grab_cnt++] = ref; @@ -898,7 +898,7 @@ static int grab_single_ref(const char *refname, const unsigned char *sha1, int f 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_array_item *a, struct ref_array_item *b) { struct atom_value *va, *vb; int cmp; @@ -925,8 +925,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_array_item *a = *((struct ref_array_item **)a_); + struct ref_array_item *b = *((struct ref_array_item **)b_); struct ref_sort *s; for (s = ref_sort; s; s = s->next) { @@ -937,10 +937,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_array_item **refs, int num_refs) { ref_sort = sort; - qsort(refs, num_refs, sizeof(struct refinfo *), compare_refs); + qsort(refs, num_refs, sizeof(struct ref_array_item *), compare_refs); } static void print_value(struct atom_value *v, int quote_style) @@ -1007,7 +1007,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_array_item *info, const char *format, int quote_style) { const char *cp, *sp, *ep; @@ -1080,7 +1080,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix) 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 ref_array_item **refs; struct grab_ref_cbdata cbdata; struct option opts[] = { -- 2.4.2 ^ permalink raw reply related [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 3/8] for-each-ref: rename 'refinfo' to 'ref_array_item' 2015-05-30 17:53 ` [WIP/PATCH v4 3/8] for-each-ref: rename 'refinfo' to 'ref_array_item' Karthik Nayak @ 2015-05-31 17:37 ` Junio C Hamano 2015-05-31 17:48 ` Karthik Nayak 0 siblings, 1 reply; 44+ messages in thread From: Junio C Hamano @ 2015-05-31 17:37 UTC (permalink / raw) To: Karthik Nayak; +Cc: git, christian.couder, Matthieu.Moy Karthik Nayak <karthik.188@gmail.com> writes: > Rename 'refinfo' to 'ref_array_item' as a preparatory step for introduction of new structures in the forthcoming patch. > > Re-order the fields in 'ref_array_item' so that refname can be > eventually converted to a FLEX_ARRAY. Not a big enough deal to require a reroll, but I think it would be easier to read if this reordering is done in the same patch that makes refname[] into a flex array. ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 3/8] for-each-ref: rename 'refinfo' to 'ref_array_item' 2015-05-31 17:37 ` Junio C Hamano @ 2015-05-31 17:48 ` Karthik Nayak 0 siblings, 0 replies; 44+ messages in thread From: Karthik Nayak @ 2015-05-31 17:48 UTC (permalink / raw) To: Junio C Hamano; +Cc: git, christian.couder, Matthieu.Moy On 05/31/2015 11:07 PM, Junio C Hamano wrote: > Karthik Nayak <karthik.188@gmail.com> writes: > >> Rename 'refinfo' to 'ref_array_item' as a preparatory step for introduction of new structures in the forthcoming patch. >> >> Re-order the fields in 'ref_array_item' so that refname can be >> eventually converted to a FLEX_ARRAY. > > Not a big enough deal to require a reroll, but I think it would be > easier to read if this reordering is done in the same patch that > makes refname[] into a flex array. > Yes I was thinking the same, I'll add that in the re-roll. -- Regards, Karthik ^ permalink raw reply [flat|nested] 44+ messages in thread
* [WIP/PATCH v4 4/8] for-each-ref: introduce new structures for better organisation [not found] <5569EF77.4010300@gmail.com> ` (2 preceding siblings ...) 2015-05-30 17:53 ` [WIP/PATCH v4 3/8] for-each-ref: rename 'refinfo' to 'ref_array_item' Karthik Nayak @ 2015-05-30 17:53 ` Karthik Nayak 2015-05-31 3:14 ` Eric Sunshine 2015-05-30 17:53 ` [WIP/PATCH v4 5/8] for-each-ref: introduce 'ref_filter_clear_data()' Karthik Nayak ` (3 subsequent siblings) 7 siblings, 1 reply; 44+ messages in thread From: Karthik Nayak @ 2015-05-30 17:53 UTC (permalink / raw) To: git; +Cc: christian.couder, Matthieu.Moy, Karthik Nayak Intoduce 'ref_filter_cbdata' which will hold 'ref_filter' (Conditions to filter the refs on) and 'ref_array' (The array of ref_array_items). Modify the code to use these new structures. This is a preparatory patch to eventually move code from 'for-each-ref' to 'ref-filter' and making it publically available. 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 | 56 ++++++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c index e634fd2..ef54c90 100644 --- a/builtin/for-each-ref.c +++ b/builtin/for-each-ref.c @@ -39,6 +39,20 @@ struct ref_array_item { 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; @@ -85,7 +99,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. ref_array_item + * 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. @@ -830,12 +844,6 @@ static void get_value(struct ref_array_item *ref, int atom, struct atom_value ** *v = &ref->value[atom]; } -struct grab_ref_cbdata { - struct ref_array_item **grab_array; - const char **grab_pattern; - int grab_cnt; -}; - /* * Given a refname, return 1 if the refname matches with one of the patterns * while the pattern is a pathname like 'refs/tags' or 'refs/heads/master' @@ -879,7 +887,8 @@ static struct ref_array_item *new_ref_array_item(const char *refname, */ 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 ref_filter_cbdata *ref_cbdata = cb_data; + struct ref_filter *filter = &ref_cbdata->filter; struct ref_array_item *ref; if (flag & REF_BAD_NAME) { @@ -887,13 +896,13 @@ static int grab_single_ref(const char *refname, const unsigned char *sha1, int f return 0; } - if (*cb->grab_pattern && !match_name_as_path(cb->grab_pattern, refname)) + if (*filter->name_patterns && !match_name_as_path(filter->name_patterns, refname)) return 0; ref = new_ref_array_item(refname, sha1, flag); - REALLOC_ARRAY(cb->grab_array, cb->grab_cnt + 1); - cb->grab_array[cb->grab_cnt++] = ref; + REALLOC_ARRAY(ref_cbdata->array.items, ref_cbdata->array.nr + 1); + ref_cbdata->array.items[ref_cbdata->array.nr++] = ref; return 0; } @@ -937,10 +946,10 @@ static int compare_refs(const void *a_, const void *b_) return 0; } -static void sort_refs(struct ref_sort *sort, struct ref_array_item **refs, int num_refs) +static void sort_refs(struct ref_sort *sort, struct ref_array *array) { ref_sort = sort; - qsort(refs, num_refs, sizeof(struct ref_array_item *), compare_refs); + qsort(array->items, array->nr, sizeof(struct ref_array_item *), compare_refs); } static void print_value(struct atom_value *v, int quote_style) @@ -1076,12 +1085,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 ref_array_item **refs; - struct grab_ref_cbdata cbdata; + struct ref_filter_cbdata ref_cbdata; + memset(&ref_cbdata, 0, sizeof(ref_cbdata)); struct option opts[] = { OPT_BIT('s', "shell", "e_style, @@ -1119,17 +1128,14 @@ 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; + ref_cbdata.filter.name_patterns = argv; + for_each_rawref(grab_single_ref, &ref_cbdata); - sort_refs(sort, refs, num_refs); + sort_refs(sort, &ref_cbdata.array); - if (!maxcount || num_refs < maxcount) - maxcount = num_refs; + if (!maxcount || ref_cbdata.array.nr < maxcount) + maxcount = ref_cbdata.array.nr; for (i = 0; i < maxcount; i++) - show_ref(refs[i], format, quote_style); + show_ref(ref_cbdata.array.items[i], format, quote_style); return 0; } -- 2.4.2 ^ permalink raw reply related [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 4/8] for-each-ref: introduce new structures for better organisation 2015-05-30 17:53 ` [WIP/PATCH v4 4/8] for-each-ref: introduce new structures for better organisation Karthik Nayak @ 2015-05-31 3:14 ` Eric Sunshine 2015-05-31 8:16 ` Karthik Nayak 0 siblings, 1 reply; 44+ messages in thread From: Eric Sunshine @ 2015-05-31 3:14 UTC (permalink / raw) To: Karthik Nayak; +Cc: Git List, Christian Couder, Matthieu Moy On Sat, May 30, 2015 at 1:53 PM, Karthik Nayak <karthik.188@gmail.com> wrote: > Intoduce 'ref_filter_cbdata' which will hold 'ref_filter' s/Intoduce/Introduce/ > (Conditions to filter the refs on) and 'ref_array' (The array s/Conditions/conditions/ s/The/the/ > of ref_array_items). Modify the code to use these new structures. > > This is a preparatory patch to eventually move code from 'for-each-ref' > to 'ref-filter' and making it publically available. s/publically/publicly/ > 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> > --- > diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c > index e634fd2..ef54c90 100644 > --- a/builtin/for-each-ref.c > +++ b/builtin/for-each-ref.c > @@ -85,7 +99,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. ref_array_item > + * of properties that we need to extract out of objects. ref_array_item Sneaking in whitespace change? > * structure will hold an array of values extracted that can be > * indexed with the "atom number", which is an index into this > * array. > @@ -1076,12 +1085,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 ref_array_item **refs; > - struct grab_ref_cbdata cbdata; > + struct ref_filter_cbdata ref_cbdata; > + memset(&ref_cbdata, 0, sizeof(ref_cbdata)); > > struct option opts[] = { Declaration (struct option opts[]) after statement (memset). I think you want to leave the memset() where it was below. > OPT_BIT('s', "shell", "e_style, > @@ -1119,17 +1128,14 @@ 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; > + ref_cbdata.filter.name_patterns = argv; > + for_each_rawref(grab_single_ref, &ref_cbdata); > > - sort_refs(sort, refs, num_refs); > + sort_refs(sort, &ref_cbdata.array); > > - if (!maxcount || num_refs < maxcount) > - maxcount = num_refs; > + if (!maxcount || ref_cbdata.array.nr < maxcount) > + maxcount = ref_cbdata.array.nr; > for (i = 0; i < maxcount; i++) > - show_ref(refs[i], format, quote_style); > + show_ref(ref_cbdata.array.items[i], format, quote_style); > return 0; > } > -- > 2.4.2 ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 4/8] for-each-ref: introduce new structures for better organisation 2015-05-31 3:14 ` Eric Sunshine @ 2015-05-31 8:16 ` Karthik Nayak 0 siblings, 0 replies; 44+ messages in thread From: Karthik Nayak @ 2015-05-31 8:16 UTC (permalink / raw) To: Eric Sunshine; +Cc: Git List, Christian Couder, Matthieu Moy On 05/31/2015 08:44 AM, Eric Sunshine wrote: > On Sat, May 30, 2015 at 1:53 PM, Karthik Nayak <karthik.188@gmail.com> wrote: >> Intoduce 'ref_filter_cbdata' which will hold 'ref_filter' > > s/Intoduce/Introduce/ > >> (Conditions to filter the refs on) and 'ref_array' (The array > > s/Conditions/conditions/ > s/The/the/ > >> of ref_array_items). Modify the code to use these new structures. >> >> This is a preparatory patch to eventually move code from 'for-each-ref' >> to 'ref-filter' and making it publically available. > > s/publically/publicly/ > >> 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> >> --- >> diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c >> index e634fd2..ef54c90 100644 >> --- a/builtin/for-each-ref.c >> +++ b/builtin/for-each-ref.c >> @@ -85,7 +99,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. ref_array_item >> + * of properties that we need to extract out of objects. ref_array_item > > Sneaking in whitespace change? > >> * structure will hold an array of values extracted that can be >> * indexed with the "atom number", which is an index into this >> * array. >> @@ -1076,12 +1085,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 ref_array_item **refs; >> - struct grab_ref_cbdata cbdata; >> + struct ref_filter_cbdata ref_cbdata; >> + memset(&ref_cbdata, 0, sizeof(ref_cbdata)); >> >> struct option opts[] = { > > Declaration (struct option opts[]) after statement (memset). I think > you want to leave the memset() where it was below. > >> OPT_BIT('s', "shell", "e_style, >> @@ -1119,17 +1128,14 @@ 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; >> + ref_cbdata.filter.name_patterns = argv; >> + for_each_rawref(grab_single_ref, &ref_cbdata); >> >> - sort_refs(sort, refs, num_refs); >> + sort_refs(sort, &ref_cbdata.array); >> >> - if (!maxcount || num_refs < maxcount) >> - maxcount = num_refs; >> + if (!maxcount || ref_cbdata.array.nr < maxcount) >> + maxcount = ref_cbdata.array.nr; >> for (i = 0; i < maxcount; i++) >> - show_ref(refs[i], format, quote_style); >> + show_ref(ref_cbdata.array.items[i], format, quote_style); >> return 0; >> } >> -- >> 2.4.2 Will change these, thanks! -- Regards, Karthik ^ permalink raw reply [flat|nested] 44+ messages in thread
* [WIP/PATCH v4 5/8] for-each-ref: introduce 'ref_filter_clear_data()' [not found] <5569EF77.4010300@gmail.com> ` (3 preceding siblings ...) 2015-05-30 17:53 ` [WIP/PATCH v4 4/8] for-each-ref: introduce new structures for better organisation Karthik Nayak @ 2015-05-30 17:53 ` Karthik Nayak 2015-05-31 7:38 ` Christian Couder 2015-05-30 17:53 ` [WIP/PATCH v4 6/8] for-each-ref: rename some functions and make them public Karthik Nayak ` (2 subsequent siblings) 7 siblings, 1 reply; 44+ messages in thread From: Karthik Nayak @ 2015-05-30 17:53 UTC (permalink / raw) To: git; +Cc: christian.couder, Matthieu.Moy, Karthik Nayak Introduce and implement 'ref_filter_clear_data()' which will free all allocated memory for 'ref_filter_cbdata' and its underlying array of 'ref_array_item'. 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 | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c index ef54c90..f896e1c 100644 --- a/builtin/for-each-ref.c +++ b/builtin/for-each-ref.c @@ -907,6 +907,18 @@ static int grab_single_ref(const char *refname, const unsigned char *sha1, int f return 0; } +/* Free all memory allocated for ref_filter_cbdata */ +void ref_filter_clear_data(struct ref_filter_cbdata *ref_cbdata) +{ + int i; + + for (i = 0; i < ref_cbdata->array.nr; i++) + free(ref_cbdata->array.items[i]); + free(ref_cbdata->array.items); + ref_cbdata->array.items = NULL; + ref_cbdata->array.nr = ref_cbdata->array.alloc = 0; +} + static int cmp_ref_sort(struct ref_sort *s, struct ref_array_item *a, struct ref_array_item *b) { struct atom_value *va, *vb; @@ -1137,5 +1149,6 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix) maxcount = ref_cbdata.array.nr; for (i = 0; i < maxcount; i++) show_ref(ref_cbdata.array.items[i], format, quote_style); + ref_filter_clear_data(&ref_cbdata); return 0; } -- 2.4.2 ^ permalink raw reply related [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 5/8] for-each-ref: introduce 'ref_filter_clear_data()' 2015-05-30 17:53 ` [WIP/PATCH v4 5/8] for-each-ref: introduce 'ref_filter_clear_data()' Karthik Nayak @ 2015-05-31 7:38 ` Christian Couder 2015-05-31 8:20 ` Karthik Nayak 0 siblings, 1 reply; 44+ messages in thread From: Christian Couder @ 2015-05-31 7:38 UTC (permalink / raw) To: Karthik Nayak; +Cc: git, Matthieu Moy On Sat, May 30, 2015 at 7:53 PM, Karthik Nayak <karthik.188@gmail.com> wrote: > Introduce and implement 'ref_filter_clear_data()' which will free > all allocated memory for 'ref_filter_cbdata' and its underlying array > of 'ref_array_item'. > > 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 | 13 +++++++++++++ > 1 file changed, 13 insertions(+) > > diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c > index ef54c90..f896e1c 100644 > --- a/builtin/for-each-ref.c > +++ b/builtin/for-each-ref.c > @@ -907,6 +907,18 @@ static int grab_single_ref(const char *refname, const unsigned char *sha1, int f > return 0; > } > > +/* Free all memory allocated for ref_filter_cbdata */ > +void ref_filter_clear_data(struct ref_filter_cbdata *ref_cbdata) > +{ > + int i; > + > + for (i = 0; i < ref_cbdata->array.nr; i++) > + free(ref_cbdata->array.items[i]); > + free(ref_cbdata->array.items); > + ref_cbdata->array.items = NULL; > + ref_cbdata->array.nr = ref_cbdata->array.alloc = 0; > +} As this is clearing the array only, it would be better to have it in a ref_array_clear() function. There are already argv_array_clear() and sha1_array_clear() by the way. And maybe if many such ref_array functions are created and start being used elsewhere we can easily move everything into new ref-array.{c,h} files. Thanks, Christian. ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 5/8] for-each-ref: introduce 'ref_filter_clear_data()' 2015-05-31 7:38 ` Christian Couder @ 2015-05-31 8:20 ` Karthik Nayak 0 siblings, 0 replies; 44+ messages in thread From: Karthik Nayak @ 2015-05-31 8:20 UTC (permalink / raw) To: Christian Couder; +Cc: git, Matthieu Moy > > As this is clearing the array only, it would be better to have it in a > ref_array_clear() function. > There are already argv_array_clear() and sha1_array_clear() by the way. > And maybe if many such ref_array functions are created and start being > used elsewhere we can easily move everything into new ref-array.{c,h} > files. > Makes sense. Will change. -- Regards, Karthik ^ permalink raw reply [flat|nested] 44+ messages in thread
* [WIP/PATCH v4 6/8] for-each-ref: rename some functions and make them public [not found] <5569EF77.4010300@gmail.com> ` (4 preceding siblings ...) 2015-05-30 17:53 ` [WIP/PATCH v4 5/8] for-each-ref: introduce 'ref_filter_clear_data()' Karthik Nayak @ 2015-05-30 17:53 ` Karthik Nayak 2015-05-31 3:21 ` Eric Sunshine ` (2 more replies) 2015-05-30 17:53 ` [WIP/PATCH v4 7/8] ref-filter: move code from 'for-each-ref' Karthik Nayak 2015-05-30 17:53 ` [WIP/PATCH v4 8/8] ref-filter: add 'ref-filter.h' Karthik Nayak 7 siblings, 3 replies; 44+ messages in thread From: Karthik Nayak @ 2015-05-30 17:53 UTC (permalink / raw) To: git; +Cc: christian.couder, Matthieu.Moy, Karthik Nayak Rename some of the functions and make them publically available. This is a preparatory step for moving code from 'for-each-ref' to 'ref-filter' to make meaningful, targeted services available to other commands via public APIs. Based-on-patch-by: Jeff King <peff@peff.net> 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 | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c index f896e1c..8fed04b 100644 --- a/builtin/for-each-ref.c +++ b/builtin/for-each-ref.c @@ -112,7 +112,7 @@ static int need_color_reset_at_eol; /* * Used to parse format string and sort specifiers */ -static int parse_atom(const char *atom, const char *ep) +int parse_ref_filter_atom(const char *atom, const char *ep) { const char *sp; int i, at; @@ -189,7 +189,7 @@ static const char *find_next(const char *cp) * Make sure the format string is well formed, and parse out * the used atoms. */ -static int verify_format(const char *format) +int verify_ref_format(const char *format) { const char *cp, *sp; @@ -201,7 +201,7 @@ static int verify_format(const char *format) if (!ep) return error("malformed format string %s", sp); /* sp points at "%(" and ep points at the closing ")" */ - at = parse_atom(sp + 2, ep); + at = parse_ref_filter_atom(sp + 2, ep); cp = ep + 1; if (skip_prefix(used_atom[at], "color:", &color)) @@ -408,7 +408,7 @@ static void grab_date(const char *buf, struct atom_value *v, const char *atomnam /* * We got here because atomname ends in "date" or "date<something>"; * it's not possible that <something> is not ":<format>" because - * parse_atom() wouldn't have allowed it, so we can assume that no + * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no * ":" means no format is specified, and use the default. */ formatp = strchr(atomname, ':'); @@ -835,7 +835,7 @@ static void populate_value(struct ref_array_item *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 ref_array_item *ref, int atom, struct atom_value **v) +static void get_ref_atom_value(struct ref_array_item *ref, int atom, struct atom_value **v) { if (!ref->value) { populate_value(ref); @@ -882,10 +882,10 @@ static struct ref_array_item *new_ref_array_item(const char *refname, } /* - * A call-back given to for_each_ref(). Filter refs and keep them for + * 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) +int ref_filter_handler(const char *refname, const unsigned char *sha1, int flag, void *cb_data) { struct ref_filter_cbdata *ref_cbdata = cb_data; struct ref_filter *filter = &ref_cbdata->filter; @@ -925,8 +925,8 @@ static int cmp_ref_sort(struct ref_sort *s, struct ref_array_item *a, struct ref int cmp; cmp_type cmp_type = used_atom_type[s->atom]; - get_value(a, s->atom, &va); - get_value(b, s->atom, &vb); + get_ref_atom_value(a, s->atom, &va); + get_ref_atom_value(b, s->atom, &vb); switch (cmp_type) { case FIELD_STR: cmp = strcmp(va->s, vb->s); @@ -958,7 +958,7 @@ static int compare_refs(const void *a_, const void *b_) return 0; } -static void sort_refs(struct ref_sort *sort, struct ref_array *array) +void sort_ref_array(struct ref_sort *sort, struct ref_array *array) { ref_sort = sort; qsort(array->items, array->nr, sizeof(struct ref_array_item *), compare_refs); @@ -1028,7 +1028,7 @@ static void emit(const char *cp, const char *ep) } } -static void show_ref(struct ref_array_item *info, const char *format, int quote_style) +void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style) { const char *cp, *sp, *ep; @@ -1038,7 +1038,7 @@ static void show_ref(struct ref_array_item *info, const char *format, int quote_ ep = strchr(sp, ')'); if (cp < sp) emit(cp, sp); - get_value(info, parse_atom(sp + 2, ep), &atomv); + get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv); print_value(atomv, quote_style); } if (*cp) { @@ -1057,18 +1057,19 @@ static void show_ref(struct ref_array_item *info, const char *format, int quote_ putchar('\n'); } -static struct ref_sort *default_sort(void) +/* If no sorting option is given, use refname to sort as default */ +struct ref_sort *ref_default_sort(void) { static const char cstr_name[] = "refname"; struct ref_sort *sort = xcalloc(1, sizeof(*sort)); sort->next = NULL; - sort->atom = parse_atom(cstr_name, cstr_name + strlen(cstr_name)); + sort->atom = parse_ref_filter_atom(cstr_name, cstr_name + strlen(cstr_name)); return sort; } -static int opt_parse_sort(const struct option *opt, const char *arg, int unset) +int opt_parse_ref_sort(const struct option *opt, const char *arg, int unset) { struct ref_sort **sort_tail = opt->value; struct ref_sort *s; @@ -1086,7 +1087,7 @@ static int opt_parse_sort(const struct option *opt, const char *arg, int unset) arg++; } len = strlen(arg); - s->atom = parse_atom(arg, arg+len); + s->atom = parse_ref_filter_atom(arg, arg+len); return 0; } @@ -1118,7 +1119,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix) OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")), OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")), OPT_CALLBACK(0 , "sort", sort_tail, N_("key"), - N_("field name to sort on"), &opt_parse_sort), + N_("field name to sort on"), &opt_parse_ref_sort), OPT_END(), }; @@ -1131,24 +1132,24 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix) error("more than one quoting style?"); usage_with_options(for_each_ref_usage, opts); } - if (verify_format(format)) + if (verify_ref_format(format)) usage_with_options(for_each_ref_usage, opts); if (!sort) - sort = default_sort(); + sort = ref_default_sort(); /* for warn_ambiguous_refs */ git_config(git_default_config, NULL); ref_cbdata.filter.name_patterns = argv; - for_each_rawref(grab_single_ref, &ref_cbdata); + for_each_rawref(ref_filter_handler, &ref_cbdata); - sort_refs(sort, &ref_cbdata.array); + sort_ref_array(sort, &ref_cbdata.array); if (!maxcount || ref_cbdata.array.nr < maxcount) maxcount = ref_cbdata.array.nr; for (i = 0; i < maxcount; i++) - show_ref(ref_cbdata.array.items[i], format, quote_style); + show_ref_array_item(ref_cbdata.array.items[i], format, quote_style); ref_filter_clear_data(&ref_cbdata); return 0; } -- 2.4.2 ^ permalink raw reply related [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 6/8] for-each-ref: rename some functions and make them public 2015-05-30 17:53 ` [WIP/PATCH v4 6/8] for-each-ref: rename some functions and make them public Karthik Nayak @ 2015-05-31 3:21 ` Eric Sunshine 2015-05-31 8:16 ` Karthik Nayak 2015-05-31 8:04 ` Christian Couder 2015-05-31 17:48 ` Junio C Hamano 2 siblings, 1 reply; 44+ messages in thread From: Eric Sunshine @ 2015-05-31 3:21 UTC (permalink / raw) To: Karthik Nayak; +Cc: Git List, Christian Couder, Matthieu Moy On Sat, May 30, 2015 at 1:53 PM, Karthik Nayak <karthik.188@gmail.com> wrote: > Rename some of the functions and make them publically available. s/publically/publicly/ > This is a preparatory step for moving code from 'for-each-ref' > to 'ref-filter' to make meaningful, targeted services available to > other commands via public APIs. > > Based-on-patch-by: Jeff King <peff@peff.net> > 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> > --- > diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c > index f896e1c..8fed04b 100644 > --- a/builtin/for-each-ref.c > +++ b/builtin/for-each-ref.c > @@ -882,10 +882,10 @@ static struct ref_array_item *new_ref_array_item(const char *refname, > } > > /* > - * A call-back given to for_each_ref(). Filter refs and keep them for > + * A call-back given to for_each_ref(). Filter refs and keep them for Sneaking in whitespace change? > * later object processing. > */ > -static int grab_single_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) > +int ref_filter_handler(const char *refname, const unsigned char *sha1, int flag, void *cb_data) > { > struct ref_filter_cbdata *ref_cbdata = cb_data; > struct ref_filter *filter = &ref_cbdata->filter; ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 6/8] for-each-ref: rename some functions and make them public 2015-05-31 3:21 ` Eric Sunshine @ 2015-05-31 8:16 ` Karthik Nayak 0 siblings, 0 replies; 44+ messages in thread From: Karthik Nayak @ 2015-05-31 8:16 UTC (permalink / raw) To: Eric Sunshine; +Cc: Git List, Christian Couder, Matthieu Moy On 05/31/2015 08:51 AM, Eric Sunshine wrote: > On Sat, May 30, 2015 at 1:53 PM, Karthik Nayak <karthik.188@gmail.com> wrote: >> Rename some of the functions and make them publically available. > > s/publically/publicly/ > >> This is a preparatory step for moving code from 'for-each-ref' >> to 'ref-filter' to make meaningful, targeted services available to >> other commands via public APIs. >> >> Based-on-patch-by: Jeff King <peff@peff.net> >> 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> >> --- >> diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c >> index f896e1c..8fed04b 100644 >> --- a/builtin/for-each-ref.c >> +++ b/builtin/for-each-ref.c >> @@ -882,10 +882,10 @@ static struct ref_array_item *new_ref_array_item(const char *refname, >> } >> >> /* >> - * A call-back given to for_each_ref(). Filter refs and keep them for >> + * A call-back given to for_each_ref(). Filter refs and keep them for > > Sneaking in whitespace change? > >> * later object processing. >> */ >> -static int grab_single_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) >> +int ref_filter_handler(const char *refname, const unsigned char *sha1, int flag, void *cb_data) >> { >> struct ref_filter_cbdata *ref_cbdata = cb_data; >> struct ref_filter *filter = &ref_cbdata->filter; Noted. Will fix! -- Regards, Karthik ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 6/8] for-each-ref: rename some functions and make them public 2015-05-30 17:53 ` [WIP/PATCH v4 6/8] for-each-ref: rename some functions and make them public Karthik Nayak 2015-05-31 3:21 ` Eric Sunshine @ 2015-05-31 8:04 ` Christian Couder 2015-05-31 8:11 ` Christian Couder 2015-05-31 17:54 ` Junio C Hamano 2015-05-31 17:48 ` Junio C Hamano 2 siblings, 2 replies; 44+ messages in thread From: Christian Couder @ 2015-05-31 8:04 UTC (permalink / raw) To: Karthik Nayak; +Cc: git, Matthieu Moy On Sat, May 30, 2015 at 7:53 PM, Karthik Nayak <karthik.188@gmail.com> wrote: > > -static void sort_refs(struct ref_sort *sort, struct ref_array *array) > +void sort_ref_array(struct ref_sort *sort, struct ref_array *array) It is probably better to call the above function ref_array_sort()... [...] > -static struct ref_sort *default_sort(void) > +/* If no sorting option is given, use refname to sort as default */ > +struct ref_sort *ref_default_sort(void) ... especially since you call the above ref_default_sort()... > -static int opt_parse_sort(const struct option *opt, const char *arg, int unset) > +int opt_parse_ref_sort(const struct option *opt, const char *arg, int unset) ... and the above opt_parse_sort(). ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 6/8] for-each-ref: rename some functions and make them public 2015-05-31 8:04 ` Christian Couder @ 2015-05-31 8:11 ` Christian Couder 2015-05-31 9:17 ` Karthik Nayak 2015-05-31 17:54 ` Junio C Hamano 1 sibling, 1 reply; 44+ messages in thread From: Christian Couder @ 2015-05-31 8:11 UTC (permalink / raw) To: Karthik Nayak; +Cc: git, Matthieu Moy On Sun, May 31, 2015 at 10:04 AM, Christian Couder <christian.couder@gmail.com> wrote: > On Sat, May 30, 2015 at 7:53 PM, Karthik Nayak <karthik.188@gmail.com> wrote: >> >> -static void sort_refs(struct ref_sort *sort, struct ref_array *array) >> +void sort_ref_array(struct ref_sort *sort, struct ref_array *array) > > It is probably better to call the above function ref_array_sort()... > > [...] > >> -static struct ref_sort *default_sort(void) >> +/* If no sorting option is given, use refname to sort as default */ >> +struct ref_sort *ref_default_sort(void) > > ... especially since you call the above ref_default_sort()... > >> -static int opt_parse_sort(const struct option *opt, const char *arg, int unset) >> +int opt_parse_ref_sort(const struct option *opt, const char *arg, int unset) > > ... and the above opt_parse_sort(). After saying that I realize that these two other functions are not doing the same thing. This might suggest that they are not named very well as well. ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 6/8] for-each-ref: rename some functions and make them public 2015-05-31 8:11 ` Christian Couder @ 2015-05-31 9:17 ` Karthik Nayak 2015-05-31 14:03 ` Christian Couder 0 siblings, 1 reply; 44+ messages in thread From: Karthik Nayak @ 2015-05-31 9:17 UTC (permalink / raw) To: Christian Couder; +Cc: git, Matthieu Moy On 05/31/2015 01:41 PM, Christian Couder wrote: > On Sun, May 31, 2015 at 10:04 AM, Christian Couder > <christian.couder@gmail.com> wrote: >> On Sat, May 30, 2015 at 7:53 PM, Karthik Nayak <karthik.188@gmail.com> wrote: >>> >>> -static void sort_refs(struct ref_sort *sort, struct ref_array *array) >>> +void sort_ref_array(struct ref_sort *sort, struct ref_array *array) >> >> It is probably better to call the above function ref_array_sort()... >> >> [...] >> >>> -static struct ref_sort *default_sort(void) >>> +/* If no sorting option is given, use refname to sort as default */ >>> +struct ref_sort *ref_default_sort(void) >> >> ... especially since you call the above ref_default_sort()... >> >>> -static int opt_parse_sort(const struct option *opt, const char *arg, int unset) >>> +int opt_parse_ref_sort(const struct option *opt, const char *arg, int unset) >> >> ... and the above opt_parse_sort(). > > After saying that I realize that these two other functions are not > doing the same thing. > This might suggest that they are not named very well as well. > What do you mean by "not doing the same thing." -- Regards, Karthik ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 6/8] for-each-ref: rename some functions and make them public 2015-05-31 9:17 ` Karthik Nayak @ 2015-05-31 14:03 ` Christian Couder 2015-05-31 15:30 ` Karthik Nayak 2015-06-01 6:38 ` Matthieu Moy 0 siblings, 2 replies; 44+ messages in thread From: Christian Couder @ 2015-05-31 14:03 UTC (permalink / raw) To: Karthik Nayak; +Cc: git, Matthieu Moy On Sun, May 31, 2015 at 11:17 AM, Karthik Nayak <karthik.188@gmail.com> wrote: > On 05/31/2015 01:41 PM, Christian Couder wrote: >> >> On Sun, May 31, 2015 at 10:04 AM, Christian Couder >> <christian.couder@gmail.com> wrote: >>> >>> On Sat, May 30, 2015 at 7:53 PM, Karthik Nayak <karthik.188@gmail.com> >>> wrote: >>>> >>>> >>>> -static void sort_refs(struct ref_sort *sort, struct ref_array *array) >>>> +void sort_ref_array(struct ref_sort *sort, struct ref_array *array) >>> >>> >>> It is probably better to call the above function ref_array_sort()... >>> >>> [...] >>> >>>> -static struct ref_sort *default_sort(void) >>>> +/* If no sorting option is given, use refname to sort as default */ >>>> +struct ref_sort *ref_default_sort(void) >>> >>> >>> ... especially since you call the above ref_default_sort()... >>> >>>> -static int opt_parse_sort(const struct option *opt, const char *arg, >>>> int unset) >>>> +int opt_parse_ref_sort(const struct option *opt, const char *arg, int >>>> unset) >>> >>> >>> ... and the above opt_parse_sort(). >> >> >> After saying that I realize that these two other functions are not >> doing the same thing. >> This might suggest that they are not named very well as well. >> > > What do you mean by "not doing the same thing." sort_ref_array() is actually sorting a ref_array, while ref_default_sort() and opt_parse_ref_sort() are not sorting anything. Maybe it would all be clearer with a renaming like the following: sort_refs() -> ref_array_sort() struct ref_sort -> struct ref_sort_criteria default_sort() -> ref_default_sort_criteria() opt_parse_sort() -> opt_parse_ref_sort_criteria() ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 6/8] for-each-ref: rename some functions and make them public 2015-05-31 14:03 ` Christian Couder @ 2015-05-31 15:30 ` Karthik Nayak 2015-06-01 6:38 ` Matthieu Moy 1 sibling, 0 replies; 44+ messages in thread From: Karthik Nayak @ 2015-05-31 15:30 UTC (permalink / raw) To: Christian Couder; +Cc: git, Matthieu Moy On 05/31/2015 07:33 PM, Christian Couder wrote: > On Sun, May 31, 2015 at 11:17 AM, Karthik Nayak <karthik.188@gmail.com> wrote: > > On 05/31/2015 01:41 PM, Christian Couder wrote: > >> > >> On Sun, May 31, 2015 at 10:04 AM, Christian Couder > >> <christian.couder@gmail.com> wrote: > >>> > >>> On Sat, May 30, 2015 at 7:53 PM, Karthik Nayak <karthik.188@gmail.com> > >>> wrote: > >>>> > >>>> > >>>> -static void sort_refs(struct ref_sort *sort, struct ref_array *array) > >>>> +void sort_ref_array(struct ref_sort *sort, struct ref_array *array) > >>> > >>> > >>> It is probably better to call the above function ref_array_sort()... > >>> > >>> [...] > >>> > >>>> -static struct ref_sort *default_sort(void) > >>>> +/* If no sorting option is given, use refname to sort as default */ > >>>> +struct ref_sort *ref_default_sort(void) > >>> > >>> > >>> ... especially since you call the above ref_default_sort()... > >>> > >>>> -static int opt_parse_sort(const struct option *opt, const char *arg, > >>>> int unset) > >>>> +int opt_parse_ref_sort(const struct option *opt, const char *arg, int > >>>> unset) > >>> > >>> > >>> ... and the above opt_parse_sort(). > >> > >> > >> After saying that I realize that these two other functions are not > >> doing the same thing. > >> This might suggest that they are not named very well as well. > >> > > > > What do you mean by "not doing the same thing." > > sort_ref_array() is actually sorting a ref_array, while > ref_default_sort() and opt_parse_ref_sort() are not sorting anything. > > Maybe it would all be clearer with a renaming like the following: > > sort_refs() -> ref_array_sort() > struct ref_sort -> struct ref_sort_criteria > default_sort() -> ref_default_sort_criteria() > opt_parse_sort() -> opt_parse_ref_sort_criteria() > Thanks will follow this, but will change opt_parse_ref_sort_criteria() to parse_opt_ref_sort_criteria() as this is what is commonly followed. -- Regards, Karthik ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 6/8] for-each-ref: rename some functions and make them public 2015-05-31 14:03 ` Christian Couder 2015-05-31 15:30 ` Karthik Nayak @ 2015-06-01 6:38 ` Matthieu Moy 2015-06-01 19:28 ` Karthik Nayak 1 sibling, 1 reply; 44+ messages in thread From: Matthieu Moy @ 2015-06-01 6:38 UTC (permalink / raw) To: Christian Couder; +Cc: Karthik Nayak, git Christian Couder <christian.couder@gmail.com> writes: > sort_refs() -> ref_array_sort() > struct ref_sort -> struct ref_sort_criteria > default_sort() -> ref_default_sort_criteria() > opt_parse_sort() -> opt_parse_ref_sort_criteria() BTW, having such summary in the log message would help review: we could think and discuss the naming by looking just at the summary, and then check that you did it correctly (easy). -- Matthieu Moy http://www-verimag.imag.fr/~moy/ ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 6/8] for-each-ref: rename some functions and make them public 2015-06-01 6:38 ` Matthieu Moy @ 2015-06-01 19:28 ` Karthik Nayak 0 siblings, 0 replies; 44+ messages in thread From: Karthik Nayak @ 2015-06-01 19:28 UTC (permalink / raw) To: Matthieu Moy, Christian Couder; +Cc: git On June 1, 2015 12:08:57 PM GMT+05:30, Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> wrote: >Christian Couder <christian.couder@gmail.com> writes: > >> sort_refs() -> ref_array_sort() >> struct ref_sort -> struct ref_sort_criteria >> default_sort() -> ref_default_sort_criteria() >> opt_parse_sort() -> opt_parse_ref_sort_criteria() > >BTW, having such summary in the log message would help review: we could >think and discuss the naming by looking just at the summary, and then >check that you did it correctly (easy). Yes agreed, will do that. ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 6/8] for-each-ref: rename some functions and make them public 2015-05-31 8:04 ` Christian Couder 2015-05-31 8:11 ` Christian Couder @ 2015-05-31 17:54 ` Junio C Hamano 1 sibling, 0 replies; 44+ messages in thread From: Junio C Hamano @ 2015-05-31 17:54 UTC (permalink / raw) To: Christian Couder; +Cc: Karthik Nayak, git, Matthieu Moy Christian Couder <christian.couder@gmail.com> writes: > On Sat, May 30, 2015 at 7:53 PM, Karthik Nayak <karthik.188@gmail.com> wrote: >> >> -static void sort_refs(struct ref_sort *sort, struct ref_array *array) >> +void sort_ref_array(struct ref_sort *sort, struct ref_array *array) > > It is probably better to call the above function ref_array_sort()... Care to explain the reasoning behind that suggestion? The function "sorts" "ref_array", so verb-object sounds like a valid construction of a name. All the functions externalized by the patch under discussion follows that pattern, I think (e.g. parse-ref-filter-atom, verify-ref-format, show-ref-array-item). Making the API function share a common prefix is another valid school of design; while that may justify ref-array-sort(), but if you are going in that direction, all the others need to be renamed along that line consistently, I would think. ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 6/8] for-each-ref: rename some functions and make them public 2015-05-30 17:53 ` [WIP/PATCH v4 6/8] for-each-ref: rename some functions and make them public Karthik Nayak 2015-05-31 3:21 ` Eric Sunshine 2015-05-31 8:04 ` Christian Couder @ 2015-05-31 17:48 ` Junio C Hamano 2015-05-31 19:34 ` Karthik Nayak 2 siblings, 1 reply; 44+ messages in thread From: Junio C Hamano @ 2015-05-31 17:48 UTC (permalink / raw) To: Karthik Nayak; +Cc: git, christian.couder, Matthieu.Moy Karthik Nayak <karthik.188@gmail.com> writes: > /* > - * A call-back given to for_each_ref(). Filter refs and keep them for > + * 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) > +int ref_filter_handler(const char *refname, const unsigned char *sha1, int flag, void *cb_data) > { I am not sure if this is a good external interface, i.e. to expect callers of ref-filter API to do this: prepare cbdata; for_each_ref(ref_filter_handler, &cbdata); It might be better to expect callers to do this instead: prepare cbdata; filter_refs(for_each_ref, &cbdata); i.e. introducing a new "filter_refs()" function as the entry point to the ref-filter API. The filter_refs() entry point may internally use ref_filter_handler() that will be file-scope static to ref-filter.c and at that point the overly generic "-handler" name would not bother anybody ;-) but more importantly, then you can extend the function signature of filter_refs() not to be so tied to for_each_ref() API. It could be that the internals of cbdata may not be something the callers of filter-refs API does not even have to know about, in which case the call might even become something like: struct ref_array refs = REF_ARRAY_INIT; const char **ref_patterns = { "refs/heads/*", "refs/tags/*", NULL}; filter_refs(&refs, for_each_rawref, ref_patterns); /* now "refs" has the result, the caller uses them */ for (i = 0; i < refs.nr; i++) refs.item[i]; Just a thought. ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 6/8] for-each-ref: rename some functions and make them public 2015-05-31 17:48 ` Junio C Hamano @ 2015-05-31 19:34 ` Karthik Nayak 2015-06-01 14:53 ` Junio C Hamano 0 siblings, 1 reply; 44+ messages in thread From: Karthik Nayak @ 2015-05-31 19:34 UTC (permalink / raw) To: Junio C Hamano; +Cc: git, christian.couder, Matthieu.Moy On 05/31/2015 11:18 PM, Junio C Hamano wrote: > Karthik Nayak <karthik.188@gmail.com> writes: > >> /* >> - * A call-back given to for_each_ref(). Filter refs and keep them for >> + * 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) >> +int ref_filter_handler(const char *refname, const unsigned char *sha1, int flag, void *cb_data) >> { > > I am not sure if this is a good external interface, i.e. to expect > callers of ref-filter API to do this: > > prepare cbdata; > for_each_ref(ref_filter_handler, &cbdata); > > It might be better to expect callers to do this instead: > > prepare cbdata; > filter_refs(for_each_ref, &cbdata); > > i.e. introducing a new "filter_refs()" function as the entry point > to the ref-filter API. The filter_refs() entry point may internally > use ref_filter_handler() that will be file-scope static to ref-filter.c > and at that point the overly generic "-handler" name would not bother > anybody ;-) but more importantly, then you can extend the function > signature of filter_refs() not to be so tied to for_each_ref() API. > It could be that the internals of cbdata may not be something the > callers of filter-refs API does not even have to know about, in > which case the call might even become something like: > > struct ref_array refs = REF_ARRAY_INIT; > const char **ref_patterns = { "refs/heads/*", "refs/tags/*", NULL}; > > filter_refs(&refs, for_each_rawref, ref_patterns); > > /* now "refs" has the result, the caller uses them */ > for (i = 0; i < refs.nr; i++) > refs.item[i]; > > Just a thought. > Thats brilliant, How about I introduce something of this sort int filter_refs(int (*for_each_ref_fn)(each_ref_fn, void *), ref_filter_cbdata *cbdata) { return for_each_ref_fn(ref_filter_handler, cbdata); } where its the most basic form, and things like > > struct ref_array refs = REF_ARRAY_INIT; > const char **ref_patterns = { "refs/heads/*", "refs/tags/*", NULL}; > > filter_refs(&refs, for_each_rawref, ref_patterns); > > /* now "refs" has the result, the caller uses them */ > for (i = 0; i < refs.nr; i++) > refs.item[i]; > Could be achieved using a simple wrapper around 'filter_refs()' something like this perhaps. int filter_refs_with_pattern(struct ref_array *ref, int (*for_each_ref_fn)(each_ref_fn, void *), char **patterns) { int i; struct ref_filter_cbdata data; data.filter.name_patterns = patterns; filter_refs(for_each_ref_fn, &data); refs->nr = data.array.nr; for(i = 0; i < refs->nr; i++) { /* copy over the refs */ } return 0; } Is this on the lines of what you had in mind? If it is, than I could just create a new patch which would make ref_filter_handler() private and introduce filter_refs() as shown. -- Regards, Karthik ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 6/8] for-each-ref: rename some functions and make them public 2015-05-31 19:34 ` Karthik Nayak @ 2015-06-01 14:53 ` Junio C Hamano 2015-06-01 19:35 ` Karthik Nayak 0 siblings, 1 reply; 44+ messages in thread From: Junio C Hamano @ 2015-06-01 14:53 UTC (permalink / raw) To: Karthik Nayak; +Cc: git, christian.couder, Matthieu.Moy Karthik Nayak <karthik.188@gmail.com> writes: > Could be achieved using a simple wrapper around 'filter_refs()' > something like this perhaps. > > int filter_refs_with_pattern(struct ref_array *ref, int > (*for_each_ref_fn)(each_ref_fn, void *), char **patterns) > { > int i; > struct ref_filter_cbdata data; > data.filter.name_patterns = patterns; > filter_refs(for_each_ref_fn, &data); I presume that this is filter_refs(&refs, for_each_ref_fn, &data); as you would need to have some way to get the result back ;-) > refs->nr = data.array.nr; > for(i = 0; i < refs->nr; i++) { > /* copy over the refs */ > } > return 0; > } > > Is this on the lines of what you had in mind? If it is, than I could > just create a new patch which would make ref_filter_handler() private > and introduce filter_refs() as shown. Yeah. Even though I suggested filter_refs(&for_each_ref, ...); I actually would think the external interface should not mention for_each_ref() like I did. The primary reason why I felt that it is bad for the API to export a generic callback function the caller can use to call for_each_ref() or for_each_rawref()" in the longer term is because it forces us to always iterate all refs; for_each_ref() does not know what the callback filter function wants to do. The most common way to filter in the context of your GSoC project is "we limit only to refs/heads/*, and then we may also filter by other criteria" (that is "git branch" "-l" or possibly with "--contains", etc.), and it is very wasteful for that codepath to allow for_each_ref() to even enumerate and feed all refs outside the refs/heads/* area to your callback, which would involve reading all entries in packed-refs (which is a fixed cost so not an overhead) and then reading everything in .git/refs/* (which is an overhead we could and should avoid when we know we are only interested in the branches that live in refs/heads*). Your first implementation of course can just call for_each_ref() or for_each_rawref(), and at the end of GSoC, the code may still do so. But by keeping the external interface free of for_each_ref(), you could later optimize. And your above sample only takes for_each_ref_fn without exposing the internal use of for_each_ref(), which is good. ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 6/8] for-each-ref: rename some functions and make them public 2015-06-01 14:53 ` Junio C Hamano @ 2015-06-01 19:35 ` Karthik Nayak 0 siblings, 0 replies; 44+ messages in thread From: Karthik Nayak @ 2015-06-01 19:35 UTC (permalink / raw) To: Junio C Hamano; +Cc: git, christian.couder, Matthieu.Moy On June 1, 2015 8:23:51 PM GMT+05:30, Junio C Hamano <gitster@pobox.com> wrote: >Karthik Nayak <karthik.188@gmail.com> writes: > >> Could be achieved using a simple wrapper around 'filter_refs()' >> something like this perhaps. >> >> int filter_refs_with_pattern(struct ref_array *ref, int >> (*for_each_ref_fn)(each_ref_fn, void *), char **patterns) >> { >> int i; >> struct ref_filter_cbdata data; >> data.filter.name_patterns = patterns; >> filter_refs(for_each_ref_fn, &data); > >I presume that this is > > filter_refs(&refs, for_each_ref_fn, &data); > >as you would need to have some way to get the result back ;-) > No it's meant to be that way, see as it takes a struct ref_filter_cbdata, the filters are put in the data.filter.* and we obtain the refs in the data.array.items. So no need for another ref_array to hold the array of items. >> refs->nr = data.array.nr; >> for(i = 0; i < refs->nr; i++) { >> /* copy over the refs */ >> } >> return 0; >> } >> >> Is this on the lines of what you had in mind? If it is, than I could >> just create a new patch which would make ref_filter_handler() private >> and introduce filter_refs() as shown. > >Yeah. Even though I suggested > > filter_refs(&for_each_ref, ...); > >I actually would think the external interface should not mention >for_each_ref() like I did. The primary reason why I felt that it is >bad for the API to export a generic callback function the caller can >use to call for_each_ref() or for_each_rawref()" in the longer term >is because it forces us to always iterate all refs; for_each_ref() >does not know what the callback filter function wants to do. The >most common way to filter in the context of your GSoC project is "we >limit only to refs/heads/*, and then we may also filter by other >criteria" (that is "git branch" "-l" or possibly with "--contains", >etc.), and it is very wasteful for that codepath to allow >for_each_ref() to even enumerate and feed all refs outside the >refs/heads/* area to your callback, which would involve reading all >entries in packed-refs (which is a fixed cost so not an overhead) >and then reading everything in .git/refs/* (which is an overhead we >could and should avoid when we know we are only interested in the >branches that live in refs/heads*). > >Your first implementation of course can just call for_each_ref() or >for_each_rawref(), and at the end of GSoC, the code may still do so. >But by keeping the external interface free of for_each_ref(), you >could later optimize. > >And your above sample only takes for_each_ref_fn without exposing the >internal use of for_each_ref(), which is good. That seems good, thanks for the guidance. ^ permalink raw reply [flat|nested] 44+ messages in thread
* [WIP/PATCH v4 7/8] ref-filter: move code from 'for-each-ref' [not found] <5569EF77.4010300@gmail.com> ` (5 preceding siblings ...) 2015-05-30 17:53 ` [WIP/PATCH v4 6/8] for-each-ref: rename some functions and make them public Karthik Nayak @ 2015-05-30 17:53 ` Karthik Nayak 2015-05-30 17:53 ` [WIP/PATCH v4 8/8] ref-filter: add 'ref-filter.h' Karthik Nayak 7 siblings, 0 replies; 44+ messages in thread From: Karthik Nayak @ 2015-05-30 17:53 UTC (permalink / raw) To: git; +Cc: christian.couder, Matthieu.Moy, Karthik Nayak Move most of the code from 'for-each-ref' to 'ref-filter' to make it publicly 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.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 | 1048 ----------------------------------------------- ref-filter.c | 1050 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1050 insertions(+), 1048 deletions(-) create mode 100644 ref-filter.c diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c index 8fed04b..65ed954 100644 --- a/builtin/for-each-ref.c +++ b/builtin/for-each-ref.c @@ -1,15 +1,7 @@ #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 @@ -18,8 +10,6 @@ #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 */ @@ -53,1044 +43,6 @@ struct ref_filter_cbdata { 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 || ep < formatp) - formatp = ep; - if (len == formatp - sp && !memcmp(valid_atom[i].name, sp, len)) - break; - } - - if (ARRAY_SIZE(valid_atom) <= i) - die("unknown field name: %.*s", (int)(ep-atom), atom); - - /* Add it in, including the deref prefix */ - at = used_atom_cnt; - used_atom_cnt++; - REALLOC_ARRAY(used_atom, used_atom_cnt); - REALLOC_ARRAY(used_atom_type, used_atom_cnt); - used_atom[at] = xmemdupz(atom, ep - atom); - used_atom_type[at] = valid_atom[i].cmp_type; - if (*atom == '*') - need_tagged = 1; - if (!strcmp(used_atom[at], "symref")) - need_symref = 1; - return at; -} - -/* - * In a format string, find the next occurrence of %(atom). - */ -static const char *find_next(const char *cp) -{ - while (*cp) { - if (*cp == '%') { - /* - * %( is the start of an atom; - * %% is a quoted per-cent. - */ - if (cp[1] == '(') - return cp; - else if (cp[1] == '%') - cp++; /* skip over two % */ - /* otherwise this is a singleton, literal % */ - } - cp++; - } - return NULL; -} - -/* - * Make sure the format string is well formed, and parse out - * the used atoms. - */ -int verify_ref_format(const char *format) -{ - const char *cp, *sp; - - need_color_reset_at_eol = 0; - for (cp = format; *cp && (sp = find_next(cp)); ) { - const char *color, *ep = strchr(sp, ')'); - int at; - - if (!ep) - return error("malformed format string %s", sp); - /* sp points at "%(" and ep points at the closing ")" */ - at = parse_ref_filter_atom(sp + 2, ep); - cp = ep + 1; - - if (skip_prefix(used_atom[at], "color:", &color)) - need_color_reset_at_eol = !!strcmp(color, "reset"); - } - return 0; -} - -/* - * Given an object name, read the object data and size, and return a - * "struct object". If the object data we are returning is also borrowed - * by the "struct object" representation, set *eaten as well---it is a - * signal from parse_object_buffer to us not to free the buffer. - */ -static void *get_obj(const unsigned char *sha1, struct object **obj, unsigned long *sz, int *eaten) -{ - enum object_type type; - void *buf = read_sha1_file(sha1, &type, sz); - - if (buf) - *obj = parse_object_buffer(sha1, type, *sz, buf, eaten); - else - *obj = NULL; - return buf; -} - -static int grab_objectname(const char *name, const unsigned char *sha1, - struct atom_value *v) -{ - if (!strcmp(name, "objectname")) { - char *s = xmalloc(41); - strcpy(s, sha1_to_hex(sha1)); - v->s = s; - return 1; - } - if (!strcmp(name, "objectname:short")) { - v->s = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV)); - return 1; - } - return 0; -} - -/* See grab_values */ -static void grab_common_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) -{ - int i; - - for (i = 0; i < used_atom_cnt; i++) { - const char *name = used_atom[i]; - struct atom_value *v = &val[i]; - if (!!deref != (*name == '*')) - continue; - if (deref) - name++; - if (!strcmp(name, "objecttype")) - v->s = typename(obj->type); - else if (!strcmp(name, "objectsize")) { - char *s = xmalloc(40); - sprintf(s, "%lu", sz); - v->ul = sz; - v->s = s; - } - else if (deref) - grab_objectname(name, obj->sha1, v); - } -} - -/* See grab_values */ -static void grab_tag_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) -{ - int i; - struct tag *tag = (struct tag *) obj; - - for (i = 0; i < used_atom_cnt; i++) { - const char *name = used_atom[i]; - struct atom_value *v = &val[i]; - if (!!deref != (*name == '*')) - continue; - if (deref) - name++; - if (!strcmp(name, "tag")) - v->s = tag->tag; - else if (!strcmp(name, "type") && tag->tagged) - v->s = typename(tag->tagged->type); - else if (!strcmp(name, "object") && tag->tagged) { - char *s = xmalloc(41); - strcpy(s, sha1_to_hex(tag->tagged->sha1)); - v->s = s; - } - } -} - -/* See grab_values */ -static void grab_commit_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) -{ - int i; - struct commit *commit = (struct commit *) obj; - - for (i = 0; i < used_atom_cnt; i++) { - const char *name = used_atom[i]; - struct atom_value *v = &val[i]; - if (!!deref != (*name == '*')) - continue; - if (deref) - name++; - if (!strcmp(name, "tree")) { - char *s = xmalloc(41); - strcpy(s, sha1_to_hex(commit->tree->object.sha1)); - v->s = s; - } - if (!strcmp(name, "numparent")) { - char *s = xmalloc(40); - v->ul = commit_list_count(commit->parents); - sprintf(s, "%lu", v->ul); - v->s = s; - } - else if (!strcmp(name, "parent")) { - int num = commit_list_count(commit->parents); - int i; - struct commit_list *parents; - char *s = xmalloc(41 * num + 1); - v->s = s; - for (i = 0, parents = commit->parents; - parents; - parents = parents->next, i = i + 41) { - struct commit *parent = parents->item; - strcpy(s+i, sha1_to_hex(parent->object.sha1)); - if (parents->next) - s[i+40] = ' '; - } - if (!i) - *s = '\0'; - } - } -} - -static const char *find_wholine(const char *who, int wholen, const char *buf, unsigned long sz) -{ - const char *eol; - while (*buf) { - if (!strncmp(buf, who, wholen) && - buf[wholen] == ' ') - return buf + wholen + 1; - eol = strchr(buf, '\n'); - if (!eol) - return ""; - eol++; - if (*eol == '\n') - return ""; /* end of header */ - buf = eol; - } - return ""; -} - -static const char *copy_line(const char *buf) -{ - const char *eol = strchrnul(buf, '\n'); - return xmemdupz(buf, eol - buf); -} - -static const char *copy_name(const char *buf) -{ - const char *cp; - for (cp = buf; *cp && *cp != '\n'; cp++) { - if (!strncmp(cp, " <", 2)) - return xmemdupz(buf, cp - buf); - } - return ""; -} - -static const char *copy_email(const char *buf) -{ - const char *email = strchr(buf, '<'); - const char *eoemail; - if (!email) - return ""; - eoemail = strchr(email, '>'); - if (!eoemail) - return ""; - return xmemdupz(email, eoemail + 1 - email); -} - -static char *copy_subject(const char *buf, unsigned long len) -{ - char *r = xmemdupz(buf, len); - int i; - - for (i = 0; i < len; i++) - if (r[i] == '\n') - r[i] = ' '; - - return r; -} - -static void grab_date(const char *buf, struct atom_value *v, const char *atomname) -{ - const char *eoemail = strstr(buf, "> "); - char *zone; - unsigned long timestamp; - long tz; - enum date_mode date_mode = DATE_NORMAL; - const char *formatp; - - /* - * We got here because atomname ends in "date" or "date<something>"; - * it's not possible that <something> is not ":<format>" because - * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no - * ":" means no format is specified, and use the default. - */ - formatp = strchr(atomname, ':'); - if (formatp != NULL) { - formatp++; - date_mode = parse_date_format(formatp); - } - - if (!eoemail) - goto bad; - timestamp = strtoul(eoemail + 2, &zone, 10); - if (timestamp == ULONG_MAX) - goto bad; - tz = strtol(zone, NULL, 10); - if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE) - goto bad; - v->s = xstrdup(show_date(timestamp, tz, date_mode)); - v->ul = timestamp; - return; - bad: - v->s = ""; - v->ul = 0; -} - -/* See grab_values */ -static void grab_person(const char *who, struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) -{ - int i; - int wholen = strlen(who); - const char *wholine = NULL; - - for (i = 0; i < used_atom_cnt; i++) { - const char *name = used_atom[i]; - struct atom_value *v = &val[i]; - if (!!deref != (*name == '*')) - continue; - if (deref) - name++; - if (strncmp(who, name, wholen)) - continue; - if (name[wholen] != 0 && - strcmp(name + wholen, "name") && - strcmp(name + wholen, "email") && - !starts_with(name + wholen, "date")) - continue; - if (!wholine) - wholine = find_wholine(who, wholen, buf, sz); - if (!wholine) - return; /* no point looking for it */ - if (name[wholen] == 0) - v->s = copy_line(wholine); - else if (!strcmp(name + wholen, "name")) - v->s = copy_name(wholine); - else if (!strcmp(name + wholen, "email")) - v->s = copy_email(wholine); - else if (starts_with(name + wholen, "date")) - grab_date(wholine, v, name); - } - - /* - * For a tag or a commit object, if "creator" or "creatordate" is - * requested, do something special. - */ - if (strcmp(who, "tagger") && strcmp(who, "committer")) - return; /* "author" for commit object is not wanted */ - if (!wholine) - wholine = find_wholine(who, wholen, buf, sz); - if (!wholine) - return; - for (i = 0; i < used_atom_cnt; i++) { - const char *name = used_atom[i]; - struct atom_value *v = &val[i]; - if (!!deref != (*name == '*')) - continue; - if (deref) - name++; - - if (starts_with(name, "creatordate")) - grab_date(wholine, v, name); - else if (!strcmp(name, "creator")) - v->s = copy_line(wholine); - } -} - -static void find_subpos(const char *buf, unsigned long sz, - const char **sub, unsigned long *sublen, - const char **body, unsigned long *bodylen, - unsigned long *nonsiglen, - const char **sig, unsigned long *siglen) -{ - const char *eol; - /* skip past header until we hit empty line */ - while (*buf && *buf != '\n') { - eol = strchrnul(buf, '\n'); - if (*eol) - eol++; - buf = eol; - } - /* skip any empty lines */ - while (*buf == '\n') - buf++; - - /* parse signature first; we might not even have a subject line */ - *sig = buf + parse_signature(buf, strlen(buf)); - *siglen = strlen(*sig); - - /* subject is first non-empty line */ - *sub = buf; - /* subject goes to first empty line */ - while (buf < *sig && *buf && *buf != '\n') { - eol = strchrnul(buf, '\n'); - if (*eol) - eol++; - buf = eol; - } - *sublen = buf - *sub; - /* drop trailing newline, if present */ - if (*sublen && (*sub)[*sublen - 1] == '\n') - *sublen -= 1; - - /* skip any empty lines */ - while (*buf == '\n') - buf++; - *body = buf; - *bodylen = strlen(buf); - *nonsiglen = *sig - buf; -} - -/* See grab_values */ -static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) -{ - int i; - const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL; - unsigned long sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0; - - for (i = 0; i < used_atom_cnt; i++) { - const char *name = used_atom[i]; - struct atom_value *v = &val[i]; - if (!!deref != (*name == '*')) - continue; - if (deref) - name++; - if (strcmp(name, "subject") && - strcmp(name, "body") && - strcmp(name, "contents") && - strcmp(name, "contents:subject") && - strcmp(name, "contents:body") && - strcmp(name, "contents:signature")) - continue; - if (!subpos) - find_subpos(buf, sz, - &subpos, &sublen, - &bodypos, &bodylen, &nonsiglen, - &sigpos, &siglen); - - if (!strcmp(name, "subject")) - v->s = copy_subject(subpos, sublen); - else if (!strcmp(name, "contents:subject")) - v->s = copy_subject(subpos, sublen); - else if (!strcmp(name, "body")) - v->s = xmemdupz(bodypos, bodylen); - else if (!strcmp(name, "contents:body")) - v->s = xmemdupz(bodypos, nonsiglen); - else if (!strcmp(name, "contents:signature")) - v->s = xmemdupz(sigpos, siglen); - else if (!strcmp(name, "contents")) - v->s = xstrdup(subpos); - } -} - -/* - * We want to have empty print-string for field requests - * that do not apply (e.g. "authordate" for a tag object) - */ -static void fill_missing_values(struct atom_value *val) -{ - int i; - for (i = 0; i < used_atom_cnt; i++) { - struct atom_value *v = &val[i]; - if (v->s == NULL) - v->s = ""; - } -} - -/* - * val is a list of atom_value to hold returned values. Extract - * the values for atoms in used_atom array out of (obj, buf, sz). - * when deref is false, (obj, buf, sz) is the object that is - * pointed at by the ref itself; otherwise it is the object the - * ref (which is a tag) refers to. - */ -static void grab_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) -{ - grab_common_values(val, deref, obj, buf, sz); - switch (obj->type) { - case OBJ_TAG: - grab_tag_values(val, deref, obj, buf, sz); - grab_sub_body_contents(val, deref, obj, buf, sz); - grab_person("tagger", val, deref, obj, buf, sz); - break; - case OBJ_COMMIT: - grab_commit_values(val, deref, obj, buf, sz); - grab_sub_body_contents(val, deref, obj, buf, sz); - grab_person("author", val, deref, obj, buf, sz); - grab_person("committer", val, deref, obj, buf, sz); - break; - case OBJ_TREE: - /* grab_tree_values(val, deref, obj, buf, sz); */ - break; - case OBJ_BLOB: - /* grab_blob_values(val, deref, obj, buf, sz); */ - break; - default: - die("Eh? Object of type %d?", obj->type); - } -} - -static inline char *copy_advance(char *dst, const char *src) -{ - while (*src) - *dst++ = *src++; - return dst; -} - -/* - * Parse the object referred by ref, and grab needed value. - */ -static void populate_value(struct ref_array_item *ref) -{ - void *buf; - struct object *obj; - int eaten, i; - unsigned long size; - const unsigned char *tagged; - - ref->value = xcalloc(used_atom_cnt, sizeof(struct atom_value)); - - if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) { - unsigned char unused1[20]; - ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING, - unused1, NULL); - if (!ref->symref) - ref->symref = ""; - } - - /* Fill in specials first */ - for (i = 0; i < used_atom_cnt; i++) { - const char *name = used_atom[i]; - struct atom_value *v = &ref->value[i]; - int deref = 0; - const char *refname; - const char *formatp; - struct branch *branch = NULL; - - if (*name == '*') { - deref = 1; - name++; - } - - if (starts_with(name, "refname")) - refname = ref->refname; - else if (starts_with(name, "symref")) - refname = ref->symref ? ref->symref : ""; - else if (starts_with(name, "upstream")) { - /* only local branches may have an upstream */ - if (!starts_with(ref->refname, "refs/heads/")) - continue; - branch = branch_get(ref->refname + 11); - - if (!branch || !branch->merge || !branch->merge[0] || - !branch->merge[0]->dst) - continue; - refname = branch->merge[0]->dst; - } else if (starts_with(name, "color:")) { - char color[COLOR_MAXLEN] = ""; - - if (color_parse(name + 6, color) < 0) - die(_("unable to parse format")); - v->s = xstrdup(color); - continue; - } else if (!strcmp(name, "flag")) { - char buf[256], *cp = buf; - if (ref->flag & REF_ISSYMREF) - cp = copy_advance(cp, ",symref"); - if (ref->flag & REF_ISPACKED) - cp = copy_advance(cp, ",packed"); - if (cp == buf) - v->s = ""; - else { - *cp = '\0'; - v->s = xstrdup(buf + 1); - } - continue; - } else if (!deref && grab_objectname(name, ref->objectname, v)) { - continue; - } else if (!strcmp(name, "HEAD")) { - const char *head; - unsigned char sha1[20]; - - head = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, - sha1, NULL); - if (!strcmp(ref->refname, head)) - v->s = "*"; - else - v->s = " "; - continue; - } else - continue; - - formatp = strchr(name, ':'); - if (formatp) { - int num_ours, num_theirs; - - formatp++; - if (!strcmp(formatp, "short")) - refname = shorten_unambiguous_ref(refname, - warn_ambiguous_refs); - else if (!strcmp(formatp, "track") && - starts_with(name, "upstream")) { - char buf[40]; - - if (stat_tracking_info(branch, &num_ours, - &num_theirs) != 1) - continue; - - if (!num_ours && !num_theirs) - v->s = ""; - else if (!num_ours) { - sprintf(buf, "[behind %d]", num_theirs); - v->s = xstrdup(buf); - } else if (!num_theirs) { - sprintf(buf, "[ahead %d]", num_ours); - v->s = xstrdup(buf); - } else { - sprintf(buf, "[ahead %d, behind %d]", - num_ours, num_theirs); - v->s = xstrdup(buf); - } - continue; - } else if (!strcmp(formatp, "trackshort") && - starts_with(name, "upstream")) { - assert(branch); - - if (stat_tracking_info(branch, &num_ours, - &num_theirs) != 1) - continue; - - if (!num_ours && !num_theirs) - v->s = "="; - else if (!num_ours) - v->s = "<"; - else if (!num_theirs) - v->s = ">"; - else - v->s = "<>"; - continue; - } else - die("unknown %.*s format %s", - (int)(formatp - name), name, formatp); - } - - if (!deref) - v->s = refname; - else { - int len = strlen(refname); - char *s = xmalloc(len + 4); - sprintf(s, "%s^{}", refname); - v->s = s; - } - } - - for (i = 0; i < used_atom_cnt; i++) { - struct atom_value *v = &ref->value[i]; - if (v->s == NULL) - goto need_obj; - } - return; - - need_obj: - buf = get_obj(ref->objectname, &obj, &size, &eaten); - if (!buf) - die("missing object %s for %s", - sha1_to_hex(ref->objectname), ref->refname); - if (!obj) - die("parse_object_buffer failed on %s for %s", - sha1_to_hex(ref->objectname), ref->refname); - - grab_values(ref->value, 0, obj, buf, size); - if (!eaten) - free(buf); - - /* - * If there is no atom that wants to know about tagged - * object, we are done. - */ - if (!need_tagged || (obj->type != OBJ_TAG)) - return; - - /* - * If it is a tag object, see if we use a value that derefs - * the object, and if we do grab the object it refers to. - */ - tagged = ((struct tag *)obj)->tagged->sha1; - - /* - * NEEDSWORK: This derefs tag only once, which - * is good to deal with chains of trust, but - * is not consistent with what deref_tag() does - * which peels the onion to the core. - */ - buf = get_obj(tagged, &obj, &size, &eaten); - if (!buf) - die("missing object %s for %s", - sha1_to_hex(tagged), ref->refname); - if (!obj) - die("parse_object_buffer failed on %s for %s", - sha1_to_hex(tagged), ref->refname); - grab_values(ref->value, 1, obj, buf, size); - if (!eaten) - free(buf); -} - -/* - * Given a ref, return the value for the atom. This lazily gets value - * out of the object by calling populate value. - */ -static void get_ref_atom_value(struct ref_array_item *ref, int atom, struct atom_value **v) -{ - if (!ref->value) { - populate_value(ref); - fill_missing_values(ref->value); - } - *v = &ref->value[atom]; -} - -/* - * Given a refname, return 1 if the refname matches with one of the patterns - * while the pattern is a pathname like 'refs/tags' or 'refs/heads/master' - * and so on, else return 0. Supports use of wild characters. - */ -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; -} - -/* Allocate space for a new ref_array_item and copy the objectname and flag to it */ -static struct ref_array_item *new_ref_array_item(const char *refname, - const unsigned char *objectname, - int flag) -{ - struct ref_array_item *ref = xcalloc(1, sizeof(struct ref_array_item)); - ref->refname = xstrdup(refname); - hashcpy(ref->objectname, objectname); - ref->flag = flag; - - return ref; -} - -/* - * A call-back given to for_each_ref(). Filter refs and keep them for - * later object processing. - */ -int ref_filter_handler(const char *refname, const unsigned char *sha1, int flag, void *cb_data) -{ - struct ref_filter_cbdata *ref_cbdata = cb_data; - struct ref_filter *filter = &ref_cbdata->filter; - struct ref_array_item *ref; - - if (flag & REF_BAD_NAME) { - warning("ignoring ref with broken name %s", refname); - return 0; - } - - if (*filter->name_patterns && !match_name_as_path(filter->name_patterns, refname)) - return 0; - - ref = new_ref_array_item(refname, sha1, flag); - - REALLOC_ARRAY(ref_cbdata->array.items, ref_cbdata->array.nr + 1); - ref_cbdata->array.items[ref_cbdata->array.nr++] = ref; - - return 0; -} - -/* Free all memory allocated for ref_filter_cbdata */ -void ref_filter_clear_data(struct ref_filter_cbdata *ref_cbdata) -{ - int i; - - for (i = 0; i < ref_cbdata->array.nr; i++) - free(ref_cbdata->array.items[i]); - free(ref_cbdata->array.items); - ref_cbdata->array.items = NULL; - ref_cbdata->array.nr = ref_cbdata->array.alloc = 0; -} - -static int cmp_ref_sort(struct ref_sort *s, struct ref_array_item *a, struct ref_array_item *b) -{ - struct atom_value *va, *vb; - int cmp; - cmp_type cmp_type = used_atom_type[s->atom]; - - get_ref_atom_value(a, s->atom, &va); - get_ref_atom_value(b, s->atom, &vb); - switch (cmp_type) { - case FIELD_STR: - cmp = strcmp(va->s, vb->s); - break; - default: - if (va->ul < vb->ul) - cmp = -1; - else if (va->ul == vb->ul) - cmp = 0; - else - cmp = 1; - break; - } - return (s->reverse) ? -cmp : cmp; -} - -static struct ref_sort *ref_sort; -static int compare_refs(const void *a_, const void *b_) -{ - struct ref_array_item *a = *((struct ref_array_item **)a_); - struct ref_array_item *b = *((struct ref_array_item **)b_); - struct ref_sort *s; - - for (s = ref_sort; s; s = s->next) { - int cmp = cmp_ref_sort(s, a, b); - if (cmp) - return cmp; - } - return 0; -} - -void sort_ref_array(struct ref_sort *sort, struct ref_array *array) -{ - ref_sort = sort; - qsort(array->items, array->nr, sizeof(struct ref_array_item *), compare_refs); -} - -static void print_value(struct atom_value *v, int quote_style) -{ - struct strbuf sb = STRBUF_INIT; - switch (quote_style) { - case QUOTE_NONE: - fputs(v->s, stdout); - break; - case QUOTE_SHELL: - sq_quote_buf(&sb, v->s); - break; - case QUOTE_PERL: - perl_quote_buf(&sb, v->s); - break; - case QUOTE_PYTHON: - python_quote_buf(&sb, v->s); - break; - case QUOTE_TCL: - tcl_quote_buf(&sb, v->s); - break; - } - if (quote_style != QUOTE_NONE) { - fputs(sb.buf, stdout); - strbuf_release(&sb); - } -} - -static int hex1(char ch) -{ - if ('0' <= ch && ch <= '9') - return ch - '0'; - else if ('a' <= ch && ch <= 'f') - return ch - 'a' + 10; - else if ('A' <= ch && ch <= 'F') - return ch - 'A' + 10; - return -1; -} -static int hex2(const char *cp) -{ - if (cp[0] && cp[1]) - return (hex1(cp[0]) << 4) | hex1(cp[1]); - else - return -1; -} - -static void emit(const char *cp, const char *ep) -{ - while (*cp && (!ep || cp < ep)) { - if (*cp == '%') { - if (cp[1] == '%') - cp++; - else { - int ch = hex2(cp + 1); - if (0 <= ch) { - putchar(ch); - cp += 3; - continue; - } - } - } - putchar(*cp); - cp++; - } -} - -void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style) -{ - const char *cp, *sp, *ep; - - for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) { - struct atom_value *atomv; - - ep = strchr(sp, ')'); - if (cp < sp) - emit(cp, sp); - get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv); - print_value(atomv, quote_style); - } - if (*cp) { - sp = cp + strlen(cp); - emit(cp, sp); - } - if (need_color_reset_at_eol) { - struct atom_value resetv; - char color[COLOR_MAXLEN] = ""; - - if (color_parse("reset", color) < 0) - die("BUG: couldn't parse 'reset' as a color"); - resetv.s = color; - print_value(&resetv, quote_style); - } - putchar('\n'); -} - -/* If no sorting option is given, use refname to sort as default */ -struct ref_sort *ref_default_sort(void) -{ - static const char cstr_name[] = "refname"; - - struct ref_sort *sort = xcalloc(1, sizeof(*sort)); - - sort->next = NULL; - sort->atom = parse_ref_filter_atom(cstr_name, cstr_name + strlen(cstr_name)); - return sort; -} - -int opt_parse_ref_sort(const struct option *opt, const char *arg, int unset) -{ - struct ref_sort **sort_tail = opt->value; - struct ref_sort *s; - int len; - - if (!arg) /* should --no-sort void the list ? */ - return -1; - - s = xcalloc(1, sizeof(*s)); - s->next = *sort_tail; - *sort_tail = s; - - if (*arg == '-') { - s->reverse = 1; - arg++; - } - len = strlen(arg); - s->atom = parse_ref_filter_atom(arg, arg+len); - return 0; -} - static char const * const for_each_ref_usage[] = { N_("git for-each-ref [<options>] [<pattern>]"), NULL diff --git a/ref-filter.c b/ref-filter.c new file mode 100644 index 0000000..2a8f504 --- /dev/null +++ b/ref-filter.c @@ -0,0 +1,1050 @@ +#include "builtin.h" +#include "cache.h" +#include "parse-options.h" +#include "refs.h" +#include "wildmatch.h" +#include "commit.h" +#include "remote.h" +#include "color.h" +#include "tag.h" +#include "quote.h" + +typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type; + +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 || ep < formatp) + formatp = ep; + if (len == formatp - sp && !memcmp(valid_atom[i].name, sp, len)) + break; + } + + if (ARRAY_SIZE(valid_atom) <= i) + die("unknown field name: %.*s", (int)(ep-atom), atom); + + /* Add it in, including the deref prefix */ + at = used_atom_cnt; + used_atom_cnt++; + REALLOC_ARRAY(used_atom, used_atom_cnt); + REALLOC_ARRAY(used_atom_type, used_atom_cnt); + used_atom[at] = xmemdupz(atom, ep - atom); + used_atom_type[at] = valid_atom[i].cmp_type; + if (*atom == '*') + need_tagged = 1; + if (!strcmp(used_atom[at], "symref")) + need_symref = 1; + return at; +} + +/* + * In a format string, find the next occurrence of %(atom). + */ +static const char *find_next(const char *cp) +{ + while (*cp) { + if (*cp == '%') { + /* + * %( is the start of an atom; + * %% is a quoted per-cent. + */ + if (cp[1] == '(') + return cp; + else if (cp[1] == '%') + cp++; /* skip over two % */ + /* otherwise this is a singleton, literal % */ + } + cp++; + } + return NULL; +} + +/* + * Make sure the format string is well formed, and parse out + * the used atoms. + */ +int verify_ref_format(const char *format) +{ + const char *cp, *sp; + + need_color_reset_at_eol = 0; + for (cp = format; *cp && (sp = find_next(cp)); ) { + const char *color, *ep = strchr(sp, ')'); + int at; + + if (!ep) + return error("malformed format string %s", sp); + /* sp points at "%(" and ep points at the closing ")" */ + at = parse_ref_filter_atom(sp + 2, ep); + cp = ep + 1; + + if (skip_prefix(used_atom[at], "color:", &color)) + need_color_reset_at_eol = !!strcmp(color, "reset"); + } + return 0; +} + +/* + * Given an object name, read the object data and size, and return a + * "struct object". If the object data we are returning is also borrowed + * by the "struct object" representation, set *eaten as well---it is a + * signal from parse_object_buffer to us not to free the buffer. + */ +static void *get_obj(const unsigned char *sha1, struct object **obj, unsigned long *sz, int *eaten) +{ + enum object_type type; + void *buf = read_sha1_file(sha1, &type, sz); + + if (buf) + *obj = parse_object_buffer(sha1, type, *sz, buf, eaten); + else + *obj = NULL; + return buf; +} + +static int grab_objectname(const char *name, const unsigned char *sha1, + struct atom_value *v) +{ + if (!strcmp(name, "objectname")) { + char *s = xmalloc(41); + strcpy(s, sha1_to_hex(sha1)); + v->s = s; + return 1; + } + if (!strcmp(name, "objectname:short")) { + v->s = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV)); + return 1; + } + return 0; +} + +/* See grab_values */ +static void grab_common_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) +{ + int i; + + for (i = 0; i < used_atom_cnt; i++) { + const char *name = used_atom[i]; + struct atom_value *v = &val[i]; + if (!!deref != (*name == '*')) + continue; + if (deref) + name++; + if (!strcmp(name, "objecttype")) + v->s = typename(obj->type); + else if (!strcmp(name, "objectsize")) { + char *s = xmalloc(40); + sprintf(s, "%lu", sz); + v->ul = sz; + v->s = s; + } + else if (deref) + grab_objectname(name, obj->sha1, v); + } +} + +/* See grab_values */ +static void grab_tag_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) +{ + int i; + struct tag *tag = (struct tag *) obj; + + for (i = 0; i < used_atom_cnt; i++) { + const char *name = used_atom[i]; + struct atom_value *v = &val[i]; + if (!!deref != (*name == '*')) + continue; + if (deref) + name++; + if (!strcmp(name, "tag")) + v->s = tag->tag; + else if (!strcmp(name, "type") && tag->tagged) + v->s = typename(tag->tagged->type); + else if (!strcmp(name, "object") && tag->tagged) { + char *s = xmalloc(41); + strcpy(s, sha1_to_hex(tag->tagged->sha1)); + v->s = s; + } + } +} + +/* See grab_values */ +static void grab_commit_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) +{ + int i; + struct commit *commit = (struct commit *) obj; + + for (i = 0; i < used_atom_cnt; i++) { + const char *name = used_atom[i]; + struct atom_value *v = &val[i]; + if (!!deref != (*name == '*')) + continue; + if (deref) + name++; + if (!strcmp(name, "tree")) { + char *s = xmalloc(41); + strcpy(s, sha1_to_hex(commit->tree->object.sha1)); + v->s = s; + } + if (!strcmp(name, "numparent")) { + char *s = xmalloc(40); + v->ul = commit_list_count(commit->parents); + sprintf(s, "%lu", v->ul); + v->s = s; + } + else if (!strcmp(name, "parent")) { + int num = commit_list_count(commit->parents); + int i; + struct commit_list *parents; + char *s = xmalloc(41 * num + 1); + v->s = s; + for (i = 0, parents = commit->parents; + parents; + parents = parents->next, i = i + 41) { + struct commit *parent = parents->item; + strcpy(s+i, sha1_to_hex(parent->object.sha1)); + if (parents->next) + s[i+40] = ' '; + } + if (!i) + *s = '\0'; + } + } +} + +static const char *find_wholine(const char *who, int wholen, const char *buf, unsigned long sz) +{ + const char *eol; + while (*buf) { + if (!strncmp(buf, who, wholen) && + buf[wholen] == ' ') + return buf + wholen + 1; + eol = strchr(buf, '\n'); + if (!eol) + return ""; + eol++; + if (*eol == '\n') + return ""; /* end of header */ + buf = eol; + } + return ""; +} + +static const char *copy_line(const char *buf) +{ + const char *eol = strchrnul(buf, '\n'); + return xmemdupz(buf, eol - buf); +} + +static const char *copy_name(const char *buf) +{ + const char *cp; + for (cp = buf; *cp && *cp != '\n'; cp++) { + if (!strncmp(cp, " <", 2)) + return xmemdupz(buf, cp - buf); + } + return ""; +} + +static const char *copy_email(const char *buf) +{ + const char *email = strchr(buf, '<'); + const char *eoemail; + if (!email) + return ""; + eoemail = strchr(email, '>'); + if (!eoemail) + return ""; + return xmemdupz(email, eoemail + 1 - email); +} + +static char *copy_subject(const char *buf, unsigned long len) +{ + char *r = xmemdupz(buf, len); + int i; + + for (i = 0; i < len; i++) + if (r[i] == '\n') + r[i] = ' '; + + return r; +} + +static void grab_date(const char *buf, struct atom_value *v, const char *atomname) +{ + const char *eoemail = strstr(buf, "> "); + char *zone; + unsigned long timestamp; + long tz; + enum date_mode date_mode = DATE_NORMAL; + const char *formatp; + + /* + * We got here because atomname ends in "date" or "date<something>"; + * it's not possible that <something> is not ":<format>" because + * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no + * ":" means no format is specified, and use the default. + */ + formatp = strchr(atomname, ':'); + if (formatp != NULL) { + formatp++; + date_mode = parse_date_format(formatp); + } + + if (!eoemail) + goto bad; + timestamp = strtoul(eoemail + 2, &zone, 10); + if (timestamp == ULONG_MAX) + goto bad; + tz = strtol(zone, NULL, 10); + if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE) + goto bad; + v->s = xstrdup(show_date(timestamp, tz, date_mode)); + v->ul = timestamp; + return; + bad: + v->s = ""; + v->ul = 0; +} + +/* See grab_values */ +static void grab_person(const char *who, struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) +{ + int i; + int wholen = strlen(who); + const char *wholine = NULL; + + for (i = 0; i < used_atom_cnt; i++) { + const char *name = used_atom[i]; + struct atom_value *v = &val[i]; + if (!!deref != (*name == '*')) + continue; + if (deref) + name++; + if (strncmp(who, name, wholen)) + continue; + if (name[wholen] != 0 && + strcmp(name + wholen, "name") && + strcmp(name + wholen, "email") && + !starts_with(name + wholen, "date")) + continue; + if (!wholine) + wholine = find_wholine(who, wholen, buf, sz); + if (!wholine) + return; /* no point looking for it */ + if (name[wholen] == 0) + v->s = copy_line(wholine); + else if (!strcmp(name + wholen, "name")) + v->s = copy_name(wholine); + else if (!strcmp(name + wholen, "email")) + v->s = copy_email(wholine); + else if (starts_with(name + wholen, "date")) + grab_date(wholine, v, name); + } + + /* + * For a tag or a commit object, if "creator" or "creatordate" is + * requested, do something special. + */ + if (strcmp(who, "tagger") && strcmp(who, "committer")) + return; /* "author" for commit object is not wanted */ + if (!wholine) + wholine = find_wholine(who, wholen, buf, sz); + if (!wholine) + return; + for (i = 0; i < used_atom_cnt; i++) { + const char *name = used_atom[i]; + struct atom_value *v = &val[i]; + if (!!deref != (*name == '*')) + continue; + if (deref) + name++; + + if (starts_with(name, "creatordate")) + grab_date(wholine, v, name); + else if (!strcmp(name, "creator")) + v->s = copy_line(wholine); + } +} + +static void find_subpos(const char *buf, unsigned long sz, + const char **sub, unsigned long *sublen, + const char **body, unsigned long *bodylen, + unsigned long *nonsiglen, + const char **sig, unsigned long *siglen) +{ + const char *eol; + /* skip past header until we hit empty line */ + while (*buf && *buf != '\n') { + eol = strchrnul(buf, '\n'); + if (*eol) + eol++; + buf = eol; + } + /* skip any empty lines */ + while (*buf == '\n') + buf++; + + /* parse signature first; we might not even have a subject line */ + *sig = buf + parse_signature(buf, strlen(buf)); + *siglen = strlen(*sig); + + /* subject is first non-empty line */ + *sub = buf; + /* subject goes to first empty line */ + while (buf < *sig && *buf && *buf != '\n') { + eol = strchrnul(buf, '\n'); + if (*eol) + eol++; + buf = eol; + } + *sublen = buf - *sub; + /* drop trailing newline, if present */ + if (*sublen && (*sub)[*sublen - 1] == '\n') + *sublen -= 1; + + /* skip any empty lines */ + while (*buf == '\n') + buf++; + *body = buf; + *bodylen = strlen(buf); + *nonsiglen = *sig - buf; +} + +/* See grab_values */ +static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) +{ + int i; + const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL; + unsigned long sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0; + + for (i = 0; i < used_atom_cnt; i++) { + const char *name = used_atom[i]; + struct atom_value *v = &val[i]; + if (!!deref != (*name == '*')) + continue; + if (deref) + name++; + if (strcmp(name, "subject") && + strcmp(name, "body") && + strcmp(name, "contents") && + strcmp(name, "contents:subject") && + strcmp(name, "contents:body") && + strcmp(name, "contents:signature")) + continue; + if (!subpos) + find_subpos(buf, sz, + &subpos, &sublen, + &bodypos, &bodylen, &nonsiglen, + &sigpos, &siglen); + + if (!strcmp(name, "subject")) + v->s = copy_subject(subpos, sublen); + else if (!strcmp(name, "contents:subject")) + v->s = copy_subject(subpos, sublen); + else if (!strcmp(name, "body")) + v->s = xmemdupz(bodypos, bodylen); + else if (!strcmp(name, "contents:body")) + v->s = xmemdupz(bodypos, nonsiglen); + else if (!strcmp(name, "contents:signature")) + v->s = xmemdupz(sigpos, siglen); + else if (!strcmp(name, "contents")) + v->s = xstrdup(subpos); + } +} + +/* + * We want to have empty print-string for field requests + * that do not apply (e.g. "authordate" for a tag object) + */ +static void fill_missing_values(struct atom_value *val) +{ + int i; + for (i = 0; i < used_atom_cnt; i++) { + struct atom_value *v = &val[i]; + if (v->s == NULL) + v->s = ""; + } +} + +/* + * val is a list of atom_value to hold returned values. Extract + * the values for atoms in used_atom array out of (obj, buf, sz). + * when deref is false, (obj, buf, sz) is the object that is + * pointed at by the ref itself; otherwise it is the object the + * ref (which is a tag) refers to. + */ +static void grab_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) +{ + grab_common_values(val, deref, obj, buf, sz); + switch (obj->type) { + case OBJ_TAG: + grab_tag_values(val, deref, obj, buf, sz); + grab_sub_body_contents(val, deref, obj, buf, sz); + grab_person("tagger", val, deref, obj, buf, sz); + break; + case OBJ_COMMIT: + grab_commit_values(val, deref, obj, buf, sz); + grab_sub_body_contents(val, deref, obj, buf, sz); + grab_person("author", val, deref, obj, buf, sz); + grab_person("committer", val, deref, obj, buf, sz); + break; + case OBJ_TREE: + /* grab_tree_values(val, deref, obj, buf, sz); */ + break; + case OBJ_BLOB: + /* grab_blob_values(val, deref, obj, buf, sz); */ + break; + default: + die("Eh? Object of type %d?", obj->type); + } +} + +static inline char *copy_advance(char *dst, const char *src) +{ + while (*src) + *dst++ = *src++; + return dst; +} + +/* + * Parse the object referred by ref, and grab needed value. + */ +static void populate_value(struct ref_array_item *ref) +{ + void *buf; + struct object *obj; + int eaten, i; + unsigned long size; + const unsigned char *tagged; + + ref->value = xcalloc(used_atom_cnt, sizeof(struct atom_value)); + + if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) { + unsigned char unused1[20]; + ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING, + unused1, NULL); + if (!ref->symref) + ref->symref = ""; + } + + /* Fill in specials first */ + for (i = 0; i < used_atom_cnt; i++) { + const char *name = used_atom[i]; + struct atom_value *v = &ref->value[i]; + int deref = 0; + const char *refname; + const char *formatp; + struct branch *branch = NULL; + + if (*name == '*') { + deref = 1; + name++; + } + + if (starts_with(name, "refname")) + refname = ref->refname; + else if (starts_with(name, "symref")) + refname = ref->symref ? ref->symref : ""; + else if (starts_with(name, "upstream")) { + /* only local branches may have an upstream */ + if (!starts_with(ref->refname, "refs/heads/")) + continue; + branch = branch_get(ref->refname + 11); + + if (!branch || !branch->merge || !branch->merge[0] || + !branch->merge[0]->dst) + continue; + refname = branch->merge[0]->dst; + } else if (starts_with(name, "color:")) { + char color[COLOR_MAXLEN] = ""; + + if (color_parse(name + 6, color) < 0) + die(_("unable to parse format")); + v->s = xstrdup(color); + continue; + } else if (!strcmp(name, "flag")) { + char buf[256], *cp = buf; + if (ref->flag & REF_ISSYMREF) + cp = copy_advance(cp, ",symref"); + if (ref->flag & REF_ISPACKED) + cp = copy_advance(cp, ",packed"); + if (cp == buf) + v->s = ""; + else { + *cp = '\0'; + v->s = xstrdup(buf + 1); + } + continue; + } else if (!deref && grab_objectname(name, ref->objectname, v)) { + continue; + } else if (!strcmp(name, "HEAD")) { + const char *head; + unsigned char sha1[20]; + + head = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, + sha1, NULL); + if (!strcmp(ref->refname, head)) + v->s = "*"; + else + v->s = " "; + continue; + } else + continue; + + formatp = strchr(name, ':'); + if (formatp) { + int num_ours, num_theirs; + + formatp++; + if (!strcmp(formatp, "short")) + refname = shorten_unambiguous_ref(refname, + warn_ambiguous_refs); + else if (!strcmp(formatp, "track") && + starts_with(name, "upstream")) { + char buf[40]; + + if (stat_tracking_info(branch, &num_ours, + &num_theirs) != 1) + continue; + + if (!num_ours && !num_theirs) + v->s = ""; + else if (!num_ours) { + sprintf(buf, "[behind %d]", num_theirs); + v->s = xstrdup(buf); + } else if (!num_theirs) { + sprintf(buf, "[ahead %d]", num_ours); + v->s = xstrdup(buf); + } else { + sprintf(buf, "[ahead %d, behind %d]", + num_ours, num_theirs); + v->s = xstrdup(buf); + } + continue; + } else if (!strcmp(formatp, "trackshort") && + starts_with(name, "upstream")) { + assert(branch); + + if (stat_tracking_info(branch, &num_ours, + &num_theirs) != 1) + continue; + + if (!num_ours && !num_theirs) + v->s = "="; + else if (!num_ours) + v->s = "<"; + else if (!num_theirs) + v->s = ">"; + else + v->s = "<>"; + continue; + } else + die("unknown %.*s format %s", + (int)(formatp - name), name, formatp); + } + + if (!deref) + v->s = refname; + else { + int len = strlen(refname); + char *s = xmalloc(len + 4); + sprintf(s, "%s^{}", refname); + v->s = s; + } + } + + for (i = 0; i < used_atom_cnt; i++) { + struct atom_value *v = &ref->value[i]; + if (v->s == NULL) + goto need_obj; + } + return; + + need_obj: + buf = get_obj(ref->objectname, &obj, &size, &eaten); + if (!buf) + die("missing object %s for %s", + sha1_to_hex(ref->objectname), ref->refname); + if (!obj) + die("parse_object_buffer failed on %s for %s", + sha1_to_hex(ref->objectname), ref->refname); + + grab_values(ref->value, 0, obj, buf, size); + if (!eaten) + free(buf); + + /* + * If there is no atom that wants to know about tagged + * object, we are done. + */ + if (!need_tagged || (obj->type != OBJ_TAG)) + return; + + /* + * If it is a tag object, see if we use a value that derefs + * the object, and if we do grab the object it refers to. + */ + tagged = ((struct tag *)obj)->tagged->sha1; + + /* + * NEEDSWORK: This derefs tag only once, which + * is good to deal with chains of trust, but + * is not consistent with what deref_tag() does + * which peels the onion to the core. + */ + buf = get_obj(tagged, &obj, &size, &eaten); + if (!buf) + die("missing object %s for %s", + sha1_to_hex(tagged), ref->refname); + if (!obj) + die("parse_object_buffer failed on %s for %s", + sha1_to_hex(tagged), ref->refname); + grab_values(ref->value, 1, obj, buf, size); + if (!eaten) + free(buf); +} + +/* + * Given a ref, return the value for the atom. This lazily gets value + * out of the object by calling populate value. + */ +static void get_ref_atom_value(struct ref_array_item *ref, int atom, struct atom_value **v) +{ + if (!ref->value) { + populate_value(ref); + fill_missing_values(ref->value); + } + *v = &ref->value[atom]; +} + +/* + * Given a refname, return 1 if the refname matches with one of the patterns + * while the pattern is a pathname like 'refs/tags' or 'refs/heads/master' + * and so on, else return 0. Supports use of wild characters. + */ +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; +} + +/* Allocate space for a new ref_array_item and copy the objectname and flag to it */ +static struct ref_array_item *new_ref_array_item(const char *refname, + const unsigned char *objectname, + int flag) +{ + struct ref_array_item *ref = xcalloc(1, sizeof(struct ref_array_item)); + ref->refname = xstrdup(refname); + hashcpy(ref->objectname, objectname); + ref->flag = flag; + + return ref; +} + +/* + * A call-back given to for_each_ref(). Filter refs and keep them for + * later object processing. + */ +int ref_filter_handler(const char *refname, const unsigned char *sha1, int flag, void *cb_data) +{ + struct ref_filter_cbdata *ref_cbdata = cb_data; + struct ref_filter *filter = &ref_cbdata->filter; + struct ref_array_item *ref; + + if (flag & REF_BAD_NAME) { + warning("ignoring ref with broken name %s", refname); + return 0; + } + + if (*filter->name_patterns && !match_name_as_path(filter->name_patterns, refname)) + return 0; + + ref = new_ref_array_item(refname, sha1, flag); + + REALLOC_ARRAY(ref_cbdata->array.items, ref_cbdata->array.nr + 1); + ref_cbdata->array.items[ref_cbdata->array.nr++] = ref; + + return 0; +} + +/* Free all memory allocated for ref_filter_cbdata */ +void ref_filter_clear_data(struct ref_filter_cbdata *ref_cbdata) +{ + int i; + + for (i = 0; i < ref_cbdata->array.nr; i++) + free(ref_cbdata->array.items[i]); + free(ref_cbdata->array.items); + ref_cbdata->array.items = NULL; + ref_cbdata->array.nr = ref_cbdata->array.alloc = 0; +} + +static int cmp_ref_sort(struct ref_sort *s, struct ref_array_item *a, struct ref_array_item *b) +{ + struct atom_value *va, *vb; + int cmp; + cmp_type cmp_type = used_atom_type[s->atom]; + + get_ref_atom_value(a, s->atom, &va); + get_ref_atom_value(b, s->atom, &vb); + switch (cmp_type) { + case FIELD_STR: + cmp = strcmp(va->s, vb->s); + break; + default: + if (va->ul < vb->ul) + cmp = -1; + else if (va->ul == vb->ul) + cmp = 0; + else + cmp = 1; + break; + } + return (s->reverse) ? -cmp : cmp; +} + +static struct ref_sort *ref_sort; +static int compare_refs(const void *a_, const void *b_) +{ + struct ref_array_item *a = *((struct ref_array_item **)a_); + struct ref_array_item *b = *((struct ref_array_item **)b_); + struct ref_sort *s; + + for (s = ref_sort; s; s = s->next) { + int cmp = cmp_ref_sort(s, a, b); + if (cmp) + return cmp; + } + return 0; +} + +void sort_ref_array(struct ref_sort *sort, struct ref_array *array) +{ + ref_sort = sort; + qsort(array->items, array->nr, sizeof(struct ref_array_item *), compare_refs); +} + +static void print_value(struct atom_value *v, int quote_style) +{ + struct strbuf sb = STRBUF_INIT; + switch (quote_style) { + case QUOTE_NONE: + fputs(v->s, stdout); + break; + case QUOTE_SHELL: + sq_quote_buf(&sb, v->s); + break; + case QUOTE_PERL: + perl_quote_buf(&sb, v->s); + break; + case QUOTE_PYTHON: + python_quote_buf(&sb, v->s); + break; + case QUOTE_TCL: + tcl_quote_buf(&sb, v->s); + break; + } + if (quote_style != QUOTE_NONE) { + fputs(sb.buf, stdout); + strbuf_release(&sb); + } +} + +static int hex1(char ch) +{ + if ('0' <= ch && ch <= '9') + return ch - '0'; + else if ('a' <= ch && ch <= 'f') + return ch - 'a' + 10; + else if ('A' <= ch && ch <= 'F') + return ch - 'A' + 10; + return -1; +} +static int hex2(const char *cp) +{ + if (cp[0] && cp[1]) + return (hex1(cp[0]) << 4) | hex1(cp[1]); + else + return -1; +} + +static void emit(const char *cp, const char *ep) +{ + while (*cp && (!ep || cp < ep)) { + if (*cp == '%') { + if (cp[1] == '%') + cp++; + else { + int ch = hex2(cp + 1); + if (0 <= ch) { + putchar(ch); + cp += 3; + continue; + } + } + } + putchar(*cp); + cp++; + } +} + +void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style) +{ + const char *cp, *sp, *ep; + + for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) { + struct atom_value *atomv; + + ep = strchr(sp, ')'); + if (cp < sp) + emit(cp, sp); + get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv); + print_value(atomv, quote_style); + } + if (*cp) { + sp = cp + strlen(cp); + emit(cp, sp); + } + if (need_color_reset_at_eol) { + struct atom_value resetv; + char color[COLOR_MAXLEN] = ""; + + if (color_parse("reset", color) < 0) + die("BUG: couldn't parse 'reset' as a color"); + resetv.s = color; + print_value(&resetv, quote_style); + } + putchar('\n'); +} + +/* If no sorting option is given, use refname to sort as default */ +struct ref_sort *ref_default_sort(void) +{ + static const char cstr_name[] = "refname"; + + struct ref_sort *sort = xcalloc(1, sizeof(*sort)); + + sort->next = NULL; + sort->atom = parse_ref_filter_atom(cstr_name, cstr_name + strlen(cstr_name)); + return sort; +} + +int opt_parse_ref_sort(const struct option *opt, const char *arg, int unset) +{ + struct ref_sort **sort_tail = opt->value; + struct ref_sort *s; + int len; + + if (!arg) /* should --no-sort void the list ? */ + return -1; + + s = xcalloc(1, sizeof(*s)); + s->next = *sort_tail; + *sort_tail = s; + + if (*arg == '-') { + s->reverse = 1; + arg++; + } + len = strlen(arg); + s->atom = parse_ref_filter_atom(arg, arg+len); + return 0; +} -- 2.4.2 ^ permalink raw reply related [flat|nested] 44+ messages in thread
* [WIP/PATCH v4 8/8] ref-filter: add 'ref-filter.h' [not found] <5569EF77.4010300@gmail.com> ` (6 preceding siblings ...) 2015-05-30 17:53 ` [WIP/PATCH v4 7/8] ref-filter: move code from 'for-each-ref' Karthik Nayak @ 2015-05-30 17:53 ` Karthik Nayak 2015-05-31 3:43 ` Eric Sunshine 2015-05-31 8:20 ` Christian Couder 7 siblings, 2 replies; 44+ messages in thread From: Karthik Nayak @ 2015-05-30 17:53 UTC (permalink / raw) To: git; +Cc: christian.couder, Matthieu.Moy, Karthik Nayak Create 'ref-filter.h', also add ref-filter to the Makefile. This completes movement of creation of 'ref-filter' from 'for-each-ref'. 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 + builtin/for-each-ref.c | 41 +------------------------------ ref-filter.c | 1 + ref-filter.h | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 40 deletions(-) create mode 100644 ref-filter.h diff --git a/Makefile b/Makefile index 323c401..ad455a3 100644 --- a/Makefile +++ b/Makefile @@ -762,6 +762,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 65ed954..2f11c01 100644 --- a/builtin/for-each-ref.c +++ b/builtin/for-each-ref.c @@ -2,46 +2,7 @@ #include "cache.h" #include "refs.h" #include "parse-options.h" - -/* Quoting styles */ -#define QUOTE_NONE 0 -#define QUOTE_SHELL 1 -#define QUOTE_PERL 2 -#define QUOTE_PYTHON 4 -#define QUOTE_TCL 8 - -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; -}; +#include "ref-filter.h" static char const * const for_each_ref_usage[] = { N_("git for-each-ref [<options>] [<pattern>]"), diff --git a/ref-filter.c b/ref-filter.c index 2a8f504..1c73750 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -2,6 +2,7 @@ #include "cache.h" #include "parse-options.h" #include "refs.h" +#include "ref-filter.h" #include "wildmatch.h" #include "commit.h" #include "remote.h" diff --git a/ref-filter.h b/ref-filter.h new file mode 100644 index 0000000..dacae59 --- /dev/null +++ b/ref-filter.h @@ -0,0 +1,66 @@ +#ifndef REF_FILTER_H +#define REF_FILTER_H + +#include "sha1-array.h" +#include "refs.h" +#include "commit.h" +#include "parse-options.h" + +/* Quoting styles */ +#define QUOTE_NONE 0 +#define QUOTE_SHELL 1 +#define QUOTE_PERL 2 +#define QUOTE_PYTHON 4 +#define QUOTE_TCL 8 + +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; +}; + +/* Callback function for for_each_*ref(). This filters the refs based on the filters set */ +int ref_filter_handler(const char *refname, const unsigned char *sha1, int flag, void *cb_data); +/* Clear all memory allocated to ref_filter_data */ +void ref_filter_clear_data(struct ref_filter_cbdata *ref_cbdata); +/* Parse format string and sort specifiers */ +int parse_ref_filter_atom(const char *atom, const char *ep); +/* Used to verify if the given format is correct and to parse out the used atoms */ +int verify_ref_format(const char *format); +/* Sort the given ref_array as per the ref_sort provided */ +void sort_ref_array(struct ref_sort *sort, struct ref_array *array); +/* Print the ref using the given format and quote_style */ +void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style); +/* Callback function for parsing the sort option */ +int opt_parse_ref_sort(const struct option *opt, const char *arg, int unset); +/* Default sort option based on refname */ +struct ref_sort *ref_default_sort(void); + +#endif /* REF_FILTER_H */ -- 2.4.2 ^ permalink raw reply related [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 8/8] ref-filter: add 'ref-filter.h' 2015-05-30 17:53 ` [WIP/PATCH v4 8/8] ref-filter: add 'ref-filter.h' Karthik Nayak @ 2015-05-31 3:43 ` Eric Sunshine 2015-05-31 8:19 ` Karthik Nayak 2015-05-31 8:20 ` Christian Couder 1 sibling, 1 reply; 44+ messages in thread From: Eric Sunshine @ 2015-05-31 3:43 UTC (permalink / raw) To: Karthik Nayak; +Cc: Git List, Christian Couder, Matthieu Moy On Sat, May 30, 2015 at 1:53 PM, Karthik Nayak <karthik.188@gmail.com> wrote: > Create 'ref-filter.h', also add ref-filter to the Makefile. > This completes movement of creation of 'ref-filter' from > 'for-each-ref'. It's important that the project can be built successfully and function correctly at each stage of a patch series. Unfortunately, the way this series is organized, the build breaks after application of patch 7/8 since for-each-ref.c is trying to access functionality which was moved to ref-filter.c, but there is no header at that stage which advertises the functionality. Fixing this may be as simple as swapping patches 7/8 and 8/8, along with whatever minor adjustments they might need to keep them sane. (The alternative would be to combine patches 7/8 and 8/8, however, Matthieu didn't seem to favor that approach[1].) Overall, this round has been a more pleasant read than previous rounds. [1]: http://article.gmane.org/gmane.comp.version-control.git/270192 > 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 + > builtin/for-each-ref.c | 41 +------------------------------ > ref-filter.c | 1 + > ref-filter.h | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ > 4 files changed, 69 insertions(+), 40 deletions(-) > create mode 100644 ref-filter.h > > diff --git a/Makefile b/Makefile > index 323c401..ad455a3 100644 > --- a/Makefile > +++ b/Makefile > @@ -762,6 +762,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 65ed954..2f11c01 100644 > --- a/builtin/for-each-ref.c > +++ b/builtin/for-each-ref.c > @@ -2,46 +2,7 @@ > #include "cache.h" > #include "refs.h" > #include "parse-options.h" > - > -/* Quoting styles */ > -#define QUOTE_NONE 0 > -#define QUOTE_SHELL 1 > -#define QUOTE_PERL 2 > -#define QUOTE_PYTHON 4 > -#define QUOTE_TCL 8 > - > -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; > -}; > +#include "ref-filter.h" > > static char const * const for_each_ref_usage[] = { > N_("git for-each-ref [<options>] [<pattern>]"), > diff --git a/ref-filter.c b/ref-filter.c > index 2a8f504..1c73750 100644 > --- a/ref-filter.c > +++ b/ref-filter.c > @@ -2,6 +2,7 @@ > #include "cache.h" > #include "parse-options.h" > #include "refs.h" > +#include "ref-filter.h" > #include "wildmatch.h" > #include "commit.h" > #include "remote.h" > diff --git a/ref-filter.h b/ref-filter.h > new file mode 100644 > index 0000000..dacae59 > --- /dev/null > +++ b/ref-filter.h > @@ -0,0 +1,66 @@ > +#ifndef REF_FILTER_H > +#define REF_FILTER_H > + > +#include "sha1-array.h" > +#include "refs.h" > +#include "commit.h" > +#include "parse-options.h" > + > +/* Quoting styles */ > +#define QUOTE_NONE 0 > +#define QUOTE_SHELL 1 > +#define QUOTE_PERL 2 > +#define QUOTE_PYTHON 4 > +#define QUOTE_TCL 8 > + > +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; > +}; > + > +/* Callback function for for_each_*ref(). This filters the refs based on the filters set */ > +int ref_filter_handler(const char *refname, const unsigned char *sha1, int flag, void *cb_data); > +/* Clear all memory allocated to ref_filter_data */ > +void ref_filter_clear_data(struct ref_filter_cbdata *ref_cbdata); > +/* Parse format string and sort specifiers */ > +int parse_ref_filter_atom(const char *atom, const char *ep); > +/* Used to verify if the given format is correct and to parse out the used atoms */ > +int verify_ref_format(const char *format); > +/* Sort the given ref_array as per the ref_sort provided */ > +void sort_ref_array(struct ref_sort *sort, struct ref_array *array); > +/* Print the ref using the given format and quote_style */ > +void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style); > +/* Callback function for parsing the sort option */ > +int opt_parse_ref_sort(const struct option *opt, const char *arg, int unset); > +/* Default sort option based on refname */ > +struct ref_sort *ref_default_sort(void); > + > +#endif /* REF_FILTER_H */ > -- > 2.4.2 ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 8/8] ref-filter: add 'ref-filter.h' 2015-05-31 3:43 ` Eric Sunshine @ 2015-05-31 8:19 ` Karthik Nayak 2015-05-31 8:29 ` Eric Sunshine 2015-05-31 20:46 ` Matthieu Moy 0 siblings, 2 replies; 44+ messages in thread From: Karthik Nayak @ 2015-05-31 8:19 UTC (permalink / raw) To: Eric Sunshine; +Cc: Git List, Christian Couder, Matthieu Moy On 05/31/2015 09:13 AM, Eric Sunshine wrote: > On Sat, May 30, 2015 at 1:53 PM, Karthik Nayak <karthik.188@gmail.com> wrote: >> Create 'ref-filter.h', also add ref-filter to the Makefile. >> This completes movement of creation of 'ref-filter' from >> 'for-each-ref'. > > It's important that the project can be built successfully and function > correctly at each stage of a patch series. Unfortunately, the way this > series is organized, the build breaks after application of patch 7/8 > since for-each-ref.c is trying to access functionality which was moved > to ref-filter.c, but there is no header at that stage which advertises > the functionality. Fixing this may be as simple as swapping patches > 7/8 and 8/8, along with whatever minor adjustments they might need to > keep them sane. (The alternative would be to combine patches 7/8 and > 8/8, however, Matthieu didn't seem to favor that approach[1].) > > Overall, this round has been a more pleasant read than previous rounds. > > [1]: http://article.gmane.org/gmane.comp.version-control.git/270192 > That is kind of a problem, If I need to swap those commits also, I'd have to add the part where ref-filter is added to the Makefile with the code movement from for-each-ref to ref-filter. This again will not just be Code movement. But I guess thats a fair trade-off, will change it. Thanks -- Regards, Karthik ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 8/8] ref-filter: add 'ref-filter.h' 2015-05-31 8:19 ` Karthik Nayak @ 2015-05-31 8:29 ` Eric Sunshine 2015-05-31 9:12 ` Karthik Nayak 2015-05-31 20:46 ` Matthieu Moy 1 sibling, 1 reply; 44+ messages in thread From: Eric Sunshine @ 2015-05-31 8:29 UTC (permalink / raw) To: Karthik Nayak; +Cc: Git List, Christian Couder, Matthieu Moy On Sun, May 31, 2015 at 4:19 AM, Karthik Nayak <karthik.188@gmail.com> wrote: > On 05/31/2015 09:13 AM, Eric Sunshine wrote: >> On Sat, May 30, 2015 at 1:53 PM, Karthik Nayak <karthik.188@gmail.com> >> wrote: >>> >>> Create 'ref-filter.h', also add ref-filter to the Makefile. >>> This completes movement of creation of 'ref-filter' from >>> 'for-each-ref'. >> >> It's important that the project can be built successfully and function >> correctly at each stage of a patch series. Unfortunately, the way this >> series is organized, the build breaks after application of patch 7/8 >> since for-each-ref.c is trying to access functionality which was moved >> to ref-filter.c, but there is no header at that stage which advertises >> the functionality. Fixing this may be as simple as swapping patches >> 7/8 and 8/8, along with whatever minor adjustments they might need to >> keep them sane. (The alternative would be to combine patches 7/8 and >> 8/8, however, Matthieu didn't seem to favor that approach[1].) > > That is kind of a problem, If I need to swap those commits also, I'd have to > add the part where ref-filter is added to the Makefile with the code > movement from for-each-ref to ref-filter. This again will not just be Code > movement. > But I guess thats a fair trade-off, will change it. Thanks Don't take the "code movement-only" recommendation too literally. What people mean is that you shouldn't make changes to the lines you're moving from one location to another (other than absolutely mandatory changes to support the movement) since it's difficult to spot and review changes in lines which are being moved. The recommendation is not saying that the patch itself can't include any other changes (though, it's often best to minimize other changes in the same patch). In this case, the Makefile change is logically related to that particular code movement, so it's correct to include it in the patch. (Also, the Makefile change is so minor that it's not a burden upon reviewers.) ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 8/8] ref-filter: add 'ref-filter.h' 2015-05-31 8:29 ` Eric Sunshine @ 2015-05-31 9:12 ` Karthik Nayak 0 siblings, 0 replies; 44+ messages in thread From: Karthik Nayak @ 2015-05-31 9:12 UTC (permalink / raw) To: Eric Sunshine; +Cc: Git List, Christian Couder, Matthieu Moy On 05/31/2015 01:59 PM, Eric Sunshine wrote: > On Sun, May 31, 2015 at 4:19 AM, Karthik Nayak <karthik.188@gmail.com> wrote: > > On 05/31/2015 09:13 AM, Eric Sunshine wrote: > >> On Sat, May 30, 2015 at 1:53 PM, Karthik Nayak <karthik.188@gmail.com> > >> wrote: > >>> > >>> Create 'ref-filter.h', also add ref-filter to the Makefile. > >>> This completes movement of creation of 'ref-filter' from > >>> 'for-each-ref'. > >> > >> It's important that the project can be built successfully and function > >> correctly at each stage of a patch series. Unfortunately, the way this > >> series is organized, the build breaks after application of patch 7/8 > >> since for-each-ref.c is trying to access functionality which was moved > >> to ref-filter.c, but there is no header at that stage which advertises > >> the functionality. Fixing this may be as simple as swapping patches > >> 7/8 and 8/8, along with whatever minor adjustments they might need to > >> keep them sane. (The alternative would be to combine patches 7/8 and > >> 8/8, however, Matthieu didn't seem to favor that approach[1].) > > > > That is kind of a problem, If I need to swap those commits also, I'd have to > > add the part where ref-filter is added to the Makefile with the code > > movement from for-each-ref to ref-filter. This again will not just be Code > > movement. > > But I guess thats a fair trade-off, will change it. Thanks > > Don't take the "code movement-only" recommendation too literally. What > people mean is that you shouldn't make changes to the lines you're > moving from one location to another (other than absolutely mandatory > changes to support the movement) since it's difficult to spot and > review changes in lines which are being moved. > > The recommendation is not saying that the patch itself can't include > any other changes (though, it's often best to minimize other changes > in the same patch). In this case, the Makefile change is logically > related to that particular code movement, so it's correct to include > it in the patch. (Also, the Makefile change is so minor that it's not > a burden upon reviewers.) > Thanks for clearing it up. -- Regards, Karthik ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 8/8] ref-filter: add 'ref-filter.h' 2015-05-31 8:19 ` Karthik Nayak 2015-05-31 8:29 ` Eric Sunshine @ 2015-05-31 20:46 ` Matthieu Moy 2015-05-31 20:50 ` Karthik Nayak 1 sibling, 1 reply; 44+ messages in thread From: Matthieu Moy @ 2015-05-31 20:46 UTC (permalink / raw) To: Karthik Nayak; +Cc: Eric Sunshine, Git List, Christian Couder Karthik Nayak <karthik.188@gmail.com> writes: > That is kind of a problem, If I need to swap those commits also, I'd > have to add the part where ref-filter is added to the Makefile with > the code movement from for-each-ref to ref-filter. This again will not > just be Code movement. You can have a preparatory patch that adds ref-filter.c containing just "#include ref-filter.h" and ref-filter.h with proper content. After this preparatory patch, you're in a rather silly situation because you have an almost empty .c file, but it's still passing all tests and compileable. Then, the next patch can be just code movement. -- Matthieu Moy http://www-verimag.imag.fr/~moy/ ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 8/8] ref-filter: add 'ref-filter.h' 2015-05-31 20:46 ` Matthieu Moy @ 2015-05-31 20:50 ` Karthik Nayak 2015-05-31 22:34 ` Christian Couder 2015-06-01 6:47 ` Matthieu Moy 0 siblings, 2 replies; 44+ messages in thread From: Karthik Nayak @ 2015-05-31 20:50 UTC (permalink / raw) To: Matthieu Moy; +Cc: Eric Sunshine, Git List, Christian Couder On 06/01/2015 02:16 AM, Matthieu Moy wrote: > Karthik Nayak <karthik.188@gmail.com> writes: > > > That is kind of a problem, If I need to swap those commits also, I'd > > have to add the part where ref-filter is added to the Makefile with > > the code movement from for-each-ref to ref-filter. This again will not > > just be Code movement. > > You can have a preparatory patch that adds ref-filter.c containing just > "#include ref-filter.h" and ref-filter.h with proper content. After this > preparatory patch, you're in a rather silly situation because you have > an almost empty .c file, but it's still passing all tests and > compileable. > > Then, the next patch can be just code movement. > Would it be okay, If I just include the Makefile addition along with the code movement from 'for-each-ref' to 'ref-filter.c' like Eric suggested? -- Regards, Karthik ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 8/8] ref-filter: add 'ref-filter.h' 2015-05-31 20:50 ` Karthik Nayak @ 2015-05-31 22:34 ` Christian Couder 2015-06-01 6:47 ` Matthieu Moy 1 sibling, 0 replies; 44+ messages in thread From: Christian Couder @ 2015-05-31 22:34 UTC (permalink / raw) To: Karthik Nayak; +Cc: Matthieu Moy, Eric Sunshine, Git List On Sun, May 31, 2015 at 10:50 PM, Karthik Nayak <karthik.188@gmail.com> wrote: > On 06/01/2015 02:16 AM, Matthieu Moy wrote: >> >> You can have a preparatory patch that adds ref-filter.c containing just >> "#include ref-filter.h" and ref-filter.h with proper content. After this >> preparatory patch, you're in a rather silly situation because you have >> an almost empty .c file, but it's still passing all tests and >> compileable. >> >> Then, the next patch can be just code movement. >> > Would it be okay, If I just include the Makefile addition along with the > code movement > from 'for-each-ref' to 'ref-filter.c' like Eric suggested? Yeah, I think it is ok as well. ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 8/8] ref-filter: add 'ref-filter.h' 2015-05-31 20:50 ` Karthik Nayak 2015-05-31 22:34 ` Christian Couder @ 2015-06-01 6:47 ` Matthieu Moy 1 sibling, 0 replies; 44+ messages in thread From: Matthieu Moy @ 2015-06-01 6:47 UTC (permalink / raw) To: Karthik Nayak; +Cc: Eric Sunshine, Git List, Christian Couder Karthik Nayak <karthik.188@gmail.com> writes: > On 06/01/2015 02:16 AM, Matthieu Moy wrote: >> Karthik Nayak <karthik.188@gmail.com> writes: >> >> > That is kind of a problem, If I need to swap those commits also, I'd >> > have to add the part where ref-filter is added to the Makefile with >> > the code movement from for-each-ref to ref-filter. This again will not >> > just be Code movement. >> >> You can have a preparatory patch that adds ref-filter.c containing just >> "#include ref-filter.h" and ref-filter.h with proper content. After this >> preparatory patch, you're in a rather silly situation because you have >> an almost empty .c file, but it's still passing all tests and >> compileable. >> >> Then, the next patch can be just code movement. >> > Would it be okay, If I just include the Makefile addition along with the code movement > from 'for-each-ref' to 'ref-filter.c' like Eric suggested? I prefer pure code movement, but as Eric said it's also OK to have small other changes as long as they are related (so it makes sense to include them in the same commit) and separate (in your case, they touch different files so it's easy to notice the Makefile change in the patch). In any case, because I prefer one way doesn't mean it's the only way or even that it's the right way. -- Matthieu Moy http://www-verimag.imag.fr/~moy/ ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 8/8] ref-filter: add 'ref-filter.h' 2015-05-30 17:53 ` [WIP/PATCH v4 8/8] ref-filter: add 'ref-filter.h' Karthik Nayak 2015-05-31 3:43 ` Eric Sunshine @ 2015-05-31 8:20 ` Christian Couder 2015-05-31 9:16 ` Karthik Nayak 1 sibling, 1 reply; 44+ messages in thread From: Christian Couder @ 2015-05-31 8:20 UTC (permalink / raw) To: Karthik Nayak; +Cc: git, Matthieu Moy On Sat, May 30, 2015 at 7:53 PM, Karthik Nayak <karthik.188@gmail.com> wrote: > > -struct ref_sort { > - struct ref_sort *next; > - int atom; /* index into used_atom array */ Where is this used_atom array? I searched but couldn't find it in the same file. > - unsigned reverse : 1; > -}; ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: [WIP/PATCH v4 8/8] ref-filter: add 'ref-filter.h' 2015-05-31 8:20 ` Christian Couder @ 2015-05-31 9:16 ` Karthik Nayak 0 siblings, 0 replies; 44+ messages in thread From: Karthik Nayak @ 2015-05-31 9:16 UTC (permalink / raw) To: Christian Couder; +Cc: git, Matthieu Moy On 05/31/2015 01:50 PM, Christian Couder wrote: > On Sat, May 30, 2015 at 7:53 PM, Karthik Nayak <karthik.188@gmail.com> wrote: >> >> -struct ref_sort { >> - struct ref_sort *next; >> - int atom; /* index into used_atom array */ > > Where is this used_atom array? > I searched but couldn't find it in the same file. > >> - unsigned reverse : 1; >> -}; It has been carried over since 9f613ddd, after which used_atom was removed, I will remove that comment, just causes confusion -- Regards, Karthik ^ permalink raw reply [flat|nested] 44+ messages in thread
end of thread, other threads:[~2015-06-01 19:36 UTC | newest] Thread overview: 44+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- [not found] <5569EF77.4010300@gmail.com> 2015-05-30 17:53 ` [WIP/PATCH v4 1/8] for-each-ref: extract helper functions out of grab_single_ref() Karthik Nayak 2015-05-31 2:58 ` Eric Sunshine 2015-05-31 8:11 ` Karthik Nayak 2015-05-31 17:34 ` Junio C Hamano 2015-05-31 17:48 ` Karthik Nayak 2015-05-31 20:39 ` Matthieu Moy 2015-06-01 14:39 ` Junio C Hamano 2015-05-30 17:53 ` [WIP/PATCH v4 2/8] for-each-ref: simplify code Karthik Nayak 2015-05-30 17:53 ` [WIP/PATCH v4 3/8] for-each-ref: rename 'refinfo' to 'ref_array_item' Karthik Nayak 2015-05-31 17:37 ` Junio C Hamano 2015-05-31 17:48 ` Karthik Nayak 2015-05-30 17:53 ` [WIP/PATCH v4 4/8] for-each-ref: introduce new structures for better organisation Karthik Nayak 2015-05-31 3:14 ` Eric Sunshine 2015-05-31 8:16 ` Karthik Nayak 2015-05-30 17:53 ` [WIP/PATCH v4 5/8] for-each-ref: introduce 'ref_filter_clear_data()' Karthik Nayak 2015-05-31 7:38 ` Christian Couder 2015-05-31 8:20 ` Karthik Nayak 2015-05-30 17:53 ` [WIP/PATCH v4 6/8] for-each-ref: rename some functions and make them public Karthik Nayak 2015-05-31 3:21 ` Eric Sunshine 2015-05-31 8:16 ` Karthik Nayak 2015-05-31 8:04 ` Christian Couder 2015-05-31 8:11 ` Christian Couder 2015-05-31 9:17 ` Karthik Nayak 2015-05-31 14:03 ` Christian Couder 2015-05-31 15:30 ` Karthik Nayak 2015-06-01 6:38 ` Matthieu Moy 2015-06-01 19:28 ` Karthik Nayak 2015-05-31 17:54 ` Junio C Hamano 2015-05-31 17:48 ` Junio C Hamano 2015-05-31 19:34 ` Karthik Nayak 2015-06-01 14:53 ` Junio C Hamano 2015-06-01 19:35 ` Karthik Nayak 2015-05-30 17:53 ` [WIP/PATCH v4 7/8] ref-filter: move code from 'for-each-ref' Karthik Nayak 2015-05-30 17:53 ` [WIP/PATCH v4 8/8] ref-filter: add 'ref-filter.h' Karthik Nayak 2015-05-31 3:43 ` Eric Sunshine 2015-05-31 8:19 ` Karthik Nayak 2015-05-31 8:29 ` Eric Sunshine 2015-05-31 9:12 ` Karthik Nayak 2015-05-31 20:46 ` Matthieu Moy 2015-05-31 20:50 ` Karthik Nayak 2015-05-31 22:34 ` Christian Couder 2015-06-01 6:47 ` Matthieu Moy 2015-05-31 8:20 ` Christian Couder 2015-05-31 9:16 ` Karthik Nayak
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).