2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
9 #include "cache-tree.h"
10 #include "tree-walk.h"
11 #include "cache-tree.h"
14 * Default to not allowing changes to the list of files. The
15 * tool doesn't actually care, but this makes it harder to add
16 * files to the revision control by mistake by doing something
17 * like "git-update-index *" and suddenly having all the object
18 * files be revision controlled.
21 static int allow_remove;
22 static int allow_replace;
23 static int allow_unmerged; /* --refresh needing merge is not error */
24 static int not_new; /* --refresh not having working tree files is not error */
25 static int quiet; /* --refresh needing update is not error */
27 static int force_remove;
29 static int mark_valid_only = 0;
31 #define UNMARK_VALID 2
34 /* Three functions to allow overloaded pointer return; see linux/err.h */
35 static inline void *ERR_PTR(long error)
37 return (void *) error;
40 static inline long PTR_ERR(const void *ptr)
45 static inline long IS_ERR(const void *ptr)
47 return (unsigned long)ptr > (unsigned long)-1000L;
50 static void report(const char *fmt, ...)
63 static int mark_valid(const char *path)
65 int namelen = strlen(path);
66 int pos = cache_name_pos(path, namelen);
68 switch (mark_valid_only) {
70 active_cache[pos]->ce_flags |= htons(CE_VALID);
73 active_cache[pos]->ce_flags &= ~htons(CE_VALID);
76 cache_tree_invalidate_path(active_cache_tree, path);
77 active_cache_changed = 1;
83 static int add_file_to_cache(const char *path)
85 int size, namelen, option, status;
86 struct cache_entry *ce;
89 status = lstat(path, &st);
91 /* We probably want to do this in remove_file_from_cache() and
92 * add_cache_entry() instead...
94 cache_tree_invalidate_path(active_cache_tree, path);
96 if (status < 0 || S_ISDIR(st.st_mode)) {
97 /* When we used to have "path" and now we want to add
98 * "path/file", we need a way to remove "path" before
99 * being able to add "path/file". However,
100 * "git-update-index --remove path" would not work.
101 * --force-remove can be used but this is more user
102 * friendly, especially since we can do the opposite
103 * case just fine without --force-remove.
105 if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) {
107 if (remove_file_from_cache(path))
108 return error("%s: cannot remove from the index",
112 } else if (status < 0) {
113 return error("%s: does not exist and --remove not passed",
118 return error("%s: is a directory - add files inside instead",
121 return error("lstat(\"%s\"): %s", path,
125 namelen = strlen(path);
126 size = cache_entry_size(namelen);
127 ce = xcalloc(1, size);
128 memcpy(ce->name, path, namelen);
129 ce->ce_flags = htons(namelen);
130 fill_stat_cache_info(ce, &st);
132 ce->ce_mode = create_ce_mode(st.st_mode);
133 if (!trust_executable_bit) {
134 /* If there is an existing entry, pick the mode bits
137 int pos = cache_name_pos(path, namelen);
139 ce->ce_mode = active_cache[pos]->ce_mode;
142 if (index_path(ce->sha1, path, &st, !info_only))
144 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
145 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
146 if (add_cache_entry(ce, option))
147 return error("%s: cannot add to the index - missing --add option?",
153 * "refresh" does not calculate a new sha1 file or bring the
154 * cache up-to-date for mode/content changes. But what it
155 * _does_ do is to "re-match" the stat information of a file
156 * with the cache, so that you can refresh the cache for a
157 * file that hasn't been changed but where the stat entry is
160 * For example, you'd want to do this after doing a "git-read-tree",
161 * to link up the stat cache details with the proper files.
163 static struct cache_entry *refresh_entry(struct cache_entry *ce, int really)
166 struct cache_entry *updated;
169 if (lstat(ce->name, &st) < 0)
170 return ERR_PTR(-errno);
172 changed = ce_match_stat(ce, &st, really);
174 if (really && assume_unchanged &&
175 !(ce->ce_flags & htons(CE_VALID)))
176 ; /* mark this one VALID again */
181 if (ce_modified(ce, &st, really))
182 return ERR_PTR(-EINVAL);
185 updated = xmalloc(size);
186 memcpy(updated, ce, size);
187 fill_stat_cache_info(updated, &st);
189 /* In this case, if really is not set, we should leave
190 * CE_VALID bit alone. Otherwise, paths marked with
191 * --no-assume-unchanged (i.e. things to be edited) will
192 * reacquire CE_VALID bit automatically, which is not
193 * really what we want.
195 if (!really && assume_unchanged && !(ce->ce_flags & htons(CE_VALID)))
196 updated->ce_flags &= ~htons(CE_VALID);
201 static int refresh_cache(int really)
206 for (i = 0; i < active_nr; i++) {
207 struct cache_entry *ce, *new;
208 ce = active_cache[i];
210 while ((i < active_nr) &&
211 ! strcmp(active_cache[i]->name, ce->name))
216 printf("%s: needs merge\n", ce->name);
221 new = refresh_entry(ce, really);
225 if (not_new && PTR_ERR(new) == -ENOENT)
227 if (really && PTR_ERR(new) == -EINVAL) {
228 /* If we are doing --really-refresh that
229 * means the index is not valid anymore.
231 ce->ce_flags &= ~htons(CE_VALID);
232 active_cache_changed = 1;
236 printf("%s: needs update\n", ce->name);
240 active_cache_changed = 1;
241 /* You can NOT just free active_cache[i] here, since it
242 * might not be necessarily malloc()ed but can also come
244 active_cache[i] = new;
250 * We fundamentally don't like some paths: we don't want
251 * dot or dot-dot anywhere, and for obvious reasons don't
252 * want to recurse into ".git" either.
254 * Also, we don't want double slashes or slashes at the
255 * end that can make pathnames ambiguous.
257 static int verify_dotfile(const char *rest)
260 * The first character was '.', but that
261 * has already been discarded, we now test
265 /* "." is not allowed */
270 * ".git" followed by NUL or slash is bad. This
271 * shares the path end test with the ".." case.
281 if (rest[1] == '\0' || rest[1] == '/')
287 static int verify_path(const char *path)
304 if (verify_dotfile(path))
313 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
314 const char *path, int stage)
316 int size, len, option;
317 struct cache_entry *ce;
319 if (!verify_path(path))
323 size = cache_entry_size(len);
324 ce = xcalloc(1, size);
326 memcpy(ce->sha1, sha1, 20);
327 memcpy(ce->name, path, len);
328 ce->ce_flags = create_ce_flags(len, stage);
329 ce->ce_mode = create_ce_mode(mode);
330 if (assume_unchanged)
331 ce->ce_flags |= htons(CE_VALID);
332 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
333 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
334 if (add_cache_entry(ce, option))
335 return error("%s: cannot add to the index - missing --add option?",
337 report("add '%s'", path);
338 cache_tree_invalidate_path(active_cache_tree, path);
342 static void chmod_path(int flip, const char *path)
345 struct cache_entry *ce;
348 pos = cache_name_pos(path, strlen(path));
351 ce = active_cache[pos];
352 mode = ntohl(ce->ce_mode);
357 ce->ce_mode |= htonl(0111); break;
359 ce->ce_mode &= htonl(~0111); break;
363 cache_tree_invalidate_path(active_cache_tree, path);
364 active_cache_changed = 1;
365 report("chmod %cx '%s'", flip, path);
368 die("git-update-index: cannot chmod %cx '%s'", flip, path);
371 static struct cache_file cache_file;
373 static void update_one(const char *path, const char *prefix, int prefix_length)
375 const char *p = prefix_path(prefix, prefix_length, path);
376 if (!verify_path(p)) {
377 fprintf(stderr, "Ignoring path %s\n", path);
380 if (mark_valid_only) {
382 die("Unable to mark file %s", path);
385 cache_tree_invalidate_path(active_cache_tree, path);
388 if (remove_file_from_cache(p))
389 die("git-update-index: unable to remove %s", path);
390 report("remove '%s'", path);
393 if (add_file_to_cache(p))
394 die("Unable to process file %s", path);
395 report("add '%s'", path);
398 static void read_index_info(int line_termination)
405 unsigned char sha1[20];
409 /* This reads lines formatted in one of three formats:
411 * (1) mode SP sha1 TAB path
412 * The first format is what "git-apply --index-info"
413 * reports, and used to reconstruct a partial tree
414 * that is used for phony merge base tree when falling
415 * back on 3-way merge.
417 * (2) mode SP type SP sha1 TAB path
418 * The second format is to stuff git-ls-tree output
419 * into the index file.
421 * (3) mode SP sha1 SP stage TAB path
422 * This format is to put higher order stages into the
423 * index file and matches git-ls-files --stage output.
425 read_line(&buf, stdin, line_termination);
429 mode = strtoul(buf.buf, &ptr, 8);
430 if (ptr == buf.buf || *ptr != ' ')
433 tab = strchr(ptr, '\t');
434 if (!tab || tab - ptr < 41)
437 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
438 stage = tab[-1] - '0';
439 ptr = tab + 1; /* point at the head of path */
440 tab = tab - 2; /* point at tail of sha1 */
444 ptr = tab + 1; /* point at the head of path */
447 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
450 if (line_termination && ptr[0] == '"')
451 path_name = unquote_c_style(ptr, NULL);
455 if (!verify_path(path_name)) {
456 fprintf(stderr, "Ignoring path %s\n", path_name);
457 if (path_name != ptr)
461 cache_tree_invalidate_path(active_cache_tree, path_name);
464 /* mode == 0 means there is no such path -- remove */
465 if (remove_file_from_cache(path_name))
466 die("git-update-index: unable to remove %s",
470 /* mode ' ' sha1 '\t' name
471 * ptr[-1] points at tab,
472 * ptr[-41] is at the beginning of sha1
474 ptr[-42] = ptr[-1] = 0;
475 if (add_cacheinfo(mode, sha1, path_name, stage))
476 die("git-update-index: unable to update %s",
479 if (path_name != ptr)
484 die("malformed index info %s", buf.buf);
488 static const char update_index_usage[] =
489 "git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--cacheinfo] [--chmod=(+|-)x] [--info-only] [--force-remove] [--stdin] [--index-info] [--ignore-missing] [-z] [--verbose] [--] <file>...";
491 static unsigned char head_sha1[20];
492 static unsigned char merge_head_sha1[20];
494 static struct cache_entry *read_one_ent(const char *which,
495 unsigned char *ent, const char *path,
496 int namelen, int stage)
499 unsigned char sha1[20];
501 struct cache_entry *ce;
503 if (get_tree_entry(ent, path, sha1, &mode)) {
504 error("%s: not in %s branch.", path, which);
507 if (mode == S_IFDIR) {
508 error("%s: not a blob in %s branch.", path, which);
511 size = cache_entry_size(namelen);
512 ce = xcalloc(1, size);
514 memcpy(ce->sha1, sha1, 20);
515 memcpy(ce->name, path, namelen);
516 ce->ce_flags = create_ce_flags(namelen, stage);
517 ce->ce_mode = create_ce_mode(mode);
521 static int unresolve_one(const char *path)
523 int namelen = strlen(path);
526 struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
528 /* See if there is such entry in the index. */
529 pos = cache_name_pos(path, namelen);
531 /* If there isn't, either it is unmerged, or
532 * resolved as "removed" by mistake. We do not
533 * want to do anything in the former case.
536 if (pos < active_nr) {
537 struct cache_entry *ce = active_cache[pos];
538 if (ce_namelen(ce) == namelen &&
539 !memcmp(ce->name, path, namelen)) {
541 "%s: skipping still unmerged path.\n",
548 /* Grab blobs from given path from HEAD and MERGE_HEAD,
549 * stuff HEAD version in stage #2,
550 * stuff MERGE_HEAD version in stage #3.
552 ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
553 ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);
555 if (!ce_2 || !ce_3) {
559 if (!memcmp(ce_2->sha1, ce_3->sha1, 20) &&
560 ce_2->ce_mode == ce_3->ce_mode) {
561 fprintf(stderr, "%s: identical in both, skipping.\n",
566 cache_tree_invalidate_path(active_cache_tree, path);
567 remove_file_from_cache(path);
568 if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
569 error("%s: cannot add our version to the index.", path);
573 if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
575 error("%s: cannot add their version to the index.", path);
583 static void read_head_pointers(void)
585 if (read_ref(git_path("HEAD"), head_sha1))
586 die("No HEAD -- no initial commit yet?\n");
587 if (read_ref(git_path("MERGE_HEAD"), merge_head_sha1)) {
588 fprintf(stderr, "Not in the middle of a merge.\n");
593 static int do_unresolve(int ac, const char **av)
598 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
599 * are not doing a merge, so exit with success status.
601 read_head_pointers();
603 for (i = 1; i < ac; i++) {
604 const char *arg = av[i];
605 err |= unresolve_one(arg);
610 int main(int argc, const char **argv)
612 int i, newfd, entries, has_errors = 0, line_termination = '\n';
613 int allow_options = 1;
614 int read_from_stdin = 0;
615 const char *prefix = setup_git_directory();
616 int prefix_length = prefix ? strlen(prefix) : 0;
617 char set_executable_bit = 0;
619 git_config(git_default_config);
621 newfd = hold_index_file_for_update(&cache_file, get_index_file());
623 die("unable to create new cachefile");
625 entries = read_cache();
627 die("cache corrupted");
629 for (i = 1 ; i < argc; i++) {
630 const char *path = argv[i];
632 if (allow_options && *path == '-') {
633 if (!strcmp(path, "--")) {
637 if (!strcmp(path, "-q")) {
641 if (!strcmp(path, "--add")) {
645 if (!strcmp(path, "--replace")) {
649 if (!strcmp(path, "--remove")) {
653 if (!strcmp(path, "--unmerged")) {
657 if (!strcmp(path, "--refresh")) {
658 has_errors |= refresh_cache(0);
661 if (!strcmp(path, "--really-refresh")) {
662 has_errors |= refresh_cache(1);
665 if (!strcmp(path, "--cacheinfo")) {
666 unsigned char sha1[20];
670 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
672 if ((sscanf(argv[i+1], "%o", &mode) != 1) ||
673 get_sha1_hex(argv[i+2], sha1) ||
674 add_cacheinfo(mode, sha1, argv[i+3], 0))
675 die("git-update-index: --cacheinfo"
676 " cannot add %s", argv[i+3]);
680 if (!strcmp(path, "--chmod=-x") ||
681 !strcmp(path, "--chmod=+x")) {
683 die("git-update-index: %s <path>", path);
684 set_executable_bit = path[8];
687 if (!strcmp(path, "--assume-unchanged")) {
688 mark_valid_only = MARK_VALID;
691 if (!strcmp(path, "--no-assume-unchanged")) {
692 mark_valid_only = UNMARK_VALID;
695 if (!strcmp(path, "--info-only")) {
699 if (!strcmp(path, "--force-remove")) {
703 if (!strcmp(path, "-z")) {
704 line_termination = 0;
707 if (!strcmp(path, "--stdin")) {
709 die("--stdin must be at the end");
713 if (!strcmp(path, "--index-info")) {
715 die("--index-info must be at the end");
716 allow_add = allow_replace = allow_remove = 1;
717 read_index_info(line_termination);
720 if (!strcmp(path, "--unresolve")) {
721 has_errors = do_unresolve(argc - i, argv + i);
723 active_cache_changed = 0;
726 if (!strcmp(path, "--ignore-missing")) {
730 if (!strcmp(path, "--verbose")) {
734 if (!strcmp(path, "-h") || !strcmp(path, "--help"))
735 usage(update_index_usage);
736 die("unknown option %s", path);
738 update_one(path, prefix, prefix_length);
739 if (set_executable_bit)
740 chmod_path(set_executable_bit, path);
742 if (read_from_stdin) {
747 read_line(&buf, stdin, line_termination);
750 if (line_termination && buf.buf[0] == '"')
751 path_name = unquote_c_style(buf.buf, NULL);
754 update_one(path_name, prefix, prefix_length);
755 if (set_executable_bit) {
756 const char *p = prefix_path(prefix, prefix_length, path_name);
757 chmod_path(set_executable_bit, p);
759 if (path_name != buf.buf)
765 if (active_cache_changed) {
766 if (write_cache(newfd, active_cache, active_nr) ||
767 commit_index_file(&cache_file))
768 die("Unable to write new cachefile");
771 return has_errors ? 1 : 0;