2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
9 #include "cache-tree.h"
12 * Default to not allowing changes to the list of files. The
13 * tool doesn't actually care, but this makes it harder to add
14 * files to the revision control by mistake by doing something
15 * like "git-update-index *" and suddenly having all the object
16 * files be revision controlled.
19 static int allow_remove
;
20 static int allow_replace
;
21 static int allow_unmerged
; /* --refresh needing merge is not error */
22 static int not_new
; /* --refresh not having working tree files is not error */
23 static int quiet
; /* --refresh needing update is not error */
25 static int force_remove
;
27 static int mark_valid_only
= 0;
29 #define UNMARK_VALID 2
32 /* Three functions to allow overloaded pointer return; see linux/err.h */
33 static inline void *ERR_PTR(long error
)
35 return (void *) error
;
38 static inline long PTR_ERR(const void *ptr
)
43 static inline long IS_ERR(const void *ptr
)
45 return (unsigned long)ptr
> (unsigned long)-1000L;
48 static void report(const char *fmt
, ...)
61 static int mark_valid(const char *path
)
63 int namelen
= strlen(path
);
64 int pos
= cache_name_pos(path
, namelen
);
66 switch (mark_valid_only
) {
68 active_cache
[pos
]->ce_flags
|= htons(CE_VALID
);
71 active_cache
[pos
]->ce_flags
&= ~htons(CE_VALID
);
74 cache_tree_invalidate_path(active_cache_tree
, path
);
75 active_cache_changed
= 1;
81 static int add_file_to_cache(const char *path
)
83 int size
, namelen
, option
, status
;
84 struct cache_entry
*ce
;
87 status
= lstat(path
, &st
);
89 /* We probably want to do this in remove_file_from_cache() and
90 * add_cache_entry() instead...
92 cache_tree_invalidate_path(active_cache_tree
, path
);
94 if (status
< 0 || S_ISDIR(st
.st_mode
)) {
95 /* When we used to have "path" and now we want to add
96 * "path/file", we need a way to remove "path" before
97 * being able to add "path/file". However,
98 * "git-update-index --remove path" would not work.
99 * --force-remove can be used but this is more user
100 * friendly, especially since we can do the opposite
101 * case just fine without --force-remove.
103 if (status
== 0 || (errno
== ENOENT
|| errno
== ENOTDIR
)) {
105 if (remove_file_from_cache(path
))
106 return error("%s: cannot remove from the index",
110 } else if (status
< 0) {
111 return error("%s: does not exist and --remove not passed",
116 return error("%s: is a directory - add files inside instead",
119 return error("lstat(\"%s\"): %s", path
,
123 namelen
= strlen(path
);
124 size
= cache_entry_size(namelen
);
125 ce
= xcalloc(1, size
);
126 memcpy(ce
->name
, path
, namelen
);
127 ce
->ce_flags
= htons(namelen
);
128 fill_stat_cache_info(ce
, &st
);
130 ce
->ce_mode
= create_ce_mode(st
.st_mode
);
131 if (!trust_executable_bit
) {
132 /* If there is an existing entry, pick the mode bits
135 int pos
= cache_name_pos(path
, namelen
);
137 ce
->ce_mode
= active_cache
[pos
]->ce_mode
;
140 if (index_path(ce
->sha1
, path
, &st
, !info_only
))
142 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
143 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
144 if (add_cache_entry(ce
, option
))
145 return error("%s: cannot add to the index - missing --add option?",
151 * "refresh" does not calculate a new sha1 file or bring the
152 * cache up-to-date for mode/content changes. But what it
153 * _does_ do is to "re-match" the stat information of a file
154 * with the cache, so that you can refresh the cache for a
155 * file that hasn't been changed but where the stat entry is
158 * For example, you'd want to do this after doing a "git-read-tree",
159 * to link up the stat cache details with the proper files.
161 static struct cache_entry
*refresh_entry(struct cache_entry
*ce
, int really
)
164 struct cache_entry
*updated
;
167 if (lstat(ce
->name
, &st
) < 0)
168 return ERR_PTR(-errno
);
170 changed
= ce_match_stat(ce
, &st
, really
);
172 if (really
&& assume_unchanged
&&
173 !(ce
->ce_flags
& htons(CE_VALID
)))
174 ; /* mark this one VALID again */
179 if (ce_modified(ce
, &st
, really
))
180 return ERR_PTR(-EINVAL
);
183 updated
= xmalloc(size
);
184 memcpy(updated
, ce
, size
);
185 fill_stat_cache_info(updated
, &st
);
187 /* In this case, if really is not set, we should leave
188 * CE_VALID bit alone. Otherwise, paths marked with
189 * --no-assume-unchanged (i.e. things to be edited) will
190 * reacquire CE_VALID bit automatically, which is not
191 * really what we want.
193 if (!really
&& assume_unchanged
&& !(ce
->ce_flags
& htons(CE_VALID
)))
194 updated
->ce_flags
&= ~htons(CE_VALID
);
199 static int refresh_cache(int really
)
204 for (i
= 0; i
< active_nr
; i
++) {
205 struct cache_entry
*ce
, *new;
206 ce
= active_cache
[i
];
208 while ((i
< active_nr
) &&
209 ! strcmp(active_cache
[i
]->name
, ce
->name
))
214 printf("%s: needs merge\n", ce
->name
);
219 new = refresh_entry(ce
, really
);
223 if (not_new
&& PTR_ERR(new) == -ENOENT
)
225 if (really
&& PTR_ERR(new) == -EINVAL
) {
226 /* If we are doing --really-refresh that
227 * means the index is not valid anymore.
229 ce
->ce_flags
&= ~htons(CE_VALID
);
230 active_cache_changed
= 1;
234 printf("%s: needs update\n", ce
->name
);
238 active_cache_changed
= 1;
239 /* You can NOT just free active_cache[i] here, since it
240 * might not be necessarily malloc()ed but can also come
242 active_cache
[i
] = new;
248 * We fundamentally don't like some paths: we don't want
249 * dot or dot-dot anywhere, and for obvious reasons don't
250 * want to recurse into ".git" either.
252 * Also, we don't want double slashes or slashes at the
253 * end that can make pathnames ambiguous.
255 static int verify_dotfile(const char *rest
)
258 * The first character was '.', but that
259 * has already been discarded, we now test
263 /* "." is not allowed */
268 * ".git" followed by NUL or slash is bad. This
269 * shares the path end test with the ".." case.
279 if (rest
[1] == '\0' || rest
[1] == '/')
285 static int verify_path(const char *path
)
302 if (verify_dotfile(path
))
311 static int add_cacheinfo(unsigned int mode
, const unsigned char *sha1
,
312 const char *path
, int stage
)
314 int size
, len
, option
;
315 struct cache_entry
*ce
;
317 if (!verify_path(path
))
321 size
= cache_entry_size(len
);
322 ce
= xcalloc(1, size
);
324 memcpy(ce
->sha1
, sha1
, 20);
325 memcpy(ce
->name
, path
, len
);
326 ce
->ce_flags
= create_ce_flags(len
, stage
);
327 ce
->ce_mode
= create_ce_mode(mode
);
328 if (assume_unchanged
)
329 ce
->ce_flags
|= htons(CE_VALID
);
330 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
331 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
332 if (add_cache_entry(ce
, option
))
333 return error("%s: cannot add to the index - missing --add option?",
335 report("add '%s'", path
);
336 cache_tree_invalidate_path(active_cache_tree
, path
);
340 static int chmod_path(int flip
, const char *path
)
343 struct cache_entry
*ce
;
346 pos
= cache_name_pos(path
, strlen(path
));
349 ce
= active_cache
[pos
];
350 mode
= ntohl(ce
->ce_mode
);
355 ce
->ce_mode
|= htonl(0111); break;
357 ce
->ce_mode
&= htonl(~0111); break;
361 cache_tree_invalidate_path(active_cache_tree
, path
);
362 active_cache_changed
= 1;
366 static struct cache_file cache_file
;
368 static void update_one(const char *path
, const char *prefix
, int prefix_length
)
370 const char *p
= prefix_path(prefix
, prefix_length
, path
);
371 if (!verify_path(p
)) {
372 fprintf(stderr
, "Ignoring path %s\n", path
);
375 if (mark_valid_only
) {
377 die("Unable to mark file %s", path
);
380 cache_tree_invalidate_path(active_cache_tree
, path
);
383 if (remove_file_from_cache(p
))
384 die("git-update-index: unable to remove %s", path
);
385 report("remove '%s'", path
);
388 if (add_file_to_cache(p
))
389 die("Unable to process file %s", path
);
390 report("add '%s'", path
);
393 static void read_index_info(int line_termination
)
400 unsigned char sha1
[20];
404 /* This reads lines formatted in one of three formats:
406 * (1) mode SP sha1 TAB path
407 * The first format is what "git-apply --index-info"
408 * reports, and used to reconstruct a partial tree
409 * that is used for phony merge base tree when falling
410 * back on 3-way merge.
412 * (2) mode SP type SP sha1 TAB path
413 * The second format is to stuff git-ls-tree output
414 * into the index file.
416 * (3) mode SP sha1 SP stage TAB path
417 * This format is to put higher order stages into the
418 * index file and matches git-ls-files --stage output.
420 read_line(&buf
, stdin
, line_termination
);
424 mode
= strtoul(buf
.buf
, &ptr
, 8);
425 if (ptr
== buf
.buf
|| *ptr
!= ' ')
428 tab
= strchr(ptr
, '\t');
429 if (!tab
|| tab
- ptr
< 41)
432 if (tab
[-2] == ' ' && '0' <= tab
[-1] && tab
[-1] <= '3') {
433 stage
= tab
[-1] - '0';
434 ptr
= tab
+ 1; /* point at the head of path */
435 tab
= tab
- 2; /* point at tail of sha1 */
439 ptr
= tab
+ 1; /* point at the head of path */
442 if (get_sha1_hex(tab
- 40, sha1
) || tab
[-41] != ' ')
445 if (line_termination
&& ptr
[0] == '"')
446 path_name
= unquote_c_style(ptr
, NULL
);
450 if (!verify_path(path_name
)) {
451 fprintf(stderr
, "Ignoring path %s\n", path_name
);
452 if (path_name
!= ptr
)
456 cache_tree_invalidate_path(active_cache_tree
, path_name
);
459 /* mode == 0 means there is no such path -- remove */
460 if (remove_file_from_cache(path_name
))
461 die("git-update-index: unable to remove %s",
465 /* mode ' ' sha1 '\t' name
466 * ptr[-1] points at tab,
467 * ptr[-41] is at the beginning of sha1
469 ptr
[-42] = ptr
[-1] = 0;
470 if (add_cacheinfo(mode
, sha1
, path_name
, stage
))
471 die("git-update-index: unable to update %s",
474 if (path_name
!= ptr
)
479 die("malformed index info %s", buf
.buf
);
483 static const char update_index_usage
[] =
484 "git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--cacheinfo] [--chmod=(+|-)x] [--info-only] [--force-remove] [--stdin] [--index-info] [--ignore-missing] [-z] [--verbose] [--] <file>...";
486 int main(int argc
, const char **argv
)
488 int i
, newfd
, entries
, has_errors
= 0, line_termination
= '\n';
489 int allow_options
= 1;
490 int read_from_stdin
= 0;
491 const char *prefix
= setup_git_directory();
492 int prefix_length
= prefix
? strlen(prefix
) : 0;
494 git_config(git_default_config
);
496 newfd
= hold_index_file_for_update(&cache_file
, get_index_file());
498 die("unable to create new cachefile");
500 entries
= read_cache();
502 die("cache corrupted");
504 for (i
= 1 ; i
< argc
; i
++) {
505 const char *path
= argv
[i
];
507 if (allow_options
&& *path
== '-') {
508 if (!strcmp(path
, "--")) {
512 if (!strcmp(path
, "-q")) {
516 if (!strcmp(path
, "--add")) {
520 if (!strcmp(path
, "--replace")) {
524 if (!strcmp(path
, "--remove")) {
528 if (!strcmp(path
, "--unmerged")) {
532 if (!strcmp(path
, "--refresh")) {
533 has_errors
|= refresh_cache(0);
536 if (!strcmp(path
, "--really-refresh")) {
537 has_errors
|= refresh_cache(1);
540 if (!strcmp(path
, "--cacheinfo")) {
541 unsigned char sha1
[20];
545 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
547 if ((sscanf(argv
[i
+1], "%o", &mode
) != 1) ||
548 get_sha1_hex(argv
[i
+2], sha1
) ||
549 add_cacheinfo(mode
, sha1
, argv
[i
+3], 0))
550 die("git-update-index: --cacheinfo"
551 " cannot add %s", argv
[i
+3]);
555 if (!strcmp(path
, "--chmod=-x") ||
556 !strcmp(path
, "--chmod=+x")) {
558 die("git-update-index: %s <path>", path
);
559 if (chmod_path(path
[8], argv
[++i
]))
560 die("git-update-index: %s cannot chmod %s", path
, argv
[i
]);
563 if (!strcmp(path
, "--assume-unchanged")) {
564 mark_valid_only
= MARK_VALID
;
567 if (!strcmp(path
, "--no-assume-unchanged")) {
568 mark_valid_only
= UNMARK_VALID
;
571 if (!strcmp(path
, "--info-only")) {
575 if (!strcmp(path
, "--force-remove")) {
579 if (!strcmp(path
, "-z")) {
580 line_termination
= 0;
583 if (!strcmp(path
, "--stdin")) {
585 die("--stdin must be at the end");
589 if (!strcmp(path
, "--index-info")) {
591 die("--index-info must be at the end");
592 allow_add
= allow_replace
= allow_remove
= 1;
593 read_index_info(line_termination
);
596 if (!strcmp(path
, "--ignore-missing")) {
600 if (!strcmp(path
, "--verbose")) {
604 if (!strcmp(path
, "-h") || !strcmp(path
, "--help"))
605 usage(update_index_usage
);
606 die("unknown option %s", path
);
608 update_one(path
, prefix
, prefix_length
);
610 if (read_from_stdin
) {
615 read_line(&buf
, stdin
, line_termination
);
618 if (line_termination
&& buf
.buf
[0] == '"')
619 path_name
= unquote_c_style(buf
.buf
, NULL
);
622 update_one(path_name
, prefix
, prefix_length
);
623 if (path_name
!= buf
.buf
)
627 if (active_cache_changed
) {
628 if (write_cache(newfd
, active_cache
, active_nr
) ||
629 commit_index_file(&cache_file
))
630 die("Unable to write new cachefile");
633 return has_errors
? 1 : 0;