Code

Add tests for parse-options.c
[git.git] / builtin-for-each-ref.c
index 00afe8931768b96ec3f8baf72ab9221a4c2426b6..c74ef2800c839a5537707c5c64aa55acb2a9efad 100644 (file)
@@ -43,7 +43,7 @@ static struct {
        { "objectsize", FIELD_ULONG },
        { "objectname" },
        { "tree" },
-       { "parent" }, /* NEEDSWORK: how to address 2nd and later parents? */
+       { "parent" },
        { "numparent", FIELD_ULONG },
        { "object" },
        { "type" },
@@ -87,7 +87,6 @@ static int used_atom_cnt, sort_atom_limit, need_tagged;
 static int parse_atom(const char *atom, const char *ep)
 {
        const char *sp;
-       char *n;
        int i, at;
 
        sp = atom;
@@ -129,10 +128,7 @@ static int parse_atom(const char *atom, const char *ep)
                             (sizeof *used_atom) * used_atom_cnt);
        used_atom_type = xrealloc(used_atom_type,
                                  (sizeof(*used_atom_type) * used_atom_cnt));
-       n = xmalloc(ep - atom + 1);
-       memcpy(n, atom, ep - atom);
-       n[ep-atom] = 0;
-       used_atom[at] = n;
+       used_atom[at] = xmemdupz(atom, ep - atom);
        used_atom_type[at] = valid_atom[i].cmp_type;
        return at;
 }
@@ -271,24 +267,26 @@ 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);
                        sprintf(s, "%lu", v->ul);
                        v->s = s;
-                       v->ul = num_parents(commit);
                }
                else if (!strcmp(name, "parent")) {
                        int num = num_parents(commit);
                        int i;
                        struct commit_list *parents;
-                       char *s = xmalloc(42 * num);
+                       char *s = xmalloc(41 * num + 1);
                        v->s = s;
                        for (i = 0, parents = commit->parents;
                             parents;
-                            parents = parents->next, i = i + 42) {
+                            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';
                }
        }
 }
@@ -314,46 +312,28 @@ static const char *find_wholine(const char *who, int wholen, const char *buf, un
 static const char *copy_line(const char *buf)
 {
        const char *eol = strchr(buf, '\n');
-       char *line;
-       int len;
        if (!eol)
                return "";
-       len = eol - buf;
-       line = xmalloc(len + 1);
-       memcpy(line, buf, len);
-       line[len] = 0;
-       return line;
+       return xmemdupz(buf, eol - buf);
 }
 
 static const char *copy_name(const char *buf)
 {
-       const char *eol = strchr(buf, '\n');
-       const char *eoname = strstr(buf, " <");
-       char *line;
-       int len;
-       if (!(eoname && eol && eoname < eol))
-               return "";
-       len = eoname - buf;
-       line = xmalloc(len + 1);
-       memcpy(line, buf, len);
-       line[len] = 0;
-       return line;
+       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 = strchr(email, '>');
-       char *line;
-       int len;
        if (!email || !eoemail)
                return "";
-       eoemail++;
-       len = eoemail - email;
-       line = xmalloc(len + 1);
-       memcpy(line, email, len);
-       line[len] = 0;
-       return line;
+       return xmemdupz(email, eoemail + 1 - email);
 }
 
 static void grab_date(const char *buf, struct atom_value *v, const char *atomname)