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