Refactor http.h USE_CURL_MULTI fill_active_slots().
[debian-git.git] / builtin-update-index.c
blob55fb679d68f141253f5d0ba5c73033d267e2a271
1 /*
2 * GIT - The information manager from hell
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 #include "builtin.h"
12 #include "refs.h"
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.
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 #define MARK_VALID 1
29 #define UNMARK_VALID 2
31 static void report(const char *fmt, ...)
33 va_list vp;
35 if (!verbose)
36 return;
38 va_start(vp, fmt);
39 vprintf(fmt, vp);
40 putchar('\n');
41 va_end(vp);
44 static int mark_valid(const char *path)
46 int namelen = strlen(path);
47 int pos = cache_name_pos(path, namelen);
48 if (0 <= pos) {
49 switch (mark_valid_only) {
50 case MARK_VALID:
51 active_cache[pos]->ce_flags |= htons(CE_VALID);
52 break;
53 case UNMARK_VALID:
54 active_cache[pos]->ce_flags &= ~htons(CE_VALID);
55 break;
57 cache_tree_invalidate_path(active_cache_tree, path);
58 active_cache_changed = 1;
59 return 0;
61 return -1;
64 static int remove_one_path(const char *path)
66 if (!allow_remove)
67 return error("%s: does not exist and --remove not passed", path);
68 if (remove_file_from_cache(path))
69 return error("%s: cannot remove from the index", path);
70 return 0;
74 * Handle a path that couldn't be lstat'ed. It's either:
75 * - missing file (ENOENT or ENOTDIR). That's ok if we're
76 * supposed to be removing it and the removal actually
77 * succeeds.
78 * - permission error. That's never ok.
80 static int process_lstat_error(const char *path, int err)
82 if (err == ENOENT || err == ENOTDIR)
83 return remove_one_path(path);
84 return error("lstat(\"%s\"): %s", path, strerror(errno));
87 static int add_one_path(struct cache_entry *old, const char *path, int len, struct stat *st)
89 int option, size;
90 struct cache_entry *ce;
92 /* Was the old index entry already up-to-date? */
93 if (old && !ce_stage(old) && !ce_match_stat(old, st, 0))
94 return 0;
96 size = cache_entry_size(len);
97 ce = xcalloc(1, size);
98 memcpy(ce->name, path, len);
99 ce->ce_flags = htons(len);
100 fill_stat_cache_info(ce, st);
101 ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
103 if (index_path(ce->sha1, path, st, !info_only))
104 return -1;
105 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
106 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
107 if (add_cache_entry(ce, option))
108 return error("%s: cannot add to the index - missing --add option?", path);
109 return 0;
113 * Handle a path that was a directory. Four cases:
115 * - it's already a gitlink in the index, and we keep it that
116 * way, and update it if we can (if we cannot find the HEAD,
117 * we're going to keep it unchanged in the index!)
119 * - it's a *file* in the index, in which case it should be
120 * removed as a file if removal is allowed, since it doesn't
121 * exist as such any more. If removal isn't allowed, it's
122 * an error.
124 * (NOTE! This is old and arguably fairly strange behaviour.
125 * We might want to make this an error unconditionally, and
126 * use "--force-remove" if you actually want to force removal).
128 * - it used to exist as a subdirectory (ie multiple files with
129 * this particular prefix) in the index, in which case it's wrong
130 * to try to update it as a directory.
132 * - it doesn't exist at all in the index, but it is a valid
133 * git directory, and it should be *added* as a gitlink.
135 static int process_directory(const char *path, int len, struct stat *st)
137 unsigned char sha1[20];
138 int pos = cache_name_pos(path, len);
140 /* Exact match: file or existing gitlink */
141 if (pos >= 0) {
142 struct cache_entry *ce = active_cache[pos];
143 if (S_ISGITLINK(ntohl(ce->ce_mode))) {
145 /* Do nothing to the index if there is no HEAD! */
146 if (resolve_gitlink_ref(path, "HEAD", sha1) < 0)
147 return 0;
149 return add_one_path(ce, path, len, st);
151 /* Should this be an unconditional error? */
152 return remove_one_path(path);
155 /* Inexact match: is there perhaps a subdirectory match? */
156 pos = -pos-1;
157 while (pos < active_nr) {
158 struct cache_entry *ce = active_cache[pos++];
160 if (strncmp(ce->name, path, len))
161 break;
162 if (ce->name[len] > '/')
163 break;
164 if (ce->name[len] < '/')
165 continue;
167 /* Subdirectory match - error out */
168 return error("%s: is a directory - add individual files instead", path);
171 /* No match - should we add it as a gitlink? */
172 if (!resolve_gitlink_ref(path, "HEAD", sha1))
173 return add_one_path(NULL, path, len, st);
175 /* Error out. */
176 return error("%s: is a directory - add files inside instead", path);
180 * Process a regular file
182 static int process_file(const char *path, int len, struct stat *st)
184 int pos = cache_name_pos(path, len);
185 struct cache_entry *ce = pos < 0 ? NULL : active_cache[pos];
187 if (ce && S_ISGITLINK(ntohl(ce->ce_mode)))
188 return error("%s is already a gitlink, not replacing", path);
190 return add_one_path(ce, path, len, st);
193 static int process_path(const char *path)
195 int len;
196 struct stat st;
199 * First things first: get the stat information, to decide
200 * what to do about the pathname!
202 if (lstat(path, &st) < 0)
203 return process_lstat_error(path, errno);
205 len = strlen(path);
206 if (S_ISDIR(st.st_mode))
207 return process_directory(path, len, &st);
209 return process_file(path, len, &st);
212 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
213 const char *path, int stage)
215 int size, len, option;
216 struct cache_entry *ce;
218 if (!verify_path(path))
219 return -1;
221 len = strlen(path);
222 size = cache_entry_size(len);
223 ce = xcalloc(1, size);
225 hashcpy(ce->sha1, sha1);
226 memcpy(ce->name, path, len);
227 ce->ce_flags = create_ce_flags(len, stage);
228 ce->ce_mode = create_ce_mode(mode);
229 if (assume_unchanged)
230 ce->ce_flags |= htons(CE_VALID);
231 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
232 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
233 if (add_cache_entry(ce, option))
234 return error("%s: cannot add to the index - missing --add option?",
235 path);
236 report("add '%s'", path);
237 return 0;
240 static void chmod_path(int flip, const char *path)
242 int pos;
243 struct cache_entry *ce;
244 unsigned int mode;
246 pos = cache_name_pos(path, strlen(path));
247 if (pos < 0)
248 goto fail;
249 ce = active_cache[pos];
250 mode = ntohl(ce->ce_mode);
251 if (!S_ISREG(mode))
252 goto fail;
253 switch (flip) {
254 case '+':
255 ce->ce_mode |= htonl(0111); break;
256 case '-':
257 ce->ce_mode &= htonl(~0111); break;
258 default:
259 goto fail;
261 cache_tree_invalidate_path(active_cache_tree, path);
262 active_cache_changed = 1;
263 report("chmod %cx '%s'", flip, path);
264 return;
265 fail:
266 die("git-update-index: cannot chmod %cx '%s'", flip, path);
269 static void update_one(const char *path, const char *prefix, int prefix_length)
271 const char *p = prefix_path(prefix, prefix_length, path);
272 if (!verify_path(p)) {
273 fprintf(stderr, "Ignoring path %s\n", path);
274 goto free_return;
276 if (mark_valid_only) {
277 if (mark_valid(p))
278 die("Unable to mark file %s", path);
279 goto free_return;
282 if (force_remove) {
283 if (remove_file_from_cache(p))
284 die("git-update-index: unable to remove %s", path);
285 report("remove '%s'", path);
286 goto free_return;
288 if (process_path(p))
289 die("Unable to process path %s", path);
290 report("add '%s'", path);
291 free_return:
292 if (p < path || p > path + strlen(path))
293 free((char*)p);
296 static void read_index_info(int line_termination)
298 struct strbuf buf;
299 strbuf_init(&buf);
300 while (1) {
301 char *ptr, *tab;
302 char *path_name;
303 unsigned char sha1[20];
304 unsigned int mode;
305 unsigned long ul;
306 int stage;
308 /* This reads lines formatted in one of three formats:
310 * (1) mode SP sha1 TAB path
311 * The first format is what "git-apply --index-info"
312 * reports, and used to reconstruct a partial tree
313 * that is used for phony merge base tree when falling
314 * back on 3-way merge.
316 * (2) mode SP type SP sha1 TAB path
317 * The second format is to stuff git-ls-tree output
318 * into the index file.
320 * (3) mode SP sha1 SP stage TAB path
321 * This format is to put higher order stages into the
322 * index file and matches git-ls-files --stage output.
324 read_line(&buf, stdin, line_termination);
325 if (buf.eof)
326 break;
328 errno = 0;
329 ul = strtoul(buf.buf, &ptr, 8);
330 if (ptr == buf.buf || *ptr != ' '
331 || errno || (unsigned int) ul != ul)
332 goto bad_line;
333 mode = ul;
335 tab = strchr(ptr, '\t');
336 if (!tab || tab - ptr < 41)
337 goto bad_line;
339 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
340 stage = tab[-1] - '0';
341 ptr = tab + 1; /* point at the head of path */
342 tab = tab - 2; /* point at tail of sha1 */
344 else {
345 stage = 0;
346 ptr = tab + 1; /* point at the head of path */
349 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
350 goto bad_line;
352 if (line_termination && ptr[0] == '"')
353 path_name = unquote_c_style(ptr, NULL);
354 else
355 path_name = ptr;
357 if (!verify_path(path_name)) {
358 fprintf(stderr, "Ignoring path %s\n", path_name);
359 if (path_name != ptr)
360 free(path_name);
361 continue;
364 if (!mode) {
365 /* mode == 0 means there is no such path -- remove */
366 if (remove_file_from_cache(path_name))
367 die("git-update-index: unable to remove %s",
368 ptr);
370 else {
371 /* mode ' ' sha1 '\t' name
372 * ptr[-1] points at tab,
373 * ptr[-41] is at the beginning of sha1
375 ptr[-42] = ptr[-1] = 0;
376 if (add_cacheinfo(mode, sha1, path_name, stage))
377 die("git-update-index: unable to update %s",
378 path_name);
380 if (path_name != ptr)
381 free(path_name);
382 continue;
384 bad_line:
385 die("malformed index info %s", buf.buf);
389 static const char update_index_usage[] =
390 "git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--really-refresh] [--cacheinfo] [--chmod=(+|-)x] [--assume-unchanged] [--info-only] [--force-remove] [--stdin] [--index-info] [--unresolve] [--again | -g] [--ignore-missing] [-z] [--verbose] [--] <file>...";
392 static unsigned char head_sha1[20];
393 static unsigned char merge_head_sha1[20];
395 static struct cache_entry *read_one_ent(const char *which,
396 unsigned char *ent, const char *path,
397 int namelen, int stage)
399 unsigned mode;
400 unsigned char sha1[20];
401 int size;
402 struct cache_entry *ce;
404 if (get_tree_entry(ent, path, sha1, &mode)) {
405 if (which)
406 error("%s: not in %s branch.", path, which);
407 return NULL;
409 if (mode == S_IFDIR) {
410 if (which)
411 error("%s: not a blob in %s branch.", path, which);
412 return NULL;
414 size = cache_entry_size(namelen);
415 ce = xcalloc(1, size);
417 hashcpy(ce->sha1, sha1);
418 memcpy(ce->name, path, namelen);
419 ce->ce_flags = create_ce_flags(namelen, stage);
420 ce->ce_mode = create_ce_mode(mode);
421 return ce;
424 static int unresolve_one(const char *path)
426 int namelen = strlen(path);
427 int pos;
428 int ret = 0;
429 struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
431 /* See if there is such entry in the index. */
432 pos = cache_name_pos(path, namelen);
433 if (pos < 0) {
434 /* If there isn't, either it is unmerged, or
435 * resolved as "removed" by mistake. We do not
436 * want to do anything in the former case.
438 pos = -pos-1;
439 if (pos < active_nr) {
440 struct cache_entry *ce = active_cache[pos];
441 if (ce_namelen(ce) == namelen &&
442 !memcmp(ce->name, path, namelen)) {
443 fprintf(stderr,
444 "%s: skipping still unmerged path.\n",
445 path);
446 goto free_return;
451 /* Grab blobs from given path from HEAD and MERGE_HEAD,
452 * stuff HEAD version in stage #2,
453 * stuff MERGE_HEAD version in stage #3.
455 ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
456 ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);
458 if (!ce_2 || !ce_3) {
459 ret = -1;
460 goto free_return;
462 if (!hashcmp(ce_2->sha1, ce_3->sha1) &&
463 ce_2->ce_mode == ce_3->ce_mode) {
464 fprintf(stderr, "%s: identical in both, skipping.\n",
465 path);
466 goto free_return;
469 remove_file_from_cache(path);
470 if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
471 error("%s: cannot add our version to the index.", path);
472 ret = -1;
473 goto free_return;
475 if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
476 return 0;
477 error("%s: cannot add their version to the index.", path);
478 ret = -1;
479 free_return:
480 free(ce_2);
481 free(ce_3);
482 return ret;
485 static void read_head_pointers(void)
487 if (read_ref("HEAD", head_sha1))
488 die("No HEAD -- no initial commit yet?\n");
489 if (read_ref("MERGE_HEAD", merge_head_sha1)) {
490 fprintf(stderr, "Not in the middle of a merge.\n");
491 exit(0);
495 static int do_unresolve(int ac, const char **av,
496 const char *prefix, int prefix_length)
498 int i;
499 int err = 0;
501 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
502 * are not doing a merge, so exit with success status.
504 read_head_pointers();
506 for (i = 1; i < ac; i++) {
507 const char *arg = av[i];
508 const char *p = prefix_path(prefix, prefix_length, arg);
509 err |= unresolve_one(p);
510 if (p < arg || p > arg + strlen(arg))
511 free((char*)p);
513 return err;
516 static int do_reupdate(int ac, const char **av,
517 const char *prefix, int prefix_length)
519 /* Read HEAD and run update-index on paths that are
520 * merged and already different between index and HEAD.
522 int pos;
523 int has_head = 1;
524 const char **pathspec = get_pathspec(prefix, av + 1);
526 if (read_ref("HEAD", head_sha1))
527 /* If there is no HEAD, that means it is an initial
528 * commit. Update everything in the index.
530 has_head = 0;
531 redo:
532 for (pos = 0; pos < active_nr; pos++) {
533 struct cache_entry *ce = active_cache[pos];
534 struct cache_entry *old = NULL;
535 int save_nr;
537 if (ce_stage(ce) || !ce_path_match(ce, pathspec))
538 continue;
539 if (has_head)
540 old = read_one_ent(NULL, head_sha1,
541 ce->name, ce_namelen(ce), 0);
542 if (old && ce->ce_mode == old->ce_mode &&
543 !hashcmp(ce->sha1, old->sha1)) {
544 free(old);
545 continue; /* unchanged */
547 /* Be careful. The working tree may not have the
548 * path anymore, in which case, under 'allow_remove',
549 * or worse yet 'allow_replace', active_nr may decrease.
551 save_nr = active_nr;
552 update_one(ce->name + prefix_length, prefix, prefix_length);
553 if (save_nr != active_nr)
554 goto redo;
556 return 0;
559 int cmd_update_index(int argc, const char **argv, const char *prefix)
561 int i, newfd, entries, has_errors = 0, line_termination = '\n';
562 int allow_options = 1;
563 int read_from_stdin = 0;
564 int prefix_length = prefix ? strlen(prefix) : 0;
565 char set_executable_bit = 0;
566 unsigned int refresh_flags = 0;
567 int lock_error = 0;
568 struct lock_file *lock_file;
570 git_config(git_default_config);
572 /* We can't free this memory, it becomes part of a linked list parsed atexit() */
573 lock_file = xcalloc(1, sizeof(struct lock_file));
575 newfd = hold_locked_index(lock_file, 0);
576 if (newfd < 0)
577 lock_error = errno;
579 entries = read_cache();
580 if (entries < 0)
581 die("cache corrupted");
583 for (i = 1 ; i < argc; i++) {
584 const char *path = argv[i];
585 const char *p;
587 if (allow_options && *path == '-') {
588 if (!strcmp(path, "--")) {
589 allow_options = 0;
590 continue;
592 if (!strcmp(path, "-q")) {
593 refresh_flags |= REFRESH_QUIET;
594 continue;
596 if (!strcmp(path, "--add")) {
597 allow_add = 1;
598 continue;
600 if (!strcmp(path, "--replace")) {
601 allow_replace = 1;
602 continue;
604 if (!strcmp(path, "--remove")) {
605 allow_remove = 1;
606 continue;
608 if (!strcmp(path, "--unmerged")) {
609 refresh_flags |= REFRESH_UNMERGED;
610 continue;
612 if (!strcmp(path, "--refresh")) {
613 has_errors |= refresh_cache(refresh_flags);
614 continue;
616 if (!strcmp(path, "--really-refresh")) {
617 has_errors |= refresh_cache(REFRESH_REALLY | refresh_flags);
618 continue;
620 if (!strcmp(path, "--cacheinfo")) {
621 unsigned char sha1[20];
622 unsigned int mode;
624 if (i+3 >= argc)
625 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
627 if (strtoul_ui(argv[i+1], 8, &mode) ||
628 get_sha1_hex(argv[i+2], sha1) ||
629 add_cacheinfo(mode, sha1, argv[i+3], 0))
630 die("git-update-index: --cacheinfo"
631 " cannot add %s", argv[i+3]);
632 i += 3;
633 continue;
635 if (!strcmp(path, "--chmod=-x") ||
636 !strcmp(path, "--chmod=+x")) {
637 if (argc <= i+1)
638 die("git-update-index: %s <path>", path);
639 set_executable_bit = path[8];
640 continue;
642 if (!strcmp(path, "--assume-unchanged")) {
643 mark_valid_only = MARK_VALID;
644 continue;
646 if (!strcmp(path, "--no-assume-unchanged")) {
647 mark_valid_only = UNMARK_VALID;
648 continue;
650 if (!strcmp(path, "--info-only")) {
651 info_only = 1;
652 continue;
654 if (!strcmp(path, "--force-remove")) {
655 force_remove = 1;
656 continue;
658 if (!strcmp(path, "-z")) {
659 line_termination = 0;
660 continue;
662 if (!strcmp(path, "--stdin")) {
663 if (i != argc - 1)
664 die("--stdin must be at the end");
665 read_from_stdin = 1;
666 break;
668 if (!strcmp(path, "--index-info")) {
669 if (i != argc - 1)
670 die("--index-info must be at the end");
671 allow_add = allow_replace = allow_remove = 1;
672 read_index_info(line_termination);
673 break;
675 if (!strcmp(path, "--unresolve")) {
676 has_errors = do_unresolve(argc - i, argv + i,
677 prefix, prefix_length);
678 if (has_errors)
679 active_cache_changed = 0;
680 goto finish;
682 if (!strcmp(path, "--again") || !strcmp(path, "-g")) {
683 has_errors = do_reupdate(argc - i, argv + i,
684 prefix, prefix_length);
685 if (has_errors)
686 active_cache_changed = 0;
687 goto finish;
689 if (!strcmp(path, "--ignore-missing")) {
690 refresh_flags |= REFRESH_IGNORE_MISSING;
691 continue;
693 if (!strcmp(path, "--verbose")) {
694 verbose = 1;
695 continue;
697 if (!strcmp(path, "-h") || !strcmp(path, "--help"))
698 usage(update_index_usage);
699 die("unknown option %s", path);
701 p = prefix_path(prefix, prefix_length, path);
702 update_one(p, NULL, 0);
703 if (set_executable_bit)
704 chmod_path(set_executable_bit, p);
705 if (p < path || p > path + strlen(path))
706 free((char*)p);
708 if (read_from_stdin) {
709 struct strbuf buf;
710 strbuf_init(&buf);
711 while (1) {
712 char *path_name;
713 const char *p;
714 read_line(&buf, stdin, line_termination);
715 if (buf.eof)
716 break;
717 if (line_termination && buf.buf[0] == '"')
718 path_name = unquote_c_style(buf.buf, NULL);
719 else
720 path_name = buf.buf;
721 p = prefix_path(prefix, prefix_length, path_name);
722 update_one(p, NULL, 0);
723 if (set_executable_bit)
724 chmod_path(set_executable_bit, p);
725 if (p < path_name || p > path_name + strlen(path_name))
726 free((char*) p);
727 if (path_name != buf.buf)
728 free(path_name);
732 finish:
733 if (active_cache_changed) {
734 if (newfd < 0) {
735 if (refresh_flags & REFRESH_QUIET)
736 exit(128);
737 die("unable to create '%s.lock': %s",
738 get_index_file(), strerror(lock_error));
740 if (write_cache(newfd, active_cache, active_nr) ||
741 close(newfd) || commit_locked_index(lock_file))
742 die("Unable to write new index file");
745 rollback_lock_file(lock_file);
747 return has_errors ? 1 : 0;