2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
9 #include "cache-tree.h"
10 #include "tree-walk.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
;
29 #define UNMARK_VALID 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
|= htons(CE_VALID
);
54 active_cache
[pos
]->ce_flags
&= ~htons(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
)
89 int option
, size
= cache_entry_size(len
);
90 struct cache_entry
*ce
= xcalloc(1, size
);
92 memcpy(ce
->name
, path
, len
);
93 ce
->ce_flags
= htons(len
);
94 fill_stat_cache_info(ce
, st
);
95 ce
->ce_mode
= ce_mode_from_stat(old
, st
->st_mode
);
97 if (index_path(ce
->sha1
, path
, st
, !info_only
))
99 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
100 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
101 if (add_cache_entry(ce
, option
))
102 return error("%s: cannot add to the index - missing --add option?", path
);
107 * Handle a path that was a directory. Four cases:
109 * - it's already a gitlink in the index, and we keep it that
110 * way, and update it if we can (if we cannot find the HEAD,
111 * we're going to keep it unchanged in the index!)
113 * - it's a *file* in the index, in which case it should be
114 * removed as a file if removal is allowed, since it doesn't
115 * exist as such any more. If removal isn't allowed, it's
118 * (NOTE! This is old and arguably fairly strange behaviour.
119 * We might want to make this an error unconditionally, and
120 * use "--force-remove" if you actually want to force removal).
122 * - it used to exist as a subdirectory (ie multiple files with
123 * this particular prefix) in the index, in which case it's wrong
124 * to try to update it as a directory.
126 * - it doesn't exist at all in the index, but it is a valid
127 * git directory, and it should be *added* as a gitlink.
129 static int process_directory(const char *path
, int len
, struct stat
*st
)
131 unsigned char sha1
[20];
132 int pos
= cache_name_pos(path
, len
);
134 /* Exact match: file or existing gitlink */
136 struct cache_entry
*ce
= active_cache
[pos
];
137 if (S_ISDIRLNK(ntohl(ce
->ce_mode
))) {
139 /* Do nothing to the index if there is no HEAD! */
140 if (resolve_gitlink_ref(path
, "HEAD", sha1
) < 0)
143 return add_one_path(ce
, path
, len
, st
);
145 /* Should this be an unconditional error? */
146 return remove_one_path(path
);
149 /* Inexact match: is there perhaps a subdirectory match? */
151 while (pos
< active_nr
) {
152 struct cache_entry
*ce
= active_cache
[pos
++];
154 if (strncmp(ce
->name
, path
, len
))
156 if (ce
->name
[len
] > '/')
158 if (ce
->name
[len
] < '/')
161 /* Subdirectory match - error out */
162 return error("%s: is a directory - add individual files instead", path
);
165 /* No match - should we add it as a gitlink? */
166 if (!resolve_gitlink_ref(path
, "HEAD", sha1
))
167 return add_one_path(NULL
, path
, len
, st
);
170 return error("%s: is a directory - add files inside instead", path
);
174 * Process a regular file
176 static int process_file(const char *path
, int len
, struct stat
*st
)
178 int pos
= cache_name_pos(path
, len
);
179 struct cache_entry
*ce
= pos
< 0 ? NULL
: active_cache
[pos
];
181 if (ce
&& S_ISDIRLNK(ntohl(ce
->ce_mode
)))
182 return error("%s is already a gitlink, not replacing", path
);
184 return add_one_path(ce
, path
, len
, st
);
187 static int process_path(const char *path
)
192 /* We probably want to do this in remove_file_from_cache() and
193 * add_cache_entry() instead...
195 cache_tree_invalidate_path(active_cache_tree
, 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
|= htons(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
);
236 cache_tree_invalidate_path(active_cache_tree
, path
);
240 static void chmod_path(int flip
, const char *path
)
243 struct cache_entry
*ce
;
246 pos
= cache_name_pos(path
, strlen(path
));
249 ce
= active_cache
[pos
];
250 mode
= ntohl(ce
->ce_mode
);
255 ce
->ce_mode
|= htonl(0111); break;
257 ce
->ce_mode
&= htonl(~0111); break;
261 cache_tree_invalidate_path(active_cache_tree
, path
);
262 active_cache_changed
= 1;
263 report("chmod %cx '%s'", flip
, path
);
266 die("git-update-index: cannot chmod %cx '%s'", flip
, path
);
269 static void update_one(const char *path
, const char *prefix
, int prefix_length
)
271 const char *p
= prefix_path(prefix
, prefix_length
, path
);
272 if (!verify_path(p
)) {
273 fprintf(stderr
, "Ignoring path %s\n", path
);
276 if (mark_valid_only
) {
278 die("Unable to mark file %s", path
);
281 cache_tree_invalidate_path(active_cache_tree
, path
);
284 if (remove_file_from_cache(p
))
285 die("git-update-index: unable to remove %s", path
);
286 report("remove '%s'", path
);
290 die("Unable to process path %s", path
);
291 report("add '%s'", path
);
293 if (p
< path
|| p
> path
+ strlen(path
))
297 static void read_index_info(int line_termination
)
304 unsigned char sha1
[20];
308 /* This reads lines formatted in one of three formats:
310 * (1) mode SP sha1 TAB path
311 * The first format is what "git-apply --index-info"
312 * reports, and used to reconstruct a partial tree
313 * that is used for phony merge base tree when falling
314 * back on 3-way merge.
316 * (2) mode SP type SP sha1 TAB path
317 * The second format is to stuff git-ls-tree output
318 * into the index file.
320 * (3) mode SP sha1 SP stage TAB path
321 * This format is to put higher order stages into the
322 * index file and matches git-ls-files --stage output.
324 read_line(&buf
, stdin
, line_termination
);
328 mode
= strtoul(buf
.buf
, &ptr
, 8);
329 if (ptr
== buf
.buf
|| *ptr
!= ' ')
332 tab
= strchr(ptr
, '\t');
333 if (!tab
|| tab
- ptr
< 41)
336 if (tab
[-2] == ' ' && '0' <= tab
[-1] && tab
[-1] <= '3') {
337 stage
= tab
[-1] - '0';
338 ptr
= tab
+ 1; /* point at the head of path */
339 tab
= tab
- 2; /* point at tail of sha1 */
343 ptr
= tab
+ 1; /* point at the head of path */
346 if (get_sha1_hex(tab
- 40, sha1
) || tab
[-41] != ' ')
349 if (line_termination
&& ptr
[0] == '"')
350 path_name
= unquote_c_style(ptr
, NULL
);
354 if (!verify_path(path_name
)) {
355 fprintf(stderr
, "Ignoring path %s\n", path_name
);
356 if (path_name
!= ptr
)
360 cache_tree_invalidate_path(active_cache_tree
, path_name
);
363 /* mode == 0 means there is no such path -- remove */
364 if (remove_file_from_cache(path_name
))
365 die("git-update-index: unable to remove %s",
369 /* mode ' ' sha1 '\t' name
370 * ptr[-1] points at tab,
371 * ptr[-41] is at the beginning of sha1
373 ptr
[-42] = ptr
[-1] = 0;
374 if (add_cacheinfo(mode
, sha1
, path_name
, stage
))
375 die("git-update-index: unable to update %s",
378 if (path_name
!= ptr
)
383 die("malformed index info %s", buf
.buf
);
387 static const char update_index_usage
[] =
388 "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>...";
390 static unsigned char head_sha1
[20];
391 static unsigned char merge_head_sha1
[20];
393 static struct cache_entry
*read_one_ent(const char *which
,
394 unsigned char *ent
, const char *path
,
395 int namelen
, int stage
)
398 unsigned char sha1
[20];
400 struct cache_entry
*ce
;
402 if (get_tree_entry(ent
, path
, sha1
, &mode
)) {
404 error("%s: not in %s branch.", path
, which
);
407 if (mode
== S_IFDIR
) {
409 error("%s: not a blob in %s branch.", path
, which
);
412 size
= cache_entry_size(namelen
);
413 ce
= xcalloc(1, size
);
415 hashcpy(ce
->sha1
, sha1
);
416 memcpy(ce
->name
, path
, namelen
);
417 ce
->ce_flags
= create_ce_flags(namelen
, stage
);
418 ce
->ce_mode
= create_ce_mode(mode
);
422 static int unresolve_one(const char *path
)
424 int namelen
= strlen(path
);
427 struct cache_entry
*ce_2
= NULL
, *ce_3
= NULL
;
429 /* See if there is such entry in the index. */
430 pos
= cache_name_pos(path
, namelen
);
432 /* If there isn't, either it is unmerged, or
433 * resolved as "removed" by mistake. We do not
434 * want to do anything in the former case.
437 if (pos
< active_nr
) {
438 struct cache_entry
*ce
= active_cache
[pos
];
439 if (ce_namelen(ce
) == namelen
&&
440 !memcmp(ce
->name
, path
, namelen
)) {
442 "%s: skipping still unmerged path.\n",
449 /* Grab blobs from given path from HEAD and MERGE_HEAD,
450 * stuff HEAD version in stage #2,
451 * stuff MERGE_HEAD version in stage #3.
453 ce_2
= read_one_ent("our", head_sha1
, path
, namelen
, 2);
454 ce_3
= read_one_ent("their", merge_head_sha1
, path
, namelen
, 3);
456 if (!ce_2
|| !ce_3
) {
460 if (!hashcmp(ce_2
->sha1
, ce_3
->sha1
) &&
461 ce_2
->ce_mode
== ce_3
->ce_mode
) {
462 fprintf(stderr
, "%s: identical in both, skipping.\n",
467 cache_tree_invalidate_path(active_cache_tree
, path
);
468 remove_file_from_cache(path
);
469 if (add_cache_entry(ce_2
, ADD_CACHE_OK_TO_ADD
)) {
470 error("%s: cannot add our version to the index.", path
);
474 if (!add_cache_entry(ce_3
, ADD_CACHE_OK_TO_ADD
))
476 error("%s: cannot add their version to the index.", path
);
484 static void read_head_pointers(void)
486 if (read_ref("HEAD", head_sha1
))
487 die("No HEAD -- no initial commit yet?\n");
488 if (read_ref("MERGE_HEAD", merge_head_sha1
)) {
489 fprintf(stderr
, "Not in the middle of a merge.\n");
494 static int do_unresolve(int ac
, const char **av
,
495 const char *prefix
, int prefix_length
)
500 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
501 * are not doing a merge, so exit with success status.
503 read_head_pointers();
505 for (i
= 1; i
< ac
; i
++) {
506 const char *arg
= av
[i
];
507 const char *p
= prefix_path(prefix
, prefix_length
, arg
);
508 err
|= unresolve_one(p
);
509 if (p
< arg
|| p
> arg
+ strlen(arg
))
515 static int do_reupdate(int ac
, const char **av
,
516 const char *prefix
, int prefix_length
)
518 /* Read HEAD and run update-index on paths that are
519 * merged and already different between index and HEAD.
523 const char **pathspec
= get_pathspec(prefix
, av
+ 1);
525 if (read_ref("HEAD", head_sha1
))
526 /* If there is no HEAD, that means it is an initial
527 * commit. Update everything in the index.
531 for (pos
= 0; pos
< active_nr
; pos
++) {
532 struct cache_entry
*ce
= active_cache
[pos
];
533 struct cache_entry
*old
= NULL
;
536 if (ce_stage(ce
) || !ce_path_match(ce
, pathspec
))
539 old
= read_one_ent(NULL
, head_sha1
,
540 ce
->name
, ce_namelen(ce
), 0);
541 if (old
&& ce
->ce_mode
== old
->ce_mode
&&
542 !hashcmp(ce
->sha1
, old
->sha1
)) {
544 continue; /* unchanged */
546 /* Be careful. The working tree may not have the
547 * path anymore, in which case, under 'allow_remove',
548 * or worse yet 'allow_replace', active_nr may decrease.
551 update_one(ce
->name
+ prefix_length
, prefix
, prefix_length
);
552 if (save_nr
!= active_nr
)
558 int cmd_update_index(int argc
, const char **argv
, const char *prefix
)
560 int i
, newfd
, entries
, has_errors
= 0, line_termination
= '\n';
561 int allow_options
= 1;
562 int read_from_stdin
= 0;
563 int prefix_length
= prefix
? strlen(prefix
) : 0;
564 char set_executable_bit
= 0;
565 unsigned int refresh_flags
= 0;
567 struct lock_file
*lock_file
;
569 git_config(git_default_config
);
571 /* We can't free this memory, it becomes part of a linked list parsed atexit() */
572 lock_file
= xcalloc(1, sizeof(struct lock_file
));
574 newfd
= hold_locked_index(lock_file
, 0);
578 entries
= read_cache();
580 die("cache corrupted");
582 for (i
= 1 ; i
< argc
; i
++) {
583 const char *path
= argv
[i
];
586 if (allow_options
&& *path
== '-') {
587 if (!strcmp(path
, "--")) {
591 if (!strcmp(path
, "-q")) {
592 refresh_flags
|= REFRESH_QUIET
;
595 if (!strcmp(path
, "--add")) {
599 if (!strcmp(path
, "--replace")) {
603 if (!strcmp(path
, "--remove")) {
607 if (!strcmp(path
, "--unmerged")) {
608 refresh_flags
|= REFRESH_UNMERGED
;
611 if (!strcmp(path
, "--refresh")) {
612 has_errors
|= refresh_cache(refresh_flags
);
615 if (!strcmp(path
, "--really-refresh")) {
616 has_errors
|= refresh_cache(REFRESH_REALLY
| refresh_flags
);
619 if (!strcmp(path
, "--cacheinfo")) {
620 unsigned char sha1
[20];
624 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
626 if ((sscanf(argv
[i
+1], "%o", &mode
) != 1) ||
627 get_sha1_hex(argv
[i
+2], sha1
) ||
628 add_cacheinfo(mode
, sha1
, argv
[i
+3], 0))
629 die("git-update-index: --cacheinfo"
630 " cannot add %s", argv
[i
+3]);
634 if (!strcmp(path
, "--chmod=-x") ||
635 !strcmp(path
, "--chmod=+x")) {
637 die("git-update-index: %s <path>", path
);
638 set_executable_bit
= path
[8];
641 if (!strcmp(path
, "--assume-unchanged")) {
642 mark_valid_only
= MARK_VALID
;
645 if (!strcmp(path
, "--no-assume-unchanged")) {
646 mark_valid_only
= UNMARK_VALID
;
649 if (!strcmp(path
, "--info-only")) {
653 if (!strcmp(path
, "--force-remove")) {
657 if (!strcmp(path
, "-z")) {
658 line_termination
= 0;
661 if (!strcmp(path
, "--stdin")) {
663 die("--stdin must be at the end");
667 if (!strcmp(path
, "--index-info")) {
669 die("--index-info must be at the end");
670 allow_add
= allow_replace
= allow_remove
= 1;
671 read_index_info(line_termination
);
674 if (!strcmp(path
, "--unresolve")) {
675 has_errors
= do_unresolve(argc
- i
, argv
+ i
,
676 prefix
, prefix_length
);
678 active_cache_changed
= 0;
681 if (!strcmp(path
, "--again") || !strcmp(path
, "-g")) {
682 has_errors
= do_reupdate(argc
- i
, argv
+ i
,
683 prefix
, prefix_length
);
685 active_cache_changed
= 0;
688 if (!strcmp(path
, "--ignore-missing")) {
689 refresh_flags
|= REFRESH_IGNORE_MISSING
;
692 if (!strcmp(path
, "--verbose")) {
696 if (!strcmp(path
, "-h") || !strcmp(path
, "--help"))
697 usage(update_index_usage
);
698 die("unknown option %s", path
);
700 p
= prefix_path(prefix
, prefix_length
, path
);
701 update_one(p
, NULL
, 0);
702 if (set_executable_bit
)
703 chmod_path(set_executable_bit
, p
);
704 if (p
< path
|| p
> path
+ strlen(path
))
707 if (read_from_stdin
) {
713 read_line(&buf
, stdin
, line_termination
);
716 if (line_termination
&& buf
.buf
[0] == '"')
717 path_name
= unquote_c_style(buf
.buf
, NULL
);
720 p
= prefix_path(prefix
, prefix_length
, path_name
);
721 update_one(p
, NULL
, 0);
722 if (set_executable_bit
)
723 chmod_path(set_executable_bit
, p
);
724 if (p
< path_name
|| p
> path_name
+ strlen(path_name
))
726 if (path_name
!= buf
.buf
)
732 if (active_cache_changed
) {
734 if (refresh_flags
& REFRESH_QUIET
)
736 die("unable to create '%s.lock': %s",
737 get_index_file(), strerror(lock_error
));
739 if (write_cache(newfd
, active_cache
, active_nr
) ||
740 close(newfd
) || commit_locked_index(lock_file
))
741 die("Unable to write new index file");
744 rollback_lock_file(lock_file
);
746 return has_errors
? 1 : 0;