2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
8 #include "cache-tree.h"
12 #include "resolve-undo.h"
13 #include "parse-options.h"
16 #include "split-index.h"
19 * Default to not allowing changes to the list of files. The
20 * tool doesn't actually care, but this makes it harder to add
21 * files to the revision control by mistake by doing something
22 * like "git update-index *" and suddenly having all the object
23 * files be revision controlled.
26 static int allow_remove;
27 static int allow_replace;
29 static int force_remove;
31 static int mark_valid_only;
32 static int mark_skip_worktree_only;
36 __attribute__((format (printf, 1, 2)))
37 static void report(const char *fmt, ...)
50 static int mark_ce_flags(const char *path, int flag, int mark)
52 int namelen = strlen(path);
53 int pos = cache_name_pos(path, namelen);
56 active_cache[pos]->ce_flags |= flag;
58 active_cache[pos]->ce_flags &= ~flag;
59 active_cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
60 cache_tree_invalidate_path(&the_index, path);
61 active_cache_changed |= CE_ENTRY_CHANGED;
67 static int remove_one_path(const char *path)
70 return error("%s: does not exist and --remove not passed", path);
71 if (remove_file_from_cache(path))
72 return error("%s: cannot remove from the index", path);
77 * Handle a path that couldn't be lstat'ed. It's either:
78 * - missing file (ENOENT or ENOTDIR). That's ok if we're
79 * supposed to be removing it and the removal actually
81 * - permission error. That's never ok.
83 static int process_lstat_error(const char *path, int err)
85 if (err == ENOENT || err == ENOTDIR)
86 return remove_one_path(path);
87 return error("lstat(\"%s\"): %s", path, strerror(errno));
90 static int add_one_path(const struct cache_entry *old, const char *path, int len, struct stat *st)
93 struct cache_entry *ce;
95 /* Was the old index entry already up-to-date? */
96 if (old && !ce_stage(old) && !ce_match_stat(old, st, 0))
99 size = cache_entry_size(len);
100 ce = xcalloc(1, size);
101 memcpy(ce->name, path, len);
102 ce->ce_flags = create_ce_flags(0);
103 ce->ce_namelen = len;
104 fill_stat_cache_info(ce, st);
105 ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
107 if (index_path(ce->sha1, path, st,
108 info_only ? 0 : HASH_WRITE_OBJECT)) {
112 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
113 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
114 if (add_cache_entry(ce, option))
115 return error("%s: cannot add to the index - missing --add option?", path);
120 * Handle a path that was a directory. Four cases:
122 * - it's already a gitlink in the index, and we keep it that
123 * way, and update it if we can (if we cannot find the HEAD,
124 * we're going to keep it unchanged in the index!)
126 * - it's a *file* in the index, in which case it should be
127 * removed as a file if removal is allowed, since it doesn't
128 * exist as such any more. If removal isn't allowed, it's
131 * (NOTE! This is old and arguably fairly strange behaviour.
132 * We might want to make this an error unconditionally, and
133 * use "--force-remove" if you actually want to force removal).
135 * - it used to exist as a subdirectory (ie multiple files with
136 * this particular prefix) in the index, in which case it's wrong
137 * to try to update it as a directory.
139 * - it doesn't exist at all in the index, but it is a valid
140 * git directory, and it should be *added* as a gitlink.
142 static int process_directory(const char *path, int len, struct stat *st)
144 unsigned char sha1[20];
145 int pos = cache_name_pos(path, len);
147 /* Exact match: file or existing gitlink */
149 const struct cache_entry *ce = active_cache[pos];
150 if (S_ISGITLINK(ce->ce_mode)) {
152 /* Do nothing to the index if there is no HEAD! */
153 if (resolve_gitlink_ref(path, "HEAD", sha1) < 0)
156 return add_one_path(ce, path, len, st);
158 /* Should this be an unconditional error? */
159 return remove_one_path(path);
162 /* Inexact match: is there perhaps a subdirectory match? */
164 while (pos < active_nr) {
165 const struct cache_entry *ce = active_cache[pos++];
167 if (strncmp(ce->name, path, len))
169 if (ce->name[len] > '/')
171 if (ce->name[len] < '/')
174 /* Subdirectory match - error out */
175 return error("%s: is a directory - add individual files instead", path);
178 /* No match - should we add it as a gitlink? */
179 if (!resolve_gitlink_ref(path, "HEAD", sha1))
180 return add_one_path(NULL, path, len, st);
183 return error("%s: is a directory - add files inside instead", path);
186 static int process_path(const char *path)
190 const struct cache_entry *ce;
193 if (has_symlink_leading_path(path, len))
194 return error("'%s' is beyond a symbolic link", path);
196 pos = cache_name_pos(path, len);
197 ce = pos < 0 ? NULL : active_cache[pos];
198 if (ce && ce_skip_worktree(ce)) {
200 * working directory version is assumed "good"
201 * so updating it does not make sense.
202 * On the other hand, removing it from index should work
204 if (allow_remove && remove_file_from_cache(path))
205 return error("%s: cannot remove from the index", path);
210 * First things first: get the stat information, to decide
211 * what to do about the pathname!
213 if (lstat(path, &st) < 0)
214 return process_lstat_error(path, errno);
216 if (S_ISDIR(st.st_mode))
217 return process_directory(path, len, &st);
219 return add_one_path(ce, path, len, &st);
222 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
223 const char *path, int stage)
225 int size, len, option;
226 struct cache_entry *ce;
228 if (!verify_path(path))
229 return error("Invalid path '%s'", path);
232 size = cache_entry_size(len);
233 ce = xcalloc(1, size);
235 hashcpy(ce->sha1, sha1);
236 memcpy(ce->name, path, len);
237 ce->ce_flags = create_ce_flags(stage);
238 ce->ce_namelen = len;
239 ce->ce_mode = create_ce_mode(mode);
240 if (assume_unchanged)
241 ce->ce_flags |= CE_VALID;
242 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
243 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
244 if (add_cache_entry(ce, option))
245 return error("%s: cannot add to the index - missing --add option?",
247 report("add '%s'", path);
251 static void chmod_path(int flip, const char *path)
254 struct cache_entry *ce;
257 pos = cache_name_pos(path, strlen(path));
260 ce = active_cache[pos];
266 ce->ce_mode |= 0111; break;
268 ce->ce_mode &= ~0111; break;
272 cache_tree_invalidate_path(&the_index, path);
273 ce->ce_flags |= CE_UPDATE_IN_BASE;
274 active_cache_changed |= CE_ENTRY_CHANGED;
275 report("chmod %cx '%s'", flip, path);
278 die("git update-index: cannot chmod %cx '%s'", flip, path);
281 static void update_one(const char *path)
283 if (!verify_path(path)) {
284 fprintf(stderr, "Ignoring path %s\n", path);
287 if (mark_valid_only) {
288 if (mark_ce_flags(path, CE_VALID, mark_valid_only == MARK_FLAG))
289 die("Unable to mark file %s", path);
292 if (mark_skip_worktree_only) {
293 if (mark_ce_flags(path, CE_SKIP_WORKTREE, mark_skip_worktree_only == MARK_FLAG))
294 die("Unable to mark file %s", path);
299 if (remove_file_from_cache(path))
300 die("git update-index: unable to remove %s", path);
301 report("remove '%s'", path);
304 if (process_path(path))
305 die("Unable to process path %s", path);
306 report("add '%s'", path);
309 static void read_index_info(int line_termination)
311 struct strbuf buf = STRBUF_INIT;
312 struct strbuf uq = STRBUF_INIT;
314 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
317 unsigned char sha1[20];
322 /* This reads lines formatted in one of three formats:
324 * (1) mode SP sha1 TAB path
325 * The first format is what "git apply --index-info"
326 * reports, and used to reconstruct a partial tree
327 * that is used for phony merge base tree when falling
328 * back on 3-way merge.
330 * (2) mode SP type SP sha1 TAB path
331 * The second format is to stuff "git ls-tree" output
332 * into the index file.
334 * (3) mode SP sha1 SP stage TAB path
335 * This format is to put higher order stages into the
336 * index file and matches "git ls-files --stage" output.
339 ul = strtoul(buf.buf, &ptr, 8);
340 if (ptr == buf.buf || *ptr != ' '
341 || errno || (unsigned int) ul != ul)
345 tab = strchr(ptr, '\t');
346 if (!tab || tab - ptr < 41)
349 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
350 stage = tab[-1] - '0';
351 ptr = tab + 1; /* point at the head of path */
352 tab = tab - 2; /* point at tail of sha1 */
356 ptr = tab + 1; /* point at the head of path */
359 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
363 if (line_termination && path_name[0] == '"') {
365 if (unquote_c_style(&uq, path_name, NULL)) {
366 die("git update-index: bad quoting of path name");
371 if (!verify_path(path_name)) {
372 fprintf(stderr, "Ignoring path %s\n", path_name);
377 /* mode == 0 means there is no such path -- remove */
378 if (remove_file_from_cache(path_name))
379 die("git update-index: unable to remove %s",
383 /* mode ' ' sha1 '\t' name
384 * ptr[-1] points at tab,
385 * ptr[-41] is at the beginning of sha1
387 ptr[-42] = ptr[-1] = 0;
388 if (add_cacheinfo(mode, sha1, path_name, stage))
389 die("git update-index: unable to update %s",
395 die("malformed index info %s", buf.buf);
397 strbuf_release(&buf);
401 static const char * const update_index_usage[] = {
402 N_("git update-index [options] [--] [<file>...]"),
406 static unsigned char head_sha1[20];
407 static unsigned char merge_head_sha1[20];
409 static struct cache_entry *read_one_ent(const char *which,
410 unsigned char *ent, const char *path,
411 int namelen, int stage)
414 unsigned char sha1[20];
416 struct cache_entry *ce;
418 if (get_tree_entry(ent, path, sha1, &mode)) {
420 error("%s: not in %s branch.", path, which);
423 if (mode == S_IFDIR) {
425 error("%s: not a blob in %s branch.", path, which);
428 size = cache_entry_size(namelen);
429 ce = xcalloc(1, size);
431 hashcpy(ce->sha1, sha1);
432 memcpy(ce->name, path, namelen);
433 ce->ce_flags = create_ce_flags(stage);
434 ce->ce_namelen = namelen;
435 ce->ce_mode = create_ce_mode(mode);
439 static int unresolve_one(const char *path)
441 int namelen = strlen(path);
444 struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
446 /* See if there is such entry in the index. */
447 pos = cache_name_pos(path, namelen);
450 pos = unmerge_cache_entry_at(pos);
451 if (pos < active_nr) {
452 const struct cache_entry *ce = active_cache[pos];
454 ce_namelen(ce) == namelen &&
455 !memcmp(ce->name, path, namelen))
458 /* no resolve-undo information; fall back */
460 /* If there isn't, either it is unmerged, or
461 * resolved as "removed" by mistake. We do not
462 * want to do anything in the former case.
465 if (pos < active_nr) {
466 const struct cache_entry *ce = active_cache[pos];
467 if (ce_namelen(ce) == namelen &&
468 !memcmp(ce->name, path, namelen)) {
470 "%s: skipping still unmerged path.\n",
477 /* Grab blobs from given path from HEAD and MERGE_HEAD,
478 * stuff HEAD version in stage #2,
479 * stuff MERGE_HEAD version in stage #3.
481 ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
482 ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);
484 if (!ce_2 || !ce_3) {
488 if (!hashcmp(ce_2->sha1, ce_3->sha1) &&
489 ce_2->ce_mode == ce_3->ce_mode) {
490 fprintf(stderr, "%s: identical in both, skipping.\n",
495 remove_file_from_cache(path);
496 if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
497 error("%s: cannot add our version to the index.", path);
501 if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
503 error("%s: cannot add their version to the index.", path);
511 static void read_head_pointers(void)
513 if (read_ref("HEAD", head_sha1))
514 die("No HEAD -- no initial commit yet?");
515 if (read_ref("MERGE_HEAD", merge_head_sha1)) {
516 fprintf(stderr, "Not in the middle of a merge.\n");
521 static int do_unresolve(int ac, const char **av,
522 const char *prefix, int prefix_length)
527 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
528 * are not doing a merge, so exit with success status.
530 read_head_pointers();
532 for (i = 1; i < ac; i++) {
533 const char *arg = av[i];
534 const char *p = prefix_path(prefix, prefix_length, arg);
535 err |= unresolve_one(p);
536 if (p < arg || p > arg + strlen(arg))
542 static int do_reupdate(int ac, const char **av,
543 const char *prefix, int prefix_length)
545 /* Read HEAD and run update-index on paths that are
546 * merged and already different between index and HEAD.
550 struct pathspec pathspec;
552 parse_pathspec(&pathspec, 0,
556 if (read_ref("HEAD", head_sha1))
557 /* If there is no HEAD, that means it is an initial
558 * commit. Update everything in the index.
562 for (pos = 0; pos < active_nr; pos++) {
563 const struct cache_entry *ce = active_cache[pos];
564 struct cache_entry *old = NULL;
568 if (ce_stage(ce) || !ce_path_match(ce, &pathspec, NULL))
571 old = read_one_ent(NULL, head_sha1,
572 ce->name, ce_namelen(ce), 0);
573 if (old && ce->ce_mode == old->ce_mode &&
574 !hashcmp(ce->sha1, old->sha1)) {
576 continue; /* unchanged */
578 /* Be careful. The working tree may not have the
579 * path anymore, in which case, under 'allow_remove',
580 * or worse yet 'allow_replace', active_nr may decrease.
583 path = xstrdup(ce->name);
586 if (save_nr != active_nr)
589 free_pathspec(&pathspec);
593 struct refresh_params {
598 static int refresh(struct refresh_params *o, unsigned int flag)
601 read_cache_preload(NULL);
602 *o->has_errors |= refresh_cache(o->flags | flag);
606 static int refresh_callback(const struct option *opt,
607 const char *arg, int unset)
609 return refresh(opt->value, 0);
612 static int really_refresh_callback(const struct option *opt,
613 const char *arg, int unset)
615 return refresh(opt->value, REFRESH_REALLY);
618 static int chmod_callback(const struct option *opt,
619 const char *arg, int unset)
621 char *flip = opt->value;
622 if ((arg[0] != '-' && arg[0] != '+') || arg[1] != 'x' || arg[2])
623 return error("option 'chmod' expects \"+x\" or \"-x\"");
628 static int resolve_undo_clear_callback(const struct option *opt,
629 const char *arg, int unset)
631 resolve_undo_clear();
635 static int parse_new_style_cacheinfo(const char *arg,
637 unsigned char sha1[],
647 ul = strtoul(arg, &endp, 8);
648 if (errno || endp == arg || *endp != ',' || (unsigned int) ul != ul)
649 return -1; /* not a new-style cacheinfo */
652 if (get_sha1_hex(endp, sha1) || endp[40] != ',')
658 static int cacheinfo_callback(struct parse_opt_ctx_t *ctx,
659 const struct option *opt, int unset)
661 unsigned char sha1[20];
665 if (!parse_new_style_cacheinfo(ctx->argv[1], &mode, sha1, &path)) {
666 if (add_cacheinfo(mode, sha1, path, 0))
667 die("git update-index: --cacheinfo cannot add %s", path);
673 return error("option 'cacheinfo' expects <mode>,<sha1>,<path>");
674 if (strtoul_ui(*++ctx->argv, 8, &mode) ||
675 get_sha1_hex(*++ctx->argv, sha1) ||
676 add_cacheinfo(mode, sha1, *++ctx->argv, 0))
677 die("git update-index: --cacheinfo cannot add %s", *ctx->argv);
682 static int stdin_cacheinfo_callback(struct parse_opt_ctx_t *ctx,
683 const struct option *opt, int unset)
685 int *line_termination = opt->value;
688 return error("option '%s' must be the last argument", opt->long_name);
689 allow_add = allow_replace = allow_remove = 1;
690 read_index_info(*line_termination);
694 static int stdin_callback(struct parse_opt_ctx_t *ctx,
695 const struct option *opt, int unset)
697 int *read_from_stdin = opt->value;
700 return error("option '%s' must be the last argument", opt->long_name);
701 *read_from_stdin = 1;
705 static int unresolve_callback(struct parse_opt_ctx_t *ctx,
706 const struct option *opt, int flags)
708 int *has_errors = opt->value;
709 const char *prefix = startup_info->prefix;
711 /* consume remaining arguments. */
712 *has_errors = do_unresolve(ctx->argc, ctx->argv,
713 prefix, prefix ? strlen(prefix) : 0);
715 active_cache_changed = 0;
717 ctx->argv += ctx->argc - 1;
722 static int reupdate_callback(struct parse_opt_ctx_t *ctx,
723 const struct option *opt, int flags)
725 int *has_errors = opt->value;
726 const char *prefix = startup_info->prefix;
728 /* consume remaining arguments. */
730 *has_errors = do_reupdate(ctx->argc, ctx->argv,
731 prefix, prefix ? strlen(prefix) : 0);
733 active_cache_changed = 0;
735 ctx->argv += ctx->argc - 1;
740 int cmd_update_index(int argc, const char **argv, const char *prefix)
742 int newfd, entries, has_errors = 0, line_termination = '\n';
743 int read_from_stdin = 0;
744 int prefix_length = prefix ? strlen(prefix) : 0;
745 int preferred_index_format = 0;
746 char set_executable_bit = 0;
747 struct refresh_params refresh_args = {0, &has_errors};
749 int split_index = -1;
750 struct lock_file *lock_file;
751 struct parse_opt_ctx_t ctx;
752 int parseopt_state = PARSE_OPT_UNKNOWN;
753 struct option options[] = {
754 OPT_BIT('q', NULL, &refresh_args.flags,
755 N_("continue refresh even when index needs update"),
757 OPT_BIT(0, "ignore-submodules", &refresh_args.flags,
758 N_("refresh: ignore submodules"),
759 REFRESH_IGNORE_SUBMODULES),
760 OPT_SET_INT(0, "add", &allow_add,
761 N_("do not ignore new files"), 1),
762 OPT_SET_INT(0, "replace", &allow_replace,
763 N_("let files replace directories and vice-versa"), 1),
764 OPT_SET_INT(0, "remove", &allow_remove,
765 N_("notice files missing from worktree"), 1),
766 OPT_BIT(0, "unmerged", &refresh_args.flags,
767 N_("refresh even if index contains unmerged entries"),
769 {OPTION_CALLBACK, 0, "refresh", &refresh_args, NULL,
770 N_("refresh stat information"),
771 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
773 {OPTION_CALLBACK, 0, "really-refresh", &refresh_args, NULL,
774 N_("like --refresh, but ignore assume-unchanged setting"),
775 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
776 really_refresh_callback},
777 {OPTION_LOWLEVEL_CALLBACK, 0, "cacheinfo", NULL,
778 N_("<mode>,<object>,<path>"),
779 N_("add the specified entry to the index"),
780 PARSE_OPT_NOARG | /* disallow --cacheinfo=<mode> form */
781 PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
782 (parse_opt_cb *) cacheinfo_callback},
783 {OPTION_CALLBACK, 0, "chmod", &set_executable_bit, N_("(+/-)x"),
784 N_("override the executable bit of the listed files"),
785 PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
787 {OPTION_SET_INT, 0, "assume-unchanged", &mark_valid_only, NULL,
788 N_("mark files as \"not changing\""),
789 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
790 {OPTION_SET_INT, 0, "no-assume-unchanged", &mark_valid_only, NULL,
791 N_("clear assumed-unchanged bit"),
792 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
793 {OPTION_SET_INT, 0, "skip-worktree", &mark_skip_worktree_only, NULL,
794 N_("mark files as \"index-only\""),
795 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
796 {OPTION_SET_INT, 0, "no-skip-worktree", &mark_skip_worktree_only, NULL,
797 N_("clear skip-worktree bit"),
798 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
799 OPT_SET_INT(0, "info-only", &info_only,
800 N_("add to index only; do not add content to object database"), 1),
801 OPT_SET_INT(0, "force-remove", &force_remove,
802 N_("remove named paths even if present in worktree"), 1),
803 OPT_SET_INT('z', NULL, &line_termination,
804 N_("with --stdin: input lines are terminated by null bytes"), '\0'),
805 {OPTION_LOWLEVEL_CALLBACK, 0, "stdin", &read_from_stdin, NULL,
806 N_("read list of paths to be updated from standard input"),
807 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
808 (parse_opt_cb *) stdin_callback},
809 {OPTION_LOWLEVEL_CALLBACK, 0, "index-info", &line_termination, NULL,
810 N_("add entries from standard input to the index"),
811 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
812 (parse_opt_cb *) stdin_cacheinfo_callback},
813 {OPTION_LOWLEVEL_CALLBACK, 0, "unresolve", &has_errors, NULL,
814 N_("repopulate stages #2 and #3 for the listed paths"),
815 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
816 (parse_opt_cb *) unresolve_callback},
817 {OPTION_LOWLEVEL_CALLBACK, 'g', "again", &has_errors, NULL,
818 N_("only update entries that differ from HEAD"),
819 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
820 (parse_opt_cb *) reupdate_callback},
821 OPT_BIT(0, "ignore-missing", &refresh_args.flags,
822 N_("ignore files missing from worktree"),
823 REFRESH_IGNORE_MISSING),
824 OPT_SET_INT(0, "verbose", &verbose,
825 N_("report actions to standard output"), 1),
826 {OPTION_CALLBACK, 0, "clear-resolve-undo", NULL, NULL,
827 N_("(for porcelains) forget saved unresolved conflicts"),
828 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
829 resolve_undo_clear_callback},
830 OPT_INTEGER(0, "index-version", &preferred_index_format,
831 N_("write index in this format")),
832 OPT_BOOL(0, "split-index", &split_index,
833 N_("enable or disable split index")),
837 if (argc == 2 && !strcmp(argv[1], "-h"))
838 usage_with_options(update_index_usage, options);
840 git_config(git_default_config, NULL);
842 /* We can't free this memory, it becomes part of a linked list parsed atexit() */
843 lock_file = xcalloc(1, sizeof(struct lock_file));
845 newfd = hold_locked_index(lock_file, 0);
849 entries = read_cache();
851 die("cache corrupted");
854 * Custom copy of parse_options() because we want to handle
855 * filename arguments as they come.
857 parse_options_start(&ctx, argc, argv, prefix,
858 options, PARSE_OPT_STOP_AT_NON_OPTION);
860 if (parseopt_state != PARSE_OPT_DONE)
861 parseopt_state = parse_options_step(&ctx, options,
865 switch (parseopt_state) {
868 case PARSE_OPT_NON_OPTION:
871 const char *path = ctx.argv[0];
875 p = prefix_path(prefix, prefix_length, path);
877 if (set_executable_bit)
878 chmod_path(set_executable_bit, p);
884 case PARSE_OPT_UNKNOWN:
885 if (ctx.argv[0][1] == '-')
886 error("unknown option '%s'", ctx.argv[0] + 2);
888 error("unknown switch '%c'", *ctx.opt);
889 usage_with_options(update_index_usage, options);
892 argc = parse_options_end(&ctx);
893 if (preferred_index_format) {
894 if (preferred_index_format < INDEX_FORMAT_LB ||
895 INDEX_FORMAT_UB < preferred_index_format)
896 die("index-version %d not in range: %d..%d",
897 preferred_index_format,
898 INDEX_FORMAT_LB, INDEX_FORMAT_UB);
900 if (the_index.version != preferred_index_format)
901 active_cache_changed |= SOMETHING_CHANGED;
902 the_index.version = preferred_index_format;
905 if (read_from_stdin) {
906 struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
909 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
911 if (line_termination && buf.buf[0] == '"') {
913 if (unquote_c_style(&nbuf, buf.buf, NULL))
914 die("line is badly quoted");
915 strbuf_swap(&buf, &nbuf);
917 p = prefix_path(prefix, prefix_length, buf.buf);
919 if (set_executable_bit)
920 chmod_path(set_executable_bit, p);
923 strbuf_release(&nbuf);
924 strbuf_release(&buf);
927 if (split_index > 0) {
928 init_split_index(&the_index);
929 the_index.cache_changed |= SPLIT_INDEX_ORDERED;
930 } else if (!split_index && the_index.split_index) {
932 * can't discard_split_index(&the_index); because that
933 * will destroy split_index->base->cache[], which may
934 * be shared with the_index.cache[]. So yeah we're
935 * leaking a bit here.
937 the_index.split_index = NULL;
938 the_index.cache_changed |= SOMETHING_CHANGED;
941 if (active_cache_changed) {
943 if (refresh_args.flags & REFRESH_QUIET)
945 unable_to_lock_index_die(get_index_file(), lock_error);
947 if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
948 die("Unable to write new index file");
951 rollback_lock_file(lock_file);
953 return has_errors ? 1 : 0;