Merge branch 'jc/builtin-n-tar-tree'
[git/kirr.git] / update-index.c
blob5854d11fd9d96536c7001dec9c4f734dde18c674
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;
123 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
124 const char *path, int stage)
126 int size, len, option;
127 struct cache_entry *ce;
129 if (!verify_path(path))
130 return -1;
132 len = strlen(path);
133 size = cache_entry_size(len);
134 ce = xcalloc(1, size);
136 memcpy(ce->sha1, sha1, 20);
137 memcpy(ce->name, path, len);
138 ce->ce_flags = create_ce_flags(len, stage);
139 ce->ce_mode = create_ce_mode(mode);
140 if (assume_unchanged)
141 ce->ce_flags |= htons(CE_VALID);
142 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
143 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
144 if (add_cache_entry(ce, option))
145 return error("%s: cannot add to the index - missing --add option?",
146 path);
147 report("add '%s'", path);
148 return 0;
151 static void chmod_path(int flip, const char *path)
153 int pos;
154 struct cache_entry *ce;
155 unsigned int mode;
157 pos = cache_name_pos(path, strlen(path));
158 if (pos < 0)
159 goto fail;
160 ce = active_cache[pos];
161 mode = ntohl(ce->ce_mode);
162 if (!S_ISREG(mode))
163 goto fail;
164 switch (flip) {
165 case '+':
166 ce->ce_mode |= htonl(0111); break;
167 case '-':
168 ce->ce_mode &= htonl(~0111); break;
169 default:
170 goto fail;
172 active_cache_changed = 1;
173 report("chmod %cx '%s'", flip, path);
174 return;
175 fail:
176 die("git-update-index: cannot chmod %cx '%s'", flip, path);
179 static struct cache_file cache_file;
181 static void update_one(const char *path, const char *prefix, int prefix_length)
183 const char *p = prefix_path(prefix, prefix_length, path);
184 if (!verify_path(p)) {
185 fprintf(stderr, "Ignoring path %s\n", path);
186 goto free_return;
188 if (mark_valid_only) {
189 if (mark_valid(p))
190 die("Unable to mark file %s", path);
191 goto free_return;
194 if (force_remove) {
195 if (remove_file_from_cache(p))
196 die("git-update-index: unable to remove %s", path);
197 report("remove '%s'", path);
198 goto free_return;
200 if (add_file_to_cache(p))
201 die("Unable to process file %s", path);
202 report("add '%s'", path);
203 free_return:
204 if (p < path || p > path + strlen(path))
205 free((char*)p);
208 static void read_index_info(int line_termination)
210 struct strbuf buf;
211 strbuf_init(&buf);
212 while (1) {
213 char *ptr, *tab;
214 char *path_name;
215 unsigned char sha1[20];
216 unsigned int mode;
217 int stage;
219 /* This reads lines formatted in one of three formats:
221 * (1) mode SP sha1 TAB path
222 * The first format is what "git-apply --index-info"
223 * reports, and used to reconstruct a partial tree
224 * that is used for phony merge base tree when falling
225 * back on 3-way merge.
227 * (2) mode SP type SP sha1 TAB path
228 * The second format is to stuff git-ls-tree output
229 * into the index file.
231 * (3) mode SP sha1 SP stage TAB path
232 * This format is to put higher order stages into the
233 * index file and matches git-ls-files --stage output.
235 read_line(&buf, stdin, line_termination);
236 if (buf.eof)
237 break;
239 mode = strtoul(buf.buf, &ptr, 8);
240 if (ptr == buf.buf || *ptr != ' ')
241 goto bad_line;
243 tab = strchr(ptr, '\t');
244 if (!tab || tab - ptr < 41)
245 goto bad_line;
247 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
248 stage = tab[-1] - '0';
249 ptr = tab + 1; /* point at the head of path */
250 tab = tab - 2; /* point at tail of sha1 */
252 else {
253 stage = 0;
254 ptr = tab + 1; /* point at the head of path */
257 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
258 goto bad_line;
260 if (line_termination && ptr[0] == '"')
261 path_name = unquote_c_style(ptr, NULL);
262 else
263 path_name = ptr;
265 if (!verify_path(path_name)) {
266 fprintf(stderr, "Ignoring path %s\n", path_name);
267 if (path_name != ptr)
268 free(path_name);
269 continue;
272 if (!mode) {
273 /* mode == 0 means there is no such path -- remove */
274 if (remove_file_from_cache(path_name))
275 die("git-update-index: unable to remove %s",
276 ptr);
278 else {
279 /* mode ' ' sha1 '\t' name
280 * ptr[-1] points at tab,
281 * ptr[-41] is at the beginning of sha1
283 ptr[-42] = ptr[-1] = 0;
284 if (add_cacheinfo(mode, sha1, path_name, stage))
285 die("git-update-index: unable to update %s",
286 path_name);
288 if (path_name != ptr)
289 free(path_name);
290 continue;
292 bad_line:
293 die("malformed index info %s", buf.buf);
297 static const char update_index_usage[] =
298 "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>...";
300 static unsigned char head_sha1[20];
301 static unsigned char merge_head_sha1[20];
303 static struct cache_entry *read_one_ent(const char *which,
304 unsigned char *ent, const char *path,
305 int namelen, int stage)
307 unsigned mode;
308 unsigned char sha1[20];
309 int size;
310 struct cache_entry *ce;
312 if (get_tree_entry(ent, path, sha1, &mode)) {
313 if (which)
314 error("%s: not in %s branch.", path, which);
315 return NULL;
317 if (mode == S_IFDIR) {
318 if (which)
319 error("%s: not a blob in %s branch.", path, which);
320 return NULL;
322 size = cache_entry_size(namelen);
323 ce = xcalloc(1, size);
325 memcpy(ce->sha1, sha1, 20);
326 memcpy(ce->name, path, namelen);
327 ce->ce_flags = create_ce_flags(namelen, stage);
328 ce->ce_mode = create_ce_mode(mode);
329 return ce;
332 static int unresolve_one(const char *path)
334 int namelen = strlen(path);
335 int pos;
336 int ret = 0;
337 struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
339 /* See if there is such entry in the index. */
340 pos = cache_name_pos(path, namelen);
341 if (pos < 0) {
342 /* If there isn't, either it is unmerged, or
343 * resolved as "removed" by mistake. We do not
344 * want to do anything in the former case.
346 pos = -pos-1;
347 if (pos < active_nr) {
348 struct cache_entry *ce = active_cache[pos];
349 if (ce_namelen(ce) == namelen &&
350 !memcmp(ce->name, path, namelen)) {
351 fprintf(stderr,
352 "%s: skipping still unmerged path.\n",
353 path);
354 goto free_return;
359 /* Grab blobs from given path from HEAD and MERGE_HEAD,
360 * stuff HEAD version in stage #2,
361 * stuff MERGE_HEAD version in stage #3.
363 ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
364 ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);
366 if (!ce_2 || !ce_3) {
367 ret = -1;
368 goto free_return;
370 if (!memcmp(ce_2->sha1, ce_3->sha1, 20) &&
371 ce_2->ce_mode == ce_3->ce_mode) {
372 fprintf(stderr, "%s: identical in both, skipping.\n",
373 path);
374 goto free_return;
377 remove_file_from_cache(path);
378 if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
379 error("%s: cannot add our version to the index.", path);
380 ret = -1;
381 goto free_return;
383 if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
384 return 0;
385 error("%s: cannot add their version to the index.", path);
386 ret = -1;
387 free_return:
388 free(ce_2);
389 free(ce_3);
390 return ret;
393 static void read_head_pointers(void)
395 if (read_ref(git_path("HEAD"), head_sha1))
396 die("No HEAD -- no initial commit yet?\n");
397 if (read_ref(git_path("MERGE_HEAD"), merge_head_sha1)) {
398 fprintf(stderr, "Not in the middle of a merge.\n");
399 exit(0);
403 static int do_unresolve(int ac, const char **av,
404 const char *prefix, int prefix_length)
406 int i;
407 int err = 0;
409 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
410 * are not doing a merge, so exit with success status.
412 read_head_pointers();
414 for (i = 1; i < ac; i++) {
415 const char *arg = av[i];
416 const char *p = prefix_path(prefix, prefix_length, arg);
417 err |= unresolve_one(p);
418 if (p < arg || p > arg + strlen(arg))
419 free((char*)p);
421 return err;
424 static int do_reupdate(int ac, const char **av,
425 const char *prefix, int prefix_length)
427 /* Read HEAD and run update-index on paths that are
428 * merged and already different between index and HEAD.
430 int pos;
431 int has_head = 1;
432 const char **pathspec = get_pathspec(prefix, av + 1);
434 if (read_ref(git_path("HEAD"), head_sha1))
435 /* If there is no HEAD, that means it is an initial
436 * commit. Update everything in the index.
438 has_head = 0;
439 redo:
440 for (pos = 0; pos < active_nr; pos++) {
441 struct cache_entry *ce = active_cache[pos];
442 struct cache_entry *old = NULL;
443 int save_nr;
445 if (ce_stage(ce) || !ce_path_match(ce, pathspec))
446 continue;
447 if (has_head)
448 old = read_one_ent(NULL, head_sha1,
449 ce->name, ce_namelen(ce), 0);
450 if (old && ce->ce_mode == old->ce_mode &&
451 !memcmp(ce->sha1, old->sha1, 20)) {
452 free(old);
453 continue; /* unchanged */
455 /* Be careful. The working tree may not have the
456 * path anymore, in which case, under 'allow_remove',
457 * or worse yet 'allow_replace', active_nr may decrease.
459 save_nr = active_nr;
460 update_one(ce->name + prefix_length, prefix, prefix_length);
461 if (save_nr != active_nr)
462 goto redo;
464 return 0;
467 int main(int argc, const char **argv)
469 int i, newfd, entries, has_errors = 0, line_termination = '\n';
470 int allow_options = 1;
471 int read_from_stdin = 0;
472 const char *prefix = setup_git_directory();
473 int prefix_length = prefix ? strlen(prefix) : 0;
474 char set_executable_bit = 0;
475 unsigned int refresh_flags = 0;
477 git_config(git_default_config);
479 newfd = hold_index_file_for_update(&cache_file, get_index_file());
480 if (newfd < 0)
481 die("unable to create new cachefile");
483 entries = read_cache();
484 if (entries < 0)
485 die("cache corrupted");
487 for (i = 1 ; i < argc; i++) {
488 const char *path = argv[i];
490 if (allow_options && *path == '-') {
491 if (!strcmp(path, "--")) {
492 allow_options = 0;
493 continue;
495 if (!strcmp(path, "-q")) {
496 refresh_flags |= REFRESH_QUIET;
497 continue;
499 if (!strcmp(path, "--add")) {
500 allow_add = 1;
501 continue;
503 if (!strcmp(path, "--replace")) {
504 allow_replace = 1;
505 continue;
507 if (!strcmp(path, "--remove")) {
508 allow_remove = 1;
509 continue;
511 if (!strcmp(path, "--unmerged")) {
512 refresh_flags |= REFRESH_UNMERGED;
513 continue;
515 if (!strcmp(path, "--refresh")) {
516 has_errors |= refresh_cache(refresh_flags);
517 continue;
519 if (!strcmp(path, "--really-refresh")) {
520 has_errors |= refresh_cache(REFRESH_REALLY | refresh_flags);
521 continue;
523 if (!strcmp(path, "--cacheinfo")) {
524 unsigned char sha1[20];
525 unsigned int mode;
527 if (i+3 >= argc)
528 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
530 if ((sscanf(argv[i+1], "%o", &mode) != 1) ||
531 get_sha1_hex(argv[i+2], sha1) ||
532 add_cacheinfo(mode, sha1, argv[i+3], 0))
533 die("git-update-index: --cacheinfo"
534 " cannot add %s", argv[i+3]);
535 i += 3;
536 continue;
538 if (!strcmp(path, "--chmod=-x") ||
539 !strcmp(path, "--chmod=+x")) {
540 if (argc <= i+1)
541 die("git-update-index: %s <path>", path);
542 set_executable_bit = path[8];
543 continue;
545 if (!strcmp(path, "--assume-unchanged")) {
546 mark_valid_only = MARK_VALID;
547 continue;
549 if (!strcmp(path, "--no-assume-unchanged")) {
550 mark_valid_only = UNMARK_VALID;
551 continue;
553 if (!strcmp(path, "--info-only")) {
554 info_only = 1;
555 continue;
557 if (!strcmp(path, "--force-remove")) {
558 force_remove = 1;
559 continue;
561 if (!strcmp(path, "-z")) {
562 line_termination = 0;
563 continue;
565 if (!strcmp(path, "--stdin")) {
566 if (i != argc - 1)
567 die("--stdin must be at the end");
568 read_from_stdin = 1;
569 break;
571 if (!strcmp(path, "--index-info")) {
572 if (i != argc - 1)
573 die("--index-info must be at the end");
574 allow_add = allow_replace = allow_remove = 1;
575 read_index_info(line_termination);
576 break;
578 if (!strcmp(path, "--unresolve")) {
579 has_errors = do_unresolve(argc - i, argv + i,
580 prefix, prefix_length);
581 if (has_errors)
582 active_cache_changed = 0;
583 goto finish;
585 if (!strcmp(path, "--again")) {
586 has_errors = do_reupdate(argc - i, argv + i,
587 prefix, prefix_length);
588 if (has_errors)
589 active_cache_changed = 0;
590 goto finish;
592 if (!strcmp(path, "--ignore-missing")) {
593 refresh_flags |= REFRESH_IGNORE_MISSING;
594 continue;
596 if (!strcmp(path, "--verbose")) {
597 verbose = 1;
598 continue;
600 if (!strcmp(path, "-h") || !strcmp(path, "--help"))
601 usage(update_index_usage);
602 die("unknown option %s", path);
604 update_one(path, prefix, prefix_length);
605 if (set_executable_bit)
606 chmod_path(set_executable_bit, path);
608 if (read_from_stdin) {
609 struct strbuf buf;
610 strbuf_init(&buf);
611 while (1) {
612 char *path_name;
613 const char *p;
614 read_line(&buf, stdin, line_termination);
615 if (buf.eof)
616 break;
617 if (line_termination && buf.buf[0] == '"')
618 path_name = unquote_c_style(buf.buf, NULL);
619 else
620 path_name = buf.buf;
621 p = prefix_path(prefix, prefix_length, path_name);
622 update_one(p, NULL, 0);
623 if (set_executable_bit)
624 chmod_path(set_executable_bit, p);
625 if (p < path_name || p > path_name + strlen(path_name))
626 free((char*) p);
627 if (path_name != buf.buf)
628 free(path_name);
632 finish:
633 if (active_cache_changed) {
634 if (write_cache(newfd, active_cache, active_nr) ||
635 commit_index_file(&cache_file))
636 die("Unable to write new cachefile");
639 return has_errors ? 1 : 0;