builtin-commit-tree.con commit Merge branch 'master' into ph/strbuf (ddb95de)
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include "cache.h"
   7#include "commit.h"
   8#include "tree.h"
   9#include "builtin.h"
  10#include "utf8.h"
  11#include "strbuf.h"
  12
  13#define BLOCKING (1ul << 14)
  14
  15/*
  16 * FIXME! Share the code with "write-tree.c"
  17 */
  18static void check_valid(unsigned char *sha1, enum object_type expect)
  19{
  20        enum object_type type = sha1_object_info(sha1, NULL);
  21        if (type < 0)
  22                die("%s is not a valid object", sha1_to_hex(sha1));
  23        if (type != expect)
  24                die("%s is not a valid '%s' object", sha1_to_hex(sha1),
  25                    typename(expect));
  26}
  27
  28/*
  29 * Having more than two parents is not strange at all, and this is
  30 * how multi-way merges are represented.
  31 */
  32#define MAXPARENT (16)
  33static unsigned char parent_sha1[MAXPARENT][20];
  34
  35static const char commit_tree_usage[] = "git-commit-tree <sha1> [-p <sha1>]* < changelog";
  36
  37static int new_parent(int idx)
  38{
  39        int i;
  40        unsigned char *sha1 = parent_sha1[idx];
  41        for (i = 0; i < idx; i++) {
  42                if (!hashcmp(parent_sha1[i], sha1)) {
  43                        error("duplicate parent %s ignored", sha1_to_hex(sha1));
  44                        return 0;
  45                }
  46        }
  47        return 1;
  48}
  49
  50static const char commit_utf8_warn[] =
  51"Warning: commit message does not conform to UTF-8.\n"
  52"You may want to amend it after fixing the message, or set the config\n"
  53"variable i18n.commitencoding to the encoding your project uses.\n";
  54
  55int cmd_commit_tree(int argc, const char **argv, const char *prefix)
  56{
  57        int i;
  58        int parents = 0;
  59        unsigned char tree_sha1[20];
  60        unsigned char commit_sha1[20];
  61        struct strbuf buffer;
  62        int encoding_is_utf8;
  63
  64        git_config(git_default_config);
  65
  66        if (argc < 2)
  67                usage(commit_tree_usage);
  68        if (get_sha1(argv[1], tree_sha1))
  69                die("Not a valid object name %s", argv[1]);
  70
  71        check_valid(tree_sha1, OBJ_TREE);
  72        for (i = 2; i < argc; i += 2) {
  73                const char *a, *b;
  74                a = argv[i]; b = argv[i+1];
  75                if (!b || strcmp(a, "-p"))
  76                        usage(commit_tree_usage);
  77
  78                if (parents >= MAXPARENT)
  79                        die("Too many parents (%d max)", MAXPARENT);
  80                if (get_sha1(b, parent_sha1[parents]))
  81                        die("Not a valid object name %s", b);
  82                check_valid(parent_sha1[parents], OBJ_COMMIT);
  83                if (new_parent(parents))
  84                        parents++;
  85        }
  86
  87        /* Not having i18n.commitencoding is the same as having utf-8 */
  88        encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
  89
  90        strbuf_init(&buffer);
  91        strbuf_grow(&buffer, 8192); /* should avoid reallocs for the headers */
  92        strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree_sha1));
  93
  94        /*
  95         * NOTE! This ordering means that the same exact tree merged with a
  96         * different order of parents will be a _different_ changeset even
  97         * if everything else stays the same.
  98         */
  99        for (i = 0; i < parents; i++)
 100                strbuf_addf(&buffer, "parent %s\n", sha1_to_hex(parent_sha1[i]));
 101
 102        /* Person/date information */
 103        strbuf_addf(&buffer, "author %s\n", git_author_info(1));
 104        strbuf_addf(&buffer, "committer %s\n", git_committer_info(1));
 105        if (!encoding_is_utf8)
 106                strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
 107        strbuf_addch(&buffer, '\n');
 108
 109        /* And add the comment */
 110        if (strbuf_read(&buffer, 0) < 0)
 111                die("git-commit-tree: read returned %s", strerror(errno));
 112
 113        /* And check the encoding */
 114        if (encoding_is_utf8 && !is_utf8(buffer.buf))
 115                fprintf(stderr, commit_utf8_warn);
 116
 117        if (!write_sha1_file(buffer.buf, buffer.len, commit_type, commit_sha1)) {
 118                printf("%s\n", sha1_to_hex(commit_sha1));
 119                return 0;
 120        }
 121        else
 122                return 1;
 123}