2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
9 #include "cache-tree.h"
10 #include "tree-walk.h"
13 * Default to not allowing changes to the list of files. The
14 * tool doesn't actually care, but this makes it harder to add
15 * files to the revision control by mistake by doing something
16 * like "git-update-index *" and suddenly having all the object
17 * files be revision controlled.
20 static int allow_remove;
21 static int allow_replace;
23 static int force_remove;
25 static int mark_valid_only = 0;
27 #define UNMARK_VALID 2
29 static void report(const char *fmt, ...)
42 static int mark_valid(const char *path)
44 int namelen = strlen(path);
45 int pos = cache_name_pos(path, namelen);
47 switch (mark_valid_only) {
49 active_cache[pos]->ce_flags |= htons(CE_VALID);
52 active_cache[pos]->ce_flags &= ~htons(CE_VALID);
55 cache_tree_invalidate_path(active_cache_tree, path);
56 active_cache_changed = 1;
62 static int add_file_to_cache(const char *path)
64 int size, namelen, option, status;
65 struct cache_entry *ce;
68 status = lstat(path, &st);
70 /* We probably want to do this in remove_file_from_cache() and
71 * add_cache_entry() instead...
73 cache_tree_invalidate_path(active_cache_tree, path);
75 if (status < 0 || S_ISDIR(st.st_mode)) {
76 /* When we used to have "path" and now we want to add
77 * "path/file", we need a way to remove "path" before
78 * being able to add "path/file". However,
79 * "git-update-index --remove path" would not work.
80 * --force-remove can be used but this is more user
81 * friendly, especially since we can do the opposite
82 * case just fine without --force-remove.
84 if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) {
86 if (remove_file_from_cache(path))
87 return error("%s: cannot remove from the index",
91 } else if (status < 0) {
92 return error("%s: does not exist and --remove not passed",
97 return error("%s: is a directory - add files inside instead",
100 return error("lstat(\"%s\"): %s", path,
104 namelen = strlen(path);
105 size = cache_entry_size(namelen);
106 ce = xcalloc(1, size);
107 memcpy(ce->name, path, namelen);
108 ce->ce_flags = htons(namelen);
109 fill_stat_cache_info(ce, &st);
111 ce->ce_mode = create_ce_mode(st.st_mode);
112 if (!trust_executable_bit) {
113 /* If there is an existing entry, pick the mode bits
116 int pos = cache_name_pos(path, namelen);
118 ce->ce_mode = active_cache[pos]->ce_mode;
121 if (index_path(ce->sha1, path, &st, !info_only))
123 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
124 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
125 if (add_cache_entry(ce, option))
126 return error("%s: cannot add to the index - missing --add option?",
131 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
132 const char *path, int stage)
134 int size, len, option;
135 struct cache_entry *ce;
137 if (!verify_path(path))
141 size = cache_entry_size(len);
142 ce = xcalloc(1, size);
144 memcpy(ce->sha1, sha1, 20);
145 memcpy(ce->name, path, len);
146 ce->ce_flags = create_ce_flags(len, stage);
147 ce->ce_mode = create_ce_mode(mode);
148 if (assume_unchanged)
149 ce->ce_flags |= htons(CE_VALID);
150 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
151 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
152 if (add_cache_entry(ce, option))
153 return error("%s: cannot add to the index - missing --add option?",
155 report("add '%s'", path);
156 cache_tree_invalidate_path(active_cache_tree, path);
160 static void chmod_path(int flip, const char *path)
163 struct cache_entry *ce;
166 pos = cache_name_pos(path, strlen(path));
169 ce = active_cache[pos];
170 mode = ntohl(ce->ce_mode);
175 ce->ce_mode |= htonl(0111); break;
177 ce->ce_mode &= htonl(~0111); break;
181 cache_tree_invalidate_path(active_cache_tree, path);
182 active_cache_changed = 1;
183 report("chmod %cx '%s'", flip, path);
186 die("git-update-index: cannot chmod %cx '%s'", flip, path);
189 static struct lock_file lock_file;
191 static void update_one(const char *path, const char *prefix, int prefix_length)
193 const char *p = prefix_path(prefix, prefix_length, path);
194 if (!verify_path(p)) {
195 fprintf(stderr, "Ignoring path %s\n", path);
198 if (mark_valid_only) {
200 die("Unable to mark file %s", path);
203 cache_tree_invalidate_path(active_cache_tree, path);
206 if (remove_file_from_cache(p))
207 die("git-update-index: unable to remove %s", path);
208 report("remove '%s'", path);
211 if (add_file_to_cache(p))
212 die("Unable to process file %s", path);
213 report("add '%s'", path);
215 if (p < path || p > path + strlen(path))
219 static void read_index_info(int line_termination)
226 unsigned char sha1[20];
230 /* This reads lines formatted in one of three formats:
232 * (1) mode SP sha1 TAB path
233 * The first format is what "git-apply --index-info"
234 * reports, and used to reconstruct a partial tree
235 * that is used for phony merge base tree when falling
236 * back on 3-way merge.
238 * (2) mode SP type SP sha1 TAB path
239 * The second format is to stuff git-ls-tree output
240 * into the index file.
242 * (3) mode SP sha1 SP stage TAB path
243 * This format is to put higher order stages into the
244 * index file and matches git-ls-files --stage output.
246 read_line(&buf, stdin, line_termination);
250 mode = strtoul(buf.buf, &ptr, 8);
251 if (ptr == buf.buf || *ptr != ' ')
254 tab = strchr(ptr, '\t');
255 if (!tab || tab - ptr < 41)
258 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
259 stage = tab[-1] - '0';
260 ptr = tab + 1; /* point at the head of path */
261 tab = tab - 2; /* point at tail of sha1 */
265 ptr = tab + 1; /* point at the head of path */
268 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
271 if (line_termination && ptr[0] == '"')
272 path_name = unquote_c_style(ptr, NULL);
276 if (!verify_path(path_name)) {
277 fprintf(stderr, "Ignoring path %s\n", path_name);
278 if (path_name != ptr)
282 cache_tree_invalidate_path(active_cache_tree, path_name);
285 /* mode == 0 means there is no such path -- remove */
286 if (remove_file_from_cache(path_name))
287 die("git-update-index: unable to remove %s",
291 /* mode ' ' sha1 '\t' name
292 * ptr[-1] points at tab,
293 * ptr[-41] is at the beginning of sha1
295 ptr[-42] = ptr[-1] = 0;
296 if (add_cacheinfo(mode, sha1, path_name, stage))
297 die("git-update-index: unable to update %s",
300 if (path_name != ptr)
305 die("malformed index info %s", buf.buf);
309 static const char update_index_usage[] =
310 "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] [--ignore-missing] [-z] [--verbose] [--] <file>...";
312 static unsigned char head_sha1[20];
313 static unsigned char merge_head_sha1[20];
315 static struct cache_entry *read_one_ent(const char *which,
316 unsigned char *ent, const char *path,
317 int namelen, int stage)
320 unsigned char sha1[20];
322 struct cache_entry *ce;
324 if (get_tree_entry(ent, path, sha1, &mode)) {
326 error("%s: not in %s branch.", path, which);
329 if (mode == S_IFDIR) {
331 error("%s: not a blob in %s branch.", path, which);
334 size = cache_entry_size(namelen);
335 ce = xcalloc(1, size);
337 memcpy(ce->sha1, sha1, 20);
338 memcpy(ce->name, path, namelen);
339 ce->ce_flags = create_ce_flags(namelen, stage);
340 ce->ce_mode = create_ce_mode(mode);
344 static int unresolve_one(const char *path)
346 int namelen = strlen(path);
349 struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
351 /* See if there is such entry in the index. */
352 pos = cache_name_pos(path, namelen);
354 /* If there isn't, either it is unmerged, or
355 * resolved as "removed" by mistake. We do not
356 * want to do anything in the former case.
359 if (pos < active_nr) {
360 struct cache_entry *ce = active_cache[pos];
361 if (ce_namelen(ce) == namelen &&
362 !memcmp(ce->name, path, namelen)) {
364 "%s: skipping still unmerged path.\n",
371 /* Grab blobs from given path from HEAD and MERGE_HEAD,
372 * stuff HEAD version in stage #2,
373 * stuff MERGE_HEAD version in stage #3.
375 ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
376 ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);
378 if (!ce_2 || !ce_3) {
382 if (!memcmp(ce_2->sha1, ce_3->sha1, 20) &&
383 ce_2->ce_mode == ce_3->ce_mode) {
384 fprintf(stderr, "%s: identical in both, skipping.\n",
389 cache_tree_invalidate_path(active_cache_tree, path);
390 remove_file_from_cache(path);
391 if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
392 error("%s: cannot add our version to the index.", path);
396 if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
398 error("%s: cannot add their version to the index.", path);
406 static void read_head_pointers(void)
408 if (read_ref(git_path("HEAD"), head_sha1))
409 die("No HEAD -- no initial commit yet?\n");
410 if (read_ref(git_path("MERGE_HEAD"), merge_head_sha1)) {
411 fprintf(stderr, "Not in the middle of a merge.\n");
416 static int do_unresolve(int ac, const char **av,
417 const char *prefix, int prefix_length)
422 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
423 * are not doing a merge, so exit with success status.
425 read_head_pointers();
427 for (i = 1; i < ac; i++) {
428 const char *arg = av[i];
429 const char *p = prefix_path(prefix, prefix_length, arg);
430 err |= unresolve_one(p);
431 if (p < arg || p > arg + strlen(arg))
437 static int do_reupdate(int ac, const char **av,
438 const char *prefix, int prefix_length)
440 /* Read HEAD and run update-index on paths that are
441 * merged and already different between index and HEAD.
445 const char **pathspec = get_pathspec(prefix, av + 1);
447 if (read_ref(git_path("HEAD"), head_sha1))
448 /* If there is no HEAD, that means it is an initial
449 * commit. Update everything in the index.
453 for (pos = 0; pos < active_nr; pos++) {
454 struct cache_entry *ce = active_cache[pos];
455 struct cache_entry *old = NULL;
458 if (ce_stage(ce) || !ce_path_match(ce, pathspec))
461 old = read_one_ent(NULL, head_sha1,
462 ce->name, ce_namelen(ce), 0);
463 if (old && ce->ce_mode == old->ce_mode &&
464 !memcmp(ce->sha1, old->sha1, 20)) {
466 continue; /* unchanged */
468 /* Be careful. The working tree may not have the
469 * path anymore, in which case, under 'allow_remove',
470 * or worse yet 'allow_replace', active_nr may decrease.
473 update_one(ce->name + prefix_length, prefix, prefix_length);
474 if (save_nr != active_nr)
480 int main(int argc, const char **argv)
482 int i, newfd, entries, has_errors = 0, line_termination = '\n';
483 int allow_options = 1;
484 int read_from_stdin = 0;
485 const char *prefix = setup_git_directory();
486 int prefix_length = prefix ? strlen(prefix) : 0;
487 char set_executable_bit = 0;
488 unsigned int refresh_flags = 0;
490 git_config(git_default_config);
492 newfd = hold_lock_file_for_update(&lock_file, get_index_file());
494 die("unable to create new index file");
496 entries = read_cache();
498 die("cache corrupted");
500 for (i = 1 ; i < argc; i++) {
501 const char *path = argv[i];
503 if (allow_options && *path == '-') {
504 if (!strcmp(path, "--")) {
508 if (!strcmp(path, "-q")) {
509 refresh_flags |= REFRESH_QUIET;
512 if (!strcmp(path, "--add")) {
516 if (!strcmp(path, "--replace")) {
520 if (!strcmp(path, "--remove")) {
524 if (!strcmp(path, "--unmerged")) {
525 refresh_flags |= REFRESH_UNMERGED;
528 if (!strcmp(path, "--refresh")) {
529 has_errors |= refresh_cache(refresh_flags);
532 if (!strcmp(path, "--really-refresh")) {
533 has_errors |= refresh_cache(REFRESH_REALLY | refresh_flags);
536 if (!strcmp(path, "--cacheinfo")) {
537 unsigned char sha1[20];
541 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
543 if ((sscanf(argv[i+1], "%o", &mode) != 1) ||
544 get_sha1_hex(argv[i+2], sha1) ||
545 add_cacheinfo(mode, sha1, argv[i+3], 0))
546 die("git-update-index: --cacheinfo"
547 " cannot add %s", argv[i+3]);
551 if (!strcmp(path, "--chmod=-x") ||
552 !strcmp(path, "--chmod=+x")) {
554 die("git-update-index: %s <path>", path);
555 set_executable_bit = path[8];
558 if (!strcmp(path, "--assume-unchanged")) {
559 mark_valid_only = MARK_VALID;
562 if (!strcmp(path, "--no-assume-unchanged")) {
563 mark_valid_only = UNMARK_VALID;
566 if (!strcmp(path, "--info-only")) {
570 if (!strcmp(path, "--force-remove")) {
574 if (!strcmp(path, "-z")) {
575 line_termination = 0;
578 if (!strcmp(path, "--stdin")) {
580 die("--stdin must be at the end");
584 if (!strcmp(path, "--index-info")) {
586 die("--index-info must be at the end");
587 allow_add = allow_replace = allow_remove = 1;
588 read_index_info(line_termination);
591 if (!strcmp(path, "--unresolve")) {
592 has_errors = do_unresolve(argc - i, argv + i,
593 prefix, prefix_length);
595 active_cache_changed = 0;
598 if (!strcmp(path, "--again")) {
599 has_errors = do_reupdate(argc - i, argv + i,
600 prefix, prefix_length);
602 active_cache_changed = 0;
605 if (!strcmp(path, "--ignore-missing")) {
606 refresh_flags |= REFRESH_IGNORE_MISSING;
609 if (!strcmp(path, "--verbose")) {
613 if (!strcmp(path, "-h") || !strcmp(path, "--help"))
614 usage(update_index_usage);
615 die("unknown option %s", path);
617 update_one(path, prefix, prefix_length);
618 if (set_executable_bit)
619 chmod_path(set_executable_bit, path);
621 if (read_from_stdin) {
627 read_line(&buf, stdin, line_termination);
630 if (line_termination && buf.buf[0] == '"')
631 path_name = unquote_c_style(buf.buf, NULL);
634 p = prefix_path(prefix, prefix_length, path_name);
635 update_one(p, NULL, 0);
636 if (set_executable_bit)
637 chmod_path(set_executable_bit, p);
638 if (p < path_name || p > path_name + strlen(path_name))
640 if (path_name != buf.buf)
646 if (active_cache_changed) {
647 if (write_cache(newfd, active_cache, active_nr) ||
648 commit_lock_file(&lock_file))
649 die("Unable to write new index file");
652 return has_errors ? 1 : 0;