git-repack -- respect -q and be quiet
[git/jnareb-git.git] / builtin-update-index.c
blobef50243452c2f27572c941a5db9365c785f1fbd5
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 "cache-tree.h"
10 #include "tree-walk.h"
11 #include "builtin.h"
14 * Default to not allowing changes to the list of files. The
15 * tool doesn't actually care, but this makes it harder to add
16 * files to the revision control by mistake by doing something
17 * like "git-update-index *" and suddenly having all the object
18 * files be revision controlled.
20 static int allow_add;
21 static int allow_remove;
22 static int allow_replace;
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
30 static void report(const char *fmt, ...)
32 va_list vp;
34 if (!verbose)
35 return;
37 va_start(vp, fmt);
38 vprintf(fmt, vp);
39 putchar('\n');
40 va_end(vp);
43 static int mark_valid(const char *path)
45 int namelen = strlen(path);
46 int pos = cache_name_pos(path, namelen);
47 if (0 <= pos) {
48 switch (mark_valid_only) {
49 case MARK_VALID:
50 active_cache[pos]->ce_flags |= htons(CE_VALID);
51 break;
52 case UNMARK_VALID:
53 active_cache[pos]->ce_flags &= ~htons(CE_VALID);
54 break;
56 cache_tree_invalidate_path(active_cache_tree, path);
57 active_cache_changed = 1;
58 return 0;
60 return -1;
63 static int add_file_to_cache(const char *path)
65 int size, namelen, option, status;
66 struct cache_entry *ce;
67 struct stat st;
69 status = lstat(path, &st);
71 /* We probably want to do this in remove_file_from_cache() and
72 * add_cache_entry() instead...
74 cache_tree_invalidate_path(active_cache_tree, path);
76 if (status < 0 || S_ISDIR(st.st_mode)) {
77 /* When we used to have "path" and now we want to add
78 * "path/file", we need a way to remove "path" before
79 * being able to add "path/file". However,
80 * "git-update-index --remove path" would not work.
81 * --force-remove can be used but this is more user
82 * friendly, especially since we can do the opposite
83 * case just fine without --force-remove.
85 if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) {
86 if (allow_remove) {
87 if (remove_file_from_cache(path))
88 return error("%s: cannot remove from the index",
89 path);
90 else
91 return 0;
92 } else if (status < 0) {
93 return error("%s: does not exist and --remove not passed",
94 path);
97 if (0 == status)
98 return error("%s: is a directory - add files inside instead",
99 path);
100 else
101 return error("lstat(\"%s\"): %s", path,
102 strerror(errno));
105 namelen = strlen(path);
106 size = cache_entry_size(namelen);
107 ce = xcalloc(1, size);
108 memcpy(ce->name, path, namelen);
109 ce->ce_flags = htons(namelen);
110 fill_stat_cache_info(ce, &st);
112 ce->ce_mode = create_ce_mode(st.st_mode);
113 if (!trust_executable_bit) {
114 /* If there is an existing entry, pick the mode bits
115 * from it.
117 int pos = cache_name_pos(path, namelen);
118 if (0 <= pos)
119 ce->ce_mode = active_cache[pos]->ce_mode;
122 if (index_path(ce->sha1, path, &st, !info_only))
123 return -1;
124 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
125 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
126 if (add_cache_entry(ce, option))
127 return error("%s: cannot add to the index - missing --add option?",
128 path);
129 return 0;
132 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
133 const char *path, int stage)
135 int size, len, option;
136 struct cache_entry *ce;
138 if (!verify_path(path))
139 return -1;
141 len = strlen(path);
142 size = cache_entry_size(len);
143 ce = xcalloc(1, size);
145 memcpy(ce->sha1, sha1, 20);
146 memcpy(ce->name, path, len);
147 ce->ce_flags = create_ce_flags(len, stage);
148 ce->ce_mode = create_ce_mode(mode);
149 if (assume_unchanged)
150 ce->ce_flags |= htons(CE_VALID);
151 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
152 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
153 if (add_cache_entry(ce, option))
154 return error("%s: cannot add to the index - missing --add option?",
155 path);
156 report("add '%s'", path);
157 cache_tree_invalidate_path(active_cache_tree, path);
158 return 0;
161 static void chmod_path(int flip, const char *path)
163 int pos;
164 struct cache_entry *ce;
165 unsigned int mode;
167 pos = cache_name_pos(path, strlen(path));
168 if (pos < 0)
169 goto fail;
170 ce = active_cache[pos];
171 mode = ntohl(ce->ce_mode);
172 if (!S_ISREG(mode))
173 goto fail;
174 switch (flip) {
175 case '+':
176 ce->ce_mode |= htonl(0111); break;
177 case '-':
178 ce->ce_mode &= htonl(~0111); break;
179 default:
180 goto fail;
182 cache_tree_invalidate_path(active_cache_tree, path);
183 active_cache_changed = 1;
184 report("chmod %cx '%s'", flip, path);
185 return;
186 fail:
187 die("git-update-index: cannot chmod %cx '%s'", flip, path);
190 static void update_one(const char *path, const char *prefix, int prefix_length)
192 const char *p = prefix_path(prefix, prefix_length, path);
193 if (!verify_path(p)) {
194 fprintf(stderr, "Ignoring path %s\n", path);
195 goto free_return;
197 if (mark_valid_only) {
198 if (mark_valid(p))
199 die("Unable to mark file %s", path);
200 goto free_return;
202 cache_tree_invalidate_path(active_cache_tree, path);
204 if (force_remove) {
205 if (remove_file_from_cache(p))
206 die("git-update-index: unable to remove %s", path);
207 report("remove '%s'", path);
208 goto free_return;
210 if (add_file_to_cache(p))
211 die("Unable to process file %s", path);
212 report("add '%s'", path);
213 free_return:
214 if (p < path || p > path + strlen(path))
215 free((char*)p);
218 static void read_index_info(int line_termination)
220 struct strbuf buf;
221 strbuf_init(&buf);
222 while (1) {
223 char *ptr, *tab;
224 char *path_name;
225 unsigned char sha1[20];
226 unsigned int mode;
227 int stage;
229 /* This reads lines formatted in one of three formats:
231 * (1) mode SP sha1 TAB path
232 * The first format is what "git-apply --index-info"
233 * reports, and used to reconstruct a partial tree
234 * that is used for phony merge base tree when falling
235 * back on 3-way merge.
237 * (2) mode SP type SP sha1 TAB path
238 * The second format is to stuff git-ls-tree output
239 * into the index file.
241 * (3) mode SP sha1 SP stage TAB path
242 * This format is to put higher order stages into the
243 * index file and matches git-ls-files --stage output.
245 read_line(&buf, stdin, line_termination);
246 if (buf.eof)
247 break;
249 mode = strtoul(buf.buf, &ptr, 8);
250 if (ptr == buf.buf || *ptr != ' ')
251 goto bad_line;
253 tab = strchr(ptr, '\t');
254 if (!tab || tab - ptr < 41)
255 goto bad_line;
257 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
258 stage = tab[-1] - '0';
259 ptr = tab + 1; /* point at the head of path */
260 tab = tab - 2; /* point at tail of sha1 */
262 else {
263 stage = 0;
264 ptr = tab + 1; /* point at the head of path */
267 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
268 goto bad_line;
270 if (line_termination && ptr[0] == '"')
271 path_name = unquote_c_style(ptr, NULL);
272 else
273 path_name = ptr;
275 if (!verify_path(path_name)) {
276 fprintf(stderr, "Ignoring path %s\n", path_name);
277 if (path_name != ptr)
278 free(path_name);
279 continue;
281 cache_tree_invalidate_path(active_cache_tree, path_name);
283 if (!mode) {
284 /* mode == 0 means there is no such path -- remove */
285 if (remove_file_from_cache(path_name))
286 die("git-update-index: unable to remove %s",
287 ptr);
289 else {
290 /* mode ' ' sha1 '\t' name
291 * ptr[-1] points at tab,
292 * ptr[-41] is at the beginning of sha1
294 ptr[-42] = ptr[-1] = 0;
295 if (add_cacheinfo(mode, sha1, path_name, stage))
296 die("git-update-index: unable to update %s",
297 path_name);
299 if (path_name != ptr)
300 free(path_name);
301 continue;
303 bad_line:
304 die("malformed index info %s", buf.buf);
308 static const char update_index_usage[] =
309 "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>...";
311 static unsigned char head_sha1[20];
312 static unsigned char merge_head_sha1[20];
314 static struct cache_entry *read_one_ent(const char *which,
315 unsigned char *ent, const char *path,
316 int namelen, int stage)
318 unsigned mode;
319 unsigned char sha1[20];
320 int size;
321 struct cache_entry *ce;
323 if (get_tree_entry(ent, path, sha1, &mode)) {
324 if (which)
325 error("%s: not in %s branch.", path, which);
326 return NULL;
328 if (mode == S_IFDIR) {
329 if (which)
330 error("%s: not a blob in %s branch.", path, which);
331 return NULL;
333 size = cache_entry_size(namelen);
334 ce = xcalloc(1, size);
336 memcpy(ce->sha1, sha1, 20);
337 memcpy(ce->name, path, namelen);
338 ce->ce_flags = create_ce_flags(namelen, stage);
339 ce->ce_mode = create_ce_mode(mode);
340 return ce;
343 static int unresolve_one(const char *path)
345 int namelen = strlen(path);
346 int pos;
347 int ret = 0;
348 struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
350 /* See if there is such entry in the index. */
351 pos = cache_name_pos(path, namelen);
352 if (pos < 0) {
353 /* If there isn't, either it is unmerged, or
354 * resolved as "removed" by mistake. We do not
355 * want to do anything in the former case.
357 pos = -pos-1;
358 if (pos < active_nr) {
359 struct cache_entry *ce = active_cache[pos];
360 if (ce_namelen(ce) == namelen &&
361 !memcmp(ce->name, path, namelen)) {
362 fprintf(stderr,
363 "%s: skipping still unmerged path.\n",
364 path);
365 goto free_return;
370 /* Grab blobs from given path from HEAD and MERGE_HEAD,
371 * stuff HEAD version in stage #2,
372 * stuff MERGE_HEAD version in stage #3.
374 ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
375 ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);
377 if (!ce_2 || !ce_3) {
378 ret = -1;
379 goto free_return;
381 if (!memcmp(ce_2->sha1, ce_3->sha1, 20) &&
382 ce_2->ce_mode == ce_3->ce_mode) {
383 fprintf(stderr, "%s: identical in both, skipping.\n",
384 path);
385 goto free_return;
388 cache_tree_invalidate_path(active_cache_tree, path);
389 remove_file_from_cache(path);
390 if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
391 error("%s: cannot add our version to the index.", path);
392 ret = -1;
393 goto free_return;
395 if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
396 return 0;
397 error("%s: cannot add their version to the index.", path);
398 ret = -1;
399 free_return:
400 free(ce_2);
401 free(ce_3);
402 return ret;
405 static void read_head_pointers(void)
407 if (read_ref(git_path("HEAD"), head_sha1))
408 die("No HEAD -- no initial commit yet?\n");
409 if (read_ref(git_path("MERGE_HEAD"), merge_head_sha1)) {
410 fprintf(stderr, "Not in the middle of a merge.\n");
411 exit(0);
415 static int do_unresolve(int ac, const char **av,
416 const char *prefix, int prefix_length)
418 int i;
419 int err = 0;
421 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
422 * are not doing a merge, so exit with success status.
424 read_head_pointers();
426 for (i = 1; i < ac; i++) {
427 const char *arg = av[i];
428 const char *p = prefix_path(prefix, prefix_length, arg);
429 err |= unresolve_one(p);
430 if (p < arg || p > arg + strlen(arg))
431 free((char*)p);
433 return err;
436 static int do_reupdate(int ac, const char **av,
437 const char *prefix, int prefix_length)
439 /* Read HEAD and run update-index on paths that are
440 * merged and already different between index and HEAD.
442 int pos;
443 int has_head = 1;
444 const char **pathspec = get_pathspec(prefix, av + 1);
446 if (read_ref(git_path("HEAD"), head_sha1))
447 /* If there is no HEAD, that means it is an initial
448 * commit. Update everything in the index.
450 has_head = 0;
451 redo:
452 for (pos = 0; pos < active_nr; pos++) {
453 struct cache_entry *ce = active_cache[pos];
454 struct cache_entry *old = NULL;
455 int save_nr;
457 if (ce_stage(ce) || !ce_path_match(ce, pathspec))
458 continue;
459 if (has_head)
460 old = read_one_ent(NULL, head_sha1,
461 ce->name, ce_namelen(ce), 0);
462 if (old && ce->ce_mode == old->ce_mode &&
463 !memcmp(ce->sha1, old->sha1, 20)) {
464 free(old);
465 continue; /* unchanged */
467 /* Be careful. The working tree may not have the
468 * path anymore, in which case, under 'allow_remove',
469 * or worse yet 'allow_replace', active_nr may decrease.
471 save_nr = active_nr;
472 update_one(ce->name + prefix_length, prefix, prefix_length);
473 if (save_nr != active_nr)
474 goto redo;
476 return 0;
479 int cmd_update_index(int argc, const char **argv, char **envp)
481 int i, newfd, entries, has_errors = 0, line_termination = '\n';
482 int allow_options = 1;
483 int read_from_stdin = 0;
484 const char *prefix = setup_git_directory();
485 int prefix_length = prefix ? strlen(prefix) : 0;
486 char set_executable_bit = 0;
487 unsigned int refresh_flags = 0;
488 struct lock_file *lock_file;
490 git_config(git_default_config);
492 /* We can't free this memory, it becomes part of a linked list parsed atexit() */
493 lock_file = xcalloc(1, sizeof(struct lock_file));
495 newfd = hold_lock_file_for_update(lock_file, get_index_file());
496 if (newfd < 0)
497 die("unable to create new cachefile");
499 entries = read_cache();
500 if (entries < 0)
501 die("cache corrupted");
503 for (i = 1 ; i < argc; i++) {
504 const char *path = argv[i];
506 if (allow_options && *path == '-') {
507 if (!strcmp(path, "--")) {
508 allow_options = 0;
509 continue;
511 if (!strcmp(path, "-q")) {
512 refresh_flags |= REFRESH_QUIET;
513 continue;
515 if (!strcmp(path, "--add")) {
516 allow_add = 1;
517 continue;
519 if (!strcmp(path, "--replace")) {
520 allow_replace = 1;
521 continue;
523 if (!strcmp(path, "--remove")) {
524 allow_remove = 1;
525 continue;
527 if (!strcmp(path, "--unmerged")) {
528 refresh_flags |= REFRESH_UNMERGED;
529 continue;
531 if (!strcmp(path, "--refresh")) {
532 has_errors |= refresh_cache(refresh_flags);
533 continue;
535 if (!strcmp(path, "--really-refresh")) {
536 has_errors |= refresh_cache(REFRESH_REALLY | refresh_flags);
537 continue;
539 if (!strcmp(path, "--cacheinfo")) {
540 unsigned char sha1[20];
541 unsigned int mode;
543 if (i+3 >= argc)
544 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
546 if ((sscanf(argv[i+1], "%o", &mode) != 1) ||
547 get_sha1_hex(argv[i+2], sha1) ||
548 add_cacheinfo(mode, sha1, argv[i+3], 0))
549 die("git-update-index: --cacheinfo"
550 " cannot add %s", argv[i+3]);
551 i += 3;
552 continue;
554 if (!strcmp(path, "--chmod=-x") ||
555 !strcmp(path, "--chmod=+x")) {
556 if (argc <= i+1)
557 die("git-update-index: %s <path>", path);
558 set_executable_bit = path[8];
559 continue;
561 if (!strcmp(path, "--assume-unchanged")) {
562 mark_valid_only = MARK_VALID;
563 continue;
565 if (!strcmp(path, "--no-assume-unchanged")) {
566 mark_valid_only = UNMARK_VALID;
567 continue;
569 if (!strcmp(path, "--info-only")) {
570 info_only = 1;
571 continue;
573 if (!strcmp(path, "--force-remove")) {
574 force_remove = 1;
575 continue;
577 if (!strcmp(path, "-z")) {
578 line_termination = 0;
579 continue;
581 if (!strcmp(path, "--stdin")) {
582 if (i != argc - 1)
583 die("--stdin must be at the end");
584 read_from_stdin = 1;
585 break;
587 if (!strcmp(path, "--index-info")) {
588 if (i != argc - 1)
589 die("--index-info must be at the end");
590 allow_add = allow_replace = allow_remove = 1;
591 read_index_info(line_termination);
592 break;
594 if (!strcmp(path, "--unresolve")) {
595 has_errors = do_unresolve(argc - i, argv + i,
596 prefix, prefix_length);
597 if (has_errors)
598 active_cache_changed = 0;
599 goto finish;
601 if (!strcmp(path, "--again")) {
602 has_errors = do_reupdate(argc - i, argv + i,
603 prefix, prefix_length);
604 if (has_errors)
605 active_cache_changed = 0;
606 goto finish;
608 if (!strcmp(path, "--ignore-missing")) {
609 refresh_flags |= REFRESH_IGNORE_MISSING;
610 continue;
612 if (!strcmp(path, "--verbose")) {
613 verbose = 1;
614 continue;
616 if (!strcmp(path, "-h") || !strcmp(path, "--help"))
617 usage(update_index_usage);
618 die("unknown option %s", path);
620 update_one(path, prefix, prefix_length);
621 if (set_executable_bit)
622 chmod_path(set_executable_bit, path);
624 if (read_from_stdin) {
625 struct strbuf buf;
626 strbuf_init(&buf);
627 while (1) {
628 char *path_name;
629 const char *p;
630 read_line(&buf, stdin, line_termination);
631 if (buf.eof)
632 break;
633 if (line_termination && buf.buf[0] == '"')
634 path_name = unquote_c_style(buf.buf, NULL);
635 else
636 path_name = buf.buf;
637 p = prefix_path(prefix, prefix_length, path_name);
638 update_one(p, NULL, 0);
639 if (set_executable_bit)
640 chmod_path(set_executable_bit, p);
641 if (p < path_name || p > path_name + strlen(path_name))
642 free((char*) p);
643 if (path_name != buf.buf)
644 free(path_name);
648 finish:
649 if (active_cache_changed) {
650 if (write_cache(newfd, active_cache, active_nr) ||
651 commit_lock_file(lock_file))
652 die("Unable to write new index file");
655 rollback_lock_file(lock_file);
657 return has_errors ? 1 : 0;