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
;
27 /* Three functions to allow overloaded pointer return; see linux/err.h */
28 static inline void *ERR_PTR(long error
)
30 return (void *) error
;
33 static inline long PTR_ERR(const void *ptr
)
38 static inline long IS_ERR(const void *ptr
)
40 return (unsigned long)ptr
> (unsigned long)-1000L;
43 static void report(const char *fmt
, ...)
56 static int add_file_to_cache(const char *path
)
58 int size
, namelen
, option
, status
;
59 struct cache_entry
*ce
;
62 status
= lstat(path
, &st
);
63 if (status
< 0 || S_ISDIR(st
.st_mode
)) {
64 /* When we used to have "path" and now we want to add
65 * "path/file", we need a way to remove "path" before
66 * being able to add "path/file". However,
67 * "git-update-index --remove path" would not work.
68 * --force-remove can be used but this is more user
69 * friendly, especially since we can do the opposite
70 * case just fine without --force-remove.
72 if (status
== 0 || (errno
== ENOENT
|| errno
== ENOTDIR
)) {
74 if (remove_file_from_cache(path
))
75 return error("%s: cannot remove from the index",
79 } else if (status
< 0) {
80 return error("%s: does not exist and --remove not passed",
85 return error("%s: is a directory - add files inside instead",
88 return error("lstat(\"%s\"): %s", path
,
92 namelen
= strlen(path
);
93 size
= cache_entry_size(namelen
);
96 memcpy(ce
->name
, path
, namelen
);
97 fill_stat_cache_info(ce
, &st
);
99 ce
->ce_mode
= create_ce_mode(st
.st_mode
);
100 if (!trust_executable_bit
) {
101 /* If there is an existing entry, pick the mode bits
104 int pos
= cache_name_pos(path
, namelen
);
106 ce
->ce_mode
= active_cache
[pos
]->ce_mode
;
108 ce
->ce_flags
= htons(namelen
);
110 if (index_path(ce
->sha1
, path
, &st
, !info_only
))
112 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
113 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
114 if (add_cache_entry(ce
, option
))
115 return error("%s: cannot add to the index - missing --add option?",
121 * "refresh" does not calculate a new sha1 file or bring the
122 * cache up-to-date for mode/content changes. But what it
123 * _does_ do is to "re-match" the stat information of a file
124 * with the cache, so that you can refresh the cache for a
125 * file that hasn't been changed but where the stat entry is
128 * For example, you'd want to do this after doing a "git-read-tree",
129 * to link up the stat cache details with the proper files.
131 static struct cache_entry
*refresh_entry(struct cache_entry
*ce
)
134 struct cache_entry
*updated
;
137 if (lstat(ce
->name
, &st
) < 0)
138 return ERR_PTR(-errno
);
140 changed
= ce_match_stat(ce
, &st
);
144 if (ce_modified(ce
, &st
))
145 return ERR_PTR(-EINVAL
);
148 updated
= xmalloc(size
);
149 memcpy(updated
, ce
, size
);
150 fill_stat_cache_info(updated
, &st
);
154 static int refresh_cache(void)
159 for (i
= 0; i
< active_nr
; i
++) {
160 struct cache_entry
*ce
, *new;
161 ce
= active_cache
[i
];
163 while ((i
< active_nr
) &&
164 ! strcmp(active_cache
[i
]->name
, ce
->name
))
169 printf("%s: needs merge\n", ce
->name
);
174 new = refresh_entry(ce
);
178 if (not_new
&& PTR_ERR(new) == -ENOENT
)
182 printf("%s: needs update\n", ce
->name
);
186 active_cache_changed
= 1;
187 /* You can NOT just free active_cache[i] here, since it
188 * might not be necessarily malloc()ed but can also come
190 active_cache
[i
] = new;
196 * We fundamentally don't like some paths: we don't want
197 * dot or dot-dot anywhere, and for obvious reasons don't
198 * want to recurse into ".git" either.
200 * Also, we don't want double slashes or slashes at the
201 * end that can make pathnames ambiguous.
203 static int verify_dotfile(const char *rest
)
206 * The first character was '.', but that
207 * has already been discarded, we now test
211 /* "." is not allowed */
216 * ".git" followed by NUL or slash is bad. This
217 * shares the path end test with the ".." case.
227 if (rest
[1] == '\0' || rest
[1] == '/')
233 static int verify_path(const char *path
)
250 if (verify_dotfile(path
))
259 static int add_cacheinfo(const char *arg1
, const char *arg2
, const char *arg3
)
261 int size
, len
, option
;
263 unsigned char sha1
[20];
264 struct cache_entry
*ce
;
266 if (sscanf(arg1
, "%o", &mode
) != 1)
268 if (get_sha1_hex(arg2
, sha1
))
270 if (!verify_path(arg3
))
274 size
= cache_entry_size(len
);
278 memcpy(ce
->sha1
, sha1
, 20);
279 memcpy(ce
->name
, arg3
, len
);
280 ce
->ce_flags
= htons(len
);
281 ce
->ce_mode
= create_ce_mode(mode
);
282 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
283 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
284 if (add_cache_entry(ce
, option
))
285 return error("%s: cannot add to the index - missing --add option?",
287 report("add '%s'", arg3
);
291 static int chmod_path(int flip
, const char *path
)
294 struct cache_entry
*ce
;
297 pos
= cache_name_pos(path
, strlen(path
));
300 ce
= active_cache
[pos
];
301 mode
= ntohl(ce
->ce_mode
);
306 ce
->ce_mode
|= htonl(0111); break;
308 ce
->ce_mode
&= htonl(~0111); break;
312 active_cache_changed
= 1;
316 static struct cache_file cache_file
;
318 static void update_one(const char *path
, const char *prefix
, int prefix_length
)
320 const char *p
= prefix_path(prefix
, prefix_length
, path
);
321 if (!verify_path(p
)) {
322 fprintf(stderr
, "Ignoring path %s\n", path
);
326 if (remove_file_from_cache(p
))
327 die("git-update-index: unable to remove %s", path
);
328 report("remove '%s'", path
);
331 if (add_file_to_cache(p
))
332 die("Unable to process file %s", path
);
333 report("add '%s'", path
);
336 static void read_index_info(int line_termination
)
343 unsigned char sha1
[20];
346 read_line(&buf
, stdin
, line_termination
);
350 mode
= strtoul(buf
.buf
, &ptr
, 8);
351 if (ptr
== buf
.buf
|| *ptr
!= ' ' ||
352 get_sha1_hex(ptr
+ 1, sha1
) ||
358 if (line_termination
&& ptr
[0] == '"')
359 path_name
= unquote_c_style(ptr
, NULL
);
363 if (!verify_path(path_name
)) {
364 fprintf(stderr
, "Ignoring path %s\n", path_name
);
365 if (path_name
!= ptr
)
371 /* mode == 0 means there is no such path -- remove */
372 if (remove_file_from_cache(path_name
))
373 die("git-update-index: unable to remove %s",
377 /* mode ' ' sha1 '\t' name
378 * ptr[-1] points at tab,
379 * ptr[-41] is at the beginning of sha1
381 ptr
[-42] = ptr
[-1] = 0;
382 if (add_cacheinfo(buf
.buf
, ptr
-41, path_name
))
383 die("git-update-index: unable to update %s",
386 if (path_name
!= ptr
)
391 die("malformed index info %s", buf
.buf
);
395 static const char update_index_usage
[] =
396 "git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--cacheinfo] [--chmod=(+|-)x] [--info-only] [--force-remove] [--stdin] [--index-info] [--ignore-missing] [-z] [--version] [--] <file>...";
398 int main(int argc
, const char **argv
)
400 int i
, newfd
, entries
, has_errors
= 0, line_termination
= '\n';
401 int allow_options
= 1;
402 int read_from_stdin
= 0;
403 const char *prefix
= setup_git_directory();
404 int prefix_length
= prefix
? strlen(prefix
) : 0;
406 git_config(git_default_config
);
408 newfd
= hold_index_file_for_update(&cache_file
, get_index_file());
410 die("unable to create new cachefile");
412 entries
= read_cache();
414 die("cache corrupted");
416 for (i
= 1 ; i
< argc
; i
++) {
417 const char *path
= argv
[i
];
419 if (allow_options
&& *path
== '-') {
420 if (!strcmp(path
, "--")) {
424 if (!strcmp(path
, "-q")) {
428 if (!strcmp(path
, "--add")) {
432 if (!strcmp(path
, "--replace")) {
436 if (!strcmp(path
, "--remove")) {
440 if (!strcmp(path
, "--unmerged")) {
444 if (!strcmp(path
, "--refresh")) {
445 has_errors
|= refresh_cache();
448 if (!strcmp(path
, "--cacheinfo")) {
450 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
451 if (add_cacheinfo(argv
[i
+1], argv
[i
+2], argv
[i
+3]))
452 die("git-update-index: --cacheinfo cannot add %s", argv
[i
+3]);
456 if (!strcmp(path
, "--chmod=-x") ||
457 !strcmp(path
, "--chmod=+x")) {
459 die("git-update-index: %s <path>", path
);
460 if (chmod_path(path
[8], argv
[++i
]))
461 die("git-update-index: %s cannot chmod %s", path
, argv
[i
]);
464 if (!strcmp(path
, "--info-only")) {
468 if (!strcmp(path
, "--force-remove")) {
472 if (!strcmp(path
, "-z")) {
473 line_termination
= 0;
476 if (!strcmp(path
, "--stdin")) {
478 die("--stdin must be at the end");
482 if (!strcmp(path
, "--index-info")) {
483 allow_add
= allow_replace
= allow_remove
= 1;
484 read_index_info(line_termination
);
487 if (!strcmp(path
, "--ignore-missing")) {
491 if (!strcmp(path
, "--verbose")) {
495 if (!strcmp(path
, "-h") || !strcmp(path
, "--help"))
496 usage(update_index_usage
);
497 die("unknown option %s", path
);
499 update_one(path
, prefix
, prefix_length
);
501 if (read_from_stdin
) {
505 read_line(&buf
, stdin
, line_termination
);
508 update_one(buf
.buf
, prefix
, prefix_length
);
511 if (active_cache_changed
) {
512 if (write_cache(newfd
, active_cache
, active_nr
) ||
513 commit_index_file(&cache_file
))
514 die("Unable to write new cachefile");
517 return has_errors
? 1 : 0;