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