2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
10 static int read_one_entry(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode)
12 int len = strlen(pathname);
13 unsigned int size = cache_entry_size(baselen + len);
14 struct cache_entry *ce = malloc(size);
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);
26 static int read_tree(unsigned char *sha1, const char *base, int baselen)
32 buffer = read_sha1_file(sha1, type, &size);
35 if (strcmp(type, "tree"))
38 int len = strlen(buffer)+1;
39 unsigned char *sha1 = buffer + len;
40 char *path = strchr(buffer, ' ')+1;
43 if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
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);
62 if (read_one_entry(sha1, base, baselen, path, mode) < 0)
68 static int remove_lock = 0;
70 static void remove_lock_file(void)
73 unlink(".git/index.lock");
76 static int path_matches(struct cache_entry *a, struct cache_entry *b)
78 int len = ce_namelen(a);
79 return ce_namelen(b) == len &&
80 !memcmp(a->name, b->name, len);
83 static int same(struct cache_entry *a, struct cache_entry *b)
85 return a->ce_mode == b->ce_mode &&
86 !memcmp(a->sha1, b->sha1, 20);
91 * This removes all trivial merges that don't change the tree
92 * and collapses them to state 0.
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.
98 static struct cache_entry *merge_entries(struct cache_entry *a,
99 struct cache_entry *b,
100 struct cache_entry *c)
102 int len = ce_namelen(a);
105 * Are they all the same filename? We won't do
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))
115 * Ok, all three entries describe the same
116 * filename, but maybe the contents or file
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,
125 * The "all entries exactly the same" case falls out as
126 * a special case of any of the "two same" cases.
128 * Here "a" is "original", and "b" and "c" are the two
129 * trees we are merging.
140 static void trivially_merge_cache(struct cache_entry **src, int nr)
142 static struct cache_entry null_entry;
143 struct cache_entry **dst = src;
144 struct cache_entry *old = &null_entry;
147 struct cache_entry *ce, *result;
151 /* We throw away original cache entries except for the stat information */
159 if (nr > 2 && (result = merge_entries(ce, src[1], src[2])) != NULL) {
161 * See if we can re-use the old CE directly?
162 * That way we get the uptodate stat info.
164 if (path_matches(result, old) && same(result, old))
167 ce->ce_flags &= ~htons(CE_STAGEMASK);
179 int main(int argc, char **argv)
182 unsigned char sha1[20];
184 newfd = open(".git/index.lock", O_RDWR | O_CREAT | O_EXCL, 0600);
186 die("unable to create new cachefile");
187 atexit(remove_lock_file);
191 for (i = 1; i < argc; i++) {
192 const char *arg = argv[i];
194 /* "-m" stands for "merge", meaning we start in stage 1 */
195 if (!strcmp(arg, "-m")) {
198 usage("-m needs to come first");
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");
208 if (get_sha1_hex(arg, sha1) < 0)
209 usage("read-tree [-m] <sha1>");
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);
218 usage("I need three trees to merge: original, branch1 and branch2");
219 trivially_merge_cache(active_cache, active_nr);
221 if (write_cache(newfd, active_cache, active_nr) ||
222 rename(".git/index.lock", ".git/index"))
223 die("unable to write new index file");