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