1/*
2 * GIT - The information manager from hell
3 *
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
15/*
16 * Default to not allowing changes to the list of files. The
17 * tool doesn't actually care, but this makes it harder to add
18 * files to the revision control by mistake by doing something
19 * like "git update-index *" and suddenly having all the object
20 * files be revision controlled.
21 */
22static int allow_add;
23static int allow_remove;
24static int allow_replace;
25static int info_only;
26static int force_remove;
27static int verbose;
28static int mark_valid_only;
29static int mark_skip_worktree_only;
30#define MARK_FLAG 1
31#define UNMARK_FLAG 2
32
33__attribute__((format (printf, 1, 2)))
34static void report(const char *fmt, ...)
35{
36 va_list vp;
37
38 if (!verbose)
39 return;
40
41 va_start(vp, fmt);
42 vprintf(fmt, vp);
43 putchar('\n');
44 va_end(vp);
45}
46
47static int mark_ce_flags(const char *path, int flag, int mark)
48{
49 int namelen = strlen(path);
50 int pos = cache_name_pos(path, namelen);
51 if (0 <= pos) {
52 if (mark)
53 active_cache[pos]->ce_flags |= flag;
54 else
55 active_cache[pos]->ce_flags &= ~flag;
56 cache_tree_invalidate_path(active_cache_tree, path);
57 active_cache_changed = 1;
58 return 0;
59 }
60 return -1;
61}
62
63static int remove_one_path(const char *path)
64{
65 if (!allow_remove)
66 return error("%s: does not exist and --remove not passed", path);
67 if (remove_file_from_cache(path))
68 return error("%s: cannot remove from the index", path);
69 return 0;
70}
71
72/*
73 * Handle a path that couldn't be lstat'ed. It's either:
74 * - missing file (ENOENT or ENOTDIR). That's ok if we're
75 * supposed to be removing it and the removal actually
76 * succeeds.
77 * - permission error. That's never ok.
78 */
79static int process_lstat_error(const char *path, int err)
80{
81 if (err == ENOENT || err == ENOTDIR)
82 return remove_one_path(path);
83 return error("lstat(\"%s\"): %s", path, strerror(errno));
84}
85
86static int add_one_path(struct cache_entry *old, const char *path, int len, struct stat *st)
87{
88 int option, size;
89 struct cache_entry *ce;
90
91 /* Was the old index entry already up-to-date? */
92 if (old && !ce_stage(old) && !ce_match_stat(old, st, 0))
93 return 0;
94
95 size = cache_entry_size(len);
96 ce = xcalloc(1, size);
97 memcpy(ce->name, path, len);
98 ce->ce_flags = len;
99 fill_stat_cache_info(ce, st);
100 ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
101
102 if (index_path(ce->sha1, path, st,
103 info_only ? 0 : HASH_WRITE_OBJECT)) {
104 free(ce);
105 return -1;
106 }
107 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
108 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
109 if (add_cache_entry(ce, option))
110 return error("%s: cannot add to the index - missing --add option?", path);
111 return 0;
112}
113
114/*
115 * Handle a path that was a directory. Four cases:
116 *
117 * - it's already a gitlink in the index, and we keep it that
118 * way, and update it if we can (if we cannot find the HEAD,
119 * we're going to keep it unchanged in the index!)
120 *
121 * - it's a *file* in the index, in which case it should be
122 * removed as a file if removal is allowed, since it doesn't
123 * exist as such any more. If removal isn't allowed, it's
124 * an error.
125 *
126 * (NOTE! This is old and arguably fairly strange behaviour.
127 * We might want to make this an error unconditionally, and
128 * use "--force-remove" if you actually want to force removal).
129 *
130 * - it used to exist as a subdirectory (ie multiple files with
131 * this particular prefix) in the index, in which case it's wrong
132 * to try to update it as a directory.
133 *
134 * - it doesn't exist at all in the index, but it is a valid
135 * git directory, and it should be *added* as a gitlink.
136 */
137static int process_directory(const char *path, int len, struct stat *st)
138{
139 unsigned char sha1[20];
140 int pos = cache_name_pos(path, len);
141
142 /* Exact match: file or existing gitlink */
143 if (pos >= 0) {
144 struct cache_entry *ce = active_cache[pos];
145 if (S_ISGITLINK(ce->ce_mode)) {
146
147 /* Do nothing to the index if there is no HEAD! */
148 if (resolve_gitlink_ref(path, "HEAD", sha1) < 0)
149 return 0;
150
151 return add_one_path(ce, path, len, st);
152 }
153 /* Should this be an unconditional error? */
154 return remove_one_path(path);
155 }
156
157 /* Inexact match: is there perhaps a subdirectory match? */
158 pos = -pos-1;
159 while (pos < active_nr) {
160 struct cache_entry *ce = active_cache[pos++];
161
162 if (strncmp(ce->name, path, len))
163 break;
164 if (ce->name[len] > '/')
165 break;
166 if (ce->name[len] < '/')
167 continue;
168
169 /* Subdirectory match - error out */
170 return error("%s: is a directory - add individual files instead", path);
171 }
172
173 /* No match - should we add it as a gitlink? */
174 if (!resolve_gitlink_ref(path, "HEAD", sha1))
175 return add_one_path(NULL, path, len, st);
176
177 /* Error out. */
178 return error("%s: is a directory - add files inside instead", path);
179}
180
181static int process_path(const char *path)
182{
183 int pos, len;
184 struct stat st;
185 struct cache_entry *ce;
186
187 len = strlen(path);
188 if (has_symlink_leading_path(path, len))
189 return error("'%s' is beyond a symbolic link", path);
190
191 pos = cache_name_pos(path, len);
192 ce = pos < 0 ? NULL : active_cache[pos];
193 if (ce && ce_skip_worktree(ce)) {
194 /*
195 * working directory version is assumed "good"
196 * so updating it does not make sense.
197 * On the other hand, removing it from index should work
198 */
199 if (allow_remove && remove_file_from_cache(path))
200 return error("%s: cannot remove from the index", path);
201 return 0;
202 }
203
204 /*
205 * First things first: get the stat information, to decide
206 * what to do about the pathname!
207 */
208 if (lstat(path, &st) < 0)
209 return process_lstat_error(path, errno);
210
211 if (S_ISDIR(st.st_mode))
212 return process_directory(path, len, &st);
213
214 return add_one_path(ce, path, len, &st);
215}
216
217static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
218 const char *path, int stage)
219{
220 int size, len, option;
221 struct cache_entry *ce;
222
223 if (!verify_path(path))
224 return error("Invalid path '%s'", path);
225
226 len = strlen(path);
227 size = cache_entry_size(len);
228 ce = xcalloc(1, size);
229
230 hashcpy(ce->sha1, sha1);
231 memcpy(ce->name, path, len);
232 ce->ce_flags = create_ce_flags(len, stage);
233 ce->ce_mode = create_ce_mode(mode);
234 if (assume_unchanged)
235 ce->ce_flags |= CE_VALID;
236 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
237 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
238 if (add_cache_entry(ce, option))
239 return error("%s: cannot add to the index - missing --add option?",
240 path);
241 report("add '%s'", path);
242 return 0;
243}
244
245static void chmod_path(int flip, const char *path)
246{
247 int pos;
248 struct cache_entry *ce;
249 unsigned int mode;
250
251 pos = cache_name_pos(path, strlen(path));
252 if (pos < 0)
253 goto fail;
254 ce = active_cache[pos];
255 mode = ce->ce_mode;
256 if (!S_ISREG(mode))
257 goto fail;
258 switch (flip) {
259 case '+':
260 ce->ce_mode |= 0111; break;
261 case '-':
262 ce->ce_mode &= ~0111; break;
263 default:
264 goto fail;
265 }
266 cache_tree_invalidate_path(active_cache_tree, path);
267 active_cache_changed = 1;
268 report("chmod %cx '%s'", flip, path);
269 return;
270 fail:
271 die("git update-index: cannot chmod %cx '%s'", flip, path);
272}
273
274static void update_one(const char *path, const char *prefix, int prefix_length)
275{
276 const char *p = prefix_path(prefix, prefix_length, path);
277 if (!verify_path(p)) {
278 fprintf(stderr, "Ignoring path %s\n", path);
279 goto free_return;
280 }
281 if (mark_valid_only) {
282 if (mark_ce_flags(p, CE_VALID, mark_valid_only == MARK_FLAG))
283 die("Unable to mark file %s", path);
284 goto free_return;
285 }
286 if (mark_skip_worktree_only) {
287 if (mark_ce_flags(p, CE_SKIP_WORKTREE, mark_skip_worktree_only == MARK_FLAG))
288 die("Unable to mark file %s", path);
289 goto free_return;
290 }
291
292 if (force_remove) {
293 if (remove_file_from_cache(p))
294 die("git update-index: unable to remove %s", path);
295 report("remove '%s'", path);
296 goto free_return;
297 }
298 if (process_path(p))
299 die("Unable to process path %s", path);
300 report("add '%s'", path);
301 free_return:
302 if (p < path || p > path + strlen(path))
303 free((char *)p);
304}
305
306static void read_index_info(int line_termination)
307{
308 struct strbuf buf = STRBUF_INIT;
309 struct strbuf uq = STRBUF_INIT;
310
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;
318
319 /* This reads lines formatted in one of three formats:
320 *
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.
326 *
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.
330 *
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.
334 */
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;
341
342 tab = strchr(ptr, '\t');
343 if (!tab || tab - ptr < 41)
344 goto bad_line;
345
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 */
350 }
351 else {
352 stage = 0;
353 ptr = tab + 1; /* point at the head of path */
354 }
355
356 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
357 goto bad_line;
358
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");
364 }
365 path_name = uq.buf;
366 }
367
368 if (!verify_path(path_name)) {
369 fprintf(stderr, "Ignoring path %s\n", path_name);
370 continue;
371 }
372
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);
378 }
379 else {
380 /* mode ' ' sha1 '\t' name
381 * ptr[-1] points at tab,
382 * ptr[-41] is at the beginning of sha1
383 */
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);
388 }
389 continue;
390
391 bad_line:
392 die("malformed index info %s", buf.buf);
393 }
394 strbuf_release(&buf);
395 strbuf_release(&uq);
396}
397
398static const char * const update_index_usage[] = {
399 "git update-index [options] [--] [<file>...]",
400 NULL
401};
402
403static unsigned char head_sha1[20];
404static unsigned char merge_head_sha1[20];
405
406static struct cache_entry *read_one_ent(const char *which,
407 unsigned char *ent, const char *path,
408 int namelen, int stage)
409{
410 unsigned mode;
411 unsigned char sha1[20];
412 int size;
413 struct cache_entry *ce;
414
415 if (get_tree_entry(ent, path, sha1, &mode)) {
416 if (which)
417 error("%s: not in %s branch.", path, which);
418 return NULL;
419 }
420 if (mode == S_IFDIR) {
421 if (which)
422 error("%s: not a blob in %s branch.", path, which);
423 return NULL;
424 }
425 size = cache_entry_size(namelen);
426 ce = xcalloc(1, size);
427
428 hashcpy(ce->sha1, sha1);
429 memcpy(ce->name, path, namelen);
430 ce->ce_flags = create_ce_flags(namelen, stage);
431 ce->ce_mode = create_ce_mode(mode);
432 return ce;
433}
434
435static int unresolve_one(const char *path)
436{
437 int namelen = strlen(path);
438 int pos;
439 int ret = 0;
440 struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
441
442 /* See if there is such entry in the index. */
443 pos = cache_name_pos(path, namelen);
444 if (0 <= pos) {
445 /* already merged */
446 pos = unmerge_cache_entry_at(pos);
447 if (pos < active_nr) {
448 struct cache_entry *ce = active_cache[pos];
449 if (ce_stage(ce) &&
450 ce_namelen(ce) == namelen &&
451 !memcmp(ce->name, path, namelen))
452 return 0;
453 }
454 /* no resolve-undo information; fall back */
455 } else {
456 /* If there isn't, either it is unmerged, or
457 * resolved as "removed" by mistake. We do not
458 * want to do anything in the former case.
459 */
460 pos = -pos-1;
461 if (pos < active_nr) {
462 struct cache_entry *ce = active_cache[pos];
463 if (ce_namelen(ce) == namelen &&
464 !memcmp(ce->name, path, namelen)) {
465 fprintf(stderr,
466 "%s: skipping still unmerged path.\n",
467 path);
468 goto free_return;
469 }
470 }
471 }
472
473 /* Grab blobs from given path from HEAD and MERGE_HEAD,
474 * stuff HEAD version in stage #2,
475 * stuff MERGE_HEAD version in stage #3.
476 */
477 ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
478 ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);
479
480 if (!ce_2 || !ce_3) {
481 ret = -1;
482 goto free_return;
483 }
484 if (!hashcmp(ce_2->sha1, ce_3->sha1) &&
485 ce_2->ce_mode == ce_3->ce_mode) {
486 fprintf(stderr, "%s: identical in both, skipping.\n",
487 path);
488 goto free_return;
489 }
490
491 remove_file_from_cache(path);
492 if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
493 error("%s: cannot add our version to the index.", path);
494 ret = -1;
495 goto free_return;
496 }
497 if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
498 return 0;
499 error("%s: cannot add their version to the index.", path);
500 ret = -1;
501 free_return:
502 free(ce_2);
503 free(ce_3);
504 return ret;
505}
506
507static void read_head_pointers(void)
508{
509 if (read_ref("HEAD", head_sha1))
510 die("No HEAD -- no initial commit yet?");
511 if (read_ref("MERGE_HEAD", merge_head_sha1)) {
512 fprintf(stderr, "Not in the middle of a merge.\n");
513 exit(0);
514 }
515}
516
517static int do_unresolve(int ac, const char **av,
518 const char *prefix, int prefix_length)
519{
520 int i;
521 int err = 0;
522
523 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
524 * are not doing a merge, so exit with success status.
525 */
526 read_head_pointers();
527
528 for (i = 1; i < ac; i++) {
529 const char *arg = av[i];
530 const char *p = prefix_path(prefix, prefix_length, arg);
531 err |= unresolve_one(p);
532 if (p < arg || p > arg + strlen(arg))
533 free((char *)p);
534 }
535 return err;
536}
537
538static int do_reupdate(int ac, const char **av,
539 const char *prefix, int prefix_length)
540{
541 /* Read HEAD and run update-index on paths that are
542 * merged and already different between index and HEAD.
543 */
544 int pos;
545 int has_head = 1;
546 const char **paths = get_pathspec(prefix, av + 1);
547 struct pathspec pathspec;
548
549 init_pathspec(&pathspec, paths);
550
551 if (read_ref("HEAD", head_sha1))
552 /* If there is no HEAD, that means it is an initial
553 * commit. Update everything in the index.
554 */
555 has_head = 0;
556 redo:
557 for (pos = 0; pos < active_nr; pos++) {
558 struct cache_entry *ce = active_cache[pos];
559 struct cache_entry *old = NULL;
560 int save_nr;
561
562 if (ce_stage(ce) || !ce_path_match(ce, &pathspec))
563 continue;
564 if (has_head)
565 old = read_one_ent(NULL, head_sha1,
566 ce->name, ce_namelen(ce), 0);
567 if (old && ce->ce_mode == old->ce_mode &&
568 !hashcmp(ce->sha1, old->sha1)) {
569 free(old);
570 continue; /* unchanged */
571 }
572 /* Be careful. The working tree may not have the
573 * path anymore, in which case, under 'allow_remove',
574 * or worse yet 'allow_replace', active_nr may decrease.
575 */
576 save_nr = active_nr;
577 update_one(ce->name + prefix_length, prefix, prefix_length);
578 if (save_nr != active_nr)
579 goto redo;
580 }
581 free_pathspec(&pathspec);
582 return 0;
583}
584
585struct refresh_params {
586 unsigned int flags;
587 int *has_errors;
588};
589
590static int refresh(struct refresh_params *o, unsigned int flag)
591{
592 setup_work_tree();
593 *o->has_errors |= refresh_cache(o->flags | flag);
594 return 0;
595}
596
597static int refresh_callback(const struct option *opt,
598 const char *arg, int unset)
599{
600 return refresh(opt->value, 0);
601}
602
603static int really_refresh_callback(const struct option *opt,
604 const char *arg, int unset)
605{
606 return refresh(opt->value, REFRESH_REALLY);
607}
608
609static int chmod_callback(const struct option *opt,
610 const char *arg, int unset)
611{
612 char *flip = opt->value;
613 if ((arg[0] != '-' && arg[0] != '+') || arg[1] != 'x' || arg[2])
614 return error("option 'chmod' expects \"+x\" or \"-x\"");
615 *flip = arg[0];
616 return 0;
617}
618
619static int resolve_undo_clear_callback(const struct option *opt,
620 const char *arg, int unset)
621{
622 resolve_undo_clear();
623 return 0;
624}
625
626static int cacheinfo_callback(struct parse_opt_ctx_t *ctx,
627 const struct option *opt, int unset)
628{
629 unsigned char sha1[20];
630 unsigned int mode;
631
632 if (ctx->argc <= 3)
633 return error("option 'cacheinfo' expects three arguments");
634 if (strtoul_ui(*++ctx->argv, 8, &mode) ||
635 get_sha1_hex(*++ctx->argv, sha1) ||
636 add_cacheinfo(mode, sha1, *++ctx->argv, 0))
637 die("git update-index: --cacheinfo cannot add %s", *ctx->argv);
638 ctx->argc -= 3;
639 return 0;
640}
641
642static int stdin_cacheinfo_callback(struct parse_opt_ctx_t *ctx,
643 const struct option *opt, int unset)
644{
645 int *line_termination = opt->value;
646
647 if (ctx->argc != 1)
648 return error("option '%s' must be the last argument", opt->long_name);
649 allow_add = allow_replace = allow_remove = 1;
650 read_index_info(*line_termination);
651 return 0;
652}
653
654static int stdin_callback(struct parse_opt_ctx_t *ctx,
655 const struct option *opt, int unset)
656{
657 int *read_from_stdin = opt->value;
658
659 if (ctx->argc != 1)
660 return error("option '%s' must be the last argument", opt->long_name);
661 *read_from_stdin = 1;
662 return 0;
663}
664
665static int unresolve_callback(struct parse_opt_ctx_t *ctx,
666 const struct option *opt, int flags)
667{
668 int *has_errors = opt->value;
669 const char *prefix = startup_info->prefix;
670
671 /* consume remaining arguments. */
672 *has_errors = do_unresolve(ctx->argc, ctx->argv,
673 prefix, prefix ? strlen(prefix) : 0);
674 if (*has_errors)
675 active_cache_changed = 0;
676
677 ctx->argv += ctx->argc - 1;
678 ctx->argc = 1;
679 return 0;
680}
681
682static int reupdate_callback(struct parse_opt_ctx_t *ctx,
683 const struct option *opt, int flags)
684{
685 int *has_errors = opt->value;
686 const char *prefix = startup_info->prefix;
687
688 /* consume remaining arguments. */
689 setup_work_tree();
690 *has_errors = do_reupdate(ctx->argc, ctx->argv,
691 prefix, prefix ? strlen(prefix) : 0);
692 if (*has_errors)
693 active_cache_changed = 0;
694
695 ctx->argv += ctx->argc - 1;
696 ctx->argc = 1;
697 return 0;
698}
699
700int cmd_update_index(int argc, const char **argv, const char *prefix)
701{
702 int newfd, entries, has_errors = 0, line_termination = '\n';
703 int read_from_stdin = 0;
704 int prefix_length = prefix ? strlen(prefix) : 0;
705 int preferred_index_format = 0;
706 char set_executable_bit = 0;
707 struct refresh_params refresh_args = {0, &has_errors};
708 int lock_error = 0;
709 struct lock_file *lock_file;
710 struct parse_opt_ctx_t ctx;
711 int parseopt_state = PARSE_OPT_UNKNOWN;
712 struct option options[] = {
713 OPT_BIT('q', NULL, &refresh_args.flags,
714 "continue refresh even when index needs update",
715 REFRESH_QUIET),
716 OPT_BIT(0, "ignore-submodules", &refresh_args.flags,
717 "refresh: ignore submodules",
718 REFRESH_IGNORE_SUBMODULES),
719 OPT_SET_INT(0, "add", &allow_add,
720 "do not ignore new files", 1),
721 OPT_SET_INT(0, "replace", &allow_replace,
722 "let files replace directories and vice-versa", 1),
723 OPT_SET_INT(0, "remove", &allow_remove,
724 "notice files missing from worktree", 1),
725 OPT_BIT(0, "unmerged", &refresh_args.flags,
726 "refresh even if index contains unmerged entries",
727 REFRESH_UNMERGED),
728 {OPTION_CALLBACK, 0, "refresh", &refresh_args, NULL,
729 "refresh stat information",
730 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
731 refresh_callback},
732 {OPTION_CALLBACK, 0, "really-refresh", &refresh_args, NULL,
733 "like --refresh, but ignore assume-unchanged setting",
734 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
735 really_refresh_callback},
736 {OPTION_LOWLEVEL_CALLBACK, 0, "cacheinfo", NULL,
737 "<mode> <object> <path>",
738 "add the specified entry to the index",
739 PARSE_OPT_NOARG | /* disallow --cacheinfo=<mode> form */
740 PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
741 (parse_opt_cb *) cacheinfo_callback},
742 {OPTION_CALLBACK, 0, "chmod", &set_executable_bit, "(+/-)x",
743 "override the executable bit of the listed files",
744 PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
745 chmod_callback},
746 {OPTION_SET_INT, 0, "assume-unchanged", &mark_valid_only, NULL,
747 "mark files as \"not changing\"",
748 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
749 {OPTION_SET_INT, 0, "no-assume-unchanged", &mark_valid_only, NULL,
750 "clear assumed-unchanged bit",
751 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
752 {OPTION_SET_INT, 0, "skip-worktree", &mark_skip_worktree_only, NULL,
753 "mark files as \"index-only\"",
754 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
755 {OPTION_SET_INT, 0, "no-skip-worktree", &mark_skip_worktree_only, NULL,
756 "clear skip-worktree bit",
757 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
758 OPT_SET_INT(0, "info-only", &info_only,
759 "add to index only; do not add content to object database", 1),
760 OPT_SET_INT(0, "force-remove", &force_remove,
761 "remove named paths even if present in worktree", 1),
762 OPT_SET_INT('z', NULL, &line_termination,
763 "with --stdin: input lines are terminated by null bytes", '\0'),
764 {OPTION_LOWLEVEL_CALLBACK, 0, "stdin", &read_from_stdin, NULL,
765 "read list of paths to be updated from standard input",
766 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
767 (parse_opt_cb *) stdin_callback},
768 {OPTION_LOWLEVEL_CALLBACK, 0, "index-info", &line_termination, NULL,
769 "add entries from standard input to the index",
770 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
771 (parse_opt_cb *) stdin_cacheinfo_callback},
772 {OPTION_LOWLEVEL_CALLBACK, 0, "unresolve", &has_errors, NULL,
773 "repopulate stages #2 and #3 for the listed paths",
774 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
775 (parse_opt_cb *) unresolve_callback},
776 {OPTION_LOWLEVEL_CALLBACK, 'g', "again", &has_errors, NULL,
777 "only update entries that differ from HEAD",
778 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
779 (parse_opt_cb *) reupdate_callback},
780 OPT_BIT(0, "ignore-missing", &refresh_args.flags,
781 "ignore files missing from worktree",
782 REFRESH_IGNORE_MISSING),
783 OPT_SET_INT(0, "verbose", &verbose,
784 "report actions to standard output", 1),
785 {OPTION_CALLBACK, 0, "clear-resolve-undo", NULL, NULL,
786 "(for porcelains) forget saved unresolved conflicts",
787 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
788 resolve_undo_clear_callback},
789 OPT_INTEGER(0, "index-version", &preferred_index_format,
790 "write index in this format"),
791 OPT_END()
792 };
793
794 if (argc == 2 && !strcmp(argv[1], "-h"))
795 usage(update_index_usage[0]);
796
797 git_config(git_default_config, NULL);
798
799 /* We can't free this memory, it becomes part of a linked list parsed atexit() */
800 lock_file = xcalloc(1, sizeof(struct lock_file));
801
802 newfd = hold_locked_index(lock_file, 0);
803 if (newfd < 0)
804 lock_error = errno;
805
806 entries = read_cache();
807 if (entries < 0)
808 die("cache corrupted");
809
810 /*
811 * Custom copy of parse_options() because we want to handle
812 * filename arguments as they come.
813 */
814 parse_options_start(&ctx, argc, argv, prefix,
815 options, PARSE_OPT_STOP_AT_NON_OPTION);
816 while (ctx.argc) {
817 if (parseopt_state != PARSE_OPT_DONE)
818 parseopt_state = parse_options_step(&ctx, options,
819 update_index_usage);
820 if (!ctx.argc)
821 break;
822 switch (parseopt_state) {
823 case PARSE_OPT_HELP:
824 exit(129);
825 case PARSE_OPT_NON_OPTION:
826 case PARSE_OPT_DONE:
827 {
828 const char *path = ctx.argv[0];
829 const char *p;
830
831 setup_work_tree();
832 p = prefix_path(prefix, prefix_length, path);
833 update_one(p, NULL, 0);
834 if (set_executable_bit)
835 chmod_path(set_executable_bit, p);
836 if (p < path || p > path + strlen(path))
837 free((char *)p);
838 ctx.argc--;
839 ctx.argv++;
840 break;
841 }
842 case PARSE_OPT_UNKNOWN:
843 if (ctx.argv[0][1] == '-')
844 error("unknown option '%s'", ctx.argv[0] + 2);
845 else
846 error("unknown switch '%c'", *ctx.opt);
847 usage_with_options(update_index_usage, options);
848 }
849 }
850 argc = parse_options_end(&ctx);
851 if (preferred_index_format) {
852 if (preferred_index_format < INDEX_FORMAT_LB ||
853 INDEX_FORMAT_UB < preferred_index_format)
854 die("index-version %d not in range: %d..%d",
855 preferred_index_format,
856 INDEX_FORMAT_LB, INDEX_FORMAT_UB);
857
858 if (the_index.version != preferred_index_format)
859 active_cache_changed = 1;
860 the_index.version = preferred_index_format;
861 }
862
863 if (read_from_stdin) {
864 struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
865
866 setup_work_tree();
867 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
868 const char *p;
869 if (line_termination && buf.buf[0] == '"') {
870 strbuf_reset(&nbuf);
871 if (unquote_c_style(&nbuf, buf.buf, NULL))
872 die("line is badly quoted");
873 strbuf_swap(&buf, &nbuf);
874 }
875 p = prefix_path(prefix, prefix_length, buf.buf);
876 update_one(p, NULL, 0);
877 if (set_executable_bit)
878 chmod_path(set_executable_bit, p);
879 if (p < buf.buf || p > buf.buf + buf.len)
880 free((char *)p);
881 }
882 strbuf_release(&nbuf);
883 strbuf_release(&buf);
884 }
885
886 if (active_cache_changed) {
887 if (newfd < 0) {
888 if (refresh_args.flags & REFRESH_QUIET)
889 exit(128);
890 unable_to_lock_index_die(get_index_file(), lock_error);
891 }
892 if (write_cache(newfd, active_cache, active_nr) ||
893 commit_locked_index(lock_file))
894 die("Unable to write new index file");
895 }
896
897 rollback_lock_file(lock_file);
898
899 return has_errors ? 1 : 0;
900}