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