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