]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-commit-tree.c
Documentation/git-archive: spell --worktree-attributes correctly
[thirdparty/git.git] / builtin-commit-tree.c
CommitLineData
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"
e83c5163 11
e83c5163 12/*
e83c5163
LT
13 * FIXME! Share the code with "write-tree.c"
14 */
66035a6b 15static void check_valid(unsigned char *sha1, enum object_type expect)
d0d7cbe7 16{
21666f1a
NP
17 enum object_type type = sha1_object_info(sha1, NULL);
18 if (type < 0)
5981e099 19 die("%s is not a valid object", sha1_to_hex(sha1));
66035a6b 20 if (type != expect)
5981e099 21 die("%s is not a valid '%s' object", sha1_to_hex(sha1),
66035a6b 22 typename(expect));
d0d7cbe7
LT
23}
24
05207a28 25static const char commit_tree_usage[] = "git commit-tree <sha1> [-p <sha1>]* < changelog";
c5bac17a 26
ef98c5ca 27static void new_parent(struct commit *parent, struct commit_list **parents_p)
b389237a 28{
ef98c5ca
JS
29 unsigned char *sha1 = parent->object.sha1;
30 struct commit_list *parents;
31 for (parents = *parents_p; parents; parents = parents->next) {
32 if (parents->item == parent) {
b389237a 33 error("duplicate parent %s ignored", sha1_to_hex(sha1));
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
9e832665
JS
41static const char commit_utf8_warn[] =
42"Warning: commit message does not conform to UTF-8.\n"
43"You may want to amend it after fixing the message, or set the config\n"
44"variable i18n.commitencoding to the encoding your project uses.\n";
45
7b9c0a69 46int commit_tree(const char *msg, unsigned char *tree,
ee20a129
MV
47 struct commit_list *parents, unsigned char *ret,
48 const char *author)
e83c5163 49{
ffa9fd95 50 int result;
4b2bced5 51 int encoding_is_utf8;
7b9c0a69 52 struct strbuf buffer;
e83c5163 53
7b9c0a69 54 check_valid(tree, OBJ_TREE);
e83c5163 55
d2c11a38 56 /* Not having i18n.commitencoding is the same as having utf-8 */
677cfed5 57 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
4b2bced5 58
f1696ee3 59 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
7b9c0a69 60 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
e83c5163
LT
61
62 /*
63 * NOTE! This ordering means that the same exact tree merged with a
64 * different order of parents will be a _different_ changeset even
65 * if everything else stays the same.
66 */
ef98c5ca
JS
67 while (parents) {
68 struct commit_list *next = parents->next;
69 strbuf_addf(&buffer, "parent %s\n",
70 sha1_to_hex(parents->item->object.sha1));
71 free(parents);
72 parents = next;
73 }
e83c5163
LT
74
75 /* Person/date information */
ee20a129
MV
76 if (!author)
77 author = git_author_info(IDENT_ERROR_ON_NO_NAME);
78 strbuf_addf(&buffer, "author %s\n", author);
774751a8 79 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
4b2bced5 80 if (!encoding_is_utf8)
af6eb822
PH
81 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
82 strbuf_addch(&buffer, '\n');
e83c5163
LT
83
84 /* And add the comment */
7b9c0a69 85 strbuf_addstr(&buffer, msg);
e83c5163 86
9e832665 87 /* And check the encoding */
af6eb822 88 if (encoding_is_utf8 && !is_utf8(buffer.buf))
9e832665
JS
89 fprintf(stderr, commit_utf8_warn);
90
ffa9fd95
SB
91 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
92 strbuf_release(&buffer);
93 return result;
7b9c0a69
MV
94}
95
96int cmd_commit_tree(int argc, const char **argv, const char *prefix)
97{
98 int i;
99 struct commit_list *parents = NULL;
100 unsigned char tree_sha1[20];
101 unsigned char commit_sha1[20];
102 struct strbuf buffer = STRBUF_INIT;
103
104 git_config(git_default_config, NULL);
105
6e9daeff 106 if (argc < 2 || !strcmp(argv[1], "-h"))
7b9c0a69
MV
107 usage(commit_tree_usage);
108 if (get_sha1(argv[1], tree_sha1))
109 die("Not a valid object name %s", argv[1]);
110
111 for (i = 2; i < argc; i += 2) {
112 unsigned char sha1[20];
113 const char *a, *b;
114 a = argv[i]; b = argv[i+1];
115 if (!b || strcmp(a, "-p"))
116 usage(commit_tree_usage);
117
118 if (get_sha1(b, sha1))
119 die("Not a valid object name %s", b);
120 check_valid(sha1, OBJ_COMMIT);
121 new_parent(lookup_commit(sha1), &parents);
122 }
123
124 if (strbuf_read(&buffer, 0, 0) < 0)
d824cbba 125 die_errno("git commit-tree: failed to read");
7b9c0a69 126
ee20a129 127 if (!commit_tree(buffer.buf, tree_sha1, parents, commit_sha1, NULL)) {
7561d9f5
JH
128 printf("%s\n", sha1_to_hex(commit_sha1));
129 return 0;
130 }
131 else
132 return 1;
e83c5163 133}