Code

git-tag(1): -v option is a subcommand; fix code block
[git.git] / commit.c
index 9ac533c85ab25f54cda97a0976f21a3232cf72ca..aa7059c1c6dd7650aa0623443372cfa804a0787c 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -4,6 +4,8 @@
 #include "pkt-line.h"
 #include "utf8.h"
 #include "interpolate.h"
+#include "diff.h"
+#include "revision.h"
 
 int save_commit_buffer = 1;
 
@@ -96,12 +98,8 @@ struct commit *lookup_commit_reference(const unsigned char *sha1)
 struct commit *lookup_commit(const unsigned char *sha1)
 {
        struct object *obj = lookup_object(sha1);
-       if (!obj) {
-               struct commit *ret = alloc_commit_node();
-               created_object(sha1, &ret->object);
-               ret->object.type = OBJ_COMMIT;
-               return ret;
-       }
+       if (!obj)
+               return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
        if (!obj->type)
                obj->type = OBJ_COMMIT;
        return check_commit(obj, sha1, 0);
@@ -528,7 +526,7 @@ static int add_rfc2047(char *buf, const char *line, int len,
 }
 
 static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf,
-                        const char *line, int relative_date,
+                        const char *line, enum date_mode dmode,
                         const char *encoding)
 {
        char *date;
@@ -571,7 +569,7 @@ static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf,
        switch (fmt) {
        case CMIT_FMT_MEDIUM:
                ret += sprintf(buf + ret, "Date:   %s\n",
-                              show_date(time, tz, relative_date));
+                              show_date(time, tz, dmode));
                break;
        case CMIT_FMT_EMAIL:
                ret += sprintf(buf + ret, "Date: %s\n",
@@ -579,7 +577,7 @@ static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf,
                break;
        case CMIT_FMT_FULLER:
                ret += sprintf(buf + ret, "%sDate: %s\n", what,
-                              show_date(time, tz, relative_date));
+                              show_date(time, tz, dmode));
                break;
        default:
                /* notin' */
@@ -654,6 +652,7 @@ 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;
@@ -661,8 +660,10 @@ static char *replace_encoding_header(char *buf, const char *encoding)
        int need_len;
        int buflen = strlen(buf) + 1;
 
-       if (!encoding_header)
-               return buf; /* should not happen but be defensive */
+       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)
@@ -717,14 +718,6 @@ static char *logmsg_reencode(const struct commit *commit,
        return out;
 }
 
-static char *xstrndup(const char *text, int len)
-{
-       char *result = xmalloc(len + 1);
-       memcpy(result, text, len);
-       result[len] = '\0';
-       return result;
-}
-
 static void fill_person(struct interp *table, const char *msg, int len)
 {
        int start, end, tz = 0;
@@ -805,7 +798,8 @@ static long format_commit_message(const struct commit *commit,
                { "%Cgreen" },  /* green */
                { "%Cblue" },   /* blue */
                { "%Creset" },  /* reset color */
-               { "%n" }        /* newline */
+               { "%n" },       /* newline */
+               { "%m" },       /* left/right/bottom */
        };
        enum interp_index {
                IHASH = 0, IHASH_ABBREV,
@@ -821,14 +815,15 @@ static long format_commit_message(const struct commit *commit,
                ISUBJECT,
                IBODY,
                IRED, IGREEN, IBLUE, IRESET_COLOR,
-               INEWLINE
+               INEWLINE,
+               ILEFT_RIGHT,
        };
        struct commit_list *p;
        char parents[1024];
        int i;
        enum { HEADER, SUBJECT, BODY } state;
 
-       if (INEWLINE + 1 != ARRAY_SIZE(table))
+       if (ILEFT_RIGHT + 1 != ARRAY_SIZE(table))
                die("invalid interp table!");
 
        /* these are independent of the commit */
@@ -849,6 +844,12 @@ static long format_commit_message(const struct commit *commit,
        interp_set_entry(table, ITREE_ABBREV,
                        find_unique_abbrev(commit->tree->object.sha1,
                                DEFAULT_ABBREV));
+       interp_set_entry(table, ILEFT_RIGHT,
+                        (commit->object.flags & BOUNDARY)
+                        ? "-"
+                        : (commit->object.flags & SYMMETRIC_LEFT)
+                        ? "<"
+                        : ">");
 
        parents[1] = 0;
        for (i = 0, p = commit->parents;
@@ -888,7 +889,8 @@ static long format_commit_message(const struct commit *commit,
                        fill_person(table + ICOMMITTER_NAME,
                                        msg + i + 10, eol - i - 10);
                else if (!prefixcmp(msg + i, "encoding "))
-                       table[IENCODING].value = xstrndup(msg + i, eol - i);
+                       table[IENCODING].value =
+                               xstrndup(msg + i + 9, eol - i - 9);
                i = eol;
        }
        if (msg[i])
@@ -909,7 +911,7 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt,
                                  char *buf, unsigned long space,
                                  int abbrev, const char *subject,
                                  const char *after_subject,
-                                 int relative_date)
+                                 enum date_mode dmode)
 {
        int hdr = 1, body = 0, seen_title = 0;
        unsigned long offset = 0;
@@ -1013,14 +1015,14 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt,
                                offset += add_user_info("Author", fmt,
                                                        buf + offset,
                                                        line + 7,
-                                                       relative_date,
+                                                       dmode,
                                                        encoding);
                        if (!memcmp(line, "committer ", 10) &&
                            (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER))
                                offset += add_user_info("Commit", fmt,
                                                        buf + offset,
                                                        line + 10,
-                                                       relative_date,
+                                                       dmode,
                                                        encoding);
                        continue;
                }