2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
11 #include "cache-tree.h"
13 static unsigned char active_cache_sha1[20];
14 static struct cache_tree *active_cache_tree;
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.
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 */
30 static int force_remove;
32 static int mark_valid_only = 0;
34 #define UNMARK_VALID 2
37 /* Three functions to allow overloaded pointer return; see linux/err.h */
38 static inline void *ERR_PTR(long error)
40 return (void *) error;
43 static inline long PTR_ERR(const void *ptr)
48 static inline long IS_ERR(const void *ptr)
50 return (unsigned long)ptr > (unsigned long)-1000L;
53 static void report(const char *fmt, ...)
66 static int mark_valid(const char *path)
68 int namelen = strlen(path);
69 int pos = cache_name_pos(path, namelen);
71 switch (mark_valid_only) {
73 active_cache[pos]->ce_flags |= htons(CE_VALID);
76 active_cache[pos]->ce_flags &= ~htons(CE_VALID);
79 cache_tree_invalidate_path(active_cache_tree, path);
80 active_cache_changed = 1;
86 static int add_file_to_cache(const char *path)
88 int size, namelen, option, status;
89 struct cache_entry *ce;
92 status = lstat(path, &st);
94 /* We probably want to do this in remove_file_from_cache() and
95 * add_cache_entry() instead...
97 cache_tree_invalidate_path(active_cache_tree, path);
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.
108 if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) {
110 if (remove_file_from_cache(path))
111 return error("%s: cannot remove from the index",
115 } else if (status < 0) {
116 return error("%s: does not exist and --remove not passed",
121 return error("%s: is a directory - add files inside instead",
124 return error("lstat(\"%s\"): %s", path,
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);
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
140 int pos = cache_name_pos(path, namelen);
142 ce->ce_mode = active_cache[pos]->ce_mode;
145 if (index_path(ce->sha1, path, &st, !info_only))
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?",
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
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.
166 static struct cache_entry *refresh_entry(struct cache_entry *ce, int really)
169 struct cache_entry *updated;
172 if (lstat(ce->name, &st) < 0)
173 return ERR_PTR(-errno);
175 changed = ce_match_stat(ce, &st, really);
177 if (really && assume_unchanged &&
178 !(ce->ce_flags & htons(CE_VALID)))
179 ; /* mark this one VALID again */
184 if (ce_modified(ce, &st, really))
185 return ERR_PTR(-EINVAL);
188 updated = xmalloc(size);
189 memcpy(updated, ce, size);
190 fill_stat_cache_info(updated, &st);
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.
198 if (!really && assume_unchanged && !(ce->ce_flags & htons(CE_VALID)))
199 updated->ce_flags &= ~htons(CE_VALID);
204 static int refresh_cache(int really)
209 for (i = 0; i < active_nr; i++) {
210 struct cache_entry *ce, *new;
211 ce = active_cache[i];
213 while ((i < active_nr) &&
214 ! strcmp(active_cache[i]->name, ce->name))
219 printf("%s: needs merge\n", ce->name);
224 new = refresh_entry(ce, really);
228 if (not_new && PTR_ERR(new) == -ENOENT)
230 if (really && PTR_ERR(new) == -EINVAL) {
231 /* If we are doing --really-refresh that
232 * means the index is not valid anymore.
234 ce->ce_flags &= ~htons(CE_VALID);
235 active_cache_changed = 1;
239 printf("%s: needs update\n", ce->name);
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
247 active_cache[i] = new;
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.
257 * Also, we don't want double slashes or slashes at the
258 * end that can make pathnames ambiguous.
260 static int verify_dotfile(const char *rest)
263 * The first character was '.', but that
264 * has already been discarded, we now test
268 /* "." is not allowed */
273 * ".git" followed by NUL or slash is bad. This
274 * shares the path end test with the ".." case.
284 if (rest[1] == '\0' || rest[1] == '/')
290 static int verify_path(const char *path)
307 if (verify_dotfile(path))
316 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
317 const char *path, int stage)
319 int size, len, option;
320 struct cache_entry *ce;
322 if (!verify_path(path))
326 size = cache_entry_size(len);
327 ce = xcalloc(1, size);
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?",
340 report("add '%s'", path);
341 cache_tree_invalidate_path(active_cache_tree, path);
345 static void chmod_path(int flip, const char *path)
348 struct cache_entry *ce;
351 pos = cache_name_pos(path, strlen(path));
354 ce = active_cache[pos];
355 mode = ntohl(ce->ce_mode);
360 ce->ce_mode |= htonl(0111); break;
362 ce->ce_mode &= htonl(~0111); break;
366 cache_tree_invalidate_path(active_cache_tree, path);
367 active_cache_changed = 1;
368 report("chmod %cx '%s'", flip, path);
371 die("git-update-index: cannot chmod %cx '%s'", flip, path);
374 static struct cache_file cache_file;
376 static void update_one(const char *path, const char *prefix, int prefix_length)
378 const char *p = prefix_path(prefix, prefix_length, path);
379 if (!verify_path(p)) {
380 fprintf(stderr, "Ignoring path %s\n", path);
383 if (mark_valid_only) {
385 die("Unable to mark file %s", path);
388 cache_tree_invalidate_path(active_cache_tree, path);
391 if (remove_file_from_cache(p))
392 die("git-update-index: unable to remove %s", path);
393 report("remove '%s'", path);
396 if (add_file_to_cache(p))
397 die("Unable to process file %s", path);
398 report("add '%s'", path);
401 static void read_index_info(int line_termination)
408 unsigned char sha1[20];
412 /* This reads lines formatted in one of three formats:
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.
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.
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.
428 read_line(&buf, stdin, line_termination);
432 mode = strtoul(buf.buf, &ptr, 8);
433 if (ptr == buf.buf || *ptr != ' ')
436 tab = strchr(ptr, '\t');
437 if (!tab || tab - ptr < 41)
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 */
447 ptr = tab + 1; /* point at the head of path */
450 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
453 if (line_termination && ptr[0] == '"')
454 path_name = unquote_c_style(ptr, NULL);
458 if (!verify_path(path_name)) {
459 fprintf(stderr, "Ignoring path %s\n", path_name);
460 if (path_name != ptr)
464 cache_tree_invalidate_path(active_cache_tree, path_name);
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",
473 /* mode ' ' sha1 '\t' name
474 * ptr[-1] points at tab,
475 * ptr[-41] is at the beginning of sha1
477 ptr[-42] = ptr[-1] = 0;
478 if (add_cacheinfo(mode, sha1, path_name, stage))
479 die("git-update-index: unable to update %s",
482 if (path_name != ptr)
487 die("malformed index info %s", buf.buf);
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>...";
494 static unsigned char head_sha1[20];
495 static unsigned char merge_head_sha1[20];
497 static struct cache_entry *read_one_ent(const char *which,
498 unsigned char *ent, const char *path,
499 int namelen, int stage)
502 unsigned char sha1[20];
504 struct cache_entry *ce;
506 if (get_tree_entry(ent, path, sha1, &mode)) {
507 error("%s: not in %s branch.", path, which);
510 if (mode == S_IFDIR) {
511 error("%s: not a blob in %s branch.", path, which);
514 size = cache_entry_size(namelen);
515 ce = xcalloc(1, size);
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);
524 static int unresolve_one(const char *path)
526 int namelen = strlen(path);
529 struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
531 /* See if there is such entry in the index. */
532 pos = cache_name_pos(path, namelen);
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.
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)) {
544 "%s: skipping still unmerged path.\n",
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.
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);
558 if (!ce_2 || !ce_3) {
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",
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);
576 if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
578 error("%s: cannot add their version to the index.", path);
586 static void read_head_pointers(void)
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");
596 static int do_unresolve(int ac, const char **av)
601 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
602 * are not doing a merge, so exit with success status.
604 read_head_pointers();
606 for (i = 1; i < ac; i++) {
607 const char *arg = av[i];
608 err |= unresolve_one(arg);
613 int main(int argc, const char **argv)
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;
622 git_config(git_default_config);
624 newfd = hold_index_file_for_update(&cache_file, get_index_file());
626 die("unable to create new cachefile");
628 entries = read_cache_1(active_cache_sha1);
630 die("cache corrupted");
631 active_cache_tree = read_cache_tree(active_cache_sha1);
633 for (i = 1 ; i < argc; i++) {
634 const char *path = argv[i];
636 if (allow_options && *path == '-') {
637 if (!strcmp(path, "--")) {
641 if (!strcmp(path, "-q")) {
645 if (!strcmp(path, "--add")) {
649 if (!strcmp(path, "--replace")) {
653 if (!strcmp(path, "--remove")) {
657 if (!strcmp(path, "--unmerged")) {
661 if (!strcmp(path, "--refresh")) {
662 has_errors |= refresh_cache(0);
665 if (!strcmp(path, "--really-refresh")) {
666 has_errors |= refresh_cache(1);
669 if (!strcmp(path, "--cacheinfo")) {
670 unsigned char sha1[20];
674 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
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]);
684 if (!strcmp(path, "--chmod=-x") ||
685 !strcmp(path, "--chmod=+x")) {
687 die("git-update-index: %s <path>", path);
688 set_executable_bit = path[8];
691 if (!strcmp(path, "--assume-unchanged")) {
692 mark_valid_only = MARK_VALID;
695 if (!strcmp(path, "--no-assume-unchanged")) {
696 mark_valid_only = UNMARK_VALID;
699 if (!strcmp(path, "--info-only")) {
703 if (!strcmp(path, "--force-remove")) {
707 if (!strcmp(path, "-z")) {
708 line_termination = 0;
711 if (!strcmp(path, "--stdin")) {
713 die("--stdin must be at the end");
717 if (!strcmp(path, "--index-info")) {
719 die("--index-info must be at the end");
720 allow_add = allow_replace = allow_remove = 1;
721 read_index_info(line_termination);
724 if (!strcmp(path, "--unresolve")) {
725 has_errors = do_unresolve(argc - i, argv + i);
727 active_cache_changed = 0;
730 if (!strcmp(path, "--ignore-missing")) {
734 if (!strcmp(path, "--verbose")) {
738 if (!strcmp(path, "-h") || !strcmp(path, "--help"))
739 usage(update_index_usage);
740 die("unknown option %s", path);
742 update_one(path, prefix, prefix_length);
743 if (set_executable_bit)
744 chmod_path(set_executable_bit, path);
746 if (read_from_stdin) {
751 read_line(&buf, stdin, line_termination);
754 if (line_termination && buf.buf[0] == '"')
755 path_name = unquote_c_style(buf.buf, NULL);
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);
763 if (path_name != buf.buf)
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);
777 return has_errors ? 1 : 0;