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