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