]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/update-index.c
read-cache*.h: move declarations for read-cache.c functions from cache.h
[thirdparty/git.git] / builtin / update-index.c
1 /*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #define USE_THE_INDEX_VARIABLE
7 #include "cache.h"
8 #include "bulk-checkin.h"
9 #include "config.h"
10 #include "environment.h"
11 #include "gettext.h"
12 #include "hash.h"
13 #include "hex.h"
14 #include "lockfile.h"
15 #include "quote.h"
16 #include "cache-tree.h"
17 #include "tree-walk.h"
18 #include "builtin.h"
19 #include "object-file.h"
20 #include "refs.h"
21 #include "resolve-undo.h"
22 #include "parse-options.h"
23 #include "pathspec.h"
24 #include "dir.h"
25 #include "read-cache.h"
26 #include "repository.h"
27 #include "setup.h"
28 #include "sparse-index.h"
29 #include "split-index.h"
30 #include "symlinks.h"
31 #include "fsmonitor.h"
32 #include "write-or-die.h"
33
34 /*
35 * Default to not allowing changes to the list of files. The
36 * tool doesn't actually care, but this makes it harder to add
37 * files to the revision control by mistake by doing something
38 * like "git update-index *" and suddenly having all the object
39 * files be revision controlled.
40 */
41 static int allow_add;
42 static int allow_remove;
43 static int allow_replace;
44 static int info_only;
45 static int force_remove;
46 static int verbose;
47 static int mark_valid_only;
48 static int mark_skip_worktree_only;
49 static int mark_fsmonitor_only;
50 static int ignore_skip_worktree_entries;
51 #define MARK_FLAG 1
52 #define UNMARK_FLAG 2
53 static struct strbuf mtime_dir = STRBUF_INIT;
54
55 /* Untracked cache mode */
56 enum uc_mode {
57 UC_UNSPECIFIED = -1,
58 UC_DISABLE = 0,
59 UC_ENABLE,
60 UC_TEST,
61 UC_FORCE
62 };
63
64 __attribute__((format (printf, 1, 2)))
65 static void report(const char *fmt, ...)
66 {
67 va_list vp;
68
69 if (!verbose)
70 return;
71
72 /*
73 * It is possible, though unlikely, that a caller could use the verbose
74 * output to synchronize with addition of objects to the object
75 * database. The current implementation of ODB transactions leaves
76 * objects invisible while a transaction is active, so flush the
77 * transaction here before reporting a change made by update-index.
78 */
79 flush_odb_transaction();
80 va_start(vp, fmt);
81 vprintf(fmt, vp);
82 putchar('\n');
83 va_end(vp);
84 }
85
86 static void remove_test_directory(void)
87 {
88 if (mtime_dir.len)
89 remove_dir_recursively(&mtime_dir, 0);
90 }
91
92 static const char *get_mtime_path(const char *path)
93 {
94 static struct strbuf sb = STRBUF_INIT;
95 strbuf_reset(&sb);
96 strbuf_addf(&sb, "%s/%s", mtime_dir.buf, path);
97 return sb.buf;
98 }
99
100 static void xmkdir(const char *path)
101 {
102 path = get_mtime_path(path);
103 if (mkdir(path, 0700))
104 die_errno(_("failed to create directory %s"), path);
105 }
106
107 static int xstat_mtime_dir(struct stat *st)
108 {
109 if (stat(mtime_dir.buf, st))
110 die_errno(_("failed to stat %s"), mtime_dir.buf);
111 return 0;
112 }
113
114 static int create_file(const char *path)
115 {
116 int fd;
117 path = get_mtime_path(path);
118 fd = xopen(path, O_CREAT | O_RDWR, 0644);
119 return fd;
120 }
121
122 static void xunlink(const char *path)
123 {
124 path = get_mtime_path(path);
125 if (unlink(path))
126 die_errno(_("failed to delete file %s"), path);
127 }
128
129 static void xrmdir(const char *path)
130 {
131 path = get_mtime_path(path);
132 if (rmdir(path))
133 die_errno(_("failed to delete directory %s"), path);
134 }
135
136 static void avoid_racy(void)
137 {
138 /*
139 * not use if we could usleep(10) if USE_NSEC is defined. The
140 * field nsec could be there, but the OS could choose to
141 * ignore it?
142 */
143 sleep(1);
144 }
145
146 static int test_if_untracked_cache_is_supported(void)
147 {
148 struct stat st;
149 struct stat_data base;
150 int fd, ret = 0;
151 char *cwd;
152
153 strbuf_addstr(&mtime_dir, "mtime-test-XXXXXX");
154 if (!mkdtemp(mtime_dir.buf))
155 die_errno("Could not make temporary directory");
156
157 cwd = xgetcwd();
158 fprintf(stderr, _("Testing mtime in '%s' "), cwd);
159 free(cwd);
160
161 atexit(remove_test_directory);
162 xstat_mtime_dir(&st);
163 fill_stat_data(&base, &st);
164 fputc('.', stderr);
165
166 avoid_racy();
167 fd = create_file("newfile");
168 xstat_mtime_dir(&st);
169 if (!match_stat_data(&base, &st)) {
170 close(fd);
171 fputc('\n', stderr);
172 fprintf_ln(stderr,_("directory stat info does not "
173 "change after adding a new file"));
174 goto done;
175 }
176 fill_stat_data(&base, &st);
177 fputc('.', stderr);
178
179 avoid_racy();
180 xmkdir("new-dir");
181 xstat_mtime_dir(&st);
182 if (!match_stat_data(&base, &st)) {
183 close(fd);
184 fputc('\n', stderr);
185 fprintf_ln(stderr, _("directory stat info does not change "
186 "after adding a new directory"));
187 goto done;
188 }
189 fill_stat_data(&base, &st);
190 fputc('.', stderr);
191
192 avoid_racy();
193 write_or_die(fd, "data", 4);
194 close(fd);
195 xstat_mtime_dir(&st);
196 if (match_stat_data(&base, &st)) {
197 fputc('\n', stderr);
198 fprintf_ln(stderr, _("directory stat info changes "
199 "after updating a file"));
200 goto done;
201 }
202 fputc('.', stderr);
203
204 avoid_racy();
205 close(create_file("new-dir/new"));
206 xstat_mtime_dir(&st);
207 if (match_stat_data(&base, &st)) {
208 fputc('\n', stderr);
209 fprintf_ln(stderr, _("directory stat info changes after "
210 "adding a file inside subdirectory"));
211 goto done;
212 }
213 fputc('.', stderr);
214
215 avoid_racy();
216 xunlink("newfile");
217 xstat_mtime_dir(&st);
218 if (!match_stat_data(&base, &st)) {
219 fputc('\n', stderr);
220 fprintf_ln(stderr, _("directory stat info does not "
221 "change after deleting a file"));
222 goto done;
223 }
224 fill_stat_data(&base, &st);
225 fputc('.', stderr);
226
227 avoid_racy();
228 xunlink("new-dir/new");
229 xrmdir("new-dir");
230 xstat_mtime_dir(&st);
231 if (!match_stat_data(&base, &st)) {
232 fputc('\n', stderr);
233 fprintf_ln(stderr, _("directory stat info does not "
234 "change after deleting a directory"));
235 goto done;
236 }
237
238 if (rmdir(mtime_dir.buf))
239 die_errno(_("failed to delete directory %s"), mtime_dir.buf);
240 fprintf_ln(stderr, _(" OK"));
241 ret = 1;
242
243 done:
244 strbuf_release(&mtime_dir);
245 return ret;
246 }
247
248 static int mark_ce_flags(const char *path, int flag, int mark)
249 {
250 int namelen = strlen(path);
251 int pos = index_name_pos(&the_index, path, namelen);
252 if (0 <= pos) {
253 mark_fsmonitor_invalid(&the_index, the_index.cache[pos]);
254 if (mark)
255 the_index.cache[pos]->ce_flags |= flag;
256 else
257 the_index.cache[pos]->ce_flags &= ~flag;
258 the_index.cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
259 cache_tree_invalidate_path(&the_index, path);
260 the_index.cache_changed |= CE_ENTRY_CHANGED;
261 return 0;
262 }
263 return -1;
264 }
265
266 static int remove_one_path(const char *path)
267 {
268 if (!allow_remove)
269 return error("%s: does not exist and --remove not passed", path);
270 if (remove_file_from_index(&the_index, path))
271 return error("%s: cannot remove from the index", path);
272 return 0;
273 }
274
275 /*
276 * Handle a path that couldn't be lstat'ed. It's either:
277 * - missing file (ENOENT or ENOTDIR). That's ok if we're
278 * supposed to be removing it and the removal actually
279 * succeeds.
280 * - permission error. That's never ok.
281 */
282 static int process_lstat_error(const char *path, int err)
283 {
284 if (is_missing_file_error(err))
285 return remove_one_path(path);
286 return error("lstat(\"%s\"): %s", path, strerror(err));
287 }
288
289 static int add_one_path(const struct cache_entry *old, const char *path, int len, struct stat *st)
290 {
291 int option;
292 struct cache_entry *ce;
293
294 /* Was the old index entry already up-to-date? */
295 if (old && !ce_stage(old) && !ie_match_stat(&the_index, old, st, 0))
296 return 0;
297
298 ce = make_empty_cache_entry(&the_index, len);
299 memcpy(ce->name, path, len);
300 ce->ce_flags = create_ce_flags(0);
301 ce->ce_namelen = len;
302 fill_stat_cache_info(&the_index, ce, st);
303 ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
304
305 if (index_path(&the_index, &ce->oid, path, st,
306 info_only ? 0 : HASH_WRITE_OBJECT)) {
307 discard_cache_entry(ce);
308 return -1;
309 }
310 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
311 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
312 if (add_index_entry(&the_index, ce, option)) {
313 discard_cache_entry(ce);
314 return error("%s: cannot add to the index - missing --add option?", path);
315 }
316 return 0;
317 }
318
319 /*
320 * Handle a path that was a directory. Four cases:
321 *
322 * - it's already a gitlink in the index, and we keep it that
323 * way, and update it if we can (if we cannot find the HEAD,
324 * we're going to keep it unchanged in the index!)
325 *
326 * - it's a *file* in the index, in which case it should be
327 * removed as a file if removal is allowed, since it doesn't
328 * exist as such any more. If removal isn't allowed, it's
329 * an error.
330 *
331 * (NOTE! This is old and arguably fairly strange behaviour.
332 * We might want to make this an error unconditionally, and
333 * use "--force-remove" if you actually want to force removal).
334 *
335 * - it used to exist as a subdirectory (ie multiple files with
336 * this particular prefix) in the index, in which case it's wrong
337 * to try to update it as a directory.
338 *
339 * - it doesn't exist at all in the index, but it is a valid
340 * git directory, and it should be *added* as a gitlink.
341 */
342 static int process_directory(const char *path, int len, struct stat *st)
343 {
344 struct object_id oid;
345 int pos = index_name_pos(&the_index, path, len);
346
347 /* Exact match: file or existing gitlink */
348 if (pos >= 0) {
349 const struct cache_entry *ce = the_index.cache[pos];
350 if (S_ISGITLINK(ce->ce_mode)) {
351
352 /* Do nothing to the index if there is no HEAD! */
353 if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
354 return 0;
355
356 return add_one_path(ce, path, len, st);
357 }
358 /* Should this be an unconditional error? */
359 return remove_one_path(path);
360 }
361
362 /* Inexact match: is there perhaps a subdirectory match? */
363 pos = -pos-1;
364 while (pos < the_index.cache_nr) {
365 const struct cache_entry *ce = the_index.cache[pos++];
366
367 if (strncmp(ce->name, path, len))
368 break;
369 if (ce->name[len] > '/')
370 break;
371 if (ce->name[len] < '/')
372 continue;
373
374 /* Subdirectory match - error out */
375 return error("%s: is a directory - add individual files instead", path);
376 }
377
378 /* No match - should we add it as a gitlink? */
379 if (!resolve_gitlink_ref(path, "HEAD", &oid))
380 return add_one_path(NULL, path, len, st);
381
382 /* Error out. */
383 return error("%s: is a directory - add files inside instead", path);
384 }
385
386 static int process_path(const char *path, struct stat *st, int stat_errno)
387 {
388 int pos, len;
389 const struct cache_entry *ce;
390
391 len = strlen(path);
392 if (has_symlink_leading_path(path, len))
393 return error("'%s' is beyond a symbolic link", path);
394
395 pos = index_name_pos(&the_index, path, len);
396 ce = pos < 0 ? NULL : the_index.cache[pos];
397 if (ce && ce_skip_worktree(ce)) {
398 /*
399 * working directory version is assumed "good"
400 * so updating it does not make sense.
401 * On the other hand, removing it from index should work
402 */
403 if (!ignore_skip_worktree_entries && allow_remove &&
404 remove_file_from_index(&the_index, path))
405 return error("%s: cannot remove from the index", path);
406 return 0;
407 }
408
409 /*
410 * First things first: get the stat information, to decide
411 * what to do about the pathname!
412 */
413 if (stat_errno)
414 return process_lstat_error(path, stat_errno);
415
416 if (S_ISDIR(st->st_mode))
417 return process_directory(path, len, st);
418
419 return add_one_path(ce, path, len, st);
420 }
421
422 static int add_cacheinfo(unsigned int mode, const struct object_id *oid,
423 const char *path, int stage)
424 {
425 int len, option;
426 struct cache_entry *ce;
427
428 if (!verify_path(path, mode))
429 return error("Invalid path '%s'", path);
430
431 len = strlen(path);
432 ce = make_empty_cache_entry(&the_index, len);
433
434 oidcpy(&ce->oid, oid);
435 memcpy(ce->name, path, len);
436 ce->ce_flags = create_ce_flags(stage);
437 ce->ce_namelen = len;
438 ce->ce_mode = create_ce_mode(mode);
439 if (assume_unchanged)
440 ce->ce_flags |= CE_VALID;
441 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
442 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
443 if (add_index_entry(&the_index, ce, option))
444 return error("%s: cannot add to the index - missing --add option?",
445 path);
446 report("add '%s'", path);
447 return 0;
448 }
449
450 static void chmod_path(char flip, const char *path)
451 {
452 int pos;
453 struct cache_entry *ce;
454
455 pos = index_name_pos(&the_index, path, strlen(path));
456 if (pos < 0)
457 goto fail;
458 ce = the_index.cache[pos];
459 if (chmod_index_entry(&the_index, ce, flip) < 0)
460 goto fail;
461
462 report("chmod %cx '%s'", flip, path);
463 return;
464 fail:
465 die("git update-index: cannot chmod %cx '%s'", flip, path);
466 }
467
468 static void update_one(const char *path)
469 {
470 int stat_errno = 0;
471 struct stat st;
472
473 if (mark_valid_only || mark_skip_worktree_only || force_remove ||
474 mark_fsmonitor_only)
475 st.st_mode = 0;
476 else if (lstat(path, &st) < 0) {
477 st.st_mode = 0;
478 stat_errno = errno;
479 } /* else stat is valid */
480
481 if (!verify_path(path, st.st_mode)) {
482 fprintf(stderr, "Ignoring path %s\n", path);
483 return;
484 }
485 if (mark_valid_only) {
486 if (mark_ce_flags(path, CE_VALID, mark_valid_only == MARK_FLAG))
487 die("Unable to mark file %s", path);
488 return;
489 }
490 if (mark_skip_worktree_only) {
491 if (mark_ce_flags(path, CE_SKIP_WORKTREE, mark_skip_worktree_only == MARK_FLAG))
492 die("Unable to mark file %s", path);
493 return;
494 }
495 if (mark_fsmonitor_only) {
496 if (mark_ce_flags(path, CE_FSMONITOR_VALID, mark_fsmonitor_only == MARK_FLAG))
497 die("Unable to mark file %s", path);
498 return;
499 }
500
501 if (force_remove) {
502 if (remove_file_from_index(&the_index, path))
503 die("git update-index: unable to remove %s", path);
504 report("remove '%s'", path);
505 return;
506 }
507 if (process_path(path, &st, stat_errno))
508 die("Unable to process path %s", path);
509 report("add '%s'", path);
510 }
511
512 static void read_index_info(int nul_term_line)
513 {
514 const int hexsz = the_hash_algo->hexsz;
515 struct strbuf buf = STRBUF_INIT;
516 struct strbuf uq = STRBUF_INIT;
517 strbuf_getline_fn getline_fn;
518
519 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
520 while (getline_fn(&buf, stdin) != EOF) {
521 char *ptr, *tab;
522 char *path_name;
523 struct object_id oid;
524 unsigned int mode;
525 unsigned long ul;
526 int stage;
527
528 /* This reads lines formatted in one of three formats:
529 *
530 * (1) mode SP sha1 TAB path
531 * The first format is what "git apply --index-info"
532 * reports, and used to reconstruct a partial tree
533 * that is used for phony merge base tree when falling
534 * back on 3-way merge.
535 *
536 * (2) mode SP type SP sha1 TAB path
537 * The second format is to stuff "git ls-tree" output
538 * into the index file.
539 *
540 * (3) mode SP sha1 SP stage TAB path
541 * This format is to put higher order stages into the
542 * index file and matches "git ls-files --stage" output.
543 */
544 errno = 0;
545 ul = strtoul(buf.buf, &ptr, 8);
546 if (ptr == buf.buf || *ptr != ' '
547 || errno || (unsigned int) ul != ul)
548 goto bad_line;
549 mode = ul;
550
551 tab = strchr(ptr, '\t');
552 if (!tab || tab - ptr < hexsz + 1)
553 goto bad_line;
554
555 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
556 stage = tab[-1] - '0';
557 ptr = tab + 1; /* point at the head of path */
558 tab = tab - 2; /* point at tail of sha1 */
559 }
560 else {
561 stage = 0;
562 ptr = tab + 1; /* point at the head of path */
563 }
564
565 if (get_oid_hex(tab - hexsz, &oid) ||
566 tab[-(hexsz + 1)] != ' ')
567 goto bad_line;
568
569 path_name = ptr;
570 if (!nul_term_line && path_name[0] == '"') {
571 strbuf_reset(&uq);
572 if (unquote_c_style(&uq, path_name, NULL)) {
573 die("git update-index: bad quoting of path name");
574 }
575 path_name = uq.buf;
576 }
577
578 if (!verify_path(path_name, mode)) {
579 fprintf(stderr, "Ignoring path %s\n", path_name);
580 continue;
581 }
582
583 if (!mode) {
584 /* mode == 0 means there is no such path -- remove */
585 if (remove_file_from_index(&the_index, path_name))
586 die("git update-index: unable to remove %s",
587 ptr);
588 }
589 else {
590 /* mode ' ' sha1 '\t' name
591 * ptr[-1] points at tab,
592 * ptr[-41] is at the beginning of sha1
593 */
594 ptr[-(hexsz + 2)] = ptr[-1] = 0;
595 if (add_cacheinfo(mode, &oid, path_name, stage))
596 die("git update-index: unable to update %s",
597 path_name);
598 }
599 continue;
600
601 bad_line:
602 die("malformed index info %s", buf.buf);
603 }
604 strbuf_release(&buf);
605 strbuf_release(&uq);
606 }
607
608 static const char * const update_index_usage[] = {
609 N_("git update-index [<options>] [--] [<file>...]"),
610 NULL
611 };
612
613 static struct object_id head_oid;
614 static struct object_id merge_head_oid;
615
616 static struct cache_entry *read_one_ent(const char *which,
617 struct object_id *ent, const char *path,
618 int namelen, int stage)
619 {
620 unsigned short mode;
621 struct object_id oid;
622 struct cache_entry *ce;
623
624 if (get_tree_entry(the_repository, ent, path, &oid, &mode)) {
625 if (which)
626 error("%s: not in %s branch.", path, which);
627 return NULL;
628 }
629 if (!the_index.sparse_index && mode == S_IFDIR) {
630 if (which)
631 error("%s: not a blob in %s branch.", path, which);
632 return NULL;
633 }
634 ce = make_empty_cache_entry(&the_index, namelen);
635
636 oidcpy(&ce->oid, &oid);
637 memcpy(ce->name, path, namelen);
638 ce->ce_flags = create_ce_flags(stage);
639 ce->ce_namelen = namelen;
640 ce->ce_mode = create_ce_mode(mode);
641 return ce;
642 }
643
644 static int unresolve_one(const char *path)
645 {
646 int namelen = strlen(path);
647 int pos;
648 int ret = 0;
649 struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
650
651 /* See if there is such entry in the index. */
652 pos = index_name_pos(&the_index, path, namelen);
653 if (0 <= pos) {
654 /* already merged */
655 pos = unmerge_index_entry_at(&the_index, pos);
656 if (pos < the_index.cache_nr) {
657 const struct cache_entry *ce = the_index.cache[pos];
658 if (ce_stage(ce) &&
659 ce_namelen(ce) == namelen &&
660 !memcmp(ce->name, path, namelen))
661 return 0;
662 }
663 /* no resolve-undo information; fall back */
664 } else {
665 /* If there isn't, either it is unmerged, or
666 * resolved as "removed" by mistake. We do not
667 * want to do anything in the former case.
668 */
669 pos = -pos-1;
670 if (pos < the_index.cache_nr) {
671 const struct cache_entry *ce = the_index.cache[pos];
672 if (ce_namelen(ce) == namelen &&
673 !memcmp(ce->name, path, namelen)) {
674 fprintf(stderr,
675 "%s: skipping still unmerged path.\n",
676 path);
677 goto free_return;
678 }
679 }
680 }
681
682 /* Grab blobs from given path from HEAD and MERGE_HEAD,
683 * stuff HEAD version in stage #2,
684 * stuff MERGE_HEAD version in stage #3.
685 */
686 ce_2 = read_one_ent("our", &head_oid, path, namelen, 2);
687 ce_3 = read_one_ent("their", &merge_head_oid, path, namelen, 3);
688
689 if (!ce_2 || !ce_3) {
690 ret = -1;
691 goto free_return;
692 }
693 if (oideq(&ce_2->oid, &ce_3->oid) &&
694 ce_2->ce_mode == ce_3->ce_mode) {
695 fprintf(stderr, "%s: identical in both, skipping.\n",
696 path);
697 goto free_return;
698 }
699
700 remove_file_from_index(&the_index, path);
701 if (add_index_entry(&the_index, ce_2, ADD_CACHE_OK_TO_ADD)) {
702 error("%s: cannot add our version to the index.", path);
703 ret = -1;
704 goto free_return;
705 }
706 if (!add_index_entry(&the_index, ce_3, ADD_CACHE_OK_TO_ADD))
707 return 0;
708 error("%s: cannot add their version to the index.", path);
709 ret = -1;
710 free_return:
711 discard_cache_entry(ce_2);
712 discard_cache_entry(ce_3);
713 return ret;
714 }
715
716 static void read_head_pointers(void)
717 {
718 if (read_ref("HEAD", &head_oid))
719 die("No HEAD -- no initial commit yet?");
720 if (read_ref("MERGE_HEAD", &merge_head_oid)) {
721 fprintf(stderr, "Not in the middle of a merge.\n");
722 exit(0);
723 }
724 }
725
726 static int do_unresolve(int ac, const char **av,
727 const char *prefix, int prefix_length)
728 {
729 int i;
730 int err = 0;
731
732 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
733 * are not doing a merge, so exit with success status.
734 */
735 read_head_pointers();
736
737 for (i = 1; i < ac; i++) {
738 const char *arg = av[i];
739 char *p = prefix_path(prefix, prefix_length, arg);
740 err |= unresolve_one(p);
741 free(p);
742 }
743 return err;
744 }
745
746 static int do_reupdate(const char **paths,
747 const char *prefix)
748 {
749 /* Read HEAD and run update-index on paths that are
750 * merged and already different between index and HEAD.
751 */
752 int pos;
753 int has_head = 1;
754 struct pathspec pathspec;
755
756 parse_pathspec(&pathspec, 0,
757 PATHSPEC_PREFER_CWD,
758 prefix, paths);
759
760 if (read_ref("HEAD", &head_oid))
761 /* If there is no HEAD, that means it is an initial
762 * commit. Update everything in the index.
763 */
764 has_head = 0;
765 redo:
766 for (pos = 0; pos < the_index.cache_nr; pos++) {
767 const struct cache_entry *ce = the_index.cache[pos];
768 struct cache_entry *old = NULL;
769 int save_nr;
770 char *path;
771
772 if (ce_stage(ce) || !ce_path_match(&the_index, ce, &pathspec, NULL))
773 continue;
774 if (has_head)
775 old = read_one_ent(NULL, &head_oid,
776 ce->name, ce_namelen(ce), 0);
777 if (old && ce->ce_mode == old->ce_mode &&
778 oideq(&ce->oid, &old->oid)) {
779 discard_cache_entry(old);
780 continue; /* unchanged */
781 }
782
783 /* At this point, we know the contents of the sparse directory are
784 * modified with respect to HEAD, so we expand the index and restart
785 * to process each path individually
786 */
787 if (S_ISSPARSEDIR(ce->ce_mode)) {
788 ensure_full_index(&the_index);
789 goto redo;
790 }
791
792 /* Be careful. The working tree may not have the
793 * path anymore, in which case, under 'allow_remove',
794 * or worse yet 'allow_replace', active_nr may decrease.
795 */
796 save_nr = the_index.cache_nr;
797 path = xstrdup(ce->name);
798 update_one(path);
799 free(path);
800 discard_cache_entry(old);
801 if (save_nr != the_index.cache_nr)
802 goto redo;
803 }
804 clear_pathspec(&pathspec);
805 return 0;
806 }
807
808 struct refresh_params {
809 unsigned int flags;
810 int *has_errors;
811 };
812
813 static int refresh(struct refresh_params *o, unsigned int flag)
814 {
815 setup_work_tree();
816 repo_read_index(the_repository);
817 *o->has_errors |= refresh_index(&the_index, o->flags | flag, NULL,
818 NULL, NULL);
819 if (has_racy_timestamp(&the_index)) {
820 /*
821 * Even if nothing else has changed, updating the file
822 * increases the chance that racy timestamps become
823 * non-racy, helping future run-time performance.
824 * We do that even in case of "errors" returned by
825 * refresh_index() as these are no actual errors.
826 * cmd_status() does the same.
827 */
828 the_index.cache_changed |= SOMETHING_CHANGED;
829 }
830 return 0;
831 }
832
833 static int refresh_callback(const struct option *opt,
834 const char *arg, int unset)
835 {
836 BUG_ON_OPT_NEG(unset);
837 BUG_ON_OPT_ARG(arg);
838 return refresh(opt->value, 0);
839 }
840
841 static int really_refresh_callback(const struct option *opt,
842 const char *arg, int unset)
843 {
844 BUG_ON_OPT_NEG(unset);
845 BUG_ON_OPT_ARG(arg);
846 return refresh(opt->value, REFRESH_REALLY);
847 }
848
849 static int chmod_callback(const struct option *opt,
850 const char *arg, int unset)
851 {
852 char *flip = opt->value;
853 BUG_ON_OPT_NEG(unset);
854 if ((arg[0] != '-' && arg[0] != '+') || arg[1] != 'x' || arg[2])
855 return error("option 'chmod' expects \"+x\" or \"-x\"");
856 *flip = arg[0];
857 return 0;
858 }
859
860 static int resolve_undo_clear_callback(const struct option *opt,
861 const char *arg, int unset)
862 {
863 BUG_ON_OPT_NEG(unset);
864 BUG_ON_OPT_ARG(arg);
865 resolve_undo_clear_index(&the_index);
866 return 0;
867 }
868
869 static int parse_new_style_cacheinfo(const char *arg,
870 unsigned int *mode,
871 struct object_id *oid,
872 const char **path)
873 {
874 unsigned long ul;
875 char *endp;
876 const char *p;
877
878 if (!arg)
879 return -1;
880
881 errno = 0;
882 ul = strtoul(arg, &endp, 8);
883 if (errno || endp == arg || *endp != ',' || (unsigned int) ul != ul)
884 return -1; /* not a new-style cacheinfo */
885 *mode = ul;
886 endp++;
887 if (parse_oid_hex(endp, oid, &p) || *p != ',')
888 return -1;
889 *path = p + 1;
890 return 0;
891 }
892
893 static enum parse_opt_result cacheinfo_callback(
894 struct parse_opt_ctx_t *ctx, const struct option *opt,
895 const char *arg, int unset)
896 {
897 struct object_id oid;
898 unsigned int mode;
899 const char *path;
900
901 BUG_ON_OPT_NEG(unset);
902 BUG_ON_OPT_ARG(arg);
903
904 if (!parse_new_style_cacheinfo(ctx->argv[1], &mode, &oid, &path)) {
905 if (add_cacheinfo(mode, &oid, path, 0))
906 die("git update-index: --cacheinfo cannot add %s", path);
907 ctx->argv++;
908 ctx->argc--;
909 return 0;
910 }
911 if (ctx->argc <= 3)
912 return error("option 'cacheinfo' expects <mode>,<sha1>,<path>");
913 if (strtoul_ui(*++ctx->argv, 8, &mode) ||
914 get_oid_hex(*++ctx->argv, &oid) ||
915 add_cacheinfo(mode, &oid, *++ctx->argv, 0))
916 die("git update-index: --cacheinfo cannot add %s", *ctx->argv);
917 ctx->argc -= 3;
918 return 0;
919 }
920
921 static enum parse_opt_result stdin_cacheinfo_callback(
922 struct parse_opt_ctx_t *ctx, const struct option *opt,
923 const char *arg, int unset)
924 {
925 int *nul_term_line = opt->value;
926
927 BUG_ON_OPT_NEG(unset);
928 BUG_ON_OPT_ARG(arg);
929
930 if (ctx->argc != 1)
931 return error("option '%s' must be the last argument", opt->long_name);
932 allow_add = allow_replace = allow_remove = 1;
933 read_index_info(*nul_term_line);
934 return 0;
935 }
936
937 static enum parse_opt_result stdin_callback(
938 struct parse_opt_ctx_t *ctx, const struct option *opt,
939 const char *arg, int unset)
940 {
941 int *read_from_stdin = opt->value;
942
943 BUG_ON_OPT_NEG(unset);
944 BUG_ON_OPT_ARG(arg);
945
946 if (ctx->argc != 1)
947 return error("option '%s' must be the last argument", opt->long_name);
948 *read_from_stdin = 1;
949 return 0;
950 }
951
952 static enum parse_opt_result unresolve_callback(
953 struct parse_opt_ctx_t *ctx, const struct option *opt,
954 const char *arg, int unset)
955 {
956 int *has_errors = opt->value;
957 const char *prefix = startup_info->prefix;
958
959 BUG_ON_OPT_NEG(unset);
960 BUG_ON_OPT_ARG(arg);
961
962 /* consume remaining arguments. */
963 *has_errors = do_unresolve(ctx->argc, ctx->argv,
964 prefix, prefix ? strlen(prefix) : 0);
965 if (*has_errors)
966 the_index.cache_changed = 0;
967
968 ctx->argv += ctx->argc - 1;
969 ctx->argc = 1;
970 return 0;
971 }
972
973 static enum parse_opt_result reupdate_callback(
974 struct parse_opt_ctx_t *ctx, const struct option *opt,
975 const char *arg, int unset)
976 {
977 int *has_errors = opt->value;
978 const char *prefix = startup_info->prefix;
979
980 BUG_ON_OPT_NEG(unset);
981 BUG_ON_OPT_ARG(arg);
982
983 /* consume remaining arguments. */
984 setup_work_tree();
985 *has_errors = do_reupdate(ctx->argv + 1, prefix);
986 if (*has_errors)
987 the_index.cache_changed = 0;
988
989 ctx->argv += ctx->argc - 1;
990 ctx->argc = 1;
991 return 0;
992 }
993
994 int cmd_update_index(int argc, const char **argv, const char *prefix)
995 {
996 int newfd, entries, has_errors = 0, nul_term_line = 0;
997 enum uc_mode untracked_cache = UC_UNSPECIFIED;
998 int read_from_stdin = 0;
999 int prefix_length = prefix ? strlen(prefix) : 0;
1000 int preferred_index_format = 0;
1001 char set_executable_bit = 0;
1002 struct refresh_params refresh_args = {0, &has_errors};
1003 int lock_error = 0;
1004 int split_index = -1;
1005 int force_write = 0;
1006 int fsmonitor = -1;
1007 struct lock_file lock_file = LOCK_INIT;
1008 struct parse_opt_ctx_t ctx;
1009 strbuf_getline_fn getline_fn;
1010 int parseopt_state = PARSE_OPT_UNKNOWN;
1011 struct repository *r = the_repository;
1012 struct option options[] = {
1013 OPT_BIT('q', NULL, &refresh_args.flags,
1014 N_("continue refresh even when index needs update"),
1015 REFRESH_QUIET),
1016 OPT_BIT(0, "ignore-submodules", &refresh_args.flags,
1017 N_("refresh: ignore submodules"),
1018 REFRESH_IGNORE_SUBMODULES),
1019 OPT_SET_INT(0, "add", &allow_add,
1020 N_("do not ignore new files"), 1),
1021 OPT_SET_INT(0, "replace", &allow_replace,
1022 N_("let files replace directories and vice-versa"), 1),
1023 OPT_SET_INT(0, "remove", &allow_remove,
1024 N_("notice files missing from worktree"), 1),
1025 OPT_BIT(0, "unmerged", &refresh_args.flags,
1026 N_("refresh even if index contains unmerged entries"),
1027 REFRESH_UNMERGED),
1028 OPT_CALLBACK_F(0, "refresh", &refresh_args, NULL,
1029 N_("refresh stat information"),
1030 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1031 refresh_callback),
1032 OPT_CALLBACK_F(0, "really-refresh", &refresh_args, NULL,
1033 N_("like --refresh, but ignore assume-unchanged setting"),
1034 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1035 really_refresh_callback),
1036 {OPTION_LOWLEVEL_CALLBACK, 0, "cacheinfo", NULL,
1037 N_("<mode>,<object>,<path>"),
1038 N_("add the specified entry to the index"),
1039 PARSE_OPT_NOARG | /* disallow --cacheinfo=<mode> form */
1040 PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
1041 NULL, 0,
1042 cacheinfo_callback},
1043 OPT_CALLBACK_F(0, "chmod", &set_executable_bit, "(+|-)x",
1044 N_("override the executable bit of the listed files"),
1045 PARSE_OPT_NONEG,
1046 chmod_callback),
1047 {OPTION_SET_INT, 0, "assume-unchanged", &mark_valid_only, NULL,
1048 N_("mark files as \"not changing\""),
1049 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
1050 {OPTION_SET_INT, 0, "no-assume-unchanged", &mark_valid_only, NULL,
1051 N_("clear assumed-unchanged bit"),
1052 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
1053 {OPTION_SET_INT, 0, "skip-worktree", &mark_skip_worktree_only, NULL,
1054 N_("mark files as \"index-only\""),
1055 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
1056 {OPTION_SET_INT, 0, "no-skip-worktree", &mark_skip_worktree_only, NULL,
1057 N_("clear skip-worktree bit"),
1058 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
1059 OPT_BOOL(0, "ignore-skip-worktree-entries", &ignore_skip_worktree_entries,
1060 N_("do not touch index-only entries")),
1061 OPT_SET_INT(0, "info-only", &info_only,
1062 N_("add to index only; do not add content to object database"), 1),
1063 OPT_SET_INT(0, "force-remove", &force_remove,
1064 N_("remove named paths even if present in worktree"), 1),
1065 OPT_BOOL('z', NULL, &nul_term_line,
1066 N_("with --stdin: input lines are terminated by null bytes")),
1067 {OPTION_LOWLEVEL_CALLBACK, 0, "stdin", &read_from_stdin, NULL,
1068 N_("read list of paths to be updated from standard input"),
1069 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
1070 NULL, 0, stdin_callback},
1071 {OPTION_LOWLEVEL_CALLBACK, 0, "index-info", &nul_term_line, NULL,
1072 N_("add entries from standard input to the index"),
1073 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
1074 NULL, 0, stdin_cacheinfo_callback},
1075 {OPTION_LOWLEVEL_CALLBACK, 0, "unresolve", &has_errors, NULL,
1076 N_("repopulate stages #2 and #3 for the listed paths"),
1077 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
1078 NULL, 0, unresolve_callback},
1079 {OPTION_LOWLEVEL_CALLBACK, 'g', "again", &has_errors, NULL,
1080 N_("only update entries that differ from HEAD"),
1081 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
1082 NULL, 0, reupdate_callback},
1083 OPT_BIT(0, "ignore-missing", &refresh_args.flags,
1084 N_("ignore files missing from worktree"),
1085 REFRESH_IGNORE_MISSING),
1086 OPT_SET_INT(0, "verbose", &verbose,
1087 N_("report actions to standard output"), 1),
1088 OPT_CALLBACK_F(0, "clear-resolve-undo", NULL, NULL,
1089 N_("(for porcelains) forget saved unresolved conflicts"),
1090 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1091 resolve_undo_clear_callback),
1092 OPT_INTEGER(0, "index-version", &preferred_index_format,
1093 N_("write index in this format")),
1094 OPT_BOOL(0, "split-index", &split_index,
1095 N_("enable or disable split index")),
1096 OPT_BOOL(0, "untracked-cache", &untracked_cache,
1097 N_("enable/disable untracked cache")),
1098 OPT_SET_INT(0, "test-untracked-cache", &untracked_cache,
1099 N_("test if the filesystem supports untracked cache"), UC_TEST),
1100 OPT_SET_INT(0, "force-untracked-cache", &untracked_cache,
1101 N_("enable untracked cache without testing the filesystem"), UC_FORCE),
1102 OPT_SET_INT(0, "force-write-index", &force_write,
1103 N_("write out the index even if is not flagged as changed"), 1),
1104 OPT_BOOL(0, "fsmonitor", &fsmonitor,
1105 N_("enable or disable file system monitor")),
1106 {OPTION_SET_INT, 0, "fsmonitor-valid", &mark_fsmonitor_only, NULL,
1107 N_("mark files as fsmonitor valid"),
1108 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
1109 {OPTION_SET_INT, 0, "no-fsmonitor-valid", &mark_fsmonitor_only, NULL,
1110 N_("clear fsmonitor valid bit"),
1111 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
1112 OPT_END()
1113 };
1114
1115 if (argc == 2 && !strcmp(argv[1], "-h"))
1116 usage_with_options(update_index_usage, options);
1117
1118 git_config(git_default_config, NULL);
1119
1120 prepare_repo_settings(r);
1121 the_repository->settings.command_requires_full_index = 0;
1122
1123 /* we will diagnose later if it turns out that we need to update it */
1124 newfd = repo_hold_locked_index(the_repository, &lock_file, 0);
1125 if (newfd < 0)
1126 lock_error = errno;
1127
1128 entries = repo_read_index(the_repository);
1129 if (entries < 0)
1130 die("cache corrupted");
1131
1132 the_index.updated_skipworktree = 1;
1133
1134 /*
1135 * Custom copy of parse_options() because we want to handle
1136 * filename arguments as they come.
1137 */
1138 parse_options_start(&ctx, argc, argv, prefix,
1139 options, PARSE_OPT_STOP_AT_NON_OPTION);
1140
1141 /*
1142 * Allow the object layer to optimize adding multiple objects in
1143 * a batch.
1144 */
1145 begin_odb_transaction();
1146 while (ctx.argc) {
1147 if (parseopt_state != PARSE_OPT_DONE)
1148 parseopt_state = parse_options_step(&ctx, options,
1149 update_index_usage);
1150 if (!ctx.argc)
1151 break;
1152 switch (parseopt_state) {
1153 case PARSE_OPT_HELP:
1154 case PARSE_OPT_ERROR:
1155 exit(129);
1156 case PARSE_OPT_COMPLETE:
1157 exit(0);
1158 case PARSE_OPT_NON_OPTION:
1159 case PARSE_OPT_DONE:
1160 {
1161 const char *path = ctx.argv[0];
1162 char *p;
1163
1164 setup_work_tree();
1165 p = prefix_path(prefix, prefix_length, path);
1166 update_one(p);
1167 if (set_executable_bit)
1168 chmod_path(set_executable_bit, p);
1169 free(p);
1170 ctx.argc--;
1171 ctx.argv++;
1172 break;
1173 }
1174 case PARSE_OPT_UNKNOWN:
1175 if (ctx.argv[0][1] == '-')
1176 error("unknown option '%s'", ctx.argv[0] + 2);
1177 else
1178 error("unknown switch '%c'", *ctx.opt);
1179 usage_with_options(update_index_usage, options);
1180 }
1181 }
1182 argc = parse_options_end(&ctx);
1183
1184 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
1185 if (preferred_index_format) {
1186 if (preferred_index_format < INDEX_FORMAT_LB ||
1187 INDEX_FORMAT_UB < preferred_index_format)
1188 die("index-version %d not in range: %d..%d",
1189 preferred_index_format,
1190 INDEX_FORMAT_LB, INDEX_FORMAT_UB);
1191
1192 if (the_index.version != preferred_index_format)
1193 the_index.cache_changed |= SOMETHING_CHANGED;
1194 the_index.version = preferred_index_format;
1195 }
1196
1197 if (read_from_stdin) {
1198 struct strbuf buf = STRBUF_INIT;
1199 struct strbuf unquoted = STRBUF_INIT;
1200
1201 setup_work_tree();
1202 while (getline_fn(&buf, stdin) != EOF) {
1203 char *p;
1204 if (!nul_term_line && buf.buf[0] == '"') {
1205 strbuf_reset(&unquoted);
1206 if (unquote_c_style(&unquoted, buf.buf, NULL))
1207 die("line is badly quoted");
1208 strbuf_swap(&buf, &unquoted);
1209 }
1210 p = prefix_path(prefix, prefix_length, buf.buf);
1211 update_one(p);
1212 if (set_executable_bit)
1213 chmod_path(set_executable_bit, p);
1214 free(p);
1215 }
1216 strbuf_release(&unquoted);
1217 strbuf_release(&buf);
1218 }
1219
1220 /*
1221 * By now we have added all of the new objects
1222 */
1223 end_odb_transaction();
1224
1225 if (split_index > 0) {
1226 if (git_config_get_split_index() == 0)
1227 warning(_("core.splitIndex is set to false; "
1228 "remove or change it, if you really want to "
1229 "enable split index"));
1230 if (the_index.split_index)
1231 the_index.cache_changed |= SPLIT_INDEX_ORDERED;
1232 else
1233 add_split_index(&the_index);
1234 } else if (!split_index) {
1235 if (git_config_get_split_index() == 1)
1236 warning(_("core.splitIndex is set to true; "
1237 "remove or change it, if you really want to "
1238 "disable split index"));
1239 remove_split_index(&the_index);
1240 }
1241
1242 prepare_repo_settings(r);
1243 switch (untracked_cache) {
1244 case UC_UNSPECIFIED:
1245 break;
1246 case UC_DISABLE:
1247 if (r->settings.core_untracked_cache == UNTRACKED_CACHE_WRITE)
1248 warning(_("core.untrackedCache is set to true; "
1249 "remove or change it, if you really want to "
1250 "disable the untracked cache"));
1251 remove_untracked_cache(&the_index);
1252 report(_("Untracked cache disabled"));
1253 break;
1254 case UC_TEST:
1255 setup_work_tree();
1256 return !test_if_untracked_cache_is_supported();
1257 case UC_ENABLE:
1258 case UC_FORCE:
1259 if (r->settings.core_untracked_cache == UNTRACKED_CACHE_REMOVE)
1260 warning(_("core.untrackedCache is set to false; "
1261 "remove or change it, if you really want to "
1262 "enable the untracked cache"));
1263 add_untracked_cache(&the_index);
1264 report(_("Untracked cache enabled for '%s'"), get_git_work_tree());
1265 break;
1266 default:
1267 BUG("bad untracked_cache value: %d", untracked_cache);
1268 }
1269
1270 if (fsmonitor > 0) {
1271 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(r);
1272 enum fsmonitor_reason reason = fsm_settings__get_reason(r);
1273
1274 /*
1275 * The user wants to turn on FSMonitor using the command
1276 * line argument. (We don't know (or care) whether that
1277 * is the IPC or HOOK version.)
1278 *
1279 * Use one of the __get routines to force load the FSMonitor
1280 * config settings into the repo-settings. That will detect
1281 * whether the file system is compatible so that we can stop
1282 * here with a nice error message.
1283 */
1284 if (reason > FSMONITOR_REASON_OK)
1285 die("%s",
1286 fsm_settings__get_incompatible_msg(r, reason));
1287
1288 if (fsm_mode == FSMONITOR_MODE_DISABLED) {
1289 warning(_("core.fsmonitor is unset; "
1290 "set it if you really want to "
1291 "enable fsmonitor"));
1292 }
1293 add_fsmonitor(&the_index);
1294 report(_("fsmonitor enabled"));
1295 } else if (!fsmonitor) {
1296 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(r);
1297 if (fsm_mode > FSMONITOR_MODE_DISABLED)
1298 warning(_("core.fsmonitor is set; "
1299 "remove it if you really want to "
1300 "disable fsmonitor"));
1301 remove_fsmonitor(&the_index);
1302 report(_("fsmonitor disabled"));
1303 }
1304
1305 if (the_index.cache_changed || force_write) {
1306 if (newfd < 0) {
1307 if (refresh_args.flags & REFRESH_QUIET)
1308 exit(128);
1309 unable_to_lock_die(get_index_file(), lock_error);
1310 }
1311 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
1312 die("Unable to write new index file");
1313 }
1314
1315 rollback_lock_file(&lock_file);
1316
1317 return has_errors ? 1 : 0;
1318 }