Skip to content

Commit b51ad43

Browse files
author
Linus Torvalds
committed
Merge the new object model thing from Daniel Barkalow
This was a real git merge with conflicts. I'll commit the scripts I used to do the merge next. Not pretty, but it's half-way functional.
2 parents a4b7dbe + b5039db commit b51ad43

File tree

12 files changed

+512
-160
lines changed

12 files changed

+512
-160
lines changed

Makefile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ 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)
5151

5252
diff-tree: diff-tree.o read-cache.o
5353
$(CC) $(CFLAGS) -o diff-tree diff-tree.o read-cache.o $(LIBS)
5454

55-
rev-tree: rev-tree.o read-cache.o
56-
$(CC) $(CFLAGS) -o rev-tree rev-tree.o read-cache.o $(LIBS)
55+
rev-tree: rev-tree.o read-cache.o object.o commit.o tree.o blob.o
56+
$(CC) $(CFLAGS) -o rev-tree rev-tree.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
5757

5858
show-files: show-files.o read-cache.o
5959
$(CC) $(CFLAGS) -o show-files show-files.o read-cache.o $(LIBS)
@@ -64,8 +64,8 @@ check-files: check-files.o read-cache.o
6464
ls-tree: ls-tree.o read-cache.o
6565
$(CC) $(CFLAGS) -o ls-tree ls-tree.o read-cache.o $(LIBS)
6666

67-
merge-base: merge-base.o read-cache.o
68-
$(CC) $(CFLAGS) -o merge-base merge-base.o read-cache.o $(LIBS)
67+
merge-base: merge-base.o read-cache.o object.o commit.o tree.o blob.o
68+
$(CC) $(CFLAGS) -o merge-base merge-base.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
6969

7070
merge-cache: merge-cache.o read-cache.o
7171
$(CC) $(CFLAGS) -o merge-cache merge-cache.o read-cache.o $(LIBS)

blob.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "blob.h"
2+
#include "cache.h"
3+
#include <stdlib.h>
4+
5+
const char *blob_type = "blob";
6+
7+
struct blob *lookup_blob(unsigned char *sha1)
8+
{
9+
struct object *obj = lookup_object(sha1);
10+
if (!obj) {
11+
struct blob *ret = malloc(sizeof(struct blob));
12+
memset(ret, 0, sizeof(struct blob));
13+
created_object(sha1, &ret->object);
14+
ret->object.type = blob_type;
15+
ret->object.parsed = 1;
16+
return ret;
17+
}
18+
if (obj->parsed && obj->type != blob_type) {
19+
error("Object %s is a %s, not a blob",
20+
sha1_to_hex(sha1), obj->type);
21+
return NULL;
22+
}
23+
return (struct blob *) obj;
24+
}

blob.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef BLOB_H
2+
#define BLOB_H
3+
4+
#include "object.h"
5+
6+
extern const char *blob_type;
7+
8+
struct blob {
9+
struct object object;
10+
};
11+
12+
struct blob *lookup_blob(unsigned char *sha1);
13+
14+
#endif /* BLOB_H */

commit.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include "commit.h"
2+
#include "cache.h"
3+
#include <string.h>
4+
5+
const char *commit_type = "commit";
6+
7+
struct commit *lookup_commit(unsigned char *sha1)
8+
{
9+
struct object *obj = lookup_object(sha1);
10+
if (!obj) {
11+
struct commit *ret = malloc(sizeof(struct commit));
12+
memset(ret, 0, sizeof(struct commit));
13+
created_object(sha1, &ret->object);
14+
return ret;
15+
}
16+
if (obj->parsed && obj->type != commit_type) {
17+
error("Object %s is a %s, not a commit",
18+
sha1_to_hex(sha1), obj->type);
19+
return NULL;
20+
}
21+
return (struct commit *) obj;
22+
}
23+
24+
static unsigned long parse_commit_date(const char *buf)
25+
{
26+
unsigned long date;
27+
28+
if (memcmp(buf, "author", 6))
29+
return 0;
30+
while (*buf++ != '\n')
31+
/* nada */;
32+
if (memcmp(buf, "committer", 9))
33+
return 0;
34+
while (*buf++ != '>')
35+
/* nada */;
36+
date = strtoul(buf, NULL, 10);
37+
if (date == ULONG_MAX)
38+
date = 0;
39+
return date;
40+
}
41+
42+
int parse_commit(struct commit *item)
43+
{
44+
char type[20];
45+
void * buffer, *bufptr;
46+
unsigned long size;
47+
unsigned char parent[20];
48+
if (item->object.parsed)
49+
return 0;
50+
item->object.parsed = 1;
51+
buffer = bufptr = read_sha1_file(item->object.sha1, type, &size);
52+
if (!buffer)
53+
return error("Could not read %s",
54+
sha1_to_hex(item->object.sha1));
55+
if (strcmp(type, commit_type))
56+
return error("Object %s not a commit",
57+
sha1_to_hex(item->object.sha1));
58+
item->object.type = commit_type;
59+
get_sha1_hex(bufptr + 5, parent);
60+
item->tree = lookup_tree(parent);
61+
add_ref(&item->object, &item->tree->object);
62+
bufptr += 46; /* "tree " + "hex sha1" + "\n" */
63+
while (!memcmp(bufptr, "parent ", 7) &&
64+
!get_sha1_hex(bufptr + 7, parent)) {
65+
struct commit_list *new_parent =
66+
malloc(sizeof(struct commit_list));
67+
new_parent->next = item->parents;
68+
new_parent->item = lookup_commit(parent);
69+
add_ref(&item->object, &new_parent->item->object);
70+
item->parents = new_parent;
71+
bufptr += 48;
72+
}
73+
item->date = parse_commit_date(bufptr);
74+
free(buffer);
75+
return 0;
76+
}
77+
78+
void free_commit_list(struct commit_list *list)
79+
{
80+
while (list) {
81+
struct commit_list *temp = list;
82+
list = temp->next;
83+
free(temp);
84+
}
85+
}

commit.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef COMMIT_H
2+
#define COMMIT_H
3+
4+
#include "object.h"
5+
#include "tree.h"
6+
7+
struct commit_list {
8+
struct commit *item;
9+
struct commit_list *next;
10+
};
11+
12+
struct commit {
13+
struct object object;
14+
unsigned long date;
15+
struct commit_list *parents;
16+
struct tree *tree;
17+
};
18+
19+
extern const char *commit_type;
20+
21+
struct commit *lookup_commit(unsigned char *sha1);
22+
23+
int parse_commit(struct commit *item);
24+
25+
void free_commit_list(struct commit_list *list);
26+
27+
#endif /* COMMIT_H */

fsck-cache.c

Lines changed: 35 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 %s\n", rev->tag, 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 %s\n", rev->tag, sha1_to_hex(rev->sha1));
27-
break;
28-
case USED:
29-
printf("missing %s, %s\n", rev->tag, sha1_to_hex(rev->sha1));
30-
break;
31-
case SEEN:
32-
printf("dangling %s %s\n", rev->tag, 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 *ptag, unsigned char *child, const char *ctag)
39-
{
40-
struct revision * child_rev = add_relationship(lookup_rev(parent, ptag), child, ctag);
41-
child_rev->flags |= USED;
42-
}
43-
44-
static int mark_sha1_seen(unsigned char *sha1, const char *tag)
45-
{
46-
struct revision *rev = lookup_rev(sha1, tag);
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, "tree", file_sha1, S_ISDIR(mode) ? "tree" : "blob");
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, "commit", tree_sha1, "tree");
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, "commit");
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,9 @@ 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, "commit"), REACHABLE);
152+
struct object *obj = &lookup_commit(head_sha1)->object;
153+
obj->used = 1;
154+
mark_reachable(obj, REACHABLE);
190155
heads++;
191156
continue;
192157
}

0 commit comments

Comments
 (0)