builtin / commit-tree.con commit Merge branch 'sg/cvs-tests-with-x' (9ecfd98)
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include "cache.h"
   7#include "config.h"
   8#include "commit.h"
   9#include "tree.h"
  10#include "builtin.h"
  11#include "utf8.h"
  12#include "gpg-interface.h"
  13
  14static const char commit_tree_usage[] = "git commit-tree [(-p <sha1>)...] [-S[<keyid>]] [-m <message>] [-F <file>] <sha1>";
  15
  16static const char *sign_commit;
  17
  18static void new_parent(struct commit *parent, struct commit_list **parents_p)
  19{
  20        struct object_id *oid = &parent->object.oid;
  21        struct commit_list *parents;
  22        for (parents = *parents_p; parents; parents = parents->next) {
  23                if (parents->item == parent) {
  24                        error("duplicate parent %s ignored", oid_to_hex(oid));
  25                        return;
  26                }
  27                parents_p = &parents->next;
  28        }
  29        commit_list_insert(parent, parents_p);
  30}
  31
  32static int commit_tree_config(const char *var, const char *value, void *cb)
  33{
  34        int status = git_gpg_config(var, value, NULL);
  35        if (status)
  36                return status;
  37        return git_default_config(var, value, cb);
  38}
  39
  40int cmd_commit_tree(int argc, const char **argv, const char *prefix)
  41{
  42        int i, got_tree = 0;
  43        struct commit_list *parents = NULL;
  44        struct object_id tree_oid;
  45        struct object_id commit_oid;
  46        struct strbuf buffer = STRBUF_INIT;
  47
  48        git_config(commit_tree_config, NULL);
  49
  50        if (argc < 2 || !strcmp(argv[1], "-h"))
  51                usage(commit_tree_usage);
  52
  53        for (i = 1; i < argc; i++) {
  54                const char *arg = argv[i];
  55                if (!strcmp(arg, "-p")) {
  56                        struct object_id oid;
  57                        if (argc <= ++i)
  58                                usage(commit_tree_usage);
  59                        if (get_oid_commit(argv[i], &oid))
  60                                die("Not a valid object name %s", argv[i]);
  61                        assert_sha1_type(oid.hash, OBJ_COMMIT);
  62                        new_parent(lookup_commit(&oid), &parents);
  63                        continue;
  64                }
  65
  66                if (skip_prefix(arg, "-S", &sign_commit))
  67                        continue;
  68
  69                if (!strcmp(arg, "--no-gpg-sign")) {
  70                        sign_commit = NULL;
  71                        continue;
  72                }
  73
  74                if (!strcmp(arg, "-m")) {
  75                        if (argc <= ++i)
  76                                usage(commit_tree_usage);
  77                        if (buffer.len)
  78                                strbuf_addch(&buffer, '\n');
  79                        strbuf_addstr(&buffer, argv[i]);
  80                        strbuf_complete_line(&buffer);
  81                        continue;
  82                }
  83
  84                if (!strcmp(arg, "-F")) {
  85                        int fd;
  86
  87                        if (argc <= ++i)
  88                                usage(commit_tree_usage);
  89                        if (buffer.len)
  90                                strbuf_addch(&buffer, '\n');
  91                        if (!strcmp(argv[i], "-"))
  92                                fd = 0;
  93                        else {
  94                                fd = open(argv[i], O_RDONLY);
  95                                if (fd < 0)
  96                                        die_errno("git commit-tree: failed to open '%s'",
  97                                                  argv[i]);
  98                        }
  99                        if (strbuf_read(&buffer, fd, 0) < 0)
 100                                die_errno("git commit-tree: failed to read '%s'",
 101                                          argv[i]);
 102                        if (fd && close(fd))
 103                                die_errno("git commit-tree: failed to close '%s'",
 104                                          argv[i]);
 105                        continue;
 106                }
 107
 108                if (get_oid_tree(arg, &tree_oid))
 109                        die("Not a valid object name %s", arg);
 110                if (got_tree)
 111                        die("Cannot give more than one trees");
 112                got_tree = 1;
 113        }
 114
 115        if (!buffer.len) {
 116                if (strbuf_read(&buffer, 0, 0) < 0)
 117                        die_errno("git commit-tree: failed to read");
 118        }
 119
 120        if (commit_tree(buffer.buf, buffer.len, &tree_oid, parents, &commit_oid,
 121                        NULL, sign_commit)) {
 122                strbuf_release(&buffer);
 123                return 1;
 124        }
 125
 126        printf("%s\n", oid_to_hex(&commit_oid));
 127        strbuf_release(&buffer);
 128        return 0;
 129}