2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
8 #include "cache-tree.h"
12 #include "resolve-undo.h"
15 * Default to not allowing changes to the list of files. The
16 * tool doesn't actually care, but this makes it harder to add
17 * files to the revision control by mistake by doing something
18 * like "git update-index *" and suddenly having all the object
19 * files be revision controlled.
22 static int allow_remove
;
23 static int allow_replace
;
25 static int force_remove
;
27 static int mark_valid_only
;
28 static int mark_skip_worktree_only
;
32 __attribute__((format (printf
, 1, 2)))
33 static void report(const char *fmt
, ...)
46 static int mark_ce_flags(const char *path
, int flag
, int mark
)
48 int namelen
= strlen(path
);
49 int pos
= cache_name_pos(path
, namelen
);
52 active_cache
[pos
]->ce_flags
|= flag
;
54 active_cache
[pos
]->ce_flags
&= ~flag
;
55 cache_tree_invalidate_path(active_cache_tree
, path
);
56 active_cache_changed
= 1;
62 static int remove_one_path(const char *path
)
65 return error("%s: does not exist and --remove not passed", path
);
66 if (remove_file_from_cache(path
))
67 return error("%s: cannot remove from the index", path
);
72 * Handle a path that couldn't be lstat'ed. It's either:
73 * - missing file (ENOENT or ENOTDIR). That's ok if we're
74 * supposed to be removing it and the removal actually
76 * - permission error. That's never ok.
78 static int process_lstat_error(const char *path
, int err
)
80 if (err
== ENOENT
|| err
== ENOTDIR
)
81 return remove_one_path(path
);
82 return error("lstat(\"%s\"): %s", path
, strerror(errno
));
85 static int add_one_path(struct cache_entry
*old
, const char *path
, int len
, struct stat
*st
)
88 struct cache_entry
*ce
;
90 /* Was the old index entry already up-to-date? */
91 if (old
&& !ce_stage(old
) && !ce_match_stat(old
, st
, 0))
94 size
= cache_entry_size(len
);
95 ce
= xcalloc(1, size
);
96 memcpy(ce
->name
, path
, len
);
98 fill_stat_cache_info(ce
, st
);
99 ce
->ce_mode
= ce_mode_from_stat(old
, st
->st_mode
);
101 if (index_path(ce
->sha1
, path
, st
, !info_only
))
103 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
104 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
105 if (add_cache_entry(ce
, option
))
106 return error("%s: cannot add to the index - missing --add option?", path
);
111 * Handle a path that was a directory. Four cases:
113 * - it's already a gitlink in the index, and we keep it that
114 * way, and update it if we can (if we cannot find the HEAD,
115 * we're going to keep it unchanged in the index!)
117 * - it's a *file* in the index, in which case it should be
118 * removed as a file if removal is allowed, since it doesn't
119 * exist as such any more. If removal isn't allowed, it's
122 * (NOTE! This is old and arguably fairly strange behaviour.
123 * We might want to make this an error unconditionally, and
124 * use "--force-remove" if you actually want to force removal).
126 * - it used to exist as a subdirectory (ie multiple files with
127 * this particular prefix) in the index, in which case it's wrong
128 * to try to update it as a directory.
130 * - it doesn't exist at all in the index, but it is a valid
131 * git directory, and it should be *added* as a gitlink.
133 static int process_directory(const char *path
, int len
, struct stat
*st
)
135 unsigned char sha1
[20];
136 int pos
= cache_name_pos(path
, len
);
138 /* Exact match: file or existing gitlink */
140 struct cache_entry
*ce
= active_cache
[pos
];
141 if (S_ISGITLINK(ce
->ce_mode
)) {
143 /* Do nothing to the index if there is no HEAD! */
144 if (resolve_gitlink_ref(path
, "HEAD", sha1
) < 0)
147 return add_one_path(ce
, path
, len
, st
);
149 /* Should this be an unconditional error? */
150 return remove_one_path(path
);
153 /* Inexact match: is there perhaps a subdirectory match? */
155 while (pos
< active_nr
) {
156 struct cache_entry
*ce
= active_cache
[pos
++];
158 if (strncmp(ce
->name
, path
, len
))
160 if (ce
->name
[len
] > '/')
162 if (ce
->name
[len
] < '/')
165 /* Subdirectory match - error out */
166 return error("%s: is a directory - add individual files instead", path
);
169 /* No match - should we add it as a gitlink? */
170 if (!resolve_gitlink_ref(path
, "HEAD", sha1
))
171 return add_one_path(NULL
, path
, len
, st
);
174 return error("%s: is a directory - add files inside instead", path
);
177 static int process_path(const char *path
)
181 struct cache_entry
*ce
;
184 if (has_symlink_leading_path(path
, len
))
185 return error("'%s' is beyond a symbolic link", path
);
187 pos
= cache_name_pos(path
, len
);
188 ce
= pos
< 0 ? NULL
: active_cache
[pos
];
189 if (ce
&& ce_skip_worktree(ce
)) {
191 * working directory version is assumed "good"
192 * so updating it does not make sense.
193 * On the other hand, removing it from index should work
195 if (allow_remove
&& remove_file_from_cache(path
))
196 return error("%s: cannot remove from the index", path
);
201 * First things first: get the stat information, to decide
202 * what to do about the pathname!
204 if (lstat(path
, &st
) < 0)
205 return process_lstat_error(path
, errno
);
207 if (S_ISDIR(st
.st_mode
))
208 return process_directory(path
, len
, &st
);
211 * Process a regular file
213 if (ce
&& S_ISGITLINK(ce
->ce_mode
))
214 return error("%s is already a gitlink, not replacing", path
);
216 return add_one_path(ce
, path
, len
, &st
);
219 static int add_cacheinfo(unsigned int mode
, const unsigned char *sha1
,
220 const char *path
, int stage
)
222 int size
, len
, option
;
223 struct cache_entry
*ce
;
225 if (!verify_path(path
))
226 return error("Invalid path '%s'", path
);
229 size
= cache_entry_size(len
);
230 ce
= xcalloc(1, size
);
232 hashcpy(ce
->sha1
, sha1
);
233 memcpy(ce
->name
, path
, len
);
234 ce
->ce_flags
= create_ce_flags(len
, stage
);
235 ce
->ce_mode
= create_ce_mode(mode
);
236 if (assume_unchanged
)
237 ce
->ce_flags
|= CE_VALID
;
238 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
239 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
240 if (add_cache_entry(ce
, option
))
241 return error("%s: cannot add to the index - missing --add option?",
243 report("add '%s'", path
);
247 static void chmod_path(int flip
, const char *path
)
250 struct cache_entry
*ce
;
253 pos
= cache_name_pos(path
, strlen(path
));
256 ce
= active_cache
[pos
];
262 ce
->ce_mode
|= 0111; break;
264 ce
->ce_mode
&= ~0111; break;
268 cache_tree_invalidate_path(active_cache_tree
, path
);
269 active_cache_changed
= 1;
270 report("chmod %cx '%s'", flip
, path
);
273 die("git update-index: cannot chmod %cx '%s'", flip
, path
);
276 static void update_one(const char *path
, const char *prefix
, int prefix_length
)
278 const char *p
= prefix_path(prefix
, prefix_length
, path
);
279 if (!verify_path(p
)) {
280 fprintf(stderr
, "Ignoring path %s\n", path
);
283 if (mark_valid_only
) {
284 if (mark_ce_flags(p
, CE_VALID
, mark_valid_only
== MARK_FLAG
))
285 die("Unable to mark file %s", path
);
288 if (mark_skip_worktree_only
) {
289 if (mark_ce_flags(p
, CE_SKIP_WORKTREE
, mark_skip_worktree_only
== MARK_FLAG
))
290 die("Unable to mark file %s", path
);
295 if (remove_file_from_cache(p
))
296 die("git update-index: unable to remove %s", path
);
297 report("remove '%s'", path
);
301 die("Unable to process path %s", path
);
302 report("add '%s'", path
);
304 if (p
< path
|| p
> path
+ strlen(path
))
308 static void read_index_info(int line_termination
)
310 struct strbuf buf
= STRBUF_INIT
;
311 struct strbuf uq
= STRBUF_INIT
;
313 while (strbuf_getline(&buf
, stdin
, line_termination
) != EOF
) {
316 unsigned char sha1
[20];
321 /* This reads lines formatted in one of three formats:
323 * (1) mode SP sha1 TAB path
324 * The first format is what "git apply --index-info"
325 * reports, and used to reconstruct a partial tree
326 * that is used for phony merge base tree when falling
327 * back on 3-way merge.
329 * (2) mode SP type SP sha1 TAB path
330 * The second format is to stuff "git ls-tree" output
331 * into the index file.
333 * (3) mode SP sha1 SP stage TAB path
334 * This format is to put higher order stages into the
335 * index file and matches "git ls-files --stage" output.
338 ul
= strtoul(buf
.buf
, &ptr
, 8);
339 if (ptr
== buf
.buf
|| *ptr
!= ' '
340 || errno
|| (unsigned int) ul
!= ul
)
344 tab
= strchr(ptr
, '\t');
345 if (!tab
|| tab
- ptr
< 41)
348 if (tab
[-2] == ' ' && '0' <= tab
[-1] && tab
[-1] <= '3') {
349 stage
= tab
[-1] - '0';
350 ptr
= tab
+ 1; /* point at the head of path */
351 tab
= tab
- 2; /* point at tail of sha1 */
355 ptr
= tab
+ 1; /* point at the head of path */
358 if (get_sha1_hex(tab
- 40, sha1
) || tab
[-41] != ' ')
362 if (line_termination
&& path_name
[0] == '"') {
364 if (unquote_c_style(&uq
, path_name
, NULL
)) {
365 die("git update-index: bad quoting of path name");
370 if (!verify_path(path_name
)) {
371 fprintf(stderr
, "Ignoring path %s\n", path_name
);
376 /* mode == 0 means there is no such path -- remove */
377 if (remove_file_from_cache(path_name
))
378 die("git update-index: unable to remove %s",
382 /* mode ' ' sha1 '\t' name
383 * ptr[-1] points at tab,
384 * ptr[-41] is at the beginning of sha1
386 ptr
[-42] = ptr
[-1] = 0;
387 if (add_cacheinfo(mode
, sha1
, path_name
, stage
))
388 die("git update-index: unable to update %s",
394 die("malformed index info %s", buf
.buf
);
396 strbuf_release(&buf
);
400 static const char update_index_usage
[] =
401 "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>...";
403 static unsigned char head_sha1
[20];
404 static unsigned char merge_head_sha1
[20];
406 static struct cache_entry
*read_one_ent(const char *which
,
407 unsigned char *ent
, const char *path
,
408 int namelen
, int stage
)
411 unsigned char sha1
[20];
413 struct cache_entry
*ce
;
415 if (get_tree_entry(ent
, path
, sha1
, &mode
)) {
417 error("%s: not in %s branch.", path
, which
);
420 if (mode
== S_IFDIR
) {
422 error("%s: not a blob in %s branch.", path
, which
);
425 size
= cache_entry_size(namelen
);
426 ce
= xcalloc(1, size
);
428 hashcpy(ce
->sha1
, sha1
);
429 memcpy(ce
->name
, path
, namelen
);
430 ce
->ce_flags
= create_ce_flags(namelen
, stage
);
431 ce
->ce_mode
= create_ce_mode(mode
);
435 static int unresolve_one(const char *path
)
437 int namelen
= strlen(path
);
440 struct cache_entry
*ce_2
= NULL
, *ce_3
= NULL
;
442 /* See if there is such entry in the index. */
443 pos
= cache_name_pos(path
, namelen
);
446 pos
= unmerge_cache_entry_at(pos
);
447 if (pos
< active_nr
) {
448 struct cache_entry
*ce
= active_cache
[pos
];
450 ce_namelen(ce
) == namelen
&&
451 !memcmp(ce
->name
, path
, namelen
))
454 /* no resolve-undo information; fall back */
456 /* If there isn't, either it is unmerged, or
457 * resolved as "removed" by mistake. We do not
458 * want to do anything in the former case.
461 if (pos
< active_nr
) {
462 struct cache_entry
*ce
= active_cache
[pos
];
463 if (ce_namelen(ce
) == namelen
&&
464 !memcmp(ce
->name
, path
, namelen
)) {
466 "%s: skipping still unmerged path.\n",
473 /* Grab blobs from given path from HEAD and MERGE_HEAD,
474 * stuff HEAD version in stage #2,
475 * stuff MERGE_HEAD version in stage #3.
477 ce_2
= read_one_ent("our", head_sha1
, path
, namelen
, 2);
478 ce_3
= read_one_ent("their", merge_head_sha1
, path
, namelen
, 3);
480 if (!ce_2
|| !ce_3
) {
484 if (!hashcmp(ce_2
->sha1
, ce_3
->sha1
) &&
485 ce_2
->ce_mode
== ce_3
->ce_mode
) {
486 fprintf(stderr
, "%s: identical in both, skipping.\n",
491 remove_file_from_cache(path
);
492 if (add_cache_entry(ce_2
, ADD_CACHE_OK_TO_ADD
)) {
493 error("%s: cannot add our version to the index.", path
);
497 if (!add_cache_entry(ce_3
, ADD_CACHE_OK_TO_ADD
))
499 error("%s: cannot add their version to the index.", path
);
507 static void read_head_pointers(void)
509 if (read_ref("HEAD", head_sha1
))
510 die("No HEAD -- no initial commit yet?");
511 if (read_ref("MERGE_HEAD", merge_head_sha1
)) {
512 fprintf(stderr
, "Not in the middle of a merge.\n");
517 static int do_unresolve(int ac
, const char **av
,
518 const char *prefix
, int prefix_length
)
523 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
524 * are not doing a merge, so exit with success status.
526 read_head_pointers();
528 for (i
= 1; i
< ac
; i
++) {
529 const char *arg
= av
[i
];
530 const char *p
= prefix_path(prefix
, prefix_length
, arg
);
531 err
|= unresolve_one(p
);
532 if (p
< arg
|| p
> arg
+ strlen(arg
))
538 static int do_reupdate(int ac
, const char **av
,
539 const char *prefix
, int prefix_length
)
541 /* Read HEAD and run update-index on paths that are
542 * merged and already different between index and HEAD.
546 const char **pathspec
= get_pathspec(prefix
, av
+ 1);
548 if (read_ref("HEAD", head_sha1
))
549 /* If there is no HEAD, that means it is an initial
550 * commit. Update everything in the index.
554 for (pos
= 0; pos
< active_nr
; pos
++) {
555 struct cache_entry
*ce
= active_cache
[pos
];
556 struct cache_entry
*old
= NULL
;
559 if (ce_stage(ce
) || !ce_path_match(ce
, pathspec
))
562 old
= read_one_ent(NULL
, head_sha1
,
563 ce
->name
, ce_namelen(ce
), 0);
564 if (old
&& ce
->ce_mode
== old
->ce_mode
&&
565 !hashcmp(ce
->sha1
, old
->sha1
)) {
567 continue; /* unchanged */
569 /* Be careful. The working tree may not have the
570 * path anymore, in which case, under 'allow_remove',
571 * or worse yet 'allow_replace', active_nr may decrease.
574 update_one(ce
->name
+ prefix_length
, prefix
, prefix_length
);
575 if (save_nr
!= active_nr
)
581 int cmd_update_index(int argc
, const char **argv
, const char *prefix
)
583 int i
, newfd
, entries
, has_errors
= 0, line_termination
= '\n';
584 int allow_options
= 1;
585 int read_from_stdin
= 0;
586 int prefix_length
= prefix
? strlen(prefix
) : 0;
587 char set_executable_bit
= 0;
588 unsigned int refresh_flags
= 0;
590 struct lock_file
*lock_file
;
592 git_config(git_default_config
, NULL
);
594 /* We can't free this memory, it becomes part of a linked list parsed atexit() */
595 lock_file
= xcalloc(1, sizeof(struct lock_file
));
597 newfd
= hold_locked_index(lock_file
, 0);
601 entries
= read_cache();
603 die("cache corrupted");
605 for (i
= 1 ; i
< argc
; i
++) {
606 const char *path
= argv
[i
];
609 if (allow_options
&& *path
== '-') {
610 if (!strcmp(path
, "--")) {
614 if (!strcmp(path
, "-q")) {
615 refresh_flags
|= REFRESH_QUIET
;
618 if (!strcmp(path
, "--ignore-submodules")) {
619 refresh_flags
|= REFRESH_IGNORE_SUBMODULES
;
622 if (!strcmp(path
, "--add")) {
626 if (!strcmp(path
, "--replace")) {
630 if (!strcmp(path
, "--remove")) {
634 if (!strcmp(path
, "--unmerged")) {
635 refresh_flags
|= REFRESH_UNMERGED
;
638 if (!strcmp(path
, "--refresh")) {
640 has_errors
|= refresh_cache(refresh_flags
);
643 if (!strcmp(path
, "--really-refresh")) {
645 has_errors
|= refresh_cache(REFRESH_REALLY
| refresh_flags
);
648 if (!strcmp(path
, "--cacheinfo")) {
649 unsigned char sha1
[20];
653 die("git update-index: --cacheinfo <mode> <sha1> <path>");
655 if (strtoul_ui(argv
[i
+1], 8, &mode
) ||
656 get_sha1_hex(argv
[i
+2], sha1
) ||
657 add_cacheinfo(mode
, sha1
, argv
[i
+3], 0))
658 die("git update-index: --cacheinfo"
659 " cannot add %s", argv
[i
+3]);
663 if (!strcmp(path
, "--chmod=-x") ||
664 !strcmp(path
, "--chmod=+x")) {
666 die("git update-index: %s <path>", path
);
667 set_executable_bit
= path
[8];
670 if (!strcmp(path
, "--assume-unchanged")) {
671 mark_valid_only
= MARK_FLAG
;
674 if (!strcmp(path
, "--no-assume-unchanged")) {
675 mark_valid_only
= UNMARK_FLAG
;
678 if (!strcmp(path
, "--no-skip-worktree")) {
679 mark_skip_worktree_only
= UNMARK_FLAG
;
682 if (!strcmp(path
, "--skip-worktree")) {
683 mark_skip_worktree_only
= MARK_FLAG
;
686 if (!strcmp(path
, "--info-only")) {
690 if (!strcmp(path
, "--force-remove")) {
694 if (!strcmp(path
, "-z")) {
695 line_termination
= 0;
698 if (!strcmp(path
, "--stdin")) {
700 die("--stdin must be at the end");
704 if (!strcmp(path
, "--index-info")) {
706 die("--index-info must be at the end");
707 allow_add
= allow_replace
= allow_remove
= 1;
708 read_index_info(line_termination
);
711 if (!strcmp(path
, "--unresolve")) {
712 has_errors
= do_unresolve(argc
- i
, argv
+ i
,
713 prefix
, prefix_length
);
715 active_cache_changed
= 0;
718 if (!strcmp(path
, "--again") || !strcmp(path
, "-g")) {
720 has_errors
= do_reupdate(argc
- i
, argv
+ i
,
721 prefix
, prefix_length
);
723 active_cache_changed
= 0;
726 if (!strcmp(path
, "--ignore-missing")) {
727 refresh_flags
|= REFRESH_IGNORE_MISSING
;
730 if (!strcmp(path
, "--verbose")) {
734 if (!strcmp(path
, "--clear-resolve-undo")) {
735 resolve_undo_clear();
738 if (!strcmp(path
, "-h") || !strcmp(path
, "--help"))
739 usage(update_index_usage
);
740 die("unknown option %s", path
);
743 p
= prefix_path(prefix
, prefix_length
, path
);
744 update_one(p
, NULL
, 0);
745 if (set_executable_bit
)
746 chmod_path(set_executable_bit
, p
);
747 if (p
< path
|| p
> path
+ strlen(path
))
750 if (read_from_stdin
) {
751 struct strbuf buf
= STRBUF_INIT
, nbuf
= STRBUF_INIT
;
754 while (strbuf_getline(&buf
, stdin
, line_termination
) != EOF
) {
756 if (line_termination
&& buf
.buf
[0] == '"') {
758 if (unquote_c_style(&nbuf
, buf
.buf
, NULL
))
759 die("line is badly quoted");
760 strbuf_swap(&buf
, &nbuf
);
762 p
= prefix_path(prefix
, prefix_length
, buf
.buf
);
763 update_one(p
, NULL
, 0);
764 if (set_executable_bit
)
765 chmod_path(set_executable_bit
, p
);
766 if (p
< buf
.buf
|| p
> buf
.buf
+ buf
.len
)
769 strbuf_release(&nbuf
);
770 strbuf_release(&buf
);
774 if (active_cache_changed
) {
776 if (refresh_flags
& REFRESH_QUIET
)
778 unable_to_lock_index_die(get_index_file(), lock_error
);
780 if (write_cache(newfd
, active_cache
, active_nr
) ||
781 commit_locked_index(lock_file
))
782 die("Unable to write new index file");
785 rollback_lock_file(lock_file
);
787 return has_errors
? 1 : 0;