Merge branch 'jc/gitlink' into next
[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 "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  */
19 static int allow_add;
20 static int allow_remove;
21 static int allow_replace;
22 static int allow_unmerged; /* --refresh needing merge is not error */
23 static int not_new; /* --refresh not having working tree files is not error */
24 static int quiet; /* --refresh needing update is not error */
25 static int info_only;
26 static int force_remove;
27 static int verbose;
28 static 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 */
34 static inline void *ERR_PTR(long error)
35 {
36         return (void *) error;
37 }
38
39 static inline long PTR_ERR(const void *ptr)
40 {
41         return (long) ptr;
42 }
43
44 static inline long IS_ERR(const void *ptr)
45 {
46         return (unsigned long)ptr > (unsigned long)-1000L;
47 }
48
49 static 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
62 static 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
82 static 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  */
162 static 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
200 static 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
248 /*
249  * We fundamentally don't like some paths: we don't want
250  * dot or dot-dot anywhere, and for obvious reasons don't
251  * want to recurse into ".git" either.
252  *
253  * Also, we don't want double slashes or slashes at the
254  * end that can make pathnames ambiguous.
255  */
256 static int verify_dotfile(const char *rest)
257 {
258         /*
259          * The first character was '.', but that
260          * has already been discarded, we now test
261          * the rest.
262          */
263         switch (*rest) {
264         /* "." is not allowed */
265         case '\0': case '/':
266                 return 0;
267
268         /*
269          * ".git" followed by  NUL or slash is bad. This
270          * shares the path end test with the ".." case.
271          */
272         case 'g':
273                 if (rest[1] != 'i')
274                         break;
275                 if (rest[2] != 't')
276                         break;
277                 rest += 2;
278         /* fallthrough */
279         case '.':
280                 if (rest[1] == '\0' || rest[1] == '/')
281                         return 0;
282         }
283         return 1;
284 }
285
286 static int verify_path(const char *path)
287 {
288         char c;
289
290         goto inside;
291         for (;;) {
292                 if (!c)
293                         return 1;
294                 if (c == '/') {
295 inside:
296                         c = *path++;
297                         switch (c) {
298                         default:
299                                 continue;
300                         case '/': case '\0':
301                                 break;
302                         case '.':
303                                 if (verify_dotfile(path))
304                                         continue;
305                         }
306                         return 0;
307                 }
308                 c = *path++;
309         }
310 }
311
312 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
313                          const char *path, int stage)
314 {
315         int size, len, option;
316         struct cache_entry *ce;
317
318         if (!verify_path(path))
319                 return -1;
320
321         len = strlen(path);
322         size = cache_entry_size(len);
323         ce = xcalloc(1, size);
324
325         memcpy(ce->sha1, sha1, 20);
326         memcpy(ce->name, path, len);
327         ce->ce_flags = create_ce_flags(len, stage);
328         ce->ce_mode = create_ce_mode(mode);
329         if (assume_unchanged)
330                 ce->ce_flags |= htons(CE_VALID);
331         option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
332         option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
333         if (add_cache_entry(ce, option))
334                 return error("%s: cannot add to the index - missing --add option?",
335                              path);
336         report("add '%s'", path);
337         cache_tree_invalidate_path(active_cache_tree, path);
338         return 0;
339 }
340
341 static void chmod_path(int flip, const char *path)
342 {
343         int pos;
344         struct cache_entry *ce;
345         unsigned int mode;
346
347         pos = cache_name_pos(path, strlen(path));
348         if (pos < 0)
349                 goto fail;
350         ce = active_cache[pos];
351         mode = ntohl(ce->ce_mode);
352         if (!S_ISREG(mode))
353                 goto fail;
354         switch (flip) {
355         case '+':
356                 ce->ce_mode |= htonl(0111); break;
357         case '-':
358                 ce->ce_mode &= htonl(~0111); break;
359         default:
360                 goto fail;
361         }
362         cache_tree_invalidate_path(active_cache_tree, path);
363         active_cache_changed = 1;
364         report("chmod %cx '%s'", flip, path);
365         return;
366  fail:
367         die("git-update-index: cannot chmod %cx '%s'", flip, path);
368 }
369
370 static struct cache_file cache_file;
371
372 static void update_one(const char *path, const char *prefix, int prefix_length)
373 {
374         const char *p = prefix_path(prefix, prefix_length, path);
375         if (!verify_path(p)) {
376                 fprintf(stderr, "Ignoring path %s\n", path);
377                 goto free_return;
378         }
379         if (mark_valid_only) {
380                 if (mark_valid(p))
381                         die("Unable to mark file %s", path);
382                 goto free_return;
383         }
384         cache_tree_invalidate_path(active_cache_tree, path);
385
386         if (force_remove) {
387                 if (remove_file_from_cache(p))
388                         die("git-update-index: unable to remove %s", path);
389                 report("remove '%s'", path);
390                 goto free_return;
391         }
392         if (add_file_to_cache(p))
393                 die("Unable to process file %s", path);
394         report("add '%s'", path);
395  free_return:
396         if (p < path || p > path + strlen(path))
397                 free((char*)p);
398 }
399
400 static void read_index_info(int line_termination)
401 {
402         struct strbuf buf;
403         strbuf_init(&buf);
404         while (1) {
405                 char *ptr, *tab;
406                 char *path_name;
407                 unsigned char sha1[20];
408                 unsigned int mode;
409                 int stage;
410
411                 /* This reads lines formatted in one of three formats:
412                  *
413                  * (1) mode         SP sha1          TAB path
414                  * The first format is what "git-apply --index-info"
415                  * reports, and used to reconstruct a partial tree
416                  * that is used for phony merge base tree when falling
417                  * back on 3-way merge.
418                  *
419                  * (2) mode SP type SP sha1          TAB path
420                  * The second format is to stuff git-ls-tree output
421                  * into the index file.
422                  * 
423                  * (3) mode         SP sha1 SP stage TAB path
424                  * This format is to put higher order stages into the
425                  * index file and matches git-ls-files --stage output.
426                  */
427                 read_line(&buf, stdin, line_termination);
428                 if (buf.eof)
429                         break;
430
431                 mode = strtoul(buf.buf, &ptr, 8);
432                 if (ptr == buf.buf || *ptr != ' ')
433                         goto bad_line;
434
435                 tab = strchr(ptr, '\t');
436                 if (!tab || tab - ptr < 41)
437                         goto bad_line;
438
439                 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
440                         stage = tab[-1] - '0';
441                         ptr = tab + 1; /* point at the head of path */
442                         tab = tab - 2; /* point at tail of sha1 */
443                 }
444                 else {
445                         stage = 0;
446                         ptr = tab + 1; /* point at the head of path */
447                 }
448
449                 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
450                         goto bad_line;
451
452                 if (line_termination && ptr[0] == '"')
453                         path_name = unquote_c_style(ptr, NULL);
454                 else
455                         path_name = ptr;
456
457                 if (!verify_path(path_name)) {
458                         fprintf(stderr, "Ignoring path %s\n", path_name);
459                         if (path_name != ptr)
460                                 free(path_name);
461                         continue;
462                 }
463                 cache_tree_invalidate_path(active_cache_tree, path_name);
464
465                 if (!mode) {
466                         /* mode == 0 means there is no such path -- remove */
467                         if (remove_file_from_cache(path_name))
468                                 die("git-update-index: unable to remove %s",
469                                     ptr);
470                 }
471                 else {
472                         /* mode ' ' sha1 '\t' name
473                          * ptr[-1] points at tab,
474                          * ptr[-41] is at the beginning of sha1
475                          */
476                         ptr[-42] = ptr[-1] = 0;
477                         if (add_cacheinfo(mode, sha1, path_name, stage))
478                                 die("git-update-index: unable to update %s",
479                                     path_name);
480                 }
481                 if (path_name != ptr)
482                         free(path_name);
483                 continue;
484
485         bad_line:
486                 die("malformed index info %s", buf.buf);
487         }
488 }
489
490 static const char update_index_usage[] =
491 "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>...";
492
493 static unsigned char head_sha1[20];
494 static unsigned char merge_head_sha1[20];
495
496 static struct cache_entry *read_one_ent(const char *which,
497                                         unsigned char *ent, const char *path,
498                                         int namelen, int stage)
499 {
500         unsigned mode;
501         unsigned char sha1[20];
502         int size;
503         struct cache_entry *ce;
504
505         if (get_tree_entry(ent, path, sha1, &mode)) {
506                 if (which)
507                         error("%s: not in %s branch.", path, which);
508                 return NULL;
509         }
510         if (mode == S_IFDIR) {
511                 if (which)
512                         error("%s: not a blob in %s branch.", path, which);
513                 return NULL;
514         }
515         size = cache_entry_size(namelen);
516         ce = xcalloc(1, size);
517
518         memcpy(ce->sha1, sha1, 20);
519         memcpy(ce->name, path, namelen);
520         ce->ce_flags = create_ce_flags(namelen, stage);
521         ce->ce_mode = create_ce_mode(mode);
522         return ce;
523 }
524
525 static int unresolve_one(const char *path)
526 {
527         int namelen = strlen(path);
528         int pos;
529         int ret = 0;
530         struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
531
532         /* See if there is such entry in the index. */
533         pos = cache_name_pos(path, namelen);
534         if (pos < 0) {
535                 /* If there isn't, either it is unmerged, or
536                  * resolved as "removed" by mistake.  We do not
537                  * want to do anything in the former case.
538                  */
539                 pos = -pos-1;
540                 if (pos < active_nr) {
541                         struct cache_entry *ce = active_cache[pos];
542                         if (ce_namelen(ce) == namelen &&
543                             !memcmp(ce->name, path, namelen)) {
544                                 fprintf(stderr,
545                                         "%s: skipping still unmerged path.\n",
546                                         path);
547                                 goto free_return;
548                         }
549                 }
550         }
551
552         /* Grab blobs from given path from HEAD and MERGE_HEAD,
553          * stuff HEAD version in stage #2,
554          * stuff MERGE_HEAD version in stage #3.
555          */
556         ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
557         ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);
558
559         if (!ce_2 || !ce_3) {
560                 ret = -1;
561                 goto free_return;
562         }
563         if (!memcmp(ce_2->sha1, ce_3->sha1, 20) &&
564             ce_2->ce_mode == ce_3->ce_mode) {
565                 fprintf(stderr, "%s: identical in both, skipping.\n",
566                         path);
567                 goto free_return;
568         }
569
570         cache_tree_invalidate_path(active_cache_tree, path);
571         remove_file_from_cache(path);
572         if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
573                 error("%s: cannot add our version to the index.", path);
574                 ret = -1;
575                 goto free_return;
576         }
577         if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
578                 return 0;
579         error("%s: cannot add their version to the index.", path);
580         ret = -1;
581  free_return:
582         free(ce_2);
583         free(ce_3);
584         return ret;
585 }
586
587 static void read_head_pointers(void)
588 {
589         if (read_ref(git_path("HEAD"), head_sha1))
590                 die("No HEAD -- no initial commit yet?\n");
591         if (read_ref(git_path("MERGE_HEAD"), merge_head_sha1)) {
592                 fprintf(stderr, "Not in the middle of a merge.\n");
593                 exit(0);
594         }
595 }
596
597 static int do_unresolve(int ac, const char **av,
598                         const char *prefix, int prefix_length)
599 {
600         int i;
601         int err = 0;
602
603         /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
604          * are not doing a merge, so exit with success status.
605          */
606         read_head_pointers();
607
608         for (i = 1; i < ac; i++) {
609                 const char *arg = av[i];
610                 const char *p = prefix_path(prefix, prefix_length, arg);
611                 err |= unresolve_one(p);
612                 if (p < arg || p > arg + strlen(arg))
613                         free((char*)p);
614         }
615         return err;
616 }
617
618 static int do_reupdate(int ac, const char **av,
619                        const char *prefix, int prefix_length)
620 {
621         /* Read HEAD and run update-index on paths that are
622          * merged and already different between index and HEAD.
623          */
624         int pos;
625         int has_head = 1;
626         const char **pathspec = get_pathspec(prefix, av + 1);
627
628         if (read_ref(git_path("HEAD"), head_sha1))
629                 /* If there is no HEAD, that means it is an initial
630                  * commit.  Update everything in the index.
631                  */
632                 has_head = 0;
633  redo:
634         for (pos = 0; pos < active_nr; pos++) {
635                 struct cache_entry *ce = active_cache[pos];
636                 struct cache_entry *old = NULL;
637                 int save_nr;
638
639                 if (ce_stage(ce) || !ce_path_match(ce, pathspec))
640                         continue;
641                 if (has_head)
642                         old = read_one_ent(NULL, head_sha1,
643                                            ce->name, ce_namelen(ce), 0);
644                 if (old && ce->ce_mode == old->ce_mode &&
645                     !memcmp(ce->sha1, old->sha1, 20)) {
646                         free(old);
647                         continue; /* unchanged */
648                 }
649                 /* Be careful.  The working tree may not have the
650                  * path anymore, in which case, under 'allow_remove',
651                  * or worse yet 'allow_replace', active_nr may decrease.
652                  */
653                 save_nr = active_nr;
654                 update_one(ce->name + prefix_length, prefix, prefix_length);
655                 if (save_nr != active_nr)
656                         goto redo;
657         }
658         return 0;
659 }
660
661 int main(int argc, const char **argv)
662 {
663         int i, newfd, entries, has_errors = 0, line_termination = '\n';
664         int allow_options = 1;
665         int read_from_stdin = 0;
666         const char *prefix = setup_git_directory();
667         int prefix_length = prefix ? strlen(prefix) : 0;
668         char set_executable_bit = 0;
669
670         git_config(git_default_config);
671
672         newfd = hold_index_file_for_update(&cache_file, get_index_file());
673         if (newfd < 0)
674                 die("unable to create new cachefile");
675
676         entries = read_cache();
677         if (entries < 0)
678                 die("cache corrupted");
679
680         for (i = 1 ; i < argc; i++) {
681                 const char *path = argv[i];
682
683                 if (allow_options && *path == '-') {
684                         if (!strcmp(path, "--")) {
685                                 allow_options = 0;
686                                 continue;
687                         }
688                         if (!strcmp(path, "-q")) {
689                                 quiet = 1;
690                                 continue;
691                         }
692                         if (!strcmp(path, "--add")) {
693                                 allow_add = 1;
694                                 continue;
695                         }
696                         if (!strcmp(path, "--replace")) {
697                                 allow_replace = 1;
698                                 continue;
699                         }
700                         if (!strcmp(path, "--remove")) {
701                                 allow_remove = 1;
702                                 continue;
703                         }
704                         if (!strcmp(path, "--unmerged")) {
705                                 allow_unmerged = 1;
706                                 continue;
707                         }
708                         if (!strcmp(path, "--refresh")) {
709                                 has_errors |= refresh_cache(0);
710                                 continue;
711                         }
712                         if (!strcmp(path, "--really-refresh")) {
713                                 has_errors |= refresh_cache(1);
714                                 continue;
715                         }
716                         if (!strcmp(path, "--cacheinfo")) {
717                                 unsigned char sha1[20];
718                                 unsigned int mode;
719
720                                 if (i+3 >= argc)
721                                         die("git-update-index: --cacheinfo <mode> <sha1> <path>");
722
723                                 if ((sscanf(argv[i+1], "%o", &mode) != 1) ||
724                                     get_sha1_hex(argv[i+2], sha1) ||
725                                     add_cacheinfo(mode, sha1, argv[i+3], 0))
726                                         die("git-update-index: --cacheinfo"
727                                             " cannot add %s", argv[i+3]);
728                                 i += 3;
729                                 continue;
730                         }
731                         if (!strcmp(path, "--chmod=-x") ||
732                             !strcmp(path, "--chmod=+x")) {
733                                 if (argc <= i+1)
734                                         die("git-update-index: %s <path>", path);
735                                 set_executable_bit = path[8];
736                                 continue;
737                         }
738                         if (!strcmp(path, "--assume-unchanged")) {
739                                 mark_valid_only = MARK_VALID;
740                                 continue;
741                         }
742                         if (!strcmp(path, "--no-assume-unchanged")) {
743                                 mark_valid_only = UNMARK_VALID;
744                                 continue;
745                         }
746                         if (!strcmp(path, "--info-only")) {
747                                 info_only = 1;
748                                 continue;
749                         }
750                         if (!strcmp(path, "--force-remove")) {
751                                 force_remove = 1;
752                                 continue;
753                         }
754                         if (!strcmp(path, "-z")) {
755                                 line_termination = 0;
756                                 continue;
757                         }
758                         if (!strcmp(path, "--stdin")) {
759                                 if (i != argc - 1)
760                                         die("--stdin must be at the end");
761                                 read_from_stdin = 1;
762                                 break;
763                         }
764                         if (!strcmp(path, "--index-info")) {
765                                 if (i != argc - 1)
766                                         die("--index-info must be at the end");
767                                 allow_add = allow_replace = allow_remove = 1;
768                                 read_index_info(line_termination);
769                                 break;
770                         }
771                         if (!strcmp(path, "--unresolve")) {
772                                 has_errors = do_unresolve(argc - i, argv + i,
773                                                           prefix, prefix_length);
774                                 if (has_errors)
775                                         active_cache_changed = 0;
776                                 goto finish;
777                         }
778                         if (!strcmp(path, "--again")) {
779                                 has_errors = do_reupdate(argc - i, argv + i,
780                                                          prefix, prefix_length);
781                                 if (has_errors)
782                                         active_cache_changed = 0;
783                                 goto finish;
784                         }
785                         if (!strcmp(path, "--ignore-missing")) {
786                                 not_new = 1;
787                                 continue;
788                         }
789                         if (!strcmp(path, "--verbose")) {
790                                 verbose = 1;
791                                 continue;
792                         }
793                         if (!strcmp(path, "-h") || !strcmp(path, "--help"))
794                                 usage(update_index_usage);
795                         die("unknown option %s", path);
796                 }
797                 update_one(path, prefix, prefix_length);
798                 if (set_executable_bit)
799                         chmod_path(set_executable_bit, path);
800         }
801         if (read_from_stdin) {
802                 struct strbuf buf;
803                 strbuf_init(&buf);
804                 while (1) {
805                         char *path_name;
806                         const char *p;
807                         read_line(&buf, stdin, line_termination);
808                         if (buf.eof)
809                                 break;
810                         if (line_termination && buf.buf[0] == '"')
811                                 path_name = unquote_c_style(buf.buf, NULL);
812                         else
813                                 path_name = buf.buf;
814                         p = prefix_path(prefix, prefix_length, path_name);
815                         update_one(p, NULL, 0);
816                         if (set_executable_bit)
817                                 chmod_path(set_executable_bit, p);
818                         if (p < path_name || p > path_name + strlen(path_name))
819                                 free((char*) p);
820                         if (path_name != buf.buf)
821                                 free(path_name);
822                 }
823         }
824
825  finish:
826         if (active_cache_changed) {
827                 if (write_cache(newfd, active_cache, active_nr) ||
828                     commit_index_file(&cache_file))
829                         die("Unable to write new cachefile");
830         }
831
832         return has_errors ? 1 : 0;
833 }