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