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