2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
11 * Default to not allowing changes to the list of files. The
12 * tool doesn't actually care, but this makes it harder to add
13 * files to the revision control by mistake by doing something
14 * like "git-update-index *" and suddenly having all the object
15 * files be revision controlled.
18 static int allow_remove
;
19 static int allow_replace
;
20 static int allow_unmerged
; /* --refresh needing merge is not error */
21 static int not_new
; /* --refresh not having working tree files is not error */
22 static int quiet
; /* --refresh needing update is not error */
24 static int force_remove
;
26 static int mark_valid_only
= 0;
28 #define UNMARK_VALID 2
31 /* Three functions to allow overloaded pointer return; see linux/err.h */
32 static inline void *ERR_PTR(long error
)
34 return (void *) error
;
37 static inline long PTR_ERR(const void *ptr
)
42 static inline long IS_ERR(const void *ptr
)
44 return (unsigned long)ptr
> (unsigned long)-1000L;
47 static void report(const char *fmt
, ...)
60 static int mark_valid(const char *path
)
62 int namelen
= strlen(path
);
63 int pos
= cache_name_pos(path
, namelen
);
65 switch (mark_valid_only
) {
67 active_cache
[pos
]->ce_flags
|= htons(CE_VALID
);
70 active_cache
[pos
]->ce_flags
&= ~htons(CE_VALID
);
73 active_cache_changed
= 1;
79 static int add_file_to_cache(const char *path
)
81 int size
, namelen
, option
, status
;
82 struct cache_entry
*ce
;
85 status
= lstat(path
, &st
);
86 if (status
< 0 || S_ISDIR(st
.st_mode
)) {
87 /* When we used to have "path" and now we want to add
88 * "path/file", we need a way to remove "path" before
89 * being able to add "path/file". However,
90 * "git-update-index --remove path" would not work.
91 * --force-remove can be used but this is more user
92 * friendly, especially since we can do the opposite
93 * case just fine without --force-remove.
95 if (status
== 0 || (errno
== ENOENT
|| errno
== ENOTDIR
)) {
97 if (remove_file_from_cache(path
))
98 return error("%s: cannot remove from the index",
102 } else if (status
< 0) {
103 return error("%s: does not exist and --remove not passed",
108 return error("%s: is a directory - add files inside instead",
111 return error("lstat(\"%s\"): %s", path
,
115 namelen
= strlen(path
);
116 size
= cache_entry_size(namelen
);
117 ce
= xcalloc(1, size
);
118 memcpy(ce
->name
, path
, namelen
);
119 ce
->ce_flags
= htons(namelen
);
120 fill_stat_cache_info(ce
, &st
);
122 ce
->ce_mode
= create_ce_mode(st
.st_mode
);
123 if (!trust_executable_bit
) {
124 /* If there is an existing entry, pick the mode bits
127 int pos
= cache_name_pos(path
, namelen
);
129 ce
->ce_mode
= active_cache
[pos
]->ce_mode
;
132 if (index_path(ce
->sha1
, path
, &st
, !info_only
))
134 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
135 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
136 if (add_cache_entry(ce
, option
))
137 return error("%s: cannot add to the index - missing --add option?",
143 * "refresh" does not calculate a new sha1 file or bring the
144 * cache up-to-date for mode/content changes. But what it
145 * _does_ do is to "re-match" the stat information of a file
146 * with the cache, so that you can refresh the cache for a
147 * file that hasn't been changed but where the stat entry is
150 * For example, you'd want to do this after doing a "git-read-tree",
151 * to link up the stat cache details with the proper files.
153 static struct cache_entry
*refresh_entry(struct cache_entry
*ce
, int really
)
156 struct cache_entry
*updated
;
159 if (lstat(ce
->name
, &st
) < 0)
160 return ERR_PTR(-errno
);
162 changed
= ce_match_stat(ce
, &st
, really
);
164 if (really
&& assume_unchanged
&&
165 !(ce
->ce_flags
& htons(CE_VALID
)))
166 ; /* mark this one VALID again */
171 if (ce_modified(ce
, &st
, really
))
172 return ERR_PTR(-EINVAL
);
175 updated
= xmalloc(size
);
176 memcpy(updated
, ce
, size
);
177 fill_stat_cache_info(updated
, &st
);
179 /* In this case, if really is not set, we should leave
180 * CE_VALID bit alone. Otherwise, paths marked with
181 * --no-assume-unchanged (i.e. things to be edited) will
182 * reacquire CE_VALID bit automatically, which is not
183 * really what we want.
185 if (!really
&& assume_unchanged
&& !(ce
->ce_flags
& htons(CE_VALID
)))
186 updated
->ce_flags
&= ~htons(CE_VALID
);
191 static int refresh_cache(int really
)
196 for (i
= 0; i
< active_nr
; i
++) {
197 struct cache_entry
*ce
, *new;
198 ce
= active_cache
[i
];
200 while ((i
< active_nr
) &&
201 ! strcmp(active_cache
[i
]->name
, ce
->name
))
206 printf("%s: needs merge\n", ce
->name
);
211 new = refresh_entry(ce
, really
);
215 if (not_new
&& PTR_ERR(new) == -ENOENT
)
217 if (really
&& PTR_ERR(new) == -EINVAL
) {
218 /* If we are doing --really-refresh that
219 * means the index is not valid anymore.
221 ce
->ce_flags
&= ~htons(CE_VALID
);
222 active_cache_changed
= 1;
226 printf("%s: needs update\n", ce
->name
);
230 active_cache_changed
= 1;
231 /* You can NOT just free active_cache[i] here, since it
232 * might not be necessarily malloc()ed but can also come
234 active_cache
[i
] = new;
240 * We fundamentally don't like some paths: we don't want
241 * dot or dot-dot anywhere, and for obvious reasons don't
242 * want to recurse into ".git" either.
244 * Also, we don't want double slashes or slashes at the
245 * end that can make pathnames ambiguous.
247 static int verify_dotfile(const char *rest
)
250 * The first character was '.', but that
251 * has already been discarded, we now test
255 /* "." is not allowed */
260 * ".git" followed by NUL or slash is bad. This
261 * shares the path end test with the ".." case.
271 if (rest
[1] == '\0' || rest
[1] == '/')
277 static int verify_path(const char *path
)
294 if (verify_dotfile(path
))
303 static int add_cacheinfo(unsigned int mode
, const unsigned char *sha1
,
304 const char *path
, int stage
)
306 int size
, len
, option
;
307 struct cache_entry
*ce
;
309 if (!verify_path(path
))
313 size
= cache_entry_size(len
);
314 ce
= xcalloc(1, size
);
316 memcpy(ce
->sha1
, sha1
, 20);
317 memcpy(ce
->name
, path
, len
);
318 ce
->ce_flags
= create_ce_flags(len
, stage
);
319 ce
->ce_mode
= create_ce_mode(mode
);
320 if (assume_unchanged
)
321 ce
->ce_flags
|= htons(CE_VALID
);
322 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
323 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
324 if (add_cache_entry(ce
, option
))
325 return error("%s: cannot add to the index - missing --add option?",
327 report("add '%s'", path
);
331 static void chmod_path(int flip
, const char *path
)
334 struct cache_entry
*ce
;
337 pos
= cache_name_pos(path
, strlen(path
));
340 ce
= active_cache
[pos
];
341 mode
= ntohl(ce
->ce_mode
);
346 ce
->ce_mode
|= htonl(0111); break;
348 ce
->ce_mode
&= htonl(~0111); break;
352 active_cache_changed
= 1;
353 report("chmod %cx '%s'", flip
, path
);
356 die("git-update-index: cannot chmod %cx '%s'", flip
, path
);
359 static struct cache_file cache_file
;
361 static void update_one(const char *path
, const char *prefix
, int prefix_length
)
363 const char *p
= prefix_path(prefix
, prefix_length
, path
);
364 if (!verify_path(p
)) {
365 fprintf(stderr
, "Ignoring path %s\n", path
);
368 if (mark_valid_only
) {
370 die("Unable to mark file %s", path
);
375 if (remove_file_from_cache(p
))
376 die("git-update-index: unable to remove %s", path
);
377 report("remove '%s'", path
);
380 if (add_file_to_cache(p
))
381 die("Unable to process file %s", path
);
382 report("add '%s'", path
);
385 static void read_index_info(int line_termination
)
392 unsigned char sha1
[20];
396 /* This reads lines formatted in one of three formats:
398 * (1) mode SP sha1 TAB path
399 * The first format is what "git-apply --index-info"
400 * reports, and used to reconstruct a partial tree
401 * that is used for phony merge base tree when falling
402 * back on 3-way merge.
404 * (2) mode SP type SP sha1 TAB path
405 * The second format is to stuff git-ls-tree output
406 * into the index file.
408 * (3) mode SP sha1 SP stage TAB path
409 * This format is to put higher order stages into the
410 * index file and matches git-ls-files --stage output.
412 read_line(&buf
, stdin
, line_termination
);
416 mode
= strtoul(buf
.buf
, &ptr
, 8);
417 if (ptr
== buf
.buf
|| *ptr
!= ' ')
420 tab
= strchr(ptr
, '\t');
421 if (!tab
|| tab
- ptr
< 41)
424 if (tab
[-2] == ' ' && '0' <= tab
[-1] && tab
[-1] <= '3') {
425 stage
= tab
[-1] - '0';
426 ptr
= tab
+ 1; /* point at the head of path */
427 tab
= tab
- 2; /* point at tail of sha1 */
431 ptr
= tab
+ 1; /* point at the head of path */
434 if (get_sha1_hex(tab
- 40, sha1
) || tab
[-41] != ' ')
437 if (line_termination
&& ptr
[0] == '"')
438 path_name
= unquote_c_style(ptr
, NULL
);
442 if (!verify_path(path_name
)) {
443 fprintf(stderr
, "Ignoring path %s\n", path_name
);
444 if (path_name
!= ptr
)
450 /* mode == 0 means there is no such path -- remove */
451 if (remove_file_from_cache(path_name
))
452 die("git-update-index: unable to remove %s",
456 /* mode ' ' sha1 '\t' name
457 * ptr[-1] points at tab,
458 * ptr[-41] is at the beginning of sha1
460 ptr
[-42] = ptr
[-1] = 0;
461 if (add_cacheinfo(mode
, sha1
, path_name
, stage
))
462 die("git-update-index: unable to update %s",
465 if (path_name
!= ptr
)
470 die("malformed index info %s", buf
.buf
);
474 static const char update_index_usage
[] =
475 "git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--cacheinfo] [--chmod=(+|-)x] [--info-only] [--force-remove] [--stdin] [--index-info] [--ignore-missing] [-z] [--verbose] [--] <file>...";
477 int main(int argc
, const char **argv
)
479 int i
, newfd
, entries
, has_errors
= 0, line_termination
= '\n';
480 int allow_options
= 1;
481 int read_from_stdin
= 0;
482 const char *prefix
= setup_git_directory();
483 int prefix_length
= prefix
? strlen(prefix
) : 0;
484 char set_executable_bit
= 0;
486 git_config(git_default_config
);
488 newfd
= hold_index_file_for_update(&cache_file
, get_index_file());
490 die("unable to create new cachefile");
492 entries
= read_cache();
494 die("cache corrupted");
496 for (i
= 1 ; i
< argc
; i
++) {
497 const char *path
= argv
[i
];
499 if (allow_options
&& *path
== '-') {
500 if (!strcmp(path
, "--")) {
504 if (!strcmp(path
, "-q")) {
508 if (!strcmp(path
, "--add")) {
512 if (!strcmp(path
, "--replace")) {
516 if (!strcmp(path
, "--remove")) {
520 if (!strcmp(path
, "--unmerged")) {
524 if (!strcmp(path
, "--refresh")) {
525 has_errors
|= refresh_cache(0);
528 if (!strcmp(path
, "--really-refresh")) {
529 has_errors
|= refresh_cache(1);
532 if (!strcmp(path
, "--cacheinfo")) {
533 unsigned char sha1
[20];
537 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
539 if ((sscanf(argv
[i
+1], "%o", &mode
) != 1) ||
540 get_sha1_hex(argv
[i
+2], sha1
) ||
541 add_cacheinfo(mode
, sha1
, argv
[i
+3], 0))
542 die("git-update-index: --cacheinfo"
543 " cannot add %s", argv
[i
+3]);
547 if (!strcmp(path
, "--chmod=-x") ||
548 !strcmp(path
, "--chmod=+x")) {
550 die("git-update-index: %s <path>", path
);
551 set_executable_bit
= path
[8];
554 if (!strcmp(path
, "--assume-unchanged")) {
555 mark_valid_only
= MARK_VALID
;
558 if (!strcmp(path
, "--no-assume-unchanged")) {
559 mark_valid_only
= UNMARK_VALID
;
562 if (!strcmp(path
, "--info-only")) {
566 if (!strcmp(path
, "--force-remove")) {
570 if (!strcmp(path
, "-z")) {
571 line_termination
= 0;
574 if (!strcmp(path
, "--stdin")) {
576 die("--stdin must be at the end");
580 if (!strcmp(path
, "--index-info")) {
582 die("--index-info must be at the end");
583 allow_add
= allow_replace
= allow_remove
= 1;
584 read_index_info(line_termination
);
587 if (!strcmp(path
, "--ignore-missing")) {
591 if (!strcmp(path
, "--verbose")) {
595 if (!strcmp(path
, "-h") || !strcmp(path
, "--help"))
596 usage(update_index_usage
);
597 die("unknown option %s", path
);
599 update_one(path
, prefix
, prefix_length
);
600 if (set_executable_bit
)
601 chmod_path(set_executable_bit
, path
);
603 if (read_from_stdin
) {
608 read_line(&buf
, stdin
, line_termination
);
611 if (line_termination
&& buf
.buf
[0] == '"')
612 path_name
= unquote_c_style(buf
.buf
, NULL
);
615 update_one(path_name
, prefix
, prefix_length
);
616 if (set_executable_bit
) {
617 const char *p
= prefix_path(prefix
, prefix_length
, path_name
);
618 chmod_path(set_executable_bit
, p
);
620 if (path_name
!= buf
.buf
)
624 if (active_cache_changed
) {
625 if (write_cache(newfd
, active_cache
, active_nr
) ||
626 commit_index_file(&cache_file
))
627 die("Unable to write new cachefile");
630 return has_errors
? 1 : 0;