1/* 2 * GIT - The information manager from hell 3 * 4 * Copyright (C) Linus Torvalds, 2005 5 */ 6#include"cache.h" 7#include"commit.h" 8#include"tree.h" 9#include"builtin.h" 10#include"utf8.h" 11 12#define BLOCKING (1ul << 14) 13 14/* 15 * FIXME! Share the code with "write-tree.c" 16 */ 17static voidcheck_valid(unsigned char*sha1,enum object_type expect) 18{ 19enum object_type type =sha1_object_info(sha1, NULL); 20if(type <0) 21die("%sis not a valid object",sha1_to_hex(sha1)); 22if(type != expect) 23die("%sis not a valid '%s' object",sha1_to_hex(sha1), 24typename(expect)); 25} 26 27/* 28 * Having more than two parents is not strange at all, and this is 29 * how multi-way merges are represented. 30 */ 31#define MAXPARENT (16) 32static unsigned char parent_sha1[MAXPARENT][20]; 33 34static const char commit_tree_usage[] ="git-commit-tree <sha1> [-p <sha1>]* < changelog"; 35 36static intnew_parent(int idx) 37{ 38int i; 39unsigned char*sha1 = parent_sha1[idx]; 40for(i =0; i < idx; i++) { 41if(!hashcmp(parent_sha1[i], sha1)) { 42error("duplicate parent%signored",sha1_to_hex(sha1)); 43return0; 44} 45} 46return1; 47} 48 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 54intcmd_commit_tree(int argc,const char**argv,const char*prefix) 55{ 56int i; 57int parents =0; 58unsigned char tree_sha1[20]; 59unsigned char commit_sha1[20]; 60struct strbuf buffer; 61int encoding_is_utf8; 62 63git_config(git_default_config); 64 65if(argc <2) 66usage(commit_tree_usage); 67if(get_sha1(argv[1], tree_sha1)) 68die("Not a valid object name%s", argv[1]); 69 70check_valid(tree_sha1, OBJ_TREE); 71for(i =2; i < argc; i +=2) { 72const char*a, *b; 73 a = argv[i]; b = argv[i+1]; 74if(!b ||strcmp(a,"-p")) 75usage(commit_tree_usage); 76 77if(parents >= MAXPARENT) 78die("Too many parents (%dmax)", MAXPARENT); 79if(get_sha1(b, parent_sha1[parents])) 80die("Not a valid object name%s", b); 81check_valid(parent_sha1[parents], OBJ_COMMIT); 82if(new_parent(parents)) 83 parents++; 84} 85 86/* Not having i18n.commitencoding is the same as having utf-8 */ 87 encoding_is_utf8 =is_encoding_utf8(git_commit_encoding); 88 89strbuf_init(&buffer,8192);/* should avoid reallocs for the headers */ 90strbuf_addf(&buffer,"tree%s\n",sha1_to_hex(tree_sha1)); 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 */ 97for(i =0; i < parents; i++) 98strbuf_addf(&buffer,"parent%s\n",sha1_to_hex(parent_sha1[i])); 99 100/* Person/date information */ 101strbuf_addf(&buffer,"author%s\n",git_author_info(1)); 102strbuf_addf(&buffer,"committer%s\n",git_committer_info(1)); 103if(!encoding_is_utf8) 104strbuf_addf(&buffer,"encoding%s\n", git_commit_encoding); 105strbuf_addch(&buffer,'\n'); 106 107/* And add the comment */ 108if(strbuf_read(&buffer,0,0) <0) 109die("git-commit-tree: read returned%s",strerror(errno)); 110 111/* And check the encoding */ 112if(encoding_is_utf8 && !is_utf8(buffer.buf)) 113fprintf(stderr, commit_utf8_warn); 114 115if(!write_sha1_file(buffer.buf, buffer.len, commit_type, commit_sha1)) { 116printf("%s\n",sha1_to_hex(commit_sha1)); 117return0; 118} 119else 120return1; 121}