]>
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 | ||
cbdeab98 BR |
40 | static int parse_parent_arg_callback(const struct option *opt, |
41 | const char *arg, int unset) | |
42 | { | |
43 | struct object_id oid; | |
44 | struct commit_list **parents = opt->value; | |
45 | ||
46 | BUG_ON_OPT_NEG_NOARG(unset, arg); | |
47 | ||
48 | if (get_oid_commit(arg, &oid)) | |
49 | die(_("not a valid object name %s"), arg); | |
50 | ||
51 | assert_oid_type(&oid, OBJ_COMMIT); | |
52 | new_parent(lookup_commit(the_repository, &oid), parents); | |
53 | return 0; | |
54 | } | |
55 | ||
56 | static int parse_message_arg_callback(const struct option *opt, | |
57 | const char *arg, int unset) | |
58 | { | |
59 | struct strbuf *buf = opt->value; | |
60 | ||
61 | BUG_ON_OPT_NEG_NOARG(unset, arg); | |
62 | ||
63 | if (buf->len) | |
64 | strbuf_addch(buf, '\n'); | |
65 | strbuf_addstr(buf, arg); | |
66 | strbuf_complete_line(buf); | |
67 | ||
68 | return 0; | |
69 | } | |
70 | ||
71 | static int parse_file_arg_callback(const struct option *opt, | |
72 | const char *arg, int unset) | |
73 | { | |
74 | int fd; | |
75 | struct strbuf *buf = opt->value; | |
76 | ||
77 | BUG_ON_OPT_NEG_NOARG(unset, arg); | |
78 | ||
79 | if (buf->len) | |
80 | strbuf_addch(buf, '\n'); | |
81 | if (!strcmp(arg, "-")) | |
82 | fd = 0; | |
83 | else { | |
66e905b7 | 84 | fd = xopen(arg, O_RDONLY); |
cbdeab98 BR |
85 | } |
86 | if (strbuf_read(buf, fd, 0) < 0) | |
87 | die_errno(_("git commit-tree: failed to read '%s'"), arg); | |
88 | if (fd && close(fd)) | |
89 | die_errno(_("git commit-tree: failed to close '%s'"), arg); | |
90 | ||
91 | return 0; | |
92 | } | |
93 | ||
7b9c0a69 MV |
94 | int cmd_commit_tree(int argc, const char **argv, const char *prefix) |
95 | { | |
cbdeab98 | 96 | static struct strbuf buffer = STRBUF_INIT; |
7b9c0a69 | 97 | struct commit_list *parents = NULL; |
031cee5b | 98 | struct object_id tree_oid; |
99 | struct object_id commit_oid; | |
cbdeab98 BR |
100 | |
101 | struct option options[] = { | |
203c8533 | 102 | OPT_CALLBACK_F('p', NULL, &parents, N_("parent"), |
cbdeab98 | 103 | N_("id of a parent commit object"), PARSE_OPT_NONEG, |
203c8533 DL |
104 | parse_parent_arg_callback), |
105 | OPT_CALLBACK_F('m', NULL, &buffer, N_("message"), | |
cbdeab98 | 106 | N_("commit message"), PARSE_OPT_NONEG, |
203c8533 DL |
107 | parse_message_arg_callback), |
108 | OPT_CALLBACK_F('F', NULL, &buffer, N_("file"), | |
cbdeab98 | 109 | N_("read commit log message from file"), PARSE_OPT_NONEG, |
203c8533 | 110 | parse_file_arg_callback), |
cbdeab98 BR |
111 | { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"), |
112 | N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" }, | |
113 | OPT_END() | |
114 | }; | |
7b9c0a69 | 115 | |
cc5d1d32 | 116 | git_config(git_default_config, NULL); |
7b9c0a69 | 117 | |
6e9daeff | 118 | if (argc < 2 || !strcmp(argv[1], "-h")) |
cbdeab98 | 119 | usage_with_options(commit_tree_usage, options); |
7b9c0a69 | 120 | |
cbdeab98 | 121 | argc = parse_options(argc, argv, prefix, options, commit_tree_usage, 0); |
70ddbd77 | 122 | |
cbdeab98 BR |
123 | if (argc != 1) |
124 | die(_("must give exactly one tree")); | |
ba3c69a9 | 125 | |
cbdeab98 BR |
126 | if (get_oid_tree(argv[0], &tree_oid)) |
127 | die(_("not a valid object name %s"), argv[0]); | |
7b9c0a69 | 128 | |
96b8d93a JH |
129 | if (!buffer.len) { |
130 | if (strbuf_read(&buffer, 0, 0) < 0) | |
cbdeab98 | 131 | die_errno(_("git commit-tree: failed to read")); |
96b8d93a | 132 | } |
7b9c0a69 | 133 | |
5078f344 PO |
134 | if (commit_tree(buffer.buf, buffer.len, &tree_oid, parents, &commit_oid, |
135 | NULL, sign_commit)) { | |
79bc2af5 | 136 | strbuf_release(&buffer); |
7561d9f5 | 137 | return 1; |
79bc2af5 JN |
138 | } |
139 | ||
031cee5b | 140 | printf("%s\n", oid_to_hex(&commit_oid)); |
79bc2af5 JN |
141 | strbuf_release(&buffer); |
142 | return 0; | |
e83c5163 | 143 | } |