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" |
8e440259 PE |
7 | #include "commit.h" |
8 | #include "tree.h" | |
6d96ac18 | 9 | #include "builtin.h" |
9e832665 | 10 | #include "utf8.h" |
ba3c69a9 | 11 | #include "gpg-interface.h" |
e83c5163 | 12 | |
33e8fc87 | 13 | static const char commit_tree_usage[] = "git commit-tree [(-p <sha1>)...] [-S[<keyid>]] [-m <message>] [-F <file>] <sha1>"; |
c5bac17a | 14 | |
d95bfb12 NV |
15 | static const char *sign_commit; |
16 | ||
ef98c5ca | 17 | static void new_parent(struct commit *parent, struct commit_list **parents_p) |
b389237a | 18 | { |
f2fd0760 | 19 | struct object_id *oid = &parent->object.oid; |
ef98c5ca JS |
20 | struct commit_list *parents; |
21 | for (parents = *parents_p; parents; parents = parents->next) { | |
22 | if (parents->item == parent) { | |
f2fd0760 | 23 | error("duplicate parent %s ignored", oid_to_hex(oid)); |
ef98c5ca | 24 | return; |
b389237a | 25 | } |
ef98c5ca | 26 | parents_p = &parents->next; |
b389237a | 27 | } |
ef98c5ca | 28 | commit_list_insert(parent, parents_p); |
b389237a LT |
29 | } |
30 | ||
ba3c69a9 JH |
31 | static int commit_tree_config(const char *var, const char *value, void *cb) |
32 | { | |
33 | int status = git_gpg_config(var, value, NULL); | |
34 | if (status) | |
35 | return status; | |
36 | return git_default_config(var, value, cb); | |
37 | } | |
38 | ||
7b9c0a69 MV |
39 | int cmd_commit_tree(int argc, const char **argv, const char *prefix) |
40 | { | |
79a9312c | 41 | int i, got_tree = 0; |
7b9c0a69 | 42 | struct commit_list *parents = NULL; |
031cee5b | 43 | struct object_id tree_oid; |
44 | struct object_id commit_oid; | |
7b9c0a69 MV |
45 | struct strbuf buffer = STRBUF_INIT; |
46 | ||
ba3c69a9 | 47 | git_config(commit_tree_config, NULL); |
7b9c0a69 | 48 | |
6e9daeff | 49 | if (argc < 2 || !strcmp(argv[1], "-h")) |
7b9c0a69 | 50 | usage(commit_tree_usage); |
7b9c0a69 | 51 | |
79a9312c JH |
52 | for (i = 1; i < argc; i++) { |
53 | const char *arg = argv[i]; | |
54 | if (!strcmp(arg, "-p")) { | |
031cee5b | 55 | struct object_id oid; |
79a9312c JH |
56 | if (argc <= ++i) |
57 | usage(commit_tree_usage); | |
031cee5b | 58 | if (get_sha1_commit(argv[i], oid.hash)) |
79a9312c | 59 | die("Not a valid object name %s", argv[i]); |
031cee5b | 60 | assert_sha1_type(oid.hash, OBJ_COMMIT); |
61 | new_parent(lookup_commit(oid.hash), &parents); | |
79a9312c JH |
62 | continue; |
63 | } | |
7b9c0a69 | 64 | |
8547e0f1 | 65 | if (skip_prefix(arg, "-S", &sign_commit)) |
ba3c69a9 | 66 | continue; |
ba3c69a9 | 67 | |
55ca3f99 JH |
68 | if (!strcmp(arg, "--no-gpg-sign")) { |
69 | sign_commit = NULL; | |
70 | continue; | |
71 | } | |
72 | ||
96b8d93a JH |
73 | if (!strcmp(arg, "-m")) { |
74 | if (argc <= ++i) | |
75 | usage(commit_tree_usage); | |
76 | if (buffer.len) | |
77 | strbuf_addch(&buffer, '\n'); | |
78 | strbuf_addstr(&buffer, argv[i]); | |
79 | strbuf_complete_line(&buffer); | |
80 | continue; | |
81 | } | |
82 | ||
83 | if (!strcmp(arg, "-F")) { | |
84 | int fd; | |
85 | ||
86 | if (argc <= ++i) | |
87 | usage(commit_tree_usage); | |
88 | if (buffer.len) | |
89 | strbuf_addch(&buffer, '\n'); | |
90 | if (!strcmp(argv[i], "-")) | |
91 | fd = 0; | |
92 | else { | |
93 | fd = open(argv[i], O_RDONLY); | |
94 | if (fd < 0) | |
95 | die_errno("git commit-tree: failed to open '%s'", | |
96 | argv[i]); | |
97 | } | |
98 | if (strbuf_read(&buffer, fd, 0) < 0) | |
99 | die_errno("git commit-tree: failed to read '%s'", | |
100 | argv[i]); | |
101 | if (fd && close(fd)) | |
102 | die_errno("git commit-tree: failed to close '%s'", | |
103 | argv[i]); | |
104 | strbuf_complete_line(&buffer); | |
105 | continue; | |
106 | } | |
107 | ||
031cee5b | 108 | if (get_sha1_tree(arg, tree_oid.hash)) |
79a9312c JH |
109 | die("Not a valid object name %s", arg); |
110 | if (got_tree) | |
111 | die("Cannot give more than one trees"); | |
112 | got_tree = 1; | |
7b9c0a69 MV |
113 | } |
114 | ||
96b8d93a JH |
115 | if (!buffer.len) { |
116 | if (strbuf_read(&buffer, 0, 0) < 0) | |
117 | die_errno("git commit-tree: failed to read"); | |
118 | } | |
7b9c0a69 | 119 | |
031cee5b | 120 | if (commit_tree(buffer.buf, buffer.len, tree_oid.hash, parents, |
121 | commit_oid.hash, NULL, sign_commit)) { | |
79bc2af5 | 122 | strbuf_release(&buffer); |
7561d9f5 | 123 | return 1; |
79bc2af5 JN |
124 | } |
125 | ||
031cee5b | 126 | printf("%s\n", oid_to_hex(&commit_oid)); |
79bc2af5 JN |
127 | strbuf_release(&buffer); |
128 | return 0; | |
e83c5163 | 129 | } |