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, const char *type,
28 const char *base, int baselen)
30 if (!buffer || strcmp(type, "tree"))
33 int len = strlen(buffer)+1;
34 unsigned char *sha1 = buffer + len;
35 char *path = strchr(buffer, ' ')+1;
38 if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
46 int pathlen = strlen(path);
47 char *newbase = malloc(baselen + 1 + pathlen);
50 unsigned long eltsize;
52 eltbuf = read_sha1_file(sha1, elttype, &eltsize);
55 memcpy(newbase, base, baselen);
56 memcpy(newbase + baselen, path, pathlen);
57 newbase[baselen + pathlen] = '/';
58 retval = read_tree_recursive(eltbuf, elttype, eltsize,
60 baselen + pathlen + 1);
67 if (read_one_entry(sha1, base, baselen, path, mode) < 0)
73 static int read_tree(unsigned char *sha1, const char *base, int baselen)
78 buffer = read_tree_with_tree_or_commit_sha1(sha1, &size, 0);
79 return read_tree_recursive(buffer, "tree", size, base, baselen);
82 static char *lockfile_name;
84 static void remove_lock_file(void)
87 unlink(lockfile_name);
90 static int path_matches(struct cache_entry *a, struct cache_entry *b)
92 int len = ce_namelen(a);
93 return ce_namelen(b) == len &&
94 !memcmp(a->name, b->name, len);
97 static int same(struct cache_entry *a, struct cache_entry *b)
99 return a->ce_mode == b->ce_mode &&
100 !memcmp(a->sha1, b->sha1, 20);
105 * This removes all trivial merges that don't change the tree
106 * and collapses them to state 0.
108 * _Any_ other merge is left to user policy. That includes "both
109 * created the same file", and "both removed the same file" - which are
110 * trivial, but the user might still want to _note_ it.
112 static struct cache_entry *merge_entries(struct cache_entry *a,
113 struct cache_entry *b,
114 struct cache_entry *c)
116 int len = ce_namelen(a);
119 * Are they all the same filename? We won't do
122 if (ce_namelen(b) != len ||
123 ce_namelen(c) != len ||
124 memcmp(a->name, b->name, len) ||
125 memcmp(a->name, c->name, len))
129 * Ok, all three entries describe the same
130 * filename, but maybe the contents or file
133 * The trivial cases end up being the ones where two
134 * out of three files are the same:
135 * - both destinations the same, trivially take either
136 * - one of the destination versions hasn't changed,
139 * The "all entries exactly the same" case falls out as
140 * a special case of any of the "two same" cases.
142 * Here "a" is "original", and "b" and "c" are the two
143 * trees we are merging.
154 static void trivially_merge_cache(struct cache_entry **src, int nr)
156 static struct cache_entry null_entry;
157 struct cache_entry **dst = src;
158 struct cache_entry *old = &null_entry;
161 struct cache_entry *ce, *result;
165 /* We throw away original cache entries except for the stat information */
173 if (nr > 2 && (result = merge_entries(ce, src[1], src[2])) != NULL) {
175 * See if we can re-use the old CE directly?
176 * That way we get the uptodate stat info.
178 if (path_matches(result, old) && same(result, old))
181 ce->ce_flags &= ~htons(CE_STAGEMASK);
192 static void merge_stat_info(struct cache_entry **src, int nr)
194 static struct cache_entry null_entry;
195 struct cache_entry **dst = src;
196 struct cache_entry *old = &null_entry;
199 struct cache_entry *ce;
203 /* We throw away original cache entries except for the stat information */
211 if (path_matches(ce, old) && same(ce, old))
213 ce->ce_flags &= ~htons(CE_STAGEMASK);
220 static char *read_tree_usage = "read-tree (<sha> | -m <sha1> [<sha2> <sha3>])";
222 int main(int argc, char **argv)
225 unsigned char sha1[20];
226 static char lockfile[MAXPATHLEN+1];
227 const char *indexfile = get_index_file();
229 snprintf(lockfile, sizeof(lockfile), "%s.lock", indexfile);
231 newfd = open(lockfile, O_RDWR | O_CREAT | O_EXCL, 0600);
233 die("unable to create new cachefile");
234 atexit(remove_lock_file);
235 lockfile_name = lockfile;
238 for (i = 1; i < argc; i++) {
239 const char *arg = argv[i];
241 /* "-m" stands for "merge", meaning we start in stage 1 */
242 if (!strcmp(arg, "-m")) {
245 die("-m needs to come first");
247 for (i = 0; i < active_nr; i++) {
248 if (ce_stage(active_cache[i]))
249 die("you need to resolve your current index first");
255 if (get_sha1_hex(arg, sha1) < 0)
256 usage(read_tree_usage);
258 usage(read_tree_usage);
259 if (read_tree(sha1, "", 0) < 0)
260 die("failed to unpack tree object %s", arg);
265 case 4: /* Three-way merge */
266 trivially_merge_cache(active_cache, active_nr);
268 case 2: /* Just read a tree, merge with old cache contents */
269 merge_stat_info(active_cache, active_nr);
272 die("just how do you expect me to merge %d trees?", stage-1);
275 if (write_cache(newfd, active_cache, active_nr) || rename(lockfile, indexfile))
276 die("unable to write new index file");
277 lockfile_name = NULL;