]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/commit-tree.c
Start the 2.46 cycle
[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 "parse-options.h"
15
16 static const char * const commit_tree_usage[] = {
17 N_("git commit-tree <tree> [(-p <parent>)...]"),
18 N_("git commit-tree [(-p <parent>)...] [-S[<keyid>]] [(-m <message>)...]\n"
19 " [(-F <file>)...] <tree>"),
20 NULL
21 };
22
23 static const char *sign_commit;
24
25 static void new_parent(struct commit *parent, struct commit_list **parents_p)
26 {
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));
32 return;
33 }
34 parents_p = &parents->next;
35 }
36 commit_list_insert(parent, parents_p);
37 }
38
39 static int parse_parent_arg_callback(const struct option *opt,
40 const char *arg, int unset)
41 {
42 struct object_id oid;
43 struct commit_list **parents = opt->value;
44
45 BUG_ON_OPT_NEG_NOARG(unset, arg);
46
47 if (repo_get_oid_commit(the_repository, arg, &oid))
48 die(_("not a valid object name %s"), arg);
49
50 assert_oid_type(&oid, OBJ_COMMIT);
51 new_parent(lookup_commit(the_repository, &oid), parents);
52 return 0;
53 }
54
55 static int parse_message_arg_callback(const struct option *opt,
56 const char *arg, int unset)
57 {
58 struct strbuf *buf = opt->value;
59
60 BUG_ON_OPT_NEG_NOARG(unset, arg);
61
62 if (buf->len)
63 strbuf_addch(buf, '\n');
64 strbuf_addstr(buf, arg);
65 strbuf_complete_line(buf);
66
67 return 0;
68 }
69
70 static int parse_file_arg_callback(const struct option *opt,
71 const char *arg, int unset)
72 {
73 int fd;
74 struct strbuf *buf = opt->value;
75
76 BUG_ON_OPT_NEG_NOARG(unset, arg);
77
78 if (buf->len)
79 strbuf_addch(buf, '\n');
80 if (!strcmp(arg, "-"))
81 fd = 0;
82 else {
83 fd = xopen(arg, O_RDONLY);
84 }
85 if (strbuf_read(buf, fd, 0) < 0)
86 die_errno(_("git commit-tree: failed to read '%s'"), arg);
87 if (fd && close(fd))
88 die_errno(_("git commit-tree: failed to close '%s'"), arg);
89
90 return 0;
91 }
92
93 int cmd_commit_tree(int argc, const char **argv, const char *prefix)
94 {
95 static struct strbuf buffer = STRBUF_INIT;
96 struct commit_list *parents = NULL;
97 struct object_id tree_oid;
98 struct object_id commit_oid;
99
100 struct option options[] = {
101 OPT_CALLBACK_F('p', NULL, &parents, N_("parent"),
102 N_("id of a parent commit object"), PARSE_OPT_NONEG,
103 parse_parent_arg_callback),
104 OPT_CALLBACK_F('m', NULL, &buffer, N_("message"),
105 N_("commit message"), PARSE_OPT_NONEG,
106 parse_message_arg_callback),
107 OPT_CALLBACK_F('F', NULL, &buffer, N_("file"),
108 N_("read commit log message from file"), PARSE_OPT_NONEG,
109 parse_file_arg_callback),
110 { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
111 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
112 OPT_END()
113 };
114
115 git_config(git_default_config, NULL);
116
117 if (argc < 2 || !strcmp(argv[1], "-h"))
118 usage_with_options(commit_tree_usage, options);
119
120 argc = parse_options(argc, argv, prefix, options, commit_tree_usage, 0);
121
122 if (argc != 1)
123 die(_("must give exactly one tree"));
124
125 if (repo_get_oid_tree(the_repository, argv[0], &tree_oid))
126 die(_("not a valid object name %s"), argv[0]);
127
128 if (!buffer.len) {
129 if (strbuf_read(&buffer, 0, 0) < 0)
130 die_errno(_("git commit-tree: failed to read"));
131 }
132
133 if (commit_tree(buffer.buf, buffer.len, &tree_oid, parents, &commit_oid,
134 NULL, sign_commit)) {
135 strbuf_release(&buffer);
136 return 1;
137 }
138
139 printf("%s\n", oid_to_hex(&commit_oid));
140 strbuf_release(&buffer);
141 return 0;
142 }