]> git.ipfire.org Git - thirdparty/git.git/blame - read-tree.c
Add "__noreturn__" attribute to die() and usage()
[thirdparty/git.git] / read-tree.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c5163
LT
6#include "cache.h"
7
d99082e0 8static int stage = 0;
220a0b52 9static int update = 0;
d99082e0 10
94537c78 11static int unpack_tree(unsigned char *sha1)
b12ec373
JH
12{
13 void *buffer;
14 unsigned long size;
14242464 15 int ret;
b12ec373 16
e99d59ff 17 buffer = read_object_with_reference(sha1, "tree", &size, NULL);
aba06682
LT
18 if (!buffer)
19 return -1;
14242464
PB
20 ret = read_tree(buffer, size, stage);
21 free(buffer);
22 return ret;
b12ec373
JH
23}
24
bb233d69 25static char *lockfile_name;
9614b8dc
LT
26
27static void remove_lock_file(void)
28{
bb233d69
LT
29 if (lockfile_name)
30 unlink(lockfile_name);
9614b8dc
LT
31}
32
ca016f0e
LT
33static int path_matches(struct cache_entry *a, struct cache_entry *b)
34{
35 int len = ce_namelen(a);
36 return ce_namelen(b) == len &&
37 !memcmp(a->name, b->name, len);
38}
39
43f91266
LT
40static int same(struct cache_entry *a, struct cache_entry *b)
41{
42 return a->ce_mode == b->ce_mode &&
43 !memcmp(a->sha1, b->sha1, 20);
44}
45
46
d99082e0 47/*
43f91266
LT
48 * This removes all trivial merges that don't change the tree
49 * and collapses them to state 0.
d99082e0 50 *
43f91266
LT
51 * _Any_ other merge is left to user policy. That includes "both
52 * created the same file", and "both removed the same file" - which are
53 * trivial, but the user might still want to _note_ it.
d99082e0 54 */
43f91266
LT
55static struct cache_entry *merge_entries(struct cache_entry *a,
56 struct cache_entry *b,
57 struct cache_entry *c)
d99082e0
LT
58{
59 int len = ce_namelen(a);
43f91266
LT
60
61 /*
62 * Are they all the same filename? We won't do
63 * any name merging
64 */
65 if (ce_namelen(b) != len ||
66 ce_namelen(c) != len ||
67 memcmp(a->name, b->name, len) ||
68 memcmp(a->name, c->name, len))
69 return NULL;
70
71 /*
72 * Ok, all three entries describe the same
73 * filename, but maybe the contents or file
74 * mode have changed?
75 *
76 * The trivial cases end up being the ones where two
77 * out of three files are the same:
78 * - both destinations the same, trivially take either
79 * - one of the destination versions hasn't changed,
80 * take the other.
81 *
82 * The "all entries exactly the same" case falls out as
83 * a special case of any of the "two same" cases.
84 *
85 * Here "a" is "original", and "b" and "c" are the two
86 * trees we are merging.
87 */
88 if (same(b,c))
89 return c;
90 if (same(a,b))
91 return c;
92 if (same(a,c))
93 return b;
94 return NULL;
d99082e0
LT
95}
96
02ede67a
LT
97/*
98 * When a CE gets turned into an unmerged entry, we
99 * want it to be up-to-date
100 */
101static void verify_uptodate(struct cache_entry *ce)
102{
103 struct stat st;
104
105 if (!lstat(ce->name, &st)) {
106 unsigned changed = ce_match_stat(ce, &st);
107 if (!changed)
108 return;
109 errno = 0;
110 }
111 if (errno == ENOENT)
112 return;
113 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
114}
115
116/*
117 * If the old tree contained a CE that isn't even in the
118 * result, that's always a problem, regardless of whether
119 * it's up-to-date or not (ie it can be a file that we
120 * have updated but not committed yet).
121 */
76f38347 122static void reject_merge(struct cache_entry *ce)
02ede67a 123{
76f38347 124 die("Entry '%s' would be overwritten by merge. Cannot merge.", ce->name);
02ede67a
LT
125}
126
76f38347 127#define CHECK_OLD(ce) if (old && same(old, ce)) { verify_uptodate(old); old = NULL; }
02ede67a 128
d99082e0
LT
129static void trivially_merge_cache(struct cache_entry **src, int nr)
130{
131 struct cache_entry **dst = src;
02ede67a 132 struct cache_entry *old = NULL;
d99082e0 133
220a0b52 134 while (nr--) {
43f91266 135 struct cache_entry *ce, *result;
d99082e0 136
220a0b52 137 ce = *src++;
ca016f0e
LT
138
139 /* We throw away original cache entries except for the stat information */
140 if (!ce_stage(ce)) {
76f38347
LT
141 if (old)
142 reject_merge(old);
ca016f0e 143 old = ce;
ca016f0e
LT
144 active_nr--;
145 continue;
146 }
76f38347
LT
147 if (old && !path_matches(old, ce))
148 reject_merge(old);
d925ffbd 149 if (nr > 1 && (result = merge_entries(ce, src[0], src[1])) != NULL) {
220a0b52 150 result->ce_flags |= htons(CE_UPDATE);
ca016f0e
LT
151 /*
152 * See if we can re-use the old CE directly?
153 * That way we get the uptodate stat info.
220a0b52
LT
154 *
155 * This also removes the UPDATE flag on
156 * a match.
ca016f0e 157 */
76f38347 158 if (old && same(old, result)) {
ca016f0e 159 *result = *old;
02ede67a
LT
160 old = NULL;
161 }
76f38347 162 CHECK_OLD(ce);
a76d74fb 163 CHECK_OLD(src[0]);
76f38347 164 CHECK_OLD(src[1]);
43f91266 165 ce = result;
d99082e0
LT
166 ce->ce_flags &= ~htons(CE_STAGEMASK);
167 src += 2;
168 nr -= 2;
169 active_nr -= 2;
170 }
02ede67a
LT
171
172 /*
173 * If we had an old entry that we now effectively
174 * overwrite, make sure it wasn't dirty.
175 */
76f38347 176 CHECK_OLD(ce);
a3a65234 177 *dst++ = ce;
a3a65234 178 }
76f38347
LT
179 if (old)
180 reject_merge(old);
a3a65234
LT
181}
182
e6ee623b
LT
183/*
184 * When we find a "stage2" entry in the two-way merge, that's
185 * the one that will remain. If we have an exact old match,
186 * we don't care whether the file is up-to-date or not, we just
187 * re-use the thing directly.
188 *
189 * If we didn't have an exact match, then we want to make sure
190 * that we've seen a stage1 that matched the old, and that the
191 * old file was up-to-date. Because it will be gone after this
192 * merge..
193 */
194static void twoway_check(struct cache_entry *old, int seen_stage1, struct cache_entry *ce)
195{
196 if (path_matches(old, ce)) {
197 /*
198 * This also removes the UPDATE flag on
199 * a match
200 */
201 if (same(old, ce)) {
202 *ce = *old;
203 return;
204 }
205 if (!seen_stage1)
206 reject_merge(old);
207 }
208 verify_uptodate(old);
209}
210
220a0b52
LT
211/*
212 * Two-way merge.
213 *
214 * The rule is:
215 * - every current entry has to match the old tree
216 * - if the current entry matches the new tree, we leave it
217 * as-is. Otherwise we require that it be up-to-date.
218 */
219static void twoway_merge(struct cache_entry **src, int nr)
a3a65234 220{
e6ee623b
LT
221 int seen_stage1 = 0;
222 struct cache_entry *old = NULL;
a3a65234 223 struct cache_entry **dst = src;
a3a65234 224
220a0b52
LT
225 while (nr--) {
226 struct cache_entry *ce = *src++;
227 int stage = ce_stage(ce);
228
229 switch (stage) {
230 case 0:
231 if (old)
232 reject_merge(old);
233 old = ce;
e6ee623b 234 seen_stage1 = 0;
220a0b52
LT
235 active_nr--;
236 continue;
237
238 case 1:
239 active_nr--;
240 if (!old)
241 continue;
242 if (!path_matches(old, ce) || !same(old, ce))
243 reject_merge(old);
e6ee623b 244 seen_stage1 = 1;
220a0b52
LT
245 continue;
246
247 case 2:
248 ce->ce_flags |= htons(CE_UPDATE);
249 if (old) {
e6ee623b 250 twoway_check(old, seen_stage1, ce);
220a0b52
LT
251 old = NULL;
252 }
253 ce->ce_flags &= ~htons(CE_STAGEMASK);
254 *dst++ = ce;
255 continue;
256 }
257 die("impossible two-way stage");
258 }
e6ee623b
LT
259
260 /*
261 * Unmatched with a new entry? Make sure it was
262 * at least uptodate in the working directory _and_
263 * the original tree..
264 */
265 if (old) {
266 if (!seen_stage1)
267 reject_merge(old);
268 verify_uptodate(old);
269 }
220a0b52
LT
270}
271
272static void merge_stat_info(struct cache_entry **src, int nr)
273{
274 static struct cache_entry null_entry;
275 struct cache_entry **dst = src;
276 struct cache_entry *stat = &null_entry;
a3a65234 277
220a0b52
LT
278 while (nr--) {
279 struct cache_entry *ce = *src++;
a3a65234
LT
280
281 /* We throw away original cache entries except for the stat information */
282 if (!ce_stage(ce)) {
220a0b52 283 stat = ce;
a3a65234
LT
284 active_nr--;
285 continue;
286 }
220a0b52
LT
287 if (path_matches(ce, stat) && same(ce, stat))
288 *ce = *stat;
a3a65234
LT
289 ce->ce_flags &= ~htons(CE_STAGEMASK);
290 *dst++ = ce;
d99082e0
LT
291 }
292}
293
220a0b52
LT
294static void check_updates(struct cache_entry **src, int nr)
295{
296 static struct checkout state = {
297 .base_dir = "",
298 .force = 1,
299 .quiet = 1,
300 .refresh_cache = 1,
301 };
302 unsigned short mask = htons(CE_UPDATE);
303 while (nr--) {
304 struct cache_entry *ce = *src++;
305 if (ce->ce_flags & mask) {
306 ce->ce_flags &= ~mask;
307 if (update)
308 checkout_entry(ce, &state);
309 }
310 }
311}
312
313static char *read_tree_usage = "git-read-tree (<sha> | -m <sha1> [<sha2> [<sha3>]])";
c5bac17a 314
e83c5163
LT
315int main(int argc, char **argv)
316{
ca016f0e 317 int i, newfd, merge;
e83c5163 318 unsigned char sha1[20];
bb233d69
LT
319 static char lockfile[MAXPATHLEN+1];
320 const char *indexfile = get_index_file();
e83c5163 321
bb233d69
LT
322 snprintf(lockfile, sizeof(lockfile), "%s.lock", indexfile);
323
324 newfd = open(lockfile, O_RDWR | O_CREAT | O_EXCL, 0600);
83adac3c 325 if (newfd < 0)
2de381f9 326 die("unable to create new cachefile");
9614b8dc 327 atexit(remove_lock_file);
bb233d69 328 lockfile_name = lockfile;
83adac3c 329
ca016f0e 330 merge = 0;
83adac3c
LT
331 for (i = 1; i < argc; i++) {
332 const char *arg = argv[i];
333
220a0b52
LT
334 /* "-u" means "update", meaning that a merge will update the working directory */
335 if (!strcmp(arg, "-u")) {
336 update = 1;
337 continue;
338 }
339
d99082e0 340 /* "-m" stands for "merge", meaning we start in stage 1 */
83adac3c 341 if (!strcmp(arg, "-m")) {
ca016f0e
LT
342 int i;
343 if (stage)
c5bac17a 344 die("-m needs to come first");
ca016f0e
LT
345 read_cache();
346 for (i = 0; i < active_nr; i++) {
347 if (ce_stage(active_cache[i]))
c5bac17a 348 die("you need to resolve your current index first");
ca016f0e 349 }
d99082e0 350 stage = 1;
ca016f0e 351 merge = 1;
83adac3c
LT
352 continue;
353 }
3c249c95 354 if (get_sha1(arg, sha1) < 0)
c5bac17a 355 usage(read_tree_usage);
d99082e0 356 if (stage > 3)
c5bac17a 357 usage(read_tree_usage);
94537c78 358 if (unpack_tree(sha1) < 0)
2de381f9 359 die("failed to unpack tree object %s", arg);
d99082e0 360 stage++;
83adac3c 361 }
ca016f0e 362 if (merge) {
a3a65234
LT
363 switch (stage) {
364 case 4: /* Three-way merge */
365 trivially_merge_cache(active_cache, active_nr);
220a0b52
LT
366 check_updates(active_cache, active_nr);
367 break;
368 case 3: /* Update from one tree to another */
369 twoway_merge(active_cache, active_nr);
370 check_updates(active_cache, active_nr);
a3a65234
LT
371 break;
372 case 2: /* Just read a tree, merge with old cache contents */
373 merge_stat_info(active_cache, active_nr);
374 break;
375 default:
376 die("just how do you expect me to merge %d trees?", stage-1);
377 }
ca016f0e 378 }
bb233d69 379 if (write_cache(newfd, active_cache, active_nr) || rename(lockfile, indexfile))
2de381f9 380 die("unable to write new index file");
bb233d69 381 lockfile_name = NULL;
9614b8dc 382 return 0;
e83c5163 383}