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