]>
Commit | Line | Data |
---|---|---|
8bc9a0c7 LT |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | */ | |
e83c5163 | 6 | #include "cache.h" |
b2141fc1 | 7 | #include "config.h" |
cbd53a21 | 8 | #include "object-store.h" |
c1f5eb49 | 9 | #include "repository.h" |
8e440259 PE |
10 | #include "commit.h" |
11 | #include "tree.h" | |
6d96ac18 | 12 | #include "builtin.h" |
9e832665 | 13 | #include "utf8.h" |
ba3c69a9 | 14 | #include "gpg-interface.h" |
cbdeab98 | 15 | #include "parse-options.h" |
e83c5163 | 16 | |
cbdeab98 | 17 | static const char * const commit_tree_usage[] = { |
d9054a19 | 18 | N_("git commit-tree <tree> [(-p <parent>)...]"), |
5af8b61c ÆAB |
19 | N_("git commit-tree [(-p <parent>)...] [-S[<keyid>]] [(-m <message>)...]\n" |
20 | " [(-F <file>)...] <tree>"), | |
cbdeab98 BR |
21 | NULL |
22 | }; | |
c5bac17a | 23 | |
d95bfb12 NV |
24 | static const char *sign_commit; |
25 | ||
ef98c5ca | 26 | static void new_parent(struct commit *parent, struct commit_list **parents_p) |
b389237a | 27 | { |
f2fd0760 | 28 | struct object_id *oid = &parent->object.oid; |
ef98c5ca JS |
29 | struct commit_list *parents; |
30 | for (parents = *parents_p; parents; parents = parents->next) { | |
31 | if (parents->item == parent) { | |
cbdeab98 | 32 | error(_("duplicate parent %s ignored"), oid_to_hex(oid)); |
ef98c5ca | 33 | return; |
b389237a | 34 | } |
ef98c5ca | 35 | parents_p = &parents->next; |
b389237a | 36 | } |
ef98c5ca | 37 | commit_list_insert(parent, parents_p); |
b389237a LT |
38 | } |
39 | ||
ba3c69a9 JH |
40 | static int commit_tree_config(const char *var, const char *value, void *cb) |
41 | { | |
42 | int status = git_gpg_config(var, value, NULL); | |
43 | if (status) | |
44 | return status; | |
45 | return git_default_config(var, value, cb); | |
46 | } | |
47 | ||
cbdeab98 BR |
48 | static int parse_parent_arg_callback(const struct option *opt, |
49 | const char *arg, int unset) | |
50 | { | |
51 | struct object_id oid; | |
52 | struct commit_list **parents = opt->value; | |
53 | ||
54 | BUG_ON_OPT_NEG_NOARG(unset, arg); | |
55 | ||
56 | if (get_oid_commit(arg, &oid)) | |
57 | die(_("not a valid object name %s"), arg); | |
58 | ||
59 | assert_oid_type(&oid, OBJ_COMMIT); | |
60 | new_parent(lookup_commit(the_repository, &oid), parents); | |
61 | return 0; | |
62 | } | |
63 | ||
64 | static int parse_message_arg_callback(const struct option *opt, | |
65 | const char *arg, int unset) | |
66 | { | |
67 | struct strbuf *buf = opt->value; | |
68 | ||
69 | BUG_ON_OPT_NEG_NOARG(unset, arg); | |
70 | ||
71 | if (buf->len) | |
72 | strbuf_addch(buf, '\n'); | |
73 | strbuf_addstr(buf, arg); | |
74 | strbuf_complete_line(buf); | |
75 | ||
76 | return 0; | |
77 | } | |
78 | ||
79 | static int parse_file_arg_callback(const struct option *opt, | |
80 | const char *arg, int unset) | |
81 | { | |
82 | int fd; | |
83 | struct strbuf *buf = opt->value; | |
84 | ||
85 | BUG_ON_OPT_NEG_NOARG(unset, arg); | |
86 | ||
87 | if (buf->len) | |
88 | strbuf_addch(buf, '\n'); | |
89 | if (!strcmp(arg, "-")) | |
90 | fd = 0; | |
91 | else { | |
66e905b7 | 92 | fd = xopen(arg, O_RDONLY); |
cbdeab98 BR |
93 | } |
94 | if (strbuf_read(buf, fd, 0) < 0) | |
95 | die_errno(_("git commit-tree: failed to read '%s'"), arg); | |
96 | if (fd && close(fd)) | |
97 | die_errno(_("git commit-tree: failed to close '%s'"), arg); | |
98 | ||
99 | return 0; | |
100 | } | |
101 | ||
7b9c0a69 MV |
102 | int cmd_commit_tree(int argc, const char **argv, const char *prefix) |
103 | { | |
cbdeab98 | 104 | static struct strbuf buffer = STRBUF_INIT; |
7b9c0a69 | 105 | struct commit_list *parents = NULL; |
031cee5b | 106 | struct object_id tree_oid; |
107 | struct object_id commit_oid; | |
cbdeab98 BR |
108 | |
109 | struct option options[] = { | |
203c8533 | 110 | OPT_CALLBACK_F('p', NULL, &parents, N_("parent"), |
cbdeab98 | 111 | N_("id of a parent commit object"), PARSE_OPT_NONEG, |
203c8533 DL |
112 | parse_parent_arg_callback), |
113 | OPT_CALLBACK_F('m', NULL, &buffer, N_("message"), | |
cbdeab98 | 114 | N_("commit message"), PARSE_OPT_NONEG, |
203c8533 DL |
115 | parse_message_arg_callback), |
116 | OPT_CALLBACK_F('F', NULL, &buffer, N_("file"), | |
cbdeab98 | 117 | N_("read commit log message from file"), PARSE_OPT_NONEG, |
203c8533 | 118 | parse_file_arg_callback), |
cbdeab98 BR |
119 | { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"), |
120 | N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" }, | |
121 | OPT_END() | |
122 | }; | |
7b9c0a69 | 123 | |
ba3c69a9 | 124 | git_config(commit_tree_config, NULL); |
7b9c0a69 | 125 | |
6e9daeff | 126 | if (argc < 2 || !strcmp(argv[1], "-h")) |
cbdeab98 | 127 | usage_with_options(commit_tree_usage, options); |
7b9c0a69 | 128 | |
cbdeab98 | 129 | argc = parse_options(argc, argv, prefix, options, commit_tree_usage, 0); |
70ddbd77 | 130 | |
cbdeab98 BR |
131 | if (argc != 1) |
132 | die(_("must give exactly one tree")); | |
ba3c69a9 | 133 | |
cbdeab98 BR |
134 | if (get_oid_tree(argv[0], &tree_oid)) |
135 | die(_("not a valid object name %s"), argv[0]); | |
7b9c0a69 | 136 | |
96b8d93a JH |
137 | if (!buffer.len) { |
138 | if (strbuf_read(&buffer, 0, 0) < 0) | |
cbdeab98 | 139 | die_errno(_("git commit-tree: failed to read")); |
96b8d93a | 140 | } |
7b9c0a69 | 141 | |
5078f344 PO |
142 | if (commit_tree(buffer.buf, buffer.len, &tree_oid, parents, &commit_oid, |
143 | NULL, sign_commit)) { | |
79bc2af5 | 144 | strbuf_release(&buffer); |
7561d9f5 | 145 | return 1; |
79bc2af5 JN |
146 | } |
147 | ||
031cee5b | 148 | printf("%s\n", oid_to_hex(&commit_oid)); |
79bc2af5 JN |
149 | strbuf_release(&buffer); |
150 | return 0; | |
e83c5163 | 151 | } |