]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-update-index.c
tests: rename duplicate t1009
[thirdparty/git.git] / builtin-update-index.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c5163 6#include "cache.h"
973d6a20 7#include "quote.h"
a6e5642f 8#include "cache-tree.h"
2bd452d3 9#include "tree-walk.h"
fefe81c9 10#include "builtin.h"
e011054b 11#include "refs.h"
e83c5163 12
121481ab
LT
13/*
14 * Default to not allowing changes to the list of files. The
15 * tool doesn't actually care, but this makes it harder to add
16 * files to the revision control by mistake by doing something
f18d244a 17 * like "git update-index *" and suddenly having all the object
121481ab
LT
18 * files be revision controlled.
19 */
caf4f582
JH
20static int allow_add;
21static int allow_remove;
22static int allow_replace;
caf4f582 23static int info_only;
9b63f501 24static int force_remove;
caf4f582 25static int verbose;
96f1e58f 26static int mark_valid_only;
44a36913 27static int mark_skip_worktree_only;
83b327ba
NTND
28#define MARK_FLAG 1
29#define UNMARK_FLAG 2
5f73076c 30
caf4f582
JH
31static void report(const char *fmt, ...)
32{
33 va_list vp;
34
35 if (!verbose)
36 return;
37
38 va_start(vp, fmt);
39 vprintf(fmt, vp);
40 putchar('\n');
41 va_end(vp);
42}
43
83b327ba 44static int mark_ce_flags(const char *path, int flag, int mark)
5f73076c
JH
45{
46 int namelen = strlen(path);
47 int pos = cache_name_pos(path, namelen);
48 if (0 <= pos) {
83b327ba
NTND
49 if (mark)
50 active_cache[pos]->ce_flags |= flag;
51 else
52 active_cache[pos]->ce_flags &= ~flag;
a6e5642f 53 cache_tree_invalidate_path(active_cache_tree, path);
5f73076c
JH
54 active_cache_changed = 1;
55 return 0;
56 }
57 return -1;
58}
59
e011054b 60static int remove_one_path(const char *path)
e83c5163 61{
e011054b
LT
62 if (!allow_remove)
63 return error("%s: does not exist and --remove not passed", path);
64 if (remove_file_from_cache(path))
65 return error("%s: cannot remove from the index", path);
66 return 0;
67}
a6e5642f 68
e011054b
LT
69/*
70 * Handle a path that couldn't be lstat'ed. It's either:
71 * - missing file (ENOENT or ENOTDIR). That's ok if we're
72 * supposed to be removing it and the removal actually
73 * succeeds.
74 * - permission error. That's never ok.
75 */
76static int process_lstat_error(const char *path, int err)
77{
78 if (err == ENOENT || err == ENOTDIR)
79 return remove_one_path(path);
80 return error("lstat(\"%s\"): %s", path, strerror(errno));
81}
a6e5642f 82
e011054b
LT
83static int add_one_path(struct cache_entry *old, const char *path, int len, struct stat *st)
84{
22631473
LT
85 int option, size;
86 struct cache_entry *ce;
3e09cdfd 87
22631473
LT
88 /* Was the old index entry already up-to-date? */
89 if (old && !ce_stage(old) && !ce_match_stat(old, st, 0))
90 return 0;
91
92 size = cache_entry_size(len);
93 ce = xcalloc(1, size);
e011054b 94 memcpy(ce->name, path, len);
7a51ed66 95 ce->ce_flags = len;
e011054b
LT
96 fill_stat_cache_info(ce, st);
97 ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
ec1fcc16 98
e011054b 99 if (index_path(ce->sha1, path, st, !info_only))
ec1fcc16 100 return -1;
192268c1
JH
101 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
102 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
34143b26 103 if (add_cache_entry(ce, option))
e011054b 104 return error("%s: cannot add to the index - missing --add option?", path);
34143b26 105 return 0;
121481ab
LT
106}
107
e011054b
LT
108/*
109 * Handle a path that was a directory. Four cases:
110 *
111 * - it's already a gitlink in the index, and we keep it that
112 * way, and update it if we can (if we cannot find the HEAD,
113 * we're going to keep it unchanged in the index!)
114 *
115 * - it's a *file* in the index, in which case it should be
116 * removed as a file if removal is allowed, since it doesn't
117 * exist as such any more. If removal isn't allowed, it's
118 * an error.
119 *
120 * (NOTE! This is old and arguably fairly strange behaviour.
121 * We might want to make this an error unconditionally, and
122 * use "--force-remove" if you actually want to force removal).
123 *
124 * - it used to exist as a subdirectory (ie multiple files with
125 * this particular prefix) in the index, in which case it's wrong
126 * to try to update it as a directory.
127 *
128 * - it doesn't exist at all in the index, but it is a valid
129 * git directory, and it should be *added* as a gitlink.
130 */
131static int process_directory(const char *path, int len, struct stat *st)
132{
133 unsigned char sha1[20];
134 int pos = cache_name_pos(path, len);
135
136 /* Exact match: file or existing gitlink */
137 if (pos >= 0) {
138 struct cache_entry *ce = active_cache[pos];
7a51ed66 139 if (S_ISGITLINK(ce->ce_mode)) {
e011054b
LT
140
141 /* Do nothing to the index if there is no HEAD! */
142 if (resolve_gitlink_ref(path, "HEAD", sha1) < 0)
143 return 0;
144
145 return add_one_path(ce, path, len, st);
146 }
147 /* Should this be an unconditional error? */
148 return remove_one_path(path);
149 }
150
151 /* Inexact match: is there perhaps a subdirectory match? */
152 pos = -pos-1;
153 while (pos < active_nr) {
154 struct cache_entry *ce = active_cache[pos++];
155
156 if (strncmp(ce->name, path, len))
157 break;
158 if (ce->name[len] > '/')
159 break;
160 if (ce->name[len] < '/')
161 continue;
162
163 /* Subdirectory match - error out */
164 return error("%s: is a directory - add individual files instead", path);
165 }
166
167 /* No match - should we add it as a gitlink? */
168 if (!resolve_gitlink_ref(path, "HEAD", sha1))
169 return add_one_path(NULL, path, len, st);
170
171 /* Error out. */
172 return error("%s: is a directory - add files inside instead", path);
173}
174
e011054b
LT
175static int process_path(const char *path)
176{
b4d1690d 177 int pos, len;
e011054b 178 struct stat st;
b4d1690d 179 struct cache_entry *ce;
e011054b 180
806d13b1 181 len = strlen(path);
57199892 182 if (has_symlink_leading_path(path, len))
806d13b1
JH
183 return error("'%s' is beyond a symbolic link", path);
184
b4d1690d
NTND
185 pos = cache_name_pos(path, len);
186 ce = pos < 0 ? NULL : active_cache[pos];
187 if (ce && ce_skip_worktree(ce)) {
188 /*
189 * working directory version is assumed "good"
190 * so updating it does not make sense.
191 * On the other hand, removing it from index should work
192 */
193 if (allow_remove && remove_file_from_cache(path))
194 return error("%s: cannot remove from the index", path);
195 return 0;
196 }
197
e011054b
LT
198 /*
199 * First things first: get the stat information, to decide
200 * what to do about the pathname!
201 */
202 if (lstat(path, &st) < 0)
203 return process_lstat_error(path, errno);
204
e011054b
LT
205 if (S_ISDIR(st.st_mode))
206 return process_directory(path, len, &st);
207
b4d1690d
NTND
208 /*
209 * Process a regular file
210 */
211 if (ce && S_ISGITLINK(ce->ce_mode))
212 return error("%s is already a gitlink, not replacing", path);
213
214 return add_one_path(ce, path, len, &st);
e011054b
LT
215}
216
d23748a6
JH
217static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
218 const char *path, int stage)
9945d980 219{
192268c1 220 int size, len, option;
9945d980
LT
221 struct cache_entry *ce;
222
d23748a6 223 if (!verify_path(path))
7e7abea9 224 return error("Invalid path '%s'", path);
9945d980 225
d23748a6 226 len = strlen(path);
9945d980 227 size = cache_entry_size(len);
90321c10 228 ce = xcalloc(1, size);
9945d980 229
e702496e 230 hashcpy(ce->sha1, sha1);
d23748a6
JH
231 memcpy(ce->name, path, len);
232 ce->ce_flags = create_ce_flags(len, stage);
e4479470 233 ce->ce_mode = create_ce_mode(mode);
5f73076c 234 if (assume_unchanged)
7a51ed66 235 ce->ce_flags |= CE_VALID;
192268c1
JH
236 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
237 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
caf4f582
JH
238 if (add_cache_entry(ce, option))
239 return error("%s: cannot add to the index - missing --add option?",
d23748a6
JH
240 path);
241 report("add '%s'", path);
caf4f582 242 return 0;
9945d980
LT
243}
244
227bdb18 245static void chmod_path(int flip, const char *path)
3e09cdfd
JH
246{
247 int pos;
248 struct cache_entry *ce;
249 unsigned int mode;
f2a19340 250
3e09cdfd
JH
251 pos = cache_name_pos(path, strlen(path));
252 if (pos < 0)
227bdb18 253 goto fail;
3e09cdfd 254 ce = active_cache[pos];
7a51ed66 255 mode = ce->ce_mode;
3e09cdfd 256 if (!S_ISREG(mode))
227bdb18 257 goto fail;
3e09cdfd
JH
258 switch (flip) {
259 case '+':
7a51ed66 260 ce->ce_mode |= 0111; break;
3e09cdfd 261 case '-':
7a51ed66 262 ce->ce_mode &= ~0111; break;
3e09cdfd 263 default:
227bdb18 264 goto fail;
3e09cdfd 265 }
a6e5642f 266 cache_tree_invalidate_path(active_cache_tree, path);
3e09cdfd 267 active_cache_changed = 1;
227bdb18
AR
268 report("chmod %cx '%s'", flip, path);
269 return;
270 fail:
7e44c935 271 die("git update-index: cannot chmod %cx '%s'", flip, path);
3e09cdfd
JH
272}
273
ee1bec3d
JH
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);
fb69a760 279 goto free_return;
ee1bec3d 280 }
5f73076c 281 if (mark_valid_only) {
83b327ba 282 if (mark_ce_flags(p, CE_VALID, mark_valid_only == MARK_FLAG))
5f73076c 283 die("Unable to mark file %s", path);
fb69a760 284 goto free_return;
5f73076c 285 }
44a36913
NTND
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 }
5f73076c 291
ee1bec3d
JH
292 if (force_remove) {
293 if (remove_file_from_cache(p))
7e44c935 294 die("git update-index: unable to remove %s", path);
caf4f582 295 report("remove '%s'", path);
fb69a760 296 goto free_return;
ee1bec3d 297 }
e011054b
LT
298 if (process_path(p))
299 die("Unable to process path %s", path);
caf4f582 300 report("add '%s'", path);
fb69a760 301 free_return:
0cc9e70c 302 if (p < path || p > path + strlen(path))
4b25d091 303 free((char *)p);
ee1bec3d
JH
304}
305
d4dbf36d
JH
306static void read_index_info(int line_termination)
307{
f285a2d7
BC
308 struct strbuf buf = STRBUF_INIT;
309 struct strbuf uq = STRBUF_INIT;
7fb1011e 310
7fb1011e 311 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
9c20a470 312 char *ptr, *tab;
973d6a20 313 char *path_name;
d4dbf36d
JH
314 unsigned char sha1[20];
315 unsigned int mode;
6aead43d 316 unsigned long ul;
d23748a6
JH
317 int stage;
318
319 /* This reads lines formatted in one of three formats:
320 *
321 * (1) mode SP sha1 TAB path
f18d244a 322 * The first format is what "git apply --index-info"
d23748a6
JH
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
f18d244a 328 * The second format is to stuff "git ls-tree" output
d23748a6 329 * into the index file.
fefe81c9 330 *
d23748a6
JH
331 * (3) mode SP sha1 SP stage TAB path
332 * This format is to put higher order stages into the
f18d244a 333 * index file and matches "git ls-files --stage" output.
d23748a6 334 */
6aead43d
JM
335 errno = 0;
336 ul = strtoul(buf.buf, &ptr, 8);
337 if (ptr == buf.buf || *ptr != ' '
338 || errno || (unsigned int) ul != ul)
d4dbf36d 339 goto bad_line;
6aead43d 340 mode = ul;
d4dbf36d 341
9c20a470
JH
342 tab = strchr(ptr, '\t');
343 if (!tab || tab - ptr < 41)
344 goto bad_line;
d23748a6 345
2d497115 346 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
d23748a6
JH
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
9c20a470
JH
356 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
357 goto bad_line;
973d6a20 358
7fb1011e
PH
359 path_name = ptr;
360 if (line_termination && path_name[0] == '"') {
361 strbuf_reset(&uq);
362 if (unquote_c_style(&uq, path_name, NULL)) {
7e44c935 363 die("git update-index: bad quoting of path name");
7fb1011e
PH
364 }
365 path_name = uq.buf;
366 }
973d6a20
JH
367
368 if (!verify_path(path_name)) {
369 fprintf(stderr, "Ignoring path %s\n", path_name);
d4dbf36d
JH
370 continue;
371 }
372
373 if (!mode) {
374 /* mode == 0 means there is no such path -- remove */
973d6a20 375 if (remove_file_from_cache(path_name))
7e44c935 376 die("git update-index: unable to remove %s",
d4dbf36d
JH
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;
d23748a6 385 if (add_cacheinfo(mode, sha1, path_name, stage))
7e44c935 386 die("git update-index: unable to update %s",
973d6a20 387 path_name);
d4dbf36d
JH
388 }
389 continue;
390
391 bad_line:
392 die("malformed index info %s", buf.buf);
393 }
e6c019d0 394 strbuf_release(&buf);
7fb1011e 395 strbuf_release(&uq);
d4dbf36d
JH
396}
397
f6ab5bb2 398static const char update_index_usage[] =
44a36913 399"git update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--really-refresh] [--cacheinfo] [--chmod=(+|-)x] [--assume-unchanged] [--skip-worktree|--no-skip-worktree] [--info-only] [--force-remove] [--stdin] [--index-info] [--unresolve] [--again | -g] [--ignore-missing] [-z] [--verbose] [--] <file>...";
f6ab5bb2 400
2bd452d3
JH
401static unsigned char head_sha1[20];
402static unsigned char merge_head_sha1[20];
403
404static struct cache_entry *read_one_ent(const char *which,
405 unsigned char *ent, const char *path,
406 int namelen, int stage)
407{
408 unsigned mode;
409 unsigned char sha1[20];
410 int size;
411 struct cache_entry *ce;
412
413 if (get_tree_entry(ent, path, sha1, &mode)) {
83e77a25
JH
414 if (which)
415 error("%s: not in %s branch.", path, which);
2bd452d3
JH
416 return NULL;
417 }
418 if (mode == S_IFDIR) {
83e77a25
JH
419 if (which)
420 error("%s: not a blob in %s branch.", path, which);
2bd452d3
JH
421 return NULL;
422 }
423 size = cache_entry_size(namelen);
424 ce = xcalloc(1, size);
425
e702496e 426 hashcpy(ce->sha1, sha1);
2bd452d3
JH
427 memcpy(ce->name, path, namelen);
428 ce->ce_flags = create_ce_flags(namelen, stage);
429 ce->ce_mode = create_ce_mode(mode);
430 return ce;
431}
432
433static int unresolve_one(const char *path)
434{
435 int namelen = strlen(path);
436 int pos;
437 int ret = 0;
438 struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
439
440 /* See if there is such entry in the index. */
441 pos = cache_name_pos(path, namelen);
442 if (pos < 0) {
443 /* If there isn't, either it is unmerged, or
444 * resolved as "removed" by mistake. We do not
445 * want to do anything in the former case.
446 */
447 pos = -pos-1;
448 if (pos < active_nr) {
449 struct cache_entry *ce = active_cache[pos];
450 if (ce_namelen(ce) == namelen &&
451 !memcmp(ce->name, path, namelen)) {
452 fprintf(stderr,
453 "%s: skipping still unmerged path.\n",
454 path);
455 goto free_return;
456 }
457 }
458 }
459
460 /* Grab blobs from given path from HEAD and MERGE_HEAD,
461 * stuff HEAD version in stage #2,
462 * stuff MERGE_HEAD version in stage #3.
463 */
464 ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
465 ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);
466
467 if (!ce_2 || !ce_3) {
468 ret = -1;
469 goto free_return;
470 }
a89fccd2 471 if (!hashcmp(ce_2->sha1, ce_3->sha1) &&
2bd452d3
JH
472 ce_2->ce_mode == ce_3->ce_mode) {
473 fprintf(stderr, "%s: identical in both, skipping.\n",
474 path);
475 goto free_return;
476 }
477
478 remove_file_from_cache(path);
479 if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
480 error("%s: cannot add our version to the index.", path);
481 ret = -1;
482 goto free_return;
483 }
484 if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
485 return 0;
486 error("%s: cannot add their version to the index.", path);
487 ret = -1;
488 free_return:
489 free(ce_2);
490 free(ce_3);
491 return ret;
492}
493
494static void read_head_pointers(void)
495{
913c983e 496 if (read_ref("HEAD", head_sha1))
d7530708 497 die("No HEAD -- no initial commit yet?");
913c983e 498 if (read_ref("MERGE_HEAD", merge_head_sha1)) {
2bd452d3
JH
499 fprintf(stderr, "Not in the middle of a merge.\n");
500 exit(0);
501 }
502}
503
09895c1f
JH
504static int do_unresolve(int ac, const char **av,
505 const char *prefix, int prefix_length)
2bd452d3
JH
506{
507 int i;
508 int err = 0;
509
510 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
511 * are not doing a merge, so exit with success status.
512 */
513 read_head_pointers();
514
515 for (i = 1; i < ac; i++) {
516 const char *arg = av[i];
09895c1f
JH
517 const char *p = prefix_path(prefix, prefix_length, arg);
518 err |= unresolve_one(p);
0cc9e70c 519 if (p < arg || p > arg + strlen(arg))
4b25d091 520 free((char *)p);
2bd452d3
JH
521 }
522 return err;
523}
524
83e77a25
JH
525static int do_reupdate(int ac, const char **av,
526 const char *prefix, int prefix_length)
527{
528 /* Read HEAD and run update-index on paths that are
529 * merged and already different between index and HEAD.
530 */
531 int pos;
532 int has_head = 1;
0cc9e70c 533 const char **pathspec = get_pathspec(prefix, av + 1);
83e77a25 534
913c983e 535 if (read_ref("HEAD", head_sha1))
83e77a25
JH
536 /* If there is no HEAD, that means it is an initial
537 * commit. Update everything in the index.
538 */
539 has_head = 0;
540 redo:
541 for (pos = 0; pos < active_nr; pos++) {
542 struct cache_entry *ce = active_cache[pos];
543 struct cache_entry *old = NULL;
544 int save_nr;
22293b9c
JH
545
546 if (ce_stage(ce) || !ce_path_match(ce, pathspec))
83e77a25
JH
547 continue;
548 if (has_head)
549 old = read_one_ent(NULL, head_sha1,
550 ce->name, ce_namelen(ce), 0);
551 if (old && ce->ce_mode == old->ce_mode &&
a89fccd2 552 !hashcmp(ce->sha1, old->sha1)) {
83e77a25
JH
553 free(old);
554 continue; /* unchanged */
555 }
556 /* Be careful. The working tree may not have the
557 * path anymore, in which case, under 'allow_remove',
558 * or worse yet 'allow_replace', active_nr may decrease.
559 */
560 save_nr = active_nr;
561 update_one(ce->name + prefix_length, prefix, prefix_length);
562 if (save_nr != active_nr)
563 goto redo;
564 }
565 return 0;
566}
567
a633fca0 568int cmd_update_index(int argc, const char **argv, const char *prefix)
e83c5163 569{
ee1bec3d 570 int i, newfd, entries, has_errors = 0, line_termination = '\n';
121481ab 571 int allow_options = 1;
ee1bec3d 572 int read_from_stdin = 0;
ee1bec3d 573 int prefix_length = prefix ? strlen(prefix) : 0;
227bdb18 574 char set_executable_bit = 0;
405e5b2f 575 unsigned int refresh_flags = 0;
7b802b86 576 int lock_error = 0;
fefe81c9 577 struct lock_file *lock_file;
bb233d69 578
ef90d6d4 579 git_config(git_default_config, NULL);
3e09cdfd 580
fefe81c9 581 /* We can't free this memory, it becomes part of a linked list parsed atexit() */
928e47e3 582 lock_file = xcalloc(1, sizeof(struct lock_file));
fefe81c9 583
30ca07a2 584 newfd = hold_locked_index(lock_file, 0);
7b802b86
JH
585 if (newfd < 0)
586 lock_error = errno;
9614b8dc 587
e83c5163 588 entries = read_cache();
9614b8dc 589 if (entries < 0)
2de381f9 590 die("cache corrupted");
e83c5163 591
e83c5163 592 for (i = 1 ; i < argc; i++) {
6b5ee137 593 const char *path = argv[i];
9ebe6cf9 594 const char *p;
121481ab
LT
595
596 if (allow_options && *path == '-') {
597 if (!strcmp(path, "--")) {
598 allow_options = 0;
599 continue;
600 }
0ed3715f 601 if (!strcmp(path, "-q")) {
405e5b2f 602 refresh_flags |= REFRESH_QUIET;
0ed3715f
LT
603 continue;
604 }
5fdeacb0
JS
605 if (!strcmp(path, "--ignore-submodules")) {
606 refresh_flags |= REFRESH_IGNORE_SUBMODULES;
607 continue;
608 }
121481ab
LT
609 if (!strcmp(path, "--add")) {
610 allow_add = 1;
611 continue;
612 }
192268c1
JH
613 if (!strcmp(path, "--replace")) {
614 allow_replace = 1;
615 continue;
616 }
121481ab
LT
617 if (!strcmp(path, "--remove")) {
618 allow_remove = 1;
619 continue;
620 }
5d1a5c02 621 if (!strcmp(path, "--unmerged")) {
405e5b2f 622 refresh_flags |= REFRESH_UNMERGED;
5d1a5c02
LT
623 continue;
624 }
121481ab 625 if (!strcmp(path, "--refresh")) {
f83eafdd 626 setup_work_tree();
405e5b2f 627 has_errors |= refresh_cache(refresh_flags);
5f73076c
JH
628 continue;
629 }
630 if (!strcmp(path, "--really-refresh")) {
f83eafdd 631 setup_work_tree();
405e5b2f 632 has_errors |= refresh_cache(REFRESH_REALLY | refresh_flags);
121481ab
LT
633 continue;
634 }
9945d980 635 if (!strcmp(path, "--cacheinfo")) {
d23748a6
JH
636 unsigned char sha1[20];
637 unsigned int mode;
638
b3f94c4b 639 if (i+3 >= argc)
7e44c935 640 die("git update-index: --cacheinfo <mode> <sha1> <path>");
d23748a6 641
6e6db39a 642 if (strtoul_ui(argv[i+1], 8, &mode) ||
d23748a6
JH
643 get_sha1_hex(argv[i+2], sha1) ||
644 add_cacheinfo(mode, sha1, argv[i+3], 0))
7e44c935 645 die("git update-index: --cacheinfo"
d23748a6 646 " cannot add %s", argv[i+3]);
9945d980
LT
647 i += 3;
648 continue;
649 }
3e09cdfd
JH
650 if (!strcmp(path, "--chmod=-x") ||
651 !strcmp(path, "--chmod=+x")) {
652 if (argc <= i+1)
7e44c935 653 die("git update-index: %s <path>", path);
227bdb18 654 set_executable_bit = path[8];
3e09cdfd
JH
655 continue;
656 }
5f73076c 657 if (!strcmp(path, "--assume-unchanged")) {
83b327ba 658 mark_valid_only = MARK_FLAG;
5f73076c
JH
659 continue;
660 }
661 if (!strcmp(path, "--no-assume-unchanged")) {
83b327ba 662 mark_valid_only = UNMARK_FLAG;
5f73076c
JH
663 continue;
664 }
44a36913
NTND
665 if (!strcmp(path, "--no-skip-worktree")) {
666 mark_skip_worktree_only = UNMARK_FLAG;
667 continue;
668 }
669 if (!strcmp(path, "--skip-worktree")) {
670 mark_skip_worktree_only = MARK_FLAG;
671 continue;
672 }
df6e1516
BL
673 if (!strcmp(path, "--info-only")) {
674 info_only = 1;
675 continue;
676 }
0ff5bf7c 677 if (!strcmp(path, "--force-remove")) {
9b63f501 678 force_remove = 1;
0ff5bf7c
JH
679 continue;
680 }
ee1bec3d
JH
681 if (!strcmp(path, "-z")) {
682 line_termination = 0;
683 continue;
684 }
685 if (!strcmp(path, "--stdin")) {
686 if (i != argc - 1)
687 die("--stdin must be at the end");
688 read_from_stdin = 1;
689 break;
690 }
d4dbf36d 691 if (!strcmp(path, "--index-info")) {
a41c175d
SP
692 if (i != argc - 1)
693 die("--index-info must be at the end");
d4dbf36d
JH
694 allow_add = allow_replace = allow_remove = 1;
695 read_index_info(line_termination);
a41c175d 696 break;
d4dbf36d 697 }
2bd452d3 698 if (!strcmp(path, "--unresolve")) {
09895c1f
JH
699 has_errors = do_unresolve(argc - i, argv + i,
700 prefix, prefix_length);
2bd452d3
JH
701 if (has_errors)
702 active_cache_changed = 0;
703 goto finish;
704 }
7099c9c7 705 if (!strcmp(path, "--again") || !strcmp(path, "-g")) {
f83eafdd 706 setup_work_tree();
83e77a25
JH
707 has_errors = do_reupdate(argc - i, argv + i,
708 prefix, prefix_length);
709 if (has_errors)
710 active_cache_changed = 0;
711 goto finish;
712 }
c6e007b0 713 if (!strcmp(path, "--ignore-missing")) {
405e5b2f 714 refresh_flags |= REFRESH_IGNORE_MISSING;
c6e007b0
JB
715 continue;
716 }
caf4f582
JH
717 if (!strcmp(path, "--verbose")) {
718 verbose = 1;
719 continue;
720 }
f6ab5bb2
PB
721 if (!strcmp(path, "-h") || !strcmp(path, "--help"))
722 usage(update_index_usage);
2de381f9 723 die("unknown option %s", path);
121481ab 724 }
f83eafdd 725 setup_work_tree();
9ebe6cf9
AR
726 p = prefix_path(prefix, prefix_length, path);
727 update_one(p, NULL, 0);
227bdb18 728 if (set_executable_bit)
9ebe6cf9
AR
729 chmod_path(set_executable_bit, p);
730 if (p < path || p > path + strlen(path))
4b25d091 731 free((char *)p);
ee1bec3d
JH
732 }
733 if (read_from_stdin) {
f285a2d7 734 struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
7fb1011e 735
f83eafdd 736 setup_work_tree();
7fb1011e 737 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
fb69a760 738 const char *p;
7fb1011e
PH
739 if (line_termination && buf.buf[0] == '"') {
740 strbuf_reset(&nbuf);
741 if (unquote_c_style(&nbuf, buf.buf, NULL))
742 die("line is badly quoted");
743 strbuf_swap(&buf, &nbuf);
744 }
745 p = prefix_path(prefix, prefix_length, buf.buf);
fb69a760
JH
746 update_one(p, NULL, 0);
747 if (set_executable_bit)
227bdb18 748 chmod_path(set_executable_bit, p);
7fb1011e
PH
749 if (p < buf.buf || p > buf.buf + buf.len)
750 free((char *)p);
9b63f501 751 }
7fb1011e 752 strbuf_release(&nbuf);
e6c019d0 753 strbuf_release(&buf);
e83c5163 754 }
2bd452d3
JH
755
756 finish:
5cd5ace7 757 if (active_cache_changed) {
7b802b86
JH
758 if (newfd < 0) {
759 if (refresh_flags & REFRESH_QUIET)
760 exit(128);
e43a6fd3 761 unable_to_lock_index_die(get_index_file(), lock_error);
7b802b86 762 }
5cd5ace7 763 if (write_cache(newfd, active_cache, active_nr) ||
4ed7cd3a 764 commit_locked_index(lock_file))
021b6e45 765 die("Unable to write new index file");
5cd5ace7 766 }
9614b8dc 767
fefe81c9
LS
768 rollback_lock_file(lock_file);
769
c4b83e61 770 return has_errors ? 1 : 0;
e83c5163 771}