]> asedeno.scripts.mit.edu Git - git.git/blobdiff - commit.c
Merge branch 'js/forkexec'
[git.git] / commit.c
index 6602e2c72fed4914ec7edaf8b6b59f33a88b2f2e..ac24266e935054c6909b8fbd513ffa937e2ae092 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -441,17 +441,22 @@ struct commit *pop_most_recent_commit(struct commit_list **list,
 
 void clear_commit_marks(struct commit *commit, unsigned int mark)
 {
-       struct commit_list *parents;
+       while (commit) {
+               struct commit_list *parents;
 
-       commit->object.flags &= ~mark;
-       parents = commit->parents;
-       while (parents) {
-               struct commit *parent = parents->item;
+               if (!(mark & commit->object.flags))
+                       return;
 
-               /* Have we already cleared this? */
-               if (mark & parent->object.flags)
-                       clear_commit_marks(parent, mark);
-               parents = parents->next;
+               commit->object.flags &= ~mark;
+
+               parents = commit->parents;
+               if (!parents)
+                       return;
+
+               while ((parents = parents->next))
+                       clear_commit_marks(parents->item, mark);
+
+               commit = commit->parents->item;
        }
 }
 
@@ -501,6 +506,7 @@ static void add_rfc2047(struct strbuf *sb, const char *line, int len,
        return;
 
 needquote:
+       strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
        strbuf_addf(sb, "=?%s?q?", encoding);
        for (i = last = 0; i < len; i++) {
                unsigned ch = line[i] & 0xFF;
@@ -520,14 +526,6 @@ needquote:
        strbuf_addstr(sb, "?=");
 }
 
-static unsigned long bound_rfc2047(unsigned long len, const char *encoding)
-{
-       /* upper bound of q encoded string of length 'len' */
-       unsigned long elen = strlen(encoding);
-
-       return len * 3 + elen + 100;
-}
-
 static void add_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
                         const char *line, enum date_mode dmode,
                         const char *encoding)
@@ -560,8 +558,7 @@ static void add_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb
                add_rfc2047(sb, line, display_name_length, encoding);
                strbuf_add(sb, name_tail, namelen - display_name_length);
                strbuf_addch(sb, '\n');
-       }
-       else {
+       } else {
                strbuf_addf(sb, "%s: %.*s%.*s\n", what,
                              (fmt == CMIT_FMT_FULLER) ? 4 : 0,
                              filler, namelen, line);
@@ -636,11 +633,7 @@ static char *get_header(const struct commit *commit, const char *key)
                if (eol - line > key_len &&
                    !strncmp(line, key, key_len) &&
                    line[key_len] == ' ') {
-                       int len = eol - line - key_len;
-                       char *ret = xmalloc(len);
-                       memcpy(ret, line + key_len + 1, len - 1);
-                       ret[len - 1] = '\0';
-                       return ret;
+                       return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
                }
                line = next;
        }
@@ -648,47 +641,34 @@ static char *get_header(const struct commit *commit, const char *key)
 
 static char *replace_encoding_header(char *buf, const char *encoding)
 {
-       char *encoding_header = strstr(buf, "\nencoding ");
-       char *header_end = strstr(buf, "\n\n");
-       char *end_of_encoding_header;
-       int encoding_header_pos;
-       int encoding_header_len;
-       int new_len;
-       int need_len;
-       int buflen = strlen(buf) + 1;
-
-       if (!header_end)
-               header_end = buf + buflen;
-       if (!encoding_header || encoding_header >= header_end)
-               return buf;
-       encoding_header++;
-       end_of_encoding_header = strchr(encoding_header, '\n');
-       if (!end_of_encoding_header)
+       struct strbuf tmp;
+       size_t start, len;
+       char *cp = buf;
+
+       /* guess if there is an encoding header before a \n\n */
+       while (strncmp(cp, "encoding ", strlen("encoding "))) {
+               cp = strchr(cp, '\n');
+               if (!cp || *++cp == '\n')
+                       return buf;
+       }
+       start = cp - buf;
+       cp = strchr(cp, '\n');
+       if (!cp)
                return buf; /* should not happen but be defensive */
-       end_of_encoding_header++;
-
-       encoding_header_len = end_of_encoding_header - encoding_header;
-       encoding_header_pos = encoding_header - buf;
+       len = cp + 1 - (buf + start);
 
+       strbuf_init(&tmp, 0);
+       strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
        if (is_encoding_utf8(encoding)) {
                /* we have re-coded to UTF-8; drop the header */
-               memmove(encoding_header, end_of_encoding_header,
-                       buflen - (encoding_header_pos + encoding_header_len));
-               return buf;
-       }
-       new_len = strlen(encoding);
-       need_len = new_len + strlen("encoding \n");
-       if (encoding_header_len < need_len) {
-               buf = xrealloc(buf, buflen + (need_len - encoding_header_len));
-               encoding_header = buf + encoding_header_pos;
-               end_of_encoding_header = encoding_header + encoding_header_len;
+               strbuf_remove(&tmp, start, len);
+       } else {
+               /* just replaces XXXX in 'encoding XXXX\n' */
+               strbuf_splice(&tmp, start + strlen("encoding "),
+                                         len - strlen("encoding \n"),
+                                         encoding, strlen(encoding));
        }
-       memmove(end_of_encoding_header + (need_len - encoding_header_len),
-               end_of_encoding_header,
-               buflen - (encoding_header_pos + encoding_header_len));
-       memcpy(encoding_header + 9, encoding, strlen(encoding));
-       encoding_header[9 + new_len] = '\n';
-       return buf;
+       return strbuf_detach(&tmp, NULL);
 }
 
 static char *logmsg_reencode(const struct commit *commit,
@@ -730,7 +710,7 @@ static void fill_person(struct interp *table, const char *msg, int len)
        start = end + 1;
        while (end > 0 && isspace(msg[end - 1]))
                end--;
-       table[0].value = xstrndup(msg, end);
+       table[0].value = xmemdupz(msg, end);
 
        if (start >= len)
                return;
@@ -742,7 +722,7 @@ static void fill_person(struct interp *table, const char *msg, int len)
        if (end >= len)
                return;
 
-       table[1].value = xstrndup(msg + start, end - start);
+       table[1].value = xmemdupz(msg + start, end - start);
 
        /* parse date */
        for (start = end + 1; start < len && isspace(msg[start]); start++)
@@ -753,7 +733,7 @@ static void fill_person(struct interp *table, const char *msg, int len)
        if (msg + start == ep)
                return;
 
-       table[5].value = xstrndup(msg + start, ep - (msg + start));
+       table[5].value = xmemdupz(msg + start, ep - (msg + start));
 
        /* parse tz */
        for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
@@ -880,7 +860,7 @@ void format_commit_message(const struct commit *commit,
                        ; /* do nothing */
 
                if (state == SUBJECT) {
-                       table[ISUBJECT].value = xstrndup(msg + i, eol - i);
+                       table[ISUBJECT].value = xmemdupz(msg + i, eol - i);
                        i = eol;
                }
                if (i == eol) {
@@ -896,14 +876,11 @@ void format_commit_message(const struct commit *commit,
                                        msg + i + 10, eol - i - 10);
                else if (!prefixcmp(msg + i, "encoding "))
                        table[IENCODING].value =
-                               xstrndup(msg + i + 9, eol - i - 9);
+                               xmemdupz(msg + i + 9, eol - i - 9);
                i = eol;
        }
        if (msg[i])
                table[IBODY].value = xstrdup(msg + i);
-       for (i = 0; i < ARRAY_SIZE(table); i++)
-               if (!table[i].value)
-                       interp_set_entry(table, i, "<unknown>");
 
        len = interpolate(sb->buf + sb->len, strbuf_avail(sb),
                                format, table, ARRAY_SIZE(table));
@@ -968,19 +945,12 @@ static void pp_header(enum cmit_fmt fmt,
                 * FULLER shows both authors and dates.
                 */
                if (!memcmp(line, "author ", 7)) {
-                       unsigned long len = linelen;
-                       if (fmt == CMIT_FMT_EMAIL)
-                               len = bound_rfc2047(linelen, encoding);
-                       strbuf_grow(sb, len + 80);
+                       strbuf_grow(sb, linelen + 80);
                        add_user_info("Author", fmt, sb, line + 7, dmode, encoding);
                }
-
                if (!memcmp(line, "committer ", 10) &&
                    (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
-                       unsigned long len = linelen;
-                       if (fmt == CMIT_FMT_EMAIL)
-                               len = bound_rfc2047(linelen, encoding);
-                       strbuf_grow(sb, len + 80);
+                       strbuf_grow(sb, linelen + 80);
                        add_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
                }
        }
@@ -995,7 +965,6 @@ static void pp_title_line(enum cmit_fmt fmt,
                          int plain_non_ascii)
 {
        struct strbuf title;
-       unsigned long len;
 
        strbuf_init(&title, 80);
 
@@ -1017,16 +986,7 @@ static void pp_title_line(enum cmit_fmt fmt,
                strbuf_add(&title, line, linelen);
        }
 
-       /* Enough slop for the MIME header and rfc2047 */
-       len = bound_rfc2047(title.len, encoding) + 1000;
-       if (subject)
-               len += strlen(subject);
-       if (after_subject)
-               len += strlen(after_subject);
-       if (encoding)
-               len += strlen(encoding);
-
-       strbuf_grow(sb, title.len + len);
+       strbuf_grow(sb, title.len + 1024);
        if (subject) {
                strbuf_addstr(sb, subject);
                add_rfc2047(sb, title.buf, title.len, encoding);