]>
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" |
a6e5642f | 9 | #include "cache-tree.h" |
2bd452d3 | 10 | #include "tree-walk.h" |
fefe81c9 | 11 | #include "builtin.h" |
e011054b | 12 | #include "refs.h" |
e83c5163 | 13 | |
121481ab LT |
14 | /* |
15 | * Default to not allowing changes to the list of files. The | |
16 | * tool doesn't actually care, but this makes it harder to add | |
17 | * files to the revision control by mistake by doing something | |
215a7ad1 | 18 | * like "git-update-index *" and suddenly having all the object |
121481ab LT |
19 | * files be revision controlled. |
20 | */ | |
caf4f582 JH |
21 | static int allow_add; |
22 | static int allow_remove; | |
23 | static int allow_replace; | |
caf4f582 | 24 | static int info_only; |
9b63f501 | 25 | static int force_remove; |
caf4f582 | 26 | static int verbose; |
96f1e58f | 27 | static int mark_valid_only; |
5f73076c JH |
28 | #define MARK_VALID 1 |
29 | #define UNMARK_VALID 2 | |
30 | ||
caf4f582 JH |
31 | static void report(const char *fmt, ...) |
32 | { | |
33 | va_list vp; | |
34 | ||
35 | if (!verbose) | |
36 | return; | |
37 | ||
38 | va_start(vp, fmt); | |
39 | vprintf(fmt, vp); | |
40 | putchar('\n'); | |
41 | va_end(vp); | |
42 | } | |
43 | ||
5f73076c JH |
44 | static int mark_valid(const char *path) |
45 | { | |
46 | int namelen = strlen(path); | |
47 | int pos = cache_name_pos(path, namelen); | |
48 | if (0 <= pos) { | |
49 | switch (mark_valid_only) { | |
50 | case MARK_VALID: | |
51 | active_cache[pos]->ce_flags |= htons(CE_VALID); | |
52 | break; | |
53 | case UNMARK_VALID: | |
54 | active_cache[pos]->ce_flags &= ~htons(CE_VALID); | |
55 | break; | |
56 | } | |
a6e5642f | 57 | cache_tree_invalidate_path(active_cache_tree, path); |
5f73076c JH |
58 | active_cache_changed = 1; |
59 | return 0; | |
60 | } | |
61 | return -1; | |
62 | } | |
63 | ||
e011054b | 64 | static int remove_one_path(const char *path) |
e83c5163 | 65 | { |
e011054b LT |
66 | if (!allow_remove) |
67 | return error("%s: does not exist and --remove not passed", path); | |
68 | if (remove_file_from_cache(path)) | |
69 | return error("%s: cannot remove from the index", path); | |
70 | return 0; | |
71 | } | |
a6e5642f | 72 | |
e011054b LT |
73 | /* |
74 | * Handle a path that couldn't be lstat'ed. It's either: | |
75 | * - missing file (ENOENT or ENOTDIR). That's ok if we're | |
76 | * supposed to be removing it and the removal actually | |
77 | * succeeds. | |
78 | * - permission error. That's never ok. | |
79 | */ | |
80 | static int process_lstat_error(const char *path, int err) | |
81 | { | |
82 | if (err == ENOENT || err == ENOTDIR) | |
83 | return remove_one_path(path); | |
84 | return error("lstat(\"%s\"): %s", path, strerror(errno)); | |
85 | } | |
a6e5642f | 86 | |
e011054b LT |
87 | static int add_one_path(struct cache_entry *old, const char *path, int len, struct stat *st) |
88 | { | |
89 | int option, size = cache_entry_size(len); | |
90 | struct cache_entry *ce = xcalloc(1, size); | |
3e09cdfd | 91 | |
e011054b LT |
92 | memcpy(ce->name, path, len); |
93 | ce->ce_flags = htons(len); | |
94 | fill_stat_cache_info(ce, st); | |
95 | ce->ce_mode = ce_mode_from_stat(old, st->st_mode); | |
ec1fcc16 | 96 | |
e011054b | 97 | if (index_path(ce->sha1, path, st, !info_only)) |
ec1fcc16 | 98 | return -1; |
192268c1 JH |
99 | option = allow_add ? ADD_CACHE_OK_TO_ADD : 0; |
100 | option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0; | |
34143b26 | 101 | if (add_cache_entry(ce, option)) |
e011054b | 102 | return error("%s: cannot add to the index - missing --add option?", path); |
34143b26 | 103 | return 0; |
121481ab LT |
104 | } |
105 | ||
e011054b LT |
106 | /* |
107 | * Handle a path that was a directory. Four cases: | |
108 | * | |
109 | * - it's already a gitlink in the index, and we keep it that | |
110 | * way, and update it if we can (if we cannot find the HEAD, | |
111 | * we're going to keep it unchanged in the index!) | |
112 | * | |
113 | * - it's a *file* in the index, in which case it should be | |
114 | * removed as a file if removal is allowed, since it doesn't | |
115 | * exist as such any more. If removal isn't allowed, it's | |
116 | * an error. | |
117 | * | |
118 | * (NOTE! This is old and arguably fairly strange behaviour. | |
119 | * We might want to make this an error unconditionally, and | |
120 | * use "--force-remove" if you actually want to force removal). | |
121 | * | |
122 | * - it used to exist as a subdirectory (ie multiple files with | |
123 | * this particular prefix) in the index, in which case it's wrong | |
124 | * to try to update it as a directory. | |
125 | * | |
126 | * - it doesn't exist at all in the index, but it is a valid | |
127 | * git directory, and it should be *added* as a gitlink. | |
128 | */ | |
129 | static int process_directory(const char *path, int len, struct stat *st) | |
130 | { | |
131 | unsigned char sha1[20]; | |
132 | int pos = cache_name_pos(path, len); | |
133 | ||
134 | /* Exact match: file or existing gitlink */ | |
135 | if (pos >= 0) { | |
136 | struct cache_entry *ce = active_cache[pos]; | |
302b9282 | 137 | if (S_ISGITLINK(ntohl(ce->ce_mode))) { |
e011054b LT |
138 | |
139 | /* Do nothing to the index if there is no HEAD! */ | |
140 | if (resolve_gitlink_ref(path, "HEAD", sha1) < 0) | |
141 | return 0; | |
142 | ||
143 | return add_one_path(ce, path, len, st); | |
144 | } | |
145 | /* Should this be an unconditional error? */ | |
146 | return remove_one_path(path); | |
147 | } | |
148 | ||
149 | /* Inexact match: is there perhaps a subdirectory match? */ | |
150 | pos = -pos-1; | |
151 | while (pos < active_nr) { | |
152 | struct cache_entry *ce = active_cache[pos++]; | |
153 | ||
154 | if (strncmp(ce->name, path, len)) | |
155 | break; | |
156 | if (ce->name[len] > '/') | |
157 | break; | |
158 | if (ce->name[len] < '/') | |
159 | continue; | |
160 | ||
161 | /* Subdirectory match - error out */ | |
162 | return error("%s: is a directory - add individual files instead", path); | |
163 | } | |
164 | ||
165 | /* No match - should we add it as a gitlink? */ | |
166 | if (!resolve_gitlink_ref(path, "HEAD", sha1)) | |
167 | return add_one_path(NULL, path, len, st); | |
168 | ||
169 | /* Error out. */ | |
170 | return error("%s: is a directory - add files inside instead", path); | |
171 | } | |
172 | ||
173 | /* | |
174 | * Process a regular file | |
175 | */ | |
176 | static int process_file(const char *path, int len, struct stat *st) | |
177 | { | |
178 | int pos = cache_name_pos(path, len); | |
179 | struct cache_entry *ce = pos < 0 ? NULL : active_cache[pos]; | |
180 | ||
302b9282 | 181 | if (ce && S_ISGITLINK(ntohl(ce->ce_mode))) |
e011054b LT |
182 | return error("%s is already a gitlink, not replacing", path); |
183 | ||
184 | return add_one_path(ce, path, len, st); | |
185 | } | |
186 | ||
187 | static int process_path(const char *path) | |
188 | { | |
189 | int len; | |
190 | struct stat st; | |
191 | ||
192 | /* We probably want to do this in remove_file_from_cache() and | |
193 | * add_cache_entry() instead... | |
194 | */ | |
195 | cache_tree_invalidate_path(active_cache_tree, path); | |
196 | ||
197 | /* | |
198 | * First things first: get the stat information, to decide | |
199 | * what to do about the pathname! | |
200 | */ | |
201 | if (lstat(path, &st) < 0) | |
202 | return process_lstat_error(path, errno); | |
203 | ||
204 | len = strlen(path); | |
205 | if (S_ISDIR(st.st_mode)) | |
206 | return process_directory(path, len, &st); | |
207 | ||
208 | return process_file(path, len, &st); | |
209 | } | |
210 | ||
d23748a6 JH |
211 | static int add_cacheinfo(unsigned int mode, const unsigned char *sha1, |
212 | const char *path, int stage) | |
9945d980 | 213 | { |
192268c1 | 214 | int size, len, option; |
9945d980 LT |
215 | struct cache_entry *ce; |
216 | ||
d23748a6 | 217 | if (!verify_path(path)) |
9945d980 | 218 | return -1; |
9945d980 | 219 | |
d23748a6 | 220 | len = strlen(path); |
9945d980 | 221 | size = cache_entry_size(len); |
90321c10 | 222 | ce = xcalloc(1, size); |
9945d980 | 223 | |
e702496e | 224 | hashcpy(ce->sha1, sha1); |
d23748a6 JH |
225 | memcpy(ce->name, path, len); |
226 | ce->ce_flags = create_ce_flags(len, stage); | |
e4479470 | 227 | ce->ce_mode = create_ce_mode(mode); |
5f73076c JH |
228 | if (assume_unchanged) |
229 | ce->ce_flags |= htons(CE_VALID); | |
192268c1 JH |
230 | option = allow_add ? ADD_CACHE_OK_TO_ADD : 0; |
231 | option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0; | |
caf4f582 JH |
232 | if (add_cache_entry(ce, option)) |
233 | return error("%s: cannot add to the index - missing --add option?", | |
d23748a6 JH |
234 | path); |
235 | report("add '%s'", path); | |
a6e5642f | 236 | cache_tree_invalidate_path(active_cache_tree, path); |
caf4f582 | 237 | return 0; |
9945d980 LT |
238 | } |
239 | ||
227bdb18 | 240 | static void chmod_path(int flip, const char *path) |
3e09cdfd JH |
241 | { |
242 | int pos; | |
243 | struct cache_entry *ce; | |
244 | unsigned int mode; | |
f2a19340 | 245 | |
3e09cdfd JH |
246 | pos = cache_name_pos(path, strlen(path)); |
247 | if (pos < 0) | |
227bdb18 | 248 | goto fail; |
3e09cdfd JH |
249 | ce = active_cache[pos]; |
250 | mode = ntohl(ce->ce_mode); | |
251 | if (!S_ISREG(mode)) | |
227bdb18 | 252 | goto fail; |
3e09cdfd JH |
253 | switch (flip) { |
254 | case '+': | |
255 | ce->ce_mode |= htonl(0111); break; | |
256 | case '-': | |
257 | ce->ce_mode &= htonl(~0111); break; | |
258 | default: | |
227bdb18 | 259 | goto fail; |
3e09cdfd | 260 | } |
a6e5642f | 261 | cache_tree_invalidate_path(active_cache_tree, path); |
3e09cdfd | 262 | active_cache_changed = 1; |
227bdb18 AR |
263 | report("chmod %cx '%s'", flip, path); |
264 | return; | |
265 | fail: | |
266 | die("git-update-index: cannot chmod %cx '%s'", flip, path); | |
3e09cdfd JH |
267 | } |
268 | ||
ee1bec3d JH |
269 | static void update_one(const char *path, const char *prefix, int prefix_length) |
270 | { | |
271 | const char *p = prefix_path(prefix, prefix_length, path); | |
272 | if (!verify_path(p)) { | |
273 | fprintf(stderr, "Ignoring path %s\n", path); | |
fb69a760 | 274 | goto free_return; |
ee1bec3d | 275 | } |
5f73076c JH |
276 | if (mark_valid_only) { |
277 | if (mark_valid(p)) | |
278 | die("Unable to mark file %s", path); | |
fb69a760 | 279 | goto free_return; |
5f73076c | 280 | } |
a6e5642f | 281 | cache_tree_invalidate_path(active_cache_tree, path); |
5f73076c | 282 | |
ee1bec3d JH |
283 | if (force_remove) { |
284 | if (remove_file_from_cache(p)) | |
285 | die("git-update-index: unable to remove %s", path); | |
caf4f582 | 286 | report("remove '%s'", path); |
fb69a760 | 287 | goto free_return; |
ee1bec3d | 288 | } |
e011054b LT |
289 | if (process_path(p)) |
290 | die("Unable to process path %s", path); | |
caf4f582 | 291 | report("add '%s'", path); |
fb69a760 | 292 | free_return: |
0cc9e70c | 293 | if (p < path || p > path + strlen(path)) |
fb69a760 | 294 | free((char*)p); |
ee1bec3d JH |
295 | } |
296 | ||
d4dbf36d JH |
297 | static void read_index_info(int line_termination) |
298 | { | |
299 | struct strbuf buf; | |
300 | strbuf_init(&buf); | |
301 | while (1) { | |
9c20a470 | 302 | char *ptr, *tab; |
973d6a20 | 303 | char *path_name; |
d4dbf36d JH |
304 | unsigned char sha1[20]; |
305 | unsigned int mode; | |
6aead43d | 306 | unsigned long ul; |
d23748a6 JH |
307 | int stage; |
308 | ||
309 | /* This reads lines formatted in one of three formats: | |
310 | * | |
311 | * (1) mode SP sha1 TAB path | |
312 | * The first format is what "git-apply --index-info" | |
313 | * reports, and used to reconstruct a partial tree | |
314 | * that is used for phony merge base tree when falling | |
315 | * back on 3-way merge. | |
316 | * | |
317 | * (2) mode SP type SP sha1 TAB path | |
318 | * The second format is to stuff git-ls-tree output | |
319 | * into the index file. | |
fefe81c9 | 320 | * |
d23748a6 JH |
321 | * (3) mode SP sha1 SP stage TAB path |
322 | * This format is to put higher order stages into the | |
323 | * index file and matches git-ls-files --stage output. | |
324 | */ | |
d4dbf36d JH |
325 | read_line(&buf, stdin, line_termination); |
326 | if (buf.eof) | |
327 | break; | |
328 | ||
6aead43d JM |
329 | errno = 0; |
330 | ul = strtoul(buf.buf, &ptr, 8); | |
331 | if (ptr == buf.buf || *ptr != ' ' | |
332 | || errno || (unsigned int) ul != ul) | |
d4dbf36d | 333 | goto bad_line; |
6aead43d | 334 | mode = ul; |
d4dbf36d | 335 | |
9c20a470 JH |
336 | tab = strchr(ptr, '\t'); |
337 | if (!tab || tab - ptr < 41) | |
338 | goto bad_line; | |
d23748a6 | 339 | |
2d497115 | 340 | if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') { |
d23748a6 JH |
341 | stage = tab[-1] - '0'; |
342 | ptr = tab + 1; /* point at the head of path */ | |
343 | tab = tab - 2; /* point at tail of sha1 */ | |
344 | } | |
345 | else { | |
346 | stage = 0; | |
347 | ptr = tab + 1; /* point at the head of path */ | |
348 | } | |
349 | ||
9c20a470 JH |
350 | if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ') |
351 | goto bad_line; | |
973d6a20 JH |
352 | |
353 | if (line_termination && ptr[0] == '"') | |
354 | path_name = unquote_c_style(ptr, NULL); | |
355 | else | |
356 | path_name = ptr; | |
357 | ||
358 | if (!verify_path(path_name)) { | |
359 | fprintf(stderr, "Ignoring path %s\n", path_name); | |
360 | if (path_name != ptr) | |
361 | free(path_name); | |
d4dbf36d JH |
362 | continue; |
363 | } | |
a6e5642f | 364 | cache_tree_invalidate_path(active_cache_tree, path_name); |
d4dbf36d JH |
365 | |
366 | if (!mode) { | |
367 | /* mode == 0 means there is no such path -- remove */ | |
973d6a20 | 368 | if (remove_file_from_cache(path_name)) |
d4dbf36d JH |
369 | die("git-update-index: unable to remove %s", |
370 | ptr); | |
371 | } | |
372 | else { | |
373 | /* mode ' ' sha1 '\t' name | |
374 | * ptr[-1] points at tab, | |
375 | * ptr[-41] is at the beginning of sha1 | |
376 | */ | |
377 | ptr[-42] = ptr[-1] = 0; | |
d23748a6 | 378 | if (add_cacheinfo(mode, sha1, path_name, stage)) |
d4dbf36d | 379 | die("git-update-index: unable to update %s", |
973d6a20 | 380 | path_name); |
d4dbf36d | 381 | } |
973d6a20 JH |
382 | if (path_name != ptr) |
383 | free(path_name); | |
d4dbf36d JH |
384 | continue; |
385 | ||
386 | bad_line: | |
387 | die("malformed index info %s", buf.buf); | |
388 | } | |
389 | } | |
390 | ||
f6ab5bb2 | 391 | static const char update_index_usage[] = |
7099c9c7 | 392 | "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 | -g] [--ignore-missing] [-z] [--verbose] [--] <file>..."; |
f6ab5bb2 | 393 | |
2bd452d3 JH |
394 | static unsigned char head_sha1[20]; |
395 | static unsigned char merge_head_sha1[20]; | |
396 | ||
397 | static struct cache_entry *read_one_ent(const char *which, | |
398 | unsigned char *ent, const char *path, | |
399 | int namelen, int stage) | |
400 | { | |
401 | unsigned mode; | |
402 | unsigned char sha1[20]; | |
403 | int size; | |
404 | struct cache_entry *ce; | |
405 | ||
406 | if (get_tree_entry(ent, path, sha1, &mode)) { | |
83e77a25 JH |
407 | if (which) |
408 | error("%s: not in %s branch.", path, which); | |
2bd452d3 JH |
409 | return NULL; |
410 | } | |
411 | if (mode == S_IFDIR) { | |
83e77a25 JH |
412 | if (which) |
413 | error("%s: not a blob in %s branch.", path, which); | |
2bd452d3 JH |
414 | return NULL; |
415 | } | |
416 | size = cache_entry_size(namelen); | |
417 | ce = xcalloc(1, size); | |
418 | ||
e702496e | 419 | hashcpy(ce->sha1, sha1); |
2bd452d3 JH |
420 | memcpy(ce->name, path, namelen); |
421 | ce->ce_flags = create_ce_flags(namelen, stage); | |
422 | ce->ce_mode = create_ce_mode(mode); | |
423 | return ce; | |
424 | } | |
425 | ||
426 | static int unresolve_one(const char *path) | |
427 | { | |
428 | int namelen = strlen(path); | |
429 | int pos; | |
430 | int ret = 0; | |
431 | struct cache_entry *ce_2 = NULL, *ce_3 = NULL; | |
432 | ||
433 | /* See if there is such entry in the index. */ | |
434 | pos = cache_name_pos(path, namelen); | |
435 | if (pos < 0) { | |
436 | /* If there isn't, either it is unmerged, or | |
437 | * resolved as "removed" by mistake. We do not | |
438 | * want to do anything in the former case. | |
439 | */ | |
440 | pos = -pos-1; | |
441 | if (pos < active_nr) { | |
442 | struct cache_entry *ce = active_cache[pos]; | |
443 | if (ce_namelen(ce) == namelen && | |
444 | !memcmp(ce->name, path, namelen)) { | |
445 | fprintf(stderr, | |
446 | "%s: skipping still unmerged path.\n", | |
447 | path); | |
448 | goto free_return; | |
449 | } | |
450 | } | |
451 | } | |
452 | ||
453 | /* Grab blobs from given path from HEAD and MERGE_HEAD, | |
454 | * stuff HEAD version in stage #2, | |
455 | * stuff MERGE_HEAD version in stage #3. | |
456 | */ | |
457 | ce_2 = read_one_ent("our", head_sha1, path, namelen, 2); | |
458 | ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3); | |
459 | ||
460 | if (!ce_2 || !ce_3) { | |
461 | ret = -1; | |
462 | goto free_return; | |
463 | } | |
a89fccd2 | 464 | if (!hashcmp(ce_2->sha1, ce_3->sha1) && |
2bd452d3 JH |
465 | ce_2->ce_mode == ce_3->ce_mode) { |
466 | fprintf(stderr, "%s: identical in both, skipping.\n", | |
467 | path); | |
468 | goto free_return; | |
469 | } | |
470 | ||
497c3213 | 471 | cache_tree_invalidate_path(active_cache_tree, path); |
2bd452d3 JH |
472 | remove_file_from_cache(path); |
473 | if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) { | |
474 | error("%s: cannot add our version to the index.", path); | |
475 | ret = -1; | |
476 | goto free_return; | |
477 | } | |
478 | if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD)) | |
479 | return 0; | |
480 | error("%s: cannot add their version to the index.", path); | |
481 | ret = -1; | |
482 | free_return: | |
483 | free(ce_2); | |
484 | free(ce_3); | |
485 | return ret; | |
486 | } | |
487 | ||
488 | static void read_head_pointers(void) | |
489 | { | |
913c983e | 490 | if (read_ref("HEAD", head_sha1)) |
2bd452d3 | 491 | die("No HEAD -- no initial commit yet?\n"); |
913c983e | 492 | if (read_ref("MERGE_HEAD", merge_head_sha1)) { |
2bd452d3 JH |
493 | fprintf(stderr, "Not in the middle of a merge.\n"); |
494 | exit(0); | |
495 | } | |
496 | } | |
497 | ||
09895c1f JH |
498 | static int do_unresolve(int ac, const char **av, |
499 | const char *prefix, int prefix_length) | |
2bd452d3 JH |
500 | { |
501 | int i; | |
502 | int err = 0; | |
503 | ||
504 | /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we | |
505 | * are not doing a merge, so exit with success status. | |
506 | */ | |
507 | read_head_pointers(); | |
508 | ||
509 | for (i = 1; i < ac; i++) { | |
510 | const char *arg = av[i]; | |
09895c1f JH |
511 | const char *p = prefix_path(prefix, prefix_length, arg); |
512 | err |= unresolve_one(p); | |
0cc9e70c | 513 | if (p < arg || p > arg + strlen(arg)) |
09895c1f | 514 | free((char*)p); |
2bd452d3 JH |
515 | } |
516 | return err; | |
517 | } | |
518 | ||
83e77a25 JH |
519 | static int do_reupdate(int ac, const char **av, |
520 | const char *prefix, int prefix_length) | |
521 | { | |
522 | /* Read HEAD and run update-index on paths that are | |
523 | * merged and already different between index and HEAD. | |
524 | */ | |
525 | int pos; | |
526 | int has_head = 1; | |
0cc9e70c | 527 | const char **pathspec = get_pathspec(prefix, av + 1); |
83e77a25 | 528 | |
913c983e | 529 | if (read_ref("HEAD", head_sha1)) |
83e77a25 JH |
530 | /* If there is no HEAD, that means it is an initial |
531 | * commit. Update everything in the index. | |
532 | */ | |
533 | has_head = 0; | |
534 | redo: | |
535 | for (pos = 0; pos < active_nr; pos++) { | |
536 | struct cache_entry *ce = active_cache[pos]; | |
537 | struct cache_entry *old = NULL; | |
538 | int save_nr; | |
22293b9c JH |
539 | |
540 | if (ce_stage(ce) || !ce_path_match(ce, pathspec)) | |
83e77a25 JH |
541 | continue; |
542 | if (has_head) | |
543 | old = read_one_ent(NULL, head_sha1, | |
544 | ce->name, ce_namelen(ce), 0); | |
545 | if (old && ce->ce_mode == old->ce_mode && | |
a89fccd2 | 546 | !hashcmp(ce->sha1, old->sha1)) { |
83e77a25 JH |
547 | free(old); |
548 | continue; /* unchanged */ | |
549 | } | |
550 | /* Be careful. The working tree may not have the | |
551 | * path anymore, in which case, under 'allow_remove', | |
552 | * or worse yet 'allow_replace', active_nr may decrease. | |
553 | */ | |
554 | save_nr = active_nr; | |
555 | update_one(ce->name + prefix_length, prefix, prefix_length); | |
556 | if (save_nr != active_nr) | |
557 | goto redo; | |
558 | } | |
559 | return 0; | |
560 | } | |
561 | ||
a633fca0 | 562 | int cmd_update_index(int argc, const char **argv, const char *prefix) |
e83c5163 | 563 | { |
ee1bec3d | 564 | int i, newfd, entries, has_errors = 0, line_termination = '\n'; |
121481ab | 565 | int allow_options = 1; |
ee1bec3d | 566 | int read_from_stdin = 0; |
ee1bec3d | 567 | int prefix_length = prefix ? strlen(prefix) : 0; |
227bdb18 | 568 | char set_executable_bit = 0; |
405e5b2f | 569 | unsigned int refresh_flags = 0; |
7b802b86 | 570 | int lock_error = 0; |
fefe81c9 | 571 | struct lock_file *lock_file; |
bb233d69 | 572 | |
3e09cdfd JH |
573 | git_config(git_default_config); |
574 | ||
fefe81c9 | 575 | /* We can't free this memory, it becomes part of a linked list parsed atexit() */ |
928e47e3 | 576 | lock_file = xcalloc(1, sizeof(struct lock_file)); |
fefe81c9 | 577 | |
30ca07a2 | 578 | newfd = hold_locked_index(lock_file, 0); |
7b802b86 JH |
579 | if (newfd < 0) |
580 | lock_error = errno; | |
9614b8dc | 581 | |
e83c5163 | 582 | entries = read_cache(); |
9614b8dc | 583 | if (entries < 0) |
2de381f9 | 584 | die("cache corrupted"); |
e83c5163 | 585 | |
e83c5163 | 586 | for (i = 1 ; i < argc; i++) { |
6b5ee137 | 587 | const char *path = argv[i]; |
9ebe6cf9 | 588 | const char *p; |
121481ab LT |
589 | |
590 | if (allow_options && *path == '-') { | |
591 | if (!strcmp(path, "--")) { | |
592 | allow_options = 0; | |
593 | continue; | |
594 | } | |
0ed3715f | 595 | if (!strcmp(path, "-q")) { |
405e5b2f | 596 | refresh_flags |= REFRESH_QUIET; |
0ed3715f LT |
597 | continue; |
598 | } | |
121481ab LT |
599 | if (!strcmp(path, "--add")) { |
600 | allow_add = 1; | |
601 | continue; | |
602 | } | |
192268c1 JH |
603 | if (!strcmp(path, "--replace")) { |
604 | allow_replace = 1; | |
605 | continue; | |
606 | } | |
121481ab LT |
607 | if (!strcmp(path, "--remove")) { |
608 | allow_remove = 1; | |
609 | continue; | |
610 | } | |
5d1a5c02 | 611 | if (!strcmp(path, "--unmerged")) { |
405e5b2f | 612 | refresh_flags |= REFRESH_UNMERGED; |
5d1a5c02 LT |
613 | continue; |
614 | } | |
121481ab | 615 | if (!strcmp(path, "--refresh")) { |
405e5b2f | 616 | has_errors |= refresh_cache(refresh_flags); |
5f73076c JH |
617 | continue; |
618 | } | |
619 | if (!strcmp(path, "--really-refresh")) { | |
405e5b2f | 620 | has_errors |= refresh_cache(REFRESH_REALLY | refresh_flags); |
121481ab LT |
621 | continue; |
622 | } | |
9945d980 | 623 | if (!strcmp(path, "--cacheinfo")) { |
d23748a6 JH |
624 | unsigned char sha1[20]; |
625 | unsigned int mode; | |
626 | ||
b3f94c4b | 627 | if (i+3 >= argc) |
215a7ad1 | 628 | die("git-update-index: --cacheinfo <mode> <sha1> <path>"); |
d23748a6 | 629 | |
6e6db39a | 630 | if (strtoul_ui(argv[i+1], 8, &mode) || |
d23748a6 JH |
631 | get_sha1_hex(argv[i+2], sha1) || |
632 | add_cacheinfo(mode, sha1, argv[i+3], 0)) | |
633 | die("git-update-index: --cacheinfo" | |
634 | " cannot add %s", argv[i+3]); | |
9945d980 LT |
635 | i += 3; |
636 | continue; | |
637 | } | |
3e09cdfd JH |
638 | if (!strcmp(path, "--chmod=-x") || |
639 | !strcmp(path, "--chmod=+x")) { | |
640 | if (argc <= i+1) | |
641 | die("git-update-index: %s <path>", path); | |
227bdb18 | 642 | set_executable_bit = path[8]; |
3e09cdfd JH |
643 | continue; |
644 | } | |
5f73076c JH |
645 | if (!strcmp(path, "--assume-unchanged")) { |
646 | mark_valid_only = MARK_VALID; | |
647 | continue; | |
648 | } | |
649 | if (!strcmp(path, "--no-assume-unchanged")) { | |
650 | mark_valid_only = UNMARK_VALID; | |
651 | continue; | |
652 | } | |
df6e1516 BL |
653 | if (!strcmp(path, "--info-only")) { |
654 | info_only = 1; | |
655 | continue; | |
656 | } | |
0ff5bf7c | 657 | if (!strcmp(path, "--force-remove")) { |
9b63f501 | 658 | force_remove = 1; |
0ff5bf7c JH |
659 | continue; |
660 | } | |
ee1bec3d JH |
661 | if (!strcmp(path, "-z")) { |
662 | line_termination = 0; | |
663 | continue; | |
664 | } | |
665 | if (!strcmp(path, "--stdin")) { | |
666 | if (i != argc - 1) | |
667 | die("--stdin must be at the end"); | |
668 | read_from_stdin = 1; | |
669 | break; | |
670 | } | |
d4dbf36d | 671 | if (!strcmp(path, "--index-info")) { |
a41c175d SP |
672 | if (i != argc - 1) |
673 | die("--index-info must be at the end"); | |
d4dbf36d JH |
674 | allow_add = allow_replace = allow_remove = 1; |
675 | read_index_info(line_termination); | |
a41c175d | 676 | break; |
d4dbf36d | 677 | } |
2bd452d3 | 678 | if (!strcmp(path, "--unresolve")) { |
09895c1f JH |
679 | has_errors = do_unresolve(argc - i, argv + i, |
680 | prefix, prefix_length); | |
2bd452d3 JH |
681 | if (has_errors) |
682 | active_cache_changed = 0; | |
683 | goto finish; | |
684 | } | |
7099c9c7 | 685 | if (!strcmp(path, "--again") || !strcmp(path, "-g")) { |
83e77a25 JH |
686 | has_errors = do_reupdate(argc - i, argv + i, |
687 | prefix, prefix_length); | |
688 | if (has_errors) | |
689 | active_cache_changed = 0; | |
690 | goto finish; | |
691 | } | |
c6e007b0 | 692 | if (!strcmp(path, "--ignore-missing")) { |
405e5b2f | 693 | refresh_flags |= REFRESH_IGNORE_MISSING; |
c6e007b0 JB |
694 | continue; |
695 | } | |
caf4f582 JH |
696 | if (!strcmp(path, "--verbose")) { |
697 | verbose = 1; | |
698 | continue; | |
699 | } | |
f6ab5bb2 PB |
700 | if (!strcmp(path, "-h") || !strcmp(path, "--help")) |
701 | usage(update_index_usage); | |
2de381f9 | 702 | die("unknown option %s", path); |
121481ab | 703 | } |
9ebe6cf9 AR |
704 | p = prefix_path(prefix, prefix_length, path); |
705 | update_one(p, NULL, 0); | |
227bdb18 | 706 | if (set_executable_bit) |
9ebe6cf9 AR |
707 | chmod_path(set_executable_bit, p); |
708 | if (p < path || p > path + strlen(path)) | |
709 | free((char*)p); | |
ee1bec3d JH |
710 | } |
711 | if (read_from_stdin) { | |
712 | struct strbuf buf; | |
713 | strbuf_init(&buf); | |
714 | while (1) { | |
a94d9948 | 715 | char *path_name; |
fb69a760 | 716 | const char *p; |
ee1bec3d JH |
717 | read_line(&buf, stdin, line_termination); |
718 | if (buf.eof) | |
719 | break; | |
a94d9948 JH |
720 | if (line_termination && buf.buf[0] == '"') |
721 | path_name = unquote_c_style(buf.buf, NULL); | |
722 | else | |
723 | path_name = buf.buf; | |
fb69a760 JH |
724 | p = prefix_path(prefix, prefix_length, path_name); |
725 | update_one(p, NULL, 0); | |
726 | if (set_executable_bit) | |
227bdb18 | 727 | chmod_path(set_executable_bit, p); |
0cc9e70c | 728 | if (p < path_name || p > path_name + strlen(path_name)) |
fb69a760 | 729 | free((char*) p); |
a94d9948 JH |
730 | if (path_name != buf.buf) |
731 | free(path_name); | |
9b63f501 | 732 | } |
e83c5163 | 733 | } |
2bd452d3 JH |
734 | |
735 | finish: | |
5cd5ace7 | 736 | if (active_cache_changed) { |
7b802b86 JH |
737 | if (newfd < 0) { |
738 | if (refresh_flags & REFRESH_QUIET) | |
739 | exit(128); | |
740 | die("unable to create '%s.lock': %s", | |
741 | get_index_file(), strerror(lock_error)); | |
742 | } | |
5cd5ace7 | 743 | if (write_cache(newfd, active_cache, active_nr) || |
30ca07a2 | 744 | close(newfd) || commit_locked_index(lock_file)) |
021b6e45 | 745 | die("Unable to write new index file"); |
5cd5ace7 | 746 | } |
9614b8dc | 747 | |
fefe81c9 LS |
748 | rollback_lock_file(lock_file); |
749 | ||
c4b83e61 | 750 | return has_errors ? 1 : 0; |
e83c5163 | 751 | } |