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