Merge branch 'sb/leaks'
[gitweb.git] / builtin / for-each-ref.c
index 51798b48b54266fccd2f7b4687646d152ab70588..83f9cf91633a514e9301762fe774ee91be092848 100644 (file)
@@ -138,10 +138,8 @@ static int parse_atom(const char *atom, const char *ep)
        /* Add it in, including the deref prefix */
        at = used_atom_cnt;
        used_atom_cnt++;
-       used_atom = xrealloc(used_atom,
-                            (sizeof *used_atom) * used_atom_cnt);
-       used_atom_type = xrealloc(used_atom_type,
-                                 (sizeof(*used_atom_type) * 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 == '*')
@@ -180,11 +178,10 @@ static const char *find_next(const char *cp)
 static int verify_format(const char *format)
 {
        const char *cp, *sp;
-       static const char color_reset[] = "color:reset";
 
        need_color_reset_at_eol = 0;
        for (cp = format; *cp && (sp = find_next(cp)); ) {
-               const char *ep = strchr(sp, ')');
+               const char *color, *ep = strchr(sp, ')');
                int at;
 
                if (!ep)
@@ -193,8 +190,8 @@ static int verify_format(const char *format)
                at = parse_atom(sp + 2, ep);
                cp = ep + 1;
 
-               if (!memcmp(used_atom[at], "color:", 6))
-                       need_color_reset_at_eol = !!strcmp(used_atom[at], color_reset);
+               if (skip_prefix(used_atom[at], "color:", &color))
+                       need_color_reset_at_eol = !!strcmp(color, "reset");
        }
        return 0;
 }
@@ -283,18 +280,6 @@ static void grab_tag_values(struct atom_value *val, int deref, struct object *ob
        }
 }
 
-static int num_parents(struct commit *commit)
-{
-       struct commit_list *parents;
-       int i;
-
-       for (i = 0, parents = commit->parents;
-            parents;
-            parents = parents->next)
-               i++;
-       return i;
-}
-
 /* See grab_values */
 static void grab_commit_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
 {
@@ -315,12 +300,12 @@ static void grab_commit_values(struct atom_value *val, int deref, struct object
                }
                if (!strcmp(name, "numparent")) {
                        char *s = xmalloc(40);
-                       v->ul = num_parents(commit);
+                       v->ul = commit_list_count(commit->parents);
                        sprintf(s, "%lu", v->ul);
                        v->s = s;
                }
                else if (!strcmp(name, "parent")) {
-                       int num = num_parents(commit);
+                       int num = commit_list_count(commit->parents);
                        int i;
                        struct commit_list *parents;
                        char *s = xmalloc(41 * num + 1);
@@ -645,11 +630,12 @@ static void populate_value(struct refinfo *ref)
        unsigned long size;
        const unsigned char *tagged;
 
-       ref->value = xcalloc(sizeof(struct atom_value), used_atom_cnt);
+       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, unused1, 1, NULL);
+               ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING,
+                                            unused1, NULL);
                if (!ref->symref)
                        ref->symref = "";
        }
@@ -685,7 +671,8 @@ static void populate_value(struct refinfo *ref)
                } else if (starts_with(name, "color:")) {
                        char color[COLOR_MAXLEN] = "";
 
-                       color_parse(name + 6, "--format", color);
+                       if (color_parse(name + 6, color) < 0)
+                               die(_("unable to parse format"));
                        v->s = xstrdup(color);
                        continue;
                } else if (!strcmp(name, "flag")) {
@@ -707,7 +694,8 @@ static void populate_value(struct refinfo *ref)
                        const char *head;
                        unsigned char sha1[20];
 
-                       head = resolve_ref_unsafe("HEAD", sha1, 1, NULL);
+                       head = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
+                                                 sha1, NULL);
                        if (!strcmp(ref->refname, head))
                                v->s = "*";
                        else
@@ -728,7 +716,10 @@ static void populate_value(struct refinfo *ref)
                                 starts_with(name, "upstream")) {
                                char buf[40];
 
-                               stat_tracking_info(branch, &num_ours, &num_theirs);
+                               if (stat_tracking_info(branch, &num_ours,
+                                                      &num_theirs) != 1)
+                                       continue;
+
                                if (!num_ours && !num_theirs)
                                        v->s = "";
                                else if (!num_ours) {
@@ -746,7 +737,11 @@ static void populate_value(struct refinfo *ref)
                        } else if (!strcmp(formatp, "trackshort") &&
                                   starts_with(name, "upstream")) {
                                assert(branch);
-                               stat_tracking_info(branch, &num_ours, &num_theirs);
+
+                               if (stat_tracking_info(branch, &num_ours,
+                                                       &num_theirs) != 1)
+                                       continue;
+
                                if (!num_ours && !num_theirs)
                                        v->s = "=";
                                else if (!num_ours)
@@ -851,6 +846,11 @@ static int grab_single_ref(const char *refname, const unsigned char *sha1, int f
        struct refinfo *ref;
        int cnt;
 
+       if (flag & REF_BAD_NAME) {
+                 warning("ignoring ref with broken name %s", refname);
+                 return 0;
+       }
+
        if (*cb->grab_pattern) {
                const char **pattern;
                int namelen = strlen(refname);
@@ -864,7 +864,7 @@ static int grab_single_ref(const char *refname, const unsigned char *sha1, int f
                             refname[plen] == '/' ||
                             p[plen-1] == '/'))
                                break;
-                       if (!fnmatch(p, refname, FNM_PATHNAME))
+                       if (!wildmatch(p, refname, WM_PATHNAME, NULL))
                                break;
                }
                if (!*pattern)
@@ -882,8 +882,7 @@ static int grab_single_ref(const char *refname, const unsigned char *sha1, int f
        ref->flag = flag;
 
        cnt = cb->grab_cnt;
-       cb->grab_array = xrealloc(cb->grab_array,
-                                 sizeof(*cb->grab_array) * (cnt + 1));
+       REALLOC_ARRAY(cb->grab_array, cnt + 1);
        cb->grab_array[cnt++] = ref;
        cb->grab_cnt = cnt;
        return 0;
@@ -1019,7 +1018,8 @@ static void show_ref(struct refinfo *info, const char *format, int quote_style)
                struct atom_value resetv;
                char color[COLOR_MAXLEN] = "";
 
-               color_parse("reset", "--format", color);
+               if (color_parse("reset", color) < 0)
+                       die("BUG: couldn't parse 'reset' as a color");
                resetv.s = color;
                print_value(&resetv, quote_style);
        }
@@ -1060,7 +1060,7 @@ static int opt_parse_sort(const struct option *opt, const char *arg, int unset)
 }
 
 static char const * const for_each_ref_usage[] = {
-       N_("git for-each-ref [options] [<pattern>]"),
+       N_("git for-each-ref [<options>] [<pattern>]"),
        NULL
 };
 
@@ -1081,7 +1081,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
                OPT_BIT(0 , "python", &quote_style,
                        N_("quote placeholders suitably for python"), QUOTE_PYTHON),
                OPT_BIT(0 , "tcl",  &quote_style,
-                       N_("quote placeholders suitably for tcl"), QUOTE_TCL),
+                       N_("quote placeholders suitably for Tcl"), QUOTE_TCL),
 
                OPT_GROUP(""),
                OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")),