]>
Commit | Line | Data |
---|---|---|
8bc9a0c7 LT |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | */ | |
e83c5163 | 6 | #include "cache.h" |
ee1bec3d | 7 | #include "strbuf.h" |
973d6a20 | 8 | #include "quote.h" |
2bd452d3 | 9 | #include "tree-walk.h" |
e83c5163 | 10 | |
121481ab LT |
11 | /* |
12 | * Default to not allowing changes to the list of files. The | |
13 | * tool doesn't actually care, but this makes it harder to add | |
14 | * files to the revision control by mistake by doing something | |
215a7ad1 | 15 | * like "git-update-index *" and suddenly having all the object |
121481ab LT |
16 | * files be revision controlled. |
17 | */ | |
caf4f582 JH |
18 | static int allow_add; |
19 | static int allow_remove; | |
20 | static int allow_replace; | |
21 | static int allow_unmerged; /* --refresh needing merge is not error */ | |
22 | static int not_new; /* --refresh not having working tree files is not error */ | |
23 | static int quiet; /* --refresh needing update is not error */ | |
24 | static int info_only; | |
9b63f501 | 25 | static int force_remove; |
caf4f582 | 26 | static int verbose; |
5f73076c JH |
27 | static int mark_valid_only = 0; |
28 | #define MARK_VALID 1 | |
29 | #define UNMARK_VALID 2 | |
30 | ||
c6e007b0 JB |
31 | |
32 | /* Three functions to allow overloaded pointer return; see linux/err.h */ | |
33 | static inline void *ERR_PTR(long error) | |
34 | { | |
35 | return (void *) error; | |
36 | } | |
37 | ||
38 | static inline long PTR_ERR(const void *ptr) | |
39 | { | |
40 | return (long) ptr; | |
41 | } | |
42 | ||
43 | static inline long IS_ERR(const void *ptr) | |
44 | { | |
45 | return (unsigned long)ptr > (unsigned long)-1000L; | |
46 | } | |
121481ab | 47 | |
caf4f582 JH |
48 | static void report(const char *fmt, ...) |
49 | { | |
50 | va_list vp; | |
51 | ||
52 | if (!verbose) | |
53 | return; | |
54 | ||
55 | va_start(vp, fmt); | |
56 | vprintf(fmt, vp); | |
57 | putchar('\n'); | |
58 | va_end(vp); | |
59 | } | |
60 | ||
5f73076c JH |
61 | static int mark_valid(const char *path) |
62 | { | |
63 | int namelen = strlen(path); | |
64 | int pos = cache_name_pos(path, namelen); | |
65 | if (0 <= pos) { | |
66 | switch (mark_valid_only) { | |
67 | case MARK_VALID: | |
68 | active_cache[pos]->ce_flags |= htons(CE_VALID); | |
69 | break; | |
70 | case UNMARK_VALID: | |
71 | active_cache[pos]->ce_flags &= ~htons(CE_VALID); | |
72 | break; | |
73 | } | |
74 | active_cache_changed = 1; | |
75 | return 0; | |
76 | } | |
77 | return -1; | |
78 | } | |
79 | ||
6b5ee137 | 80 | static int add_file_to_cache(const char *path) |
e83c5163 | 81 | { |
4c5abf42 | 82 | int size, namelen, option, status; |
e83c5163 LT |
83 | struct cache_entry *ce; |
84 | struct stat st; | |
e83c5163 | 85 | |
4c5abf42 JH |
86 | status = lstat(path, &st); |
87 | if (status < 0 || S_ISDIR(st.st_mode)) { | |
88 | /* When we used to have "path" and now we want to add | |
89 | * "path/file", we need a way to remove "path" before | |
90 | * being able to add "path/file". However, | |
215a7ad1 | 91 | * "git-update-index --remove path" would not work. |
4c5abf42 JH |
92 | * --force-remove can be used but this is more user |
93 | * friendly, especially since we can do the opposite | |
94 | * case just fine without --force-remove. | |
95 | */ | |
96 | if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) { | |
34143b26 PB |
97 | if (allow_remove) { |
98 | if (remove_file_from_cache(path)) | |
99 | return error("%s: cannot remove from the index", | |
100 | path); | |
101 | else | |
102 | return 0; | |
103 | } else if (status < 0) { | |
104 | return error("%s: does not exist and --remove not passed", | |
105 | path); | |
106 | } | |
121481ab | 107 | } |
89bc8c78 | 108 | if (0 == status) |
34143b26 PB |
109 | return error("%s: is a directory - add files inside instead", |
110 | path); | |
89bc8c78 AW |
111 | else |
112 | return error("lstat(\"%s\"): %s", path, | |
113 | strerror(errno)); | |
e83c5163 | 114 | } |
3e09cdfd | 115 | |
e83c5163 LT |
116 | namelen = strlen(path); |
117 | size = cache_entry_size(namelen); | |
90321c10 | 118 | ce = xcalloc(1, size); |
e83c5163 | 119 | memcpy(ce->name, path, namelen); |
5f73076c | 120 | ce->ce_flags = htons(namelen); |
711cf3a0 | 121 | fill_stat_cache_info(ce, &st); |
3e09cdfd | 122 | |
e4479470 | 123 | ce->ce_mode = create_ce_mode(st.st_mode); |
3e09cdfd JH |
124 | if (!trust_executable_bit) { |
125 | /* If there is an existing entry, pick the mode bits | |
126 | * from it. | |
127 | */ | |
128 | int pos = cache_name_pos(path, namelen); | |
129 | if (0 <= pos) | |
130 | ce->ce_mode = active_cache[pos]->ce_mode; | |
131 | } | |
ec1fcc16 JH |
132 | |
133 | if (index_path(ce->sha1, path, &st, !info_only)) | |
134 | return -1; | |
192268c1 JH |
135 | option = allow_add ? ADD_CACHE_OK_TO_ADD : 0; |
136 | option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0; | |
34143b26 PB |
137 | if (add_cache_entry(ce, option)) |
138 | return error("%s: cannot add to the index - missing --add option?", | |
139 | path); | |
140 | return 0; | |
121481ab LT |
141 | } |
142 | ||
711cf3a0 LT |
143 | /* |
144 | * "refresh" does not calculate a new sha1 file or bring the | |
145 | * cache up-to-date for mode/content changes. But what it | |
146 | * _does_ do is to "re-match" the stat information of a file | |
147 | * with the cache, so that you can refresh the cache for a | |
148 | * file that hasn't been changed but where the stat entry is | |
149 | * out of date. | |
150 | * | |
667bb59b | 151 | * For example, you'd want to do this after doing a "git-read-tree", |
711cf3a0 LT |
152 | * to link up the stat cache details with the proper files. |
153 | */ | |
5f73076c | 154 | static struct cache_entry *refresh_entry(struct cache_entry *ce, int really) |
711cf3a0 LT |
155 | { |
156 | struct stat st; | |
157 | struct cache_entry *updated; | |
158 | int changed, size; | |
159 | ||
8ae0a8c5 | 160 | if (lstat(ce->name, &st) < 0) |
c6e007b0 | 161 | return ERR_PTR(-errno); |
711cf3a0 | 162 | |
5f73076c | 163 | changed = ce_match_stat(ce, &st, really); |
b92b2ce9 JH |
164 | if (!changed) { |
165 | if (really && assume_unchanged && | |
166 | !(ce->ce_flags & htons(CE_VALID))) | |
167 | ; /* mark this one VALID again */ | |
168 | else | |
169 | return NULL; | |
170 | } | |
711cf3a0 | 171 | |
5f73076c | 172 | if (ce_modified(ce, &st, really)) |
c6e007b0 | 173 | return ERR_PTR(-EINVAL); |
711cf3a0 LT |
174 | |
175 | size = ce_size(ce); | |
812666c8 | 176 | updated = xmalloc(size); |
711cf3a0 LT |
177 | memcpy(updated, ce, size); |
178 | fill_stat_cache_info(updated, &st); | |
5f73076c | 179 | |
8b9b0f3a JH |
180 | /* In this case, if really is not set, we should leave |
181 | * CE_VALID bit alone. Otherwise, paths marked with | |
182 | * --no-assume-unchanged (i.e. things to be edited) will | |
183 | * reacquire CE_VALID bit automatically, which is not | |
184 | * really what we want. | |
185 | */ | |
186 | if (!really && assume_unchanged && !(ce->ce_flags & htons(CE_VALID))) | |
187 | updated->ce_flags &= ~htons(CE_VALID); | |
188 | ||
711cf3a0 | 189 | return updated; |
121481ab LT |
190 | } |
191 | ||
5f73076c | 192 | static int refresh_cache(int really) |
121481ab LT |
193 | { |
194 | int i; | |
90535218 | 195 | int has_errors = 0; |
121481ab | 196 | |
711cf3a0 | 197 | for (i = 0; i < active_nr; i++) { |
1bc992ac JH |
198 | struct cache_entry *ce, *new; |
199 | ce = active_cache[i]; | |
200 | if (ce_stage(ce)) { | |
1bc992ac JH |
201 | while ((i < active_nr) && |
202 | ! strcmp(active_cache[i]->name, ce->name)) | |
203 | i++; | |
204 | i--; | |
5d1a5c02 LT |
205 | if (allow_unmerged) |
206 | continue; | |
207 | printf("%s: needs merge\n", ce->name); | |
208 | has_errors = 1; | |
1bc992ac JH |
209 | continue; |
210 | } | |
711cf3a0 | 211 | |
5f73076c | 212 | new = refresh_entry(ce, really); |
5d1a5c02 LT |
213 | if (!new) |
214 | continue; | |
c6e007b0 | 215 | if (IS_ERR(new)) { |
0ed3715f LT |
216 | if (not_new && PTR_ERR(new) == -ENOENT) |
217 | continue; | |
5f73076c JH |
218 | if (really && PTR_ERR(new) == -EINVAL) { |
219 | /* If we are doing --really-refresh that | |
220 | * means the index is not valid anymore. | |
221 | */ | |
222 | ce->ce_flags &= ~htons(CE_VALID); | |
223 | active_cache_changed = 1; | |
224 | } | |
0ed3715f LT |
225 | if (quiet) |
226 | continue; | |
227 | printf("%s: needs update\n", ce->name); | |
228 | has_errors = 1; | |
711cf3a0 LT |
229 | continue; |
230 | } | |
ee267527 | 231 | active_cache_changed = 1; |
62d046a0 PB |
232 | /* You can NOT just free active_cache[i] here, since it |
233 | * might not be necessarily malloc()ed but can also come | |
234 | * from mmap(). */ | |
711cf3a0 LT |
235 | active_cache[i] = new; |
236 | } | |
90535218 | 237 | return has_errors; |
e83c5163 LT |
238 | } |
239 | ||
e83c5163 LT |
240 | /* |
241 | * We fundamentally don't like some paths: we don't want | |
320d3a1b LT |
242 | * dot or dot-dot anywhere, and for obvious reasons don't |
243 | * want to recurse into ".git" either. | |
e83c5163 LT |
244 | * |
245 | * Also, we don't want double slashes or slashes at the | |
aebb2679 | 246 | * end that can make pathnames ambiguous. |
e83c5163 | 247 | */ |
320d3a1b LT |
248 | static int verify_dotfile(const char *rest) |
249 | { | |
250 | /* | |
251 | * The first character was '.', but that | |
252 | * has already been discarded, we now test | |
253 | * the rest. | |
254 | */ | |
255 | switch (*rest) { | |
256 | /* "." is not allowed */ | |
257 | case '\0': case '/': | |
258 | return 0; | |
259 | ||
260 | /* | |
261 | * ".git" followed by NUL or slash is bad. This | |
262 | * shares the path end test with the ".." case. | |
263 | */ | |
264 | case 'g': | |
265 | if (rest[1] != 'i') | |
266 | break; | |
267 | if (rest[2] != 't') | |
268 | break; | |
269 | rest += 2; | |
270 | /* fallthrough */ | |
271 | case '.': | |
272 | if (rest[1] == '\0' || rest[1] == '/') | |
273 | return 0; | |
274 | } | |
275 | return 1; | |
276 | } | |
277 | ||
6b5ee137 | 278 | static int verify_path(const char *path) |
e83c5163 LT |
279 | { |
280 | char c; | |
281 | ||
282 | goto inside; | |
283 | for (;;) { | |
284 | if (!c) | |
285 | return 1; | |
286 | if (c == '/') { | |
287 | inside: | |
288 | c = *path++; | |
320d3a1b LT |
289 | switch (c) { |
290 | default: | |
e83c5163 | 291 | continue; |
320d3a1b LT |
292 | case '/': case '\0': |
293 | break; | |
294 | case '.': | |
295 | if (verify_dotfile(path)) | |
296 | continue; | |
297 | } | |
e83c5163 LT |
298 | return 0; |
299 | } | |
300 | c = *path++; | |
301 | } | |
302 | } | |
303 | ||
d23748a6 JH |
304 | static int add_cacheinfo(unsigned int mode, const unsigned char *sha1, |
305 | const char *path, int stage) | |
9945d980 | 306 | { |
192268c1 | 307 | int size, len, option; |
9945d980 LT |
308 | struct cache_entry *ce; |
309 | ||
d23748a6 | 310 | if (!verify_path(path)) |
9945d980 | 311 | return -1; |
9945d980 | 312 | |
d23748a6 | 313 | len = strlen(path); |
9945d980 | 314 | size = cache_entry_size(len); |
90321c10 | 315 | ce = xcalloc(1, size); |
9945d980 LT |
316 | |
317 | memcpy(ce->sha1, sha1, 20); | |
d23748a6 JH |
318 | memcpy(ce->name, path, len); |
319 | ce->ce_flags = create_ce_flags(len, stage); | |
e4479470 | 320 | ce->ce_mode = create_ce_mode(mode); |
5f73076c JH |
321 | if (assume_unchanged) |
322 | ce->ce_flags |= htons(CE_VALID); | |
192268c1 JH |
323 | option = allow_add ? ADD_CACHE_OK_TO_ADD : 0; |
324 | option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0; | |
caf4f582 JH |
325 | if (add_cache_entry(ce, option)) |
326 | return error("%s: cannot add to the index - missing --add option?", | |
d23748a6 JH |
327 | path); |
328 | report("add '%s'", path); | |
caf4f582 | 329 | return 0; |
9945d980 LT |
330 | } |
331 | ||
227bdb18 | 332 | static void chmod_path(int flip, const char *path) |
3e09cdfd JH |
333 | { |
334 | int pos; | |
335 | struct cache_entry *ce; | |
336 | unsigned int mode; | |
f2a19340 | 337 | |
3e09cdfd JH |
338 | pos = cache_name_pos(path, strlen(path)); |
339 | if (pos < 0) | |
227bdb18 | 340 | goto fail; |
3e09cdfd JH |
341 | ce = active_cache[pos]; |
342 | mode = ntohl(ce->ce_mode); | |
343 | if (!S_ISREG(mode)) | |
227bdb18 | 344 | goto fail; |
3e09cdfd JH |
345 | switch (flip) { |
346 | case '+': | |
347 | ce->ce_mode |= htonl(0111); break; | |
348 | case '-': | |
349 | ce->ce_mode &= htonl(~0111); break; | |
350 | default: | |
227bdb18 | 351 | goto fail; |
3e09cdfd JH |
352 | } |
353 | active_cache_changed = 1; | |
227bdb18 AR |
354 | report("chmod %cx '%s'", flip, path); |
355 | return; | |
356 | fail: | |
357 | die("git-update-index: cannot chmod %cx '%s'", flip, path); | |
3e09cdfd JH |
358 | } |
359 | ||
360 | static struct cache_file cache_file; | |
ee1bec3d JH |
361 | |
362 | static void update_one(const char *path, const char *prefix, int prefix_length) | |
363 | { | |
364 | const char *p = prefix_path(prefix, prefix_length, path); | |
365 | if (!verify_path(p)) { | |
366 | fprintf(stderr, "Ignoring path %s\n", path); | |
fb69a760 | 367 | goto free_return; |
ee1bec3d | 368 | } |
5f73076c JH |
369 | if (mark_valid_only) { |
370 | if (mark_valid(p)) | |
371 | die("Unable to mark file %s", path); | |
fb69a760 | 372 | goto free_return; |
5f73076c JH |
373 | } |
374 | ||
ee1bec3d JH |
375 | if (force_remove) { |
376 | if (remove_file_from_cache(p)) | |
377 | die("git-update-index: unable to remove %s", path); | |
caf4f582 | 378 | report("remove '%s'", path); |
fb69a760 | 379 | goto free_return; |
ee1bec3d JH |
380 | } |
381 | if (add_file_to_cache(p)) | |
382 | die("Unable to process file %s", path); | |
caf4f582 | 383 | report("add '%s'", path); |
fb69a760 | 384 | free_return: |
0cc9e70c | 385 | if (p < path || p > path + strlen(path)) |
fb69a760 | 386 | free((char*)p); |
ee1bec3d JH |
387 | } |
388 | ||
d4dbf36d JH |
389 | static void read_index_info(int line_termination) |
390 | { | |
391 | struct strbuf buf; | |
392 | strbuf_init(&buf); | |
393 | while (1) { | |
9c20a470 | 394 | char *ptr, *tab; |
973d6a20 | 395 | char *path_name; |
d4dbf36d JH |
396 | unsigned char sha1[20]; |
397 | unsigned int mode; | |
d23748a6 JH |
398 | int stage; |
399 | ||
400 | /* This reads lines formatted in one of three formats: | |
401 | * | |
402 | * (1) mode SP sha1 TAB path | |
403 | * The first format is what "git-apply --index-info" | |
404 | * reports, and used to reconstruct a partial tree | |
405 | * that is used for phony merge base tree when falling | |
406 | * back on 3-way merge. | |
407 | * | |
408 | * (2) mode SP type SP sha1 TAB path | |
409 | * The second format is to stuff git-ls-tree output | |
410 | * into the index file. | |
411 | * | |
412 | * (3) mode SP sha1 SP stage TAB path | |
413 | * This format is to put higher order stages into the | |
414 | * index file and matches git-ls-files --stage output. | |
415 | */ | |
d4dbf36d JH |
416 | read_line(&buf, stdin, line_termination); |
417 | if (buf.eof) | |
418 | break; | |
419 | ||
420 | mode = strtoul(buf.buf, &ptr, 8); | |
9c20a470 | 421 | if (ptr == buf.buf || *ptr != ' ') |
d4dbf36d JH |
422 | goto bad_line; |
423 | ||
9c20a470 JH |
424 | tab = strchr(ptr, '\t'); |
425 | if (!tab || tab - ptr < 41) | |
426 | goto bad_line; | |
d23748a6 | 427 | |
2d497115 | 428 | if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') { |
d23748a6 JH |
429 | stage = tab[-1] - '0'; |
430 | ptr = tab + 1; /* point at the head of path */ | |
431 | tab = tab - 2; /* point at tail of sha1 */ | |
432 | } | |
433 | else { | |
434 | stage = 0; | |
435 | ptr = tab + 1; /* point at the head of path */ | |
436 | } | |
437 | ||
9c20a470 JH |
438 | if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ') |
439 | goto bad_line; | |
973d6a20 JH |
440 | |
441 | if (line_termination && ptr[0] == '"') | |
442 | path_name = unquote_c_style(ptr, NULL); | |
443 | else | |
444 | path_name = ptr; | |
445 | ||
446 | if (!verify_path(path_name)) { | |
447 | fprintf(stderr, "Ignoring path %s\n", path_name); | |
448 | if (path_name != ptr) | |
449 | free(path_name); | |
d4dbf36d JH |
450 | continue; |
451 | } | |
452 | ||
453 | if (!mode) { | |
454 | /* mode == 0 means there is no such path -- remove */ | |
973d6a20 | 455 | if (remove_file_from_cache(path_name)) |
d4dbf36d JH |
456 | die("git-update-index: unable to remove %s", |
457 | ptr); | |
458 | } | |
459 | else { | |
460 | /* mode ' ' sha1 '\t' name | |
461 | * ptr[-1] points at tab, | |
462 | * ptr[-41] is at the beginning of sha1 | |
463 | */ | |
464 | ptr[-42] = ptr[-1] = 0; | |
d23748a6 | 465 | if (add_cacheinfo(mode, sha1, path_name, stage)) |
d4dbf36d | 466 | die("git-update-index: unable to update %s", |
973d6a20 | 467 | path_name); |
d4dbf36d | 468 | } |
973d6a20 JH |
469 | if (path_name != ptr) |
470 | free(path_name); | |
d4dbf36d JH |
471 | continue; |
472 | ||
473 | bad_line: | |
474 | die("malformed index info %s", buf.buf); | |
475 | } | |
476 | } | |
477 | ||
f6ab5bb2 | 478 | static const char update_index_usage[] = |
83e77a25 | 479 | "git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--really-refresh] [--cacheinfo] [--chmod=(+|-)x] [--assume-unchanged] [--info-only] [--force-remove] [--stdin] [--index-info] [--unresolve] [--again] [--ignore-missing] [-z] [--verbose] [--] <file>..."; |
f6ab5bb2 | 480 | |
2bd452d3 JH |
481 | static unsigned char head_sha1[20]; |
482 | static unsigned char merge_head_sha1[20]; | |
483 | ||
484 | static struct cache_entry *read_one_ent(const char *which, | |
485 | unsigned char *ent, const char *path, | |
486 | int namelen, int stage) | |
487 | { | |
488 | unsigned mode; | |
489 | unsigned char sha1[20]; | |
490 | int size; | |
491 | struct cache_entry *ce; | |
492 | ||
493 | if (get_tree_entry(ent, path, sha1, &mode)) { | |
83e77a25 JH |
494 | if (which) |
495 | error("%s: not in %s branch.", path, which); | |
2bd452d3 JH |
496 | return NULL; |
497 | } | |
498 | if (mode == S_IFDIR) { | |
83e77a25 JH |
499 | if (which) |
500 | error("%s: not a blob in %s branch.", path, which); | |
2bd452d3 JH |
501 | return NULL; |
502 | } | |
503 | size = cache_entry_size(namelen); | |
504 | ce = xcalloc(1, size); | |
505 | ||
506 | memcpy(ce->sha1, sha1, 20); | |
507 | memcpy(ce->name, path, namelen); | |
508 | ce->ce_flags = create_ce_flags(namelen, stage); | |
509 | ce->ce_mode = create_ce_mode(mode); | |
510 | return ce; | |
511 | } | |
512 | ||
513 | static int unresolve_one(const char *path) | |
514 | { | |
515 | int namelen = strlen(path); | |
516 | int pos; | |
517 | int ret = 0; | |
518 | struct cache_entry *ce_2 = NULL, *ce_3 = NULL; | |
519 | ||
520 | /* See if there is such entry in the index. */ | |
521 | pos = cache_name_pos(path, namelen); | |
522 | if (pos < 0) { | |
523 | /* If there isn't, either it is unmerged, or | |
524 | * resolved as "removed" by mistake. We do not | |
525 | * want to do anything in the former case. | |
526 | */ | |
527 | pos = -pos-1; | |
528 | if (pos < active_nr) { | |
529 | struct cache_entry *ce = active_cache[pos]; | |
530 | if (ce_namelen(ce) == namelen && | |
531 | !memcmp(ce->name, path, namelen)) { | |
532 | fprintf(stderr, | |
533 | "%s: skipping still unmerged path.\n", | |
534 | path); | |
535 | goto free_return; | |
536 | } | |
537 | } | |
538 | } | |
539 | ||
540 | /* Grab blobs from given path from HEAD and MERGE_HEAD, | |
541 | * stuff HEAD version in stage #2, | |
542 | * stuff MERGE_HEAD version in stage #3. | |
543 | */ | |
544 | ce_2 = read_one_ent("our", head_sha1, path, namelen, 2); | |
545 | ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3); | |
546 | ||
547 | if (!ce_2 || !ce_3) { | |
548 | ret = -1; | |
549 | goto free_return; | |
550 | } | |
551 | if (!memcmp(ce_2->sha1, ce_3->sha1, 20) && | |
552 | ce_2->ce_mode == ce_3->ce_mode) { | |
553 | fprintf(stderr, "%s: identical in both, skipping.\n", | |
554 | path); | |
555 | goto free_return; | |
556 | } | |
557 | ||
558 | remove_file_from_cache(path); | |
559 | if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) { | |
560 | error("%s: cannot add our version to the index.", path); | |
561 | ret = -1; | |
562 | goto free_return; | |
563 | } | |
564 | if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD)) | |
565 | return 0; | |
566 | error("%s: cannot add their version to the index.", path); | |
567 | ret = -1; | |
568 | free_return: | |
569 | free(ce_2); | |
570 | free(ce_3); | |
571 | return ret; | |
572 | } | |
573 | ||
574 | static void read_head_pointers(void) | |
575 | { | |
576 | if (read_ref(git_path("HEAD"), head_sha1)) | |
577 | die("No HEAD -- no initial commit yet?\n"); | |
578 | if (read_ref(git_path("MERGE_HEAD"), merge_head_sha1)) { | |
579 | fprintf(stderr, "Not in the middle of a merge.\n"); | |
580 | exit(0); | |
581 | } | |
582 | } | |
583 | ||
09895c1f JH |
584 | static int do_unresolve(int ac, const char **av, |
585 | const char *prefix, int prefix_length) | |
2bd452d3 JH |
586 | { |
587 | int i; | |
588 | int err = 0; | |
589 | ||
590 | /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we | |
591 | * are not doing a merge, so exit with success status. | |
592 | */ | |
593 | read_head_pointers(); | |
594 | ||
595 | for (i = 1; i < ac; i++) { | |
596 | const char *arg = av[i]; | |
09895c1f JH |
597 | const char *p = prefix_path(prefix, prefix_length, arg); |
598 | err |= unresolve_one(p); | |
0cc9e70c | 599 | if (p < arg || p > arg + strlen(arg)) |
09895c1f | 600 | free((char*)p); |
2bd452d3 JH |
601 | } |
602 | return err; | |
603 | } | |
604 | ||
83e77a25 JH |
605 | static int do_reupdate(int ac, const char **av, |
606 | const char *prefix, int prefix_length) | |
607 | { | |
608 | /* Read HEAD and run update-index on paths that are | |
609 | * merged and already different between index and HEAD. | |
610 | */ | |
611 | int pos; | |
612 | int has_head = 1; | |
0cc9e70c | 613 | const char **pathspec = get_pathspec(prefix, av + 1); |
83e77a25 JH |
614 | |
615 | if (read_ref(git_path("HEAD"), head_sha1)) | |
616 | /* If there is no HEAD, that means it is an initial | |
617 | * commit. Update everything in the index. | |
618 | */ | |
619 | has_head = 0; | |
620 | redo: | |
621 | for (pos = 0; pos < active_nr; pos++) { | |
622 | struct cache_entry *ce = active_cache[pos]; | |
623 | struct cache_entry *old = NULL; | |
624 | int save_nr; | |
22293b9c JH |
625 | |
626 | if (ce_stage(ce) || !ce_path_match(ce, pathspec)) | |
83e77a25 JH |
627 | continue; |
628 | if (has_head) | |
629 | old = read_one_ent(NULL, head_sha1, | |
630 | ce->name, ce_namelen(ce), 0); | |
631 | if (old && ce->ce_mode == old->ce_mode && | |
632 | !memcmp(ce->sha1, old->sha1, 20)) { | |
633 | free(old); | |
634 | continue; /* unchanged */ | |
635 | } | |
636 | /* Be careful. The working tree may not have the | |
637 | * path anymore, in which case, under 'allow_remove', | |
638 | * or worse yet 'allow_replace', active_nr may decrease. | |
639 | */ | |
640 | save_nr = active_nr; | |
641 | update_one(ce->name + prefix_length, prefix, prefix_length); | |
642 | if (save_nr != active_nr) | |
643 | goto redo; | |
644 | } | |
645 | return 0; | |
646 | } | |
647 | ||
6b5ee137 | 648 | int main(int argc, const char **argv) |
e83c5163 | 649 | { |
ee1bec3d | 650 | int i, newfd, entries, has_errors = 0, line_termination = '\n'; |
121481ab | 651 | int allow_options = 1; |
ee1bec3d | 652 | int read_from_stdin = 0; |
cfb0af1d | 653 | const char *prefix = setup_git_directory(); |
ee1bec3d | 654 | int prefix_length = prefix ? strlen(prefix) : 0; |
227bdb18 | 655 | char set_executable_bit = 0; |
bb233d69 | 656 | |
3e09cdfd JH |
657 | git_config(git_default_config); |
658 | ||
415e96c8 | 659 | newfd = hold_index_file_for_update(&cache_file, get_index_file()); |
9614b8dc | 660 | if (newfd < 0) |
2de381f9 | 661 | die("unable to create new cachefile"); |
9614b8dc | 662 | |
e83c5163 | 663 | entries = read_cache(); |
9614b8dc | 664 | if (entries < 0) |
2de381f9 | 665 | die("cache corrupted"); |
e83c5163 | 666 | |
e83c5163 | 667 | for (i = 1 ; i < argc; i++) { |
6b5ee137 | 668 | const char *path = argv[i]; |
121481ab LT |
669 | |
670 | if (allow_options && *path == '-') { | |
671 | if (!strcmp(path, "--")) { | |
672 | allow_options = 0; | |
673 | continue; | |
674 | } | |
0ed3715f LT |
675 | if (!strcmp(path, "-q")) { |
676 | quiet = 1; | |
677 | continue; | |
678 | } | |
121481ab LT |
679 | if (!strcmp(path, "--add")) { |
680 | allow_add = 1; | |
681 | continue; | |
682 | } | |
192268c1 JH |
683 | if (!strcmp(path, "--replace")) { |
684 | allow_replace = 1; | |
685 | continue; | |
686 | } | |
121481ab LT |
687 | if (!strcmp(path, "--remove")) { |
688 | allow_remove = 1; | |
689 | continue; | |
690 | } | |
5d1a5c02 LT |
691 | if (!strcmp(path, "--unmerged")) { |
692 | allow_unmerged = 1; | |
693 | continue; | |
694 | } | |
121481ab | 695 | if (!strcmp(path, "--refresh")) { |
5f73076c JH |
696 | has_errors |= refresh_cache(0); |
697 | continue; | |
698 | } | |
699 | if (!strcmp(path, "--really-refresh")) { | |
700 | has_errors |= refresh_cache(1); | |
121481ab LT |
701 | continue; |
702 | } | |
9945d980 | 703 | if (!strcmp(path, "--cacheinfo")) { |
d23748a6 JH |
704 | unsigned char sha1[20]; |
705 | unsigned int mode; | |
706 | ||
b3f94c4b | 707 | if (i+3 >= argc) |
215a7ad1 | 708 | die("git-update-index: --cacheinfo <mode> <sha1> <path>"); |
d23748a6 JH |
709 | |
710 | if ((sscanf(argv[i+1], "%o", &mode) != 1) || | |
711 | get_sha1_hex(argv[i+2], sha1) || | |
712 | add_cacheinfo(mode, sha1, argv[i+3], 0)) | |
713 | die("git-update-index: --cacheinfo" | |
714 | " cannot add %s", argv[i+3]); | |
9945d980 LT |
715 | i += 3; |
716 | continue; | |
717 | } | |
3e09cdfd JH |
718 | if (!strcmp(path, "--chmod=-x") || |
719 | !strcmp(path, "--chmod=+x")) { | |
720 | if (argc <= i+1) | |
721 | die("git-update-index: %s <path>", path); | |
227bdb18 | 722 | set_executable_bit = path[8]; |
3e09cdfd JH |
723 | continue; |
724 | } | |
5f73076c JH |
725 | if (!strcmp(path, "--assume-unchanged")) { |
726 | mark_valid_only = MARK_VALID; | |
727 | continue; | |
728 | } | |
729 | if (!strcmp(path, "--no-assume-unchanged")) { | |
730 | mark_valid_only = UNMARK_VALID; | |
731 | continue; | |
732 | } | |
df6e1516 BL |
733 | if (!strcmp(path, "--info-only")) { |
734 | info_only = 1; | |
735 | continue; | |
736 | } | |
0ff5bf7c | 737 | if (!strcmp(path, "--force-remove")) { |
9b63f501 | 738 | force_remove = 1; |
0ff5bf7c JH |
739 | continue; |
740 | } | |
ee1bec3d JH |
741 | if (!strcmp(path, "-z")) { |
742 | line_termination = 0; | |
743 | continue; | |
744 | } | |
745 | if (!strcmp(path, "--stdin")) { | |
746 | if (i != argc - 1) | |
747 | die("--stdin must be at the end"); | |
748 | read_from_stdin = 1; | |
749 | break; | |
750 | } | |
d4dbf36d | 751 | if (!strcmp(path, "--index-info")) { |
a41c175d SP |
752 | if (i != argc - 1) |
753 | die("--index-info must be at the end"); | |
d4dbf36d JH |
754 | allow_add = allow_replace = allow_remove = 1; |
755 | read_index_info(line_termination); | |
a41c175d | 756 | break; |
d4dbf36d | 757 | } |
2bd452d3 | 758 | if (!strcmp(path, "--unresolve")) { |
09895c1f JH |
759 | has_errors = do_unresolve(argc - i, argv + i, |
760 | prefix, prefix_length); | |
2bd452d3 JH |
761 | if (has_errors) |
762 | active_cache_changed = 0; | |
763 | goto finish; | |
764 | } | |
83e77a25 JH |
765 | if (!strcmp(path, "--again")) { |
766 | has_errors = do_reupdate(argc - i, argv + i, | |
767 | prefix, prefix_length); | |
768 | if (has_errors) | |
769 | active_cache_changed = 0; | |
770 | goto finish; | |
771 | } | |
c6e007b0 JB |
772 | if (!strcmp(path, "--ignore-missing")) { |
773 | not_new = 1; | |
774 | continue; | |
775 | } | |
caf4f582 JH |
776 | if (!strcmp(path, "--verbose")) { |
777 | verbose = 1; | |
778 | continue; | |
779 | } | |
f6ab5bb2 PB |
780 | if (!strcmp(path, "-h") || !strcmp(path, "--help")) |
781 | usage(update_index_usage); | |
2de381f9 | 782 | die("unknown option %s", path); |
121481ab | 783 | } |
ee1bec3d | 784 | update_one(path, prefix, prefix_length); |
227bdb18 AR |
785 | if (set_executable_bit) |
786 | chmod_path(set_executable_bit, path); | |
ee1bec3d JH |
787 | } |
788 | if (read_from_stdin) { | |
789 | struct strbuf buf; | |
790 | strbuf_init(&buf); | |
791 | while (1) { | |
a94d9948 | 792 | char *path_name; |
fb69a760 | 793 | const char *p; |
ee1bec3d JH |
794 | read_line(&buf, stdin, line_termination); |
795 | if (buf.eof) | |
796 | break; | |
a94d9948 JH |
797 | if (line_termination && buf.buf[0] == '"') |
798 | path_name = unquote_c_style(buf.buf, NULL); | |
799 | else | |
800 | path_name = buf.buf; | |
fb69a760 JH |
801 | p = prefix_path(prefix, prefix_length, path_name); |
802 | update_one(p, NULL, 0); | |
803 | if (set_executable_bit) | |
227bdb18 | 804 | chmod_path(set_executable_bit, p); |
0cc9e70c | 805 | if (p < path_name || p > path_name + strlen(path_name)) |
fb69a760 | 806 | free((char*) p); |
a94d9948 JH |
807 | if (path_name != buf.buf) |
808 | free(path_name); | |
9b63f501 | 809 | } |
e83c5163 | 810 | } |
2bd452d3 JH |
811 | |
812 | finish: | |
5cd5ace7 LT |
813 | if (active_cache_changed) { |
814 | if (write_cache(newfd, active_cache, active_nr) || | |
815 | commit_index_file(&cache_file)) | |
816 | die("Unable to write new cachefile"); | |
817 | } | |
9614b8dc | 818 | |
c4b83e61 | 819 | return has_errors ? 1 : 0; |
e83c5163 | 820 | } |