Imported Upstream version 2.12.1
[platform/upstream/git.git] / commit.c
index d1810c9..2cf8515 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -38,7 +38,7 @@ struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_n
        struct commit *c = lookup_commit_reference(sha1);
        if (!c)
                die(_("could not parse %s"), ref_name);
-       if (hashcmp(sha1, c->object.sha1)) {
+       if (hashcmp(sha1, c->object.oid.hash)) {
                warning(_("%s %s is not a commit!"),
                        ref_name, sha1_to_hex(sha1));
        }
@@ -147,7 +147,7 @@ struct commit_graft *read_graft_line(char *buf, int len)
        if ((len + 1) % entry_size)
                goto bad_graft_data;
        i = (len + 1) / entry_size - 1;
-       graft = xmalloc(sizeof(*graft) + GIT_SHA1_RAWSZ * i);
+       graft = xmalloc(st_add(sizeof(*graft), st_mult(GIT_SHA1_RAWSZ, i)));
        graft->nr_parent = i;
        if (get_oid_hex(buf, &graft->oid))
                goto bad_graft_data;
@@ -262,13 +262,13 @@ const void *get_commit_buffer(const struct commit *commit, unsigned long *sizep)
        if (!ret) {
                enum object_type type;
                unsigned long size;
-               ret = read_sha1_file(commit->object.sha1, &type, &size);
+               ret = read_sha1_file(commit->object.oid.hash, &type, &size);
                if (!ret)
                        die("cannot read commit object %s",
-                           sha1_to_hex(commit->object.sha1));
+                           oid_to_hex(&commit->object.oid));
                if (type != OBJ_COMMIT)
                        die("expected commit for %s, got %s",
-                           sha1_to_hex(commit->object.sha1), typename(type));
+                           oid_to_hex(&commit->object.oid), typename(type));
                if (sizep)
                        *sizep = size;
        }
@@ -327,22 +327,22 @@ int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long s
        tail += size;
        if (tail <= bufptr + tree_entry_len + 1 || memcmp(bufptr, "tree ", 5) ||
                        bufptr[tree_entry_len] != '\n')
-               return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
+               return error("bogus commit object %s", oid_to_hex(&item->object.oid));
        if (get_sha1_hex(bufptr + 5, parent.hash) < 0)
                return error("bad tree pointer in commit %s",
-                            sha1_to_hex(item->object.sha1));
+                            oid_to_hex(&item->object.oid));
        item->tree = lookup_tree(parent.hash);
        bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
        pptr = &item->parents;
 
-       graft = lookup_commit_graft(item->object.sha1);
+       graft = lookup_commit_graft(item->object.oid.hash);
        while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) {
                struct commit *new_parent;
 
                if (tail <= bufptr + parent_entry_len + 1 ||
                    get_sha1_hex(bufptr + 7, parent.hash) ||
                    bufptr[parent_entry_len] != '\n')
-                       return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
+                       return error("bad parents in commit %s", oid_to_hex(&item->object.oid));
                bufptr += parent_entry_len + 1;
                /*
                 * The clone is shallow if nr_parent < 0, and we must
@@ -380,15 +380,15 @@ int parse_commit_gently(struct commit *item, int quiet_on_missing)
                return -1;
        if (item->object.parsed)
                return 0;
-       buffer = read_sha1_file(item->object.sha1, &type, &size);
+       buffer = read_sha1_file(item->object.oid.hash, &type, &size);
        if (!buffer)
                return quiet_on_missing ? -1 :
                        error("Could not read %s",
-                            sha1_to_hex(item->object.sha1));
+                            oid_to_hex(&item->object.oid));
        if (type != OBJ_COMMIT) {
                free(buffer);
                return error("Object %s not a commit",
-                            sha1_to_hex(item->object.sha1));
+                            oid_to_hex(&item->object.oid));
        }
        ret = parse_commit_buffer(item, buffer, size);
        if (save_commit_buffer && !ret) {
@@ -403,7 +403,7 @@ void parse_commit_or_die(struct commit *item)
 {
        if (parse_commit(item))
                die("unable to parse commit %s",
-                   item ? sha1_to_hex(item->object.sha1) : "(null)");
+                   item ? oid_to_hex(&item->object.oid) : "(null)");
 }
 
 int find_commit_subject(const char *commit_buffer, const char **subject)
@@ -414,7 +414,7 @@ int find_commit_subject(const char *commit_buffer, const char **subject)
        while (*p && (*p != '\n' || p[1] != '\n'))
                p++;
        if (*p) {
-               p += 2;
+               p = skip_blank_lines(p + 2);
                for (eol = p; *eol && *eol != '\n'; eol++)
                        ; /* do nothing */
        } else
@@ -563,7 +563,7 @@ void clear_commit_marks_for_object_array(struct object_array *a, unsigned mark)
 
        for (i = 0; i < a->nr; i++) {
                object = a->objects[i].item;
-               commit = lookup_commit_reference_gently(object->sha1, 1);
+               commit = lookup_commit_reference_gently(object->oid.hash, 1);
                if (commit)
                        clear_commit_marks(commit, mark);
        }
@@ -903,7 +903,7 @@ static int remove_redundant(struct commit **array, int cnt)
 
        work = xcalloc(cnt, sizeof(*work));
        redundant = xcalloc(cnt, 1);
-       filled_index = xmalloc(sizeof(*filled_index) * (cnt - 1));
+       ALLOC_ARRAY(filled_index, cnt - 1);
 
        for (i = 0; i < cnt; i++)
                parse_commit(array[i]);
@@ -931,7 +931,7 @@ static int remove_redundant(struct commit **array, int cnt)
        }
 
        /* Now collect the result */
-       memcpy(work, array, sizeof(*array) * cnt);
+       COPY_ARRAY(work, array, cnt);
        for (i = filled = 0; i < cnt; i++)
                if (!redundant[i])
                        array[filled++] = work[i];
@@ -1092,9 +1092,14 @@ static int do_sign_commit(struct strbuf *buf, const char *keyid)
 {
        struct strbuf sig = STRBUF_INIT;
        int inspos, copypos;
+       const char *eoh;
 
        /* find the end of the header */
-       inspos = strstr(buf->buf, "\n\n") - buf->buf + 1;
+       eoh = strstr(buf->buf, "\n\n");
+       if (!eoh)
+               inspos = buf->len;
+       else
+               inspos = eoh - buf->buf + 1;
 
        if (!keyid || !*keyid)
                keyid = get_signing_key();
@@ -1206,7 +1211,7 @@ static void handle_signed_tag(struct commit *parent, struct commit_extra_header
        desc = merge_remote_util(parent);
        if (!desc || !desc->obj)
                return;
-       buf = read_sha1_file(desc->obj->sha1, &type, &size);
+       buf = read_sha1_file(desc->obj->oid.hash, &type, &size);
        if (!buf || type != OBJ_TAG)
                goto free_return;
        len = parse_signature(buf, size);
@@ -1506,9 +1511,9 @@ static int verify_utf8(struct strbuf *buf)
 }
 
 static const char commit_utf8_warn[] =
-"Warning: commit message did not conform to UTF-8.\n"
-"You may want to amend it after fixing the message, or set the config\n"
-"variable i18n.commitencoding to the encoding your project uses.\n";
+N_("Warning: commit message did not conform to UTF-8.\n"
+   "You may want to amend it after fixing the message, or set the config\n"
+   "variable i18n.commitencoding to the encoding your project uses.\n");
 
 int commit_tree_extended(const char *msg, size_t msg_len,
                         const unsigned char *tree,
@@ -1539,7 +1544,7 @@ int commit_tree_extended(const char *msg, size_t msg_len,
        while (parents) {
                struct commit *parent = pop_commit(&parents);
                strbuf_addf(&buffer, "parent %s\n",
-                           sha1_to_hex(parent->object.sha1));
+                           oid_to_hex(&parent->object.oid));
        }
 
        /* Person/date information */
@@ -1561,7 +1566,7 @@ int commit_tree_extended(const char *msg, size_t msg_len,
 
        /* And check the encoding */
        if (encoding_is_utf8 && !verify_utf8(&buffer))
-               fprintf(stderr, commit_utf8_warn);
+               fprintf(stderr, _(commit_utf8_warn));
 
        if (sign_commit && do_sign_commit(&buffer, sign_commit))
                return -1;
@@ -1571,6 +1576,15 @@ int commit_tree_extended(const char *msg, size_t msg_len,
        return result;
 }
 
+void set_merge_remote_desc(struct commit *commit,
+                          const char *name, struct object *obj)
+{
+       struct merge_remote_desc *desc;
+       FLEX_ALLOC_STR(desc, name, name);
+       desc->obj = obj;
+       commit->util = desc;
+}
+
 struct commit *get_merge_parent(const char *name)
 {
        struct object *obj;
@@ -1580,13 +1594,8 @@ struct commit *get_merge_parent(const char *name)
                return NULL;
        obj = parse_object(oid.hash);
        commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
-       if (commit && !commit->util) {
-               struct merge_remote_desc *desc;
-               desc = xmalloc(sizeof(*desc));
-               desc->obj = obj;
-               desc->name = strdup(name);
-               commit->util = desc;
-       }
+       if (commit && !commit->util)
+               set_merge_remote_desc(commit, name, obj);
        return commit;
 }
 
@@ -1617,16 +1626,6 @@ struct commit_list **commit_list_append(struct commit *commit,
        return &new->next;
 }
 
-void print_commit_list(struct commit_list *list,
-                      const char *format_cur,
-                      const char *format_last)
-{
-       for ( ; list; list = list->next) {
-               const char *format = list->next ? format_cur : format_last;
-               printf(format, sha1_to_hex(list->item->object.sha1));
-       }
-}
-
 const char *find_commit_header(const char *msg, const char *key, size_t *out_len)
 {
        int key_len = strlen(key);
@@ -1650,7 +1649,7 @@ const char *find_commit_header(const char *msg, const char *key, size_t *out_len
 }
 
 /*
- * Inspect sb and determine the true "end" of the log message, in
+ * Inspect the given string and determine the true "end" of the log message, in
  * order to find where to put a new Signed-off-by: line.  Ignored are
  * trailing comment lines and blank lines, and also the traditional
  * "Conflicts:" block that is not commented out, so that we can use
@@ -1660,37 +1659,37 @@ const char *find_commit_header(const char *msg, const char *key, size_t *out_len
  * Returns the number of bytes from the tail to ignore, to be fed as
  * the second parameter to append_signoff().
  */
-int ignore_non_trailer(struct strbuf *sb)
+int ignore_non_trailer(const char *buf, size_t len)
 {
        int boc = 0;
        int bol = 0;
        int in_old_conflicts_block = 0;
 
-       while (bol < sb->len) {
-               char *next_line;
+       while (bol < len) {
+               const char *next_line = memchr(buf + bol, '\n', len - bol);
 
-               if (!(next_line = memchr(sb->buf + bol, '\n', sb->len - bol)))
-                       next_line = sb->buf + sb->len;
+               if (!next_line)
+                       next_line = buf + len;
                else
                        next_line++;
 
-               if (sb->buf[bol] == comment_line_char || sb->buf[bol] == '\n') {
+               if (buf[bol] == comment_line_char || buf[bol] == '\n') {
                        /* is this the first of the run of comments? */
                        if (!boc)
                                boc = bol;
                        /* otherwise, it is just continuing */
-               } else if (starts_with(sb->buf + bol, "Conflicts:\n")) {
+               } else if (starts_with(buf + bol, "Conflicts:\n")) {
                        in_old_conflicts_block = 1;
                        if (!boc)
                                boc = bol;
-               } else if (in_old_conflicts_block && sb->buf[bol] == '\t') {
+               } else if (in_old_conflicts_block && buf[bol] == '\t') {
                        ; /* a pathname in the conflicts block */
                } else if (boc) {
                        /* the previous was not trailing comment */
                        boc = 0;
                        in_old_conflicts_block = 0;
                }
-               bol = next_line - sb->buf;
+               bol = next_line - buf;
        }
-       return boc ? sb->len - boc : 0;
+       return boc ? len - boc : 0;
 }