GIT 1.3.0-rc2
[git/mingw/4msysgit/kblees.git] / update-index.c
blob1efac27c6baf26ed881099e91a3cbd5dbb3c2289
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "strbuf.h"
8 #include "quote.h"
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.
17 static int allow_add;
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 */
23 static int info_only;
24 static int force_remove;
25 static int verbose;
26 static int mark_valid_only = 0;
27 #define MARK_VALID 1
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)
39 return (long) 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, ...)
49 va_list vp;
51 if (!verbose)
52 return;
54 va_start(vp, fmt);
55 vprintf(fmt, vp);
56 putchar('\n');
57 va_end(vp);
60 static int mark_valid(const char *path)
62 int namelen = strlen(path);
63 int pos = cache_name_pos(path, namelen);
64 if (0 <= pos) {
65 switch (mark_valid_only) {
66 case MARK_VALID:
67 active_cache[pos]->ce_flags |= htons(CE_VALID);
68 break;
69 case UNMARK_VALID:
70 active_cache[pos]->ce_flags &= ~htons(CE_VALID);
71 break;
73 active_cache_changed = 1;
74 return 0;
76 return -1;
79 static int add_file_to_cache(const char *path)
81 int size, namelen, option, status;
82 struct cache_entry *ce;
83 struct stat st;
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)) {
96 if (allow_remove) {
97 if (remove_file_from_cache(path))
98 return error("%s: cannot remove from the index",
99 path);
100 else
101 return 0;
102 } else if (status < 0) {
103 return error("%s: does not exist and --remove not passed",
104 path);
107 if (0 == status)
108 return error("%s: is a directory - add files inside instead",
109 path);
110 else
111 return error("lstat(\"%s\"): %s", path,
112 strerror(errno));
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
125 * from it.
127 int pos = cache_name_pos(path, namelen);
128 if (0 <= pos)
129 ce->ce_mode = active_cache[pos]->ce_mode;
132 if (index_path(ce->sha1, path, &st, !info_only))
133 return -1;
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?",
138 path);
139 return 0;
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
148 * out of date.
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)
155 struct stat st;
156 struct cache_entry *updated;
157 int changed, size;
159 if (lstat(ce->name, &st) < 0)
160 return ERR_PTR(-errno);
162 changed = ce_match_stat(ce, &st, really);
163 if (!changed) {
164 if (really && assume_unchanged &&
165 !(ce->ce_flags & htons(CE_VALID)))
166 ; /* mark this one VALID again */
167 else
168 return NULL;
171 if (ce_modified(ce, &st, really))
172 return ERR_PTR(-EINVAL);
174 size = ce_size(ce);
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);
188 return updated;
191 static int refresh_cache(int really)
193 int i;
194 int has_errors = 0;
196 for (i = 0; i < active_nr; i++) {
197 struct cache_entry *ce, *new;
198 ce = active_cache[i];
199 if (ce_stage(ce)) {
200 while ((i < active_nr) &&
201 ! strcmp(active_cache[i]->name, ce->name))
202 i++;
203 i--;
204 if (allow_unmerged)
205 continue;
206 printf("%s: needs merge\n", ce->name);
207 has_errors = 1;
208 continue;
211 new = refresh_entry(ce, really);
212 if (!new)
213 continue;
214 if (IS_ERR(new)) {
215 if (not_new && PTR_ERR(new) == -ENOENT)
216 continue;
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;
224 if (quiet)
225 continue;
226 printf("%s: needs update\n", ce->name);
227 has_errors = 1;
228 continue;
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
233 * from mmap(). */
234 active_cache[i] = new;
236 return has_errors;
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
252 * the rest.
254 switch (*rest) {
255 /* "." is not allowed */
256 case '\0': case '/':
257 return 0;
260 * ".git" followed by NUL or slash is bad. This
261 * shares the path end test with the ".." case.
263 case 'g':
264 if (rest[1] != 'i')
265 break;
266 if (rest[2] != 't')
267 break;
268 rest += 2;
269 /* fallthrough */
270 case '.':
271 if (rest[1] == '\0' || rest[1] == '/')
272 return 0;
274 return 1;
277 static int verify_path(const char *path)
279 char c;
281 goto inside;
282 for (;;) {
283 if (!c)
284 return 1;
285 if (c == '/') {
286 inside:
287 c = *path++;
288 switch (c) {
289 default:
290 continue;
291 case '/': case '\0':
292 break;
293 case '.':
294 if (verify_dotfile(path))
295 continue;
297 return 0;
299 c = *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))
310 return -1;
312 len = strlen(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?",
326 path);
327 report("add '%s'", path);
328 return 0;
331 static int chmod_path(int flip, const char *path)
333 int pos;
334 struct cache_entry *ce;
335 unsigned int mode;
337 pos = cache_name_pos(path, strlen(path));
338 if (pos < 0)
339 return -1;
340 ce = active_cache[pos];
341 mode = ntohl(ce->ce_mode);
342 if (!S_ISREG(mode))
343 return -1;
344 switch (flip) {
345 case '+':
346 ce->ce_mode |= htonl(0111); break;
347 case '-':
348 ce->ce_mode &= htonl(~0111); break;
349 default:
350 return -1;
352 active_cache_changed = 1;
353 return 0;
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);
363 return;
365 if (mark_valid_only) {
366 if (mark_valid(p))
367 die("Unable to mark file %s", path);
368 return;
371 if (force_remove) {
372 if (remove_file_from_cache(p))
373 die("git-update-index: unable to remove %s", path);
374 report("remove '%s'", path);
375 return;
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)
384 struct strbuf buf;
385 strbuf_init(&buf);
386 while (1) {
387 char *ptr, *tab;
388 char *path_name;
389 unsigned char sha1[20];
390 unsigned int mode;
391 int stage;
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);
410 if (buf.eof)
411 break;
413 mode = strtoul(buf.buf, &ptr, 8);
414 if (ptr == buf.buf || *ptr != ' ')
415 goto bad_line;
417 tab = strchr(ptr, '\t');
418 if (!tab || tab - ptr < 41)
419 goto bad_line;
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 */
426 else {
427 stage = 0;
428 ptr = tab + 1; /* point at the head of path */
431 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
432 goto bad_line;
434 if (line_termination && ptr[0] == '"')
435 path_name = unquote_c_style(ptr, NULL);
436 else
437 path_name = ptr;
439 if (!verify_path(path_name)) {
440 fprintf(stderr, "Ignoring path %s\n", path_name);
441 if (path_name != ptr)
442 free(path_name);
443 continue;
446 if (!mode) {
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",
450 ptr);
452 else {
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",
460 path_name);
462 if (path_name != ptr)
463 free(path_name);
464 continue;
466 bad_line:
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());
485 if (newfd < 0)
486 die("unable to create new cachefile");
488 entries = read_cache();
489 if (entries < 0)
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, "--")) {
497 allow_options = 0;
498 continue;
500 if (!strcmp(path, "-q")) {
501 quiet = 1;
502 continue;
504 if (!strcmp(path, "--add")) {
505 allow_add = 1;
506 continue;
508 if (!strcmp(path, "--replace")) {
509 allow_replace = 1;
510 continue;
512 if (!strcmp(path, "--remove")) {
513 allow_remove = 1;
514 continue;
516 if (!strcmp(path, "--unmerged")) {
517 allow_unmerged = 1;
518 continue;
520 if (!strcmp(path, "--refresh")) {
521 has_errors |= refresh_cache(0);
522 continue;
524 if (!strcmp(path, "--really-refresh")) {
525 has_errors |= refresh_cache(1);
526 continue;
528 if (!strcmp(path, "--cacheinfo")) {
529 unsigned char sha1[20];
530 unsigned int mode;
532 if (i+3 >= argc)
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]);
540 i += 3;
541 continue;
543 if (!strcmp(path, "--chmod=-x") ||
544 !strcmp(path, "--chmod=+x")) {
545 if (argc <= i+1)
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]);
549 continue;
551 if (!strcmp(path, "--assume-unchanged")) {
552 mark_valid_only = MARK_VALID;
553 continue;
555 if (!strcmp(path, "--no-assume-unchanged")) {
556 mark_valid_only = UNMARK_VALID;
557 continue;
559 if (!strcmp(path, "--info-only")) {
560 info_only = 1;
561 continue;
563 if (!strcmp(path, "--force-remove")) {
564 force_remove = 1;
565 continue;
567 if (!strcmp(path, "-z")) {
568 line_termination = 0;
569 continue;
571 if (!strcmp(path, "--stdin")) {
572 if (i != argc - 1)
573 die("--stdin must be at the end");
574 read_from_stdin = 1;
575 break;
577 if (!strcmp(path, "--index-info")) {
578 if (i != argc - 1)
579 die("--index-info must be at the end");
580 allow_add = allow_replace = allow_remove = 1;
581 read_index_info(line_termination);
582 break;
584 if (!strcmp(path, "--ignore-missing")) {
585 not_new = 1;
586 continue;
588 if (!strcmp(path, "--verbose")) {
589 verbose = 1;
590 continue;
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) {
599 struct strbuf buf;
600 strbuf_init(&buf);
601 while (1) {
602 char *path_name;
603 read_line(&buf, stdin, line_termination);
604 if (buf.eof)
605 break;
606 if (line_termination && buf.buf[0] == '"')
607 path_name = unquote_c_style(buf.buf, NULL);
608 else
609 path_name = buf.buf;
610 update_one(path_name, prefix, prefix_length);
611 if (path_name != buf.buf)
612 free(path_name);
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;