2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
9 #include "cache-tree.h"
10 #include "tree-walk.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
|= htons(CE_VALID
);
53 active_cache
[pos
]->ce_flags
&= ~htons(CE_VALID
);
56 cache_tree_invalidate_path(active_cache_tree
, path
);
57 active_cache_changed
= 1;
63 static int process_file(const char *path
)
65 int size
, namelen
, option
, status
;
66 struct cache_entry
*ce
;
69 status
= lstat(path
, &st
);
71 /* We probably want to do this in remove_file_from_cache() and
72 * add_cache_entry() instead...
74 cache_tree_invalidate_path(active_cache_tree
, path
);
76 if (status
< 0 || S_ISDIR(st
.st_mode
)) {
77 /* When we used to have "path" and now we want to add
78 * "path/file", we need a way to remove "path" before
79 * being able to add "path/file". However,
80 * "git-update-index --remove path" would not work.
81 * --force-remove can be used but this is more user
82 * friendly, especially since we can do the opposite
83 * case just fine without --force-remove.
85 if (status
== 0 || (errno
== ENOENT
|| errno
== ENOTDIR
)) {
87 if (remove_file_from_cache(path
))
88 return error("%s: cannot remove from the index",
92 } else if (status
< 0) {
93 return error("%s: does not exist and --remove not passed",
98 return error("%s: is a directory - add files inside instead",
101 return error("lstat(\"%s\"): %s", path
,
105 namelen
= strlen(path
);
106 size
= cache_entry_size(namelen
);
107 ce
= xcalloc(1, size
);
108 memcpy(ce
->name
, path
, namelen
);
109 ce
->ce_flags
= htons(namelen
);
110 fill_stat_cache_info(ce
, &st
);
112 if (trust_executable_bit
&& has_symlinks
)
113 ce
->ce_mode
= create_ce_mode(st
.st_mode
);
115 /* If there is an existing entry, pick the mode bits and type
116 * from it, otherwise assume unexecutable regular file.
118 struct cache_entry
*ent
;
119 int pos
= cache_name_pos(path
, namelen
);
121 ent
= (0 <= pos
) ? active_cache
[pos
] : NULL
;
122 ce
->ce_mode
= ce_mode_from_stat(ent
, st
.st_mode
);
125 if (index_path(ce
->sha1
, path
, &st
, !info_only
))
127 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
128 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
129 if (add_cache_entry(ce
, option
))
130 return error("%s: cannot add to the index - missing --add option?",
135 static int add_cacheinfo(unsigned int mode
, const unsigned char *sha1
,
136 const char *path
, int stage
)
138 int size
, len
, option
;
139 struct cache_entry
*ce
;
141 if (!verify_path(path
))
145 size
= cache_entry_size(len
);
146 ce
= xcalloc(1, size
);
148 hashcpy(ce
->sha1
, sha1
);
149 memcpy(ce
->name
, path
, len
);
150 ce
->ce_flags
= create_ce_flags(len
, stage
);
151 ce
->ce_mode
= create_ce_mode(mode
);
152 if (assume_unchanged
)
153 ce
->ce_flags
|= htons(CE_VALID
);
154 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
155 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
156 if (add_cache_entry(ce
, option
))
157 return error("%s: cannot add to the index - missing --add option?",
159 report("add '%s'", path
);
160 cache_tree_invalidate_path(active_cache_tree
, path
);
164 static void chmod_path(int flip
, const char *path
)
167 struct cache_entry
*ce
;
170 pos
= cache_name_pos(path
, strlen(path
));
173 ce
= active_cache
[pos
];
174 mode
= ntohl(ce
->ce_mode
);
179 ce
->ce_mode
|= htonl(0111); break;
181 ce
->ce_mode
&= htonl(~0111); break;
185 cache_tree_invalidate_path(active_cache_tree
, path
);
186 active_cache_changed
= 1;
187 report("chmod %cx '%s'", flip
, path
);
190 die("git-update-index: cannot chmod %cx '%s'", flip
, path
);
193 static void update_one(const char *path
, const char *prefix
, int prefix_length
)
195 const char *p
= prefix_path(prefix
, prefix_length
, path
);
196 if (!verify_path(p
)) {
197 fprintf(stderr
, "Ignoring path %s\n", path
);
200 if (mark_valid_only
) {
202 die("Unable to mark file %s", path
);
205 cache_tree_invalidate_path(active_cache_tree
, path
);
208 if (remove_file_from_cache(p
))
209 die("git-update-index: unable to remove %s", path
);
210 report("remove '%s'", path
);
214 die("Unable to process file %s", path
);
215 report("add '%s'", path
);
217 if (p
< path
|| p
> path
+ strlen(path
))
221 static void read_index_info(int line_termination
)
228 unsigned char sha1
[20];
233 /* This reads lines formatted in one of three formats:
235 * (1) mode SP sha1 TAB path
236 * The first format is what "git-apply --index-info"
237 * reports, and used to reconstruct a partial tree
238 * that is used for phony merge base tree when falling
239 * back on 3-way merge.
241 * (2) mode SP type SP sha1 TAB path
242 * The second format is to stuff git-ls-tree output
243 * into the index file.
245 * (3) mode SP sha1 SP stage TAB path
246 * This format is to put higher order stages into the
247 * index file and matches git-ls-files --stage output.
249 read_line(&buf
, stdin
, line_termination
);
254 ul
= strtoul(buf
.buf
, &ptr
, 8);
255 if (ptr
== buf
.buf
|| *ptr
!= ' '
256 || errno
|| (unsigned int) ul
!= ul
)
260 tab
= strchr(ptr
, '\t');
261 if (!tab
|| tab
- ptr
< 41)
264 if (tab
[-2] == ' ' && '0' <= tab
[-1] && tab
[-1] <= '3') {
265 stage
= tab
[-1] - '0';
266 ptr
= tab
+ 1; /* point at the head of path */
267 tab
= tab
- 2; /* point at tail of sha1 */
271 ptr
= tab
+ 1; /* point at the head of path */
274 if (get_sha1_hex(tab
- 40, sha1
) || tab
[-41] != ' ')
277 if (line_termination
&& ptr
[0] == '"')
278 path_name
= unquote_c_style(ptr
, NULL
);
282 if (!verify_path(path_name
)) {
283 fprintf(stderr
, "Ignoring path %s\n", path_name
);
284 if (path_name
!= ptr
)
288 cache_tree_invalidate_path(active_cache_tree
, path_name
);
291 /* mode == 0 means there is no such path -- remove */
292 if (remove_file_from_cache(path_name
))
293 die("git-update-index: unable to remove %s",
297 /* mode ' ' sha1 '\t' name
298 * ptr[-1] points at tab,
299 * ptr[-41] is at the beginning of sha1
301 ptr
[-42] = ptr
[-1] = 0;
302 if (add_cacheinfo(mode
, sha1
, path_name
, stage
))
303 die("git-update-index: unable to update %s",
306 if (path_name
!= ptr
)
311 die("malformed index info %s", buf
.buf
);
315 static const char update_index_usage
[] =
316 "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>...";
318 static unsigned char head_sha1
[20];
319 static unsigned char merge_head_sha1
[20];
321 static struct cache_entry
*read_one_ent(const char *which
,
322 unsigned char *ent
, const char *path
,
323 int namelen
, int stage
)
326 unsigned char sha1
[20];
328 struct cache_entry
*ce
;
330 if (get_tree_entry(ent
, path
, sha1
, &mode
)) {
332 error("%s: not in %s branch.", path
, which
);
335 if (mode
== S_IFDIR
) {
337 error("%s: not a blob in %s branch.", path
, which
);
340 size
= cache_entry_size(namelen
);
341 ce
= xcalloc(1, size
);
343 hashcpy(ce
->sha1
, sha1
);
344 memcpy(ce
->name
, path
, namelen
);
345 ce
->ce_flags
= create_ce_flags(namelen
, stage
);
346 ce
->ce_mode
= create_ce_mode(mode
);
350 static int unresolve_one(const char *path
)
352 int namelen
= strlen(path
);
355 struct cache_entry
*ce_2
= NULL
, *ce_3
= NULL
;
357 /* See if there is such entry in the index. */
358 pos
= cache_name_pos(path
, namelen
);
360 /* If there isn't, either it is unmerged, or
361 * resolved as "removed" by mistake. We do not
362 * want to do anything in the former case.
365 if (pos
< active_nr
) {
366 struct cache_entry
*ce
= active_cache
[pos
];
367 if (ce_namelen(ce
) == namelen
&&
368 !memcmp(ce
->name
, path
, namelen
)) {
370 "%s: skipping still unmerged path.\n",
377 /* Grab blobs from given path from HEAD and MERGE_HEAD,
378 * stuff HEAD version in stage #2,
379 * stuff MERGE_HEAD version in stage #3.
381 ce_2
= read_one_ent("our", head_sha1
, path
, namelen
, 2);
382 ce_3
= read_one_ent("their", merge_head_sha1
, path
, namelen
, 3);
384 if (!ce_2
|| !ce_3
) {
388 if (!hashcmp(ce_2
->sha1
, ce_3
->sha1
) &&
389 ce_2
->ce_mode
== ce_3
->ce_mode
) {
390 fprintf(stderr
, "%s: identical in both, skipping.\n",
395 cache_tree_invalidate_path(active_cache_tree
, path
);
396 remove_file_from_cache(path
);
397 if (add_cache_entry(ce_2
, ADD_CACHE_OK_TO_ADD
)) {
398 error("%s: cannot add our version to the index.", path
);
402 if (!add_cache_entry(ce_3
, ADD_CACHE_OK_TO_ADD
))
404 error("%s: cannot add their version to the index.", path
);
412 static void read_head_pointers(void)
414 if (read_ref("HEAD", head_sha1
))
415 die("No HEAD -- no initial commit yet?\n");
416 if (read_ref("MERGE_HEAD", merge_head_sha1
)) {
417 fprintf(stderr
, "Not in the middle of a merge.\n");
422 static int do_unresolve(int ac
, const char **av
,
423 const char *prefix
, int prefix_length
)
428 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
429 * are not doing a merge, so exit with success status.
431 read_head_pointers();
433 for (i
= 1; i
< ac
; i
++) {
434 const char *arg
= av
[i
];
435 const char *p
= prefix_path(prefix
, prefix_length
, arg
);
436 err
|= unresolve_one(p
);
437 if (p
< arg
|| p
> arg
+ strlen(arg
))
443 static int do_reupdate(int ac
, const char **av
,
444 const char *prefix
, int prefix_length
)
446 /* Read HEAD and run update-index on paths that are
447 * merged and already different between index and HEAD.
451 const char **pathspec
= get_pathspec(prefix
, av
+ 1);
453 if (read_ref("HEAD", head_sha1
))
454 /* If there is no HEAD, that means it is an initial
455 * commit. Update everything in the index.
459 for (pos
= 0; pos
< active_nr
; pos
++) {
460 struct cache_entry
*ce
= active_cache
[pos
];
461 struct cache_entry
*old
= NULL
;
464 if (ce_stage(ce
) || !ce_path_match(ce
, pathspec
))
467 old
= read_one_ent(NULL
, head_sha1
,
468 ce
->name
, ce_namelen(ce
), 0);
469 if (old
&& ce
->ce_mode
== old
->ce_mode
&&
470 !hashcmp(ce
->sha1
, old
->sha1
)) {
472 continue; /* unchanged */
474 /* Be careful. The working tree may not have the
475 * path anymore, in which case, under 'allow_remove',
476 * or worse yet 'allow_replace', active_nr may decrease.
479 update_one(ce
->name
+ prefix_length
, prefix
, prefix_length
);
480 if (save_nr
!= active_nr
)
486 int cmd_update_index(int argc
, const char **argv
, const char *prefix
)
488 int i
, newfd
, entries
, has_errors
= 0, line_termination
= '\n';
489 int allow_options
= 1;
490 int read_from_stdin
= 0;
491 int prefix_length
= prefix
? strlen(prefix
) : 0;
492 char set_executable_bit
= 0;
493 unsigned int refresh_flags
= 0;
495 struct lock_file
*lock_file
;
497 git_config(git_default_config
);
499 /* We can't free this memory, it becomes part of a linked list parsed atexit() */
500 lock_file
= xcalloc(1, sizeof(struct lock_file
));
502 newfd
= hold_locked_index(lock_file
, 0);
506 entries
= read_cache();
508 die("cache corrupted");
510 for (i
= 1 ; i
< argc
; i
++) {
511 const char *path
= argv
[i
];
514 if (allow_options
&& *path
== '-') {
515 if (!strcmp(path
, "--")) {
519 if (!strcmp(path
, "-q")) {
520 refresh_flags
|= REFRESH_QUIET
;
523 if (!strcmp(path
, "--add")) {
527 if (!strcmp(path
, "--replace")) {
531 if (!strcmp(path
, "--remove")) {
535 if (!strcmp(path
, "--unmerged")) {
536 refresh_flags
|= REFRESH_UNMERGED
;
539 if (!strcmp(path
, "--refresh")) {
540 has_errors
|= refresh_cache(refresh_flags
);
543 if (!strcmp(path
, "--really-refresh")) {
544 has_errors
|= refresh_cache(REFRESH_REALLY
| refresh_flags
);
547 if (!strcmp(path
, "--cacheinfo")) {
548 unsigned char sha1
[20];
552 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
554 if (strtoul_ui(argv
[i
+1], 8, &mode
) ||
555 get_sha1_hex(argv
[i
+2], sha1
) ||
556 add_cacheinfo(mode
, sha1
, argv
[i
+3], 0))
557 die("git-update-index: --cacheinfo"
558 " cannot add %s", argv
[i
+3]);
562 if (!strcmp(path
, "--chmod=-x") ||
563 !strcmp(path
, "--chmod=+x")) {
565 die("git-update-index: %s <path>", path
);
566 set_executable_bit
= path
[8];
569 if (!strcmp(path
, "--assume-unchanged")) {
570 mark_valid_only
= MARK_VALID
;
573 if (!strcmp(path
, "--no-assume-unchanged")) {
574 mark_valid_only
= UNMARK_VALID
;
577 if (!strcmp(path
, "--info-only")) {
581 if (!strcmp(path
, "--force-remove")) {
585 if (!strcmp(path
, "-z")) {
586 line_termination
= 0;
589 if (!strcmp(path
, "--stdin")) {
591 die("--stdin must be at the end");
595 if (!strcmp(path
, "--index-info")) {
597 die("--index-info must be at the end");
598 allow_add
= allow_replace
= allow_remove
= 1;
599 read_index_info(line_termination
);
602 if (!strcmp(path
, "--unresolve")) {
603 has_errors
= do_unresolve(argc
- i
, argv
+ i
,
604 prefix
, prefix_length
);
606 active_cache_changed
= 0;
609 if (!strcmp(path
, "--again") || !strcmp(path
, "-g")) {
610 has_errors
= do_reupdate(argc
- i
, argv
+ i
,
611 prefix
, prefix_length
);
613 active_cache_changed
= 0;
616 if (!strcmp(path
, "--ignore-missing")) {
617 refresh_flags
|= REFRESH_IGNORE_MISSING
;
620 if (!strcmp(path
, "--verbose")) {
624 if (!strcmp(path
, "-h") || !strcmp(path
, "--help"))
625 usage(update_index_usage
);
626 die("unknown option %s", path
);
628 p
= prefix_path(prefix
, prefix_length
, path
);
629 update_one(p
, NULL
, 0);
630 if (set_executable_bit
)
631 chmod_path(set_executable_bit
, p
);
632 if (p
< path
|| p
> path
+ strlen(path
))
635 if (read_from_stdin
) {
641 read_line(&buf
, stdin
, line_termination
);
644 if (line_termination
&& buf
.buf
[0] == '"')
645 path_name
= unquote_c_style(buf
.buf
, NULL
);
648 p
= prefix_path(prefix
, prefix_length
, path_name
);
649 update_one(p
, NULL
, 0);
650 if (set_executable_bit
)
651 chmod_path(set_executable_bit
, p
);
652 if (p
< path_name
|| p
> path_name
+ strlen(path_name
))
654 if (path_name
!= buf
.buf
)
660 if (active_cache_changed
) {
662 if (refresh_flags
& REFRESH_QUIET
)
664 die("unable to create '%s.lock': %s",
665 get_index_file(), strerror(lock_error
));
667 if (write_cache(newfd
, active_cache
, active_nr
) ||
668 close(newfd
) || commit_locked_index(lock_file
))
669 die("Unable to write new index file");
672 rollback_lock_file(lock_file
);
674 return has_errors
? 1 : 0;