git-checkout.txt: document "git checkout <pathspec>" better
[git/git-svn.git] / builtin / update-index.c
blob6271b54adc24c8765ca0b179faf53f6af86dc630
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "lockfile.h"
8 #include "quote.h"
9 #include "cache-tree.h"
10 #include "tree-walk.h"
11 #include "builtin.h"
12 #include "refs.h"
13 #include "resolve-undo.h"
14 #include "parse-options.h"
15 #include "pathspec.h"
16 #include "dir.h"
17 #include "split-index.h"
20 * Default to not allowing changes to the list of files. The
21 * tool doesn't actually care, but this makes it harder to add
22 * files to the revision control by mistake by doing something
23 * like "git update-index *" and suddenly having all the object
24 * files be revision controlled.
26 static int allow_add;
27 static int allow_remove;
28 static int allow_replace;
29 static int info_only;
30 static int force_remove;
31 static int verbose;
32 static int mark_valid_only;
33 static int mark_skip_worktree_only;
34 #define MARK_FLAG 1
35 #define UNMARK_FLAG 2
37 __attribute__((format (printf, 1, 2)))
38 static void report(const char *fmt, ...)
40 va_list vp;
42 if (!verbose)
43 return;
45 va_start(vp, fmt);
46 vprintf(fmt, vp);
47 putchar('\n');
48 va_end(vp);
51 static int mark_ce_flags(const char *path, int flag, int mark)
53 int namelen = strlen(path);
54 int pos = cache_name_pos(path, namelen);
55 if (0 <= pos) {
56 if (mark)
57 active_cache[pos]->ce_flags |= flag;
58 else
59 active_cache[pos]->ce_flags &= ~flag;
60 active_cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
61 cache_tree_invalidate_path(&the_index, path);
62 active_cache_changed |= CE_ENTRY_CHANGED;
63 return 0;
65 return -1;
68 static int remove_one_path(const char *path)
70 if (!allow_remove)
71 return error("%s: does not exist and --remove not passed", path);
72 if (remove_file_from_cache(path))
73 return error("%s: cannot remove from the index", path);
74 return 0;
78 * Handle a path that couldn't be lstat'ed. It's either:
79 * - missing file (ENOENT or ENOTDIR). That's ok if we're
80 * supposed to be removing it and the removal actually
81 * succeeds.
82 * - permission error. That's never ok.
84 static int process_lstat_error(const char *path, int err)
86 if (err == ENOENT || err == ENOTDIR)
87 return remove_one_path(path);
88 return error("lstat(\"%s\"): %s", path, strerror(errno));
91 static int add_one_path(const struct cache_entry *old, const char *path, int len, struct stat *st)
93 int option, size;
94 struct cache_entry *ce;
96 /* Was the old index entry already up-to-date? */
97 if (old && !ce_stage(old) && !ce_match_stat(old, st, 0))
98 return 0;
100 size = cache_entry_size(len);
101 ce = xcalloc(1, size);
102 memcpy(ce->name, path, len);
103 ce->ce_flags = create_ce_flags(0);
104 ce->ce_namelen = len;
105 fill_stat_cache_info(ce, st);
106 ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
108 if (index_path(ce->sha1, path, st,
109 info_only ? 0 : HASH_WRITE_OBJECT)) {
110 free(ce);
111 return -1;
113 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
114 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
115 if (add_cache_entry(ce, option))
116 return error("%s: cannot add to the index - missing --add option?", path);
117 return 0;
121 * Handle a path that was a directory. Four cases:
123 * - it's already a gitlink in the index, and we keep it that
124 * way, and update it if we can (if we cannot find the HEAD,
125 * we're going to keep it unchanged in the index!)
127 * - it's a *file* in the index, in which case it should be
128 * removed as a file if removal is allowed, since it doesn't
129 * exist as such any more. If removal isn't allowed, it's
130 * an error.
132 * (NOTE! This is old and arguably fairly strange behaviour.
133 * We might want to make this an error unconditionally, and
134 * use "--force-remove" if you actually want to force removal).
136 * - it used to exist as a subdirectory (ie multiple files with
137 * this particular prefix) in the index, in which case it's wrong
138 * to try to update it as a directory.
140 * - it doesn't exist at all in the index, but it is a valid
141 * git directory, and it should be *added* as a gitlink.
143 static int process_directory(const char *path, int len, struct stat *st)
145 unsigned char sha1[20];
146 int pos = cache_name_pos(path, len);
148 /* Exact match: file or existing gitlink */
149 if (pos >= 0) {
150 const struct cache_entry *ce = active_cache[pos];
151 if (S_ISGITLINK(ce->ce_mode)) {
153 /* Do nothing to the index if there is no HEAD! */
154 if (resolve_gitlink_ref(path, "HEAD", sha1) < 0)
155 return 0;
157 return add_one_path(ce, path, len, st);
159 /* Should this be an unconditional error? */
160 return remove_one_path(path);
163 /* Inexact match: is there perhaps a subdirectory match? */
164 pos = -pos-1;
165 while (pos < active_nr) {
166 const struct cache_entry *ce = active_cache[pos++];
168 if (strncmp(ce->name, path, len))
169 break;
170 if (ce->name[len] > '/')
171 break;
172 if (ce->name[len] < '/')
173 continue;
175 /* Subdirectory match - error out */
176 return error("%s: is a directory - add individual files instead", path);
179 /* No match - should we add it as a gitlink? */
180 if (!resolve_gitlink_ref(path, "HEAD", sha1))
181 return add_one_path(NULL, path, len, st);
183 /* Error out. */
184 return error("%s: is a directory - add files inside instead", path);
187 static int process_path(const char *path)
189 int pos, len;
190 struct stat st;
191 const struct cache_entry *ce;
193 len = strlen(path);
194 if (has_symlink_leading_path(path, len))
195 return error("'%s' is beyond a symbolic link", path);
197 pos = cache_name_pos(path, len);
198 ce = pos < 0 ? NULL : active_cache[pos];
199 if (ce && ce_skip_worktree(ce)) {
201 * working directory version is assumed "good"
202 * so updating it does not make sense.
203 * On the other hand, removing it from index should work
205 if (allow_remove && remove_file_from_cache(path))
206 return error("%s: cannot remove from the index", path);
207 return 0;
211 * First things first: get the stat information, to decide
212 * what to do about the pathname!
214 if (lstat(path, &st) < 0)
215 return process_lstat_error(path, errno);
217 if (S_ISDIR(st.st_mode))
218 return process_directory(path, len, &st);
220 return add_one_path(ce, path, len, &st);
223 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
224 const char *path, int stage)
226 int size, len, option;
227 struct cache_entry *ce;
229 if (!verify_path(path))
230 return error("Invalid path '%s'", path);
232 len = strlen(path);
233 size = cache_entry_size(len);
234 ce = xcalloc(1, size);
236 hashcpy(ce->sha1, sha1);
237 memcpy(ce->name, path, len);
238 ce->ce_flags = create_ce_flags(stage);
239 ce->ce_namelen = len;
240 ce->ce_mode = create_ce_mode(mode);
241 if (assume_unchanged)
242 ce->ce_flags |= CE_VALID;
243 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
244 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
245 if (add_cache_entry(ce, option))
246 return error("%s: cannot add to the index - missing --add option?",
247 path);
248 report("add '%s'", path);
249 return 0;
252 static void chmod_path(int flip, const char *path)
254 int pos;
255 struct cache_entry *ce;
256 unsigned int mode;
258 pos = cache_name_pos(path, strlen(path));
259 if (pos < 0)
260 goto fail;
261 ce = active_cache[pos];
262 mode = ce->ce_mode;
263 if (!S_ISREG(mode))
264 goto fail;
265 switch (flip) {
266 case '+':
267 ce->ce_mode |= 0111; break;
268 case '-':
269 ce->ce_mode &= ~0111; break;
270 default:
271 goto fail;
273 cache_tree_invalidate_path(&the_index, path);
274 ce->ce_flags |= CE_UPDATE_IN_BASE;
275 active_cache_changed |= CE_ENTRY_CHANGED;
276 report("chmod %cx '%s'", flip, path);
277 return;
278 fail:
279 die("git update-index: cannot chmod %cx '%s'", flip, path);
282 static void update_one(const char *path)
284 if (!verify_path(path)) {
285 fprintf(stderr, "Ignoring path %s\n", path);
286 return;
288 if (mark_valid_only) {
289 if (mark_ce_flags(path, CE_VALID, mark_valid_only == MARK_FLAG))
290 die("Unable to mark file %s", path);
291 return;
293 if (mark_skip_worktree_only) {
294 if (mark_ce_flags(path, CE_SKIP_WORKTREE, mark_skip_worktree_only == MARK_FLAG))
295 die("Unable to mark file %s", path);
296 return;
299 if (force_remove) {
300 if (remove_file_from_cache(path))
301 die("git update-index: unable to remove %s", path);
302 report("remove '%s'", path);
303 return;
305 if (process_path(path))
306 die("Unable to process path %s", path);
307 report("add '%s'", path);
310 static void read_index_info(int line_termination)
312 struct strbuf buf = STRBUF_INIT;
313 struct strbuf uq = STRBUF_INIT;
315 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
316 char *ptr, *tab;
317 char *path_name;
318 unsigned char sha1[20];
319 unsigned int mode;
320 unsigned long ul;
321 int stage;
323 /* This reads lines formatted in one of three formats:
325 * (1) mode SP sha1 TAB path
326 * The first format is what "git apply --index-info"
327 * reports, and used to reconstruct a partial tree
328 * that is used for phony merge base tree when falling
329 * back on 3-way merge.
331 * (2) mode SP type SP sha1 TAB path
332 * The second format is to stuff "git ls-tree" output
333 * into the index file.
335 * (3) mode SP sha1 SP stage TAB path
336 * This format is to put higher order stages into the
337 * index file and matches "git ls-files --stage" output.
339 errno = 0;
340 ul = strtoul(buf.buf, &ptr, 8);
341 if (ptr == buf.buf || *ptr != ' '
342 || errno || (unsigned int) ul != ul)
343 goto bad_line;
344 mode = ul;
346 tab = strchr(ptr, '\t');
347 if (!tab || tab - ptr < 41)
348 goto bad_line;
350 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
351 stage = tab[-1] - '0';
352 ptr = tab + 1; /* point at the head of path */
353 tab = tab - 2; /* point at tail of sha1 */
355 else {
356 stage = 0;
357 ptr = tab + 1; /* point at the head of path */
360 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
361 goto bad_line;
363 path_name = ptr;
364 if (line_termination && path_name[0] == '"') {
365 strbuf_reset(&uq);
366 if (unquote_c_style(&uq, path_name, NULL)) {
367 die("git update-index: bad quoting of path name");
369 path_name = uq.buf;
372 if (!verify_path(path_name)) {
373 fprintf(stderr, "Ignoring path %s\n", path_name);
374 continue;
377 if (!mode) {
378 /* mode == 0 means there is no such path -- remove */
379 if (remove_file_from_cache(path_name))
380 die("git update-index: unable to remove %s",
381 ptr);
383 else {
384 /* mode ' ' sha1 '\t' name
385 * ptr[-1] points at tab,
386 * ptr[-41] is at the beginning of sha1
388 ptr[-42] = ptr[-1] = 0;
389 if (add_cacheinfo(mode, sha1, path_name, stage))
390 die("git update-index: unable to update %s",
391 path_name);
393 continue;
395 bad_line:
396 die("malformed index info %s", buf.buf);
398 strbuf_release(&buf);
399 strbuf_release(&uq);
402 static const char * const update_index_usage[] = {
403 N_("git update-index [<options>] [--] [<file>...]"),
404 NULL
407 static unsigned char head_sha1[20];
408 static unsigned char merge_head_sha1[20];
410 static struct cache_entry *read_one_ent(const char *which,
411 unsigned char *ent, const char *path,
412 int namelen, int stage)
414 unsigned mode;
415 unsigned char sha1[20];
416 int size;
417 struct cache_entry *ce;
419 if (get_tree_entry(ent, path, sha1, &mode)) {
420 if (which)
421 error("%s: not in %s branch.", path, which);
422 return NULL;
424 if (mode == S_IFDIR) {
425 if (which)
426 error("%s: not a blob in %s branch.", path, which);
427 return NULL;
429 size = cache_entry_size(namelen);
430 ce = xcalloc(1, size);
432 hashcpy(ce->sha1, sha1);
433 memcpy(ce->name, path, namelen);
434 ce->ce_flags = create_ce_flags(stage);
435 ce->ce_namelen = namelen;
436 ce->ce_mode = create_ce_mode(mode);
437 return ce;
440 static int unresolve_one(const char *path)
442 int namelen = strlen(path);
443 int pos;
444 int ret = 0;
445 struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
447 /* See if there is such entry in the index. */
448 pos = cache_name_pos(path, namelen);
449 if (0 <= pos) {
450 /* already merged */
451 pos = unmerge_cache_entry_at(pos);
452 if (pos < active_nr) {
453 const struct cache_entry *ce = active_cache[pos];
454 if (ce_stage(ce) &&
455 ce_namelen(ce) == namelen &&
456 !memcmp(ce->name, path, namelen))
457 return 0;
459 /* no resolve-undo information; fall back */
460 } else {
461 /* If there isn't, either it is unmerged, or
462 * resolved as "removed" by mistake. We do not
463 * want to do anything in the former case.
465 pos = -pos-1;
466 if (pos < active_nr) {
467 const struct cache_entry *ce = active_cache[pos];
468 if (ce_namelen(ce) == namelen &&
469 !memcmp(ce->name, path, namelen)) {
470 fprintf(stderr,
471 "%s: skipping still unmerged path.\n",
472 path);
473 goto free_return;
478 /* Grab blobs from given path from HEAD and MERGE_HEAD,
479 * stuff HEAD version in stage #2,
480 * stuff MERGE_HEAD version in stage #3.
482 ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
483 ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);
485 if (!ce_2 || !ce_3) {
486 ret = -1;
487 goto free_return;
489 if (!hashcmp(ce_2->sha1, ce_3->sha1) &&
490 ce_2->ce_mode == ce_3->ce_mode) {
491 fprintf(stderr, "%s: identical in both, skipping.\n",
492 path);
493 goto free_return;
496 remove_file_from_cache(path);
497 if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
498 error("%s: cannot add our version to the index.", path);
499 ret = -1;
500 goto free_return;
502 if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
503 return 0;
504 error("%s: cannot add their version to the index.", path);
505 ret = -1;
506 free_return:
507 free(ce_2);
508 free(ce_3);
509 return ret;
512 static void read_head_pointers(void)
514 if (read_ref("HEAD", head_sha1))
515 die("No HEAD -- no initial commit yet?");
516 if (read_ref("MERGE_HEAD", merge_head_sha1)) {
517 fprintf(stderr, "Not in the middle of a merge.\n");
518 exit(0);
522 static int do_unresolve(int ac, const char **av,
523 const char *prefix, int prefix_length)
525 int i;
526 int err = 0;
528 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
529 * are not doing a merge, so exit with success status.
531 read_head_pointers();
533 for (i = 1; i < ac; i++) {
534 const char *arg = av[i];
535 const char *p = prefix_path(prefix, prefix_length, arg);
536 err |= unresolve_one(p);
537 if (p < arg || p > arg + strlen(arg))
538 free((char *)p);
540 return err;
543 static int do_reupdate(int ac, const char **av,
544 const char *prefix, int prefix_length)
546 /* Read HEAD and run update-index on paths that are
547 * merged and already different between index and HEAD.
549 int pos;
550 int has_head = 1;
551 struct pathspec pathspec;
553 parse_pathspec(&pathspec, 0,
554 PATHSPEC_PREFER_CWD,
555 prefix, av + 1);
557 if (read_ref("HEAD", head_sha1))
558 /* If there is no HEAD, that means it is an initial
559 * commit. Update everything in the index.
561 has_head = 0;
562 redo:
563 for (pos = 0; pos < active_nr; pos++) {
564 const struct cache_entry *ce = active_cache[pos];
565 struct cache_entry *old = NULL;
566 int save_nr;
567 char *path;
569 if (ce_stage(ce) || !ce_path_match(ce, &pathspec, NULL))
570 continue;
571 if (has_head)
572 old = read_one_ent(NULL, head_sha1,
573 ce->name, ce_namelen(ce), 0);
574 if (old && ce->ce_mode == old->ce_mode &&
575 !hashcmp(ce->sha1, old->sha1)) {
576 free(old);
577 continue; /* unchanged */
579 /* Be careful. The working tree may not have the
580 * path anymore, in which case, under 'allow_remove',
581 * or worse yet 'allow_replace', active_nr may decrease.
583 save_nr = active_nr;
584 path = xstrdup(ce->name);
585 update_one(path);
586 free(path);
587 free(old);
588 if (save_nr != active_nr)
589 goto redo;
591 free_pathspec(&pathspec);
592 return 0;
595 struct refresh_params {
596 unsigned int flags;
597 int *has_errors;
600 static int refresh(struct refresh_params *o, unsigned int flag)
602 setup_work_tree();
603 read_cache_preload(NULL);
604 *o->has_errors |= refresh_cache(o->flags | flag);
605 return 0;
608 static int refresh_callback(const struct option *opt,
609 const char *arg, int unset)
611 return refresh(opt->value, 0);
614 static int really_refresh_callback(const struct option *opt,
615 const char *arg, int unset)
617 return refresh(opt->value, REFRESH_REALLY);
620 static int chmod_callback(const struct option *opt,
621 const char *arg, int unset)
623 char *flip = opt->value;
624 if ((arg[0] != '-' && arg[0] != '+') || arg[1] != 'x' || arg[2])
625 return error("option 'chmod' expects \"+x\" or \"-x\"");
626 *flip = arg[0];
627 return 0;
630 static int resolve_undo_clear_callback(const struct option *opt,
631 const char *arg, int unset)
633 resolve_undo_clear();
634 return 0;
637 static int parse_new_style_cacheinfo(const char *arg,
638 unsigned int *mode,
639 unsigned char sha1[],
640 const char **path)
642 unsigned long ul;
643 char *endp;
645 if (!arg)
646 return -1;
648 errno = 0;
649 ul = strtoul(arg, &endp, 8);
650 if (errno || endp == arg || *endp != ',' || (unsigned int) ul != ul)
651 return -1; /* not a new-style cacheinfo */
652 *mode = ul;
653 endp++;
654 if (get_sha1_hex(endp, sha1) || endp[40] != ',')
655 return -1;
656 *path = endp + 41;
657 return 0;
660 static int cacheinfo_callback(struct parse_opt_ctx_t *ctx,
661 const struct option *opt, int unset)
663 unsigned char sha1[20];
664 unsigned int mode;
665 const char *path;
667 if (!parse_new_style_cacheinfo(ctx->argv[1], &mode, sha1, &path)) {
668 if (add_cacheinfo(mode, sha1, path, 0))
669 die("git update-index: --cacheinfo cannot add %s", path);
670 ctx->argv++;
671 ctx->argc--;
672 return 0;
674 if (ctx->argc <= 3)
675 return error("option 'cacheinfo' expects <mode>,<sha1>,<path>");
676 if (strtoul_ui(*++ctx->argv, 8, &mode) ||
677 get_sha1_hex(*++ctx->argv, sha1) ||
678 add_cacheinfo(mode, sha1, *++ctx->argv, 0))
679 die("git update-index: --cacheinfo cannot add %s", *ctx->argv);
680 ctx->argc -= 3;
681 return 0;
684 static int stdin_cacheinfo_callback(struct parse_opt_ctx_t *ctx,
685 const struct option *opt, int unset)
687 int *line_termination = opt->value;
689 if (ctx->argc != 1)
690 return error("option '%s' must be the last argument", opt->long_name);
691 allow_add = allow_replace = allow_remove = 1;
692 read_index_info(*line_termination);
693 return 0;
696 static int stdin_callback(struct parse_opt_ctx_t *ctx,
697 const struct option *opt, int unset)
699 int *read_from_stdin = opt->value;
701 if (ctx->argc != 1)
702 return error("option '%s' must be the last argument", opt->long_name);
703 *read_from_stdin = 1;
704 return 0;
707 static int unresolve_callback(struct parse_opt_ctx_t *ctx,
708 const struct option *opt, int flags)
710 int *has_errors = opt->value;
711 const char *prefix = startup_info->prefix;
713 /* consume remaining arguments. */
714 *has_errors = do_unresolve(ctx->argc, ctx->argv,
715 prefix, prefix ? strlen(prefix) : 0);
716 if (*has_errors)
717 active_cache_changed = 0;
719 ctx->argv += ctx->argc - 1;
720 ctx->argc = 1;
721 return 0;
724 static int reupdate_callback(struct parse_opt_ctx_t *ctx,
725 const struct option *opt, int flags)
727 int *has_errors = opt->value;
728 const char *prefix = startup_info->prefix;
730 /* consume remaining arguments. */
731 setup_work_tree();
732 *has_errors = do_reupdate(ctx->argc, ctx->argv,
733 prefix, prefix ? strlen(prefix) : 0);
734 if (*has_errors)
735 active_cache_changed = 0;
737 ctx->argv += ctx->argc - 1;
738 ctx->argc = 1;
739 return 0;
742 int cmd_update_index(int argc, const char **argv, const char *prefix)
744 int newfd, entries, has_errors = 0, line_termination = '\n';
745 int read_from_stdin = 0;
746 int prefix_length = prefix ? strlen(prefix) : 0;
747 int preferred_index_format = 0;
748 char set_executable_bit = 0;
749 struct refresh_params refresh_args = {0, &has_errors};
750 int lock_error = 0;
751 int split_index = -1;
752 struct lock_file *lock_file;
753 struct parse_opt_ctx_t ctx;
754 int parseopt_state = PARSE_OPT_UNKNOWN;
755 struct option options[] = {
756 OPT_BIT('q', NULL, &refresh_args.flags,
757 N_("continue refresh even when index needs update"),
758 REFRESH_QUIET),
759 OPT_BIT(0, "ignore-submodules", &refresh_args.flags,
760 N_("refresh: ignore submodules"),
761 REFRESH_IGNORE_SUBMODULES),
762 OPT_SET_INT(0, "add", &allow_add,
763 N_("do not ignore new files"), 1),
764 OPT_SET_INT(0, "replace", &allow_replace,
765 N_("let files replace directories and vice-versa"), 1),
766 OPT_SET_INT(0, "remove", &allow_remove,
767 N_("notice files missing from worktree"), 1),
768 OPT_BIT(0, "unmerged", &refresh_args.flags,
769 N_("refresh even if index contains unmerged entries"),
770 REFRESH_UNMERGED),
771 {OPTION_CALLBACK, 0, "refresh", &refresh_args, NULL,
772 N_("refresh stat information"),
773 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
774 refresh_callback},
775 {OPTION_CALLBACK, 0, "really-refresh", &refresh_args, NULL,
776 N_("like --refresh, but ignore assume-unchanged setting"),
777 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
778 really_refresh_callback},
779 {OPTION_LOWLEVEL_CALLBACK, 0, "cacheinfo", NULL,
780 N_("<mode>,<object>,<path>"),
781 N_("add the specified entry to the index"),
782 PARSE_OPT_NOARG | /* disallow --cacheinfo=<mode> form */
783 PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
784 (parse_opt_cb *) cacheinfo_callback},
785 {OPTION_CALLBACK, 0, "chmod", &set_executable_bit, N_("(+/-)x"),
786 N_("override the executable bit of the listed files"),
787 PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
788 chmod_callback},
789 {OPTION_SET_INT, 0, "assume-unchanged", &mark_valid_only, NULL,
790 N_("mark files as \"not changing\""),
791 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
792 {OPTION_SET_INT, 0, "no-assume-unchanged", &mark_valid_only, NULL,
793 N_("clear assumed-unchanged bit"),
794 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
795 {OPTION_SET_INT, 0, "skip-worktree", &mark_skip_worktree_only, NULL,
796 N_("mark files as \"index-only\""),
797 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
798 {OPTION_SET_INT, 0, "no-skip-worktree", &mark_skip_worktree_only, NULL,
799 N_("clear skip-worktree bit"),
800 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
801 OPT_SET_INT(0, "info-only", &info_only,
802 N_("add to index only; do not add content to object database"), 1),
803 OPT_SET_INT(0, "force-remove", &force_remove,
804 N_("remove named paths even if present in worktree"), 1),
805 OPT_SET_INT('z', NULL, &line_termination,
806 N_("with --stdin: input lines are terminated by null bytes"), '\0'),
807 {OPTION_LOWLEVEL_CALLBACK, 0, "stdin", &read_from_stdin, NULL,
808 N_("read list of paths to be updated from standard input"),
809 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
810 (parse_opt_cb *) stdin_callback},
811 {OPTION_LOWLEVEL_CALLBACK, 0, "index-info", &line_termination, NULL,
812 N_("add entries from standard input to the index"),
813 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
814 (parse_opt_cb *) stdin_cacheinfo_callback},
815 {OPTION_LOWLEVEL_CALLBACK, 0, "unresolve", &has_errors, NULL,
816 N_("repopulate stages #2 and #3 for the listed paths"),
817 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
818 (parse_opt_cb *) unresolve_callback},
819 {OPTION_LOWLEVEL_CALLBACK, 'g', "again", &has_errors, NULL,
820 N_("only update entries that differ from HEAD"),
821 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
822 (parse_opt_cb *) reupdate_callback},
823 OPT_BIT(0, "ignore-missing", &refresh_args.flags,
824 N_("ignore files missing from worktree"),
825 REFRESH_IGNORE_MISSING),
826 OPT_SET_INT(0, "verbose", &verbose,
827 N_("report actions to standard output"), 1),
828 {OPTION_CALLBACK, 0, "clear-resolve-undo", NULL, NULL,
829 N_("(for porcelains) forget saved unresolved conflicts"),
830 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
831 resolve_undo_clear_callback},
832 OPT_INTEGER(0, "index-version", &preferred_index_format,
833 N_("write index in this format")),
834 OPT_BOOL(0, "split-index", &split_index,
835 N_("enable or disable split index")),
836 OPT_END()
839 if (argc == 2 && !strcmp(argv[1], "-h"))
840 usage_with_options(update_index_usage, options);
842 git_config(git_default_config, NULL);
844 /* We can't free this memory, it becomes part of a linked list parsed atexit() */
845 lock_file = xcalloc(1, sizeof(struct lock_file));
847 newfd = hold_locked_index(lock_file, 0);
848 if (newfd < 0)
849 lock_error = errno;
851 entries = read_cache();
852 if (entries < 0)
853 die("cache corrupted");
856 * Custom copy of parse_options() because we want to handle
857 * filename arguments as they come.
859 parse_options_start(&ctx, argc, argv, prefix,
860 options, PARSE_OPT_STOP_AT_NON_OPTION);
861 while (ctx.argc) {
862 if (parseopt_state != PARSE_OPT_DONE)
863 parseopt_state = parse_options_step(&ctx, options,
864 update_index_usage);
865 if (!ctx.argc)
866 break;
867 switch (parseopt_state) {
868 case PARSE_OPT_HELP:
869 exit(129);
870 case PARSE_OPT_NON_OPTION:
871 case PARSE_OPT_DONE:
873 const char *path = ctx.argv[0];
874 const char *p;
876 setup_work_tree();
877 p = prefix_path(prefix, prefix_length, path);
878 update_one(p);
879 if (set_executable_bit)
880 chmod_path(set_executable_bit, p);
881 free((char *)p);
882 ctx.argc--;
883 ctx.argv++;
884 break;
886 case PARSE_OPT_UNKNOWN:
887 if (ctx.argv[0][1] == '-')
888 error("unknown option '%s'", ctx.argv[0] + 2);
889 else
890 error("unknown switch '%c'", *ctx.opt);
891 usage_with_options(update_index_usage, options);
894 argc = parse_options_end(&ctx);
895 if (preferred_index_format) {
896 if (preferred_index_format < INDEX_FORMAT_LB ||
897 INDEX_FORMAT_UB < preferred_index_format)
898 die("index-version %d not in range: %d..%d",
899 preferred_index_format,
900 INDEX_FORMAT_LB, INDEX_FORMAT_UB);
902 if (the_index.version != preferred_index_format)
903 active_cache_changed |= SOMETHING_CHANGED;
904 the_index.version = preferred_index_format;
907 if (read_from_stdin) {
908 struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
910 setup_work_tree();
911 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
912 const char *p;
913 if (line_termination && buf.buf[0] == '"') {
914 strbuf_reset(&nbuf);
915 if (unquote_c_style(&nbuf, buf.buf, NULL))
916 die("line is badly quoted");
917 strbuf_swap(&buf, &nbuf);
919 p = prefix_path(prefix, prefix_length, buf.buf);
920 update_one(p);
921 if (set_executable_bit)
922 chmod_path(set_executable_bit, p);
923 free((char *)p);
925 strbuf_release(&nbuf);
926 strbuf_release(&buf);
929 if (split_index > 0) {
930 init_split_index(&the_index);
931 the_index.cache_changed |= SPLIT_INDEX_ORDERED;
932 } else if (!split_index && the_index.split_index) {
934 * can't discard_split_index(&the_index); because that
935 * will destroy split_index->base->cache[], which may
936 * be shared with the_index.cache[]. So yeah we're
937 * leaking a bit here.
939 the_index.split_index = NULL;
940 the_index.cache_changed |= SOMETHING_CHANGED;
943 if (active_cache_changed) {
944 if (newfd < 0) {
945 if (refresh_args.flags & REFRESH_QUIET)
946 exit(128);
947 unable_to_lock_die(get_index_file(), lock_error);
949 if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
950 die("Unable to write new index file");
953 rollback_lock_file(lock_file);
955 return has_errors ? 1 : 0;