blob: 6610d18358bae81ac1f162ca0c71062d36664c2a [file] [log] [blame]
Linus Torvalds8bc9a0c2005-04-07 15:16:10 -07001/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
Linus Torvaldse83c5162005-04-07 15:13:13 -07006#include "cache.h"
Peter Eriksen8e440252006-04-02 14:44:09 +02007#include "commit.h"
8#include "tree.h"
Peter Eriksen6d96ac12006-05-23 14:15:33 +02009#include "builtin.h"
Johannes Schindelin9e832662006-12-22 22:06:08 +010010#include "utf8.h"
Linus Torvaldse83c5162005-04-07 15:13:13 -070011
Linus Torvaldse83c5162005-04-07 15:13:13 -070012#define BLOCKING (1ul << 14)
Linus Torvaldse83c5162005-04-07 15:13:13 -070013
14/*
Linus Torvaldse83c5162005-04-07 15:13:13 -070015 * FIXME! Share the code with "write-tree.c"
16 */
Shawn O. Pearce66035a62007-02-27 10:00:33 -050017static void check_valid(unsigned char *sha1, enum object_type expect)
Linus Torvaldsd0d7cbe2005-04-17 15:26:13 -070018{
Nicolas Pitre21666f12007-02-26 14:55:59 -050019 enum object_type type = sha1_object_info(sha1, NULL);
20 if (type < 0)
Junio C Hamano5981e092006-04-26 16:55:25 -070021 die("%s is not a valid object", sha1_to_hex(sha1));
Shawn O. Pearce66035a62007-02-27 10:00:33 -050022 if (type != expect)
Junio C Hamano5981e092006-04-26 16:55:25 -070023 die("%s is not a valid '%s' object", sha1_to_hex(sha1),
Shawn O. Pearce66035a62007-02-27 10:00:33 -050024 typename(expect));
Linus Torvaldsd0d7cbe2005-04-17 15:26:13 -070025}
26
Linus Torvaldse83c5162005-04-07 15:13:13 -070027/*
Junio C Hamanoc5bac172005-04-20 19:49:16 -070028 * Having more than two parents is not strange at all, and this is
29 * how multi-way merges are represented.
Linus Torvaldse83c5162005-04-07 15:13:13 -070030 */
31#define MAXPARENT (16)
Linus Torvaldsb3892372005-06-19 10:40:10 -070032static unsigned char parent_sha1[MAXPARENT][20];
Linus Torvaldse83c5162005-04-07 15:13:13 -070033
Petr Baudis4d1f1192005-07-29 11:01:26 +020034static const char commit_tree_usage[] = "git-commit-tree <sha1> [-p <sha1>]* < changelog";
Junio C Hamanoc5bac172005-04-20 19:49:16 -070035
Linus Torvaldsb3892372005-06-19 10:40:10 -070036static int new_parent(int idx)
37{
38 int i;
39 unsigned char *sha1 = parent_sha1[idx];
40 for (i = 0; i < idx; i++) {
David Rientjesa89fccd2006-08-17 11:54:57 -070041 if (!hashcmp(parent_sha1[i], sha1)) {
Linus Torvaldsb3892372005-06-19 10:40:10 -070042 error("duplicate parent %s ignored", sha1_to_hex(sha1));
43 return 0;
44 }
45 }
46 return 1;
47}
48
Johannes Schindelin9e832662006-12-22 22:06:08 +010049static 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
Linus Torvaldsa633fca2006-07-28 22:44:25 -070054int cmd_commit_tree(int argc, const char **argv, const char *prefix)
Linus Torvaldse83c5162005-04-07 15:13:13 -070055{
Linus Torvalds6aa33f42005-07-12 11:49:27 -070056 int i;
Linus Torvaldse83c5162005-04-07 15:13:13 -070057 int parents = 0;
58 unsigned char tree_sha1[20];
Linus Torvaldsd6d3f9d2005-04-09 17:09:34 -070059 unsigned char commit_sha1[20];
Pierre Habouzitaf6eb822007-09-06 13:20:09 +020060 struct strbuf buffer;
Junio C Hamano4b2bced2006-12-23 23:53:02 -080061 int encoding_is_utf8;
Linus Torvaldse83c5162005-04-07 15:13:13 -070062
Linus Torvaldse1b10392005-10-11 18:47:34 -070063 git_config(git_default_config);
64
Dmitry V. Levin31fff302006-05-09 01:43:38 +040065 if (argc < 2)
Junio C Hamanoc5bac172005-04-20 19:49:16 -070066 usage(commit_tree_usage);
Dmitry V. Levin31fff302006-05-09 01:43:38 +040067 if (get_sha1(argv[1], tree_sha1))
68 die("Not a valid object name %s", argv[1]);
Linus Torvaldse83c5162005-04-07 15:13:13 -070069
Shawn O. Pearce66035a62007-02-27 10:00:33 -050070 check_valid(tree_sha1, OBJ_TREE);
Linus Torvaldse83c5162005-04-07 15:13:13 -070071 for (i = 2; i < argc; i += 2) {
Peter Eriksen6d96ac12006-05-23 14:15:33 +020072 const char *a, *b;
Linus Torvaldse83c5162005-04-07 15:13:13 -070073 a = argv[i]; b = argv[i+1];
Dmitry V. Levin31fff302006-05-09 01:43:38 +040074 if (!b || strcmp(a, "-p"))
Junio C Hamanoc5bac172005-04-20 19:49:16 -070075 usage(commit_tree_usage);
Junio C Hamano2295e8d2006-12-21 23:49:13 -080076
77 if (parents >= MAXPARENT)
78 die("Too many parents (%d max)", MAXPARENT);
Dmitry V. Levin31fff302006-05-09 01:43:38 +040079 if (get_sha1(b, parent_sha1[parents]))
80 die("Not a valid object name %s", b);
Shawn O. Pearce66035a62007-02-27 10:00:33 -050081 check_valid(parent_sha1[parents], OBJ_COMMIT);
Linus Torvaldsb3892372005-06-19 10:40:10 -070082 if (new_parent(parents))
83 parents++;
Linus Torvaldse83c5162005-04-07 15:13:13 -070084 }
Linus Torvaldse83c5162005-04-07 15:13:13 -070085
Junio C Hamanod2c11a32006-12-27 16:41:33 -080086 /* Not having i18n.commitencoding is the same as having utf-8 */
Junio C Hamano677cfed2006-12-30 12:20:43 -080087 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
Junio C Hamano4b2bced2006-12-23 23:53:02 -080088
Pierre Habouzitf1696ee2007-09-10 12:35:04 +020089 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
Pierre Habouzitaf6eb822007-09-06 13:20:09 +020090 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree_sha1));
Linus Torvaldse83c5162005-04-07 15:13:13 -070091
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++)
Pierre Habouzitaf6eb822007-09-06 13:20:09 +020098 strbuf_addf(&buffer, "parent %s\n", sha1_to_hex(parent_sha1[i]));
Linus Torvaldse83c5162005-04-07 15:13:13 -070099
100 /* Person/date information */
Junio C Hamano774751a2007-12-08 17:32:08 -0800101 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));
Junio C Hamano4b2bced2006-12-23 23:53:02 -0800103 if (!encoding_is_utf8)
Pierre Habouzitaf6eb822007-09-06 13:20:09 +0200104 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
105 strbuf_addch(&buffer, '\n');
Linus Torvaldse83c5162005-04-07 15:13:13 -0700106
107 /* And add the comment */
Pierre Habouzitf1696ee2007-09-10 12:35:04 +0200108 if (strbuf_read(&buffer, 0, 0) < 0)
Pierre Habouzitaf6eb822007-09-06 13:20:09 +0200109 die("git-commit-tree: read returned %s", strerror(errno));
Linus Torvaldse83c5162005-04-07 15:13:13 -0700110
Johannes Schindelin9e832662006-12-22 22:06:08 +0100111 /* And check the encoding */
Pierre Habouzitaf6eb822007-09-06 13:20:09 +0200112 if (encoding_is_utf8 && !is_utf8(buffer.buf))
Johannes Schindelin9e832662006-12-22 22:06:08 +0100113 fprintf(stderr, commit_utf8_warn);
114
Pierre Habouzitaf6eb822007-09-06 13:20:09 +0200115 if (!write_sha1_file(buffer.buf, buffer.len, commit_type, commit_sha1)) {
Junio C Hamano7561d9f2006-03-24 22:23:25 -0800116 printf("%s\n", sha1_to_hex(commit_sha1));
117 return 0;
118 }
119 else
120 return 1;
Linus Torvaldse83c5162005-04-07 15:13:13 -0700121}