Merge branch 'jc/grep'
[git/dscho.git] / update-index.c
blob7d6de821e23fc02b19b970df94200edd31bc864f
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"
9 #include "tree-walk.h"
12 * Default to not allowing changes to the list of files. The
13 * tool doesn't actually care, but this makes it harder to add
14 * files to the revision control by mistake by doing something
15 * like "git-update-index *" and suddenly having all the object
16 * files be revision controlled.
18 static int allow_add;
19 static int allow_remove;
20 static int allow_replace;
21 static int info_only;
22 static int force_remove;
23 static int verbose;
24 static int mark_valid_only = 0;
25 #define MARK_VALID 1
26 #define UNMARK_VALID 2
28 static void report(const char *fmt, ...)
30 va_list vp;
32 if (!verbose)
33 return;
35 va_start(vp, fmt);
36 vprintf(fmt, vp);
37 putchar('\n');
38 va_end(vp);
41 static int mark_valid(const char *path)
43 int namelen = strlen(path);
44 int pos = cache_name_pos(path, namelen);
45 if (0 <= pos) {
46 switch (mark_valid_only) {
47 case MARK_VALID:
48 active_cache[pos]->ce_flags |= htons(CE_VALID);
49 break;
50 case UNMARK_VALID:
51 active_cache[pos]->ce_flags &= ~htons(CE_VALID);
52 break;
54 active_cache_changed = 1;
55 return 0;
57 return -1;
60 static int add_file_to_cache(const char *path)
62 int size, namelen, option, status;
63 struct cache_entry *ce;
64 struct stat st;
66 status = lstat(path, &st);
67 if (status < 0 || S_ISDIR(st.st_mode)) {
68 /* When we used to have "path" and now we want to add
69 * "path/file", we need a way to remove "path" before
70 * being able to add "path/file". However,
71 * "git-update-index --remove path" would not work.
72 * --force-remove can be used but this is more user
73 * friendly, especially since we can do the opposite
74 * case just fine without --force-remove.
76 if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) {
77 if (allow_remove) {
78 if (remove_file_from_cache(path))
79 return error("%s: cannot remove from the index",
80 path);
81 else
82 return 0;
83 } else if (status < 0) {
84 return error("%s: does not exist and --remove not passed",
85 path);
88 if (0 == status)
89 return error("%s: is a directory - add files inside instead",
90 path);
91 else
92 return error("lstat(\"%s\"): %s", path,
93 strerror(errno));
96 namelen = strlen(path);
97 size = cache_entry_size(namelen);
98 ce = xcalloc(1, size);
99 memcpy(ce->name, path, namelen);
100 ce->ce_flags = htons(namelen);
101 fill_stat_cache_info(ce, &st);
103 ce->ce_mode = create_ce_mode(st.st_mode);
104 if (!trust_executable_bit) {
105 /* If there is an existing entry, pick the mode bits
106 * from it.
108 int pos = cache_name_pos(path, namelen);
109 if (0 <= pos)
110 ce->ce_mode = active_cache[pos]->ce_mode;
113 if (index_path(ce->sha1, path, &st, !info_only))
114 return -1;
115 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
116 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
117 if (add_cache_entry(ce, option))
118 return error("%s: cannot add to the index - missing --add option?",
119 path);
120 return 0;
124 * We fundamentally don't like some paths: we don't want
125 * dot or dot-dot anywhere, and for obvious reasons don't
126 * want to recurse into ".git" either.
128 * Also, we don't want double slashes or slashes at the
129 * end that can make pathnames ambiguous.
131 static int verify_dotfile(const char *rest)
134 * The first character was '.', but that
135 * has already been discarded, we now test
136 * the rest.
138 switch (*rest) {
139 /* "." is not allowed */
140 case '\0': case '/':
141 return 0;
144 * ".git" followed by NUL or slash is bad. This
145 * shares the path end test with the ".." case.
147 case 'g':
148 if (rest[1] != 'i')
149 break;
150 if (rest[2] != 't')
151 break;
152 rest += 2;
153 /* fallthrough */
154 case '.':
155 if (rest[1] == '\0' || rest[1] == '/')
156 return 0;
158 return 1;
161 static int verify_path(const char *path)
163 char c;
165 goto inside;
166 for (;;) {
167 if (!c)
168 return 1;
169 if (c == '/') {
170 inside:
171 c = *path++;
172 switch (c) {
173 default:
174 continue;
175 case '/': case '\0':
176 break;
177 case '.':
178 if (verify_dotfile(path))
179 continue;
181 return 0;
183 c = *path++;
187 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
188 const char *path, int stage)
190 int size, len, option;
191 struct cache_entry *ce;
193 if (!verify_path(path))
194 return -1;
196 len = strlen(path);
197 size = cache_entry_size(len);
198 ce = xcalloc(1, size);
200 memcpy(ce->sha1, sha1, 20);
201 memcpy(ce->name, path, len);
202 ce->ce_flags = create_ce_flags(len, stage);
203 ce->ce_mode = create_ce_mode(mode);
204 if (assume_unchanged)
205 ce->ce_flags |= htons(CE_VALID);
206 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
207 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
208 if (add_cache_entry(ce, option))
209 return error("%s: cannot add to the index - missing --add option?",
210 path);
211 report("add '%s'", path);
212 return 0;
215 static void chmod_path(int flip, const char *path)
217 int pos;
218 struct cache_entry *ce;
219 unsigned int mode;
221 pos = cache_name_pos(path, strlen(path));
222 if (pos < 0)
223 goto fail;
224 ce = active_cache[pos];
225 mode = ntohl(ce->ce_mode);
226 if (!S_ISREG(mode))
227 goto fail;
228 switch (flip) {
229 case '+':
230 ce->ce_mode |= htonl(0111); break;
231 case '-':
232 ce->ce_mode &= htonl(~0111); break;
233 default:
234 goto fail;
236 active_cache_changed = 1;
237 report("chmod %cx '%s'", flip, path);
238 return;
239 fail:
240 die("git-update-index: cannot chmod %cx '%s'", flip, path);
243 static struct cache_file cache_file;
245 static void update_one(const char *path, const char *prefix, int prefix_length)
247 const char *p = prefix_path(prefix, prefix_length, path);
248 if (!verify_path(p)) {
249 fprintf(stderr, "Ignoring path %s\n", path);
250 goto free_return;
252 if (mark_valid_only) {
253 if (mark_valid(p))
254 die("Unable to mark file %s", path);
255 goto free_return;
258 if (force_remove) {
259 if (remove_file_from_cache(p))
260 die("git-update-index: unable to remove %s", path);
261 report("remove '%s'", path);
262 goto free_return;
264 if (add_file_to_cache(p))
265 die("Unable to process file %s", path);
266 report("add '%s'", path);
267 free_return:
268 if (p < path || p > path + strlen(path))
269 free((char*)p);
272 static void read_index_info(int line_termination)
274 struct strbuf buf;
275 strbuf_init(&buf);
276 while (1) {
277 char *ptr, *tab;
278 char *path_name;
279 unsigned char sha1[20];
280 unsigned int mode;
281 int stage;
283 /* This reads lines formatted in one of three formats:
285 * (1) mode SP sha1 TAB path
286 * The first format is what "git-apply --index-info"
287 * reports, and used to reconstruct a partial tree
288 * that is used for phony merge base tree when falling
289 * back on 3-way merge.
291 * (2) mode SP type SP sha1 TAB path
292 * The second format is to stuff git-ls-tree output
293 * into the index file.
295 * (3) mode SP sha1 SP stage TAB path
296 * This format is to put higher order stages into the
297 * index file and matches git-ls-files --stage output.
299 read_line(&buf, stdin, line_termination);
300 if (buf.eof)
301 break;
303 mode = strtoul(buf.buf, &ptr, 8);
304 if (ptr == buf.buf || *ptr != ' ')
305 goto bad_line;
307 tab = strchr(ptr, '\t');
308 if (!tab || tab - ptr < 41)
309 goto bad_line;
311 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
312 stage = tab[-1] - '0';
313 ptr = tab + 1; /* point at the head of path */
314 tab = tab - 2; /* point at tail of sha1 */
316 else {
317 stage = 0;
318 ptr = tab + 1; /* point at the head of path */
321 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
322 goto bad_line;
324 if (line_termination && ptr[0] == '"')
325 path_name = unquote_c_style(ptr, NULL);
326 else
327 path_name = ptr;
329 if (!verify_path(path_name)) {
330 fprintf(stderr, "Ignoring path %s\n", path_name);
331 if (path_name != ptr)
332 free(path_name);
333 continue;
336 if (!mode) {
337 /* mode == 0 means there is no such path -- remove */
338 if (remove_file_from_cache(path_name))
339 die("git-update-index: unable to remove %s",
340 ptr);
342 else {
343 /* mode ' ' sha1 '\t' name
344 * ptr[-1] points at tab,
345 * ptr[-41] is at the beginning of sha1
347 ptr[-42] = ptr[-1] = 0;
348 if (add_cacheinfo(mode, sha1, path_name, stage))
349 die("git-update-index: unable to update %s",
350 path_name);
352 if (path_name != ptr)
353 free(path_name);
354 continue;
356 bad_line:
357 die("malformed index info %s", buf.buf);
361 static const char update_index_usage[] =
362 "git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--really-refresh] [--cacheinfo] [--chmod=(+|-)x] [--assume-unchanged] [--info-only] [--force-remove] [--stdin] [--index-info] [--unresolve] [--again] [--ignore-missing] [-z] [--verbose] [--] <file>...";
364 static unsigned char head_sha1[20];
365 static unsigned char merge_head_sha1[20];
367 static struct cache_entry *read_one_ent(const char *which,
368 unsigned char *ent, const char *path,
369 int namelen, int stage)
371 unsigned mode;
372 unsigned char sha1[20];
373 int size;
374 struct cache_entry *ce;
376 if (get_tree_entry(ent, path, sha1, &mode)) {
377 if (which)
378 error("%s: not in %s branch.", path, which);
379 return NULL;
381 if (mode == S_IFDIR) {
382 if (which)
383 error("%s: not a blob in %s branch.", path, which);
384 return NULL;
386 size = cache_entry_size(namelen);
387 ce = xcalloc(1, size);
389 memcpy(ce->sha1, sha1, 20);
390 memcpy(ce->name, path, namelen);
391 ce->ce_flags = create_ce_flags(namelen, stage);
392 ce->ce_mode = create_ce_mode(mode);
393 return ce;
396 static int unresolve_one(const char *path)
398 int namelen = strlen(path);
399 int pos;
400 int ret = 0;
401 struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
403 /* See if there is such entry in the index. */
404 pos = cache_name_pos(path, namelen);
405 if (pos < 0) {
406 /* If there isn't, either it is unmerged, or
407 * resolved as "removed" by mistake. We do not
408 * want to do anything in the former case.
410 pos = -pos-1;
411 if (pos < active_nr) {
412 struct cache_entry *ce = active_cache[pos];
413 if (ce_namelen(ce) == namelen &&
414 !memcmp(ce->name, path, namelen)) {
415 fprintf(stderr,
416 "%s: skipping still unmerged path.\n",
417 path);
418 goto free_return;
423 /* Grab blobs from given path from HEAD and MERGE_HEAD,
424 * stuff HEAD version in stage #2,
425 * stuff MERGE_HEAD version in stage #3.
427 ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
428 ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);
430 if (!ce_2 || !ce_3) {
431 ret = -1;
432 goto free_return;
434 if (!memcmp(ce_2->sha1, ce_3->sha1, 20) &&
435 ce_2->ce_mode == ce_3->ce_mode) {
436 fprintf(stderr, "%s: identical in both, skipping.\n",
437 path);
438 goto free_return;
441 remove_file_from_cache(path);
442 if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
443 error("%s: cannot add our version to the index.", path);
444 ret = -1;
445 goto free_return;
447 if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
448 return 0;
449 error("%s: cannot add their version to the index.", path);
450 ret = -1;
451 free_return:
452 free(ce_2);
453 free(ce_3);
454 return ret;
457 static void read_head_pointers(void)
459 if (read_ref(git_path("HEAD"), head_sha1))
460 die("No HEAD -- no initial commit yet?\n");
461 if (read_ref(git_path("MERGE_HEAD"), merge_head_sha1)) {
462 fprintf(stderr, "Not in the middle of a merge.\n");
463 exit(0);
467 static int do_unresolve(int ac, const char **av,
468 const char *prefix, int prefix_length)
470 int i;
471 int err = 0;
473 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
474 * are not doing a merge, so exit with success status.
476 read_head_pointers();
478 for (i = 1; i < ac; i++) {
479 const char *arg = av[i];
480 const char *p = prefix_path(prefix, prefix_length, arg);
481 err |= unresolve_one(p);
482 if (p < arg || p > arg + strlen(arg))
483 free((char*)p);
485 return err;
488 static int do_reupdate(int ac, const char **av,
489 const char *prefix, int prefix_length)
491 /* Read HEAD and run update-index on paths that are
492 * merged and already different between index and HEAD.
494 int pos;
495 int has_head = 1;
496 const char **pathspec = get_pathspec(prefix, av + 1);
498 if (read_ref(git_path("HEAD"), head_sha1))
499 /* If there is no HEAD, that means it is an initial
500 * commit. Update everything in the index.
502 has_head = 0;
503 redo:
504 for (pos = 0; pos < active_nr; pos++) {
505 struct cache_entry *ce = active_cache[pos];
506 struct cache_entry *old = NULL;
507 int save_nr;
509 if (ce_stage(ce) || !ce_path_match(ce, pathspec))
510 continue;
511 if (has_head)
512 old = read_one_ent(NULL, head_sha1,
513 ce->name, ce_namelen(ce), 0);
514 if (old && ce->ce_mode == old->ce_mode &&
515 !memcmp(ce->sha1, old->sha1, 20)) {
516 free(old);
517 continue; /* unchanged */
519 /* Be careful. The working tree may not have the
520 * path anymore, in which case, under 'allow_remove',
521 * or worse yet 'allow_replace', active_nr may decrease.
523 save_nr = active_nr;
524 update_one(ce->name + prefix_length, prefix, prefix_length);
525 if (save_nr != active_nr)
526 goto redo;
528 return 0;
531 int main(int argc, const char **argv)
533 int i, newfd, entries, has_errors = 0, line_termination = '\n';
534 int allow_options = 1;
535 int read_from_stdin = 0;
536 const char *prefix = setup_git_directory();
537 int prefix_length = prefix ? strlen(prefix) : 0;
538 char set_executable_bit = 0;
539 unsigned int refresh_flags = 0;
541 git_config(git_default_config);
543 newfd = hold_index_file_for_update(&cache_file, get_index_file());
544 if (newfd < 0)
545 die("unable to create new cachefile");
547 entries = read_cache();
548 if (entries < 0)
549 die("cache corrupted");
551 for (i = 1 ; i < argc; i++) {
552 const char *path = argv[i];
554 if (allow_options && *path == '-') {
555 if (!strcmp(path, "--")) {
556 allow_options = 0;
557 continue;
559 if (!strcmp(path, "-q")) {
560 refresh_flags |= REFRESH_QUIET;
561 continue;
563 if (!strcmp(path, "--add")) {
564 allow_add = 1;
565 continue;
567 if (!strcmp(path, "--replace")) {
568 allow_replace = 1;
569 continue;
571 if (!strcmp(path, "--remove")) {
572 allow_remove = 1;
573 continue;
575 if (!strcmp(path, "--unmerged")) {
576 refresh_flags |= REFRESH_UNMERGED;
577 continue;
579 if (!strcmp(path, "--refresh")) {
580 has_errors |= refresh_cache(refresh_flags);
581 continue;
583 if (!strcmp(path, "--really-refresh")) {
584 has_errors |= refresh_cache(REFRESH_REALLY | refresh_flags);
585 continue;
587 if (!strcmp(path, "--cacheinfo")) {
588 unsigned char sha1[20];
589 unsigned int mode;
591 if (i+3 >= argc)
592 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
594 if ((sscanf(argv[i+1], "%o", &mode) != 1) ||
595 get_sha1_hex(argv[i+2], sha1) ||
596 add_cacheinfo(mode, sha1, argv[i+3], 0))
597 die("git-update-index: --cacheinfo"
598 " cannot add %s", argv[i+3]);
599 i += 3;
600 continue;
602 if (!strcmp(path, "--chmod=-x") ||
603 !strcmp(path, "--chmod=+x")) {
604 if (argc <= i+1)
605 die("git-update-index: %s <path>", path);
606 set_executable_bit = path[8];
607 continue;
609 if (!strcmp(path, "--assume-unchanged")) {
610 mark_valid_only = MARK_VALID;
611 continue;
613 if (!strcmp(path, "--no-assume-unchanged")) {
614 mark_valid_only = UNMARK_VALID;
615 continue;
617 if (!strcmp(path, "--info-only")) {
618 info_only = 1;
619 continue;
621 if (!strcmp(path, "--force-remove")) {
622 force_remove = 1;
623 continue;
625 if (!strcmp(path, "-z")) {
626 line_termination = 0;
627 continue;
629 if (!strcmp(path, "--stdin")) {
630 if (i != argc - 1)
631 die("--stdin must be at the end");
632 read_from_stdin = 1;
633 break;
635 if (!strcmp(path, "--index-info")) {
636 if (i != argc - 1)
637 die("--index-info must be at the end");
638 allow_add = allow_replace = allow_remove = 1;
639 read_index_info(line_termination);
640 break;
642 if (!strcmp(path, "--unresolve")) {
643 has_errors = do_unresolve(argc - i, argv + i,
644 prefix, prefix_length);
645 if (has_errors)
646 active_cache_changed = 0;
647 goto finish;
649 if (!strcmp(path, "--again")) {
650 has_errors = do_reupdate(argc - i, argv + i,
651 prefix, prefix_length);
652 if (has_errors)
653 active_cache_changed = 0;
654 goto finish;
656 if (!strcmp(path, "--ignore-missing")) {
657 refresh_flags |= REFRESH_IGNORE_MISSING;
658 continue;
660 if (!strcmp(path, "--verbose")) {
661 verbose = 1;
662 continue;
664 if (!strcmp(path, "-h") || !strcmp(path, "--help"))
665 usage(update_index_usage);
666 die("unknown option %s", path);
668 update_one(path, prefix, prefix_length);
669 if (set_executable_bit)
670 chmod_path(set_executable_bit, path);
672 if (read_from_stdin) {
673 struct strbuf buf;
674 strbuf_init(&buf);
675 while (1) {
676 char *path_name;
677 const char *p;
678 read_line(&buf, stdin, line_termination);
679 if (buf.eof)
680 break;
681 if (line_termination && buf.buf[0] == '"')
682 path_name = unquote_c_style(buf.buf, NULL);
683 else
684 path_name = buf.buf;
685 p = prefix_path(prefix, prefix_length, path_name);
686 update_one(p, NULL, 0);
687 if (set_executable_bit)
688 chmod_path(set_executable_bit, p);
689 if (p < path_name || p > path_name + strlen(path_name))
690 free((char*) p);
691 if (path_name != buf.buf)
692 free(path_name);
696 finish:
697 if (active_cache_changed) {
698 if (write_cache(newfd, active_cache, active_nr) ||
699 commit_index_file(&cache_file))
700 die("Unable to write new cachefile");
703 return has_errors ? 1 : 0;