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 <tree> [(-p <parent>)...]"),
19 N_("git commit-tree [(-p <parent>)...] [-S[<keyid>]] [(-m <message>)...]\n"
20 " [(-F <file>)...] <tree>"),
24 static const char *sign_commit
;
26 static void new_parent(struct commit
*parent
, struct commit_list
**parents_p
)
28 struct object_id
*oid
= &parent
->object
.oid
;
29 struct commit_list
*parents
;
30 for (parents
= *parents_p
; parents
; parents
= parents
->next
) {
31 if (parents
->item
== parent
) {
32 error(_("duplicate parent %s ignored"), oid_to_hex(oid
));
35 parents_p
= &parents
->next
;
37 commit_list_insert(parent
, parents_p
);
40 static int commit_tree_config(const char *var
, const char *value
, void *cb
)
42 int status
= git_gpg_config(var
, value
, NULL
);
45 return git_default_config(var
, value
, cb
);
48 static int parse_parent_arg_callback(const struct option
*opt
,
49 const char *arg
, int unset
)
52 struct commit_list
**parents
= opt
->value
;
54 BUG_ON_OPT_NEG_NOARG(unset
, arg
);
56 if (get_oid_commit(arg
, &oid
))
57 die(_("not a valid object name %s"), arg
);
59 assert_oid_type(&oid
, OBJ_COMMIT
);
60 new_parent(lookup_commit(the_repository
, &oid
), parents
);
64 static int parse_message_arg_callback(const struct option
*opt
,
65 const char *arg
, int unset
)
67 struct strbuf
*buf
= opt
->value
;
69 BUG_ON_OPT_NEG_NOARG(unset
, arg
);
72 strbuf_addch(buf
, '\n');
73 strbuf_addstr(buf
, arg
);
74 strbuf_complete_line(buf
);
79 static int parse_file_arg_callback(const struct option
*opt
,
80 const char *arg
, int unset
)
83 struct strbuf
*buf
= opt
->value
;
85 BUG_ON_OPT_NEG_NOARG(unset
, arg
);
88 strbuf_addch(buf
, '\n');
89 if (!strcmp(arg
, "-"))
92 fd
= xopen(arg
, O_RDONLY
);
94 if (strbuf_read(buf
, fd
, 0) < 0)
95 die_errno(_("git commit-tree: failed to read '%s'"), arg
);
97 die_errno(_("git commit-tree: failed to close '%s'"), arg
);
102 int cmd_commit_tree(int argc
, const char **argv
, const char *prefix
)
104 static struct strbuf buffer
= STRBUF_INIT
;
105 struct commit_list
*parents
= NULL
;
106 struct object_id tree_oid
;
107 struct object_id commit_oid
;
109 struct option options
[] = {
110 OPT_CALLBACK_F('p', NULL
, &parents
, N_("parent"),
111 N_("id of a parent commit object"), PARSE_OPT_NONEG
,
112 parse_parent_arg_callback
),
113 OPT_CALLBACK_F('m', NULL
, &buffer
, N_("message"),
114 N_("commit message"), PARSE_OPT_NONEG
,
115 parse_message_arg_callback
),
116 OPT_CALLBACK_F('F', NULL
, &buffer
, N_("file"),
117 N_("read commit log message from file"), PARSE_OPT_NONEG
,
118 parse_file_arg_callback
),
119 { OPTION_STRING
, 'S', "gpg-sign", &sign_commit
, N_("key-id"),
120 N_("GPG sign commit"), PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
124 git_config(commit_tree_config
, NULL
);
126 if (argc
< 2 || !strcmp(argv
[1], "-h"))
127 usage_with_options(commit_tree_usage
, options
);
129 argc
= parse_options(argc
, argv
, prefix
, options
, commit_tree_usage
, 0);
132 die(_("must give exactly one tree"));
134 if (get_oid_tree(argv
[0], &tree_oid
))
135 die(_("not a valid object name %s"), argv
[0]);
138 if (strbuf_read(&buffer
, 0, 0) < 0)
139 die_errno(_("git commit-tree: failed to read"));
142 if (commit_tree(buffer
.buf
, buffer
.len
, &tree_oid
, parents
, &commit_oid
,
143 NULL
, sign_commit
)) {
144 strbuf_release(&buffer
);
148 printf("%s\n", oid_to_hex(&commit_oid
));
149 strbuf_release(&buffer
);