1/* 2 * GIT - The information manager from hell 3 * 4 * Copyright (C) Linus Torvalds, 2005 5 */ 6#include "cache.h" 7 8static int stage = 0; 9 10static int read_one_entry(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode) 11{ 12 int len = strlen(pathname); 13 unsigned int size = cache_entry_size(baselen + len); 14 struct cache_entry *ce = malloc(size); 15 16 memset(ce, 0, size); 17 18 ce->ce_mode = create_ce_mode(mode); 19 ce->ce_flags = create_ce_flags(baselen + len, stage); 20 memcpy(ce->name, base, baselen); 21 memcpy(ce->name + baselen, pathname, len+1); 22 memcpy(ce->sha1, sha1, 20); 23 return add_cache_entry(ce, 1); 24} 25 26static int read_tree(unsigned char *sha1, const char *base, int baselen) 27{ 28 void *buffer; 29 unsigned long size; 30 char type[20]; 31 32 buffer = read_sha1_file(sha1, type, &size); 33 if (!buffer) 34 return -1; 35 if (strcmp(type, "tree")) 36 return -1; 37 while (size) { 38 int len = strlen(buffer)+1; 39 unsigned char *sha1 = buffer + len; 40 char *path = strchr(buffer, ' ')+1; 41 unsigned int mode; 42 43 if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1) 44 return -1; 45 46 buffer = sha1 + 20; 47 size -= len + 20; 48 49 if (S_ISDIR(mode)) { 50 int retval; 51 int pathlen = strlen(path); 52 char *newbase = malloc(baselen + 1 + pathlen); 53 memcpy(newbase, base, baselen); 54 memcpy(newbase + baselen, path, pathlen); 55 newbase[baselen + pathlen] = '/'; 56 retval = read_tree(sha1, newbase, baselen + pathlen + 1); 57 free(newbase); 58 if (retval) 59 return -1; 60 continue; 61 } 62 if (read_one_entry(sha1, base, baselen, path, mode) < 0) 63 return -1; 64 } 65 return 0; 66} 67 68static int remove_lock = 0; 69 70static void remove_lock_file(void) 71{ 72 if (remove_lock) 73 unlink(".git/index.lock"); 74} 75 76static int path_matches(struct cache_entry *a, struct cache_entry *b) 77{ 78 int len = ce_namelen(a); 79 return ce_namelen(b) == len && 80 !memcmp(a->name, b->name, len); 81} 82 83static int same(struct cache_entry *a, struct cache_entry *b) 84{ 85 return a->ce_mode == b->ce_mode && 86 !memcmp(a->sha1, b->sha1, 20); 87} 88 89 90/* 91 * This removes all trivial merges that don't change the tree 92 * and collapses them to state 0. 93 * 94 * _Any_ other merge is left to user policy. That includes "both 95 * created the same file", and "both removed the same file" - which are 96 * trivial, but the user might still want to _note_ it. 97 */ 98static struct cache_entry *merge_entries(struct cache_entry *a, 99 struct cache_entry *b, 100 struct cache_entry *c) 101{ 102 int len = ce_namelen(a); 103 104 /* 105 * Are they all the same filename? We won't do 106 * any name merging 107 */ 108 if (ce_namelen(b) != len || 109 ce_namelen(c) != len || 110 memcmp(a->name, b->name, len) || 111 memcmp(a->name, c->name, len)) 112 return NULL; 113 114 /* 115 * Ok, all three entries describe the same 116 * filename, but maybe the contents or file 117 * mode have changed? 118 * 119 * The trivial cases end up being the ones where two 120 * out of three files are the same: 121 * - both destinations the same, trivially take either 122 * - one of the destination versions hasn't changed, 123 * take the other. 124 * 125 * The "all entries exactly the same" case falls out as 126 * a special case of any of the "two same" cases. 127 * 128 * Here "a" is "original", and "b" and "c" are the two 129 * trees we are merging. 130 */ 131 if (same(b,c)) 132 return c; 133 if (same(a,b)) 134 return c; 135 if (same(a,c)) 136 return b; 137 return NULL; 138} 139 140static void trivially_merge_cache(struct cache_entry **src, int nr) 141{ 142 static struct cache_entry null_entry; 143 struct cache_entry **dst = src; 144 struct cache_entry *old = &null_entry; 145 146 while (nr) { 147 struct cache_entry *ce, *result; 148 149 ce = src[0]; 150 151 /* We throw away original cache entries except for the stat information */ 152 if (!ce_stage(ce)) { 153 old = ce; 154 src++; 155 nr--; 156 active_nr--; 157 continue; 158 } 159 if (nr > 2 && (result = merge_entries(ce, src[1], src[2])) != NULL) { 160 /* 161 * See if we can re-use the old CE directly? 162 * That way we get the uptodate stat info. 163 */ 164 if (path_matches(result, old) && same(result, old)) 165 *result = *old; 166 ce = result; 167 ce->ce_flags &= ~htons(CE_STAGEMASK); 168 src += 2; 169 nr -= 2; 170 active_nr -= 2; 171 } 172 *dst = ce; 173 src++; 174 dst++; 175 nr--; 176 } 177} 178 179int main(int argc, char **argv) 180{ 181 int i, newfd, merge; 182 unsigned char sha1[20]; 183 184 newfd = open(".git/index.lock", O_RDWR | O_CREAT | O_EXCL, 0600); 185 if (newfd < 0) 186 die("unable to create new cachefile"); 187 atexit(remove_lock_file); 188 remove_lock = 1; 189 190 merge = 0; 191 for (i = 1; i < argc; i++) { 192 const char *arg = argv[i]; 193 194 /* "-m" stands for "merge", meaning we start in stage 1 */ 195 if (!strcmp(arg, "-m")) { 196 int i; 197 if (stage) 198 usage("-m needs to come first"); 199 read_cache(); 200 for (i = 0; i < active_nr; i++) { 201 if (ce_stage(active_cache[i])) 202 usage("you need to resolve your current index first"); 203 } 204 stage = 1; 205 merge = 1; 206 continue; 207 } 208 if (get_sha1_hex(arg, sha1) < 0) 209 usage("read-tree [-m] <sha1>"); 210 if (stage > 3) 211 usage("can't merge more than two trees"); 212 if (read_tree(sha1, "", 0) < 0) 213 die("failed to unpack tree object %s", arg); 214 stage++; 215 } 216 if (merge) { 217 if (stage != 4) 218 usage("I need three trees to merge: original, branch1 and branch2"); 219 trivially_merge_cache(active_cache, active_nr); 220 } 221 if (write_cache(newfd, active_cache, active_nr) || 222 rename(".git/index.lock", ".git/index")) 223 die("unable to write new index file"); 224 remove_lock = 0; 225 return 0; 226}