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