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