2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
10 #include "cache-tree.h"
12 static unsigned char active_cache_sha1[20];
13 static struct cache_tree *active_cache_tree;
16 * Default to not allowing changes to the list of files. The
17 * tool doesn't actually care, but this makes it harder to add
18 * files to the revision control by mistake by doing something
19 * like "git-update-index *" and suddenly having all the object
20 * files be revision controlled.
23 static int allow_remove;
24 static int allow_replace;
25 static int allow_unmerged; /* --refresh needing merge is not error */
26 static int not_new; /* --refresh not having working tree files is not error */
27 static int quiet; /* --refresh needing update is not error */
29 static int force_remove;
31 static int mark_valid_only = 0;
33 #define UNMARK_VALID 2
36 /* Three functions to allow overloaded pointer return; see linux/err.h */
37 static inline void *ERR_PTR(long error)
39 return (void *) error;
42 static inline long PTR_ERR(const void *ptr)
47 static inline long IS_ERR(const void *ptr)
49 return (unsigned long)ptr > (unsigned long)-1000L;
52 static void report(const char *fmt, ...)
65 static int mark_valid(const char *path)
67 int namelen = strlen(path);
68 int pos = cache_name_pos(path, namelen);
70 switch (mark_valid_only) {
72 active_cache[pos]->ce_flags |= htons(CE_VALID);
75 active_cache[pos]->ce_flags &= ~htons(CE_VALID);
78 cache_tree_invalidate_path(active_cache_tree, path);
79 active_cache_changed = 1;
85 static int add_file_to_cache(const char *path)
87 int size, namelen, option, status;
88 struct cache_entry *ce;
91 status = lstat(path, &st);
93 /* We probably want to do this in remove_file_from_cache() and
94 * add_cache_entry() instead...
96 cache_tree_invalidate_path(active_cache_tree, path);
98 if (status < 0 || S_ISDIR(st.st_mode)) {
99 /* When we used to have "path" and now we want to add
100 * "path/file", we need a way to remove "path" before
101 * being able to add "path/file". However,
102 * "git-update-index --remove path" would not work.
103 * --force-remove can be used but this is more user
104 * friendly, especially since we can do the opposite
105 * case just fine without --force-remove.
107 if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) {
109 if (remove_file_from_cache(path))
110 return error("%s: cannot remove from the index",
114 } else if (status < 0) {
115 return error("%s: does not exist and --remove not passed",
120 return error("%s: is a directory - add files inside instead",
123 return error("lstat(\"%s\"): %s", path,
127 namelen = strlen(path);
128 size = cache_entry_size(namelen);
129 ce = xcalloc(1, size);
130 memcpy(ce->name, path, namelen);
131 ce->ce_flags = htons(namelen);
132 fill_stat_cache_info(ce, &st);
134 ce->ce_mode = create_ce_mode(st.st_mode);
135 if (!trust_executable_bit) {
136 /* If there is an existing entry, pick the mode bits
139 int pos = cache_name_pos(path, namelen);
141 ce->ce_mode = active_cache[pos]->ce_mode;
144 if (index_path(ce->sha1, path, &st, !info_only))
146 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
147 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
148 if (add_cache_entry(ce, option))
149 return error("%s: cannot add to the index - missing --add option?",
155 * "refresh" does not calculate a new sha1 file or bring the
156 * cache up-to-date for mode/content changes. But what it
157 * _does_ do is to "re-match" the stat information of a file
158 * with the cache, so that you can refresh the cache for a
159 * file that hasn't been changed but where the stat entry is
162 * For example, you'd want to do this after doing a "git-read-tree",
163 * to link up the stat cache details with the proper files.
165 static struct cache_entry *refresh_entry(struct cache_entry *ce, int really)
168 struct cache_entry *updated;
171 if (lstat(ce->name, &st) < 0)
172 return ERR_PTR(-errno);
174 changed = ce_match_stat(ce, &st, really);
176 if (really && assume_unchanged &&
177 !(ce->ce_flags & htons(CE_VALID)))
178 ; /* mark this one VALID again */
183 if (ce_modified(ce, &st, really))
184 return ERR_PTR(-EINVAL);
187 updated = xmalloc(size);
188 memcpy(updated, ce, size);
189 fill_stat_cache_info(updated, &st);
191 /* In this case, if really is not set, we should leave
192 * CE_VALID bit alone. Otherwise, paths marked with
193 * --no-assume-unchanged (i.e. things to be edited) will
194 * reacquire CE_VALID bit automatically, which is not
195 * really what we want.
197 if (!really && assume_unchanged && !(ce->ce_flags & htons(CE_VALID)))
198 updated->ce_flags &= ~htons(CE_VALID);
203 static int refresh_cache(int really)
208 for (i = 0; i < active_nr; i++) {
209 struct cache_entry *ce, *new;
210 ce = active_cache[i];
212 while ((i < active_nr) &&
213 ! strcmp(active_cache[i]->name, ce->name))
218 printf("%s: needs merge\n", ce->name);
223 new = refresh_entry(ce, really);
227 if (not_new && PTR_ERR(new) == -ENOENT)
229 if (really && PTR_ERR(new) == -EINVAL) {
230 /* If we are doing --really-refresh that
231 * means the index is not valid anymore.
233 ce->ce_flags &= ~htons(CE_VALID);
234 active_cache_changed = 1;
238 printf("%s: needs update\n", ce->name);
242 active_cache_changed = 1;
243 /* You can NOT just free active_cache[i] here, since it
244 * might not be necessarily malloc()ed but can also come
246 active_cache[i] = new;
252 * We fundamentally don't like some paths: we don't want
253 * dot or dot-dot anywhere, and for obvious reasons don't
254 * want to recurse into ".git" either.
256 * Also, we don't want double slashes or slashes at the
257 * end that can make pathnames ambiguous.
259 static int verify_dotfile(const char *rest)
262 * The first character was '.', but that
263 * has already been discarded, we now test
267 /* "." is not allowed */
272 * ".git" followed by NUL or slash is bad. This
273 * shares the path end test with the ".." case.
283 if (rest[1] == '\0' || rest[1] == '/')
289 static int verify_path(const char *path)
306 if (verify_dotfile(path))
315 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
316 const char *path, int stage)
318 int size, len, option;
319 struct cache_entry *ce;
321 if (!verify_path(path))
325 size = cache_entry_size(len);
326 ce = xcalloc(1, size);
328 memcpy(ce->sha1, sha1, 20);
329 memcpy(ce->name, path, len);
330 ce->ce_flags = create_ce_flags(len, stage);
331 ce->ce_mode = create_ce_mode(mode);
332 if (assume_unchanged)
333 ce->ce_flags |= htons(CE_VALID);
334 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
335 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
336 if (add_cache_entry(ce, option))
337 return error("%s: cannot add to the index - missing --add option?",
339 report("add '%s'", path);
340 cache_tree_invalidate_path(active_cache_tree, path);
344 static int chmod_path(int flip, const char *path)
347 struct cache_entry *ce;
350 pos = cache_name_pos(path, strlen(path));
353 ce = active_cache[pos];
354 mode = ntohl(ce->ce_mode);
359 ce->ce_mode |= htonl(0111); break;
361 ce->ce_mode &= htonl(~0111); break;
365 cache_tree_invalidate_path(active_cache_tree, path);
366 active_cache_changed = 1;
370 static struct cache_file cache_file;
372 static void update_one(const char *path, const char *prefix, int prefix_length)
374 const char *p = prefix_path(prefix, prefix_length, path);
375 if (!verify_path(p)) {
376 fprintf(stderr, "Ignoring path %s\n", path);
379 if (mark_valid_only) {
381 die("Unable to mark file %s", path);
384 cache_tree_invalidate_path(active_cache_tree, path);
387 if (remove_file_from_cache(p))
388 die("git-update-index: unable to remove %s", path);
389 report("remove '%s'", path);
392 if (add_file_to_cache(p))
393 die("Unable to process file %s", path);
394 report("add '%s'", path);
397 static void read_index_info(int line_termination)
404 unsigned char sha1[20];
408 /* This reads lines formatted in one of three formats:
410 * (1) mode SP sha1 TAB path
411 * The first format is what "git-apply --index-info"
412 * reports, and used to reconstruct a partial tree
413 * that is used for phony merge base tree when falling
414 * back on 3-way merge.
416 * (2) mode SP type SP sha1 TAB path
417 * The second format is to stuff git-ls-tree output
418 * into the index file.
420 * (3) mode SP sha1 SP stage TAB path
421 * This format is to put higher order stages into the
422 * index file and matches git-ls-files --stage output.
424 read_line(&buf, stdin, line_termination);
428 mode = strtoul(buf.buf, &ptr, 8);
429 if (ptr == buf.buf || *ptr != ' ')
432 tab = strchr(ptr, '\t');
433 if (!tab || tab - ptr < 41)
436 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
437 stage = tab[-1] - '0';
438 ptr = tab + 1; /* point at the head of path */
439 tab = tab - 2; /* point at tail of sha1 */
443 ptr = tab + 1; /* point at the head of path */
446 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
449 if (line_termination && ptr[0] == '"')
450 path_name = unquote_c_style(ptr, NULL);
454 if (!verify_path(path_name)) {
455 fprintf(stderr, "Ignoring path %s\n", path_name);
456 if (path_name != ptr)
460 cache_tree_invalidate_path(active_cache_tree, path_name);
463 /* mode == 0 means there is no such path -- remove */
464 if (remove_file_from_cache(path_name))
465 die("git-update-index: unable to remove %s",
469 /* mode ' ' sha1 '\t' name
470 * ptr[-1] points at tab,
471 * ptr[-41] is at the beginning of sha1
473 ptr[-42] = ptr[-1] = 0;
474 if (add_cacheinfo(mode, sha1, path_name, stage))
475 die("git-update-index: unable to update %s",
478 if (path_name != ptr)
483 die("malformed index info %s", buf.buf);
487 static const char update_index_usage[] =
488 "git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--cacheinfo] [--chmod=(+|-)x] [--info-only] [--force-remove] [--stdin] [--index-info] [--ignore-missing] [-z] [--verbose] [--] <file>...";
490 int main(int argc, const char **argv)
492 int i, newfd, entries, has_errors = 0, line_termination = '\n';
493 int allow_options = 1;
494 int read_from_stdin = 0;
495 const char *prefix = setup_git_directory();
496 int prefix_length = prefix ? strlen(prefix) : 0;
498 git_config(git_default_config);
500 newfd = hold_index_file_for_update(&cache_file, get_index_file());
502 die("unable to create new cachefile");
504 entries = read_cache_1(active_cache_sha1);
506 die("cache corrupted");
507 active_cache_tree = read_cache_tree(active_cache_sha1);
509 for (i = 1 ; i < argc; i++) {
510 const char *path = argv[i];
512 if (allow_options && *path == '-') {
513 if (!strcmp(path, "--")) {
517 if (!strcmp(path, "-q")) {
521 if (!strcmp(path, "--add")) {
525 if (!strcmp(path, "--replace")) {
529 if (!strcmp(path, "--remove")) {
533 if (!strcmp(path, "--unmerged")) {
537 if (!strcmp(path, "--refresh")) {
538 has_errors |= refresh_cache(0);
541 if (!strcmp(path, "--really-refresh")) {
542 has_errors |= refresh_cache(1);
545 if (!strcmp(path, "--cacheinfo")) {
546 unsigned char sha1[20];
550 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
552 if ((sscanf(argv[i+1], "%o", &mode) != 1) ||
553 get_sha1_hex(argv[i+2], sha1) ||
554 add_cacheinfo(mode, sha1, argv[i+3], 0))
555 die("git-update-index: --cacheinfo"
556 " cannot add %s", argv[i+3]);
560 if (!strcmp(path, "--chmod=-x") ||
561 !strcmp(path, "--chmod=+x")) {
563 die("git-update-index: %s <path>", path);
564 if (chmod_path(path[8], argv[++i]))
565 die("git-update-index: %s cannot chmod %s", path, argv[i]);
568 if (!strcmp(path, "--assume-unchanged")) {
569 mark_valid_only = MARK_VALID;
572 if (!strcmp(path, "--no-assume-unchanged")) {
573 mark_valid_only = UNMARK_VALID;
576 if (!strcmp(path, "--info-only")) {
580 if (!strcmp(path, "--force-remove")) {
584 if (!strcmp(path, "-z")) {
585 line_termination = 0;
588 if (!strcmp(path, "--stdin")) {
590 die("--stdin must be at the end");
594 if (!strcmp(path, "--index-info")) {
596 die("--index-info must be at the end");
597 allow_add = allow_replace = allow_remove = 1;
598 read_index_info(line_termination);
601 if (!strcmp(path, "--ignore-missing")) {
605 if (!strcmp(path, "--verbose")) {
609 if (!strcmp(path, "-h") || !strcmp(path, "--help"))
610 usage(update_index_usage);
611 die("unknown option %s", path);
613 update_one(path, prefix, prefix_length);
615 if (read_from_stdin) {
620 read_line(&buf, stdin, line_termination);
623 if (line_termination && buf.buf[0] == '"')
624 path_name = unquote_c_style(buf.buf, NULL);
627 update_one(path_name, prefix, prefix_length);
628 if (path_name != buf.buf)
632 if (active_cache_changed) {
633 if (write_cache_1(newfd, active_cache, active_nr,
634 active_cache_sha1) ||
635 commit_index_file(&cache_file))
636 die("Unable to write new cachefile");
637 write_cache_tree(active_cache_sha1, active_cache_tree);
640 return has_errors ? 1 : 0;