Linus Torvalds | 8bc9a0c | 2005-04-07 15:16:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * GIT - The information manager from hell |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
| 5 | */ |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 6 | #include "cache.h" |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 7 | #include "commit.h" |
| 8 | #include "tree.h" |
Peter Eriksen | 6d96ac1 | 2006-05-23 14:15:33 +0200 | [diff] [blame] | 9 | #include "builtin.h" |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 10 | #include "utf8.h" |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 11 | |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 12 | #define BLOCKING (1ul << 14) |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 13 | |
| 14 | /* |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 15 | * FIXME! Share the code with "write-tree.c" |
| 16 | */ |
Shawn O. Pearce | 66035a6 | 2007-02-27 10:00:33 -0500 | [diff] [blame] | 17 | static void check_valid(unsigned char *sha1, enum object_type expect) |
Linus Torvalds | d0d7cbe | 2005-04-17 15:26:13 -0700 | [diff] [blame] | 18 | { |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 19 | enum object_type type = sha1_object_info(sha1, NULL); |
| 20 | if (type < 0) |
Junio C Hamano | 5981e09 | 2006-04-26 16:55:25 -0700 | [diff] [blame] | 21 | die("%s is not a valid object", sha1_to_hex(sha1)); |
Shawn O. Pearce | 66035a6 | 2007-02-27 10:00:33 -0500 | [diff] [blame] | 22 | if (type != expect) |
Junio C Hamano | 5981e09 | 2006-04-26 16:55:25 -0700 | [diff] [blame] | 23 | die("%s is not a valid '%s' object", sha1_to_hex(sha1), |
Shawn O. Pearce | 66035a6 | 2007-02-27 10:00:33 -0500 | [diff] [blame] | 24 | typename(expect)); |
Linus Torvalds | d0d7cbe | 2005-04-17 15:26:13 -0700 | [diff] [blame] | 25 | } |
| 26 | |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 27 | /* |
Junio C Hamano | c5bac17 | 2005-04-20 19:49:16 -0700 | [diff] [blame] | 28 | * Having more than two parents is not strange at all, and this is |
| 29 | * how multi-way merges are represented. |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 30 | */ |
| 31 | #define MAXPARENT (16) |
Linus Torvalds | b389237 | 2005-06-19 10:40:10 -0700 | [diff] [blame] | 32 | static unsigned char parent_sha1[MAXPARENT][20]; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 33 | |
Petr Baudis | 4d1f119 | 2005-07-29 11:01:26 +0200 | [diff] [blame] | 34 | static const char commit_tree_usage[] = "git-commit-tree <sha1> [-p <sha1>]* < changelog"; |
Junio C Hamano | c5bac17 | 2005-04-20 19:49:16 -0700 | [diff] [blame] | 35 | |
Linus Torvalds | b389237 | 2005-06-19 10:40:10 -0700 | [diff] [blame] | 36 | static int new_parent(int idx) |
| 37 | { |
| 38 | int i; |
| 39 | unsigned char *sha1 = parent_sha1[idx]; |
| 40 | for (i = 0; i < idx; i++) { |
David Rientjes | a89fccd | 2006-08-17 11:54:57 -0700 | [diff] [blame] | 41 | if (!hashcmp(parent_sha1[i], sha1)) { |
Linus Torvalds | b389237 | 2005-06-19 10:40:10 -0700 | [diff] [blame] | 42 | error("duplicate parent %s ignored", sha1_to_hex(sha1)); |
| 43 | return 0; |
| 44 | } |
| 45 | } |
| 46 | return 1; |
| 47 | } |
| 48 | |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 49 | static 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 Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 54 | int cmd_commit_tree(int argc, const char **argv, const char *prefix) |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 55 | { |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 56 | int i; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 57 | int parents = 0; |
| 58 | unsigned char tree_sha1[20]; |
Linus Torvalds | d6d3f9d | 2005-04-09 17:09:34 -0700 | [diff] [blame] | 59 | unsigned char commit_sha1[20]; |
Pierre Habouzit | af6eb82 | 2007-09-06 13:20:09 +0200 | [diff] [blame] | 60 | struct strbuf buffer; |
Junio C Hamano | 4b2bced | 2006-12-23 23:53:02 -0800 | [diff] [blame] | 61 | int encoding_is_utf8; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 62 | |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 63 | git_config(git_default_config); |
| 64 | |
Dmitry V. Levin | 31fff30 | 2006-05-09 01:43:38 +0400 | [diff] [blame] | 65 | if (argc < 2) |
Junio C Hamano | c5bac17 | 2005-04-20 19:49:16 -0700 | [diff] [blame] | 66 | usage(commit_tree_usage); |
Dmitry V. Levin | 31fff30 | 2006-05-09 01:43:38 +0400 | [diff] [blame] | 67 | if (get_sha1(argv[1], tree_sha1)) |
| 68 | die("Not a valid object name %s", argv[1]); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 69 | |
Shawn O. Pearce | 66035a6 | 2007-02-27 10:00:33 -0500 | [diff] [blame] | 70 | check_valid(tree_sha1, OBJ_TREE); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 71 | for (i = 2; i < argc; i += 2) { |
Peter Eriksen | 6d96ac1 | 2006-05-23 14:15:33 +0200 | [diff] [blame] | 72 | const char *a, *b; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 73 | a = argv[i]; b = argv[i+1]; |
Dmitry V. Levin | 31fff30 | 2006-05-09 01:43:38 +0400 | [diff] [blame] | 74 | if (!b || strcmp(a, "-p")) |
Junio C Hamano | c5bac17 | 2005-04-20 19:49:16 -0700 | [diff] [blame] | 75 | usage(commit_tree_usage); |
Junio C Hamano | 2295e8d | 2006-12-21 23:49:13 -0800 | [diff] [blame] | 76 | |
| 77 | if (parents >= MAXPARENT) |
| 78 | die("Too many parents (%d max)", MAXPARENT); |
Dmitry V. Levin | 31fff30 | 2006-05-09 01:43:38 +0400 | [diff] [blame] | 79 | if (get_sha1(b, parent_sha1[parents])) |
| 80 | die("Not a valid object name %s", b); |
Shawn O. Pearce | 66035a6 | 2007-02-27 10:00:33 -0500 | [diff] [blame] | 81 | check_valid(parent_sha1[parents], OBJ_COMMIT); |
Linus Torvalds | b389237 | 2005-06-19 10:40:10 -0700 | [diff] [blame] | 82 | if (new_parent(parents)) |
| 83 | parents++; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 84 | } |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 85 | |
Junio C Hamano | d2c11a3 | 2006-12-27 16:41:33 -0800 | [diff] [blame] | 86 | /* Not having i18n.commitencoding is the same as having utf-8 */ |
Junio C Hamano | 677cfed | 2006-12-30 12:20:43 -0800 | [diff] [blame] | 87 | encoding_is_utf8 = is_encoding_utf8(git_commit_encoding); |
Junio C Hamano | 4b2bced | 2006-12-23 23:53:02 -0800 | [diff] [blame] | 88 | |
Pierre Habouzit | f1696ee | 2007-09-10 12:35:04 +0200 | [diff] [blame] | 89 | strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */ |
Pierre Habouzit | af6eb82 | 2007-09-06 13:20:09 +0200 | [diff] [blame] | 90 | strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree_sha1)); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 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++) |
Pierre Habouzit | af6eb82 | 2007-09-06 13:20:09 +0200 | [diff] [blame] | 98 | strbuf_addf(&buffer, "parent %s\n", sha1_to_hex(parent_sha1[i])); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 99 | |
| 100 | /* Person/date information */ |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 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)); |
Junio C Hamano | 4b2bced | 2006-12-23 23:53:02 -0800 | [diff] [blame] | 103 | if (!encoding_is_utf8) |
Pierre Habouzit | af6eb82 | 2007-09-06 13:20:09 +0200 | [diff] [blame] | 104 | strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding); |
| 105 | strbuf_addch(&buffer, '\n'); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 106 | |
| 107 | /* And add the comment */ |
Pierre Habouzit | f1696ee | 2007-09-10 12:35:04 +0200 | [diff] [blame] | 108 | if (strbuf_read(&buffer, 0, 0) < 0) |
Pierre Habouzit | af6eb82 | 2007-09-06 13:20:09 +0200 | [diff] [blame] | 109 | die("git-commit-tree: read returned %s", strerror(errno)); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 110 | |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 111 | /* And check the encoding */ |
Pierre Habouzit | af6eb82 | 2007-09-06 13:20:09 +0200 | [diff] [blame] | 112 | if (encoding_is_utf8 && !is_utf8(buffer.buf)) |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 113 | fprintf(stderr, commit_utf8_warn); |
| 114 | |
Pierre Habouzit | af6eb82 | 2007-09-06 13:20:09 +0200 | [diff] [blame] | 115 | if (!write_sha1_file(buffer.buf, buffer.len, commit_type, commit_sha1)) { |
Junio C Hamano | 7561d9f | 2006-03-24 22:23:25 -0800 | [diff] [blame] | 116 | printf("%s\n", sha1_to_hex(commit_sha1)); |
| 117 | return 0; |
| 118 | } |
| 119 | else |
| 120 | return 1; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 121 | } |