2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
10 * Default to not allowing changes to the list of files. The
11 * tool doesn't actually care, but this makes it harder to add
12 * files to the revision control by mistake by doing something
13 * like "git-update-index *" and suddenly having all the object
14 * files be revision controlled.
16 static int allow_add = 0, allow_remove = 0, allow_replace = 0, allow_unmerged = 0, not_new = 0, quiet = 0, info_only = 0;
17 static int force_remove;
19 /* Three functions to allow overloaded pointer return; see linux/err.h */
20 static inline void *ERR_PTR(long error)
22 return (void *) error;
25 static inline long PTR_ERR(const void *ptr)
30 static inline long IS_ERR(const void *ptr)
32 return (unsigned long)ptr > (unsigned long)-1000L;
35 static int add_file_to_cache(const char *path)
37 int size, namelen, option, status;
38 struct cache_entry *ce;
41 status = lstat(path, &st);
42 if (status < 0 || S_ISDIR(st.st_mode)) {
43 /* When we used to have "path" and now we want to add
44 * "path/file", we need a way to remove "path" before
45 * being able to add "path/file". However,
46 * "git-update-index --remove path" would not work.
47 * --force-remove can be used but this is more user
48 * friendly, especially since we can do the opposite
49 * case just fine without --force-remove.
51 if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) {
53 if (remove_file_from_cache(path))
54 return error("%s: cannot remove from the index",
58 } else if (status < 0) {
59 return error("%s: does not exist and --remove not passed",
64 return error("%s: is a directory - add files inside instead",
67 return error("lstat(\"%s\"): %s", path,
71 namelen = strlen(path);
72 size = cache_entry_size(namelen);
75 memcpy(ce->name, path, namelen);
76 fill_stat_cache_info(ce, &st);
78 ce->ce_mode = create_ce_mode(st.st_mode);
79 if (!trust_executable_bit) {
80 /* If there is an existing entry, pick the mode bits
83 int pos = cache_name_pos(path, namelen);
85 ce->ce_mode = active_cache[pos]->ce_mode;
87 ce->ce_flags = htons(namelen);
89 if (index_path(ce->sha1, path, &st, !info_only))
91 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
92 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
93 if (add_cache_entry(ce, option))
94 return error("%s: cannot add to the index - missing --add option?",
100 * "refresh" does not calculate a new sha1 file or bring the
101 * cache up-to-date for mode/content changes. But what it
102 * _does_ do is to "re-match" the stat information of a file
103 * with the cache, so that you can refresh the cache for a
104 * file that hasn't been changed but where the stat entry is
107 * For example, you'd want to do this after doing a "git-read-tree",
108 * to link up the stat cache details with the proper files.
110 static struct cache_entry *refresh_entry(struct cache_entry *ce)
113 struct cache_entry *updated;
116 if (lstat(ce->name, &st) < 0)
117 return ERR_PTR(-errno);
119 changed = ce_match_stat(ce, &st);
123 if (ce_modified(ce, &st))
124 return ERR_PTR(-EINVAL);
127 updated = xmalloc(size);
128 memcpy(updated, ce, size);
129 fill_stat_cache_info(updated, &st);
133 static int refresh_cache(void)
138 for (i = 0; i < active_nr; i++) {
139 struct cache_entry *ce, *new;
140 ce = active_cache[i];
142 while ((i < active_nr) &&
143 ! strcmp(active_cache[i]->name, ce->name))
148 printf("%s: needs merge\n", ce->name);
153 new = refresh_entry(ce);
157 if (not_new && PTR_ERR(new) == -ENOENT)
161 printf("%s: needs update\n", ce->name);
165 active_cache_changed = 1;
166 /* You can NOT just free active_cache[i] here, since it
167 * might not be necessarily malloc()ed but can also come
169 active_cache[i] = new;
175 * We fundamentally don't like some paths: we don't want
176 * dot or dot-dot anywhere, and for obvious reasons don't
177 * want to recurse into ".git" either.
179 * Also, we don't want double slashes or slashes at the
180 * end that can make pathnames ambiguous.
182 static int verify_dotfile(const char *rest)
185 * The first character was '.', but that
186 * has already been discarded, we now test
190 /* "." is not allowed */
195 * ".git" followed by NUL or slash is bad. This
196 * shares the path end test with the ".." case.
206 if (rest[1] == '\0' || rest[1] == '/')
212 static int verify_path(const char *path)
229 if (verify_dotfile(path))
238 static int add_cacheinfo(const char *arg1, const char *arg2, const char *arg3)
240 int size, len, option;
242 unsigned char sha1[20];
243 struct cache_entry *ce;
245 if (sscanf(arg1, "%o", &mode) != 1)
247 if (get_sha1_hex(arg2, sha1))
249 if (!verify_path(arg3))
253 size = cache_entry_size(len);
257 memcpy(ce->sha1, sha1, 20);
258 memcpy(ce->name, arg3, len);
259 ce->ce_flags = htons(len);
260 ce->ce_mode = create_ce_mode(mode);
261 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
262 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
263 return add_cache_entry(ce, option);
266 static int chmod_path(int flip, const char *path)
269 struct cache_entry *ce;
272 pos = cache_name_pos(path, strlen(path));
275 ce = active_cache[pos];
276 mode = ntohl(ce->ce_mode);
281 ce->ce_mode |= htonl(0111); break;
283 ce->ce_mode &= htonl(~0111); break;
287 active_cache_changed = 1;
291 static struct cache_file cache_file;
293 static void update_one(const char *path, const char *prefix, int prefix_length)
295 const char *p = prefix_path(prefix, prefix_length, path);
296 if (!verify_path(p)) {
297 fprintf(stderr, "Ignoring path %s\n", path);
301 if (remove_file_from_cache(p))
302 die("git-update-index: unable to remove %s", path);
305 if (add_file_to_cache(p))
306 die("Unable to process file %s", path);
309 static void read_index_info(int line_termination)
315 unsigned char sha1[20];
318 read_line(&buf, stdin, line_termination);
322 mode = strtoul(buf.buf, &ptr, 8);
323 if (ptr == buf.buf || *ptr != ' ' ||
324 get_sha1_hex(ptr + 1, sha1) ||
329 if (!verify_path(ptr)) {
330 fprintf(stderr, "Ignoring path %s\n", ptr);
335 /* mode == 0 means there is no such path -- remove */
336 if (remove_file_from_cache(ptr))
337 die("git-update-index: unable to remove %s",
341 /* mode ' ' sha1 '\t' name
342 * ptr[-1] points at tab,
343 * ptr[-41] is at the beginning of sha1
345 ptr[-42] = ptr[-1] = 0;
346 if (add_cacheinfo(buf.buf, ptr-41, ptr))
347 die("git-update-index: unable to update %s",
353 die("malformed index info %s", buf.buf);
357 int main(int argc, const char **argv)
359 int i, newfd, entries, has_errors = 0, line_termination = '\n';
360 int allow_options = 1;
361 int read_from_stdin = 0;
362 const char *prefix = setup_git_directory();
363 int prefix_length = prefix ? strlen(prefix) : 0;
365 git_config(git_default_config);
367 newfd = hold_index_file_for_update(&cache_file, get_index_file());
369 die("unable to create new cachefile");
371 entries = read_cache();
373 die("cache corrupted");
375 for (i = 1 ; i < argc; i++) {
376 const char *path = argv[i];
378 if (allow_options && *path == '-') {
379 if (!strcmp(path, "--")) {
383 if (!strcmp(path, "-q")) {
387 if (!strcmp(path, "--add")) {
391 if (!strcmp(path, "--replace")) {
395 if (!strcmp(path, "--remove")) {
399 if (!strcmp(path, "--unmerged")) {
403 if (!strcmp(path, "--refresh")) {
404 has_errors |= refresh_cache();
407 if (!strcmp(path, "--cacheinfo")) {
409 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
410 if (add_cacheinfo(argv[i+1], argv[i+2], argv[i+3]))
411 die("git-update-index: --cacheinfo cannot add %s", argv[i+3]);
415 if (!strcmp(path, "--chmod=-x") ||
416 !strcmp(path, "--chmod=+x")) {
418 die("git-update-index: %s <path>", path);
419 if (chmod_path(path[8], argv[++i]))
420 die("git-update-index: %s cannot chmod %s", path, argv[i]);
423 if (!strcmp(path, "--info-only")) {
427 if (!strcmp(path, "--force-remove")) {
431 if (!strcmp(path, "-z")) {
432 line_termination = 0;
435 if (!strcmp(path, "--stdin")) {
437 die("--stdin must be at the end");
441 if (!strcmp(path, "--index-info")) {
442 allow_add = allow_replace = allow_remove = 1;
443 read_index_info(line_termination);
446 if (!strcmp(path, "--ignore-missing")) {
450 die("unknown option %s", path);
452 update_one(path, prefix, prefix_length);
454 if (read_from_stdin) {
458 read_line(&buf, stdin, line_termination);
461 update_one(buf.buf, prefix, prefix_length);
464 if (active_cache_changed) {
465 if (write_cache(newfd, active_cache, active_nr) ||
466 commit_index_file(&cache_file))
467 die("Unable to write new cachefile");
470 return has_errors ? 1 : 0;