1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "cache.h"
7#include "quote.h"
8#include "cache-tree.h"
9#include "tree-walk.h"
10#include "builtin.h"
11#include "refs.h"
12
13/*
14 * Default to not allowing changes to the list of files. The
15 * tool doesn't actually care, but this makes it harder to add
16 * files to the revision control by mistake by doing something
17 * like "git update-index *" and suddenly having all the object
18 * files be revision controlled.
19 */
20static int allow_add;
21static int allow_remove;
22static int allow_replace;
23static int info_only;
24static int force_remove;
25static int verbose;
26static int mark_valid_only;
27#define MARK_FLAG 1
28#define UNMARK_FLAG 2
29
30static void report(const char *fmt, ...)
31{
32 va_list vp;
33
34 if (!verbose)
35 return;
36
37 va_start(vp, fmt);
38 vprintf(fmt, vp);
39 putchar('\n');
40 va_end(vp);
41}
42
43static int mark_ce_flags(const char *path, int flag, int mark)
44{
45 int namelen = strlen(path);
46 int pos = cache_name_pos(path, namelen);
47 if (0 <= pos) {
48 if (mark)
49 active_cache[pos]->ce_flags |= flag;
50 else
51 active_cache[pos]->ce_flags &= ~flag;
52 cache_tree_invalidate_path(active_cache_tree, path);
53 active_cache_changed = 1;
54 return 0;
55 }
56 return -1;
57}
58
59static int remove_one_path(const char *path)
60{
61 if (!allow_remove)
62 return error("%s: does not exist and --remove not passed", path);
63 if (remove_file_from_cache(path))
64 return error("%s: cannot remove from the index", path);
65 return 0;
66}
67
68/*
69 * Handle a path that couldn't be lstat'ed. It's either:
70 * - missing file (ENOENT or ENOTDIR). That's ok if we're
71 * supposed to be removing it and the removal actually
72 * succeeds.
73 * - permission error. That's never ok.
74 */
75static int process_lstat_error(const char *path, int err)
76{
77 if (err == ENOENT || err == ENOTDIR)
78 return remove_one_path(path);
79 return error("lstat(\"%s\"): %s", path, strerror(errno));
80}
81
82static int add_one_path(struct cache_entry *old, const char *path, int len, struct stat *st)
83{
84 int option, size;
85 struct cache_entry *ce;
86
87 /* Was the old index entry already up-to-date? */
88 if (old && !ce_stage(old) && !ce_match_stat(old, st, 0))
89 return 0;
90
91 size = cache_entry_size(len);
92 ce = xcalloc(1, size);
93 memcpy(ce->name, path, len);
94 ce->ce_flags = len;
95 fill_stat_cache_info(ce, st);
96 ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
97
98 if (index_path(ce->sha1, path, st, !info_only))
99 return -1;
100 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
101 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
102 if (add_cache_entry(ce, option))
103 return error("%s: cannot add to the index - missing --add option?", path);
104 return 0;
105}
106
107/*
108 * Handle a path that was a directory. Four cases:
109 *
110 * - it's already a gitlink in the index, and we keep it that
111 * way, and update it if we can (if we cannot find the HEAD,
112 * we're going to keep it unchanged in the index!)
113 *
114 * - it's a *file* in the index, in which case it should be
115 * removed as a file if removal is allowed, since it doesn't
116 * exist as such any more. If removal isn't allowed, it's
117 * an error.
118 *
119 * (NOTE! This is old and arguably fairly strange behaviour.
120 * We might want to make this an error unconditionally, and
121 * use "--force-remove" if you actually want to force removal).
122 *
123 * - it used to exist as a subdirectory (ie multiple files with
124 * this particular prefix) in the index, in which case it's wrong
125 * to try to update it as a directory.
126 *
127 * - it doesn't exist at all in the index, but it is a valid
128 * git directory, and it should be *added* as a gitlink.
129 */
130static int process_directory(const char *path, int len, struct stat *st)
131{
132 unsigned char sha1[20];
133 int pos = cache_name_pos(path, len);
134
135 /* Exact match: file or existing gitlink */
136 if (pos >= 0) {
137 struct cache_entry *ce = active_cache[pos];
138 if (S_ISGITLINK(ce->ce_mode)) {
139
140 /* Do nothing to the index if there is no HEAD! */
141 if (resolve_gitlink_ref(path, "HEAD", sha1) < 0)
142 return 0;
143
144 return add_one_path(ce, path, len, st);
145 }
146 /* Should this be an unconditional error? */
147 return remove_one_path(path);
148 }
149
150 /* Inexact match: is there perhaps a subdirectory match? */
151 pos = -pos-1;
152 while (pos < active_nr) {
153 struct cache_entry *ce = active_cache[pos++];
154
155 if (strncmp(ce->name, path, len))
156 break;
157 if (ce->name[len] > '/')
158 break;
159 if (ce->name[len] < '/')
160 continue;
161
162 /* Subdirectory match - error out */
163 return error("%s: is a directory - add individual files instead", path);
164 }
165
166 /* No match - should we add it as a gitlink? */
167 if (!resolve_gitlink_ref(path, "HEAD", sha1))
168 return add_one_path(NULL, path, len, st);
169
170 /* Error out. */
171 return error("%s: is a directory - add files inside instead", path);
172}
173
174/*
175 * Process a regular file
176 */
177static int process_file(const char *path, int len, struct stat *st)
178{
179 int pos = cache_name_pos(path, len);
180 struct cache_entry *ce = pos < 0 ? NULL : active_cache[pos];
181
182 if (ce && S_ISGITLINK(ce->ce_mode))
183 return error("%s is already a gitlink, not replacing", path);
184
185 return add_one_path(ce, path, len, st);
186}
187
188static int process_path(const char *path)
189{
190 int len;
191 struct stat st;
192
193 len = strlen(path);
194 if (has_symlink_leading_path(path, len))
195 return error("'%s' is beyond a symbolic link", path);
196
197 /*
198 * First things first: get the stat information, to decide
199 * what to do about the pathname!
200 */
201 if (lstat(path, &st) < 0)
202 return process_lstat_error(path, errno);
203
204 if (S_ISDIR(st.st_mode))
205 return process_directory(path, len, &st);
206
207 return process_file(path, len, &st);
208}
209
210static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
211 const char *path, int stage)
212{
213 int size, len, option;
214 struct cache_entry *ce;
215
216 if (!verify_path(path))
217 return error("Invalid path '%s'", path);
218
219 len = strlen(path);
220 size = cache_entry_size(len);
221 ce = xcalloc(1, size);
222
223 hashcpy(ce->sha1, sha1);
224 memcpy(ce->name, path, len);
225 ce->ce_flags = create_ce_flags(len, stage);
226 ce->ce_mode = create_ce_mode(mode);
227 if (assume_unchanged)
228 ce->ce_flags |= CE_VALID;
229 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
230 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
231 if (add_cache_entry(ce, option))
232 return error("%s: cannot add to the index - missing --add option?",
233 path);
234 report("add '%s'", path);
235 return 0;
236}
237
238static void chmod_path(int flip, const char *path)
239{
240 int pos;
241 struct cache_entry *ce;
242 unsigned int mode;
243
244 pos = cache_name_pos(path, strlen(path));
245 if (pos < 0)
246 goto fail;
247 ce = active_cache[pos];
248 mode = ce->ce_mode;
249 if (!S_ISREG(mode))
250 goto fail;
251 switch (flip) {
252 case '+':
253 ce->ce_mode |= 0111; break;
254 case '-':
255 ce->ce_mode &= ~0111; break;
256 default:
257 goto fail;
258 }
259 cache_tree_invalidate_path(active_cache_tree, path);
260 active_cache_changed = 1;
261 report("chmod %cx '%s'", flip, path);
262 return;
263 fail:
264 die("git update-index: cannot chmod %cx '%s'", flip, path);
265}
266
267static void update_one(const char *path, const char *prefix, int prefix_length)
268{
269 const char *p = prefix_path(prefix, prefix_length, path);
270 if (!verify_path(p)) {
271 fprintf(stderr, "Ignoring path %s\n", path);
272 goto free_return;
273 }
274 if (mark_valid_only) {
275 if (mark_ce_flags(p, CE_VALID, mark_valid_only == MARK_FLAG))
276 die("Unable to mark file %s", path);
277 goto free_return;
278 }
279
280 if (force_remove) {
281 if (remove_file_from_cache(p))
282 die("git update-index: unable to remove %s", path);
283 report("remove '%s'", path);
284 goto free_return;
285 }
286 if (process_path(p))
287 die("Unable to process path %s", path);
288 report("add '%s'", path);
289 free_return:
290 if (p < path || p > path + strlen(path))
291 free((char *)p);
292}
293
294static void read_index_info(int line_termination)
295{
296 struct strbuf buf = STRBUF_INIT;
297 struct strbuf uq = STRBUF_INIT;
298
299 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
300 char *ptr, *tab;
301 char *path_name;
302 unsigned char sha1[20];
303 unsigned int mode;
304 unsigned long ul;
305 int stage;
306
307 /* This reads lines formatted in one of three formats:
308 *
309 * (1) mode SP sha1 TAB path
310 * The first format is what "git apply --index-info"
311 * reports, and used to reconstruct a partial tree
312 * that is used for phony merge base tree when falling
313 * back on 3-way merge.
314 *
315 * (2) mode SP type SP sha1 TAB path
316 * The second format is to stuff "git ls-tree" output
317 * into the index file.
318 *
319 * (3) mode SP sha1 SP stage TAB path
320 * This format is to put higher order stages into the
321 * index file and matches "git ls-files --stage" output.
322 */
323 errno = 0;
324 ul = strtoul(buf.buf, &ptr, 8);
325 if (ptr == buf.buf || *ptr != ' '
326 || errno || (unsigned int) ul != ul)
327 goto bad_line;
328 mode = ul;
329
330 tab = strchr(ptr, '\t');
331 if (!tab || tab - ptr < 41)
332 goto bad_line;
333
334 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
335 stage = tab[-1] - '0';
336 ptr = tab + 1; /* point at the head of path */
337 tab = tab - 2; /* point at tail of sha1 */
338 }
339 else {
340 stage = 0;
341 ptr = tab + 1; /* point at the head of path */
342 }
343
344 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
345 goto bad_line;
346
347 path_name = ptr;
348 if (line_termination && path_name[0] == '"') {
349 strbuf_reset(&uq);
350 if (unquote_c_style(&uq, path_name, NULL)) {
351 die("git update-index: bad quoting of path name");
352 }
353 path_name = uq.buf;
354 }
355
356 if (!verify_path(path_name)) {
357 fprintf(stderr, "Ignoring path %s\n", path_name);
358 continue;
359 }
360
361 if (!mode) {
362 /* mode == 0 means there is no such path -- remove */
363 if (remove_file_from_cache(path_name))
364 die("git update-index: unable to remove %s",
365 ptr);
366 }
367 else {
368 /* mode ' ' sha1 '\t' name
369 * ptr[-1] points at tab,
370 * ptr[-41] is at the beginning of sha1
371 */
372 ptr[-42] = ptr[-1] = 0;
373 if (add_cacheinfo(mode, sha1, path_name, stage))
374 die("git update-index: unable to update %s",
375 path_name);
376 }
377 continue;
378
379 bad_line:
380 die("malformed index info %s", buf.buf);
381 }
382 strbuf_release(&buf);
383 strbuf_release(&uq);
384}
385
386static const char update_index_usage[] =
387"git update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--really-refresh] [--cacheinfo] [--chmod=(+|-)x] [--assume-unchanged] [--info-only] [--force-remove] [--stdin] [--index-info] [--unresolve] [--again | -g] [--ignore-missing] [-z] [--verbose] [--] <file>...";
388
389static unsigned char head_sha1[20];
390static unsigned char merge_head_sha1[20];
391
392static struct cache_entry *read_one_ent(const char *which,
393 unsigned char *ent, const char *path,
394 int namelen, int stage)
395{
396 unsigned mode;
397 unsigned char sha1[20];
398 int size;
399 struct cache_entry *ce;
400
401 if (get_tree_entry(ent, path, sha1, &mode)) {
402 if (which)
403 error("%s: not in %s branch.", path, which);
404 return NULL;
405 }
406 if (mode == S_IFDIR) {
407 if (which)
408 error("%s: not a blob in %s branch.", path, which);
409 return NULL;
410 }
411 size = cache_entry_size(namelen);
412 ce = xcalloc(1, size);
413
414 hashcpy(ce->sha1, sha1);
415 memcpy(ce->name, path, namelen);
416 ce->ce_flags = create_ce_flags(namelen, stage);
417 ce->ce_mode = create_ce_mode(mode);
418 return ce;
419}
420
421static int unresolve_one(const char *path)
422{
423 int namelen = strlen(path);
424 int pos;
425 int ret = 0;
426 struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
427
428 /* See if there is such entry in the index. */
429 pos = cache_name_pos(path, namelen);
430 if (pos < 0) {
431 /* If there isn't, either it is unmerged, or
432 * resolved as "removed" by mistake. We do not
433 * want to do anything in the former case.
434 */
435 pos = -pos-1;
436 if (pos < active_nr) {
437 struct cache_entry *ce = active_cache[pos];
438 if (ce_namelen(ce) == namelen &&
439 !memcmp(ce->name, path, namelen)) {
440 fprintf(stderr,
441 "%s: skipping still unmerged path.\n",
442 path);
443 goto free_return;
444 }
445 }
446 }
447
448 /* Grab blobs from given path from HEAD and MERGE_HEAD,
449 * stuff HEAD version in stage #2,
450 * stuff MERGE_HEAD version in stage #3.
451 */
452 ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
453 ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);
454
455 if (!ce_2 || !ce_3) {
456 ret = -1;
457 goto free_return;
458 }
459 if (!hashcmp(ce_2->sha1, ce_3->sha1) &&
460 ce_2->ce_mode == ce_3->ce_mode) {
461 fprintf(stderr, "%s: identical in both, skipping.\n",
462 path);
463 goto free_return;
464 }
465
466 remove_file_from_cache(path);
467 if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
468 error("%s: cannot add our version to the index.", path);
469 ret = -1;
470 goto free_return;
471 }
472 if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
473 return 0;
474 error("%s: cannot add their version to the index.", path);
475 ret = -1;
476 free_return:
477 free(ce_2);
478 free(ce_3);
479 return ret;
480}
481
482static void read_head_pointers(void)
483{
484 if (read_ref("HEAD", head_sha1))
485 die("No HEAD -- no initial commit yet?");
486 if (read_ref("MERGE_HEAD", merge_head_sha1)) {
487 fprintf(stderr, "Not in the middle of a merge.\n");
488 exit(0);
489 }
490}
491
492static int do_unresolve(int ac, const char **av,
493 const char *prefix, int prefix_length)
494{
495 int i;
496 int err = 0;
497
498 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
499 * are not doing a merge, so exit with success status.
500 */
501 read_head_pointers();
502
503 for (i = 1; i < ac; i++) {
504 const char *arg = av[i];
505 const char *p = prefix_path(prefix, prefix_length, arg);
506 err |= unresolve_one(p);
507 if (p < arg || p > arg + strlen(arg))
508 free((char *)p);
509 }
510 return err;
511}
512
513static int do_reupdate(int ac, const char **av,
514 const char *prefix, int prefix_length)
515{
516 /* Read HEAD and run update-index on paths that are
517 * merged and already different between index and HEAD.
518 */
519 int pos;
520 int has_head = 1;
521 const char **pathspec = get_pathspec(prefix, av + 1);
522
523 if (read_ref("HEAD", head_sha1))
524 /* If there is no HEAD, that means it is an initial
525 * commit. Update everything in the index.
526 */
527 has_head = 0;
528 redo:
529 for (pos = 0; pos < active_nr; pos++) {
530 struct cache_entry *ce = active_cache[pos];
531 struct cache_entry *old = NULL;
532 int save_nr;
533
534 if (ce_stage(ce) || !ce_path_match(ce, pathspec))
535 continue;
536 if (has_head)
537 old = read_one_ent(NULL, head_sha1,
538 ce->name, ce_namelen(ce), 0);
539 if (old && ce->ce_mode == old->ce_mode &&
540 !hashcmp(ce->sha1, old->sha1)) {
541 free(old);
542 continue; /* unchanged */
543 }
544 /* Be careful. The working tree may not have the
545 * path anymore, in which case, under 'allow_remove',
546 * or worse yet 'allow_replace', active_nr may decrease.
547 */
548 save_nr = active_nr;
549 update_one(ce->name + prefix_length, prefix, prefix_length);
550 if (save_nr != active_nr)
551 goto redo;
552 }
553 return 0;
554}
555
556int cmd_update_index(int argc, const char **argv, const char *prefix)
557{
558 int i, newfd, entries, has_errors = 0, line_termination = '\n';
559 int allow_options = 1;
560 int read_from_stdin = 0;
561 int prefix_length = prefix ? strlen(prefix) : 0;
562 char set_executable_bit = 0;
563 unsigned int refresh_flags = 0;
564 int lock_error = 0;
565 struct lock_file *lock_file;
566
567 git_config(git_default_config, NULL);
568
569 /* We can't free this memory, it becomes part of a linked list parsed atexit() */
570 lock_file = xcalloc(1, sizeof(struct lock_file));
571
572 newfd = hold_locked_index(lock_file, 0);
573 if (newfd < 0)
574 lock_error = errno;
575
576 entries = read_cache();
577 if (entries < 0)
578 die("cache corrupted");
579
580 for (i = 1 ; i < argc; i++) {
581 const char *path = argv[i];
582 const char *p;
583
584 if (allow_options && *path == '-') {
585 if (!strcmp(path, "--")) {
586 allow_options = 0;
587 continue;
588 }
589 if (!strcmp(path, "-q")) {
590 refresh_flags |= REFRESH_QUIET;
591 continue;
592 }
593 if (!strcmp(path, "--ignore-submodules")) {
594 refresh_flags |= REFRESH_IGNORE_SUBMODULES;
595 continue;
596 }
597 if (!strcmp(path, "--add")) {
598 allow_add = 1;
599 continue;
600 }
601 if (!strcmp(path, "--replace")) {
602 allow_replace = 1;
603 continue;
604 }
605 if (!strcmp(path, "--remove")) {
606 allow_remove = 1;
607 continue;
608 }
609 if (!strcmp(path, "--unmerged")) {
610 refresh_flags |= REFRESH_UNMERGED;
611 continue;
612 }
613 if (!strcmp(path, "--refresh")) {
614 setup_work_tree();
615 has_errors |= refresh_cache(refresh_flags);
616 continue;
617 }
618 if (!strcmp(path, "--really-refresh")) {
619 setup_work_tree();
620 has_errors |= refresh_cache(REFRESH_REALLY | refresh_flags);
621 continue;
622 }
623 if (!strcmp(path, "--cacheinfo")) {
624 unsigned char sha1[20];
625 unsigned int mode;
626
627 if (i+3 >= argc)
628 die("git update-index: --cacheinfo <mode> <sha1> <path>");
629
630 if (strtoul_ui(argv[i+1], 8, &mode) ||
631 get_sha1_hex(argv[i+2], sha1) ||
632 add_cacheinfo(mode, sha1, argv[i+3], 0))
633 die("git update-index: --cacheinfo"
634 " cannot add %s", argv[i+3]);
635 i += 3;
636 continue;
637 }
638 if (!strcmp(path, "--chmod=-x") ||
639 !strcmp(path, "--chmod=+x")) {
640 if (argc <= i+1)
641 die("git update-index: %s <path>", path);
642 set_executable_bit = path[8];
643 continue;
644 }
645 if (!strcmp(path, "--assume-unchanged")) {
646 mark_valid_only = MARK_FLAG;
647 continue;
648 }
649 if (!strcmp(path, "--no-assume-unchanged")) {
650 mark_valid_only = UNMARK_FLAG;
651 continue;
652 }
653 if (!strcmp(path, "--info-only")) {
654 info_only = 1;
655 continue;
656 }
657 if (!strcmp(path, "--force-remove")) {
658 force_remove = 1;
659 continue;
660 }
661 if (!strcmp(path, "-z")) {
662 line_termination = 0;
663 continue;
664 }
665 if (!strcmp(path, "--stdin")) {
666 if (i != argc - 1)
667 die("--stdin must be at the end");
668 read_from_stdin = 1;
669 break;
670 }
671 if (!strcmp(path, "--index-info")) {
672 if (i != argc - 1)
673 die("--index-info must be at the end");
674 allow_add = allow_replace = allow_remove = 1;
675 read_index_info(line_termination);
676 break;
677 }
678 if (!strcmp(path, "--unresolve")) {
679 has_errors = do_unresolve(argc - i, argv + i,
680 prefix, prefix_length);
681 if (has_errors)
682 active_cache_changed = 0;
683 goto finish;
684 }
685 if (!strcmp(path, "--again") || !strcmp(path, "-g")) {
686 setup_work_tree();
687 has_errors = do_reupdate(argc - i, argv + i,
688 prefix, prefix_length);
689 if (has_errors)
690 active_cache_changed = 0;
691 goto finish;
692 }
693 if (!strcmp(path, "--ignore-missing")) {
694 refresh_flags |= REFRESH_IGNORE_MISSING;
695 continue;
696 }
697 if (!strcmp(path, "--verbose")) {
698 verbose = 1;
699 continue;
700 }
701 if (!strcmp(path, "-h") || !strcmp(path, "--help"))
702 usage(update_index_usage);
703 die("unknown option %s", path);
704 }
705 setup_work_tree();
706 p = prefix_path(prefix, prefix_length, path);
707 update_one(p, NULL, 0);
708 if (set_executable_bit)
709 chmod_path(set_executable_bit, p);
710 if (p < path || p > path + strlen(path))
711 free((char *)p);
712 }
713 if (read_from_stdin) {
714 struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
715
716 setup_work_tree();
717 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
718 const char *p;
719 if (line_termination && buf.buf[0] == '"') {
720 strbuf_reset(&nbuf);
721 if (unquote_c_style(&nbuf, buf.buf, NULL))
722 die("line is badly quoted");
723 strbuf_swap(&buf, &nbuf);
724 }
725 p = prefix_path(prefix, prefix_length, buf.buf);
726 update_one(p, NULL, 0);
727 if (set_executable_bit)
728 chmod_path(set_executable_bit, p);
729 if (p < buf.buf || p > buf.buf + buf.len)
730 free((char *)p);
731 }
732 strbuf_release(&nbuf);
733 strbuf_release(&buf);
734 }
735
736 finish:
737 if (active_cache_changed) {
738 if (newfd < 0) {
739 if (refresh_flags & REFRESH_QUIET)
740 exit(128);
741 unable_to_lock_index_die(get_index_file(), lock_error);
742 }
743 if (write_cache(newfd, active_cache, active_nr) ||
744 commit_locked_index(lock_file))
745 die("Unable to write new index file");
746 }
747
748 rollback_lock_file(lock_file);
749
750 return has_errors ? 1 : 0;
751}