Skip to content

Commit ff5ebe3

Browse files
iabervonLinus Torvalds
authored andcommitted
[PATCH] Port fsck-cache to use parsing functions
This ports fsck-cache to use parsing functions. Note that performance could be improved here by only reading each object once, but this requires somewhat more complicated flow control. Signed-Off-By: Daniel Barkalow <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 5873b67 commit ff5ebe3

File tree

2 files changed

+38
-72
lines changed

2 files changed

+38
-72
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ commit-tree: commit-tree.o read-cache.o
4343
cat-file: cat-file.o read-cache.o
4444
$(CC) $(CFLAGS) -o cat-file cat-file.o read-cache.o $(LIBS)
4545

46-
fsck-cache: fsck-cache.o read-cache.o
47-
$(CC) $(CFLAGS) -o fsck-cache fsck-cache.o read-cache.o $(LIBS)
46+
fsck-cache: fsck-cache.o read-cache.o object.o commit.o tree.o blob.o
47+
$(CC) $(CFLAGS) -o fsck-cache fsck-cache.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
4848

4949
checkout-cache: checkout-cache.o read-cache.o
5050
$(CC) $(CFLAGS) -o checkout-cache checkout-cache.o read-cache.o $(LIBS)

fsck-cache.c

Lines changed: 36 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
#include <sys/types.h>
44
#include <dirent.h>
55

6-
#include "revision.h"
6+
#include "commit.h"
7+
#include "tree.h"
8+
#include "blob.h"
9+
10+
#define REACHABLE 0x0001
711

812
static int show_unreachable = 0;
913
static unsigned char head_sha1[20];
@@ -13,96 +17,54 @@ static void check_connectivity(void)
1317
int i;
1418

1519
/* Look up all the requirements, warn about missing objects.. */
16-
for (i = 0; i < nr_revs; i++) {
17-
struct revision *rev = revs[i];
20+
for (i = 0; i < nr_objs; i++) {
21+
struct object *obj = objs[i];
1822

19-
if (show_unreachable && !(rev->flags & REACHABLE)) {
20-
printf("unreachable %s\n", sha1_to_hex(rev->sha1));
23+
if (show_unreachable && !(obj->flags & REACHABLE)) {
24+
printf("unreachable %s\n", sha1_to_hex(obj->sha1));
2125
continue;
2226
}
2327

24-
switch (rev->flags & (SEEN | USED)) {
25-
case 0:
26-
printf("bad %s\n", sha1_to_hex(rev->sha1));
27-
break;
28-
case USED:
29-
printf("missing %s\n", sha1_to_hex(rev->sha1));
30-
break;
31-
case SEEN:
32-
printf("dangling %s\n", sha1_to_hex(rev->sha1));
33-
break;
28+
if (!obj->parsed) {
29+
printf("missing %s %s\n", obj->type,
30+
sha1_to_hex(obj->sha1));
31+
}
32+
if (!obj->used) {
33+
printf("dangling %s %s\n", obj->type,
34+
sha1_to_hex(obj->sha1));
3435
}
3536
}
3637
}
3738

38-
static void mark_needs_sha1(unsigned char *parent, const char * tag, unsigned char *child)
39-
{
40-
struct revision * child_rev = add_relationship(lookup_rev(parent), child);
41-
child_rev->flags |= USED;
42-
}
43-
44-
static int mark_sha1_seen(unsigned char *sha1, char *tag)
45-
{
46-
struct revision *rev = lookup_rev(sha1);
47-
48-
rev->flags |= SEEN;
49-
return 0;
50-
}
51-
5239
static int fsck_tree(unsigned char *sha1, void *data, unsigned long size)
5340
{
54-
int warn_old_tree = 1;
55-
56-
while (size) {
57-
int len = 1+strlen(data);
58-
unsigned char *file_sha1 = data + len;
59-
char *path = strchr(data, ' ');
60-
unsigned int mode;
61-
if (size < len + 20 || !path || sscanf(data, "%o", &mode) != 1)
62-
return -1;
63-
64-
/* Warn about trees that don't do the recursive thing.. */
65-
if (warn_old_tree && strchr(path, '/')) {
66-
fprintf(stderr, "warning: fsck-cache: tree %s has full pathnames in it\n", sha1_to_hex(sha1));
67-
warn_old_tree = 0;
68-
}
69-
70-
data += len + 20;
71-
size -= len + 20;
72-
mark_needs_sha1(sha1, S_ISDIR(mode) ? "tree" : "blob", file_sha1);
41+
struct tree *item = lookup_tree(sha1);
42+
if (parse_tree(item))
43+
return -1;
44+
if (item->has_full_path) {
45+
fprintf(stderr, "warning: fsck-cache: tree %s "
46+
"has full pathnames in it\n", sha1_to_hex(sha1));
7347
}
7448
return 0;
7549
}
7650

7751
static int fsck_commit(unsigned char *sha1, void *data, unsigned long size)
7852
{
79-
int parents;
80-
unsigned char tree_sha1[20];
81-
unsigned char parent_sha1[20];
82-
83-
if (memcmp(data, "tree ", 5))
53+
struct commit *commit = lookup_commit(sha1);
54+
if (parse_commit(commit))
8455
return -1;
85-
if (get_sha1_hex(data + 5, tree_sha1) < 0)
56+
if (!commit->tree)
8657
return -1;
87-
mark_needs_sha1(sha1, "tree", tree_sha1);
88-
data += 5 + 40 + 1; /* "tree " + <hex sha1> + '\n' */
89-
parents = 0;
90-
while (!memcmp(data, "parent ", 7)) {
91-
if (get_sha1_hex(data + 7, parent_sha1) < 0)
92-
return -1;
93-
mark_needs_sha1(sha1, "commit", parent_sha1);
94-
data += 7 + 40 + 1; /* "parent " + <hex sha1> + '\n' */
95-
parents++;
96-
}
97-
if (!parents)
58+
if (!commit->parents)
9859
printf("root %s\n", sha1_to_hex(sha1));
9960
return 0;
10061
}
10162

102-
static int fsck_entry(unsigned char *sha1, char *tag, void *data, unsigned long size)
63+
static int fsck_entry(unsigned char *sha1, char *tag, void *data,
64+
unsigned long size)
10365
{
10466
if (!strcmp(tag, "blob")) {
105-
/* Nothing to check */;
67+
lookup_blob(sha1); /* Nothing to check; but notice it. */
10668
} else if (!strcmp(tag, "tree")) {
10769
if (fsck_tree(sha1, data, size) < 0)
10870
return -1;
@@ -111,7 +73,7 @@ static int fsck_entry(unsigned char *sha1, char *tag, void *data, unsigned long
11173
return -1;
11274
} else
11375
return -1;
114-
return mark_sha1_seen(sha1, tag);
76+
return 0;
11577
}
11678

11779
static int fsck_name(char *hex)
@@ -125,7 +87,8 @@ static int fsck_name(char *hex)
12587
unsigned long size;
12688
void *buffer = NULL;
12789
if (!check_sha1_signature(sha1, map, mapsize))
128-
buffer = unpack_sha1_file(map, mapsize, type, &size);
90+
buffer = unpack_sha1_file(map, mapsize, type,
91+
&size);
12992
munmap(map, mapsize);
13093
if (buffer && !fsck_entry(sha1, type, buffer, size))
13194
return 0;
@@ -186,7 +149,10 @@ int main(int argc, char **argv)
186149
continue;
187150
}
188151
if (!get_sha1_hex(argv[i], head_sha1)) {
189-
mark_reachable(lookup_rev(head_sha1), REACHABLE);
152+
struct object *obj =
153+
&lookup_commit(head_sha1)->object;
154+
obj->used = 1;
155+
mark_reachable(obj, REACHABLE);
190156
heads++;
191157
continue;
192158
}

0 commit comments

Comments
 (0)