Provide git_config with a callback-data parameter
[git/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
e83c5163 27/*
c5bac17a
JH
28 * Having more than two parents is not strange at all, and this is
29 * how multi-way merges are represented.
e83c5163
LT
30 */
31#define MAXPARENT (16)
b389237a 32static unsigned char parent_sha1[MAXPARENT][20];
e83c5163 33
4d1f1190 34static const char commit_tree_usage[] = "git-commit-tree <sha1> [-p <sha1>]* < changelog";
c5bac17a 35
b389237a
LT
36static int new_parent(int idx)
37{
38 int i;
39 unsigned char *sha1 = parent_sha1[idx];
40 for (i = 0; i < idx; i++) {
a89fccd2 41 if (!hashcmp(parent_sha1[i], sha1)) {
b389237a
LT
42 error("duplicate parent %s ignored", sha1_to_hex(sha1));
43 return 0;
44 }
45 }
46 return 1;
47}
48
9e832665
JS
49static const char commit_utf8_warn[] =
50"Warning: commit message does not conform to UTF-8.\n"
51"You may want to amend it after fixing the message, or set the config\n"
52"variable i18n.commitencoding to the encoding your project uses.\n";
53
a633fca0 54int cmd_commit_tree(int argc, const char **argv, const char *prefix)
e83c5163 55{
6aa33f40 56 int i;
e83c5163
LT
57 int parents = 0;
58 unsigned char tree_sha1[20];
d6d3f9d0 59 unsigned char commit_sha1[20];
af6eb822 60 struct strbuf buffer;
4b2bced5 61 int encoding_is_utf8;
e83c5163 62
ef90d6d4 63 git_config(git_default_config, NULL);
e1b10391 64
31fff305 65 if (argc < 2)
c5bac17a 66 usage(commit_tree_usage);
31fff305
DL
67 if (get_sha1(argv[1], tree_sha1))
68 die("Not a valid object name %s", argv[1]);
e83c5163 69
66035a6b 70 check_valid(tree_sha1, OBJ_TREE);
e83c5163 71 for (i = 2; i < argc; i += 2) {
6d96ac18 72 const char *a, *b;
e83c5163 73 a = argv[i]; b = argv[i+1];
31fff305 74 if (!b || strcmp(a, "-p"))
c5bac17a 75 usage(commit_tree_usage);
2295e8d0
JH
76
77 if (parents >= MAXPARENT)
78 die("Too many parents (%d max)", MAXPARENT);
31fff305
DL
79 if (get_sha1(b, parent_sha1[parents]))
80 die("Not a valid object name %s", b);
66035a6b 81 check_valid(parent_sha1[parents], OBJ_COMMIT);
b389237a
LT
82 if (new_parent(parents))
83 parents++;
e83c5163 84 }
e83c5163 85
d2c11a38 86 /* Not having i18n.commitencoding is the same as having utf-8 */
677cfed5 87 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
4b2bced5 88
f1696ee3 89 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
af6eb822 90 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree_sha1));
e83c5163
LT
91
92 /*
93 * NOTE! This ordering means that the same exact tree merged with a
94 * different order of parents will be a _different_ changeset even
95 * if everything else stays the same.
96 */
97 for (i = 0; i < parents; i++)
af6eb822 98 strbuf_addf(&buffer, "parent %s\n", sha1_to_hex(parent_sha1[i]));
e83c5163
LT
99
100 /* Person/date information */
774751a8
JH
101 strbuf_addf(&buffer, "author %s\n", git_author_info(IDENT_ERROR_ON_NO_NAME));
102 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
4b2bced5 103 if (!encoding_is_utf8)
af6eb822
PH
104 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
105 strbuf_addch(&buffer, '\n');
e83c5163
LT
106
107 /* And add the comment */
f1696ee3 108 if (strbuf_read(&buffer, 0, 0) < 0)
af6eb822 109 die("git-commit-tree: read returned %s", strerror(errno));
e83c5163 110
9e832665 111 /* And check the encoding */
af6eb822 112 if (encoding_is_utf8 && !is_utf8(buffer.buf))
9e832665
JS
113 fprintf(stderr, commit_utf8_warn);
114
af6eb822 115 if (!write_sha1_file(buffer.buf, buffer.len, commit_type, commit_sha1)) {
7561d9f5
JH
116 printf("%s\n", sha1_to_hex(commit_sha1));
117 return 0;
118 }
119 else
120 return 1;
e83c5163 121}