2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
8 #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
;
24 static int force_remove
;
26 static int mark_valid_only
;
27 static int mark_skip_worktree_only
;
31 __attribute__((format (printf
, 1, 2)))
32 static void report(const char *fmt
, ...)
45 static int mark_ce_flags(const char *path
, int flag
, int mark
)
47 int namelen
= strlen(path
);
48 int pos
= cache_name_pos(path
, namelen
);
51 active_cache
[pos
]->ce_flags
|= flag
;
53 active_cache
[pos
]->ce_flags
&= ~flag
;
54 cache_tree_invalidate_path(active_cache_tree
, path
);
55 active_cache_changed
= 1;
61 static int remove_one_path(const char *path
)
64 return error("%s: does not exist and --remove not passed", path
);
65 if (remove_file_from_cache(path
))
66 return error("%s: cannot remove from the index", path
);
71 * Handle a path that couldn't be lstat'ed. It's either:
72 * - missing file (ENOENT or ENOTDIR). That's ok if we're
73 * supposed to be removing it and the removal actually
75 * - permission error. That's never ok.
77 static int process_lstat_error(const char *path
, int err
)
79 if (err
== ENOENT
|| err
== ENOTDIR
)
80 return remove_one_path(path
);
81 return error("lstat(\"%s\"): %s", path
, strerror(errno
));
84 static int add_one_path(struct cache_entry
*old
, const char *path
, int len
, struct stat
*st
)
87 struct cache_entry
*ce
;
89 /* Was the old index entry already up-to-date? */
90 if (old
&& !ce_stage(old
) && !ce_match_stat(old
, st
, 0))
93 size
= cache_entry_size(len
);
94 ce
= xcalloc(1, size
);
95 memcpy(ce
->name
, path
, len
);
97 fill_stat_cache_info(ce
, st
);
98 ce
->ce_mode
= ce_mode_from_stat(old
, st
->st_mode
);
100 if (index_path(ce
->sha1
, path
, st
, !info_only
))
102 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
103 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
104 if (add_cache_entry(ce
, option
))
105 return error("%s: cannot add to the index - missing --add option?", path
);
110 * Handle a path that was a directory. Four cases:
112 * - it's already a gitlink in the index, and we keep it that
113 * way, and update it if we can (if we cannot find the HEAD,
114 * we're going to keep it unchanged in the index!)
116 * - it's a *file* in the index, in which case it should be
117 * removed as a file if removal is allowed, since it doesn't
118 * exist as such any more. If removal isn't allowed, it's
121 * (NOTE! This is old and arguably fairly strange behaviour.
122 * We might want to make this an error unconditionally, and
123 * use "--force-remove" if you actually want to force removal).
125 * - it used to exist as a subdirectory (ie multiple files with
126 * this particular prefix) in the index, in which case it's wrong
127 * to try to update it as a directory.
129 * - it doesn't exist at all in the index, but it is a valid
130 * git directory, and it should be *added* as a gitlink.
132 static int process_directory(const char *path
, int len
, struct stat
*st
)
134 unsigned char sha1
[20];
135 int pos
= cache_name_pos(path
, len
);
137 /* Exact match: file or existing gitlink */
139 struct cache_entry
*ce
= active_cache
[pos
];
140 if (S_ISGITLINK(ce
->ce_mode
)) {
142 /* Do nothing to the index if there is no HEAD! */
143 if (resolve_gitlink_ref(path
, "HEAD", sha1
) < 0)
146 return add_one_path(ce
, path
, len
, st
);
148 /* Should this be an unconditional error? */
149 return remove_one_path(path
);
152 /* Inexact match: is there perhaps a subdirectory match? */
154 while (pos
< active_nr
) {
155 struct cache_entry
*ce
= active_cache
[pos
++];
157 if (strncmp(ce
->name
, path
, len
))
159 if (ce
->name
[len
] > '/')
161 if (ce
->name
[len
] < '/')
164 /* Subdirectory match - error out */
165 return error("%s: is a directory - add individual files instead", path
);
168 /* No match - should we add it as a gitlink? */
169 if (!resolve_gitlink_ref(path
, "HEAD", sha1
))
170 return add_one_path(NULL
, path
, len
, st
);
173 return error("%s: is a directory - add files inside instead", path
);
176 static int process_path(const char *path
)
180 struct cache_entry
*ce
;
183 if (has_symlink_leading_path(path
, len
))
184 return error("'%s' is beyond a symbolic link", path
);
186 pos
= cache_name_pos(path
, len
);
187 ce
= pos
< 0 ? NULL
: active_cache
[pos
];
188 if (ce
&& ce_skip_worktree(ce
)) {
190 * working directory version is assumed "good"
191 * so updating it does not make sense.
192 * On the other hand, removing it from index should work
194 if (allow_remove
&& remove_file_from_cache(path
))
195 return error("%s: cannot remove from the index", path
);
200 * First things first: get the stat information, to decide
201 * what to do about the pathname!
203 if (lstat(path
, &st
) < 0)
204 return process_lstat_error(path
, errno
);
206 if (S_ISDIR(st
.st_mode
))
207 return process_directory(path
, len
, &st
);
210 * Process a regular file
212 if (ce
&& S_ISGITLINK(ce
->ce_mode
))
213 return error("%s is already a gitlink, not replacing", path
);
215 return add_one_path(ce
, path
, len
, &st
);
218 static int add_cacheinfo(unsigned int mode
, const unsigned char *sha1
,
219 const char *path
, int stage
)
221 int size
, len
, option
;
222 struct cache_entry
*ce
;
224 if (!verify_path(path
))
225 return error("Invalid path '%s'", path
);
228 size
= cache_entry_size(len
);
229 ce
= xcalloc(1, size
);
231 hashcpy(ce
->sha1
, sha1
);
232 memcpy(ce
->name
, path
, len
);
233 ce
->ce_flags
= create_ce_flags(len
, stage
);
234 ce
->ce_mode
= create_ce_mode(mode
);
235 if (assume_unchanged
)
236 ce
->ce_flags
|= CE_VALID
;
237 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
238 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
239 if (add_cache_entry(ce
, option
))
240 return error("%s: cannot add to the index - missing --add option?",
242 report("add '%s'", path
);
246 static void chmod_path(int flip
, const char *path
)
249 struct cache_entry
*ce
;
252 pos
= cache_name_pos(path
, strlen(path
));
255 ce
= active_cache
[pos
];
261 ce
->ce_mode
|= 0111; break;
263 ce
->ce_mode
&= ~0111; break;
267 cache_tree_invalidate_path(active_cache_tree
, path
);
268 active_cache_changed
= 1;
269 report("chmod %cx '%s'", flip
, path
);
272 die("git update-index: cannot chmod %cx '%s'", flip
, path
);
275 static void update_one(const char *path
, const char *prefix
, int prefix_length
)
277 const char *p
= prefix_path(prefix
, prefix_length
, path
);
278 if (!verify_path(p
)) {
279 fprintf(stderr
, "Ignoring path %s\n", path
);
282 if (mark_valid_only
) {
283 if (mark_ce_flags(p
, CE_VALID
, mark_valid_only
== MARK_FLAG
))
284 die("Unable to mark file %s", path
);
287 if (mark_skip_worktree_only
) {
288 if (mark_ce_flags(p
, CE_SKIP_WORKTREE
, mark_skip_worktree_only
== MARK_FLAG
))
289 die("Unable to mark file %s", path
);
294 if (remove_file_from_cache(p
))
295 die("git update-index: unable to remove %s", path
);
296 report("remove '%s'", path
);
300 die("Unable to process path %s", path
);
301 report("add '%s'", path
);
303 if (p
< path
|| p
> path
+ strlen(path
))
307 static void read_index_info(int line_termination
)
309 struct strbuf buf
= STRBUF_INIT
;
310 struct strbuf uq
= STRBUF_INIT
;
312 while (strbuf_getline(&buf
, stdin
, line_termination
) != EOF
) {
315 unsigned char sha1
[20];
320 /* This reads lines formatted in one of three formats:
322 * (1) mode SP sha1 TAB path
323 * The first format is what "git apply --index-info"
324 * reports, and used to reconstruct a partial tree
325 * that is used for phony merge base tree when falling
326 * back on 3-way merge.
328 * (2) mode SP type SP sha1 TAB path
329 * The second format is to stuff "git ls-tree" output
330 * into the index file.
332 * (3) mode SP sha1 SP stage TAB path
333 * This format is to put higher order stages into the
334 * index file and matches "git ls-files --stage" output.
337 ul
= strtoul(buf
.buf
, &ptr
, 8);
338 if (ptr
== buf
.buf
|| *ptr
!= ' '
339 || errno
|| (unsigned int) ul
!= ul
)
343 tab
= strchr(ptr
, '\t');
344 if (!tab
|| tab
- ptr
< 41)
347 if (tab
[-2] == ' ' && '0' <= tab
[-1] && tab
[-1] <= '3') {
348 stage
= tab
[-1] - '0';
349 ptr
= tab
+ 1; /* point at the head of path */
350 tab
= tab
- 2; /* point at tail of sha1 */
354 ptr
= tab
+ 1; /* point at the head of path */
357 if (get_sha1_hex(tab
- 40, sha1
) || tab
[-41] != ' ')
361 if (line_termination
&& path_name
[0] == '"') {
363 if (unquote_c_style(&uq
, path_name
, NULL
)) {
364 die("git update-index: bad quoting of path name");
369 if (!verify_path(path_name
)) {
370 fprintf(stderr
, "Ignoring path %s\n", path_name
);
375 /* mode == 0 means there is no such path -- remove */
376 if (remove_file_from_cache(path_name
))
377 die("git update-index: unable to remove %s",
381 /* mode ' ' sha1 '\t' name
382 * ptr[-1] points at tab,
383 * ptr[-41] is at the beginning of sha1
385 ptr
[-42] = ptr
[-1] = 0;
386 if (add_cacheinfo(mode
, sha1
, path_name
, stage
))
387 die("git update-index: unable to update %s",
393 die("malformed index info %s", buf
.buf
);
395 strbuf_release(&buf
);
399 static const char update_index_usage
[] =
400 "git update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--really-refresh] [--cacheinfo] [--chmod=(+|-)x] [--assume-unchanged] [--skip-worktree|--no-skip-worktree] [--info-only] [--force-remove] [--stdin] [--index-info] [--unresolve] [--again | -g] [--ignore-missing] [-z] [--verbose] [--] <file>...";
402 static unsigned char head_sha1
[20];
403 static unsigned char merge_head_sha1
[20];
405 static struct cache_entry
*read_one_ent(const char *which
,
406 unsigned char *ent
, const char *path
,
407 int namelen
, int stage
)
410 unsigned char sha1
[20];
412 struct cache_entry
*ce
;
414 if (get_tree_entry(ent
, path
, sha1
, &mode
)) {
416 error("%s: not in %s branch.", path
, which
);
419 if (mode
== S_IFDIR
) {
421 error("%s: not a blob in %s branch.", path
, which
);
424 size
= cache_entry_size(namelen
);
425 ce
= xcalloc(1, size
);
427 hashcpy(ce
->sha1
, sha1
);
428 memcpy(ce
->name
, path
, namelen
);
429 ce
->ce_flags
= create_ce_flags(namelen
, stage
);
430 ce
->ce_mode
= create_ce_mode(mode
);
434 static int unresolve_one(const char *path
)
436 int namelen
= strlen(path
);
439 struct cache_entry
*ce_2
= NULL
, *ce_3
= NULL
;
441 /* See if there is such entry in the index. */
442 pos
= cache_name_pos(path
, namelen
);
444 /* If there isn't, either it is unmerged, or
445 * resolved as "removed" by mistake. We do not
446 * want to do anything in the former case.
449 if (pos
< active_nr
) {
450 struct cache_entry
*ce
= active_cache
[pos
];
451 if (ce_namelen(ce
) == namelen
&&
452 !memcmp(ce
->name
, path
, namelen
)) {
454 "%s: skipping still unmerged path.\n",
461 /* Grab blobs from given path from HEAD and MERGE_HEAD,
462 * stuff HEAD version in stage #2,
463 * stuff MERGE_HEAD version in stage #3.
465 ce_2
= read_one_ent("our", head_sha1
, path
, namelen
, 2);
466 ce_3
= read_one_ent("their", merge_head_sha1
, path
, namelen
, 3);
468 if (!ce_2
|| !ce_3
) {
472 if (!hashcmp(ce_2
->sha1
, ce_3
->sha1
) &&
473 ce_2
->ce_mode
== ce_3
->ce_mode
) {
474 fprintf(stderr
, "%s: identical in both, skipping.\n",
479 remove_file_from_cache(path
);
480 if (add_cache_entry(ce_2
, ADD_CACHE_OK_TO_ADD
)) {
481 error("%s: cannot add our version to the index.", path
);
485 if (!add_cache_entry(ce_3
, ADD_CACHE_OK_TO_ADD
))
487 error("%s: cannot add their version to the index.", path
);
495 static void read_head_pointers(void)
497 if (read_ref("HEAD", head_sha1
))
498 die("No HEAD -- no initial commit yet?");
499 if (read_ref("MERGE_HEAD", merge_head_sha1
)) {
500 fprintf(stderr
, "Not in the middle of a merge.\n");
505 static int do_unresolve(int ac
, const char **av
,
506 const char *prefix
, int prefix_length
)
511 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
512 * are not doing a merge, so exit with success status.
514 read_head_pointers();
516 for (i
= 1; i
< ac
; i
++) {
517 const char *arg
= av
[i
];
518 const char *p
= prefix_path(prefix
, prefix_length
, arg
);
519 err
|= unresolve_one(p
);
520 if (p
< arg
|| p
> arg
+ strlen(arg
))
526 static int do_reupdate(int ac
, const char **av
,
527 const char *prefix
, int prefix_length
)
529 /* Read HEAD and run update-index on paths that are
530 * merged and already different between index and HEAD.
534 const char **pathspec
= get_pathspec(prefix
, av
+ 1);
536 if (read_ref("HEAD", head_sha1
))
537 /* If there is no HEAD, that means it is an initial
538 * commit. Update everything in the index.
542 for (pos
= 0; pos
< active_nr
; pos
++) {
543 struct cache_entry
*ce
= active_cache
[pos
];
544 struct cache_entry
*old
= NULL
;
547 if (ce_stage(ce
) || !ce_path_match(ce
, pathspec
))
550 old
= read_one_ent(NULL
, head_sha1
,
551 ce
->name
, ce_namelen(ce
), 0);
552 if (old
&& ce
->ce_mode
== old
->ce_mode
&&
553 !hashcmp(ce
->sha1
, old
->sha1
)) {
555 continue; /* unchanged */
557 /* Be careful. The working tree may not have the
558 * path anymore, in which case, under 'allow_remove',
559 * or worse yet 'allow_replace', active_nr may decrease.
562 update_one(ce
->name
+ prefix_length
, prefix
, prefix_length
);
563 if (save_nr
!= active_nr
)
569 int cmd_update_index(int argc
, const char **argv
, const char *prefix
)
571 int i
, newfd
, entries
, has_errors
= 0, line_termination
= '\n';
572 int allow_options
= 1;
573 int read_from_stdin
= 0;
574 int prefix_length
= prefix
? strlen(prefix
) : 0;
575 char set_executable_bit
= 0;
576 unsigned int refresh_flags
= 0;
578 struct lock_file
*lock_file
;
580 git_config(git_default_config
, NULL
);
582 /* We can't free this memory, it becomes part of a linked list parsed atexit() */
583 lock_file
= xcalloc(1, sizeof(struct lock_file
));
585 newfd
= hold_locked_index(lock_file
, 0);
589 entries
= read_cache();
591 die("cache corrupted");
593 for (i
= 1 ; i
< argc
; i
++) {
594 const char *path
= argv
[i
];
597 if (allow_options
&& *path
== '-') {
598 if (!strcmp(path
, "--")) {
602 if (!strcmp(path
, "-q")) {
603 refresh_flags
|= REFRESH_QUIET
;
606 if (!strcmp(path
, "--ignore-submodules")) {
607 refresh_flags
|= REFRESH_IGNORE_SUBMODULES
;
610 if (!strcmp(path
, "--add")) {
614 if (!strcmp(path
, "--replace")) {
618 if (!strcmp(path
, "--remove")) {
622 if (!strcmp(path
, "--unmerged")) {
623 refresh_flags
|= REFRESH_UNMERGED
;
626 if (!strcmp(path
, "--refresh")) {
628 has_errors
|= refresh_cache(refresh_flags
);
631 if (!strcmp(path
, "--really-refresh")) {
633 has_errors
|= refresh_cache(REFRESH_REALLY
| refresh_flags
);
636 if (!strcmp(path
, "--cacheinfo")) {
637 unsigned char sha1
[20];
641 die("git update-index: --cacheinfo <mode> <sha1> <path>");
643 if (strtoul_ui(argv
[i
+1], 8, &mode
) ||
644 get_sha1_hex(argv
[i
+2], sha1
) ||
645 add_cacheinfo(mode
, sha1
, argv
[i
+3], 0))
646 die("git update-index: --cacheinfo"
647 " cannot add %s", argv
[i
+3]);
651 if (!strcmp(path
, "--chmod=-x") ||
652 !strcmp(path
, "--chmod=+x")) {
654 die("git update-index: %s <path>", path
);
655 set_executable_bit
= path
[8];
658 if (!strcmp(path
, "--assume-unchanged")) {
659 mark_valid_only
= MARK_FLAG
;
662 if (!strcmp(path
, "--no-assume-unchanged")) {
663 mark_valid_only
= UNMARK_FLAG
;
666 if (!strcmp(path
, "--no-skip-worktree")) {
667 mark_skip_worktree_only
= UNMARK_FLAG
;
670 if (!strcmp(path
, "--skip-worktree")) {
671 mark_skip_worktree_only
= MARK_FLAG
;
674 if (!strcmp(path
, "--info-only")) {
678 if (!strcmp(path
, "--force-remove")) {
682 if (!strcmp(path
, "-z")) {
683 line_termination
= 0;
686 if (!strcmp(path
, "--stdin")) {
688 die("--stdin must be at the end");
692 if (!strcmp(path
, "--index-info")) {
694 die("--index-info must be at the end");
695 allow_add
= allow_replace
= allow_remove
= 1;
696 read_index_info(line_termination
);
699 if (!strcmp(path
, "--unresolve")) {
700 has_errors
= do_unresolve(argc
- i
, argv
+ i
,
701 prefix
, prefix_length
);
703 active_cache_changed
= 0;
706 if (!strcmp(path
, "--again") || !strcmp(path
, "-g")) {
708 has_errors
= do_reupdate(argc
- i
, argv
+ i
,
709 prefix
, prefix_length
);
711 active_cache_changed
= 0;
714 if (!strcmp(path
, "--ignore-missing")) {
715 refresh_flags
|= REFRESH_IGNORE_MISSING
;
718 if (!strcmp(path
, "--verbose")) {
722 if (!strcmp(path
, "-h") || !strcmp(path
, "--help"))
723 usage(update_index_usage
);
724 die("unknown option %s", path
);
727 p
= prefix_path(prefix
, prefix_length
, path
);
728 update_one(p
, NULL
, 0);
729 if (set_executable_bit
)
730 chmod_path(set_executable_bit
, p
);
731 if (p
< path
|| p
> path
+ strlen(path
))
734 if (read_from_stdin
) {
735 struct strbuf buf
= STRBUF_INIT
, nbuf
= STRBUF_INIT
;
738 while (strbuf_getline(&buf
, stdin
, line_termination
) != EOF
) {
740 if (line_termination
&& buf
.buf
[0] == '"') {
742 if (unquote_c_style(&nbuf
, buf
.buf
, NULL
))
743 die("line is badly quoted");
744 strbuf_swap(&buf
, &nbuf
);
746 p
= prefix_path(prefix
, prefix_length
, buf
.buf
);
747 update_one(p
, NULL
, 0);
748 if (set_executable_bit
)
749 chmod_path(set_executable_bit
, p
);
750 if (p
< buf
.buf
|| p
> buf
.buf
+ buf
.len
)
753 strbuf_release(&nbuf
);
754 strbuf_release(&buf
);
758 if (active_cache_changed
) {
760 if (refresh_flags
& REFRESH_QUIET
)
762 unable_to_lock_index_die(get_index_file(), lock_error
);
764 if (write_cache(newfd
, active_cache
, active_nr
) ||
765 commit_locked_index(lock_file
))
766 die("Unable to write new index file");
769 rollback_lock_file(lock_file
);
771 return has_errors
? 1 : 0;