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 int 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;
356 static struct cache_file cache_file
;
358 static void update_one(const char *path
, const char *prefix
, int prefix_length
)
360 const char *p
= prefix_path(prefix
, prefix_length
, path
);
361 if (!verify_path(p
)) {
362 fprintf(stderr
, "Ignoring path %s\n", path
);
365 if (mark_valid_only
) {
367 die("Unable to mark file %s", path
);
372 if (remove_file_from_cache(p
))
373 die("git-update-index: unable to remove %s", path
);
374 report("remove '%s'", path
);
377 if (add_file_to_cache(p
))
378 die("Unable to process file %s", path
);
379 report("add '%s'", path
);
382 static void read_index_info(int line_termination
)
389 unsigned char sha1
[20];
393 /* This reads lines formatted in one of three formats:
395 * (1) mode SP sha1 TAB path
396 * The first format is what "git-apply --index-info"
397 * reports, and used to reconstruct a partial tree
398 * that is used for phony merge base tree when falling
399 * back on 3-way merge.
401 * (2) mode SP type SP sha1 TAB path
402 * The second format is to stuff git-ls-tree output
403 * into the index file.
405 * (3) mode SP sha1 SP stage TAB path
406 * This format is to put higher order stages into the
407 * index file and matches git-ls-files --stage output.
409 read_line(&buf
, stdin
, line_termination
);
413 mode
= strtoul(buf
.buf
, &ptr
, 8);
414 if (ptr
== buf
.buf
|| *ptr
!= ' ')
417 tab
= strchr(ptr
, '\t');
418 if (!tab
|| tab
- ptr
< 41)
421 if (tab
[-2] == ' ' && '0' <= tab
[-1] && tab
[-1] <= '3') {
422 stage
= tab
[-1] - '0';
423 ptr
= tab
+ 1; /* point at the head of path */
424 tab
= tab
- 2; /* point at tail of sha1 */
428 ptr
= tab
+ 1; /* point at the head of path */
431 if (get_sha1_hex(tab
- 40, sha1
) || tab
[-41] != ' ')
434 if (line_termination
&& ptr
[0] == '"')
435 path_name
= unquote_c_style(ptr
, NULL
);
439 if (!verify_path(path_name
)) {
440 fprintf(stderr
, "Ignoring path %s\n", path_name
);
441 if (path_name
!= ptr
)
447 /* mode == 0 means there is no such path -- remove */
448 if (remove_file_from_cache(path_name
))
449 die("git-update-index: unable to remove %s",
453 /* mode ' ' sha1 '\t' name
454 * ptr[-1] points at tab,
455 * ptr[-41] is at the beginning of sha1
457 ptr
[-42] = ptr
[-1] = 0;
458 if (add_cacheinfo(mode
, sha1
, path_name
, stage
))
459 die("git-update-index: unable to update %s",
462 if (path_name
!= ptr
)
467 die("malformed index info %s", buf
.buf
);
471 static const char update_index_usage
[] =
472 "git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--cacheinfo] [--chmod=(+|-)x] [--info-only] [--force-remove] [--stdin] [--index-info] [--ignore-missing] [-z] [--verbose] [--] <file>...";
474 int main(int argc
, const char **argv
)
476 int i
, newfd
, entries
, has_errors
= 0, line_termination
= '\n';
477 int allow_options
= 1;
478 int read_from_stdin
= 0;
479 const char *prefix
= setup_git_directory();
480 int prefix_length
= prefix
? strlen(prefix
) : 0;
482 git_config(git_default_config
);
484 newfd
= hold_index_file_for_update(&cache_file
, get_index_file());
486 die("unable to create new cachefile");
488 entries
= read_cache();
490 die("cache corrupted");
492 for (i
= 1 ; i
< argc
; i
++) {
493 const char *path
= argv
[i
];
495 if (allow_options
&& *path
== '-') {
496 if (!strcmp(path
, "--")) {
500 if (!strcmp(path
, "-q")) {
504 if (!strcmp(path
, "--add")) {
508 if (!strcmp(path
, "--replace")) {
512 if (!strcmp(path
, "--remove")) {
516 if (!strcmp(path
, "--unmerged")) {
520 if (!strcmp(path
, "--refresh")) {
521 has_errors
|= refresh_cache(0);
524 if (!strcmp(path
, "--really-refresh")) {
525 has_errors
|= refresh_cache(1);
528 if (!strcmp(path
, "--cacheinfo")) {
529 unsigned char sha1
[20];
533 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
535 if ((sscanf(argv
[i
+1], "%o", &mode
) != 1) ||
536 get_sha1_hex(argv
[i
+2], sha1
) ||
537 add_cacheinfo(mode
, sha1
, argv
[i
+3], 0))
538 die("git-update-index: --cacheinfo"
539 " cannot add %s", argv
[i
+3]);
543 if (!strcmp(path
, "--chmod=-x") ||
544 !strcmp(path
, "--chmod=+x")) {
546 die("git-update-index: %s <path>", path
);
547 if (chmod_path(path
[8], argv
[++i
]))
548 die("git-update-index: %s cannot chmod %s", path
, argv
[i
]);
551 if (!strcmp(path
, "--assume-unchanged")) {
552 mark_valid_only
= MARK_VALID
;
555 if (!strcmp(path
, "--no-assume-unchanged")) {
556 mark_valid_only
= UNMARK_VALID
;
559 if (!strcmp(path
, "--info-only")) {
563 if (!strcmp(path
, "--force-remove")) {
567 if (!strcmp(path
, "-z")) {
568 line_termination
= 0;
571 if (!strcmp(path
, "--stdin")) {
573 die("--stdin must be at the end");
577 if (!strcmp(path
, "--index-info")) {
579 die("--index-info must be at the end");
580 allow_add
= allow_replace
= allow_remove
= 1;
581 read_index_info(line_termination
);
584 if (!strcmp(path
, "--ignore-missing")) {
588 if (!strcmp(path
, "--verbose")) {
592 if (!strcmp(path
, "-h") || !strcmp(path
, "--help"))
593 usage(update_index_usage
);
594 die("unknown option %s", path
);
596 update_one(path
, prefix
, prefix_length
);
598 if (read_from_stdin
) {
603 read_line(&buf
, stdin
, line_termination
);
606 if (line_termination
&& buf
.buf
[0] == '"')
607 path_name
= unquote_c_style(buf
.buf
, NULL
);
610 update_one(path_name
, prefix
, prefix_length
);
611 if (path_name
!= buf
.buf
)
615 if (active_cache_changed
) {
616 if (write_cache(newfd
, active_cache
, active_nr
) ||
617 commit_index_file(&cache_file
))
618 die("Unable to write new cachefile");
621 return has_errors
? 1 : 0;