]> asedeno.scripts.mit.edu Git - git.git/blobdiff - pretty.c
gitignore(5): Allow "foo/" in ignore list to match directory "foo"
[git.git] / pretty.c
index 17a3010a6ecf315f75d7d6c970ac4b3d1877d168..b987ff245b310a6693dc69ba8c71ef2915da7864 100644 (file)
--- a/pretty.c
+++ b/pretty.c
@@ -292,7 +292,18 @@ static void format_person_part(struct strbuf *sb, char part,
        /* parse name */
        for (end = 0; end < len && msg[end] != '<'; end++)
                ; /* do nothing */
+       /*
+        * If it does not even have a '<' and '>', that is
+        * quite a bogus commit author and we discard it;
+        * this is in line with add_user_info() that is used
+        * in the normal codepath.  When end points at the '<'
+        * that we found, it should have matching '>' later,
+        * which means start (beginning of email address) must
+        * be strictly below len.
+        */
        start = end + 1;
+       if (start >= len - 1)
+               return;
        while (end > 0 && isspace(msg[end - 1]))
                end--;
        if (part == 'n') {      /* name */
@@ -300,11 +311,8 @@ static void format_person_part(struct strbuf *sb, char part,
                return;
        }
 
-       if (start >= len)
-               return;
-
        /* parse email */
-       for (end = start + 1; end < len && msg[end] != '>'; end++)
+       for (end = start; end < len && msg[end] != '>'; end++)
                ; /* do nothing */
 
        if (end >= len)
@@ -369,8 +377,30 @@ struct format_commit_context {
        struct chunk committer;
        struct chunk encoding;
        size_t body_off;
+
+       /* The following ones are relative to the result struct strbuf. */
+       struct chunk abbrev_commit_hash;
+       struct chunk abbrev_tree_hash;
+       struct chunk abbrev_parent_hashes;
 };
 
+static int add_again(struct strbuf *sb, struct chunk *chunk)
+{
+       if (chunk->len) {
+               strbuf_adddup(sb, chunk->off, chunk->len);
+               return 1;
+       }
+
+       /*
+        * We haven't seen this chunk before.  Our caller is surely
+        * going to add it the hard way now.  Remember the most likely
+        * start of the to-be-added chunk: the current end of the
+        * struct strbuf.
+        */
+       chunk->off = sb->len;
+       return 0;
+}
+
 static void parse_commit_header(struct format_commit_context *context)
 {
        const char *msg = context->commit->buffer;
@@ -390,7 +420,7 @@ static void parse_commit_header(struct format_commit_context *context)
                if (i == eol) {
                        state++;
                        /* strip empty lines */
-                       while (msg[eol + 1] == '\n')
+                       while (msg[eol] == '\n' && msg[eol + 1] == '\n')
                                eol++;
                } else if (!prefixcmp(msg + i, "author ")) {
                        context->author.off = i + 7;
@@ -403,6 +433,8 @@ static void parse_commit_header(struct format_commit_context *context)
                        context->encoding.len = eol - i - 9;
                }
                i = eol;
+               if (!msg[i])
+                       break;
        }
        context->body_off = i;
        context->commit_header_parsed = 1;
@@ -447,15 +479,21 @@ static void format_commit_item(struct strbuf *sb, const char *placeholder,
                strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
                return;
        case 'h':               /* abbreviated commit hash */
+               if (add_again(sb, &c->abbrev_commit_hash))
+                       return;
                strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
                                                     DEFAULT_ABBREV));
+               c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
                return;
        case 'T':               /* tree hash */
                strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
                return;
        case 't':               /* abbreviated tree hash */
+               if (add_again(sb, &c->abbrev_tree_hash))
+                       return;
                strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
                                                     DEFAULT_ABBREV));
+               c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
                return;
        case 'P':               /* parent hashes */
                for (p = commit->parents; p; p = p->next) {
@@ -465,12 +503,16 @@ static void format_commit_item(struct strbuf *sb, const char *placeholder,
                }
                return;
        case 'p':               /* abbreviated parent hashes */
+               if (add_again(sb, &c->abbrev_parent_hashes))
+                       return;
                for (p = commit->parents; p; p = p->next) {
                        if (p != commit->parents)
                                strbuf_addch(sb, ' ');
                        strbuf_addstr(sb, find_unique_abbrev(
                                        p->item->object.sha1, DEFAULT_ABBREV));
                }
+               c->abbrev_parent_hashes.len = sb->len -
+                                             c->abbrev_parent_hashes.off;
                return;
        case 'm':               /* left/right/bottom */
                strbuf_addch(sb, (commit->object.flags & BOUNDARY)