From 08ca0b04ba80b7127dd19b6d17c548eaca057ab3 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Sun, 17 Apr 2005 16:19:32 -0700 Subject: [PATCH] Make the revision tracking track the object types too. This allows fsck to verify not just that an object exists, but also that it has the type it was expected to have. --- fsck-cache.c | 24 ++++++++++++------------ rev-tree.c | 6 +++--- revision.h | 22 ++++++++++++++-------- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/fsck-cache.c b/fsck-cache.c index a8050f84a..b8b66d7d1 100644 --- a/fsck-cache.c +++ b/fsck-cache.c @@ -17,33 +17,33 @@ static void check_connectivity(void) struct revision *rev = revs[i]; if (show_unreachable && !(rev->flags & REACHABLE)) { - printf("unreachable %s\n", sha1_to_hex(rev->sha1)); + printf("unreachable %s %s\n", rev->tag, sha1_to_hex(rev->sha1)); continue; } switch (rev->flags & (SEEN | USED)) { case 0: - printf("bad %s\n", sha1_to_hex(rev->sha1)); + printf("bad %s %s\n", rev->tag, sha1_to_hex(rev->sha1)); break; case USED: - printf("missing %s\n", sha1_to_hex(rev->sha1)); + printf("missing %s, %s\n", rev->tag, sha1_to_hex(rev->sha1)); break; case SEEN: - printf("dangling %s\n", sha1_to_hex(rev->sha1)); + printf("dangling %s %s\n", rev->tag, sha1_to_hex(rev->sha1)); break; } } } -static void mark_needs_sha1(unsigned char *parent, const char * tag, unsigned char *child) +static void mark_needs_sha1(unsigned char *parent, const char *ptag, unsigned char *child, const char *ctag) { - struct revision * child_rev = add_relationship(lookup_rev(parent), child); + struct revision * child_rev = add_relationship(lookup_rev(parent, ptag), child, ctag); child_rev->flags |= USED; } -static int mark_sha1_seen(unsigned char *sha1, char *tag) +static int mark_sha1_seen(unsigned char *sha1, const char *tag) { - struct revision *rev = lookup_rev(sha1); + struct revision *rev = lookup_rev(sha1, tag); rev->flags |= SEEN; return 0; @@ -69,7 +69,7 @@ static int fsck_tree(unsigned char *sha1, void *data, unsigned long size) data += len + 20; size -= len + 20; - mark_needs_sha1(sha1, S_ISDIR(mode) ? "tree" : "blob", file_sha1); + mark_needs_sha1(sha1, "tree", file_sha1, S_ISDIR(mode) ? "tree" : "blob"); } return 0; } @@ -84,13 +84,13 @@ static int fsck_commit(unsigned char *sha1, void *data, unsigned long size) return -1; if (get_sha1_hex(data + 5, tree_sha1) < 0) return -1; - mark_needs_sha1(sha1, "tree", tree_sha1); + mark_needs_sha1(sha1, "commit", tree_sha1, "tree"); data += 5 + 40 + 1; /* "tree " + + '\n' */ parents = 0; while (!memcmp(data, "parent ", 7)) { if (get_sha1_hex(data + 7, parent_sha1) < 0) return -1; - mark_needs_sha1(sha1, "commit", parent_sha1); + mark_needs_sha1(sha1, "commit", parent_sha1, "commit"); data += 7 + 40 + 1; /* "parent " + + '\n' */ parents++; } @@ -186,7 +186,7 @@ int main(int argc, char **argv) continue; } if (!get_sha1_hex(argv[i], head_sha1)) { - mark_reachable(lookup_rev(head_sha1), REACHABLE); + mark_reachable(lookup_rev(head_sha1, "commit"), REACHABLE); heads++; continue; } diff --git a/rev-tree.c b/rev-tree.c index fe114ab36..3c5476925 100644 --- a/rev-tree.c +++ b/rev-tree.c @@ -37,7 +37,7 @@ static void read_cache_file(const char *path) break; if (get_sha1_hex(buf+1, sha1)) break; - rev = lookup_rev(sha1); + rev = lookup_rev(sha1, "commit"); rev->flags |= SEEN; rev->date = date; @@ -46,7 +46,7 @@ static void read_cache_file(const char *path) unsigned char parent[20]; if (get_sha1_hex(buf + 1, parent)) break; - add_relationship(rev, parent); + add_relationship(rev, parent, "commit"); } } fclose(file); @@ -127,7 +127,7 @@ int main(int argc, char **argv) * Now we have the maximal tree. Walk the different sha files back to the root. */ for (i = 0; i < nr; i++) - mark_reachable(lookup_rev(sha1[i]), 1 << i); + mark_reachable(lookup_rev(sha1[i], "commit"), 1 << i); /* * Now print out the results.. diff --git a/revision.h b/revision.h index 482729575..7791a72e0 100644 --- a/revision.h +++ b/revision.h @@ -25,6 +25,7 @@ struct revision { unsigned char sha1[20]; unsigned long date; struct parent *parent; + char tag[1]; }; static struct revision **revs; @@ -51,13 +52,17 @@ static int find_rev(unsigned char *sha1) return -first-1; } -static struct revision *lookup_rev(unsigned char *sha1) +static struct revision *lookup_rev(unsigned char *sha1, const char *tag) { int pos = find_rev(sha1); struct revision *n; - if (pos >= 0) - return revs[pos]; + if (pos >= 0) { + n = revs[pos]; + if (strcmp(n->tag, tag)) + error("expected tag %s on object %s: got %s", tag, sha1_to_hex(sha1), n->tag); + return n; + } pos = -pos-1; @@ -65,11 +70,12 @@ static struct revision *lookup_rev(unsigned char *sha1) rev_allocs = alloc_nr(rev_allocs); revs = realloc(revs, rev_allocs * sizeof(struct revision *)); } - n = malloc(sizeof(struct revision)); + n = malloc(sizeof(struct revision) + strlen(tag)); n->flags = 0; memcpy(n->sha1, sha1, 20); n->parent = NULL; + strcpy(n->tag, tag); /* Insert it into the right place */ memmove(revs + pos + 1, revs + pos, (nr_revs - pos) * sizeof(struct revision *)); @@ -79,9 +85,9 @@ static struct revision *lookup_rev(unsigned char *sha1) return n; } -static struct revision *add_relationship(struct revision *rev, unsigned char *needs) +static struct revision *add_relationship(struct revision *rev, unsigned char *needs, const char *tag) { - struct revision *parent_rev = lookup_rev(needs); + struct revision *parent_rev = lookup_rev(needs, tag); struct parent **pp = &rev->parent, *p; while ((p = *pp) != NULL) { @@ -131,7 +137,7 @@ static unsigned long parse_commit_date(const char *buf) static struct revision * parse_commit(unsigned char *sha1) { - struct revision *rev = lookup_rev(sha1); + struct revision *rev = lookup_rev(sha1, "commit"); if (!(rev->flags & SEEN)) { void *buffer, *bufptr; @@ -145,7 +151,7 @@ static struct revision * parse_commit(unsigned char *sha1) die("%s is not a commit object", sha1_to_hex(sha1)); bufptr += 46; /* "tree " + "hex sha1" + "\n" */ while (!memcmp(bufptr, "parent ", 7) && !get_sha1_hex(bufptr+7, parent)) { - add_relationship(rev, parent); + add_relationship(rev, parent, "commit"); parse_commit(parent); bufptr += 48; /* "parent " + "hex sha1" + "\n" */ } -- 2.30.2