X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=update-index.c;h=01b4088ad661691c8a8b778071b12527bfc45c3c;hb=380792390e05e744f9d7eefbc35d1db80e44e27a;hp=cb0265bdf7b39acbac2252117cde41a40733614d;hpb=dbc37438687e110697574d175e4eca5f9cbeae81;p=git.git diff --git a/update-index.c b/update-index.c index cb0265bd..01b4088a 100644 --- a/update-index.c +++ b/update-index.c @@ -4,6 +4,7 @@ * Copyright (C) Linus Torvalds, 2005 */ #include "cache.h" +#include "strbuf.h" /* * Default to not allowing changes to the list of files. The @@ -12,7 +13,7 @@ * like "git-update-index *" and suddenly having all the object * files be revision controlled. */ -static int allow_add = 0, allow_remove = 0, allow_replace = 0, not_new = 0, quiet = 0, info_only = 0; +static int allow_add = 0, allow_remove = 0, allow_replace = 0, allow_unmerged = 0, not_new = 0, quiet = 0, info_only = 0; static int force_remove; /* Three functions to allow overloaded pointer return; see linux/err.h */ @@ -31,13 +32,11 @@ static inline long IS_ERR(const void *ptr) return (unsigned long)ptr > (unsigned long)-1000L; } -static int add_file_to_cache(char *path) +static int add_file_to_cache(const char *path) { int size, namelen, option, status; struct cache_entry *ce; struct stat st; - int fd; - char *target; status = lstat(path, &st); if (status < 0 || S_ISDIR(st.st_mode)) { @@ -76,34 +75,9 @@ static int add_file_to_cache(char *path) fill_stat_cache_info(ce, &st); ce->ce_mode = create_ce_mode(st.st_mode); ce->ce_flags = htons(namelen); - switch (st.st_mode & S_IFMT) { - case S_IFREG: - fd = open(path, O_RDONLY); - if (fd < 0) - return error("open(\"%s\"): %s", path, strerror(errno)); - if (index_fd(ce->sha1, fd, &st, !info_only, NULL) < 0) - return error("%s: failed to insert into database", path); - break; - case S_IFLNK: - target = xmalloc(st.st_size+1); - if (readlink(path, target, st.st_size+1) != st.st_size) { - char *errstr = strerror(errno); - free(target); - return error("readlink(\"%s\"): %s", path, - errstr); - } - if (info_only) { - unsigned char hdr[50]; - int hdrlen; - write_sha1_file_prepare(target, st.st_size, "blob", - ce->sha1, hdr, &hdrlen); - } else if (write_sha1_file(target, st.st_size, "blob", ce->sha1)) - return error("%s: failed to insert into database", path); - free(target); - break; - default: - return error("%s: unsupported file type", path); - } + + if (index_path(ce->sha1, path, &st, !info_only)) + return -1; option = allow_add ? ADD_CACHE_OK_TO_ADD : 0; option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0; if (add_cache_entry(ce, option)) @@ -134,7 +108,7 @@ static struct cache_entry *refresh_entry(struct cache_entry *ce) changed = ce_match_stat(ce, &st); if (!changed) - return ce; + return NULL; if (ce_modified(ce, &st)) return ERR_PTR(-EINVAL); @@ -155,16 +129,20 @@ static int refresh_cache(void) struct cache_entry *ce, *new; ce = active_cache[i]; if (ce_stage(ce)) { - printf("%s: needs merge\n", ce->name); - has_errors = 1; while ((i < active_nr) && ! strcmp(active_cache[i]->name, ce->name)) i++; i--; + if (allow_unmerged) + continue; + printf("%s: needs merge\n", ce->name); + has_errors = 1; continue; } new = refresh_entry(ce); + if (!new) + continue; if (IS_ERR(new)) { if (not_new && PTR_ERR(new) == -ENOENT) continue; @@ -221,7 +199,7 @@ static int verify_dotfile(const char *rest) return 1; } -static int verify_path(char *path) +static int verify_path(const char *path) { char c; @@ -247,7 +225,7 @@ inside: } } -static int add_cacheinfo(char *arg1, char *arg2, char *arg3) +static int add_cacheinfo(const char *arg1, const char *arg2, const char *arg3) { int size, len, option; unsigned int mode; @@ -277,11 +255,78 @@ static int add_cacheinfo(char *arg1, char *arg2, char *arg3) static struct cache_file cache_file; -int main(int argc, char **argv) + +static void update_one(const char *path, const char *prefix, int prefix_length) { - int i, newfd, entries, has_errors = 0; + const char *p = prefix_path(prefix, prefix_length, path); + if (!verify_path(p)) { + fprintf(stderr, "Ignoring path %s\n", path); + return; + } + if (force_remove) { + if (remove_file_from_cache(p)) + die("git-update-index: unable to remove %s", path); + return; + } + if (add_file_to_cache(p)) + die("Unable to process file %s", path); +} + +static void read_index_info(int line_termination) +{ + struct strbuf buf; + strbuf_init(&buf); + while (1) { + char *ptr; + unsigned char sha1[20]; + unsigned int mode; + + read_line(&buf, stdin, line_termination); + if (buf.eof) + break; + + mode = strtoul(buf.buf, &ptr, 8); + if (ptr == buf.buf || *ptr != ' ' || + get_sha1_hex(ptr + 1, sha1) || + ptr[41] != '\t') + goto bad_line; + + ptr += 42; + if (!verify_path(ptr)) { + fprintf(stderr, "Ignoring path %s\n", ptr); + continue; + } + + if (!mode) { + /* mode == 0 means there is no such path -- remove */ + if (remove_file_from_cache(ptr)) + die("git-update-index: unable to remove %s", + ptr); + } + else { + /* mode ' ' sha1 '\t' name + * ptr[-1] points at tab, + * ptr[-41] is at the beginning of sha1 + */ + ptr[-42] = ptr[-1] = 0; + if (add_cacheinfo(buf.buf, ptr-41, ptr)) + die("git-update-index: unable to update %s", + ptr); + } + continue; + + bad_line: + die("malformed index info %s", buf.buf); + } +} + +int main(int argc, const char **argv) +{ + int i, newfd, entries, has_errors = 0, line_termination = '\n'; int allow_options = 1; + int read_from_stdin = 0; const char *prefix = setup_git_directory(); + int prefix_length = prefix ? strlen(prefix) : 0; newfd = hold_index_file_for_update(&cache_file, get_index_file()); if (newfd < 0) @@ -292,7 +337,7 @@ int main(int argc, char **argv) die("cache corrupted"); for (i = 1 ; i < argc; i++) { - char *path = argv[i]; + const char *path = argv[i]; if (allow_options && *path == '-') { if (!strcmp(path, "--")) { @@ -315,6 +360,10 @@ int main(int argc, char **argv) allow_remove = 1; continue; } + if (!strcmp(path, "--unmerged")) { + allow_unmerged = 1; + continue; + } if (!strcmp(path, "--refresh")) { has_errors |= refresh_cache(); continue; @@ -335,29 +384,44 @@ int main(int argc, char **argv) force_remove = 1; continue; } - + if (!strcmp(path, "-z")) { + line_termination = 0; + continue; + } + if (!strcmp(path, "--stdin")) { + if (i != argc - 1) + die("--stdin must be at the end"); + read_from_stdin = 1; + break; + } + if (!strcmp(path, "--index-info")) { + allow_add = allow_replace = allow_remove = 1; + read_index_info(line_termination); + continue; + } if (!strcmp(path, "--ignore-missing")) { not_new = 1; continue; } die("unknown option %s", path); } - path = prefix_path(prefix, prefix ? strlen(prefix) : 0, path); - if (!verify_path(path)) { - fprintf(stderr, "Ignoring path %s\n", argv[i]); - continue; - } - if (force_remove) { - if (remove_file_from_cache(path)) - die("git-update-index: unable to remove %s", path); - continue; + update_one(path, prefix, prefix_length); + } + if (read_from_stdin) { + struct strbuf buf; + strbuf_init(&buf); + while (1) { + read_line(&buf, stdin, line_termination); + if (buf.eof) + break; + update_one(buf.buf, prefix, prefix_length); } - if (add_file_to_cache(path)) - die("Unable to process file %s", path); } - if (write_cache(newfd, active_cache, active_nr) || - commit_index_file(&cache_file)) - die("Unable to write new cachefile"); + if (active_cache_changed) { + if (write_cache(newfd, active_cache, active_nr) || + commit_index_file(&cache_file)) + die("Unable to write new cachefile"); + } return has_errors ? 1 : 0; }