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