Merge branch 'jc/am-parseopt-fix'
[git.git] / builtin / commit-tree.c
blob02625e71761dbf8d25f063b5ff4835e5eb4bf477
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "builtin.h"
7 #include "config.h"
8 #include "gettext.h"
9 #include "hex.h"
10 #include "object-name.h"
11 #include "object-store-ll.h"
12 #include "repository.h"
13 #include "commit.h"
14 #include "tree.h"
15 #include "utf8.h"
16 #include "gpg-interface.h"
17 #include "parse-options.h"
19 static const char * const commit_tree_usage[] = {
20 N_("git commit-tree <tree> [(-p <parent>)...]"),
21 N_("git commit-tree [(-p <parent>)...] [-S[<keyid>]] [(-m <message>)...]\n"
22 " [(-F <file>)...] <tree>"),
23 NULL
26 static const char *sign_commit;
28 static void new_parent(struct commit *parent, struct commit_list **parents_p)
30 struct object_id *oid = &parent->object.oid;
31 struct commit_list *parents;
32 for (parents = *parents_p; parents; parents = parents->next) {
33 if (parents->item == parent) {
34 error(_("duplicate parent %s ignored"), oid_to_hex(oid));
35 return;
37 parents_p = &parents->next;
39 commit_list_insert(parent, parents_p);
42 static int parse_parent_arg_callback(const struct option *opt,
43 const char *arg, int unset)
45 struct object_id oid;
46 struct commit_list **parents = opt->value;
48 BUG_ON_OPT_NEG_NOARG(unset, arg);
50 if (repo_get_oid_commit(the_repository, arg, &oid))
51 die(_("not a valid object name %s"), arg);
53 assert_oid_type(&oid, OBJ_COMMIT);
54 new_parent(lookup_commit(the_repository, &oid), parents);
55 return 0;
58 static int parse_message_arg_callback(const struct option *opt,
59 const char *arg, int unset)
61 struct strbuf *buf = opt->value;
63 BUG_ON_OPT_NEG_NOARG(unset, arg);
65 if (buf->len)
66 strbuf_addch(buf, '\n');
67 strbuf_addstr(buf, arg);
68 strbuf_complete_line(buf);
70 return 0;
73 static int parse_file_arg_callback(const struct option *opt,
74 const char *arg, int unset)
76 int fd;
77 struct strbuf *buf = opt->value;
79 BUG_ON_OPT_NEG_NOARG(unset, arg);
81 if (buf->len)
82 strbuf_addch(buf, '\n');
83 if (!strcmp(arg, "-"))
84 fd = 0;
85 else {
86 fd = xopen(arg, O_RDONLY);
88 if (strbuf_read(buf, fd, 0) < 0)
89 die_errno(_("git commit-tree: failed to read '%s'"), arg);
90 if (fd && close(fd))
91 die_errno(_("git commit-tree: failed to close '%s'"), arg);
93 return 0;
96 int cmd_commit_tree(int argc, const char **argv, const char *prefix)
98 static struct strbuf buffer = STRBUF_INIT;
99 struct commit_list *parents = NULL;
100 struct object_id tree_oid;
101 struct object_id commit_oid;
103 struct option options[] = {
104 OPT_CALLBACK_F('p', NULL, &parents, N_("parent"),
105 N_("id of a parent commit object"), PARSE_OPT_NONEG,
106 parse_parent_arg_callback),
107 OPT_CALLBACK_F('m', NULL, &buffer, N_("message"),
108 N_("commit message"), PARSE_OPT_NONEG,
109 parse_message_arg_callback),
110 OPT_CALLBACK_F('F', NULL, &buffer, N_("file"),
111 N_("read commit log message from file"), PARSE_OPT_NONEG,
112 parse_file_arg_callback),
113 { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
114 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
115 OPT_END()
118 git_config(git_default_config, NULL);
120 if (argc < 2 || !strcmp(argv[1], "-h"))
121 usage_with_options(commit_tree_usage, options);
123 argc = parse_options(argc, argv, prefix, options, commit_tree_usage, 0);
125 if (argc != 1)
126 die(_("must give exactly one tree"));
128 if (repo_get_oid_tree(the_repository, argv[0], &tree_oid))
129 die(_("not a valid object name %s"), argv[0]);
131 if (!buffer.len) {
132 if (strbuf_read(&buffer, 0, 0) < 0)
133 die_errno(_("git commit-tree: failed to read"));
136 if (commit_tree(buffer.buf, buffer.len, &tree_oid, parents, &commit_oid,
137 NULL, sign_commit)) {
138 strbuf_release(&buffer);
139 return 1;
142 printf("%s\n", oid_to_hex(&commit_oid));
143 strbuf_release(&buffer);
144 return 0;