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