]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/update-index.c
read-cache: save deleted entries in split index
[thirdparty/git.git] / builtin / update-index.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c5163 6#include "cache.h"
973d6a20 7#include "quote.h"
a6e5642f 8#include "cache-tree.h"
2bd452d3 9#include "tree-walk.h"
fefe81c9 10#include "builtin.h"
e011054b 11#include "refs.h"
4a39f79d 12#include "resolve-undo.h"
309be813 13#include "parse-options.h"
64acde94 14#include "pathspec.h"
429bb40a 15#include "dir.h"
e83c5163 16
121481ab
LT
17/*
18 * Default to not allowing changes to the list of files. The
19 * tool doesn't actually care, but this makes it harder to add
20 * files to the revision control by mistake by doing something
f18d244a 21 * like "git update-index *" and suddenly having all the object
121481ab
LT
22 * files be revision controlled.
23 */
caf4f582
JH
24static int allow_add;
25static int allow_remove;
26static int allow_replace;
caf4f582 27static int info_only;
9b63f501 28static int force_remove;
caf4f582 29static int verbose;
96f1e58f 30static int mark_valid_only;
44a36913 31static int mark_skip_worktree_only;
83b327ba
NTND
32#define MARK_FLAG 1
33#define UNMARK_FLAG 2
5f73076c 34
28bea9e5 35__attribute__((format (printf, 1, 2)))
caf4f582
JH
36static void report(const char *fmt, ...)
37{
38 va_list vp;
39
40 if (!verbose)
41 return;
42
43 va_start(vp, fmt);
44 vprintf(fmt, vp);
45 putchar('\n');
46 va_end(vp);
47}
48
83b327ba 49static int mark_ce_flags(const char *path, int flag, int mark)
5f73076c
JH
50{
51 int namelen = strlen(path);
52 int pos = cache_name_pos(path, namelen);
53 if (0 <= pos) {
83b327ba
NTND
54 if (mark)
55 active_cache[pos]->ce_flags |= flag;
56 else
57 active_cache[pos]->ce_flags &= ~flag;
a5400efe 58 cache_tree_invalidate_path(&the_index, path);
782a5ff9 59 active_cache_changed |= CE_ENTRY_CHANGED;
5f73076c
JH
60 return 0;
61 }
62 return -1;
63}
64
e011054b 65static int remove_one_path(const char *path)
e83c5163 66{
e011054b
LT
67 if (!allow_remove)
68 return error("%s: does not exist and --remove not passed", path);
69 if (remove_file_from_cache(path))
70 return error("%s: cannot remove from the index", path);
71 return 0;
72}
a6e5642f 73
e011054b
LT
74/*
75 * Handle a path that couldn't be lstat'ed. It's either:
76 * - missing file (ENOENT or ENOTDIR). That's ok if we're
77 * supposed to be removing it and the removal actually
78 * succeeds.
79 * - permission error. That's never ok.
80 */
81static int process_lstat_error(const char *path, int err)
82{
83 if (err == ENOENT || err == ENOTDIR)
84 return remove_one_path(path);
85 return error("lstat(\"%s\"): %s", path, strerror(errno));
86}
a6e5642f 87
9c5e6c80 88static int add_one_path(const struct cache_entry *old, const char *path, int len, struct stat *st)
e011054b 89{
22631473
LT
90 int option, size;
91 struct cache_entry *ce;
3e09cdfd 92
22631473
LT
93 /* Was the old index entry already up-to-date? */
94 if (old && !ce_stage(old) && !ce_match_stat(old, st, 0))
95 return 0;
96
97 size = cache_entry_size(len);
98 ce = xcalloc(1, size);
e011054b 99 memcpy(ce->name, path, len);
b60e188c
TG
100 ce->ce_flags = create_ce_flags(0);
101 ce->ce_namelen = len;
e011054b
LT
102 fill_stat_cache_info(ce, st);
103 ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
ec1fcc16 104
c4ce46fc 105 if (index_path(ce->sha1, path, st,
dc4cd767
JM
106 info_only ? 0 : HASH_WRITE_OBJECT)) {
107 free(ce);
ec1fcc16 108 return -1;
dc4cd767 109 }
192268c1
JH
110 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
111 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
34143b26 112 if (add_cache_entry(ce, option))
e011054b 113 return error("%s: cannot add to the index - missing --add option?", path);
34143b26 114 return 0;
121481ab
LT
115}
116
e011054b
LT
117/*
118 * Handle a path that was a directory. Four cases:
119 *
120 * - it's already a gitlink in the index, and we keep it that
121 * way, and update it if we can (if we cannot find the HEAD,
122 * we're going to keep it unchanged in the index!)
123 *
124 * - it's a *file* in the index, in which case it should be
125 * removed as a file if removal is allowed, since it doesn't
126 * exist as such any more. If removal isn't allowed, it's
127 * an error.
128 *
129 * (NOTE! This is old and arguably fairly strange behaviour.
130 * We might want to make this an error unconditionally, and
131 * use "--force-remove" if you actually want to force removal).
132 *
133 * - it used to exist as a subdirectory (ie multiple files with
134 * this particular prefix) in the index, in which case it's wrong
135 * to try to update it as a directory.
136 *
137 * - it doesn't exist at all in the index, but it is a valid
138 * git directory, and it should be *added* as a gitlink.
139 */
140static int process_directory(const char *path, int len, struct stat *st)
141{
142 unsigned char sha1[20];
143 int pos = cache_name_pos(path, len);
144
145 /* Exact match: file or existing gitlink */
146 if (pos >= 0) {
9c5e6c80 147 const struct cache_entry *ce = active_cache[pos];
7a51ed66 148 if (S_ISGITLINK(ce->ce_mode)) {
e011054b
LT
149
150 /* Do nothing to the index if there is no HEAD! */
151 if (resolve_gitlink_ref(path, "HEAD", sha1) < 0)
152 return 0;
153
154 return add_one_path(ce, path, len, st);
155 }
156 /* Should this be an unconditional error? */
157 return remove_one_path(path);
158 }
159
160 /* Inexact match: is there perhaps a subdirectory match? */
161 pos = -pos-1;
162 while (pos < active_nr) {
9c5e6c80 163 const struct cache_entry *ce = active_cache[pos++];
e011054b
LT
164
165 if (strncmp(ce->name, path, len))
166 break;
167 if (ce->name[len] > '/')
168 break;
169 if (ce->name[len] < '/')
170 continue;
171
172 /* Subdirectory match - error out */
173 return error("%s: is a directory - add individual files instead", path);
174 }
175
176 /* No match - should we add it as a gitlink? */
177 if (!resolve_gitlink_ref(path, "HEAD", sha1))
178 return add_one_path(NULL, path, len, st);
179
180 /* Error out. */
181 return error("%s: is a directory - add files inside instead", path);
182}
183
e011054b
LT
184static int process_path(const char *path)
185{
b4d1690d 186 int pos, len;
e011054b 187 struct stat st;
9c5e6c80 188 const struct cache_entry *ce;
e011054b 189
806d13b1 190 len = strlen(path);
57199892 191 if (has_symlink_leading_path(path, len))
806d13b1
JH
192 return error("'%s' is beyond a symbolic link", path);
193
b4d1690d
NTND
194 pos = cache_name_pos(path, len);
195 ce = pos < 0 ? NULL : active_cache[pos];
196 if (ce && ce_skip_worktree(ce)) {
197 /*
198 * working directory version is assumed "good"
199 * so updating it does not make sense.
200 * On the other hand, removing it from index should work
201 */
202 if (allow_remove && remove_file_from_cache(path))
203 return error("%s: cannot remove from the index", path);
204 return 0;
205 }
206
e011054b
LT
207 /*
208 * First things first: get the stat information, to decide
209 * what to do about the pathname!
210 */
211 if (lstat(path, &st) < 0)
212 return process_lstat_error(path, errno);
213
e011054b
LT
214 if (S_ISDIR(st.st_mode))
215 return process_directory(path, len, &st);
216
b4d1690d 217 return add_one_path(ce, path, len, &st);
e011054b
LT
218}
219
d23748a6
JH
220static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
221 const char *path, int stage)
9945d980 222{
192268c1 223 int size, len, option;
9945d980
LT
224 struct cache_entry *ce;
225
d23748a6 226 if (!verify_path(path))
7e7abea9 227 return error("Invalid path '%s'", path);
9945d980 228
d23748a6 229 len = strlen(path);
9945d980 230 size = cache_entry_size(len);
90321c10 231 ce = xcalloc(1, size);
9945d980 232
e702496e 233 hashcpy(ce->sha1, sha1);
d23748a6 234 memcpy(ce->name, path, len);
b60e188c
TG
235 ce->ce_flags = create_ce_flags(stage);
236 ce->ce_namelen = len;
e4479470 237 ce->ce_mode = create_ce_mode(mode);
5f73076c 238 if (assume_unchanged)
7a51ed66 239 ce->ce_flags |= CE_VALID;
192268c1
JH
240 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
241 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
caf4f582
JH
242 if (add_cache_entry(ce, option))
243 return error("%s: cannot add to the index - missing --add option?",
d23748a6
JH
244 path);
245 report("add '%s'", path);
caf4f582 246 return 0;
9945d980
LT
247}
248
227bdb18 249static void chmod_path(int flip, const char *path)
3e09cdfd
JH
250{
251 int pos;
252 struct cache_entry *ce;
253 unsigned int mode;
f2a19340 254
3e09cdfd
JH
255 pos = cache_name_pos(path, strlen(path));
256 if (pos < 0)
227bdb18 257 goto fail;
3e09cdfd 258 ce = active_cache[pos];
7a51ed66 259 mode = ce->ce_mode;
3e09cdfd 260 if (!S_ISREG(mode))
227bdb18 261 goto fail;
3e09cdfd
JH
262 switch (flip) {
263 case '+':
7a51ed66 264 ce->ce_mode |= 0111; break;
3e09cdfd 265 case '-':
7a51ed66 266 ce->ce_mode &= ~0111; break;
3e09cdfd 267 default:
227bdb18 268 goto fail;
3e09cdfd 269 }
a5400efe 270 cache_tree_invalidate_path(&the_index, path);
782a5ff9 271 active_cache_changed |= CE_ENTRY_CHANGED;
227bdb18
AR
272 report("chmod %cx '%s'", flip, path);
273 return;
274 fail:
7e44c935 275 die("git update-index: cannot chmod %cx '%s'", flip, path);
3e09cdfd
JH
276}
277
6bb69077 278static void update_one(const char *path)
ee1bec3d 279{
6bb69077 280 if (!verify_path(path)) {
ee1bec3d 281 fprintf(stderr, "Ignoring path %s\n", path);
6bb69077 282 return;
ee1bec3d 283 }
5f73076c 284 if (mark_valid_only) {
6bb69077 285 if (mark_ce_flags(path, CE_VALID, mark_valid_only == MARK_FLAG))
5f73076c 286 die("Unable to mark file %s", path);
6bb69077 287 return;
5f73076c 288 }
44a36913 289 if (mark_skip_worktree_only) {
6bb69077 290 if (mark_ce_flags(path, CE_SKIP_WORKTREE, mark_skip_worktree_only == MARK_FLAG))
5f73076c 291 die("Unable to mark file %s", path);
6bb69077 292 return;
5f73076c
JH
293 }
294
ee1bec3d 295 if (force_remove) {
6bb69077 296 if (remove_file_from_cache(path))
7e44c935 297 die("git update-index: unable to remove %s", path);
caf4f582 298 report("remove '%s'", path);
6bb69077 299 return;
ee1bec3d 300 }
6bb69077 301 if (process_path(path))
e011054b 302 die("Unable to process path %s", path);
caf4f582 303 report("add '%s'", path);
ee1bec3d
JH
304}
305
d4dbf36d
JH
306static void read_index_info(int line_termination)
307{
f285a2d7
BC
308 struct strbuf buf = STRBUF_INIT;
309 struct strbuf uq = STRBUF_INIT;
7fb1011e 310
7fb1011e 311 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
9c20a470 312 char *ptr, *tab;
973d6a20 313 char *path_name;
d4dbf36d
JH
314 unsigned char sha1[20];
315 unsigned int mode;
6aead43d 316 unsigned long ul;
d23748a6
JH
317 int stage;
318
319 /* This reads lines formatted in one of three formats:
320 *
321 * (1) mode SP sha1 TAB path
f18d244a 322 * The first format is what "git apply --index-info"
d23748a6
JH
323 * reports, and used to reconstruct a partial tree
324 * that is used for phony merge base tree when falling
325 * back on 3-way merge.
326 *
327 * (2) mode SP type SP sha1 TAB path
f18d244a 328 * The second format is to stuff "git ls-tree" output
d23748a6 329 * into the index file.
fefe81c9 330 *
d23748a6
JH
331 * (3) mode SP sha1 SP stage TAB path
332 * This format is to put higher order stages into the
f18d244a 333 * index file and matches "git ls-files --stage" output.
d23748a6 334 */
6aead43d
JM
335 errno = 0;
336 ul = strtoul(buf.buf, &ptr, 8);
337 if (ptr == buf.buf || *ptr != ' '
338 || errno || (unsigned int) ul != ul)
d4dbf36d 339 goto bad_line;
6aead43d 340 mode = ul;
d4dbf36d 341
9c20a470
JH
342 tab = strchr(ptr, '\t');
343 if (!tab || tab - ptr < 41)
344 goto bad_line;
d23748a6 345
2d497115 346 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
d23748a6
JH
347 stage = tab[-1] - '0';
348 ptr = tab + 1; /* point at the head of path */
349 tab = tab - 2; /* point at tail of sha1 */
350 }
351 else {
352 stage = 0;
353 ptr = tab + 1; /* point at the head of path */
354 }
355
9c20a470
JH
356 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
357 goto bad_line;
973d6a20 358
7fb1011e
PH
359 path_name = ptr;
360 if (line_termination && path_name[0] == '"') {
361 strbuf_reset(&uq);
362 if (unquote_c_style(&uq, path_name, NULL)) {
7e44c935 363 die("git update-index: bad quoting of path name");
7fb1011e
PH
364 }
365 path_name = uq.buf;
366 }
973d6a20
JH
367
368 if (!verify_path(path_name)) {
369 fprintf(stderr, "Ignoring path %s\n", path_name);
d4dbf36d
JH
370 continue;
371 }
372
373 if (!mode) {
374 /* mode == 0 means there is no such path -- remove */
973d6a20 375 if (remove_file_from_cache(path_name))
7e44c935 376 die("git update-index: unable to remove %s",
d4dbf36d
JH
377 ptr);
378 }
379 else {
380 /* mode ' ' sha1 '\t' name
381 * ptr[-1] points at tab,
382 * ptr[-41] is at the beginning of sha1
383 */
384 ptr[-42] = ptr[-1] = 0;
d23748a6 385 if (add_cacheinfo(mode, sha1, path_name, stage))
7e44c935 386 die("git update-index: unable to update %s",
973d6a20 387 path_name);
d4dbf36d
JH
388 }
389 continue;
390
391 bad_line:
392 die("malformed index info %s", buf.buf);
393 }
e6c019d0 394 strbuf_release(&buf);
7fb1011e 395 strbuf_release(&uq);
d4dbf36d
JH
396}
397
309be813 398static const char * const update_index_usage[] = {
4a4838b4 399 N_("git update-index [options] [--] [<file>...]"),
309be813
JN
400 NULL
401};
f6ab5bb2 402
2bd452d3
JH
403static unsigned char head_sha1[20];
404static unsigned char merge_head_sha1[20];
405
406static struct cache_entry *read_one_ent(const char *which,
407 unsigned char *ent, const char *path,
408 int namelen, int stage)
409{
410 unsigned mode;
411 unsigned char sha1[20];
412 int size;
413 struct cache_entry *ce;
414
415 if (get_tree_entry(ent, path, sha1, &mode)) {
83e77a25
JH
416 if (which)
417 error("%s: not in %s branch.", path, which);
2bd452d3
JH
418 return NULL;
419 }
420 if (mode == S_IFDIR) {
83e77a25
JH
421 if (which)
422 error("%s: not a blob in %s branch.", path, which);
2bd452d3
JH
423 return NULL;
424 }
425 size = cache_entry_size(namelen);
426 ce = xcalloc(1, size);
427
e702496e 428 hashcpy(ce->sha1, sha1);
2bd452d3 429 memcpy(ce->name, path, namelen);
b60e188c
TG
430 ce->ce_flags = create_ce_flags(stage);
431 ce->ce_namelen = namelen;
2bd452d3
JH
432 ce->ce_mode = create_ce_mode(mode);
433 return ce;
434}
435
436static int unresolve_one(const char *path)
437{
438 int namelen = strlen(path);
439 int pos;
440 int ret = 0;
441 struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
442
443 /* See if there is such entry in the index. */
444 pos = cache_name_pos(path, namelen);
8aa38563
JH
445 if (0 <= pos) {
446 /* already merged */
447 pos = unmerge_cache_entry_at(pos);
448 if (pos < active_nr) {
9c5e6c80 449 const struct cache_entry *ce = active_cache[pos];
8aa38563
JH
450 if (ce_stage(ce) &&
451 ce_namelen(ce) == namelen &&
452 !memcmp(ce->name, path, namelen))
453 return 0;
454 }
455 /* no resolve-undo information; fall back */
456 } else {
2bd452d3
JH
457 /* If there isn't, either it is unmerged, or
458 * resolved as "removed" by mistake. We do not
459 * want to do anything in the former case.
460 */
461 pos = -pos-1;
462 if (pos < active_nr) {
9c5e6c80 463 const struct cache_entry *ce = active_cache[pos];
2bd452d3
JH
464 if (ce_namelen(ce) == namelen &&
465 !memcmp(ce->name, path, namelen)) {
466 fprintf(stderr,
467 "%s: skipping still unmerged path.\n",
468 path);
469 goto free_return;
470 }
471 }
472 }
473
474 /* Grab blobs from given path from HEAD and MERGE_HEAD,
475 * stuff HEAD version in stage #2,
476 * stuff MERGE_HEAD version in stage #3.
477 */
478 ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
479 ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);
480
481 if (!ce_2 || !ce_3) {
482 ret = -1;
483 goto free_return;
484 }
a89fccd2 485 if (!hashcmp(ce_2->sha1, ce_3->sha1) &&
2bd452d3
JH
486 ce_2->ce_mode == ce_3->ce_mode) {
487 fprintf(stderr, "%s: identical in both, skipping.\n",
488 path);
489 goto free_return;
490 }
491
492 remove_file_from_cache(path);
493 if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
494 error("%s: cannot add our version to the index.", path);
495 ret = -1;
496 goto free_return;
497 }
498 if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
499 return 0;
500 error("%s: cannot add their version to the index.", path);
501 ret = -1;
502 free_return:
503 free(ce_2);
504 free(ce_3);
505 return ret;
506}
507
508static void read_head_pointers(void)
509{
913c983e 510 if (read_ref("HEAD", head_sha1))
d7530708 511 die("No HEAD -- no initial commit yet?");
913c983e 512 if (read_ref("MERGE_HEAD", merge_head_sha1)) {
2bd452d3
JH
513 fprintf(stderr, "Not in the middle of a merge.\n");
514 exit(0);
515 }
516}
517
09895c1f
JH
518static int do_unresolve(int ac, const char **av,
519 const char *prefix, int prefix_length)
2bd452d3
JH
520{
521 int i;
522 int err = 0;
523
524 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
525 * are not doing a merge, so exit with success status.
526 */
527 read_head_pointers();
528
529 for (i = 1; i < ac; i++) {
530 const char *arg = av[i];
09895c1f
JH
531 const char *p = prefix_path(prefix, prefix_length, arg);
532 err |= unresolve_one(p);
0cc9e70c 533 if (p < arg || p > arg + strlen(arg))
4b25d091 534 free((char *)p);
2bd452d3
JH
535 }
536 return err;
537}
538
83e77a25
JH
539static int do_reupdate(int ac, const char **av,
540 const char *prefix, int prefix_length)
541{
542 /* Read HEAD and run update-index on paths that are
543 * merged and already different between index and HEAD.
544 */
545 int pos;
546 int has_head = 1;
eb9cb55b
NTND
547 struct pathspec pathspec;
548
0fdc2ae5
NTND
549 parse_pathspec(&pathspec, 0,
550 PATHSPEC_PREFER_CWD,
551 prefix, av + 1);
83e77a25 552
913c983e 553 if (read_ref("HEAD", head_sha1))
83e77a25
JH
554 /* If there is no HEAD, that means it is an initial
555 * commit. Update everything in the index.
556 */
557 has_head = 0;
558 redo:
559 for (pos = 0; pos < active_nr; pos++) {
9c5e6c80 560 const struct cache_entry *ce = active_cache[pos];
83e77a25
JH
561 struct cache_entry *old = NULL;
562 int save_nr;
5699d17e 563 char *path;
22293b9c 564
429bb40a 565 if (ce_stage(ce) || !ce_path_match(ce, &pathspec, NULL))
83e77a25
JH
566 continue;
567 if (has_head)
568 old = read_one_ent(NULL, head_sha1,
569 ce->name, ce_namelen(ce), 0);
570 if (old && ce->ce_mode == old->ce_mode &&
a89fccd2 571 !hashcmp(ce->sha1, old->sha1)) {
83e77a25
JH
572 free(old);
573 continue; /* unchanged */
574 }
575 /* Be careful. The working tree may not have the
576 * path anymore, in which case, under 'allow_remove',
577 * or worse yet 'allow_replace', active_nr may decrease.
578 */
579 save_nr = active_nr;
5699d17e
KB
580 path = xstrdup(ce->name);
581 update_one(path);
582 free(path);
83e77a25
JH
583 if (save_nr != active_nr)
584 goto redo;
585 }
eb9cb55b 586 free_pathspec(&pathspec);
83e77a25
JH
587 return 0;
588}
589
309be813
JN
590struct refresh_params {
591 unsigned int flags;
592 int *has_errors;
593};
594
595static int refresh(struct refresh_params *o, unsigned int flag)
596{
597 setup_work_tree();
7349afd2 598 read_cache_preload(NULL);
309be813
JN
599 *o->has_errors |= refresh_cache(o->flags | flag);
600 return 0;
601}
602
603static int refresh_callback(const struct option *opt,
604 const char *arg, int unset)
605{
606 return refresh(opt->value, 0);
607}
608
609static int really_refresh_callback(const struct option *opt,
610 const char *arg, int unset)
611{
612 return refresh(opt->value, REFRESH_REALLY);
613}
614
615static int chmod_callback(const struct option *opt,
616 const char *arg, int unset)
617{
618 char *flip = opt->value;
619 if ((arg[0] != '-' && arg[0] != '+') || arg[1] != 'x' || arg[2])
620 return error("option 'chmod' expects \"+x\" or \"-x\"");
621 *flip = arg[0];
622 return 0;
623}
624
625static int resolve_undo_clear_callback(const struct option *opt,
626 const char *arg, int unset)
627{
628 resolve_undo_clear();
629 return 0;
630}
631
ec160ae1
JH
632static int parse_new_style_cacheinfo(const char *arg,
633 unsigned int *mode,
634 unsigned char sha1[],
635 const char **path)
636{
637 unsigned long ul;
638 char *endp;
639
640 errno = 0;
641 ul = strtoul(arg, &endp, 8);
642 if (errno || endp == arg || *endp != ',' || (unsigned int) ul != ul)
643 return -1; /* not a new-style cacheinfo */
644 *mode = ul;
645 endp++;
646 if (get_sha1_hex(endp, sha1) || endp[40] != ',')
647 return -1;
648 *path = endp + 41;
649 return 0;
650}
651
309be813
JN
652static int cacheinfo_callback(struct parse_opt_ctx_t *ctx,
653 const struct option *opt, int unset)
654{
655 unsigned char sha1[20];
656 unsigned int mode;
ec160ae1 657 const char *path;
309be813 658
ec160ae1
JH
659 if (!parse_new_style_cacheinfo(ctx->argv[1], &mode, sha1, &path)) {
660 if (add_cacheinfo(mode, sha1, path, 0))
661 die("git update-index: --cacheinfo cannot add %s", path);
662 ctx->argv++;
663 ctx->argc--;
664 return 0;
665 }
309be813 666 if (ctx->argc <= 3)
ec160ae1 667 return error("option 'cacheinfo' expects <mode>,<sha1>,<path>");
309be813
JN
668 if (strtoul_ui(*++ctx->argv, 8, &mode) ||
669 get_sha1_hex(*++ctx->argv, sha1) ||
670 add_cacheinfo(mode, sha1, *++ctx->argv, 0))
671 die("git update-index: --cacheinfo cannot add %s", *ctx->argv);
672 ctx->argc -= 3;
673 return 0;
674}
675
676static int stdin_cacheinfo_callback(struct parse_opt_ctx_t *ctx,
677 const struct option *opt, int unset)
678{
679 int *line_termination = opt->value;
680
681 if (ctx->argc != 1)
682 return error("option '%s' must be the last argument", opt->long_name);
683 allow_add = allow_replace = allow_remove = 1;
684 read_index_info(*line_termination);
685 return 0;
686}
687
688static int stdin_callback(struct parse_opt_ctx_t *ctx,
689 const struct option *opt, int unset)
690{
691 int *read_from_stdin = opt->value;
692
693 if (ctx->argc != 1)
694 return error("option '%s' must be the last argument", opt->long_name);
695 *read_from_stdin = 1;
696 return 0;
697}
698
699static int unresolve_callback(struct parse_opt_ctx_t *ctx,
700 const struct option *opt, int flags)
701{
702 int *has_errors = opt->value;
703 const char *prefix = startup_info->prefix;
704
705 /* consume remaining arguments. */
706 *has_errors = do_unresolve(ctx->argc, ctx->argv,
707 prefix, prefix ? strlen(prefix) : 0);
708 if (*has_errors)
709 active_cache_changed = 0;
710
711 ctx->argv += ctx->argc - 1;
712 ctx->argc = 1;
713 return 0;
714}
715
716static int reupdate_callback(struct parse_opt_ctx_t *ctx,
717 const struct option *opt, int flags)
718{
719 int *has_errors = opt->value;
720 const char *prefix = startup_info->prefix;
721
722 /* consume remaining arguments. */
723 setup_work_tree();
724 *has_errors = do_reupdate(ctx->argc, ctx->argv,
725 prefix, prefix ? strlen(prefix) : 0);
726 if (*has_errors)
727 active_cache_changed = 0;
728
729 ctx->argv += ctx->argc - 1;
730 ctx->argc = 1;
731 return 0;
732}
733
a633fca0 734int cmd_update_index(int argc, const char **argv, const char *prefix)
e83c5163 735{
309be813 736 int newfd, entries, has_errors = 0, line_termination = '\n';
ee1bec3d 737 int read_from_stdin = 0;
ee1bec3d 738 int prefix_length = prefix ? strlen(prefix) : 0;
69dec66b 739 int preferred_index_format = 0;
227bdb18 740 char set_executable_bit = 0;
309be813 741 struct refresh_params refresh_args = {0, &has_errors};
7b802b86 742 int lock_error = 0;
fefe81c9 743 struct lock_file *lock_file;
309be813
JN
744 struct parse_opt_ctx_t ctx;
745 int parseopt_state = PARSE_OPT_UNKNOWN;
746 struct option options[] = {
747 OPT_BIT('q', NULL, &refresh_args.flags,
4a4838b4 748 N_("continue refresh even when index needs update"),
309be813
JN
749 REFRESH_QUIET),
750 OPT_BIT(0, "ignore-submodules", &refresh_args.flags,
4a4838b4 751 N_("refresh: ignore submodules"),
309be813
JN
752 REFRESH_IGNORE_SUBMODULES),
753 OPT_SET_INT(0, "add", &allow_add,
4a4838b4 754 N_("do not ignore new files"), 1),
309be813 755 OPT_SET_INT(0, "replace", &allow_replace,
4a4838b4 756 N_("let files replace directories and vice-versa"), 1),
309be813 757 OPT_SET_INT(0, "remove", &allow_remove,
4a4838b4 758 N_("notice files missing from worktree"), 1),
309be813 759 OPT_BIT(0, "unmerged", &refresh_args.flags,
4a4838b4 760 N_("refresh even if index contains unmerged entries"),
309be813
JN
761 REFRESH_UNMERGED),
762 {OPTION_CALLBACK, 0, "refresh", &refresh_args, NULL,
4a4838b4 763 N_("refresh stat information"),
309be813
JN
764 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
765 refresh_callback},
766 {OPTION_CALLBACK, 0, "really-refresh", &refresh_args, NULL,
4a4838b4 767 N_("like --refresh, but ignore assume-unchanged setting"),
309be813
JN
768 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
769 really_refresh_callback},
770 {OPTION_LOWLEVEL_CALLBACK, 0, "cacheinfo", NULL,
ec160ae1 771 N_("<mode>,<object>,<path>"),
4a4838b4 772 N_("add the specified entry to the index"),
ec160ae1 773 PARSE_OPT_NOARG | /* disallow --cacheinfo=<mode> form */
309be813
JN
774 PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
775 (parse_opt_cb *) cacheinfo_callback},
4a4838b4
NTND
776 {OPTION_CALLBACK, 0, "chmod", &set_executable_bit, N_("(+/-)x"),
777 N_("override the executable bit of the listed files"),
309be813
JN
778 PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
779 chmod_callback},
780 {OPTION_SET_INT, 0, "assume-unchanged", &mark_valid_only, NULL,
4a4838b4 781 N_("mark files as \"not changing\""),
309be813
JN
782 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
783 {OPTION_SET_INT, 0, "no-assume-unchanged", &mark_valid_only, NULL,
4a4838b4 784 N_("clear assumed-unchanged bit"),
309be813
JN
785 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
786 {OPTION_SET_INT, 0, "skip-worktree", &mark_skip_worktree_only, NULL,
4a4838b4 787 N_("mark files as \"index-only\""),
309be813
JN
788 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
789 {OPTION_SET_INT, 0, "no-skip-worktree", &mark_skip_worktree_only, NULL,
4a4838b4 790 N_("clear skip-worktree bit"),
309be813
JN
791 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
792 OPT_SET_INT(0, "info-only", &info_only,
4a4838b4 793 N_("add to index only; do not add content to object database"), 1),
309be813 794 OPT_SET_INT(0, "force-remove", &force_remove,
4a4838b4 795 N_("remove named paths even if present in worktree"), 1),
309be813 796 OPT_SET_INT('z', NULL, &line_termination,
4a4838b4 797 N_("with --stdin: input lines are terminated by null bytes"), '\0'),
309be813 798 {OPTION_LOWLEVEL_CALLBACK, 0, "stdin", &read_from_stdin, NULL,
4a4838b4 799 N_("read list of paths to be updated from standard input"),
309be813
JN
800 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
801 (parse_opt_cb *) stdin_callback},
802 {OPTION_LOWLEVEL_CALLBACK, 0, "index-info", &line_termination, NULL,
4a4838b4 803 N_("add entries from standard input to the index"),
309be813
JN
804 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
805 (parse_opt_cb *) stdin_cacheinfo_callback},
806 {OPTION_LOWLEVEL_CALLBACK, 0, "unresolve", &has_errors, NULL,
4a4838b4 807 N_("repopulate stages #2 and #3 for the listed paths"),
309be813
JN
808 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
809 (parse_opt_cb *) unresolve_callback},
810 {OPTION_LOWLEVEL_CALLBACK, 'g', "again", &has_errors, NULL,
4a4838b4 811 N_("only update entries that differ from HEAD"),
309be813
JN
812 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
813 (parse_opt_cb *) reupdate_callback},
814 OPT_BIT(0, "ignore-missing", &refresh_args.flags,
4a4838b4 815 N_("ignore files missing from worktree"),
309be813
JN
816 REFRESH_IGNORE_MISSING),
817 OPT_SET_INT(0, "verbose", &verbose,
4a4838b4 818 N_("report actions to standard output"), 1),
309be813 819 {OPTION_CALLBACK, 0, "clear-resolve-undo", NULL, NULL,
4a4838b4 820 N_("(for porcelains) forget saved unresolved conflicts"),
309be813
JN
821 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
822 resolve_undo_clear_callback},
69dec66b 823 OPT_INTEGER(0, "index-version", &preferred_index_format,
4a4838b4 824 N_("write index in this format")),
309be813
JN
825 OPT_END()
826 };
bb233d69 827
9c7c27ee 828 if (argc == 2 && !strcmp(argv[1], "-h"))
b04d930b 829 usage_with_options(update_index_usage, options);
9c7c27ee 830
ef90d6d4 831 git_config(git_default_config, NULL);
3e09cdfd 832
fefe81c9 833 /* We can't free this memory, it becomes part of a linked list parsed atexit() */
928e47e3 834 lock_file = xcalloc(1, sizeof(struct lock_file));
fefe81c9 835
30ca07a2 836 newfd = hold_locked_index(lock_file, 0);
7b802b86
JH
837 if (newfd < 0)
838 lock_error = errno;
9614b8dc 839
e83c5163 840 entries = read_cache();
9614b8dc 841 if (entries < 0)
2de381f9 842 die("cache corrupted");
e83c5163 843
309be813
JN
844 /*
845 * Custom copy of parse_options() because we want to handle
846 * filename arguments as they come.
847 */
848 parse_options_start(&ctx, argc, argv, prefix,
849 options, PARSE_OPT_STOP_AT_NON_OPTION);
850 while (ctx.argc) {
851 if (parseopt_state != PARSE_OPT_DONE)
852 parseopt_state = parse_options_step(&ctx, options,
853 update_index_usage);
854 if (!ctx.argc)
855 break;
856 switch (parseopt_state) {
857 case PARSE_OPT_HELP:
858 exit(129);
859 case PARSE_OPT_NON_OPTION:
860 case PARSE_OPT_DONE:
861 {
862 const char *path = ctx.argv[0];
863 const char *p;
121481ab 864
309be813
JN
865 setup_work_tree();
866 p = prefix_path(prefix, prefix_length, path);
6bb69077 867 update_one(p);
309be813
JN
868 if (set_executable_bit)
869 chmod_path(set_executable_bit, p);
6bb69077 870 free((char *)p);
309be813
JN
871 ctx.argc--;
872 ctx.argv++;
873 break;
874 }
875 case PARSE_OPT_UNKNOWN:
876 if (ctx.argv[0][1] == '-')
877 error("unknown option '%s'", ctx.argv[0] + 2);
878 else
879 error("unknown switch '%c'", *ctx.opt);
880 usage_with_options(update_index_usage, options);
121481ab 881 }
ee1bec3d 882 }
309be813 883 argc = parse_options_end(&ctx);
69dec66b
JH
884 if (preferred_index_format) {
885 if (preferred_index_format < INDEX_FORMAT_LB ||
886 INDEX_FORMAT_UB < preferred_index_format)
887 die("index-version %d not in range: %d..%d",
888 preferred_index_format,
889 INDEX_FORMAT_LB, INDEX_FORMAT_UB);
890
891 if (the_index.version != preferred_index_format)
782a5ff9 892 active_cache_changed |= SOMETHING_CHANGED;
69dec66b
JH
893 the_index.version = preferred_index_format;
894 }
309be813 895
ee1bec3d 896 if (read_from_stdin) {
f285a2d7 897 struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
7fb1011e 898
f83eafdd 899 setup_work_tree();
7fb1011e 900 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
fb69a760 901 const char *p;
7fb1011e
PH
902 if (line_termination && buf.buf[0] == '"') {
903 strbuf_reset(&nbuf);
904 if (unquote_c_style(&nbuf, buf.buf, NULL))
905 die("line is badly quoted");
906 strbuf_swap(&buf, &nbuf);
907 }
908 p = prefix_path(prefix, prefix_length, buf.buf);
6bb69077 909 update_one(p);
fb69a760 910 if (set_executable_bit)
227bdb18 911 chmod_path(set_executable_bit, p);
6bb69077 912 free((char *)p);
9b63f501 913 }
7fb1011e 914 strbuf_release(&nbuf);
e6c019d0 915 strbuf_release(&buf);
e83c5163 916 }
2bd452d3 917
5cd5ace7 918 if (active_cache_changed) {
7b802b86 919 if (newfd < 0) {
309be813 920 if (refresh_args.flags & REFRESH_QUIET)
7b802b86 921 exit(128);
e43a6fd3 922 unable_to_lock_index_die(get_index_file(), lock_error);
7b802b86 923 }
03b86647 924 if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
021b6e45 925 die("Unable to write new index file");
5cd5ace7 926 }
9614b8dc 927
fefe81c9
LS
928 rollback_lock_file(lock_file);
929
c4b83e61 930 return has_errors ? 1 : 0;
e83c5163 931}