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
;
22 static int allow_unmerged
; /* --refresh needing merge is not error */
23 static int not_new
; /* --refresh not having working tree files is not error */
24 static int quiet
; /* --refresh needing update is not error */
26 static int force_remove
;
28 static int mark_valid_only
= 0;
30 #define UNMARK_VALID 2
33 /* Three functions to allow overloaded pointer return; see linux/err.h */
34 static inline void *ERR_PTR(long error
)
36 return (void *) error
;
39 static inline long PTR_ERR(const void *ptr
)
44 static inline long IS_ERR(const void *ptr
)
46 return (unsigned long)ptr
> (unsigned long)-1000L;
49 static void report(const char *fmt
, ...)
62 static int mark_valid(const char *path
)
64 int namelen
= strlen(path
);
65 int pos
= cache_name_pos(path
, namelen
);
67 switch (mark_valid_only
) {
69 active_cache
[pos
]->ce_flags
|= htons(CE_VALID
);
72 active_cache
[pos
]->ce_flags
&= ~htons(CE_VALID
);
75 cache_tree_invalidate_path(active_cache_tree
, path
);
76 active_cache_changed
= 1;
82 static int add_file_to_cache(const char *path
)
84 int size
, namelen
, option
, status
;
85 struct cache_entry
*ce
;
88 status
= lstat(path
, &st
);
90 /* We probably want to do this in remove_file_from_cache() and
91 * add_cache_entry() instead...
93 cache_tree_invalidate_path(active_cache_tree
, path
);
95 if (status
< 0 || S_ISDIR(st
.st_mode
)) {
96 /* When we used to have "path" and now we want to add
97 * "path/file", we need a way to remove "path" before
98 * being able to add "path/file". However,
99 * "git-update-index --remove path" would not work.
100 * --force-remove can be used but this is more user
101 * friendly, especially since we can do the opposite
102 * case just fine without --force-remove.
104 if (status
== 0 || (errno
== ENOENT
|| errno
== ENOTDIR
)) {
106 if (remove_file_from_cache(path
))
107 return error("%s: cannot remove from the index",
111 } else if (status
< 0) {
112 return error("%s: does not exist and --remove not passed",
117 return error("%s: is a directory - add files inside instead",
120 return error("lstat(\"%s\"): %s", path
,
124 namelen
= strlen(path
);
125 size
= cache_entry_size(namelen
);
126 ce
= xcalloc(1, size
);
127 memcpy(ce
->name
, path
, namelen
);
128 ce
->ce_flags
= htons(namelen
);
129 fill_stat_cache_info(ce
, &st
);
131 ce
->ce_mode
= create_ce_mode(st
.st_mode
);
132 if (!trust_executable_bit
) {
133 /* If there is an existing entry, pick the mode bits
136 int pos
= cache_name_pos(path
, namelen
);
138 ce
->ce_mode
= active_cache
[pos
]->ce_mode
;
141 if (index_path(ce
->sha1
, path
, &st
, !info_only
))
143 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
144 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
145 if (add_cache_entry(ce
, option
))
146 return error("%s: cannot add to the index - missing --add option?",
152 * "refresh" does not calculate a new sha1 file or bring the
153 * cache up-to-date for mode/content changes. But what it
154 * _does_ do is to "re-match" the stat information of a file
155 * with the cache, so that you can refresh the cache for a
156 * file that hasn't been changed but where the stat entry is
159 * For example, you'd want to do this after doing a "git-read-tree",
160 * to link up the stat cache details with the proper files.
162 static struct cache_entry
*refresh_entry(struct cache_entry
*ce
, int really
)
165 struct cache_entry
*updated
;
168 if (lstat(ce
->name
, &st
) < 0)
169 return ERR_PTR(-errno
);
171 changed
= ce_match_stat(ce
, &st
, really
);
173 if (really
&& assume_unchanged
&&
174 !(ce
->ce_flags
& htons(CE_VALID
)))
175 ; /* mark this one VALID again */
180 if (ce_modified(ce
, &st
, really
))
181 return ERR_PTR(-EINVAL
);
184 updated
= xmalloc(size
);
185 memcpy(updated
, ce
, size
);
186 fill_stat_cache_info(updated
, &st
);
188 /* In this case, if really is not set, we should leave
189 * CE_VALID bit alone. Otherwise, paths marked with
190 * --no-assume-unchanged (i.e. things to be edited) will
191 * reacquire CE_VALID bit automatically, which is not
192 * really what we want.
194 if (!really
&& assume_unchanged
&& !(ce
->ce_flags
& htons(CE_VALID
)))
195 updated
->ce_flags
&= ~htons(CE_VALID
);
200 static int refresh_cache(int really
)
205 for (i
= 0; i
< active_nr
; i
++) {
206 struct cache_entry
*ce
, *new;
207 ce
= active_cache
[i
];
209 while ((i
< active_nr
) &&
210 ! strcmp(active_cache
[i
]->name
, ce
->name
))
215 printf("%s: needs merge\n", ce
->name
);
220 new = refresh_entry(ce
, really
);
224 if (not_new
&& PTR_ERR(new) == -ENOENT
)
226 if (really
&& PTR_ERR(new) == -EINVAL
) {
227 /* If we are doing --really-refresh that
228 * means the index is not valid anymore.
230 ce
->ce_flags
&= ~htons(CE_VALID
);
231 active_cache_changed
= 1;
235 printf("%s: needs update\n", ce
->name
);
239 active_cache_changed
= 1;
240 /* You can NOT just free active_cache[i] here, since it
241 * might not be necessarily malloc()ed but can also come
243 active_cache
[i
] = new;
249 * We fundamentally don't like some paths: we don't want
250 * dot or dot-dot anywhere, and for obvious reasons don't
251 * want to recurse into ".git" either.
253 * Also, we don't want double slashes or slashes at the
254 * end that can make pathnames ambiguous.
256 static int verify_dotfile(const char *rest
)
259 * The first character was '.', but that
260 * has already been discarded, we now test
264 /* "." is not allowed */
269 * ".git" followed by NUL or slash is bad. This
270 * shares the path end test with the ".." case.
280 if (rest
[1] == '\0' || rest
[1] == '/')
286 static int verify_path(const char *path
)
303 if (verify_dotfile(path
))
312 static int add_cacheinfo(unsigned int mode
, const unsigned char *sha1
,
313 const char *path
, int stage
)
315 int size
, len
, option
;
316 struct cache_entry
*ce
;
318 if (!verify_path(path
))
322 size
= cache_entry_size(len
);
323 ce
= xcalloc(1, size
);
325 memcpy(ce
->sha1
, sha1
, 20);
326 memcpy(ce
->name
, path
, len
);
327 ce
->ce_flags
= create_ce_flags(len
, stage
);
328 ce
->ce_mode
= create_ce_mode(mode
);
329 if (assume_unchanged
)
330 ce
->ce_flags
|= htons(CE_VALID
);
331 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
332 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
333 if (add_cache_entry(ce
, option
))
334 return error("%s: cannot add to the index - missing --add option?",
336 report("add '%s'", path
);
337 cache_tree_invalidate_path(active_cache_tree
, path
);
341 static void chmod_path(int flip
, const char *path
)
344 struct cache_entry
*ce
;
347 pos
= cache_name_pos(path
, strlen(path
));
350 ce
= active_cache
[pos
];
351 mode
= ntohl(ce
->ce_mode
);
356 ce
->ce_mode
|= htonl(0111); break;
358 ce
->ce_mode
&= htonl(~0111); break;
362 cache_tree_invalidate_path(active_cache_tree
, path
);
363 active_cache_changed
= 1;
364 report("chmod %cx '%s'", flip
, path
);
367 die("git-update-index: cannot chmod %cx '%s'", flip
, path
);
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 static unsigned char head_sha1
[20];
491 static unsigned char merge_head_sha1
[20];
493 static struct cache_entry
*read_one_ent(const char *which
,
494 unsigned char *ent
, const char *path
,
495 int namelen
, int stage
)
498 unsigned char sha1
[20];
500 struct cache_entry
*ce
;
502 if (get_tree_entry(ent
, path
, sha1
, &mode
)) {
503 error("%s: not in %s branch.", path
, which
);
506 if (mode
== S_IFDIR
) {
507 error("%s: not a blob in %s branch.", path
, which
);
510 size
= cache_entry_size(namelen
);
511 ce
= xcalloc(1, size
);
513 memcpy(ce
->sha1
, sha1
, 20);
514 memcpy(ce
->name
, path
, namelen
);
515 ce
->ce_flags
= create_ce_flags(namelen
, stage
);
516 ce
->ce_mode
= create_ce_mode(mode
);
520 static int unresolve_one(const char *path
)
522 int namelen
= strlen(path
);
525 struct cache_entry
*ce_2
= NULL
, *ce_3
= NULL
;
527 /* See if there is such entry in the index. */
528 pos
= cache_name_pos(path
, namelen
);
530 /* If there isn't, either it is unmerged, or
531 * resolved as "removed" by mistake. We do not
532 * want to do anything in the former case.
535 if (pos
< active_nr
) {
536 struct cache_entry
*ce
= active_cache
[pos
];
537 if (ce_namelen(ce
) == namelen
&&
538 !memcmp(ce
->name
, path
, namelen
)) {
540 "%s: skipping still unmerged path.\n",
547 /* Grab blobs from given path from HEAD and MERGE_HEAD,
548 * stuff HEAD version in stage #2,
549 * stuff MERGE_HEAD version in stage #3.
551 ce_2
= read_one_ent("our", head_sha1
, path
, namelen
, 2);
552 ce_3
= read_one_ent("their", merge_head_sha1
, path
, namelen
, 3);
554 if (!ce_2
|| !ce_3
) {
558 if (!memcmp(ce_2
->sha1
, ce_3
->sha1
, 20) &&
559 ce_2
->ce_mode
== ce_3
->ce_mode
) {
560 fprintf(stderr
, "%s: identical in both, skipping.\n",
565 cache_tree_invalidate_path(active_cache_tree
, path
);
566 remove_file_from_cache(path
);
567 if (add_cache_entry(ce_2
, ADD_CACHE_OK_TO_ADD
)) {
568 error("%s: cannot add our version to the index.", path
);
572 if (!add_cache_entry(ce_3
, ADD_CACHE_OK_TO_ADD
))
574 error("%s: cannot add their version to the index.", path
);
582 static void read_head_pointers(void)
584 if (read_ref(git_path("HEAD"), head_sha1
))
585 die("No HEAD -- no initial commit yet?\n");
586 if (read_ref(git_path("MERGE_HEAD"), merge_head_sha1
)) {
587 fprintf(stderr
, "Not in the middle of a merge.\n");
592 static int do_unresolve(int ac
, const char **av
)
597 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
598 * are not doing a merge, so exit with success status.
600 read_head_pointers();
602 for (i
= 1; i
< ac
; i
++) {
603 const char *arg
= av
[i
];
604 err
|= unresolve_one(arg
);
609 int main(int argc
, const char **argv
)
611 int i
, newfd
, entries
, has_errors
= 0, line_termination
= '\n';
612 int allow_options
= 1;
613 int read_from_stdin
= 0;
614 const char *prefix
= setup_git_directory();
615 int prefix_length
= prefix
? strlen(prefix
) : 0;
616 char set_executable_bit
= 0;
618 git_config(git_default_config
);
620 newfd
= hold_index_file_for_update(&cache_file
, get_index_file());
622 die("unable to create new cachefile");
624 entries
= read_cache();
626 die("cache corrupted");
628 for (i
= 1 ; i
< argc
; i
++) {
629 const char *path
= argv
[i
];
631 if (allow_options
&& *path
== '-') {
632 if (!strcmp(path
, "--")) {
636 if (!strcmp(path
, "-q")) {
640 if (!strcmp(path
, "--add")) {
644 if (!strcmp(path
, "--replace")) {
648 if (!strcmp(path
, "--remove")) {
652 if (!strcmp(path
, "--unmerged")) {
656 if (!strcmp(path
, "--refresh")) {
657 has_errors
|= refresh_cache(0);
660 if (!strcmp(path
, "--really-refresh")) {
661 has_errors
|= refresh_cache(1);
664 if (!strcmp(path
, "--cacheinfo")) {
665 unsigned char sha1
[20];
669 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
671 if ((sscanf(argv
[i
+1], "%o", &mode
) != 1) ||
672 get_sha1_hex(argv
[i
+2], sha1
) ||
673 add_cacheinfo(mode
, sha1
, argv
[i
+3], 0))
674 die("git-update-index: --cacheinfo"
675 " cannot add %s", argv
[i
+3]);
679 if (!strcmp(path
, "--chmod=-x") ||
680 !strcmp(path
, "--chmod=+x")) {
682 die("git-update-index: %s <path>", path
);
683 set_executable_bit
= path
[8];
686 if (!strcmp(path
, "--assume-unchanged")) {
687 mark_valid_only
= MARK_VALID
;
690 if (!strcmp(path
, "--no-assume-unchanged")) {
691 mark_valid_only
= UNMARK_VALID
;
694 if (!strcmp(path
, "--info-only")) {
698 if (!strcmp(path
, "--force-remove")) {
702 if (!strcmp(path
, "-z")) {
703 line_termination
= 0;
706 if (!strcmp(path
, "--stdin")) {
708 die("--stdin must be at the end");
712 if (!strcmp(path
, "--index-info")) {
714 die("--index-info must be at the end");
715 allow_add
= allow_replace
= allow_remove
= 1;
716 read_index_info(line_termination
);
719 if (!strcmp(path
, "--unresolve")) {
720 has_errors
= do_unresolve(argc
- i
, argv
+ i
);
722 active_cache_changed
= 0;
725 if (!strcmp(path
, "--ignore-missing")) {
729 if (!strcmp(path
, "--verbose")) {
733 if (!strcmp(path
, "-h") || !strcmp(path
, "--help"))
734 usage(update_index_usage
);
735 die("unknown option %s", path
);
737 update_one(path
, prefix
, prefix_length
);
738 if (set_executable_bit
)
739 chmod_path(set_executable_bit
, path
);
741 if (read_from_stdin
) {
746 read_line(&buf
, stdin
, line_termination
);
749 if (line_termination
&& buf
.buf
[0] == '"')
750 path_name
= unquote_c_style(buf
.buf
, NULL
);
753 update_one(path_name
, prefix
, prefix_length
);
754 if (set_executable_bit
) {
755 const char *p
= prefix_path(prefix
, prefix_length
, path_name
);
756 chmod_path(set_executable_bit
, p
);
758 if (path_name
!= buf
.buf
)
764 if (active_cache_changed
) {
765 if (write_cache(newfd
, active_cache
, active_nr
) ||
766 commit_index_file(&cache_file
))
767 die("Unable to write new cachefile");
770 return has_errors
? 1 : 0;