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
;
28 #define UNMARK_VALID 2
30 static void report(const char *fmt
, ...)
43 static int mark_valid(const char *path
)
45 int namelen
= strlen(path
);
46 int pos
= cache_name_pos(path
, namelen
);
48 switch (mark_valid_only
) {
50 active_cache
[pos
]->ce_flags
|= CE_VALID
;
53 active_cache
[pos
]->ce_flags
&= ~CE_VALID
;
56 cache_tree_invalidate_path(active_cache_tree
, path
);
57 active_cache_changed
= 1;
63 static int remove_one_path(const char *path
)
66 return error("%s: does not exist and --remove not passed", path
);
67 if (remove_file_from_cache(path
))
68 return error("%s: cannot remove from the index", path
);
73 * Handle a path that couldn't be lstat'ed. It's either:
74 * - missing file (ENOENT or ENOTDIR). That's ok if we're
75 * supposed to be removing it and the removal actually
77 * - permission error. That's never ok.
79 static int process_lstat_error(const char *path
, int err
)
81 if (err
== ENOENT
|| err
== ENOTDIR
)
82 return remove_one_path(path
);
83 return error("lstat(\"%s\"): %s", path
, strerror(errno
));
86 static int add_one_path(struct cache_entry
*old
, const char *path
, int len
, struct stat
*st
)
89 struct cache_entry
*ce
;
91 /* Was the old index entry already up-to-date? */
92 if (old
&& !ce_stage(old
) && !ce_match_stat(old
, st
, 0))
95 size
= cache_entry_size(len
);
96 ce
= xcalloc(1, size
);
97 memcpy(ce
->name
, path
, len
);
99 fill_stat_cache_info(ce
, st
);
100 ce
->ce_mode
= ce_mode_from_stat(old
, st
->st_mode
);
102 if (index_path(ce
->sha1
, path
, st
, !info_only
))
104 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
105 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
106 if (add_cache_entry(ce
, option
))
107 return error("%s: cannot add to the index - missing --add option?", path
);
112 * Handle a path that was a directory. Four cases:
114 * - it's already a gitlink in the index, and we keep it that
115 * way, and update it if we can (if we cannot find the HEAD,
116 * we're going to keep it unchanged in the index!)
118 * - it's a *file* in the index, in which case it should be
119 * removed as a file if removal is allowed, since it doesn't
120 * exist as such any more. If removal isn't allowed, it's
123 * (NOTE! This is old and arguably fairly strange behaviour.
124 * We might want to make this an error unconditionally, and
125 * use "--force-remove" if you actually want to force removal).
127 * - it used to exist as a subdirectory (ie multiple files with
128 * this particular prefix) in the index, in which case it's wrong
129 * to try to update it as a directory.
131 * - it doesn't exist at all in the index, but it is a valid
132 * git directory, and it should be *added* as a gitlink.
134 static int process_directory(const char *path
, int len
, struct stat
*st
)
136 unsigned char sha1
[20];
137 int pos
= cache_name_pos(path
, len
);
139 /* Exact match: file or existing gitlink */
141 struct cache_entry
*ce
= active_cache
[pos
];
142 if (S_ISGITLINK(ce
->ce_mode
)) {
144 /* Do nothing to the index if there is no HEAD! */
145 if (resolve_gitlink_ref(path
, "HEAD", sha1
) < 0)
148 return add_one_path(ce
, path
, len
, st
);
150 /* Should this be an unconditional error? */
151 return remove_one_path(path
);
154 /* Inexact match: is there perhaps a subdirectory match? */
156 while (pos
< active_nr
) {
157 struct cache_entry
*ce
= active_cache
[pos
++];
159 if (strncmp(ce
->name
, path
, len
))
161 if (ce
->name
[len
] > '/')
163 if (ce
->name
[len
] < '/')
166 /* Subdirectory match - error out */
167 return error("%s: is a directory - add individual files instead", path
);
170 /* No match - should we add it as a gitlink? */
171 if (!resolve_gitlink_ref(path
, "HEAD", sha1
))
172 return add_one_path(NULL
, path
, len
, st
);
175 return error("%s: is a directory - add files inside instead", path
);
179 * Process a regular file
181 static int process_file(const char *path
, int len
, struct stat
*st
)
183 int pos
= cache_name_pos(path
, len
);
184 struct cache_entry
*ce
= pos
< 0 ? NULL
: active_cache
[pos
];
186 if (ce
&& S_ISGITLINK(ce
->ce_mode
))
187 return error("%s is already a gitlink, not replacing", path
);
189 return add_one_path(ce
, path
, len
, st
);
192 static int process_path(const char *path
)
198 * First things first: get the stat information, to decide
199 * what to do about the pathname!
201 if (lstat(path
, &st
) < 0)
202 return process_lstat_error(path
, errno
);
205 if (S_ISDIR(st
.st_mode
))
206 return process_directory(path
, len
, &st
);
208 return process_file(path
, len
, &st
);
211 static int add_cacheinfo(unsigned int mode
, const unsigned char *sha1
,
212 const char *path
, int stage
)
214 int size
, len
, option
;
215 struct cache_entry
*ce
;
217 if (!verify_path(path
))
221 size
= cache_entry_size(len
);
222 ce
= xcalloc(1, size
);
224 hashcpy(ce
->sha1
, sha1
);
225 memcpy(ce
->name
, path
, len
);
226 ce
->ce_flags
= create_ce_flags(len
, stage
);
227 ce
->ce_mode
= create_ce_mode(mode
);
228 if (assume_unchanged
)
229 ce
->ce_flags
|= CE_VALID
;
230 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
231 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
232 if (add_cache_entry(ce
, option
))
233 return error("%s: cannot add to the index - missing --add option?",
235 report("add '%s'", path
);
239 static void chmod_path(int flip
, const char *path
)
242 struct cache_entry
*ce
;
245 pos
= cache_name_pos(path
, strlen(path
));
248 ce
= active_cache
[pos
];
254 ce
->ce_mode
|= 0111; break;
256 ce
->ce_mode
&= ~0111; break;
260 cache_tree_invalidate_path(active_cache_tree
, path
);
261 active_cache_changed
= 1;
262 report("chmod %cx '%s'", flip
, path
);
265 die("git-update-index: cannot chmod %cx '%s'", flip
, path
);
268 static void update_one(const char *path
, const char *prefix
, int prefix_length
)
270 const char *p
= prefix_path(prefix
, prefix_length
, path
);
271 if (!verify_path(p
)) {
272 fprintf(stderr
, "Ignoring path %s\n", path
);
275 if (mark_valid_only
) {
277 die("Unable to mark file %s", path
);
282 if (remove_file_from_cache(p
))
283 die("git-update-index: unable to remove %s", path
);
284 report("remove '%s'", path
);
288 die("Unable to process path %s", path
);
289 report("add '%s'", path
);
291 if (p
< path
|| p
> path
+ strlen(path
))
295 static void read_index_info(int line_termination
)
300 strbuf_init(&buf
, 0);
302 while (strbuf_getline(&buf
, stdin
, line_termination
) != EOF
) {
305 unsigned char sha1
[20];
310 /* This reads lines formatted in one of three formats:
312 * (1) mode SP sha1 TAB path
313 * The first format is what "git-apply --index-info"
314 * reports, and used to reconstruct a partial tree
315 * that is used for phony merge base tree when falling
316 * back on 3-way merge.
318 * (2) mode SP type SP sha1 TAB path
319 * The second format is to stuff git-ls-tree output
320 * into the index file.
322 * (3) mode SP sha1 SP stage TAB path
323 * This format is to put higher order stages into the
324 * index file and matches git-ls-files --stage output.
327 ul
= strtoul(buf
.buf
, &ptr
, 8);
328 if (ptr
== buf
.buf
|| *ptr
!= ' '
329 || errno
|| (unsigned int) ul
!= ul
)
333 tab
= strchr(ptr
, '\t');
334 if (!tab
|| tab
- ptr
< 41)
337 if (tab
[-2] == ' ' && '0' <= tab
[-1] && tab
[-1] <= '3') {
338 stage
= tab
[-1] - '0';
339 ptr
= tab
+ 1; /* point at the head of path */
340 tab
= tab
- 2; /* point at tail of sha1 */
344 ptr
= tab
+ 1; /* point at the head of path */
347 if (get_sha1_hex(tab
- 40, sha1
) || tab
[-41] != ' ')
351 if (line_termination
&& path_name
[0] == '"') {
353 if (unquote_c_style(&uq
, path_name
, NULL
)) {
354 die("git-update-index: bad quoting of path name");
359 if (!verify_path(path_name
)) {
360 fprintf(stderr
, "Ignoring path %s\n", path_name
);
365 /* mode == 0 means there is no such path -- remove */
366 if (remove_file_from_cache(path_name
))
367 die("git-update-index: unable to remove %s",
371 /* mode ' ' sha1 '\t' name
372 * ptr[-1] points at tab,
373 * ptr[-41] is at the beginning of sha1
375 ptr
[-42] = ptr
[-1] = 0;
376 if (add_cacheinfo(mode
, sha1
, path_name
, stage
))
377 die("git-update-index: unable to update %s",
383 die("malformed index info %s", buf
.buf
);
385 strbuf_release(&buf
);
389 static const char update_index_usage
[] =
390 "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 | -g] [--ignore-missing] [-z] [--verbose] [--] <file>...";
392 static unsigned char head_sha1
[20];
393 static unsigned char merge_head_sha1
[20];
395 static struct cache_entry
*read_one_ent(const char *which
,
396 unsigned char *ent
, const char *path
,
397 int namelen
, int stage
)
400 unsigned char sha1
[20];
402 struct cache_entry
*ce
;
404 if (get_tree_entry(ent
, path
, sha1
, &mode
)) {
406 error("%s: not in %s branch.", path
, which
);
409 if (mode
== S_IFDIR
) {
411 error("%s: not a blob in %s branch.", path
, which
);
414 size
= cache_entry_size(namelen
);
415 ce
= xcalloc(1, size
);
417 hashcpy(ce
->sha1
, sha1
);
418 memcpy(ce
->name
, path
, namelen
);
419 ce
->ce_flags
= create_ce_flags(namelen
, stage
);
420 ce
->ce_mode
= create_ce_mode(mode
);
424 static int unresolve_one(const char *path
)
426 int namelen
= strlen(path
);
429 struct cache_entry
*ce_2
= NULL
, *ce_3
= NULL
;
431 /* See if there is such entry in the index. */
432 pos
= cache_name_pos(path
, namelen
);
434 /* If there isn't, either it is unmerged, or
435 * resolved as "removed" by mistake. We do not
436 * want to do anything in the former case.
439 if (pos
< active_nr
) {
440 struct cache_entry
*ce
= active_cache
[pos
];
441 if (ce_namelen(ce
) == namelen
&&
442 !memcmp(ce
->name
, path
, namelen
)) {
444 "%s: skipping still unmerged path.\n",
451 /* Grab blobs from given path from HEAD and MERGE_HEAD,
452 * stuff HEAD version in stage #2,
453 * stuff MERGE_HEAD version in stage #3.
455 ce_2
= read_one_ent("our", head_sha1
, path
, namelen
, 2);
456 ce_3
= read_one_ent("their", merge_head_sha1
, path
, namelen
, 3);
458 if (!ce_2
|| !ce_3
) {
462 if (!hashcmp(ce_2
->sha1
, ce_3
->sha1
) &&
463 ce_2
->ce_mode
== ce_3
->ce_mode
) {
464 fprintf(stderr
, "%s: identical in both, skipping.\n",
469 remove_file_from_cache(path
);
470 if (add_cache_entry(ce_2
, ADD_CACHE_OK_TO_ADD
)) {
471 error("%s: cannot add our version to the index.", path
);
475 if (!add_cache_entry(ce_3
, ADD_CACHE_OK_TO_ADD
))
477 error("%s: cannot add their version to the index.", path
);
485 static void read_head_pointers(void)
487 if (read_ref("HEAD", head_sha1
))
488 die("No HEAD -- no initial commit yet?\n");
489 if (read_ref("MERGE_HEAD", merge_head_sha1
)) {
490 fprintf(stderr
, "Not in the middle of a merge.\n");
495 static int do_unresolve(int ac
, const char **av
,
496 const char *prefix
, int prefix_length
)
501 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
502 * are not doing a merge, so exit with success status.
504 read_head_pointers();
506 for (i
= 1; i
< ac
; i
++) {
507 const char *arg
= av
[i
];
508 const char *p
= prefix_path(prefix
, prefix_length
, arg
);
509 err
|= unresolve_one(p
);
510 if (p
< arg
|| p
> arg
+ strlen(arg
))
516 static int do_reupdate(int ac
, const char **av
,
517 const char *prefix
, int prefix_length
)
519 /* Read HEAD and run update-index on paths that are
520 * merged and already different between index and HEAD.
524 const char **pathspec
= get_pathspec(prefix
, av
+ 1);
526 if (read_ref("HEAD", head_sha1
))
527 /* If there is no HEAD, that means it is an initial
528 * commit. Update everything in the index.
532 for (pos
= 0; pos
< active_nr
; pos
++) {
533 struct cache_entry
*ce
= active_cache
[pos
];
534 struct cache_entry
*old
= NULL
;
537 if (ce_stage(ce
) || !ce_path_match(ce
, pathspec
))
540 old
= read_one_ent(NULL
, head_sha1
,
541 ce
->name
, ce_namelen(ce
), 0);
542 if (old
&& ce
->ce_mode
== old
->ce_mode
&&
543 !hashcmp(ce
->sha1
, old
->sha1
)) {
545 continue; /* unchanged */
547 /* Be careful. The working tree may not have the
548 * path anymore, in which case, under 'allow_remove',
549 * or worse yet 'allow_replace', active_nr may decrease.
552 update_one(ce
->name
+ prefix_length
, prefix
, prefix_length
);
553 if (save_nr
!= active_nr
)
559 int cmd_update_index(int argc
, const char **argv
, const char *prefix
)
561 int i
, newfd
, entries
, has_errors
= 0, line_termination
= '\n';
562 int allow_options
= 1;
563 int read_from_stdin
= 0;
564 int prefix_length
= prefix
? strlen(prefix
) : 0;
565 char set_executable_bit
= 0;
566 unsigned int refresh_flags
= 0;
568 struct lock_file
*lock_file
;
570 git_config(git_default_config
);
572 /* We can't free this memory, it becomes part of a linked list parsed atexit() */
573 lock_file
= xcalloc(1, sizeof(struct lock_file
));
575 newfd
= hold_locked_index(lock_file
, 0);
579 entries
= read_cache();
581 die("cache corrupted");
583 for (i
= 1 ; i
< argc
; i
++) {
584 const char *path
= argv
[i
];
587 if (allow_options
&& *path
== '-') {
588 if (!strcmp(path
, "--")) {
592 if (!strcmp(path
, "-q")) {
593 refresh_flags
|= REFRESH_QUIET
;
596 if (!strcmp(path
, "--ignore-submodules")) {
597 refresh_flags
|= REFRESH_IGNORE_SUBMODULES
;
600 if (!strcmp(path
, "--add")) {
604 if (!strcmp(path
, "--replace")) {
608 if (!strcmp(path
, "--remove")) {
612 if (!strcmp(path
, "--unmerged")) {
613 refresh_flags
|= REFRESH_UNMERGED
;
616 if (!strcmp(path
, "--refresh")) {
617 has_errors
|= refresh_cache(refresh_flags
);
620 if (!strcmp(path
, "--really-refresh")) {
621 has_errors
|= refresh_cache(REFRESH_REALLY
| refresh_flags
);
624 if (!strcmp(path
, "--cacheinfo")) {
625 unsigned char sha1
[20];
629 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
631 if (strtoul_ui(argv
[i
+1], 8, &mode
) ||
632 get_sha1_hex(argv
[i
+2], sha1
) ||
633 add_cacheinfo(mode
, sha1
, argv
[i
+3], 0))
634 die("git-update-index: --cacheinfo"
635 " cannot add %s", argv
[i
+3]);
639 if (!strcmp(path
, "--chmod=-x") ||
640 !strcmp(path
, "--chmod=+x")) {
642 die("git-update-index: %s <path>", path
);
643 set_executable_bit
= path
[8];
646 if (!strcmp(path
, "--assume-unchanged")) {
647 mark_valid_only
= MARK_VALID
;
650 if (!strcmp(path
, "--no-assume-unchanged")) {
651 mark_valid_only
= UNMARK_VALID
;
654 if (!strcmp(path
, "--info-only")) {
658 if (!strcmp(path
, "--force-remove")) {
662 if (!strcmp(path
, "-z")) {
663 line_termination
= 0;
666 if (!strcmp(path
, "--stdin")) {
668 die("--stdin must be at the end");
672 if (!strcmp(path
, "--index-info")) {
674 die("--index-info must be at the end");
675 allow_add
= allow_replace
= allow_remove
= 1;
676 read_index_info(line_termination
);
679 if (!strcmp(path
, "--unresolve")) {
680 has_errors
= do_unresolve(argc
- i
, argv
+ i
,
681 prefix
, prefix_length
);
683 active_cache_changed
= 0;
686 if (!strcmp(path
, "--again") || !strcmp(path
, "-g")) {
687 has_errors
= do_reupdate(argc
- i
, argv
+ i
,
688 prefix
, prefix_length
);
690 active_cache_changed
= 0;
693 if (!strcmp(path
, "--ignore-missing")) {
694 refresh_flags
|= REFRESH_IGNORE_MISSING
;
697 if (!strcmp(path
, "--verbose")) {
701 if (!strcmp(path
, "-h") || !strcmp(path
, "--help"))
702 usage(update_index_usage
);
703 die("unknown option %s", path
);
705 p
= prefix_path(prefix
, prefix_length
, path
);
706 update_one(p
, NULL
, 0);
707 if (set_executable_bit
)
708 chmod_path(set_executable_bit
, p
);
709 if (p
< path
|| p
> path
+ strlen(path
))
712 if (read_from_stdin
) {
713 struct strbuf buf
, nbuf
;
715 strbuf_init(&buf
, 0);
716 strbuf_init(&nbuf
, 0);
717 while (strbuf_getline(&buf
, stdin
, line_termination
) != EOF
) {
719 if (line_termination
&& buf
.buf
[0] == '"') {
721 if (unquote_c_style(&nbuf
, buf
.buf
, NULL
))
722 die("line is badly quoted");
723 strbuf_swap(&buf
, &nbuf
);
725 p
= prefix_path(prefix
, prefix_length
, buf
.buf
);
726 update_one(p
, NULL
, 0);
727 if (set_executable_bit
)
728 chmod_path(set_executable_bit
, p
);
729 if (p
< buf
.buf
|| p
> buf
.buf
+ buf
.len
)
732 strbuf_release(&nbuf
);
733 strbuf_release(&buf
);
737 if (active_cache_changed
) {
739 if (refresh_flags
& REFRESH_QUIET
)
741 die("unable to create '%s.lock': %s",
742 get_index_file(), strerror(lock_error
));
744 if (write_cache(newfd
, active_cache
, active_nr
) ||
745 commit_locked_index(lock_file
))
746 die("Unable to write new index file");
749 rollback_lock_file(lock_file
);
751 return has_errors
? 1 : 0;