2  * GIT - The information manager from hell
 
   4  * Copyright (C) Linus Torvalds, 2005
 
   8 #include "object-store.h"
 
   9 #include "repository.h"
 
  14 #include "gpg-interface.h"
 
  15 #include "parse-options.h"
 
  17 static const char * const commit_tree_usage[] = {
 
  18         N_("git commit-tree [(-p <parent>)...] [-S[<keyid>]] [(-m <message>)...] "
 
  19                 "[(-F <file>)...] <tree>"),
 
  23 static const char *sign_commit;
 
  25 static void new_parent(struct commit *parent, struct commit_list **parents_p)
 
  27         struct object_id *oid = &parent->object.oid;
 
  28         struct commit_list *parents;
 
  29         for (parents = *parents_p; parents; parents = parents->next) {
 
  30                 if (parents->item == parent) {
 
  31                         error(_("duplicate parent %s ignored"), oid_to_hex(oid));
 
  34                 parents_p = &parents->next;
 
  36         commit_list_insert(parent, parents_p);
 
  39 static int commit_tree_config(const char *var, const char *value, void *cb)
 
  41         int status = git_gpg_config(var, value, NULL);
 
  44         return git_default_config(var, value, cb);
 
  47 static int parse_parent_arg_callback(const struct option *opt,
 
  48                 const char *arg, int unset)
 
  51         struct commit_list **parents = opt->value;
 
  53         BUG_ON_OPT_NEG_NOARG(unset, arg);
 
  55         if (get_oid_commit(arg, &oid))
 
  56                 die(_("not a valid object name %s"), arg);
 
  58         assert_oid_type(&oid, OBJ_COMMIT);
 
  59         new_parent(lookup_commit(the_repository, &oid), parents);
 
  63 static int parse_message_arg_callback(const struct option *opt,
 
  64                 const char *arg, int unset)
 
  66         struct strbuf *buf = opt->value;
 
  68         BUG_ON_OPT_NEG_NOARG(unset, arg);
 
  71                 strbuf_addch(buf, '\n');
 
  72         strbuf_addstr(buf, arg);
 
  73         strbuf_complete_line(buf);
 
  78 static int parse_file_arg_callback(const struct option *opt,
 
  79                 const char *arg, int unset)
 
  82         struct strbuf *buf = opt->value;
 
  84         BUG_ON_OPT_NEG_NOARG(unset, arg);
 
  87                 strbuf_addch(buf, '\n');
 
  88         if (!strcmp(arg, "-"))
 
  91                 fd = open(arg, O_RDONLY);
 
  93                         die_errno(_("git commit-tree: failed to open '%s'"), arg);
 
  95         if (strbuf_read(buf, fd, 0) < 0)
 
  96                 die_errno(_("git commit-tree: failed to read '%s'"), arg);
 
  98                 die_errno(_("git commit-tree: failed to close '%s'"), arg);
 
 103 int cmd_commit_tree(int argc, const char **argv, const char *prefix)
 
 105         static struct strbuf buffer = STRBUF_INIT;
 
 106         struct commit_list *parents = NULL;
 
 107         struct object_id tree_oid;
 
 108         struct object_id commit_oid;
 
 110         struct option options[] = {
 
 111                 OPT_CALLBACK_F('p', NULL, &parents, N_("parent"),
 
 112                         N_("id of a parent commit object"), PARSE_OPT_NONEG,
 
 113                         parse_parent_arg_callback),
 
 114                 OPT_CALLBACK_F('m', NULL, &buffer, N_("message"),
 
 115                         N_("commit message"), PARSE_OPT_NONEG,
 
 116                         parse_message_arg_callback),
 
 117                 OPT_CALLBACK_F('F', NULL, &buffer, N_("file"),
 
 118                         N_("read commit log message from file"), PARSE_OPT_NONEG,
 
 119                         parse_file_arg_callback),
 
 120                 { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
 
 121                         N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
 
 125         git_config(commit_tree_config, NULL);
 
 127         if (argc < 2 || !strcmp(argv[1], "-h"))
 
 128                 usage_with_options(commit_tree_usage, options);
 
 130         argc = parse_options(argc, argv, prefix, options, commit_tree_usage, 0);
 
 133                 die(_("must give exactly one tree"));
 
 135         if (get_oid_tree(argv[0], &tree_oid))
 
 136                 die(_("not a valid object name %s"), argv[0]);
 
 139                 if (strbuf_read(&buffer, 0, 0) < 0)
 
 140                         die_errno(_("git commit-tree: failed to read"));
 
 143         if (commit_tree(buffer.buf, buffer.len, &tree_oid, parents, &commit_oid,
 
 144                         NULL, sign_commit)) {
 
 145                 strbuf_release(&buffer);
 
 149         printf("%s\n", oid_to_hex(&commit_oid));
 
 150         strbuf_release(&buffer);