]>
Commit | Line | Data |
---|---|---|
8bc9a0c7 LT |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | */ | |
bc5c5ec0 | 6 | #include "builtin.h" |
b2141fc1 | 7 | #include "config.h" |
f394e093 | 8 | #include "gettext.h" |
41771fa4 | 9 | #include "hex.h" |
dabab1d6 | 10 | #include "object-name.h" |
a034e910 | 11 | #include "object-store-ll.h" |
c1f5eb49 | 12 | #include "repository.h" |
8e440259 | 13 | #include "commit.h" |
cbdeab98 | 14 | #include "parse-options.h" |
e83c5163 | 15 | |
cbdeab98 | 16 | static const char * const commit_tree_usage[] = { |
d9054a19 | 17 | N_("git commit-tree <tree> [(-p <parent>)...]"), |
5af8b61c ÆAB |
18 | N_("git commit-tree [(-p <parent>)...] [-S[<keyid>]] [(-m <message>)...]\n" |
19 | " [(-F <file>)...] <tree>"), | |
cbdeab98 BR |
20 | NULL |
21 | }; | |
c5bac17a | 22 | |
d95bfb12 NV |
23 | static const char *sign_commit; |
24 | ||
ef98c5ca | 25 | static void new_parent(struct commit *parent, struct commit_list **parents_p) |
b389237a | 26 | { |
f2fd0760 | 27 | struct object_id *oid = &parent->object.oid; |
ef98c5ca JS |
28 | struct commit_list *parents; |
29 | for (parents = *parents_p; parents; parents = parents->next) { | |
30 | if (parents->item == parent) { | |
cbdeab98 | 31 | error(_("duplicate parent %s ignored"), oid_to_hex(oid)); |
ef98c5ca | 32 | return; |
b389237a | 33 | } |
ef98c5ca | 34 | parents_p = &parents->next; |
b389237a | 35 | } |
ef98c5ca | 36 | commit_list_insert(parent, parents_p); |
b389237a LT |
37 | } |
38 | ||
cbdeab98 BR |
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 | ||
d850b7a5 | 47 | if (repo_get_oid_commit(the_repository, arg, &oid)) |
cbdeab98 BR |
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 { | |
66e905b7 | 83 | fd = xopen(arg, O_RDONLY); |
cbdeab98 BR |
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 | ||
7b9c0a69 MV |
93 | int cmd_commit_tree(int argc, const char **argv, const char *prefix) |
94 | { | |
cbdeab98 | 95 | static struct strbuf buffer = STRBUF_INIT; |
7b9c0a69 | 96 | struct commit_list *parents = NULL; |
031cee5b | 97 | struct object_id tree_oid; |
98 | struct object_id commit_oid; | |
cbdeab98 BR |
99 | |
100 | struct option options[] = { | |
203c8533 | 101 | OPT_CALLBACK_F('p', NULL, &parents, N_("parent"), |
cbdeab98 | 102 | N_("id of a parent commit object"), PARSE_OPT_NONEG, |
203c8533 DL |
103 | parse_parent_arg_callback), |
104 | OPT_CALLBACK_F('m', NULL, &buffer, N_("message"), | |
cbdeab98 | 105 | N_("commit message"), PARSE_OPT_NONEG, |
203c8533 DL |
106 | parse_message_arg_callback), |
107 | OPT_CALLBACK_F('F', NULL, &buffer, N_("file"), | |
cbdeab98 | 108 | N_("read commit log message from file"), PARSE_OPT_NONEG, |
203c8533 | 109 | parse_file_arg_callback), |
cbdeab98 BR |
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 | }; | |
7b9c0a69 | 114 | |
cc5d1d32 | 115 | git_config(git_default_config, NULL); |
7b9c0a69 | 116 | |
6e9daeff | 117 | if (argc < 2 || !strcmp(argv[1], "-h")) |
cbdeab98 | 118 | usage_with_options(commit_tree_usage, options); |
7b9c0a69 | 119 | |
cbdeab98 | 120 | argc = parse_options(argc, argv, prefix, options, commit_tree_usage, 0); |
70ddbd77 | 121 | |
cbdeab98 BR |
122 | if (argc != 1) |
123 | die(_("must give exactly one tree")); | |
ba3c69a9 | 124 | |
d850b7a5 | 125 | if (repo_get_oid_tree(the_repository, argv[0], &tree_oid)) |
cbdeab98 | 126 | die(_("not a valid object name %s"), argv[0]); |
7b9c0a69 | 127 | |
96b8d93a JH |
128 | if (!buffer.len) { |
129 | if (strbuf_read(&buffer, 0, 0) < 0) | |
cbdeab98 | 130 | die_errno(_("git commit-tree: failed to read")); |
96b8d93a | 131 | } |
7b9c0a69 | 132 | |
5078f344 PO |
133 | if (commit_tree(buffer.buf, buffer.len, &tree_oid, parents, &commit_oid, |
134 | NULL, sign_commit)) { | |
79bc2af5 | 135 | strbuf_release(&buffer); |
7561d9f5 | 136 | return 1; |
79bc2af5 JN |
137 | } |
138 | ||
031cee5b | 139 | printf("%s\n", oid_to_hex(&commit_oid)); |
79bc2af5 JN |
140 | strbuf_release(&buffer); |
141 | return 0; | |
e83c5163 | 142 | } |