[PATCH] Teach read-tree about commit objects
[git/dscho.git] / read-tree.c
blob2773ce0cf171e9b54e9ed53bc9cc9e5d34f6a268
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
8 static int stage = 0;
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);
16 memset(ce, 0, 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,
27 unsigned long size,
28 const char *base, int baselen)
30 if (!buffer || strcmp(type, "tree"))
31 return -1;
32 while (size) {
33 int len = strlen(buffer)+1;
34 unsigned char *sha1 = buffer + len;
35 char *path = strchr(buffer, ' ')+1;
36 unsigned int mode;
38 if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
39 return -1;
41 buffer = sha1 + 20;
42 size -= len + 20;
44 if (S_ISDIR(mode)) {
45 int retval;
46 int pathlen = strlen(path);
47 char *newbase = malloc(baselen + 1 + pathlen);
48 void *eltbuf;
49 char elttype[20];
50 unsigned long eltsize;
52 eltbuf = read_sha1_file(sha1, elttype, &eltsize);
53 if (!eltbuf)
54 return -1;
55 memcpy(newbase, base, baselen);
56 memcpy(newbase + baselen, path, pathlen);
57 newbase[baselen + pathlen] = '/';
58 retval = read_tree_recursive(eltbuf, elttype, eltsize,
59 newbase,
60 baselen + pathlen + 1);
61 free(eltbuf);
62 free(newbase);
63 if (retval)
64 return -1;
65 continue;
67 if (read_one_entry(sha1, base, baselen, path, mode) < 0)
68 return -1;
70 return 0;
73 static int read_tree(unsigned char *sha1, const char *base, int baselen)
75 void *buffer;
76 unsigned long size;
78 buffer = read_tree_with_tree_or_commit_sha1(sha1, &size, 0);
79 return read_tree_recursive(buffer, "tree", size, base, baselen);
82 static int remove_lock = 0;
84 static void remove_lock_file(void)
86 if (remove_lock)
87 unlink(".git/index.lock");
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
120 * any name merging
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))
126 return NULL;
129 * Ok, all three entries describe the same
130 * filename, but maybe the contents or file
131 * mode have changed?
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,
137 * take the other.
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.
145 if (same(b,c))
146 return c;
147 if (same(a,b))
148 return c;
149 if (same(a,c))
150 return b;
151 return NULL;
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;
160 while (nr) {
161 struct cache_entry *ce, *result;
163 ce = src[0];
165 /* We throw away original cache entries except for the stat information */
166 if (!ce_stage(ce)) {
167 old = ce;
168 src++;
169 nr--;
170 active_nr--;
171 continue;
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))
179 *result = *old;
180 ce = result;
181 ce->ce_flags &= ~htons(CE_STAGEMASK);
182 src += 2;
183 nr -= 2;
184 active_nr -= 2;
186 *dst++ = ce;
187 src++;
188 nr--;
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;
198 while (nr) {
199 struct cache_entry *ce;
201 ce = src[0];
203 /* We throw away original cache entries except for the stat information */
204 if (!ce_stage(ce)) {
205 old = ce;
206 src++;
207 nr--;
208 active_nr--;
209 continue;
211 if (path_matches(ce, old) && same(ce, old))
212 *ce = *old;
213 ce->ce_flags &= ~htons(CE_STAGEMASK);
214 *dst++ = ce;
215 src++;
216 nr--;
220 int main(int argc, char **argv)
222 int i, newfd, merge;
223 unsigned char sha1[20];
225 newfd = open(".git/index.lock", O_RDWR | O_CREAT | O_EXCL, 0600);
226 if (newfd < 0)
227 die("unable to create new cachefile");
228 atexit(remove_lock_file);
229 remove_lock = 1;
231 merge = 0;
232 for (i = 1; i < argc; i++) {
233 const char *arg = argv[i];
235 /* "-m" stands for "merge", meaning we start in stage 1 */
236 if (!strcmp(arg, "-m")) {
237 int i;
238 if (stage)
239 usage("-m needs to come first");
240 read_cache();
241 for (i = 0; i < active_nr; i++) {
242 if (ce_stage(active_cache[i]))
243 usage("you need to resolve your current index first");
245 stage = 1;
246 merge = 1;
247 continue;
249 if (get_sha1_hex(arg, sha1) < 0)
250 usage("read-tree [-m] <sha1>");
251 if (stage > 3)
252 usage("can't merge more than two trees");
253 if (read_tree(sha1, "", 0) < 0)
254 die("failed to unpack tree object %s", arg);
255 stage++;
257 if (merge) {
258 switch (stage) {
259 case 4: /* Three-way merge */
260 trivially_merge_cache(active_cache, active_nr);
261 break;
262 case 2: /* Just read a tree, merge with old cache contents */
263 merge_stat_info(active_cache, active_nr);
264 break;
265 default:
266 die("just how do you expect me to merge %d trees?", stage-1);
269 if (write_cache(newfd, active_cache, active_nr) ||
270 rename(".git/index.lock", ".git/index"))
271 die("unable to write new index file");
272 remove_lock = 0;
273 return 0;