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 __attribute__((format (printf
, 1, 2)))
31 static void report(const char *fmt
, ...)
44 static int mark_valid(const char *path
)
46 int namelen
= strlen(path
);
47 int pos
= cache_name_pos(path
, namelen
);
49 switch (mark_valid_only
) {
51 active_cache
[pos
]->ce_flags
|= CE_VALID
;
54 active_cache
[pos
]->ce_flags
&= ~CE_VALID
;
57 cache_tree_invalidate_path(active_cache_tree
, path
);
58 active_cache_changed
= 1;
64 static int remove_one_path(const char *path
)
67 return error("%s: does not exist and --remove not passed", path
);
68 if (remove_file_from_cache(path
))
69 return error("%s: cannot remove from the index", path
);
74 * Handle a path that couldn't be lstat'ed. It's either:
75 * - missing file (ENOENT or ENOTDIR). That's ok if we're
76 * supposed to be removing it and the removal actually
78 * - permission error. That's never ok.
80 static int process_lstat_error(const char *path
, int err
)
82 if (err
== ENOENT
|| err
== ENOTDIR
)
83 return remove_one_path(path
);
84 return error("lstat(\"%s\"): %s", path
, strerror(errno
));
87 static int add_one_path(struct cache_entry
*old
, const char *path
, int len
, struct stat
*st
)
90 struct cache_entry
*ce
;
92 /* Was the old index entry already up-to-date? */
93 if (old
&& !ce_stage(old
) && !ce_match_stat(old
, st
, 0))
96 size
= cache_entry_size(len
);
97 ce
= xcalloc(1, size
);
98 memcpy(ce
->name
, path
, len
);
100 fill_stat_cache_info(ce
, st
);
101 ce
->ce_mode
= ce_mode_from_stat(old
, st
->st_mode
);
103 if (index_path(ce
->sha1
, path
, st
, !info_only
))
105 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
106 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
107 if (add_cache_entry(ce
, option
))
108 return error("%s: cannot add to the index - missing --add option?", path
);
113 * Handle a path that was a directory. Four cases:
115 * - it's already a gitlink in the index, and we keep it that
116 * way, and update it if we can (if we cannot find the HEAD,
117 * we're going to keep it unchanged in the index!)
119 * - it's a *file* in the index, in which case it should be
120 * removed as a file if removal is allowed, since it doesn't
121 * exist as such any more. If removal isn't allowed, it's
124 * (NOTE! This is old and arguably fairly strange behaviour.
125 * We might want to make this an error unconditionally, and
126 * use "--force-remove" if you actually want to force removal).
128 * - it used to exist as a subdirectory (ie multiple files with
129 * this particular prefix) in the index, in which case it's wrong
130 * to try to update it as a directory.
132 * - it doesn't exist at all in the index, but it is a valid
133 * git directory, and it should be *added* as a gitlink.
135 static int process_directory(const char *path
, int len
, struct stat
*st
)
137 unsigned char sha1
[20];
138 int pos
= cache_name_pos(path
, len
);
140 /* Exact match: file or existing gitlink */
142 struct cache_entry
*ce
= active_cache
[pos
];
143 if (S_ISGITLINK(ce
->ce_mode
)) {
145 /* Do nothing to the index if there is no HEAD! */
146 if (resolve_gitlink_ref(path
, "HEAD", sha1
) < 0)
149 return add_one_path(ce
, path
, len
, st
);
151 /* Should this be an unconditional error? */
152 return remove_one_path(path
);
155 /* Inexact match: is there perhaps a subdirectory match? */
157 while (pos
< active_nr
) {
158 struct cache_entry
*ce
= active_cache
[pos
++];
160 if (strncmp(ce
->name
, path
, len
))
162 if (ce
->name
[len
] > '/')
164 if (ce
->name
[len
] < '/')
167 /* Subdirectory match - error out */
168 return error("%s: is a directory - add individual files instead", path
);
171 /* No match - should we add it as a gitlink? */
172 if (!resolve_gitlink_ref(path
, "HEAD", sha1
))
173 return add_one_path(NULL
, path
, len
, st
);
176 return error("%s: is a directory - add files inside instead", path
);
180 * Process a regular file
182 static int process_file(const char *path
, int len
, struct stat
*st
)
184 int pos
= cache_name_pos(path
, len
);
185 struct cache_entry
*ce
= pos
< 0 ? NULL
: active_cache
[pos
];
187 if (ce
&& S_ISGITLINK(ce
->ce_mode
))
188 return error("%s is already a gitlink, not replacing", path
);
190 return add_one_path(ce
, path
, len
, st
);
193 static int process_path(const char *path
)
199 if (has_symlink_leading_path(path
, len
))
200 return error("'%s' is beyond a symbolic link", path
);
203 * First things first: get the stat information, to decide
204 * what to do about the pathname!
206 if (lstat(path
, &st
) < 0)
207 return process_lstat_error(path
, errno
);
209 if (S_ISDIR(st
.st_mode
))
210 return process_directory(path
, len
, &st
);
212 return process_file(path
, len
, &st
);
215 static int add_cacheinfo(unsigned int mode
, const unsigned char *sha1
,
216 const char *path
, int stage
)
218 int size
, len
, option
;
219 struct cache_entry
*ce
;
221 if (!verify_path(path
))
222 return error("Invalid path '%s'", path
);
225 size
= cache_entry_size(len
);
226 ce
= xcalloc(1, size
);
228 hashcpy(ce
->sha1
, sha1
);
229 memcpy(ce
->name
, path
, len
);
230 ce
->ce_flags
= create_ce_flags(len
, stage
);
231 ce
->ce_mode
= create_ce_mode(mode
);
232 if (assume_unchanged
)
233 ce
->ce_flags
|= CE_VALID
;
234 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
235 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
236 if (add_cache_entry(ce
, option
))
237 return error("%s: cannot add to the index - missing --add option?",
239 report("add '%s'", path
);
243 static void chmod_path(int flip
, const char *path
)
246 struct cache_entry
*ce
;
249 pos
= cache_name_pos(path
, strlen(path
));
252 ce
= active_cache
[pos
];
258 ce
->ce_mode
|= 0111; break;
260 ce
->ce_mode
&= ~0111; break;
264 cache_tree_invalidate_path(active_cache_tree
, path
);
265 active_cache_changed
= 1;
266 report("chmod %cx '%s'", flip
, path
);
269 die("git update-index: cannot chmod %cx '%s'", flip
, path
);
272 static void update_one(const char *path
, const char *prefix
, int prefix_length
)
274 const char *p
= prefix_path(prefix
, prefix_length
, path
);
275 if (!verify_path(p
)) {
276 fprintf(stderr
, "Ignoring path %s\n", path
);
279 if (mark_valid_only
) {
281 die("Unable to mark file %s", path
);
286 if (remove_file_from_cache(p
))
287 die("git update-index: unable to remove %s", path
);
288 report("remove '%s'", path
);
292 die("Unable to process path %s", path
);
293 report("add '%s'", path
);
295 if (p
< path
|| p
> path
+ strlen(path
))
299 static void read_index_info(int line_termination
)
301 struct strbuf buf
= STRBUF_INIT
;
302 struct strbuf uq
= STRBUF_INIT
;
304 while (strbuf_getline(&buf
, stdin
, line_termination
) != EOF
) {
307 unsigned char sha1
[20];
312 /* This reads lines formatted in one of three formats:
314 * (1) mode SP sha1 TAB path
315 * The first format is what "git apply --index-info"
316 * reports, and used to reconstruct a partial tree
317 * that is used for phony merge base tree when falling
318 * back on 3-way merge.
320 * (2) mode SP type SP sha1 TAB path
321 * The second format is to stuff "git ls-tree" output
322 * into the index file.
324 * (3) mode SP sha1 SP stage TAB path
325 * This format is to put higher order stages into the
326 * index file and matches "git ls-files --stage" output.
329 ul
= strtoul(buf
.buf
, &ptr
, 8);
330 if (ptr
== buf
.buf
|| *ptr
!= ' '
331 || errno
|| (unsigned int) ul
!= ul
)
335 tab
= strchr(ptr
, '\t');
336 if (!tab
|| tab
- ptr
< 41)
339 if (tab
[-2] == ' ' && '0' <= tab
[-1] && tab
[-1] <= '3') {
340 stage
= tab
[-1] - '0';
341 ptr
= tab
+ 1; /* point at the head of path */
342 tab
= tab
- 2; /* point at tail of sha1 */
346 ptr
= tab
+ 1; /* point at the head of path */
349 if (get_sha1_hex(tab
- 40, sha1
) || tab
[-41] != ' ')
353 if (line_termination
&& path_name
[0] == '"') {
355 if (unquote_c_style(&uq
, path_name
, NULL
)) {
356 die("git update-index: bad quoting of path name");
361 if (!verify_path(path_name
)) {
362 fprintf(stderr
, "Ignoring path %s\n", path_name
);
367 /* mode == 0 means there is no such path -- remove */
368 if (remove_file_from_cache(path_name
))
369 die("git update-index: unable to remove %s",
373 /* mode ' ' sha1 '\t' name
374 * ptr[-1] points at tab,
375 * ptr[-41] is at the beginning of sha1
377 ptr
[-42] = ptr
[-1] = 0;
378 if (add_cacheinfo(mode
, sha1
, path_name
, stage
))
379 die("git update-index: unable to update %s",
385 die("malformed index info %s", buf
.buf
);
387 strbuf_release(&buf
);
391 static const char update_index_usage
[] =
392 "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>...";
394 static unsigned char head_sha1
[20];
395 static unsigned char merge_head_sha1
[20];
397 static struct cache_entry
*read_one_ent(const char *which
,
398 unsigned char *ent
, const char *path
,
399 int namelen
, int stage
)
402 unsigned char sha1
[20];
404 struct cache_entry
*ce
;
406 if (get_tree_entry(ent
, path
, sha1
, &mode
)) {
408 error("%s: not in %s branch.", path
, which
);
411 if (mode
== S_IFDIR
) {
413 error("%s: not a blob in %s branch.", path
, which
);
416 size
= cache_entry_size(namelen
);
417 ce
= xcalloc(1, size
);
419 hashcpy(ce
->sha1
, sha1
);
420 memcpy(ce
->name
, path
, namelen
);
421 ce
->ce_flags
= create_ce_flags(namelen
, stage
);
422 ce
->ce_mode
= create_ce_mode(mode
);
426 static int unresolve_one(const char *path
)
428 int namelen
= strlen(path
);
431 struct cache_entry
*ce_2
= NULL
, *ce_3
= NULL
;
433 /* See if there is such entry in the index. */
434 pos
= cache_name_pos(path
, namelen
);
436 /* If there isn't, either it is unmerged, or
437 * resolved as "removed" by mistake. We do not
438 * want to do anything in the former case.
441 if (pos
< active_nr
) {
442 struct cache_entry
*ce
= active_cache
[pos
];
443 if (ce_namelen(ce
) == namelen
&&
444 !memcmp(ce
->name
, path
, namelen
)) {
446 "%s: skipping still unmerged path.\n",
453 /* Grab blobs from given path from HEAD and MERGE_HEAD,
454 * stuff HEAD version in stage #2,
455 * stuff MERGE_HEAD version in stage #3.
457 ce_2
= read_one_ent("our", head_sha1
, path
, namelen
, 2);
458 ce_3
= read_one_ent("their", merge_head_sha1
, path
, namelen
, 3);
460 if (!ce_2
|| !ce_3
) {
464 if (!hashcmp(ce_2
->sha1
, ce_3
->sha1
) &&
465 ce_2
->ce_mode
== ce_3
->ce_mode
) {
466 fprintf(stderr
, "%s: identical in both, skipping.\n",
471 remove_file_from_cache(path
);
472 if (add_cache_entry(ce_2
, ADD_CACHE_OK_TO_ADD
)) {
473 error("%s: cannot add our version to the index.", path
);
477 if (!add_cache_entry(ce_3
, ADD_CACHE_OK_TO_ADD
))
479 error("%s: cannot add their version to the index.", path
);
487 static void read_head_pointers(void)
489 if (read_ref("HEAD", head_sha1
))
490 die("No HEAD -- no initial commit yet?");
491 if (read_ref("MERGE_HEAD", merge_head_sha1
)) {
492 fprintf(stderr
, "Not in the middle of a merge.\n");
497 static int do_unresolve(int ac
, const char **av
,
498 const char *prefix
, int prefix_length
)
503 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
504 * are not doing a merge, so exit with success status.
506 read_head_pointers();
508 for (i
= 1; i
< ac
; i
++) {
509 const char *arg
= av
[i
];
510 const char *p
= prefix_path(prefix
, prefix_length
, arg
);
511 err
|= unresolve_one(p
);
512 if (p
< arg
|| p
> arg
+ strlen(arg
))
518 static int do_reupdate(int ac
, const char **av
,
519 const char *prefix
, int prefix_length
)
521 /* Read HEAD and run update-index on paths that are
522 * merged and already different between index and HEAD.
526 const char **pathspec
= get_pathspec(prefix
, av
+ 1);
528 if (read_ref("HEAD", head_sha1
))
529 /* If there is no HEAD, that means it is an initial
530 * commit. Update everything in the index.
534 for (pos
= 0; pos
< active_nr
; pos
++) {
535 struct cache_entry
*ce
= active_cache
[pos
];
536 struct cache_entry
*old
= NULL
;
539 if (ce_stage(ce
) || !ce_path_match(ce
, pathspec
))
542 old
= read_one_ent(NULL
, head_sha1
,
543 ce
->name
, ce_namelen(ce
), 0);
544 if (old
&& ce
->ce_mode
== old
->ce_mode
&&
545 !hashcmp(ce
->sha1
, old
->sha1
)) {
547 continue; /* unchanged */
549 /* Be careful. The working tree may not have the
550 * path anymore, in which case, under 'allow_remove',
551 * or worse yet 'allow_replace', active_nr may decrease.
554 update_one(ce
->name
+ prefix_length
, prefix
, prefix_length
);
555 if (save_nr
!= active_nr
)
561 int cmd_update_index(int argc
, const char **argv
, const char *prefix
)
563 int i
, newfd
, entries
, has_errors
= 0, line_termination
= '\n';
564 int allow_options
= 1;
565 int read_from_stdin
= 0;
566 int prefix_length
= prefix
? strlen(prefix
) : 0;
567 char set_executable_bit
= 0;
568 unsigned int refresh_flags
= 0;
570 struct lock_file
*lock_file
;
572 git_config(git_default_config
, NULL
);
574 /* We can't free this memory, it becomes part of a linked list parsed atexit() */
575 lock_file
= xcalloc(1, sizeof(struct lock_file
));
577 newfd
= hold_locked_index(lock_file
, 0);
581 entries
= read_cache();
583 die("cache corrupted");
585 for (i
= 1 ; i
< argc
; i
++) {
586 const char *path
= argv
[i
];
589 if (allow_options
&& *path
== '-') {
590 if (!strcmp(path
, "--")) {
594 if (!strcmp(path
, "-q")) {
595 refresh_flags
|= REFRESH_QUIET
;
598 if (!strcmp(path
, "--ignore-submodules")) {
599 refresh_flags
|= REFRESH_IGNORE_SUBMODULES
;
602 if (!strcmp(path
, "--add")) {
606 if (!strcmp(path
, "--replace")) {
610 if (!strcmp(path
, "--remove")) {
614 if (!strcmp(path
, "--unmerged")) {
615 refresh_flags
|= REFRESH_UNMERGED
;
618 if (!strcmp(path
, "--refresh")) {
620 has_errors
|= refresh_cache(refresh_flags
);
623 if (!strcmp(path
, "--really-refresh")) {
625 has_errors
|= refresh_cache(REFRESH_REALLY
| refresh_flags
);
628 if (!strcmp(path
, "--cacheinfo")) {
629 unsigned char sha1
[20];
633 die("git update-index: --cacheinfo <mode> <sha1> <path>");
635 if (strtoul_ui(argv
[i
+1], 8, &mode
) ||
636 get_sha1_hex(argv
[i
+2], sha1
) ||
637 add_cacheinfo(mode
, sha1
, argv
[i
+3], 0))
638 die("git update-index: --cacheinfo"
639 " cannot add %s", argv
[i
+3]);
643 if (!strcmp(path
, "--chmod=-x") ||
644 !strcmp(path
, "--chmod=+x")) {
646 die("git update-index: %s <path>", path
);
647 set_executable_bit
= path
[8];
650 if (!strcmp(path
, "--assume-unchanged")) {
651 mark_valid_only
= MARK_VALID
;
654 if (!strcmp(path
, "--no-assume-unchanged")) {
655 mark_valid_only
= UNMARK_VALID
;
658 if (!strcmp(path
, "--info-only")) {
662 if (!strcmp(path
, "--force-remove")) {
666 if (!strcmp(path
, "-z")) {
667 line_termination
= 0;
670 if (!strcmp(path
, "--stdin")) {
672 die("--stdin must be at the end");
676 if (!strcmp(path
, "--index-info")) {
678 die("--index-info must be at the end");
679 allow_add
= allow_replace
= allow_remove
= 1;
680 read_index_info(line_termination
);
683 if (!strcmp(path
, "--unresolve")) {
684 has_errors
= do_unresolve(argc
- i
, argv
+ i
,
685 prefix
, prefix_length
);
687 active_cache_changed
= 0;
690 if (!strcmp(path
, "--again") || !strcmp(path
, "-g")) {
692 has_errors
= do_reupdate(argc
- i
, argv
+ i
,
693 prefix
, prefix_length
);
695 active_cache_changed
= 0;
698 if (!strcmp(path
, "--ignore-missing")) {
699 refresh_flags
|= REFRESH_IGNORE_MISSING
;
702 if (!strcmp(path
, "--verbose")) {
706 if (!strcmp(path
, "-h") || !strcmp(path
, "--help"))
707 usage(update_index_usage
);
708 die("unknown option %s", path
);
711 p
= prefix_path(prefix
, prefix_length
, path
);
712 update_one(p
, NULL
, 0);
713 if (set_executable_bit
)
714 chmod_path(set_executable_bit
, p
);
715 if (p
< path
|| p
> path
+ strlen(path
))
718 if (read_from_stdin
) {
719 struct strbuf buf
= STRBUF_INIT
, nbuf
= STRBUF_INIT
;
722 while (strbuf_getline(&buf
, stdin
, line_termination
) != EOF
) {
724 if (line_termination
&& buf
.buf
[0] == '"') {
726 if (unquote_c_style(&nbuf
, buf
.buf
, NULL
))
727 die("line is badly quoted");
728 strbuf_swap(&buf
, &nbuf
);
730 p
= prefix_path(prefix
, prefix_length
, buf
.buf
);
731 update_one(p
, NULL
, 0);
732 if (set_executable_bit
)
733 chmod_path(set_executable_bit
, p
);
734 if (p
< buf
.buf
|| p
> buf
.buf
+ buf
.len
)
737 strbuf_release(&nbuf
);
738 strbuf_release(&buf
);
742 if (active_cache_changed
) {
744 if (refresh_flags
& REFRESH_QUIET
)
746 unable_to_lock_index_die(get_index_file(), lock_error
);
748 if (write_cache(newfd
, active_cache
, active_nr
) ||
749 commit_locked_index(lock_file
))
750 die("Unable to write new index file");
753 rollback_lock_file(lock_file
);
755 return has_errors
? 1 : 0;