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_recursive(void *buffer, unsigned long size,
27 const char *base, int baselen)
30 int len = strlen(buffer)+1;
31 unsigned char *sha1 = buffer + len;
32 char *path = strchr(buffer, ' ')+1;
35 if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
43 int pathlen = strlen(path);
44 char *newbase = malloc(baselen + 1 + pathlen);
47 unsigned long eltsize;
49 eltbuf = read_sha1_file(sha1, elttype, &eltsize);
50 if (!eltbuf || strcmp(elttype, "tree"))
52 memcpy(newbase, base, baselen);
53 memcpy(newbase + baselen, path, pathlen);
54 newbase[baselen + pathlen] = '/';
55 retval = read_tree_recursive(eltbuf, eltsize,
57 baselen + pathlen + 1);
64 if (read_one_entry(sha1, base, baselen, path, mode) < 0)
70 static int read_tree(unsigned char *sha1, const char *base, int baselen)
75 buffer = read_tree_with_tree_or_commit_sha1(sha1, &size, 0);
78 return read_tree_recursive(buffer, size, base, baselen);
81 static char *lockfile_name;
83 static void remove_lock_file(void)
86 unlink(lockfile_name);
89 static int path_matches(struct cache_entry *a, struct cache_entry *b)
91 int len = ce_namelen(a);
92 return ce_namelen(b) == len &&
93 !memcmp(a->name, b->name, len);
96 static int same(struct cache_entry *a, struct cache_entry *b)
98 return a->ce_mode == b->ce_mode &&
99 !memcmp(a->sha1, b->sha1, 20);
104 * This removes all trivial merges that don't change the tree
105 * and collapses them to state 0.
107 * _Any_ other merge is left to user policy. That includes "both
108 * created the same file", and "both removed the same file" - which are
109 * trivial, but the user might still want to _note_ it.
111 static struct cache_entry *merge_entries(struct cache_entry *a,
112 struct cache_entry *b,
113 struct cache_entry *c)
115 int len = ce_namelen(a);
118 * Are they all the same filename? We won't do
121 if (ce_namelen(b) != len ||
122 ce_namelen(c) != len ||
123 memcmp(a->name, b->name, len) ||
124 memcmp(a->name, c->name, len))
128 * Ok, all three entries describe the same
129 * filename, but maybe the contents or file
132 * The trivial cases end up being the ones where two
133 * out of three files are the same:
134 * - both destinations the same, trivially take either
135 * - one of the destination versions hasn't changed,
138 * The "all entries exactly the same" case falls out as
139 * a special case of any of the "two same" cases.
141 * Here "a" is "original", and "b" and "c" are the two
142 * trees we are merging.
153 static void trivially_merge_cache(struct cache_entry **src, int nr)
155 static struct cache_entry null_entry;
156 struct cache_entry **dst = src;
157 struct cache_entry *old = &null_entry;
160 struct cache_entry *ce, *result;
164 /* We throw away original cache entries except for the stat information */
172 if (nr > 2 && (result = merge_entries(ce, src[1], src[2])) != NULL) {
174 * See if we can re-use the old CE directly?
175 * That way we get the uptodate stat info.
177 if (path_matches(result, old) && same(result, old))
180 ce->ce_flags &= ~htons(CE_STAGEMASK);
191 static void merge_stat_info(struct cache_entry **src, int nr)
193 static struct cache_entry null_entry;
194 struct cache_entry **dst = src;
195 struct cache_entry *old = &null_entry;
198 struct cache_entry *ce;
202 /* We throw away original cache entries except for the stat information */
210 if (path_matches(ce, old) && same(ce, old))
212 ce->ce_flags &= ~htons(CE_STAGEMASK);
219 static char *read_tree_usage = "read-tree (<sha> | -m <sha1> [<sha2> <sha3>])";
221 int main(int argc, char **argv)
224 unsigned char sha1[20];
225 static char lockfile[MAXPATHLEN+1];
226 const char *indexfile = get_index_file();
228 snprintf(lockfile, sizeof(lockfile), "%s.lock", indexfile);
230 newfd = open(lockfile, O_RDWR | O_CREAT | O_EXCL, 0600);
232 die("unable to create new cachefile");
233 atexit(remove_lock_file);
234 lockfile_name = lockfile;
237 for (i = 1; i < argc; i++) {
238 const char *arg = argv[i];
240 /* "-m" stands for "merge", meaning we start in stage 1 */
241 if (!strcmp(arg, "-m")) {
244 die("-m needs to come first");
246 for (i = 0; i < active_nr; i++) {
247 if (ce_stage(active_cache[i]))
248 die("you need to resolve your current index first");
254 if (get_sha1_hex(arg, sha1) < 0)
255 usage(read_tree_usage);
257 usage(read_tree_usage);
258 if (read_tree(sha1, "", 0) < 0)
259 die("failed to unpack tree object %s", arg);
264 case 4: /* Three-way merge */
265 trivially_merge_cache(active_cache, active_nr);
267 case 2: /* Just read a tree, merge with old cache contents */
268 merge_stat_info(active_cache, active_nr);
271 die("just how do you expect me to merge %d trees?", stage-1);
274 if (write_cache(newfd, active_cache, active_nr) || rename(lockfile, indexfile))
275 die("unable to write new index file");
276 lockfile_name = NULL;