2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
12 * Default to not allowing changes to the list of files. The
13 * tool doesn't actually care, but this makes it harder to add
14 * files to the revision control by mistake by doing something
15 * like "git-update-index *" and suddenly having all the object
16 * files be revision controlled.
19 static int allow_remove;
20 static int allow_replace;
21 static int allow_unmerged; /* --refresh needing merge is not error */
22 static int not_new; /* --refresh not having working tree files is not error */
23 static int quiet; /* --refresh needing update is not error */
25 static int force_remove;
27 static int mark_valid_only = 0;
29 #define UNMARK_VALID 2
32 /* Three functions to allow overloaded pointer return; see linux/err.h */
33 static inline void *ERR_PTR(long error)
35 return (void *) error;
38 static inline long PTR_ERR(const void *ptr)
43 static inline long IS_ERR(const void *ptr)
45 return (unsigned long)ptr > (unsigned long)-1000L;
48 static void report(const char *fmt, ...)
61 static int mark_valid(const char *path)
63 int namelen = strlen(path);
64 int pos = cache_name_pos(path, namelen);
66 switch (mark_valid_only) {
68 active_cache[pos]->ce_flags |= htons(CE_VALID);
71 active_cache[pos]->ce_flags &= ~htons(CE_VALID);
74 active_cache_changed = 1;
80 static int add_file_to_cache(const char *path)
82 int size, namelen, option, status;
83 struct cache_entry *ce;
86 status = lstat(path, &st);
87 if (status < 0 || S_ISDIR(st.st_mode)) {
88 /* When we used to have "path" and now we want to add
89 * "path/file", we need a way to remove "path" before
90 * being able to add "path/file". However,
91 * "git-update-index --remove path" would not work.
92 * --force-remove can be used but this is more user
93 * friendly, especially since we can do the opposite
94 * case just fine without --force-remove.
96 if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) {
98 if (remove_file_from_cache(path))
99 return error("%s: cannot remove from the index",
103 } else if (status < 0) {
104 return error("%s: does not exist and --remove not passed",
109 return error("%s: is a directory - add files inside instead",
112 return error("lstat(\"%s\"): %s", path,
116 namelen = strlen(path);
117 size = cache_entry_size(namelen);
118 ce = xcalloc(1, size);
119 memcpy(ce->name, path, namelen);
120 ce->ce_flags = htons(namelen);
121 fill_stat_cache_info(ce, &st);
123 ce->ce_mode = create_ce_mode(st.st_mode);
124 if (!trust_executable_bit) {
125 /* If there is an existing entry, pick the mode bits
128 int pos = cache_name_pos(path, namelen);
130 ce->ce_mode = active_cache[pos]->ce_mode;
133 if (index_path(ce->sha1, path, &st, !info_only))
135 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
136 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
137 if (add_cache_entry(ce, option))
138 return error("%s: cannot add to the index - missing --add option?",
144 * "refresh" does not calculate a new sha1 file or bring the
145 * cache up-to-date for mode/content changes. But what it
146 * _does_ do is to "re-match" the stat information of a file
147 * with the cache, so that you can refresh the cache for a
148 * file that hasn't been changed but where the stat entry is
151 * For example, you'd want to do this after doing a "git-read-tree",
152 * to link up the stat cache details with the proper files.
154 static struct cache_entry *refresh_entry(struct cache_entry *ce, int really)
157 struct cache_entry *updated;
160 if (lstat(ce->name, &st) < 0)
161 return ERR_PTR(-errno);
163 changed = ce_match_stat(ce, &st, really);
165 if (really && assume_unchanged &&
166 !(ce->ce_flags & htons(CE_VALID)))
167 ; /* mark this one VALID again */
172 if (ce_modified(ce, &st, really))
173 return ERR_PTR(-EINVAL);
176 updated = xmalloc(size);
177 memcpy(updated, ce, size);
178 fill_stat_cache_info(updated, &st);
180 /* In this case, if really is not set, we should leave
181 * CE_VALID bit alone. Otherwise, paths marked with
182 * --no-assume-unchanged (i.e. things to be edited) will
183 * reacquire CE_VALID bit automatically, which is not
184 * really what we want.
186 if (!really && assume_unchanged && !(ce->ce_flags & htons(CE_VALID)))
187 updated->ce_flags &= ~htons(CE_VALID);
192 static int refresh_cache(int really)
197 for (i = 0; i < active_nr; i++) {
198 struct cache_entry *ce, *new;
199 ce = active_cache[i];
201 while ((i < active_nr) &&
202 ! strcmp(active_cache[i]->name, ce->name))
207 printf("%s: needs merge\n", ce->name);
212 new = refresh_entry(ce, really);
216 if (not_new && PTR_ERR(new) == -ENOENT)
218 if (really && PTR_ERR(new) == -EINVAL) {
219 /* If we are doing --really-refresh that
220 * means the index is not valid anymore.
222 ce->ce_flags &= ~htons(CE_VALID);
223 active_cache_changed = 1;
227 printf("%s: needs update\n", ce->name);
231 active_cache_changed = 1;
232 /* You can NOT just free active_cache[i] here, since it
233 * might not be necessarily malloc()ed but can also come
235 active_cache[i] = new;
241 * We fundamentally don't like some paths: we don't want
242 * dot or dot-dot anywhere, and for obvious reasons don't
243 * want to recurse into ".git" either.
245 * Also, we don't want double slashes or slashes at the
246 * end that can make pathnames ambiguous.
248 static int verify_dotfile(const char *rest)
251 * The first character was '.', but that
252 * has already been discarded, we now test
256 /* "." is not allowed */
261 * ".git" followed by NUL or slash is bad. This
262 * shares the path end test with the ".." case.
272 if (rest[1] == '\0' || rest[1] == '/')
278 static int verify_path(const char *path)
295 if (verify_dotfile(path))
304 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
305 const char *path, int stage)
307 int size, len, option;
308 struct cache_entry *ce;
310 if (!verify_path(path))
314 size = cache_entry_size(len);
315 ce = xcalloc(1, size);
317 memcpy(ce->sha1, sha1, 20);
318 memcpy(ce->name, path, len);
319 ce->ce_flags = create_ce_flags(len, stage);
320 ce->ce_mode = create_ce_mode(mode);
321 if (assume_unchanged)
322 ce->ce_flags |= htons(CE_VALID);
323 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
324 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
325 if (add_cache_entry(ce, option))
326 return error("%s: cannot add to the index - missing --add option?",
328 report("add '%s'", path);
332 static void chmod_path(int flip, const char *path)
335 struct cache_entry *ce;
338 pos = cache_name_pos(path, strlen(path));
341 ce = active_cache[pos];
342 mode = ntohl(ce->ce_mode);
347 ce->ce_mode |= htonl(0111); break;
349 ce->ce_mode &= htonl(~0111); break;
353 active_cache_changed = 1;
354 report("chmod %cx '%s'", flip, path);
357 die("git-update-index: cannot chmod %cx '%s'", flip, path);
360 static struct cache_file cache_file;
362 static void update_one(const char *path, const char *prefix, int prefix_length)
364 const char *p = prefix_path(prefix, prefix_length, path);
365 if (!verify_path(p)) {
366 fprintf(stderr, "Ignoring path %s\n", path);
369 if (mark_valid_only) {
371 die("Unable to mark file %s", path);
376 if (remove_file_from_cache(p))
377 die("git-update-index: unable to remove %s", path);
378 report("remove '%s'", path);
381 if (add_file_to_cache(p))
382 die("Unable to process file %s", path);
383 report("add '%s'", path);
386 static void read_index_info(int line_termination)
393 unsigned char sha1[20];
397 /* This reads lines formatted in one of three formats:
399 * (1) mode SP sha1 TAB path
400 * The first format is what "git-apply --index-info"
401 * reports, and used to reconstruct a partial tree
402 * that is used for phony merge base tree when falling
403 * back on 3-way merge.
405 * (2) mode SP type SP sha1 TAB path
406 * The second format is to stuff git-ls-tree output
407 * into the index file.
409 * (3) mode SP sha1 SP stage TAB path
410 * This format is to put higher order stages into the
411 * index file and matches git-ls-files --stage output.
413 read_line(&buf, stdin, line_termination);
417 mode = strtoul(buf.buf, &ptr, 8);
418 if (ptr == buf.buf || *ptr != ' ')
421 tab = strchr(ptr, '\t');
422 if (!tab || tab - ptr < 41)
425 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
426 stage = tab[-1] - '0';
427 ptr = tab + 1; /* point at the head of path */
428 tab = tab - 2; /* point at tail of sha1 */
432 ptr = tab + 1; /* point at the head of path */
435 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
438 if (line_termination && ptr[0] == '"')
439 path_name = unquote_c_style(ptr, NULL);
443 if (!verify_path(path_name)) {
444 fprintf(stderr, "Ignoring path %s\n", path_name);
445 if (path_name != ptr)
451 /* mode == 0 means there is no such path -- remove */
452 if (remove_file_from_cache(path_name))
453 die("git-update-index: unable to remove %s",
457 /* mode ' ' sha1 '\t' name
458 * ptr[-1] points at tab,
459 * ptr[-41] is at the beginning of sha1
461 ptr[-42] = ptr[-1] = 0;
462 if (add_cacheinfo(mode, sha1, path_name, stage))
463 die("git-update-index: unable to update %s",
466 if (path_name != ptr)
471 die("malformed index info %s", buf.buf);
475 static const char update_index_usage[] =
476 "git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--cacheinfo] [--chmod=(+|-)x] [--info-only] [--force-remove] [--stdin] [--index-info] [--ignore-missing] [-z] [--verbose] [--] <file>...";
478 static unsigned char head_sha1[20];
479 static unsigned char merge_head_sha1[20];
481 static struct cache_entry *read_one_ent(const char *which,
482 unsigned char *ent, const char *path,
483 int namelen, int stage)
486 unsigned char sha1[20];
488 struct cache_entry *ce;
490 if (get_tree_entry(ent, path, sha1, &mode)) {
491 error("%s: not in %s branch.", path, which);
494 if (mode == S_IFDIR) {
495 error("%s: not a blob in %s branch.", path, which);
498 size = cache_entry_size(namelen);
499 ce = xcalloc(1, size);
501 memcpy(ce->sha1, sha1, 20);
502 memcpy(ce->name, path, namelen);
503 ce->ce_flags = create_ce_flags(namelen, stage);
504 ce->ce_mode = create_ce_mode(mode);
508 static int unresolve_one(const char *path)
510 int namelen = strlen(path);
513 struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
515 /* See if there is such entry in the index. */
516 pos = cache_name_pos(path, namelen);
518 /* If there isn't, either it is unmerged, or
519 * resolved as "removed" by mistake. We do not
520 * want to do anything in the former case.
523 if (pos < active_nr) {
524 struct cache_entry *ce = active_cache[pos];
525 if (ce_namelen(ce) == namelen &&
526 !memcmp(ce->name, path, namelen)) {
528 "%s: skipping still unmerged path.\n",
535 /* Grab blobs from given path from HEAD and MERGE_HEAD,
536 * stuff HEAD version in stage #2,
537 * stuff MERGE_HEAD version in stage #3.
539 ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
540 ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);
542 if (!ce_2 || !ce_3) {
546 if (!memcmp(ce_2->sha1, ce_3->sha1, 20) &&
547 ce_2->ce_mode == ce_3->ce_mode) {
548 fprintf(stderr, "%s: identical in both, skipping.\n",
553 remove_file_from_cache(path);
554 if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
555 error("%s: cannot add our version to the index.", path);
559 if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
561 error("%s: cannot add their version to the index.", path);
569 static void read_head_pointers(void)
571 if (read_ref(git_path("HEAD"), head_sha1))
572 die("No HEAD -- no initial commit yet?\n");
573 if (read_ref(git_path("MERGE_HEAD"), merge_head_sha1)) {
574 fprintf(stderr, "Not in the middle of a merge.\n");
579 static int do_unresolve(int ac, const char **av)
584 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
585 * are not doing a merge, so exit with success status.
587 read_head_pointers();
589 for (i = 1; i < ac; i++) {
590 const char *arg = av[i];
591 err |= unresolve_one(arg);
596 int main(int argc, const char **argv)
598 int i, newfd, entries, has_errors = 0, line_termination = '\n';
599 int allow_options = 1;
600 int read_from_stdin = 0;
601 const char *prefix = setup_git_directory();
602 int prefix_length = prefix ? strlen(prefix) : 0;
603 char set_executable_bit = 0;
605 git_config(git_default_config);
607 newfd = hold_index_file_for_update(&cache_file, get_index_file());
609 die("unable to create new cachefile");
611 entries = read_cache();
613 die("cache corrupted");
615 for (i = 1 ; i < argc; i++) {
616 const char *path = argv[i];
618 if (allow_options && *path == '-') {
619 if (!strcmp(path, "--")) {
623 if (!strcmp(path, "-q")) {
627 if (!strcmp(path, "--add")) {
631 if (!strcmp(path, "--replace")) {
635 if (!strcmp(path, "--remove")) {
639 if (!strcmp(path, "--unmerged")) {
643 if (!strcmp(path, "--refresh")) {
644 has_errors |= refresh_cache(0);
647 if (!strcmp(path, "--really-refresh")) {
648 has_errors |= refresh_cache(1);
651 if (!strcmp(path, "--cacheinfo")) {
652 unsigned char sha1[20];
656 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
658 if ((sscanf(argv[i+1], "%o", &mode) != 1) ||
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]);
666 if (!strcmp(path, "--chmod=-x") ||
667 !strcmp(path, "--chmod=+x")) {
669 die("git-update-index: %s <path>", path);
670 set_executable_bit = path[8];
673 if (!strcmp(path, "--assume-unchanged")) {
674 mark_valid_only = MARK_VALID;
677 if (!strcmp(path, "--no-assume-unchanged")) {
678 mark_valid_only = UNMARK_VALID;
681 if (!strcmp(path, "--info-only")) {
685 if (!strcmp(path, "--force-remove")) {
689 if (!strcmp(path, "-z")) {
690 line_termination = 0;
693 if (!strcmp(path, "--stdin")) {
695 die("--stdin must be at the end");
699 if (!strcmp(path, "--index-info")) {
701 die("--index-info must be at the end");
702 allow_add = allow_replace = allow_remove = 1;
703 read_index_info(line_termination);
706 if (!strcmp(path, "--unresolve")) {
707 has_errors = do_unresolve(argc - i, argv + i);
709 active_cache_changed = 0;
712 if (!strcmp(path, "--ignore-missing")) {
716 if (!strcmp(path, "--verbose")) {
720 if (!strcmp(path, "-h") || !strcmp(path, "--help"))
721 usage(update_index_usage);
722 die("unknown option %s", path);
724 update_one(path, prefix, prefix_length);
725 if (set_executable_bit)
726 chmod_path(set_executable_bit, path);
728 if (read_from_stdin) {
733 read_line(&buf, stdin, line_termination);
736 if (line_termination && buf.buf[0] == '"')
737 path_name = unquote_c_style(buf.buf, NULL);
740 update_one(path_name, prefix, prefix_length);
741 if (set_executable_bit) {
742 const char *p = prefix_path(prefix, prefix_length, path_name);
743 chmod_path(set_executable_bit, p);
745 if (path_name != buf.buf)
751 if (active_cache_changed) {
752 if (write_cache(newfd, active_cache, active_nr) ||
753 commit_index_file(&cache_file))
754 die("Unable to write new cachefile");
757 return has_errors ? 1 : 0;