]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/commit-tree.c
parse-options: simplify positivation handling
[thirdparty/git.git] / builtin / commit-tree.c
1 /*
2 * GIT - The information manager from hell
3 *
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"
18
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
24 };
25
26 static const char *sign_commit;
27
28 static void new_parent(struct commit *parent, struct commit_list **parents_p)
29 {
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;
36 }
37 parents_p = &parents->next;
38 }
39 commit_list_insert(parent, parents_p);
40 }
41
42 static int parse_parent_arg_callback(const struct option *opt,
43 const char *arg, int unset)
44 {
45 struct object_id oid;
46 struct commit_list **parents = opt->value;
47
48 BUG_ON_OPT_NEG_NOARG(unset, arg);
49
50 if (repo_get_oid_commit(the_repository, arg, &oid))
51 die(_("not a valid object name %s"), arg);
52
53 assert_oid_type(&oid, OBJ_COMMIT);
54 new_parent(lookup_commit(the_repository, &oid), parents);
55 return 0;
56 }
57
58 static int parse_message_arg_callback(const struct option *opt,
59 const char *arg, int unset)
60 {
61 struct strbuf *buf = opt->value;
62
63 BUG_ON_OPT_NEG_NOARG(unset, arg);
64
65 if (buf->len)
66 strbuf_addch(buf, '\n');
67 strbuf_addstr(buf, arg);
68 strbuf_complete_line(buf);
69
70 return 0;
71 }
72
73 static int parse_file_arg_callback(const struct option *opt,
74 const char *arg, int unset)
75 {
76 int fd;
77 struct strbuf *buf = opt->value;
78
79 BUG_ON_OPT_NEG_NOARG(unset, arg);
80
81 if (buf->len)
82 strbuf_addch(buf, '\n');
83 if (!strcmp(arg, "-"))
84 fd = 0;
85 else {
86 fd = xopen(arg, O_RDONLY);
87 }
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);
92
93 return 0;
94 }
95
96 int cmd_commit_tree(int argc, const char **argv, const char *prefix)
97 {
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;
102
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()
116 };
117
118 git_config(git_default_config, NULL);
119
120 if (argc < 2 || !strcmp(argv[1], "-h"))
121 usage_with_options(commit_tree_usage, options);
122
123 argc = parse_options(argc, argv, prefix, options, commit_tree_usage, 0);
124
125 if (argc != 1)
126 die(_("must give exactly one tree"));
127
128 if (repo_get_oid_tree(the_repository, argv[0], &tree_oid))
129 die(_("not a valid object name %s"), argv[0]);
130
131 if (!buffer.len) {
132 if (strbuf_read(&buffer, 0, 0) < 0)
133 die_errno(_("git commit-tree: failed to read"));
134 }
135
136 if (commit_tree(buffer.buf, buffer.len, &tree_oid, parents, &commit_oid,
137 NULL, sign_commit)) {
138 strbuf_release(&buffer);
139 return 1;
140 }
141
142 printf("%s\n", oid_to_hex(&commit_oid));
143 strbuf_release(&buffer);
144 return 0;
145 }