]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/update-index.c
update-index: use unmerge_index_entry() to support removal
[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 */
babed893 6#define USE_THE_INDEX_VARIABLE
e83c5163 7#include "cache.h"
23a3a303 8#include "bulk-checkin.h"
b2141fc1 9#include "config.h"
32a8f510 10#include "environment.h"
f394e093 11#include "gettext.h"
d1cbe1e6 12#include "hash.h"
41771fa4 13#include "hex.h"
697cc8ef 14#include "lockfile.h"
973d6a20 15#include "quote.h"
a6e5642f 16#include "cache-tree.h"
2bd452d3 17#include "tree-walk.h"
fefe81c9 18#include "builtin.h"
87bed179 19#include "object-file.h"
e011054b 20#include "refs.h"
4a39f79d 21#include "resolve-undo.h"
309be813 22#include "parse-options.h"
64acde94 23#include "pathspec.h"
429bb40a 24#include "dir.h"
d1cbe1e6 25#include "repository.h"
e38da487 26#include "setup.h"
c18b80a0 27#include "split-index.h"
cb2a5135 28#include "symlinks.h"
883e248b 29#include "fsmonitor.h"
d48be35c 30#include "write-or-die.h"
e83c5163 31
121481ab
LT
32/*
33 * Default to not allowing changes to the list of files. The
34 * tool doesn't actually care, but this makes it harder to add
35 * files to the revision control by mistake by doing something
f18d244a 36 * like "git update-index *" and suddenly having all the object
121481ab
LT
37 * files be revision controlled.
38 */
caf4f582
JH
39static int allow_add;
40static int allow_remove;
41static int allow_replace;
caf4f582 42static int info_only;
9b63f501 43static int force_remove;
caf4f582 44static int verbose;
96f1e58f 45static int mark_valid_only;
44a36913 46static int mark_skip_worktree_only;
9d406cba 47static int mark_fsmonitor_only;
8dfb04ae 48static int ignore_skip_worktree_entries;
83b327ba
NTND
49#define MARK_FLAG 1
50#define UNMARK_FLAG 2
f64cb88d 51static struct strbuf mtime_dir = STRBUF_INIT;
5f73076c 52
113e6413
CC
53/* Untracked cache mode */
54enum uc_mode {
55 UC_UNSPECIFIED = -1,
56 UC_DISABLE = 0,
57 UC_ENABLE,
eaab83d0 58 UC_TEST,
113e6413
CC
59 UC_FORCE
60};
61
28bea9e5 62__attribute__((format (printf, 1, 2)))
caf4f582
JH
63static void report(const char *fmt, ...)
64{
65 va_list vp;
66
67 if (!verbose)
68 return;
69
23a3a303
NS
70 /*
71 * It is possible, though unlikely, that a caller could use the verbose
72 * output to synchronize with addition of objects to the object
73 * database. The current implementation of ODB transactions leaves
74 * objects invisible while a transaction is active, so flush the
75 * transaction here before reporting a change made by update-index.
76 */
77 flush_odb_transaction();
caf4f582
JH
78 va_start(vp, fmt);
79 vprintf(fmt, vp);
80 putchar('\n');
81 va_end(vp);
82}
83
f64cb88d
NTND
84static void remove_test_directory(void)
85{
86 if (mtime_dir.len)
87 remove_dir_recursively(&mtime_dir, 0);
88}
89
90static const char *get_mtime_path(const char *path)
91{
92 static struct strbuf sb = STRBUF_INIT;
93 strbuf_reset(&sb);
94 strbuf_addf(&sb, "%s/%s", mtime_dir.buf, path);
95 return sb.buf;
96}
97
98static void xmkdir(const char *path)
99{
100 path = get_mtime_path(path);
101 if (mkdir(path, 0700))
102 die_errno(_("failed to create directory %s"), path);
103}
104
105static int xstat_mtime_dir(struct stat *st)
106{
107 if (stat(mtime_dir.buf, st))
108 die_errno(_("failed to stat %s"), mtime_dir.buf);
109 return 0;
110}
111
112static int create_file(const char *path)
113{
114 int fd;
115 path = get_mtime_path(path);
66e905b7 116 fd = xopen(path, O_CREAT | O_RDWR, 0644);
f64cb88d
NTND
117 return fd;
118}
119
120static void xunlink(const char *path)
121{
122 path = get_mtime_path(path);
123 if (unlink(path))
124 die_errno(_("failed to delete file %s"), path);
125}
126
127static void xrmdir(const char *path)
128{
129 path = get_mtime_path(path);
130 if (rmdir(path))
131 die_errno(_("failed to delete directory %s"), path);
132}
133
134static void avoid_racy(void)
135{
136 /*
137 * not use if we could usleep(10) if USE_NSEC is defined. The
138 * field nsec could be there, but the OS could choose to
139 * ignore it?
140 */
141 sleep(1);
142}
143
144static int test_if_untracked_cache_is_supported(void)
145{
146 struct stat st;
147 struct stat_data base;
148 int fd, ret = 0;
c105f563 149 char *cwd;
f64cb88d
NTND
150
151 strbuf_addstr(&mtime_dir, "mtime-test-XXXXXX");
152 if (!mkdtemp(mtime_dir.buf))
153 die_errno("Could not make temporary directory");
154
c105f563
CC
155 cwd = xgetcwd();
156 fprintf(stderr, _("Testing mtime in '%s' "), cwd);
157 free(cwd);
158
f64cb88d
NTND
159 atexit(remove_test_directory);
160 xstat_mtime_dir(&st);
161 fill_stat_data(&base, &st);
162 fputc('.', stderr);
163
164 avoid_racy();
165 fd = create_file("newfile");
166 xstat_mtime_dir(&st);
167 if (!match_stat_data(&base, &st)) {
168 close(fd);
169 fputc('\n', stderr);
170 fprintf_ln(stderr,_("directory stat info does not "
171 "change after adding a new file"));
172 goto done;
173 }
174 fill_stat_data(&base, &st);
175 fputc('.', stderr);
176
177 avoid_racy();
178 xmkdir("new-dir");
179 xstat_mtime_dir(&st);
180 if (!match_stat_data(&base, &st)) {
181 close(fd);
182 fputc('\n', stderr);
183 fprintf_ln(stderr, _("directory stat info does not change "
184 "after adding a new directory"));
185 goto done;
186 }
187 fill_stat_data(&base, &st);
188 fputc('.', stderr);
189
190 avoid_racy();
191 write_or_die(fd, "data", 4);
192 close(fd);
193 xstat_mtime_dir(&st);
194 if (match_stat_data(&base, &st)) {
195 fputc('\n', stderr);
196 fprintf_ln(stderr, _("directory stat info changes "
197 "after updating a file"));
198 goto done;
199 }
200 fputc('.', stderr);
201
202 avoid_racy();
203 close(create_file("new-dir/new"));
204 xstat_mtime_dir(&st);
205 if (match_stat_data(&base, &st)) {
206 fputc('\n', stderr);
207 fprintf_ln(stderr, _("directory stat info changes after "
208 "adding a file inside subdirectory"));
209 goto done;
210 }
211 fputc('.', stderr);
212
213 avoid_racy();
214 xunlink("newfile");
215 xstat_mtime_dir(&st);
216 if (!match_stat_data(&base, &st)) {
217 fputc('\n', stderr);
218 fprintf_ln(stderr, _("directory stat info does not "
219 "change after deleting a file"));
220 goto done;
221 }
222 fill_stat_data(&base, &st);
223 fputc('.', stderr);
224
225 avoid_racy();
226 xunlink("new-dir/new");
227 xrmdir("new-dir");
228 xstat_mtime_dir(&st);
229 if (!match_stat_data(&base, &st)) {
230 fputc('\n', stderr);
231 fprintf_ln(stderr, _("directory stat info does not "
232 "change after deleting a directory"));
233 goto done;
234 }
235
236 if (rmdir(mtime_dir.buf))
237 die_errno(_("failed to delete directory %s"), mtime_dir.buf);
238 fprintf_ln(stderr, _(" OK"));
239 ret = 1;
240
241done:
242 strbuf_release(&mtime_dir);
243 return ret;
244}
245
83b327ba 246static int mark_ce_flags(const char *path, int flag, int mark)
5f73076c
JH
247{
248 int namelen = strlen(path);
07047d68 249 int pos = index_name_pos(&the_index, path, namelen);
5f73076c 250 if (0 <= pos) {
dc594180 251 mark_fsmonitor_invalid(&the_index, the_index.cache[pos]);
83b327ba 252 if (mark)
dc594180 253 the_index.cache[pos]->ce_flags |= flag;
83b327ba 254 else
dc594180
ÆAB
255 the_index.cache[pos]->ce_flags &= ~flag;
256 the_index.cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
a5400efe 257 cache_tree_invalidate_path(&the_index, path);
dc594180 258 the_index.cache_changed |= CE_ENTRY_CHANGED;
5f73076c
JH
259 return 0;
260 }
261 return -1;
262}
263
e011054b 264static int remove_one_path(const char *path)
e83c5163 265{
e011054b
LT
266 if (!allow_remove)
267 return error("%s: does not exist and --remove not passed", path);
031b2033 268 if (remove_file_from_index(&the_index, path))
e011054b
LT
269 return error("%s: cannot remove from the index", path);
270 return 0;
271}
a6e5642f 272
e011054b
LT
273/*
274 * Handle a path that couldn't be lstat'ed. It's either:
275 * - missing file (ENOENT or ENOTDIR). That's ok if we're
276 * supposed to be removing it and the removal actually
277 * succeeds.
278 * - permission error. That's never ok.
279 */
280static int process_lstat_error(const char *path, int err)
281{
c7054209 282 if (is_missing_file_error(err))
e011054b 283 return remove_one_path(path);
23d05364 284 return error("lstat(\"%s\"): %s", path, strerror(err));
e011054b 285}
a6e5642f 286
9c5e6c80 287static int add_one_path(const struct cache_entry *old, const char *path, int len, struct stat *st)
e011054b 288{
a849735b 289 int option;
22631473 290 struct cache_entry *ce;
3e09cdfd 291
22631473 292 /* Was the old index entry already up-to-date? */
031b2033 293 if (old && !ce_stage(old) && !ie_match_stat(&the_index, old, st, 0))
22631473
LT
294 return 0;
295
a849735b 296 ce = make_empty_cache_entry(&the_index, len);
e011054b 297 memcpy(ce->name, path, len);
b60e188c
TG
298 ce->ce_flags = create_ce_flags(0);
299 ce->ce_namelen = len;
d4c0a3ac 300 fill_stat_cache_info(&the_index, ce, st);
e011054b 301 ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
ec1fcc16 302
58bf2a4c 303 if (index_path(&the_index, &ce->oid, path, st,
dc4cd767 304 info_only ? 0 : HASH_WRITE_OBJECT)) {
a849735b 305 discard_cache_entry(ce);
ec1fcc16 306 return -1;
dc4cd767 307 }
192268c1
JH
308 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
309 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
031b2033 310 if (add_index_entry(&the_index, ce, option)) {
a849735b 311 discard_cache_entry(ce);
e011054b 312 return error("%s: cannot add to the index - missing --add option?", path);
baddc96b 313 }
34143b26 314 return 0;
121481ab
LT
315}
316
e011054b
LT
317/*
318 * Handle a path that was a directory. Four cases:
319 *
320 * - it's already a gitlink in the index, and we keep it that
321 * way, and update it if we can (if we cannot find the HEAD,
322 * we're going to keep it unchanged in the index!)
323 *
324 * - it's a *file* in the index, in which case it should be
325 * removed as a file if removal is allowed, since it doesn't
326 * exist as such any more. If removal isn't allowed, it's
327 * an error.
328 *
329 * (NOTE! This is old and arguably fairly strange behaviour.
330 * We might want to make this an error unconditionally, and
331 * use "--force-remove" if you actually want to force removal).
332 *
333 * - it used to exist as a subdirectory (ie multiple files with
334 * this particular prefix) in the index, in which case it's wrong
335 * to try to update it as a directory.
336 *
337 * - it doesn't exist at all in the index, but it is a valid
338 * git directory, and it should be *added* as a gitlink.
339 */
340static int process_directory(const char *path, int len, struct stat *st)
341{
71445a0f 342 struct object_id oid;
07047d68 343 int pos = index_name_pos(&the_index, path, len);
e011054b
LT
344
345 /* Exact match: file or existing gitlink */
346 if (pos >= 0) {
dc594180 347 const struct cache_entry *ce = the_index.cache[pos];
7a51ed66 348 if (S_ISGITLINK(ce->ce_mode)) {
e011054b
LT
349
350 /* Do nothing to the index if there is no HEAD! */
a98e6101 351 if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
e011054b
LT
352 return 0;
353
354 return add_one_path(ce, path, len, st);
355 }
356 /* Should this be an unconditional error? */
357 return remove_one_path(path);
358 }
359
360 /* Inexact match: is there perhaps a subdirectory match? */
361 pos = -pos-1;
dc594180
ÆAB
362 while (pos < the_index.cache_nr) {
363 const struct cache_entry *ce = the_index.cache[pos++];
e011054b
LT
364
365 if (strncmp(ce->name, path, len))
366 break;
367 if (ce->name[len] > '/')
368 break;
369 if (ce->name[len] < '/')
370 continue;
371
372 /* Subdirectory match - error out */
373 return error("%s: is a directory - add individual files instead", path);
374 }
375
376 /* No match - should we add it as a gitlink? */
a98e6101 377 if (!resolve_gitlink_ref(path, "HEAD", &oid))
e011054b
LT
378 return add_one_path(NULL, path, len, st);
379
380 /* Error out. */
381 return error("%s: is a directory - add files inside instead", path);
382}
383
eb12dd0c 384static int process_path(const char *path, struct stat *st, int stat_errno)
e011054b 385{
b4d1690d 386 int pos, len;
9c5e6c80 387 const struct cache_entry *ce;
e011054b 388
806d13b1 389 len = strlen(path);
57199892 390 if (has_symlink_leading_path(path, len))
806d13b1
JH
391 return error("'%s' is beyond a symbolic link", path);
392
babed893 393 pos = index_name_pos(&the_index, path, len);
dc594180 394 ce = pos < 0 ? NULL : the_index.cache[pos];
b4d1690d
NTND
395 if (ce && ce_skip_worktree(ce)) {
396 /*
397 * working directory version is assumed "good"
398 * so updating it does not make sense.
399 * On the other hand, removing it from index should work
400 */
8dfb04ae 401 if (!ignore_skip_worktree_entries && allow_remove &&
031b2033 402 remove_file_from_index(&the_index, path))
b4d1690d
NTND
403 return error("%s: cannot remove from the index", path);
404 return 0;
405 }
406
e011054b
LT
407 /*
408 * First things first: get the stat information, to decide
409 * what to do about the pathname!
410 */
eb12dd0c
JK
411 if (stat_errno)
412 return process_lstat_error(path, stat_errno);
e011054b 413
eb12dd0c
JK
414 if (S_ISDIR(st->st_mode))
415 return process_directory(path, len, st);
e011054b 416
eb12dd0c 417 return add_one_path(ce, path, len, st);
e011054b
LT
418}
419
71445a0f 420static int add_cacheinfo(unsigned int mode, const struct object_id *oid,
d23748a6 421 const char *path, int stage)
9945d980 422{
a849735b 423 int len, option;
9945d980
LT
424 struct cache_entry *ce;
425
10ecfa76 426 if (!verify_path(path, mode))
7e7abea9 427 return error("Invalid path '%s'", path);
9945d980 428
d23748a6 429 len = strlen(path);
a849735b 430 ce = make_empty_cache_entry(&the_index, len);
9945d980 431
71445a0f 432 oidcpy(&ce->oid, oid);
d23748a6 433 memcpy(ce->name, path, len);
b60e188c
TG
434 ce->ce_flags = create_ce_flags(stage);
435 ce->ce_namelen = len;
e4479470 436 ce->ce_mode = create_ce_mode(mode);
5f73076c 437 if (assume_unchanged)
7a51ed66 438 ce->ce_flags |= CE_VALID;
192268c1
JH
439 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
440 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
031b2033 441 if (add_index_entry(&the_index, ce, option))
caf4f582 442 return error("%s: cannot add to the index - missing --add option?",
d23748a6
JH
443 path);
444 report("add '%s'", path);
caf4f582 445 return 0;
9945d980
LT
446}
447
22433ce4 448static void chmod_path(char flip, const char *path)
3e09cdfd
JH
449{
450 int pos;
451 struct cache_entry *ce;
f2a19340 452
07047d68 453 pos = index_name_pos(&the_index, path, strlen(path));
3e09cdfd 454 if (pos < 0)
227bdb18 455 goto fail;
dc594180 456 ce = the_index.cache[pos];
fbc1ed62 457 if (chmod_index_entry(&the_index, ce, flip) < 0)
227bdb18 458 goto fail;
d9d70966 459
227bdb18
AR
460 report("chmod %cx '%s'", flip, path);
461 return;
462 fail:
7e44c935 463 die("git update-index: cannot chmod %cx '%s'", flip, path);
3e09cdfd
JH
464}
465
6bb69077 466static void update_one(const char *path)
ee1bec3d 467{
eb12dd0c
JK
468 int stat_errno = 0;
469 struct stat st;
470
68f95b26
JH
471 if (mark_valid_only || mark_skip_worktree_only || force_remove ||
472 mark_fsmonitor_only)
eb12dd0c
JK
473 st.st_mode = 0;
474 else if (lstat(path, &st) < 0) {
475 st.st_mode = 0;
476 stat_errno = errno;
477 } /* else stat is valid */
478
10ecfa76 479 if (!verify_path(path, st.st_mode)) {
ee1bec3d 480 fprintf(stderr, "Ignoring path %s\n", path);
6bb69077 481 return;
ee1bec3d 482 }
5f73076c 483 if (mark_valid_only) {
6bb69077 484 if (mark_ce_flags(path, CE_VALID, mark_valid_only == MARK_FLAG))
5f73076c 485 die("Unable to mark file %s", path);
6bb69077 486 return;
5f73076c 487 }
44a36913 488 if (mark_skip_worktree_only) {
6bb69077 489 if (mark_ce_flags(path, CE_SKIP_WORKTREE, mark_skip_worktree_only == MARK_FLAG))
5f73076c 490 die("Unable to mark file %s", path);
6bb69077 491 return;
5f73076c 492 }
9d406cba
BP
493 if (mark_fsmonitor_only) {
494 if (mark_ce_flags(path, CE_FSMONITOR_VALID, mark_fsmonitor_only == MARK_FLAG))
495 die("Unable to mark file %s", path);
496 return;
497 }
5f73076c 498
ee1bec3d 499 if (force_remove) {
031b2033 500 if (remove_file_from_index(&the_index, path))
7e44c935 501 die("git update-index: unable to remove %s", path);
caf4f582 502 report("remove '%s'", path);
6bb69077 503 return;
ee1bec3d 504 }
eb12dd0c 505 if (process_path(path, &st, stat_errno))
e011054b 506 die("Unable to process path %s", path);
caf4f582 507 report("add '%s'", path);
ee1bec3d
JH
508}
509
7e07ed84 510static void read_index_info(int nul_term_line)
d4dbf36d 511{
1928c944 512 const int hexsz = the_hash_algo->hexsz;
f285a2d7
BC
513 struct strbuf buf = STRBUF_INIT;
514 struct strbuf uq = STRBUF_INIT;
7e07ed84 515 strbuf_getline_fn getline_fn;
7fb1011e 516
7e07ed84
JH
517 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
518 while (getline_fn(&buf, stdin) != EOF) {
9c20a470 519 char *ptr, *tab;
973d6a20 520 char *path_name;
71445a0f 521 struct object_id oid;
d4dbf36d 522 unsigned int mode;
6aead43d 523 unsigned long ul;
d23748a6
JH
524 int stage;
525
526 /* This reads lines formatted in one of three formats:
527 *
528 * (1) mode SP sha1 TAB path
f18d244a 529 * The first format is what "git apply --index-info"
d23748a6
JH
530 * reports, and used to reconstruct a partial tree
531 * that is used for phony merge base tree when falling
532 * back on 3-way merge.
533 *
534 * (2) mode SP type SP sha1 TAB path
f18d244a 535 * The second format is to stuff "git ls-tree" output
d23748a6 536 * into the index file.
fefe81c9 537 *
d23748a6
JH
538 * (3) mode SP sha1 SP stage TAB path
539 * This format is to put higher order stages into the
f18d244a 540 * index file and matches "git ls-files --stage" output.
d23748a6 541 */
6aead43d
JM
542 errno = 0;
543 ul = strtoul(buf.buf, &ptr, 8);
544 if (ptr == buf.buf || *ptr != ' '
545 || errno || (unsigned int) ul != ul)
d4dbf36d 546 goto bad_line;
6aead43d 547 mode = ul;
d4dbf36d 548
9c20a470 549 tab = strchr(ptr, '\t');
1928c944 550 if (!tab || tab - ptr < hexsz + 1)
9c20a470 551 goto bad_line;
d23748a6 552
2d497115 553 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
d23748a6
JH
554 stage = tab[-1] - '0';
555 ptr = tab + 1; /* point at the head of path */
556 tab = tab - 2; /* point at tail of sha1 */
557 }
558 else {
559 stage = 0;
560 ptr = tab + 1; /* point at the head of path */
561 }
562
1928c944 563 if (get_oid_hex(tab - hexsz, &oid) ||
564 tab[-(hexsz + 1)] != ' ')
9c20a470 565 goto bad_line;
973d6a20 566
7fb1011e 567 path_name = ptr;
7e07ed84 568 if (!nul_term_line && path_name[0] == '"') {
7fb1011e
PH
569 strbuf_reset(&uq);
570 if (unquote_c_style(&uq, path_name, NULL)) {
7e44c935 571 die("git update-index: bad quoting of path name");
7fb1011e
PH
572 }
573 path_name = uq.buf;
574 }
973d6a20 575
10ecfa76 576 if (!verify_path(path_name, mode)) {
973d6a20 577 fprintf(stderr, "Ignoring path %s\n", path_name);
d4dbf36d
JH
578 continue;
579 }
580
581 if (!mode) {
582 /* mode == 0 means there is no such path -- remove */
031b2033 583 if (remove_file_from_index(&the_index, path_name))
7e44c935 584 die("git update-index: unable to remove %s",
d4dbf36d
JH
585 ptr);
586 }
587 else {
588 /* mode ' ' sha1 '\t' name
589 * ptr[-1] points at tab,
590 * ptr[-41] is at the beginning of sha1
591 */
1928c944 592 ptr[-(hexsz + 2)] = ptr[-1] = 0;
71445a0f 593 if (add_cacheinfo(mode, &oid, path_name, stage))
7e44c935 594 die("git update-index: unable to update %s",
973d6a20 595 path_name);
d4dbf36d
JH
596 }
597 continue;
598
599 bad_line:
600 die("malformed index info %s", buf.buf);
601 }
e6c019d0 602 strbuf_release(&buf);
7fb1011e 603 strbuf_release(&uq);
d4dbf36d
JH
604}
605
309be813 606static const char * const update_index_usage[] = {
9c9b4f2f 607 N_("git update-index [<options>] [--] [<file>...]"),
309be813
JN
608 NULL
609};
f6ab5bb2 610
71445a0f 611static struct object_id head_oid;
612static struct object_id merge_head_oid;
2bd452d3
JH
613
614static struct cache_entry *read_one_ent(const char *which,
71445a0f 615 struct object_id *ent, const char *path,
2bd452d3
JH
616 int namelen, int stage)
617{
5ec1e728 618 unsigned short mode;
71445a0f 619 struct object_id oid;
2bd452d3
JH
620 struct cache_entry *ce;
621
50ddb089 622 if (get_tree_entry(the_repository, ent, path, &oid, &mode)) {
83e77a25
JH
623 if (which)
624 error("%s: not in %s branch.", path, which);
2bd452d3
JH
625 return NULL;
626 }
b9ca5e26 627 if (!the_index.sparse_index && mode == S_IFDIR) {
83e77a25
JH
628 if (which)
629 error("%s: not a blob in %s branch.", path, which);
2bd452d3
JH
630 return NULL;
631 }
a849735b 632 ce = make_empty_cache_entry(&the_index, namelen);
2bd452d3 633
71445a0f 634 oidcpy(&ce->oid, &oid);
2bd452d3 635 memcpy(ce->name, path, namelen);
b60e188c
TG
636 ce->ce_flags = create_ce_flags(stage);
637 ce->ce_namelen = namelen;
2bd452d3
JH
638 ce->ce_mode = create_ce_mode(mode);
639 return ce;
640}
641
91e07058
JH
642static int read_head_pointers(void)
643{
644 static int result = -2; /* unknown yet */
645
646 if (result == -2) {
647 result = -1;
648 if (read_ref("HEAD", &head_oid))
649 return error("No HEAD -- no initial commit yet?");
650 if (read_ref("MERGE_HEAD", &merge_head_oid))
651 return error("Not in the middle of a merge");
652 result = 0;
653 }
654 return result;
655}
656
2bd452d3
JH
657static int unresolve_one(const char *path)
658{
659 int namelen = strlen(path);
660 int pos;
661 int ret = 0;
662 struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
35901f1c
JH
663 struct resolve_undo_info *ru = NULL;
664
665 if (the_index.resolve_undo) {
666 struct string_list_item *item;
667 item = string_list_lookup(the_index.resolve_undo, path);
668 if (item) {
669 ru = item->util;
670 item->util = NULL;
671 }
672 }
673
674 /* resolve-undo record exists for the path */
675 if (ru) {
676 ret = unmerge_index_entry(&the_index, path, ru);
677 free(ru);
678 return ret;
679 }
2bd452d3
JH
680
681 /* See if there is such entry in the index. */
07047d68 682 pos = index_name_pos(&the_index, path, namelen);
8aa38563 683 if (0 <= pos) {
35901f1c 684 ; /* resolve-undo record was used already -- fall back */
8aa38563 685 } else {
35901f1c
JH
686 /* Is it unmerged? */
687 pos = -pos - 1;
dc594180
ÆAB
688 if (pos < the_index.cache_nr) {
689 const struct cache_entry *ce = the_index.cache[pos];
2bd452d3
JH
690 if (ce_namelen(ce) == namelen &&
691 !memcmp(ce->name, path, namelen)) {
692 fprintf(stderr,
693 "%s: skipping still unmerged path.\n",
694 path);
2bd452d3 695 }
35901f1c 696 goto free_return;
2bd452d3 697 }
35901f1c 698 /* No, such a path does not exist -- removed */
2bd452d3
JH
699 }
700
91e07058
JH
701 /*
702 * We are not using resolve-undo information but just
703 * populating the stages #2 and #3 from HEAD and MERGE_HEAD.
704 *
705 * This is a flawed replacement of true "unresolve", as we do
706 * not have a way to recreate the stage #1 for the common
707 * ancestor (which may not be a unique merge-base between the
708 * two).
2bd452d3 709 */
91e07058
JH
710 if (read_head_pointers()) {
711 ret = -1;
712 goto free_return;
713 }
714
71445a0f 715 ce_2 = read_one_ent("our", &head_oid, path, namelen, 2);
716 ce_3 = read_one_ent("their", &merge_head_oid, path, namelen, 3);
2bd452d3
JH
717
718 if (!ce_2 || !ce_3) {
719 ret = -1;
720 goto free_return;
721 }
4a7e27e9 722 if (oideq(&ce_2->oid, &ce_3->oid) &&
2bd452d3
JH
723 ce_2->ce_mode == ce_3->ce_mode) {
724 fprintf(stderr, "%s: identical in both, skipping.\n",
725 path);
726 goto free_return;
727 }
728
031b2033
ÆAB
729 remove_file_from_index(&the_index, path);
730 if (add_index_entry(&the_index, ce_2, ADD_CACHE_OK_TO_ADD)) {
2bd452d3
JH
731 error("%s: cannot add our version to the index.", path);
732 ret = -1;
733 goto free_return;
734 }
031b2033 735 if (!add_index_entry(&the_index, ce_3, ADD_CACHE_OK_TO_ADD))
2bd452d3
JH
736 return 0;
737 error("%s: cannot add their version to the index.", path);
738 ret = -1;
739 free_return:
a849735b
JM
740 discard_cache_entry(ce_2);
741 discard_cache_entry(ce_3);
2bd452d3
JH
742 return ret;
743}
744
09895c1f
JH
745static int do_unresolve(int ac, const char **av,
746 const char *prefix, int prefix_length)
2bd452d3
JH
747{
748 int i;
749 int err = 0;
750
2bd452d3
JH
751 for (i = 1; i < ac; i++) {
752 const char *arg = av[i];
d7a643b7 753 char *p = prefix_path(prefix, prefix_length, arg);
09895c1f 754 err |= unresolve_one(p);
d7a643b7 755 free(p);
2bd452d3
JH
756 }
757 return err;
758}
759
827f8305 760static int do_reupdate(const char **paths,
5bb1cb6d 761 const char *prefix)
83e77a25
JH
762{
763 /* Read HEAD and run update-index on paths that are
764 * merged and already different between index and HEAD.
765 */
766 int pos;
767 int has_head = 1;
eb9cb55b
NTND
768 struct pathspec pathspec;
769
0fdc2ae5
NTND
770 parse_pathspec(&pathspec, 0,
771 PATHSPEC_PREFER_CWD,
827f8305 772 prefix, paths);
83e77a25 773
34c290a6 774 if (read_ref("HEAD", &head_oid))
83e77a25
JH
775 /* If there is no HEAD, that means it is an initial
776 * commit. Update everything in the index.
777 */
778 has_head = 0;
779 redo:
dc594180
ÆAB
780 for (pos = 0; pos < the_index.cache_nr; pos++) {
781 const struct cache_entry *ce = the_index.cache[pos];
83e77a25
JH
782 struct cache_entry *old = NULL;
783 int save_nr;
5699d17e 784 char *path;
22293b9c 785
6d2df284 786 if (ce_stage(ce) || !ce_path_match(&the_index, ce, &pathspec, NULL))
83e77a25
JH
787 continue;
788 if (has_head)
71445a0f 789 old = read_one_ent(NULL, &head_oid,
83e77a25
JH
790 ce->name, ce_namelen(ce), 0);
791 if (old && ce->ce_mode == old->ce_mode &&
4a7e27e9 792 oideq(&ce->oid, &old->oid)) {
a849735b 793 discard_cache_entry(old);
83e77a25
JH
794 continue; /* unchanged */
795 }
b9ca5e26
VD
796
797 /* At this point, we know the contents of the sparse directory are
798 * modified with respect to HEAD, so we expand the index and restart
799 * to process each path individually
800 */
801 if (S_ISSPARSEDIR(ce->ce_mode)) {
802 ensure_full_index(&the_index);
803 goto redo;
804 }
805
83e77a25
JH
806 /* Be careful. The working tree may not have the
807 * path anymore, in which case, under 'allow_remove',
808 * or worse yet 'allow_replace', active_nr may decrease.
809 */
dc594180 810 save_nr = the_index.cache_nr;
5699d17e
KB
811 path = xstrdup(ce->name);
812 update_one(path);
813 free(path);
a849735b 814 discard_cache_entry(old);
dc594180 815 if (save_nr != the_index.cache_nr)
83e77a25
JH
816 goto redo;
817 }
ed6e8038 818 clear_pathspec(&pathspec);
83e77a25
JH
819 return 0;
820}
821
309be813
JN
822struct refresh_params {
823 unsigned int flags;
824 int *has_errors;
825};
826
827static int refresh(struct refresh_params *o, unsigned int flag)
828{
829 setup_work_tree();
07047d68
ÆAB
830 repo_read_index(the_repository);
831 *o->has_errors |= refresh_index(&the_index, o->flags | flag, NULL,
832 NULL, NULL);
2ede073f
MS
833 if (has_racy_timestamp(&the_index)) {
834 /*
835 * Even if nothing else has changed, updating the file
836 * increases the chance that racy timestamps become
837 * non-racy, helping future run-time performance.
838 * We do that even in case of "errors" returned by
07047d68 839 * refresh_index() as these are no actual errors.
2ede073f
MS
840 * cmd_status() does the same.
841 */
dc594180 842 the_index.cache_changed |= SOMETHING_CHANGED;
2ede073f 843 }
309be813
JN
844 return 0;
845}
846
847static int refresh_callback(const struct option *opt,
848 const char *arg, int unset)
849{
517fe807
JK
850 BUG_ON_OPT_NEG(unset);
851 BUG_ON_OPT_ARG(arg);
309be813
JN
852 return refresh(opt->value, 0);
853}
854
855static int really_refresh_callback(const struct option *opt,
856 const char *arg, int unset)
857{
517fe807
JK
858 BUG_ON_OPT_NEG(unset);
859 BUG_ON_OPT_ARG(arg);
309be813
JN
860 return refresh(opt->value, REFRESH_REALLY);
861}
862
863static int chmod_callback(const struct option *opt,
864 const char *arg, int unset)
865{
866 char *flip = opt->value;
517fe807 867 BUG_ON_OPT_NEG(unset);
309be813
JN
868 if ((arg[0] != '-' && arg[0] != '+') || arg[1] != 'x' || arg[2])
869 return error("option 'chmod' expects \"+x\" or \"-x\"");
870 *flip = arg[0];
871 return 0;
872}
873
874static int resolve_undo_clear_callback(const struct option *opt,
875 const char *arg, int unset)
876{
517fe807
JK
877 BUG_ON_OPT_NEG(unset);
878 BUG_ON_OPT_ARG(arg);
031b2033 879 resolve_undo_clear_index(&the_index);
309be813
JN
880 return 0;
881}
882
ec160ae1
JH
883static int parse_new_style_cacheinfo(const char *arg,
884 unsigned int *mode,
71445a0f 885 struct object_id *oid,
ec160ae1
JH
886 const char **path)
887{
888 unsigned long ul;
889 char *endp;
fe04ccf7 890 const char *p;
ec160ae1 891
c8e1ee4f
JK
892 if (!arg)
893 return -1;
894
ec160ae1
JH
895 errno = 0;
896 ul = strtoul(arg, &endp, 8);
897 if (errno || endp == arg || *endp != ',' || (unsigned int) ul != ul)
898 return -1; /* not a new-style cacheinfo */
899 *mode = ul;
900 endp++;
fe04ccf7 901 if (parse_oid_hex(endp, oid, &p) || *p != ',')
ec160ae1 902 return -1;
fe04ccf7 903 *path = p + 1;
ec160ae1
JH
904 return 0;
905}
906
f41179f1 907static enum parse_opt_result cacheinfo_callback(
3ebbe289
NTND
908 struct parse_opt_ctx_t *ctx, const struct option *opt,
909 const char *arg, int unset)
309be813 910{
71445a0f 911 struct object_id oid;
309be813 912 unsigned int mode;
ec160ae1 913 const char *path;
309be813 914
517fe807 915 BUG_ON_OPT_NEG(unset);
3ebbe289 916 BUG_ON_OPT_ARG(arg);
517fe807 917
71445a0f 918 if (!parse_new_style_cacheinfo(ctx->argv[1], &mode, &oid, &path)) {
919 if (add_cacheinfo(mode, &oid, path, 0))
ec160ae1
JH
920 die("git update-index: --cacheinfo cannot add %s", path);
921 ctx->argv++;
922 ctx->argc--;
923 return 0;
924 }
309be813 925 if (ctx->argc <= 3)
ec160ae1 926 return error("option 'cacheinfo' expects <mode>,<sha1>,<path>");
309be813 927 if (strtoul_ui(*++ctx->argv, 8, &mode) ||
71445a0f 928 get_oid_hex(*++ctx->argv, &oid) ||
929 add_cacheinfo(mode, &oid, *++ctx->argv, 0))
309be813
JN
930 die("git update-index: --cacheinfo cannot add %s", *ctx->argv);
931 ctx->argc -= 3;
932 return 0;
933}
934
f41179f1 935static enum parse_opt_result stdin_cacheinfo_callback(
3ebbe289
NTND
936 struct parse_opt_ctx_t *ctx, const struct option *opt,
937 const char *arg, int unset)
309be813 938{
7e07ed84 939 int *nul_term_line = opt->value;
309be813 940
517fe807 941 BUG_ON_OPT_NEG(unset);
3ebbe289 942 BUG_ON_OPT_ARG(arg);
517fe807 943
309be813
JN
944 if (ctx->argc != 1)
945 return error("option '%s' must be the last argument", opt->long_name);
946 allow_add = allow_replace = allow_remove = 1;
7e07ed84 947 read_index_info(*nul_term_line);
309be813
JN
948 return 0;
949}
950
f41179f1 951static enum parse_opt_result stdin_callback(
3ebbe289
NTND
952 struct parse_opt_ctx_t *ctx, const struct option *opt,
953 const char *arg, int unset)
309be813
JN
954{
955 int *read_from_stdin = opt->value;
956
517fe807 957 BUG_ON_OPT_NEG(unset);
3ebbe289 958 BUG_ON_OPT_ARG(arg);
517fe807 959
309be813
JN
960 if (ctx->argc != 1)
961 return error("option '%s' must be the last argument", opt->long_name);
962 *read_from_stdin = 1;
963 return 0;
964}
965
f41179f1 966static enum parse_opt_result unresolve_callback(
3ebbe289
NTND
967 struct parse_opt_ctx_t *ctx, const struct option *opt,
968 const char *arg, int unset)
309be813
JN
969{
970 int *has_errors = opt->value;
971 const char *prefix = startup_info->prefix;
972
517fe807 973 BUG_ON_OPT_NEG(unset);
3ebbe289 974 BUG_ON_OPT_ARG(arg);
517fe807 975
309be813
JN
976 /* consume remaining arguments. */
977 *has_errors = do_unresolve(ctx->argc, ctx->argv,
978 prefix, prefix ? strlen(prefix) : 0);
979 if (*has_errors)
dc594180 980 the_index.cache_changed = 0;
309be813
JN
981
982 ctx->argv += ctx->argc - 1;
983 ctx->argc = 1;
984 return 0;
985}
986
f41179f1 987static enum parse_opt_result reupdate_callback(
3ebbe289
NTND
988 struct parse_opt_ctx_t *ctx, const struct option *opt,
989 const char *arg, int unset)
309be813
JN
990{
991 int *has_errors = opt->value;
992 const char *prefix = startup_info->prefix;
993
517fe807 994 BUG_ON_OPT_NEG(unset);
3ebbe289 995 BUG_ON_OPT_ARG(arg);
517fe807 996
309be813
JN
997 /* consume remaining arguments. */
998 setup_work_tree();
827f8305 999 *has_errors = do_reupdate(ctx->argv + 1, prefix);
309be813 1000 if (*has_errors)
dc594180 1001 the_index.cache_changed = 0;
309be813
JN
1002
1003 ctx->argv += ctx->argc - 1;
1004 ctx->argc = 1;
1005 return 0;
1006}
1007
a633fca0 1008int cmd_update_index(int argc, const char **argv, const char *prefix)
e83c5163 1009{
7e07ed84 1010 int newfd, entries, has_errors = 0, nul_term_line = 0;
113e6413 1011 enum uc_mode untracked_cache = UC_UNSPECIFIED;
ee1bec3d 1012 int read_from_stdin = 0;
ee1bec3d 1013 int prefix_length = prefix ? strlen(prefix) : 0;
69dec66b 1014 int preferred_index_format = 0;
227bdb18 1015 char set_executable_bit = 0;
309be813 1016 struct refresh_params refresh_args = {0, &has_errors};
7b802b86 1017 int lock_error = 0;
c18b80a0 1018 int split_index = -1;
7c545be9 1019 int force_write = 0;
9d406cba 1020 int fsmonitor = -1;
bfffb48c 1021 struct lock_file lock_file = LOCK_INIT;
309be813 1022 struct parse_opt_ctx_t ctx;
7e07ed84 1023 strbuf_getline_fn getline_fn;
309be813 1024 int parseopt_state = PARSE_OPT_UNKNOWN;
ad0fb659 1025 struct repository *r = the_repository;
309be813
JN
1026 struct option options[] = {
1027 OPT_BIT('q', NULL, &refresh_args.flags,
4a4838b4 1028 N_("continue refresh even when index needs update"),
309be813
JN
1029 REFRESH_QUIET),
1030 OPT_BIT(0, "ignore-submodules", &refresh_args.flags,
4a4838b4 1031 N_("refresh: ignore submodules"),
309be813
JN
1032 REFRESH_IGNORE_SUBMODULES),
1033 OPT_SET_INT(0, "add", &allow_add,
4a4838b4 1034 N_("do not ignore new files"), 1),
309be813 1035 OPT_SET_INT(0, "replace", &allow_replace,
4a4838b4 1036 N_("let files replace directories and vice-versa"), 1),
309be813 1037 OPT_SET_INT(0, "remove", &allow_remove,
4a4838b4 1038 N_("notice files missing from worktree"), 1),
309be813 1039 OPT_BIT(0, "unmerged", &refresh_args.flags,
4a4838b4 1040 N_("refresh even if index contains unmerged entries"),
309be813 1041 REFRESH_UNMERGED),
203c8533 1042 OPT_CALLBACK_F(0, "refresh", &refresh_args, NULL,
4a4838b4 1043 N_("refresh stat information"),
309be813 1044 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
203c8533
DL
1045 refresh_callback),
1046 OPT_CALLBACK_F(0, "really-refresh", &refresh_args, NULL,
4a4838b4 1047 N_("like --refresh, but ignore assume-unchanged setting"),
309be813 1048 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
203c8533 1049 really_refresh_callback),
309be813 1050 {OPTION_LOWLEVEL_CALLBACK, 0, "cacheinfo", NULL,
ec160ae1 1051 N_("<mode>,<object>,<path>"),
4a4838b4 1052 N_("add the specified entry to the index"),
ec160ae1 1053 PARSE_OPT_NOARG | /* disallow --cacheinfo=<mode> form */
309be813 1054 PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
bf3ff338
NTND
1055 NULL, 0,
1056 cacheinfo_callback},
203c8533 1057 OPT_CALLBACK_F(0, "chmod", &set_executable_bit, "(+|-)x",
4a4838b4 1058 N_("override the executable bit of the listed files"),
5f0df44c 1059 PARSE_OPT_NONEG,
203c8533 1060 chmod_callback),
309be813 1061 {OPTION_SET_INT, 0, "assume-unchanged", &mark_valid_only, NULL,
4a4838b4 1062 N_("mark files as \"not changing\""),
309be813
JN
1063 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
1064 {OPTION_SET_INT, 0, "no-assume-unchanged", &mark_valid_only, NULL,
4a4838b4 1065 N_("clear assumed-unchanged bit"),
309be813
JN
1066 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
1067 {OPTION_SET_INT, 0, "skip-worktree", &mark_skip_worktree_only, NULL,
4a4838b4 1068 N_("mark files as \"index-only\""),
309be813
JN
1069 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
1070 {OPTION_SET_INT, 0, "no-skip-worktree", &mark_skip_worktree_only, NULL,
4a4838b4 1071 N_("clear skip-worktree bit"),
309be813 1072 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
8dfb04ae
JS
1073 OPT_BOOL(0, "ignore-skip-worktree-entries", &ignore_skip_worktree_entries,
1074 N_("do not touch index-only entries")),
309be813 1075 OPT_SET_INT(0, "info-only", &info_only,
4a4838b4 1076 N_("add to index only; do not add content to object database"), 1),
309be813 1077 OPT_SET_INT(0, "force-remove", &force_remove,
4a4838b4 1078 N_("remove named paths even if present in worktree"), 1),
7e07ed84
JH
1079 OPT_BOOL('z', NULL, &nul_term_line,
1080 N_("with --stdin: input lines are terminated by null bytes")),
309be813 1081 {OPTION_LOWLEVEL_CALLBACK, 0, "stdin", &read_from_stdin, NULL,
4a4838b4 1082 N_("read list of paths to be updated from standard input"),
309be813 1083 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
bf3ff338 1084 NULL, 0, stdin_callback},
7e07ed84 1085 {OPTION_LOWLEVEL_CALLBACK, 0, "index-info", &nul_term_line, NULL,
4a4838b4 1086 N_("add entries from standard input to the index"),
309be813 1087 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
bf3ff338 1088 NULL, 0, stdin_cacheinfo_callback},
309be813 1089 {OPTION_LOWLEVEL_CALLBACK, 0, "unresolve", &has_errors, NULL,
4a4838b4 1090 N_("repopulate stages #2 and #3 for the listed paths"),
309be813 1091 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
bf3ff338 1092 NULL, 0, unresolve_callback},
309be813 1093 {OPTION_LOWLEVEL_CALLBACK, 'g', "again", &has_errors, NULL,
4a4838b4 1094 N_("only update entries that differ from HEAD"),
309be813 1095 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
bf3ff338 1096 NULL, 0, reupdate_callback},
309be813 1097 OPT_BIT(0, "ignore-missing", &refresh_args.flags,
4a4838b4 1098 N_("ignore files missing from worktree"),
309be813
JN
1099 REFRESH_IGNORE_MISSING),
1100 OPT_SET_INT(0, "verbose", &verbose,
4a4838b4 1101 N_("report actions to standard output"), 1),
203c8533 1102 OPT_CALLBACK_F(0, "clear-resolve-undo", NULL, NULL,
4a4838b4 1103 N_("(for porcelains) forget saved unresolved conflicts"),
309be813 1104 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
203c8533 1105 resolve_undo_clear_callback),
69dec66b 1106 OPT_INTEGER(0, "index-version", &preferred_index_format,
4a4838b4 1107 N_("write index in this format")),
c18b80a0
NTND
1108 OPT_BOOL(0, "split-index", &split_index,
1109 N_("enable or disable split index")),
9e597241
NTND
1110 OPT_BOOL(0, "untracked-cache", &untracked_cache,
1111 N_("enable/disable untracked cache")),
eaab83d0
CC
1112 OPT_SET_INT(0, "test-untracked-cache", &untracked_cache,
1113 N_("test if the filesystem supports untracked cache"), UC_TEST),
f64cb88d 1114 OPT_SET_INT(0, "force-untracked-cache", &untracked_cache,
113e6413 1115 N_("enable untracked cache without testing the filesystem"), UC_FORCE),
7c545be9
BP
1116 OPT_SET_INT(0, "force-write-index", &force_write,
1117 N_("write out the index even if is not flagged as changed"), 1),
9d406cba
BP
1118 OPT_BOOL(0, "fsmonitor", &fsmonitor,
1119 N_("enable or disable file system monitor")),
1120 {OPTION_SET_INT, 0, "fsmonitor-valid", &mark_fsmonitor_only, NULL,
1121 N_("mark files as fsmonitor valid"),
1122 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
1123 {OPTION_SET_INT, 0, "no-fsmonitor-valid", &mark_fsmonitor_only, NULL,
1124 N_("clear fsmonitor valid bit"),
1125 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
309be813
JN
1126 OPT_END()
1127 };
bb233d69 1128
9c7c27ee 1129 if (argc == 2 && !strcmp(argv[1], "-h"))
b04d930b 1130 usage_with_options(update_index_usage, options);
9c7c27ee 1131
ef90d6d4 1132 git_config(git_default_config, NULL);
3e09cdfd 1133
c35e9f5e
VD
1134 prepare_repo_settings(r);
1135 the_repository->settings.command_requires_full_index = 0;
1136
b3e83cc7 1137 /* we will diagnose later if it turns out that we need to update it */
07047d68 1138 newfd = repo_hold_locked_index(the_repository, &lock_file, 0);
7b802b86
JH
1139 if (newfd < 0)
1140 lock_error = errno;
9614b8dc 1141
07047d68 1142 entries = repo_read_index(the_repository);
9614b8dc 1143 if (entries < 0)
2de381f9 1144 die("cache corrupted");
e83c5163 1145
1956ecd0
BP
1146 the_index.updated_skipworktree = 1;
1147
309be813
JN
1148 /*
1149 * Custom copy of parse_options() because we want to handle
1150 * filename arguments as they come.
1151 */
1152 parse_options_start(&ctx, argc, argv, prefix,
1153 options, PARSE_OPT_STOP_AT_NON_OPTION);
23a3a303
NS
1154
1155 /*
1156 * Allow the object layer to optimize adding multiple objects in
1157 * a batch.
1158 */
1159 begin_odb_transaction();
309be813
JN
1160 while (ctx.argc) {
1161 if (parseopt_state != PARSE_OPT_DONE)
1162 parseopt_state = parse_options_step(&ctx, options,
1163 update_index_usage);
1164 if (!ctx.argc)
1165 break;
1166 switch (parseopt_state) {
1167 case PARSE_OPT_HELP:
3bb0923f 1168 case PARSE_OPT_ERROR:
309be813 1169 exit(129);
a92ec7ef
NTND
1170 case PARSE_OPT_COMPLETE:
1171 exit(0);
309be813
JN
1172 case PARSE_OPT_NON_OPTION:
1173 case PARSE_OPT_DONE:
1174 {
1175 const char *path = ctx.argv[0];
d7a643b7 1176 char *p;
121481ab 1177
309be813
JN
1178 setup_work_tree();
1179 p = prefix_path(prefix, prefix_length, path);
6bb69077 1180 update_one(p);
309be813
JN
1181 if (set_executable_bit)
1182 chmod_path(set_executable_bit, p);
d7a643b7 1183 free(p);
309be813
JN
1184 ctx.argc--;
1185 ctx.argv++;
1186 break;
1187 }
1188 case PARSE_OPT_UNKNOWN:
1189 if (ctx.argv[0][1] == '-')
1190 error("unknown option '%s'", ctx.argv[0] + 2);
1191 else
1192 error("unknown switch '%c'", *ctx.opt);
1193 usage_with_options(update_index_usage, options);
121481ab 1194 }
ee1bec3d 1195 }
309be813 1196 argc = parse_options_end(&ctx);
7e07ed84
JH
1197
1198 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
69dec66b
JH
1199 if (preferred_index_format) {
1200 if (preferred_index_format < INDEX_FORMAT_LB ||
1201 INDEX_FORMAT_UB < preferred_index_format)
1202 die("index-version %d not in range: %d..%d",
1203 preferred_index_format,
1204 INDEX_FORMAT_LB, INDEX_FORMAT_UB);
1205
1206 if (the_index.version != preferred_index_format)
dc594180 1207 the_index.cache_changed |= SOMETHING_CHANGED;
69dec66b
JH
1208 the_index.version = preferred_index_format;
1209 }
309be813 1210
ee1bec3d 1211 if (read_from_stdin) {
0d4cc1b4
JK
1212 struct strbuf buf = STRBUF_INIT;
1213 struct strbuf unquoted = STRBUF_INIT;
7fb1011e 1214
f83eafdd 1215 setup_work_tree();
7e07ed84 1216 while (getline_fn(&buf, stdin) != EOF) {
d7a643b7 1217 char *p;
7e07ed84 1218 if (!nul_term_line && buf.buf[0] == '"') {
0d4cc1b4
JK
1219 strbuf_reset(&unquoted);
1220 if (unquote_c_style(&unquoted, buf.buf, NULL))
7fb1011e 1221 die("line is badly quoted");
0d4cc1b4 1222 strbuf_swap(&buf, &unquoted);
7fb1011e
PH
1223 }
1224 p = prefix_path(prefix, prefix_length, buf.buf);
6bb69077 1225 update_one(p);
fb69a760 1226 if (set_executable_bit)
227bdb18 1227 chmod_path(set_executable_bit, p);
d7a643b7 1228 free(p);
9b63f501 1229 }
0d4cc1b4 1230 strbuf_release(&unquoted);
e6c019d0 1231 strbuf_release(&buf);
e83c5163 1232 }
2bd452d3 1233
23a3a303
NS
1234 /*
1235 * By now we have added all of the new objects
1236 */
1237 end_odb_transaction();
1238
c18b80a0 1239 if (split_index > 0) {
6cc10533
CC
1240 if (git_config_get_split_index() == 0)
1241 warning(_("core.splitIndex is set to false; "
1242 "remove or change it, if you really want to "
1243 "enable split index"));
cef4fc7e
CC
1244 if (the_index.split_index)
1245 the_index.cache_changed |= SPLIT_INDEX_ORDERED;
1246 else
1247 add_split_index(&the_index);
6cc10533
CC
1248 } else if (!split_index) {
1249 if (git_config_get_split_index() == 1)
1250 warning(_("core.splitIndex is set to true; "
1251 "remove or change it, if you really want to "
1252 "disable split index"));
cef4fc7e 1253 remove_split_index(&the_index);
6cc10533 1254 }
9e597241 1255
ad0fb659 1256 prepare_repo_settings(r);
435ec090
CC
1257 switch (untracked_cache) {
1258 case UC_UNSPECIFIED:
1259 break;
1260 case UC_DISABLE:
ad0fb659 1261 if (r->settings.core_untracked_cache == UNTRACKED_CACHE_WRITE)
43073f89
VA
1262 warning(_("core.untrackedCache is set to true; "
1263 "remove or change it, if you really want to "
1264 "disable the untracked cache"));
07b29bfd 1265 remove_untracked_cache(&the_index);
6d19db14 1266 report(_("Untracked cache disabled"));
435ec090
CC
1267 break;
1268 case UC_TEST:
1269 setup_work_tree();
1270 return !test_if_untracked_cache_is_supported();
1271 case UC_ENABLE:
1272 case UC_FORCE:
ad0fb659 1273 if (r->settings.core_untracked_cache == UNTRACKED_CACHE_REMOVE)
43073f89
VA
1274 warning(_("core.untrackedCache is set to false; "
1275 "remove or change it, if you really want to "
1276 "enable the untracked cache"));
435ec090
CC
1277 add_untracked_cache(&the_index);
1278 report(_("Untracked cache enabled for '%s'"), get_git_work_tree());
1279 break;
1280 default:
033abf97 1281 BUG("bad untracked_cache value: %d", untracked_cache);
9e597241 1282 }
c18b80a0 1283
9d406cba 1284 if (fsmonitor > 0) {
1e0ea5c4 1285 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(r);
62a62a28
JH
1286 enum fsmonitor_reason reason = fsm_settings__get_reason(r);
1287
1288 /*
1289 * The user wants to turn on FSMonitor using the command
1290 * line argument. (We don't know (or care) whether that
1291 * is the IPC or HOOK version.)
1292 *
1293 * Use one of the __get routines to force load the FSMonitor
1294 * config settings into the repo-settings. That will detect
1295 * whether the file system is compatible so that we can stop
1296 * here with a nice error message.
1297 */
1298 if (reason > FSMONITOR_REASON_OK)
1299 die("%s",
1300 fsm_settings__get_incompatible_msg(r, reason));
1301
1e0ea5c4 1302 if (fsm_mode == FSMONITOR_MODE_DISABLED) {
9d406cba
BP
1303 warning(_("core.fsmonitor is unset; "
1304 "set it if you really want to "
1305 "enable fsmonitor"));
1e0ea5c4 1306 }
9d406cba
BP
1307 add_fsmonitor(&the_index);
1308 report(_("fsmonitor enabled"));
1309 } else if (!fsmonitor) {
1e0ea5c4
JH
1310 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(r);
1311 if (fsm_mode > FSMONITOR_MODE_DISABLED)
9d406cba
BP
1312 warning(_("core.fsmonitor is set; "
1313 "remove it if you really want to "
1314 "disable fsmonitor"));
1315 remove_fsmonitor(&the_index);
1316 report(_("fsmonitor disabled"));
1317 }
1318
dc594180 1319 if (the_index.cache_changed || force_write) {
7b802b86 1320 if (newfd < 0) {
309be813 1321 if (refresh_args.flags & REFRESH_QUIET)
7b802b86 1322 exit(128);
e197c218 1323 unable_to_lock_die(get_index_file(), lock_error);
7b802b86 1324 }
bfffb48c 1325 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
021b6e45 1326 die("Unable to write new index file");
5cd5ace7 1327 }
9614b8dc 1328
bfffb48c 1329 rollback_lock_file(&lock_file);
fefe81c9 1330
c4b83e61 1331 return has_errors ? 1 : 0;
e83c5163 1332}