completion: complete `git push --force-with-lease=`
[git/mjg.git] / builtin / update-index.c
blobebea285e1b6863bdb8e4b2a30d1487d18ecc5084
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "quote.h"
8 #include "cache-tree.h"
9 #include "tree-walk.h"
10 #include "builtin.h"
11 #include "refs.h"
12 #include "resolve-undo.h"
13 #include "parse-options.h"
14 #include "pathspec.h"
15 #include "dir.h"
18 * Default to not allowing changes to the list of files. The
19 * tool doesn't actually care, but this makes it harder to add
20 * files to the revision control by mistake by doing something
21 * like "git update-index *" and suddenly having all the object
22 * files be revision controlled.
24 static int allow_add;
25 static int allow_remove;
26 static int allow_replace;
27 static int info_only;
28 static int force_remove;
29 static int verbose;
30 static int mark_valid_only;
31 static int mark_skip_worktree_only;
32 #define MARK_FLAG 1
33 #define UNMARK_FLAG 2
35 __attribute__((format (printf, 1, 2)))
36 static void report(const char *fmt, ...)
38 va_list vp;
40 if (!verbose)
41 return;
43 va_start(vp, fmt);
44 vprintf(fmt, vp);
45 putchar('\n');
46 va_end(vp);
49 static int mark_ce_flags(const char *path, int flag, int mark)
51 int namelen = strlen(path);
52 int pos = cache_name_pos(path, namelen);
53 if (0 <= pos) {
54 if (mark)
55 active_cache[pos]->ce_flags |= flag;
56 else
57 active_cache[pos]->ce_flags &= ~flag;
58 cache_tree_invalidate_path(active_cache_tree, path);
59 active_cache_changed = 1;
60 return 0;
62 return -1;
65 static int remove_one_path(const char *path)
67 if (!allow_remove)
68 return error("%s: does not exist and --remove not passed", path);
69 if (remove_file_from_cache(path))
70 return error("%s: cannot remove from the index", path);
71 return 0;
75 * Handle a path that couldn't be lstat'ed. It's either:
76 * - missing file (ENOENT or ENOTDIR). That's ok if we're
77 * supposed to be removing it and the removal actually
78 * succeeds.
79 * - permission error. That's never ok.
81 static int process_lstat_error(const char *path, int err)
83 if (err == ENOENT || err == ENOTDIR)
84 return remove_one_path(path);
85 return error("lstat(\"%s\"): %s", path, strerror(errno));
88 static int add_one_path(const struct cache_entry *old, const char *path, int len, struct stat *st)
90 int option, size;
91 struct cache_entry *ce;
93 /* Was the old index entry already up-to-date? */
94 if (old && !ce_stage(old) && !ce_match_stat(old, st, 0))
95 return 0;
97 size = cache_entry_size(len);
98 ce = xcalloc(1, size);
99 memcpy(ce->name, path, len);
100 ce->ce_flags = create_ce_flags(0);
101 ce->ce_namelen = len;
102 fill_stat_cache_info(ce, st);
103 ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
105 if (index_path(ce->sha1, path, st,
106 info_only ? 0 : HASH_WRITE_OBJECT)) {
107 free(ce);
108 return -1;
110 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
111 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
112 if (add_cache_entry(ce, option))
113 return error("%s: cannot add to the index - missing --add option?", path);
114 return 0;
118 * Handle a path that was a directory. Four cases:
120 * - it's already a gitlink in the index, and we keep it that
121 * way, and update it if we can (if we cannot find the HEAD,
122 * we're going to keep it unchanged in the index!)
124 * - it's a *file* in the index, in which case it should be
125 * removed as a file if removal is allowed, since it doesn't
126 * exist as such any more. If removal isn't allowed, it's
127 * an error.
129 * (NOTE! This is old and arguably fairly strange behaviour.
130 * We might want to make this an error unconditionally, and
131 * use "--force-remove" if you actually want to force removal).
133 * - it used to exist as a subdirectory (ie multiple files with
134 * this particular prefix) in the index, in which case it's wrong
135 * to try to update it as a directory.
137 * - it doesn't exist at all in the index, but it is a valid
138 * git directory, and it should be *added* as a gitlink.
140 static int process_directory(const char *path, int len, struct stat *st)
142 unsigned char sha1[20];
143 int pos = cache_name_pos(path, len);
145 /* Exact match: file or existing gitlink */
146 if (pos >= 0) {
147 const struct cache_entry *ce = active_cache[pos];
148 if (S_ISGITLINK(ce->ce_mode)) {
150 /* Do nothing to the index if there is no HEAD! */
151 if (resolve_gitlink_ref(path, "HEAD", sha1) < 0)
152 return 0;
154 return add_one_path(ce, path, len, st);
156 /* Should this be an unconditional error? */
157 return remove_one_path(path);
160 /* Inexact match: is there perhaps a subdirectory match? */
161 pos = -pos-1;
162 while (pos < active_nr) {
163 const struct cache_entry *ce = active_cache[pos++];
165 if (strncmp(ce->name, path, len))
166 break;
167 if (ce->name[len] > '/')
168 break;
169 if (ce->name[len] < '/')
170 continue;
172 /* Subdirectory match - error out */
173 return error("%s: is a directory - add individual files instead", path);
176 /* No match - should we add it as a gitlink? */
177 if (!resolve_gitlink_ref(path, "HEAD", sha1))
178 return add_one_path(NULL, path, len, st);
180 /* Error out. */
181 return error("%s: is a directory - add files inside instead", path);
184 static int process_path(const char *path)
186 int pos, len;
187 struct stat st;
188 const struct cache_entry *ce;
190 len = strlen(path);
191 if (has_symlink_leading_path(path, len))
192 return error("'%s' is beyond a symbolic link", path);
194 pos = cache_name_pos(path, len);
195 ce = pos < 0 ? NULL : active_cache[pos];
196 if (ce && ce_skip_worktree(ce)) {
198 * working directory version is assumed "good"
199 * so updating it does not make sense.
200 * On the other hand, removing it from index should work
202 if (allow_remove && remove_file_from_cache(path))
203 return error("%s: cannot remove from the index", path);
204 return 0;
208 * First things first: get the stat information, to decide
209 * what to do about the pathname!
211 if (lstat(path, &st) < 0)
212 return process_lstat_error(path, errno);
214 if (S_ISDIR(st.st_mode))
215 return process_directory(path, len, &st);
217 return add_one_path(ce, path, len, &st);
220 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
221 const char *path, int stage)
223 int size, len, option;
224 struct cache_entry *ce;
226 if (!verify_path(path))
227 return error("Invalid path '%s'", path);
229 len = strlen(path);
230 size = cache_entry_size(len);
231 ce = xcalloc(1, size);
233 hashcpy(ce->sha1, sha1);
234 memcpy(ce->name, path, len);
235 ce->ce_flags = create_ce_flags(stage);
236 ce->ce_namelen = len;
237 ce->ce_mode = create_ce_mode(mode);
238 if (assume_unchanged)
239 ce->ce_flags |= CE_VALID;
240 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
241 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
242 if (add_cache_entry(ce, option))
243 return error("%s: cannot add to the index - missing --add option?",
244 path);
245 report("add '%s'", path);
246 return 0;
249 static void chmod_path(int flip, const char *path)
251 int pos;
252 struct cache_entry *ce;
253 unsigned int mode;
255 pos = cache_name_pos(path, strlen(path));
256 if (pos < 0)
257 goto fail;
258 ce = active_cache[pos];
259 mode = ce->ce_mode;
260 if (!S_ISREG(mode))
261 goto fail;
262 switch (flip) {
263 case '+':
264 ce->ce_mode |= 0111; break;
265 case '-':
266 ce->ce_mode &= ~0111; break;
267 default:
268 goto fail;
270 cache_tree_invalidate_path(active_cache_tree, path);
271 active_cache_changed = 1;
272 report("chmod %cx '%s'", flip, path);
273 return;
274 fail:
275 die("git update-index: cannot chmod %cx '%s'", flip, path);
278 static void update_one(const char *path)
280 if (!verify_path(path)) {
281 fprintf(stderr, "Ignoring path %s\n", path);
282 return;
284 if (mark_valid_only) {
285 if (mark_ce_flags(path, CE_VALID, mark_valid_only == MARK_FLAG))
286 die("Unable to mark file %s", path);
287 return;
289 if (mark_skip_worktree_only) {
290 if (mark_ce_flags(path, CE_SKIP_WORKTREE, mark_skip_worktree_only == MARK_FLAG))
291 die("Unable to mark file %s", path);
292 return;
295 if (force_remove) {
296 if (remove_file_from_cache(path))
297 die("git update-index: unable to remove %s", path);
298 report("remove '%s'", path);
299 return;
301 if (process_path(path))
302 die("Unable to process path %s", path);
303 report("add '%s'", path);
306 static void read_index_info(int line_termination)
308 struct strbuf buf = STRBUF_INIT;
309 struct strbuf uq = STRBUF_INIT;
311 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
312 char *ptr, *tab;
313 char *path_name;
314 unsigned char sha1[20];
315 unsigned int mode;
316 unsigned long ul;
317 int stage;
319 /* This reads lines formatted in one of three formats:
321 * (1) mode SP sha1 TAB path
322 * The first format is what "git apply --index-info"
323 * reports, and used to reconstruct a partial tree
324 * that is used for phony merge base tree when falling
325 * back on 3-way merge.
327 * (2) mode SP type SP sha1 TAB path
328 * The second format is to stuff "git ls-tree" output
329 * into the index file.
331 * (3) mode SP sha1 SP stage TAB path
332 * This format is to put higher order stages into the
333 * index file and matches "git ls-files --stage" output.
335 errno = 0;
336 ul = strtoul(buf.buf, &ptr, 8);
337 if (ptr == buf.buf || *ptr != ' '
338 || errno || (unsigned int) ul != ul)
339 goto bad_line;
340 mode = ul;
342 tab = strchr(ptr, '\t');
343 if (!tab || tab - ptr < 41)
344 goto bad_line;
346 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
347 stage = tab[-1] - '0';
348 ptr = tab + 1; /* point at the head of path */
349 tab = tab - 2; /* point at tail of sha1 */
351 else {
352 stage = 0;
353 ptr = tab + 1; /* point at the head of path */
356 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
357 goto bad_line;
359 path_name = ptr;
360 if (line_termination && path_name[0] == '"') {
361 strbuf_reset(&uq);
362 if (unquote_c_style(&uq, path_name, NULL)) {
363 die("git update-index: bad quoting of path name");
365 path_name = uq.buf;
368 if (!verify_path(path_name)) {
369 fprintf(stderr, "Ignoring path %s\n", path_name);
370 continue;
373 if (!mode) {
374 /* mode == 0 means there is no such path -- remove */
375 if (remove_file_from_cache(path_name))
376 die("git update-index: unable to remove %s",
377 ptr);
379 else {
380 /* mode ' ' sha1 '\t' name
381 * ptr[-1] points at tab,
382 * ptr[-41] is at the beginning of sha1
384 ptr[-42] = ptr[-1] = 0;
385 if (add_cacheinfo(mode, sha1, path_name, stage))
386 die("git update-index: unable to update %s",
387 path_name);
389 continue;
391 bad_line:
392 die("malformed index info %s", buf.buf);
394 strbuf_release(&buf);
395 strbuf_release(&uq);
398 static const char * const update_index_usage[] = {
399 N_("git update-index [options] [--] [<file>...]"),
400 NULL
403 static unsigned char head_sha1[20];
404 static unsigned char merge_head_sha1[20];
406 static struct cache_entry *read_one_ent(const char *which,
407 unsigned char *ent, const char *path,
408 int namelen, int stage)
410 unsigned mode;
411 unsigned char sha1[20];
412 int size;
413 struct cache_entry *ce;
415 if (get_tree_entry(ent, path, sha1, &mode)) {
416 if (which)
417 error("%s: not in %s branch.", path, which);
418 return NULL;
420 if (mode == S_IFDIR) {
421 if (which)
422 error("%s: not a blob in %s branch.", path, which);
423 return NULL;
425 size = cache_entry_size(namelen);
426 ce = xcalloc(1, size);
428 hashcpy(ce->sha1, sha1);
429 memcpy(ce->name, path, namelen);
430 ce->ce_flags = create_ce_flags(stage);
431 ce->ce_namelen = namelen;
432 ce->ce_mode = create_ce_mode(mode);
433 return ce;
436 static int unresolve_one(const char *path)
438 int namelen = strlen(path);
439 int pos;
440 int ret = 0;
441 struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
443 /* See if there is such entry in the index. */
444 pos = cache_name_pos(path, namelen);
445 if (0 <= pos) {
446 /* already merged */
447 pos = unmerge_cache_entry_at(pos);
448 if (pos < active_nr) {
449 const struct cache_entry *ce = active_cache[pos];
450 if (ce_stage(ce) &&
451 ce_namelen(ce) == namelen &&
452 !memcmp(ce->name, path, namelen))
453 return 0;
455 /* no resolve-undo information; fall back */
456 } else {
457 /* If there isn't, either it is unmerged, or
458 * resolved as "removed" by mistake. We do not
459 * want to do anything in the former case.
461 pos = -pos-1;
462 if (pos < active_nr) {
463 const struct cache_entry *ce = active_cache[pos];
464 if (ce_namelen(ce) == namelen &&
465 !memcmp(ce->name, path, namelen)) {
466 fprintf(stderr,
467 "%s: skipping still unmerged path.\n",
468 path);
469 goto free_return;
474 /* Grab blobs from given path from HEAD and MERGE_HEAD,
475 * stuff HEAD version in stage #2,
476 * stuff MERGE_HEAD version in stage #3.
478 ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
479 ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);
481 if (!ce_2 || !ce_3) {
482 ret = -1;
483 goto free_return;
485 if (!hashcmp(ce_2->sha1, ce_3->sha1) &&
486 ce_2->ce_mode == ce_3->ce_mode) {
487 fprintf(stderr, "%s: identical in both, skipping.\n",
488 path);
489 goto free_return;
492 remove_file_from_cache(path);
493 if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
494 error("%s: cannot add our version to the index.", path);
495 ret = -1;
496 goto free_return;
498 if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
499 return 0;
500 error("%s: cannot add their version to the index.", path);
501 ret = -1;
502 free_return:
503 free(ce_2);
504 free(ce_3);
505 return ret;
508 static void read_head_pointers(void)
510 if (read_ref("HEAD", head_sha1))
511 die("No HEAD -- no initial commit yet?");
512 if (read_ref("MERGE_HEAD", merge_head_sha1)) {
513 fprintf(stderr, "Not in the middle of a merge.\n");
514 exit(0);
518 static int do_unresolve(int ac, const char **av,
519 const char *prefix, int prefix_length)
521 int i;
522 int err = 0;
524 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
525 * are not doing a merge, so exit with success status.
527 read_head_pointers();
529 for (i = 1; i < ac; i++) {
530 const char *arg = av[i];
531 const char *p = prefix_path(prefix, prefix_length, arg);
532 err |= unresolve_one(p);
533 if (p < arg || p > arg + strlen(arg))
534 free((char *)p);
536 return err;
539 static int do_reupdate(int ac, const char **av,
540 const char *prefix, int prefix_length)
542 /* Read HEAD and run update-index on paths that are
543 * merged and already different between index and HEAD.
545 int pos;
546 int has_head = 1;
547 struct pathspec pathspec;
549 parse_pathspec(&pathspec, 0,
550 PATHSPEC_PREFER_CWD,
551 prefix, av + 1);
553 if (read_ref("HEAD", head_sha1))
554 /* If there is no HEAD, that means it is an initial
555 * commit. Update everything in the index.
557 has_head = 0;
558 redo:
559 for (pos = 0; pos < active_nr; pos++) {
560 const struct cache_entry *ce = active_cache[pos];
561 struct cache_entry *old = NULL;
562 int save_nr;
563 char *path;
565 if (ce_stage(ce) || !ce_path_match(ce, &pathspec, NULL))
566 continue;
567 if (has_head)
568 old = read_one_ent(NULL, head_sha1,
569 ce->name, ce_namelen(ce), 0);
570 if (old && ce->ce_mode == old->ce_mode &&
571 !hashcmp(ce->sha1, old->sha1)) {
572 free(old);
573 continue; /* unchanged */
575 /* Be careful. The working tree may not have the
576 * path anymore, in which case, under 'allow_remove',
577 * or worse yet 'allow_replace', active_nr may decrease.
579 save_nr = active_nr;
580 path = xstrdup(ce->name);
581 update_one(path);
582 free(path);
583 if (save_nr != active_nr)
584 goto redo;
586 free_pathspec(&pathspec);
587 return 0;
590 struct refresh_params {
591 unsigned int flags;
592 int *has_errors;
595 static int refresh(struct refresh_params *o, unsigned int flag)
597 setup_work_tree();
598 read_cache_preload(NULL);
599 *o->has_errors |= refresh_cache(o->flags | flag);
600 return 0;
603 static int refresh_callback(const struct option *opt,
604 const char *arg, int unset)
606 return refresh(opt->value, 0);
609 static int really_refresh_callback(const struct option *opt,
610 const char *arg, int unset)
612 return refresh(opt->value, REFRESH_REALLY);
615 static int chmod_callback(const struct option *opt,
616 const char *arg, int unset)
618 char *flip = opt->value;
619 if ((arg[0] != '-' && arg[0] != '+') || arg[1] != 'x' || arg[2])
620 return error("option 'chmod' expects \"+x\" or \"-x\"");
621 *flip = arg[0];
622 return 0;
625 static int resolve_undo_clear_callback(const struct option *opt,
626 const char *arg, int unset)
628 resolve_undo_clear();
629 return 0;
632 static int parse_new_style_cacheinfo(const char *arg,
633 unsigned int *mode,
634 unsigned char sha1[],
635 const char **path)
637 unsigned long ul;
638 char *endp;
640 if (!arg)
641 return -1;
643 errno = 0;
644 ul = strtoul(arg, &endp, 8);
645 if (errno || endp == arg || *endp != ',' || (unsigned int) ul != ul)
646 return -1; /* not a new-style cacheinfo */
647 *mode = ul;
648 endp++;
649 if (get_sha1_hex(endp, sha1) || endp[40] != ',')
650 return -1;
651 *path = endp + 41;
652 return 0;
655 static int cacheinfo_callback(struct parse_opt_ctx_t *ctx,
656 const struct option *opt, int unset)
658 unsigned char sha1[20];
659 unsigned int mode;
660 const char *path;
662 if (!parse_new_style_cacheinfo(ctx->argv[1], &mode, sha1, &path)) {
663 if (add_cacheinfo(mode, sha1, path, 0))
664 die("git update-index: --cacheinfo cannot add %s", path);
665 ctx->argv++;
666 ctx->argc--;
667 return 0;
669 if (ctx->argc <= 3)
670 return error("option 'cacheinfo' expects <mode>,<sha1>,<path>");
671 if (strtoul_ui(*++ctx->argv, 8, &mode) ||
672 get_sha1_hex(*++ctx->argv, sha1) ||
673 add_cacheinfo(mode, sha1, *++ctx->argv, 0))
674 die("git update-index: --cacheinfo cannot add %s", *ctx->argv);
675 ctx->argc -= 3;
676 return 0;
679 static int stdin_cacheinfo_callback(struct parse_opt_ctx_t *ctx,
680 const struct option *opt, int unset)
682 int *line_termination = opt->value;
684 if (ctx->argc != 1)
685 return error("option '%s' must be the last argument", opt->long_name);
686 allow_add = allow_replace = allow_remove = 1;
687 read_index_info(*line_termination);
688 return 0;
691 static int stdin_callback(struct parse_opt_ctx_t *ctx,
692 const struct option *opt, int unset)
694 int *read_from_stdin = opt->value;
696 if (ctx->argc != 1)
697 return error("option '%s' must be the last argument", opt->long_name);
698 *read_from_stdin = 1;
699 return 0;
702 static int unresolve_callback(struct parse_opt_ctx_t *ctx,
703 const struct option *opt, int flags)
705 int *has_errors = opt->value;
706 const char *prefix = startup_info->prefix;
708 /* consume remaining arguments. */
709 *has_errors = do_unresolve(ctx->argc, ctx->argv,
710 prefix, prefix ? strlen(prefix) : 0);
711 if (*has_errors)
712 active_cache_changed = 0;
714 ctx->argv += ctx->argc - 1;
715 ctx->argc = 1;
716 return 0;
719 static int reupdate_callback(struct parse_opt_ctx_t *ctx,
720 const struct option *opt, int flags)
722 int *has_errors = opt->value;
723 const char *prefix = startup_info->prefix;
725 /* consume remaining arguments. */
726 setup_work_tree();
727 *has_errors = do_reupdate(ctx->argc, ctx->argv,
728 prefix, prefix ? strlen(prefix) : 0);
729 if (*has_errors)
730 active_cache_changed = 0;
732 ctx->argv += ctx->argc - 1;
733 ctx->argc = 1;
734 return 0;
737 int cmd_update_index(int argc, const char **argv, const char *prefix)
739 int newfd, entries, has_errors = 0, line_termination = '\n';
740 int read_from_stdin = 0;
741 int prefix_length = prefix ? strlen(prefix) : 0;
742 int preferred_index_format = 0;
743 char set_executable_bit = 0;
744 struct refresh_params refresh_args = {0, &has_errors};
745 int lock_error = 0;
746 struct lock_file *lock_file;
747 struct parse_opt_ctx_t ctx;
748 int parseopt_state = PARSE_OPT_UNKNOWN;
749 struct option options[] = {
750 OPT_BIT('q', NULL, &refresh_args.flags,
751 N_("continue refresh even when index needs update"),
752 REFRESH_QUIET),
753 OPT_BIT(0, "ignore-submodules", &refresh_args.flags,
754 N_("refresh: ignore submodules"),
755 REFRESH_IGNORE_SUBMODULES),
756 OPT_SET_INT(0, "add", &allow_add,
757 N_("do not ignore new files"), 1),
758 OPT_SET_INT(0, "replace", &allow_replace,
759 N_("let files replace directories and vice-versa"), 1),
760 OPT_SET_INT(0, "remove", &allow_remove,
761 N_("notice files missing from worktree"), 1),
762 OPT_BIT(0, "unmerged", &refresh_args.flags,
763 N_("refresh even if index contains unmerged entries"),
764 REFRESH_UNMERGED),
765 {OPTION_CALLBACK, 0, "refresh", &refresh_args, NULL,
766 N_("refresh stat information"),
767 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
768 refresh_callback},
769 {OPTION_CALLBACK, 0, "really-refresh", &refresh_args, NULL,
770 N_("like --refresh, but ignore assume-unchanged setting"),
771 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
772 really_refresh_callback},
773 {OPTION_LOWLEVEL_CALLBACK, 0, "cacheinfo", NULL,
774 N_("<mode>,<object>,<path>"),
775 N_("add the specified entry to the index"),
776 PARSE_OPT_NOARG | /* disallow --cacheinfo=<mode> form */
777 PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
778 (parse_opt_cb *) cacheinfo_callback},
779 {OPTION_CALLBACK, 0, "chmod", &set_executable_bit, N_("(+/-)x"),
780 N_("override the executable bit of the listed files"),
781 PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
782 chmod_callback},
783 {OPTION_SET_INT, 0, "assume-unchanged", &mark_valid_only, NULL,
784 N_("mark files as \"not changing\""),
785 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
786 {OPTION_SET_INT, 0, "no-assume-unchanged", &mark_valid_only, NULL,
787 N_("clear assumed-unchanged bit"),
788 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
789 {OPTION_SET_INT, 0, "skip-worktree", &mark_skip_worktree_only, NULL,
790 N_("mark files as \"index-only\""),
791 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
792 {OPTION_SET_INT, 0, "no-skip-worktree", &mark_skip_worktree_only, NULL,
793 N_("clear skip-worktree bit"),
794 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
795 OPT_SET_INT(0, "info-only", &info_only,
796 N_("add to index only; do not add content to object database"), 1),
797 OPT_SET_INT(0, "force-remove", &force_remove,
798 N_("remove named paths even if present in worktree"), 1),
799 OPT_SET_INT('z', NULL, &line_termination,
800 N_("with --stdin: input lines are terminated by null bytes"), '\0'),
801 {OPTION_LOWLEVEL_CALLBACK, 0, "stdin", &read_from_stdin, NULL,
802 N_("read list of paths to be updated from standard input"),
803 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
804 (parse_opt_cb *) stdin_callback},
805 {OPTION_LOWLEVEL_CALLBACK, 0, "index-info", &line_termination, NULL,
806 N_("add entries from standard input to the index"),
807 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
808 (parse_opt_cb *) stdin_cacheinfo_callback},
809 {OPTION_LOWLEVEL_CALLBACK, 0, "unresolve", &has_errors, NULL,
810 N_("repopulate stages #2 and #3 for the listed paths"),
811 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
812 (parse_opt_cb *) unresolve_callback},
813 {OPTION_LOWLEVEL_CALLBACK, 'g', "again", &has_errors, NULL,
814 N_("only update entries that differ from HEAD"),
815 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
816 (parse_opt_cb *) reupdate_callback},
817 OPT_BIT(0, "ignore-missing", &refresh_args.flags,
818 N_("ignore files missing from worktree"),
819 REFRESH_IGNORE_MISSING),
820 OPT_SET_INT(0, "verbose", &verbose,
821 N_("report actions to standard output"), 1),
822 {OPTION_CALLBACK, 0, "clear-resolve-undo", NULL, NULL,
823 N_("(for porcelains) forget saved unresolved conflicts"),
824 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
825 resolve_undo_clear_callback},
826 OPT_INTEGER(0, "index-version", &preferred_index_format,
827 N_("write index in this format")),
828 OPT_END()
831 if (argc == 2 && !strcmp(argv[1], "-h"))
832 usage_with_options(update_index_usage, options);
834 git_config(git_default_config, NULL);
836 /* We can't free this memory, it becomes part of a linked list parsed atexit() */
837 lock_file = xcalloc(1, sizeof(struct lock_file));
839 newfd = hold_locked_index(lock_file, 0);
840 if (newfd < 0)
841 lock_error = errno;
843 entries = read_cache();
844 if (entries < 0)
845 die("cache corrupted");
848 * Custom copy of parse_options() because we want to handle
849 * filename arguments as they come.
851 parse_options_start(&ctx, argc, argv, prefix,
852 options, PARSE_OPT_STOP_AT_NON_OPTION);
853 while (ctx.argc) {
854 if (parseopt_state != PARSE_OPT_DONE)
855 parseopt_state = parse_options_step(&ctx, options,
856 update_index_usage);
857 if (!ctx.argc)
858 break;
859 switch (parseopt_state) {
860 case PARSE_OPT_HELP:
861 exit(129);
862 case PARSE_OPT_NON_OPTION:
863 case PARSE_OPT_DONE:
865 const char *path = ctx.argv[0];
866 const char *p;
868 setup_work_tree();
869 p = prefix_path(prefix, prefix_length, path);
870 update_one(p);
871 if (set_executable_bit)
872 chmod_path(set_executable_bit, p);
873 free((char *)p);
874 ctx.argc--;
875 ctx.argv++;
876 break;
878 case PARSE_OPT_UNKNOWN:
879 if (ctx.argv[0][1] == '-')
880 error("unknown option '%s'", ctx.argv[0] + 2);
881 else
882 error("unknown switch '%c'", *ctx.opt);
883 usage_with_options(update_index_usage, options);
886 argc = parse_options_end(&ctx);
887 if (preferred_index_format) {
888 if (preferred_index_format < INDEX_FORMAT_LB ||
889 INDEX_FORMAT_UB < preferred_index_format)
890 die("index-version %d not in range: %d..%d",
891 preferred_index_format,
892 INDEX_FORMAT_LB, INDEX_FORMAT_UB);
894 if (the_index.version != preferred_index_format)
895 active_cache_changed = 1;
896 the_index.version = preferred_index_format;
899 if (read_from_stdin) {
900 struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
902 setup_work_tree();
903 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
904 const char *p;
905 if (line_termination && buf.buf[0] == '"') {
906 strbuf_reset(&nbuf);
907 if (unquote_c_style(&nbuf, buf.buf, NULL))
908 die("line is badly quoted");
909 strbuf_swap(&buf, &nbuf);
911 p = prefix_path(prefix, prefix_length, buf.buf);
912 update_one(p);
913 if (set_executable_bit)
914 chmod_path(set_executable_bit, p);
915 free((char *)p);
917 strbuf_release(&nbuf);
918 strbuf_release(&buf);
921 if (active_cache_changed) {
922 if (newfd < 0) {
923 if (refresh_args.flags & REFRESH_QUIET)
924 exit(128);
925 unable_to_lock_index_die(get_index_file(), lock_error);
927 if (write_cache(newfd, active_cache, active_nr) ||
928 commit_locked_index(lock_file))
929 die("Unable to write new index file");
932 rollback_lock_file(lock_file);
934 return has_errors ? 1 : 0;