static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const char *base, int baselen, struct diff_options *opt) { unsigned mode1, mode2; const char *path1, *path2; const unsigned char *sha1, *sha2; int cmp, pathlen1, pathlen2; char *fullname; sha1 = tree_entry_extract(t1, &path1, &mode1); sha2 = tree_entry_extract(t2, &path2, &mode2); pathlen1 = tree_entry_len(path1, sha1); pathlen2 = tree_entry_len(path2, sha2); cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2); if (cmp < 0) { show_entry(opt, "-", t1, base, baselen); return -1; } if (cmp > 0) { show_entry(opt, "+", t2, base, baselen); return 1; } if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER) && !hashcmp(sha1, sha2) && mode1 == mode2) return 0; /* * If the filemode has changed to/from a directory from/to a regular * file, we need to consider it a remove and an add. */ if (S_ISDIR(mode1) != S_ISDIR(mode2)) { show_entry(opt, "-", t1, base, baselen); show_entry(opt, "+", t2, base, baselen); return 0; } if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode1)) { int retval; char *newbase = malloc_base(base, baselen, path1, pathlen1); if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) { newbase[baselen + pathlen1] = 0; opt->change(opt, mode1, mode2, sha1, sha2, newbase); newbase[baselen + pathlen1] = '/'; } retval = diff_tree_sha1(sha1, sha2, newbase, opt); free(newbase); return retval; } fullname = malloc_fullname(base, baselen, path1, pathlen1); opt->change(opt, mode1, mode2, sha1, sha2, fullname); free(fullname); return 0; }
static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, struct strbuf *base, struct diff_options *opt) { unsigned mode1, mode2; const char *path1, *path2; const unsigned char *sha1, *sha2; int cmp, pathlen1, pathlen2; int old_baselen = base->len; sha1 = tree_entry_extract(t1, &path1, &mode1); sha2 = tree_entry_extract(t2, &path2, &mode2); pathlen1 = tree_entry_len(path1, sha1); pathlen2 = tree_entry_len(path2, sha2); cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2); if (cmp < 0) { show_entry(opt, "-", t1, base); return -1; } if (cmp > 0) { show_entry(opt, "+", t2, base); return 1; } if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER) && !hashcmp(sha1, sha2) && mode1 == mode2) return 0; /* * If the filemode has changed to/from a directory from/to a regular * file, we need to consider it a remove and an add. */ if (S_ISDIR(mode1) != S_ISDIR(mode2)) { show_entry(opt, "-", t1, base); show_entry(opt, "+", t2, base); return 0; } strbuf_add(base, path1, pathlen1); if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode1)) { if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) { opt->change(opt, mode1, mode2, sha1, sha2, base->buf, 0, 0); } strbuf_addch(base, '/'); diff_tree_sha1(sha1, sha2, base->buf, opt); } else { opt->change(opt, mode1, mode2, sha1, sha2, base->buf, 0, 0); } strbuf_setlen(base, old_baselen); return 0; }
/* A file entry went away or appeared */ static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base, int baselen) { unsigned mode; const char *path; const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode); int pathlen = tree_entry_len(path, sha1); if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode)) { enum object_type type; char *newbase = malloc_base(base, baselen, path, pathlen); struct tree_desc inner; void *tree; unsigned long size; tree = read_sha1_file(sha1, &type, &size); if (!tree || type != OBJ_TREE) die("corrupt tree sha %s", sha1_to_hex(sha1)); init_tree_desc(&inner, tree, size); show_tree(opt, prefix, &inner, newbase, baselen + 1 + pathlen); free(tree); free(newbase); } else { char *fullname = malloc_fullname(base, baselen, path, pathlen); opt->add_remove(opt, prefix[0], mode, sha1, fullname); free(fullname); } }
static int find_tree_entry(struct tree_desc *t, const char *name, unsigned char *result, unsigned *mode) { int namelen = strlen(name); while (t->size) { const char *entry; const unsigned char *sha1; int entrylen, cmp; sha1 = tree_entry_extract(t, &entry, mode); update_tree_entry(t); entrylen = tree_entry_len(entry, sha1); if (entrylen > namelen) continue; cmp = memcmp(name, entry, entrylen); if (cmp > 0) continue; if (cmp < 0) break; if (entrylen == namelen) { hashcpy(result, sha1); return 0; } if (name[entrylen] != '/') continue; if (!S_ISDIR(*mode)) break; if (++entrylen == namelen) { hashcpy(result, sha1); return 0; } return get_tree_entry(sha1, name + entrylen, result, mode); } return -1; }
/* A file entry went away or appeared */ static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc, struct strbuf *base) { unsigned mode; const char *path; const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode); int pathlen = tree_entry_len(path, sha1); int old_baselen = base->len; strbuf_add(base, path, pathlen); if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode)) { enum object_type type; struct tree_desc inner; void *tree; unsigned long size; tree = read_sha1_file(sha1, &type, &size); if (!tree || type != OBJ_TREE) die("corrupt tree sha %s", sha1_to_hex(sha1)); if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) opt->add_remove(opt, *prefix, mode, sha1, base->buf, 0); strbuf_addch(base, '/'); init_tree_desc(&inner, tree, size); show_tree(opt, prefix, &inner, base); free(tree); } else opt->add_remove(opt, prefix[0], mode, sha1, base->buf, 0); strbuf_setlen(base, old_baselen); }
/* * A tree "hash1" has a subdirectory at "prefix". Come up with a * tree object by replacing it with another tree "hash2". */ static int splice_tree(const unsigned char *hash1, const char *prefix, const unsigned char *hash2, unsigned char *result) { char *subpath; int toplen; char *buf; unsigned long sz; struct tree_desc desc; unsigned char *rewrite_here; const unsigned char *rewrite_with; unsigned char subtree[20]; enum object_type type; int status; subpath = strchrnul(prefix, '/'); toplen = subpath - prefix; if (*subpath) subpath++; buf = read_sha1_file(hash1, &type, &sz); if (!buf) die("cannot read tree %s", sha1_to_hex(hash1)); init_tree_desc(&desc, buf, sz); rewrite_here = NULL; while (desc.size) { const char *name; unsigned mode; const unsigned char *sha1; sha1 = tree_entry_extract(&desc, &name, &mode); if (strlen(name) == toplen && !memcmp(name, prefix, toplen)) { if (!S_ISDIR(mode)) die("entry %s in tree %s is not a tree", name, sha1_to_hex(hash1)); rewrite_here = (unsigned char *) sha1; break; } update_tree_entry(&desc); } if (!rewrite_here) die("entry %.*s not found in tree %s", toplen, prefix, sha1_to_hex(hash1)); if (*subpath) { status = splice_tree(rewrite_here, subpath, hash2, subtree); if (status) return status; rewrite_with = subtree; } else rewrite_with = hash2; hashcpy(rewrite_here, rewrite_with); status = write_sha1_file(buf, sz, tree_type, result); free(buf); return status; }
/* * Match one itself and its subtrees with two and pick the best match. */ static void match_trees(const unsigned char *hash1, const unsigned char *hash2, int *best_score, char **best_match, const char *base, int recurse_limit) { struct tree_desc one; void *one_buf; enum object_type type; unsigned long size; one_buf = read_sha1_file(hash1, &type, &size); if (!one_buf) die("unable to read tree (%s)", sha1_to_hex(hash1)); if (type != OBJ_TREE) die("%s is not a tree", sha1_to_hex(hash1)); init_tree_desc(&one, one_buf, size); while (one.size) { const char *path; const unsigned char *elem; unsigned mode; int score; elem = tree_entry_extract(&one, &path, &mode); if (!S_ISDIR(mode)) goto next; score = score_trees(elem, hash2); if (*best_score < score) { char *newpath; newpath = xmalloc(strlen(base) + strlen(path) + 1); sprintf(newpath, "%s%s", base, path); free(*best_match); *best_match = newpath; *best_score = score; } if (recurse_limit) { char *newbase; newbase = xmalloc(strlen(base) + strlen(path) + 2); sprintf(newbase, "%s%s/", base, path); match_trees(elem, hash2, best_score, best_match, newbase, recurse_limit - 1); free(newbase); } next: update_tree_entry(&one); } free(one_buf); }
/* * Match one itself and its subtrees with two and pick the best match. */ static void match_trees(const unsigned char *hash1, const unsigned char *hash2, int *best_score, char **best_match, const char *base, int recurse_limit) { struct tree_desc one; void *one_buf = fill_tree_desc_strict(&one, hash1); while (one.size) { const char *path; const unsigned char *elem; unsigned mode; int score; elem = tree_entry_extract(&one, &path, &mode); if (!S_ISDIR(mode)) goto next; score = score_trees(elem, hash2); if (*best_score < score) { char *newpath; newpath = xmalloc(strlen(base) + strlen(path) + 1); sprintf(newpath, "%s%s", base, path); free(*best_match); *best_match = newpath; *best_score = score; } if (recurse_limit) { char *newbase; newbase = xmalloc(strlen(base) + strlen(path) + 2); sprintf(newbase, "%s%s/", base, path); match_trees(elem, hash2, best_score, best_match, newbase, recurse_limit - 1); free(newbase); } next: update_tree_entry(&one); } free(one_buf); }
static int fsck_tree(struct tree *item, int strict, fsck_error error_func) { int retval; int has_full_path = 0; int has_empty_name = 0; int has_zero_pad = 0; int has_bad_modes = 0; int has_dup_entries = 0; int not_properly_sorted = 0; struct tree_desc desc; unsigned o_mode; const char *o_name; init_tree_desc(&desc, item->buffer, item->size); o_mode = 0; o_name = NULL; while (desc.size) { unsigned mode; const char *name; tree_entry_extract(&desc, &name, &mode); if (strchr(name, '/')) has_full_path = 1; if (!*name) has_empty_name = 1; has_zero_pad |= *(char *)desc.buffer == '0'; update_tree_entry(&desc); switch (mode) { /* * Standard modes.. */ case S_IFREG | 0755: case S_IFREG | 0644: case S_IFLNK: case S_IFDIR: case S_IFGITLINK: break; /* * This is nonstandard, but we had a few of these * early on when we honored the full set of mode * bits.. */ case S_IFREG | 0664: if (!strict) break; default: has_bad_modes = 1; } if (o_name) { switch (verify_ordered(o_mode, o_name, mode, name)) { case TREE_UNORDERED: not_properly_sorted = 1; break; case TREE_HAS_DUPS: has_dup_entries = 1; break; default: break; } } o_mode = mode; o_name = name; } retval = 0; if (has_full_path) retval += error_func(&item->object, FSCK_WARN, "contains full pathnames"); if (has_empty_name) retval += error_func(&item->object, FSCK_WARN, "contains empty pathname"); if (has_zero_pad) retval += error_func(&item->object, FSCK_WARN, "contains zero-padded file modes"); if (has_bad_modes) retval += error_func(&item->object, FSCK_WARN, "contains bad file modes"); if (has_dup_entries) retval += error_func(&item->object, FSCK_ERROR, "contains duplicate file entries"); if (not_properly_sorted) retval += error_func(&item->object, FSCK_ERROR, "not properly sorted"); return retval; }
/* * Is a tree entry interesting given the pathspec we have? * * Return: * - 2 for "yes, and all subsequent entries will be" * - 1 for yes * - zero for no * - negative for "no, and no subsequent entries will be either" */ static int tree_entry_interesting(struct tree_desc *desc, const char *base, int baselen, struct diff_options *opt) { const char *path; const unsigned char *sha1; unsigned mode; int i; int pathlen; int never_interesting = -1; if (!opt->nr_paths) return 1; sha1 = tree_entry_extract(desc, &path, &mode); pathlen = tree_entry_len(path, sha1); for (i = 0; i < opt->nr_paths; i++) { const char *match = opt->paths[i]; int matchlen = opt->pathlens[i]; int m = -1; /* signals that we haven't called strncmp() */ if (baselen >= matchlen) { /* If it doesn't match, move along... */ if (strncmp(base, match, matchlen)) continue; /* * The base is a subdirectory of a path which * was specified, so all of them are interesting. */ return 2; } /* Does the base match? */ if (strncmp(base, match, baselen)) continue; match += baselen; matchlen -= baselen; if (never_interesting) { /* * We have not seen any match that sorts later * than the current path. */ /* * Does match sort strictly earlier than path * with their common parts? */ m = strncmp(match, path, (matchlen < pathlen) ? matchlen : pathlen); if (m < 0) continue; /* * If we come here even once, that means there is at * least one pathspec that would sort equal to or * later than the path we are currently looking at. * In other words, if we have never reached this point * after iterating all pathspecs, it means all * pathspecs are either outside of base, or inside the * base but sorts strictly earlier than the current * one. In either case, they will never match the * subsequent entries. In such a case, we initialized * the variable to -1 and that is what will be * returned, allowing the caller to terminate early. */ never_interesting = 0; } if (pathlen > matchlen) continue; if (matchlen > pathlen) { if (match[pathlen] != '/') continue; if (!S_ISDIR(mode)) continue; } if (m == -1) /* * we cheated and did not do strncmp(), so we do * that here. */ m = strncmp(match, path, pathlen); /* * If common part matched earlier then it is a hit, * because we rejected the case where path is not a * leading directory and is shorter than match. */ if (!m) return 1; } return never_interesting; /* No matches */ }
static int fsck_tree(struct tree *item, struct fsck_options *options) { int retval; int has_null_sha1 = 0; int has_full_path = 0; int has_empty_name = 0; int has_dot = 0; int has_dotdot = 0; int has_dotgit = 0; int has_zero_pad = 0; int has_bad_modes = 0; int has_dup_entries = 0; int not_properly_sorted = 0; struct tree_desc desc; unsigned o_mode; const char *o_name; init_tree_desc(&desc, item->buffer, item->size); o_mode = 0; o_name = NULL; while (desc.size) { unsigned mode; const char *name; const unsigned char *sha1; sha1 = tree_entry_extract(&desc, &name, &mode); has_null_sha1 |= is_null_sha1(sha1); has_full_path |= !!strchr(name, '/'); has_empty_name |= !*name; has_dot |= !strcmp(name, "."); has_dotdot |= !strcmp(name, ".."); has_dotgit |= (!strcmp(name, ".git") || is_hfs_dotgit(name) || is_ntfs_dotgit(name)); has_zero_pad |= *(char *)desc.buffer == '0'; update_tree_entry(&desc); switch (mode) { /* * Standard modes.. */ case S_IFREG | 0755: case S_IFREG | 0644: case S_IFLNK: case S_IFDIR: case S_IFGITLINK: break; /* * This is nonstandard, but we had a few of these * early on when we honored the full set of mode * bits.. */ case S_IFREG | 0664: if (!options->strict) break; default: has_bad_modes = 1; } if (o_name) { switch (verify_ordered(o_mode, o_name, mode, name)) { case TREE_UNORDERED: not_properly_sorted = 1; break; case TREE_HAS_DUPS: has_dup_entries = 1; break; default: break; } } o_mode = mode; o_name = name; } retval = 0; if (has_null_sha1) retval += report(options, &item->object, FSCK_MSG_NULL_SHA1, "contains entries pointing to null sha1"); if (has_full_path) retval += report(options, &item->object, FSCK_MSG_FULL_PATHNAME, "contains full pathnames"); if (has_empty_name) retval += report(options, &item->object, FSCK_MSG_EMPTY_NAME, "contains empty pathname"); if (has_dot) retval += report(options, &item->object, FSCK_MSG_HAS_DOT, "contains '.'"); if (has_dotdot) retval += report(options, &item->object, FSCK_MSG_HAS_DOTDOT, "contains '..'"); if (has_dotgit) retval += report(options, &item->object, FSCK_MSG_HAS_DOTGIT, "contains '.git'"); if (has_zero_pad) retval += report(options, &item->object, FSCK_MSG_ZERO_PADDED_FILEMODE, "contains zero-padded file modes"); if (has_bad_modes) retval += report(options, &item->object, FSCK_MSG_BAD_FILEMODE, "contains bad file modes"); if (has_dup_entries) retval += report(options, &item->object, FSCK_MSG_DUPLICATE_ENTRIES, "contains duplicate file entries"); if (not_properly_sorted) retval += report(options, &item->object, FSCK_MSG_TREE_NOT_SORTED, "not properly sorted"); return retval; }
/* * Inspect two trees, and give a score that tells how similar they are. */ static int score_trees(const unsigned char *hash1, const unsigned char *hash2) { struct tree_desc one; struct tree_desc two; void *one_buf, *two_buf; int score = 0; enum object_type type; unsigned long size; one_buf = read_sha1_file(hash1, &type, &size); if (!one_buf) die("unable to read tree (%s)", sha1_to_hex(hash1)); if (type != OBJ_TREE) die("%s is not a tree", sha1_to_hex(hash1)); init_tree_desc(&one, one_buf, size); two_buf = read_sha1_file(hash2, &type, &size); if (!two_buf) die("unable to read tree (%s)", sha1_to_hex(hash2)); if (type != OBJ_TREE) die("%s is not a tree", sha1_to_hex(hash2)); init_tree_desc(&two, two_buf, size); while (one.size | two.size) { const unsigned char *elem1 = elem1; const unsigned char *elem2 = elem2; const char *path1 = path1; const char *path2 = path2; unsigned mode1 = mode1; unsigned mode2 = mode2; int cmp; if (one.size) elem1 = tree_entry_extract(&one, &path1, &mode1); if (two.size) elem2 = tree_entry_extract(&two, &path2, &mode2); if (!one.size) { /* two has more entries */ score += score_missing(mode2, path2); update_tree_entry(&two); continue; } if (!two.size) { /* two lacks this entry */ score += score_missing(mode1, path1); update_tree_entry(&one); continue; } cmp = base_name_compare(path1, strlen(path1), mode1, path2, strlen(path2), mode2); if (cmp < 0) { /* path1 does not appear in two */ score += score_missing(mode1, path1); update_tree_entry(&one); continue; } else if (cmp > 0) { /* path2 does not appear in one */ score += score_missing(mode2, path2); update_tree_entry(&two); continue; } else if (hashcmp(elem1, elem2)) /* they are different */ score += score_differs(mode1, mode2, path1); else /* same subtree or blob */ score += score_matches(mode1, mode2, path1); update_tree_entry(&one); update_tree_entry(&two); } free(one_buf); free(two_buf); return score; }
/* * new path should be added to combine diff * * 3 cases on how/when it should be called and behaves: * * t, !tp -> path added, all parents lack it * !t, tp -> path removed from all parents * t, tp -> path modified/added * (M for tp[i]=tp[imin], A otherwise) */ static struct combine_diff_path *emit_path(struct combine_diff_path *p, struct strbuf *base, struct diff_options *opt, int nparent, struct tree_desc *t, struct tree_desc *tp, int imin) { unsigned mode; const char *path; const unsigned char *sha1; int pathlen; int old_baselen = base->len; int i, isdir, recurse = 0, emitthis = 1; /* at least something has to be valid */ assert(t || tp); if (t) { /* path present in resulting tree */ sha1 = tree_entry_extract(t, &path, &mode)->hash; pathlen = tree_entry_len(&t->entry); isdir = S_ISDIR(mode); } else { /* * a path was removed - take path from imin parent. Also take * mode from that parent, to decide on recursion(1). * * 1) all modes for tp[i]=tp[imin] should be the same wrt * S_ISDIR, thanks to base_name_compare(). */ tree_entry_extract(&tp[imin], &path, &mode); pathlen = tree_entry_len(&tp[imin].entry); isdir = S_ISDIR(mode); sha1 = NULL; mode = 0; } if (DIFF_OPT_TST(opt, RECURSIVE) && isdir) { recurse = 1; emitthis = DIFF_OPT_TST(opt, TREE_IN_RECURSIVE); } if (emitthis) { int keep; struct combine_diff_path *pprev = p; p = path_appendnew(p, nparent, base, path, pathlen, mode, sha1); for (i = 0; i < nparent; ++i) { /* * tp[i] is valid, if present and if tp[i]==tp[imin] - * otherwise, we should ignore it. */ int tpi_valid = tp && !(tp[i].entry.mode & S_IFXMIN_NEQ); const unsigned char *sha1_i; unsigned mode_i; p->parent[i].status = !t ? DIFF_STATUS_DELETED : tpi_valid ? DIFF_STATUS_MODIFIED : DIFF_STATUS_ADDED; if (tpi_valid) { sha1_i = tp[i].entry.oid->hash; mode_i = tp[i].entry.mode; } else { sha1_i = NULL; mode_i = 0; } p->parent[i].mode = mode_i; hashcpy(p->parent[i].oid.hash, sha1_i ? sha1_i : null_sha1); } keep = 1; if (opt->pathchange) keep = opt->pathchange(opt, p); /* * If a path was filtered or consumed - we don't need to add it * to the list and can reuse its memory, leaving it as * pre-allocated element on the tail. * * On the other hand, if path needs to be kept, we need to * correct its .next to NULL, as it was pre-initialized to how * much memory was allocated. * * see path_appendnew() for details. */ if (!keep) p = pprev; else p->next = NULL; } if (recurse) { const unsigned char **parents_sha1; FAST_ARRAY_ALLOC(parents_sha1, nparent); for (i = 0; i < nparent; ++i) { /* same rule as in emitthis */ int tpi_valid = tp && !(tp[i].entry.mode & S_IFXMIN_NEQ); parents_sha1[i] = tpi_valid ? tp[i].entry.oid->hash : NULL; } strbuf_add(base, path, pathlen); strbuf_addch(base, '/'); p = ll_diff_tree_paths(p, sha1, parents_sha1, nparent, base, opt); FAST_ARRAY_FREE(parents_sha1, nparent); } strbuf_setlen(base, old_baselen); return p; }