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