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