6 const char *tree_type
= "tree";
8 static int read_one_entry(unsigned char *sha1
, const char *base
, int baselen
, const char *pathname
, unsigned mode
, int stage
)
10 int len
= strlen(pathname
);
11 unsigned int size
= cache_entry_size(baselen
+ len
);
12 struct cache_entry
*ce
= malloc(size
);
16 ce
->ce_mode
= create_ce_mode(mode
);
17 ce
->ce_flags
= create_ce_flags(baselen
+ len
, stage
);
18 memcpy(ce
->name
, base
, baselen
);
19 memcpy(ce
->name
+ baselen
, pathname
, len
+1);
20 memcpy(ce
->sha1
, sha1
, 20);
21 return add_cache_entry(ce
, 1);
24 static int read_tree_recursive(void *buffer
, unsigned long size
,
25 const char *base
, int baselen
, int stage
)
28 int len
= strlen(buffer
)+1;
29 unsigned char *sha1
= buffer
+ len
;
30 char *path
= strchr(buffer
, ' ')+1;
33 if (size
< len
+ 20 || sscanf(buffer
, "%o", &mode
) != 1)
41 int pathlen
= strlen(path
);
42 char *newbase
= malloc(baselen
+ 1 + pathlen
);
45 unsigned long eltsize
;
47 eltbuf
= read_sha1_file(sha1
, elttype
, &eltsize
);
48 if (!eltbuf
|| strcmp(elttype
, "tree"))
50 memcpy(newbase
, base
, baselen
);
51 memcpy(newbase
+ baselen
, path
, pathlen
);
52 newbase
[baselen
+ pathlen
] = '/';
53 retval
= read_tree_recursive(eltbuf
, eltsize
,
55 baselen
+ pathlen
+ 1, stage
);
62 if (read_one_entry(sha1
, base
, baselen
, path
, mode
, stage
) < 0)
68 int read_tree(void *buffer
, unsigned long size
, int stage
)
70 return read_tree_recursive(buffer
, size
, "", 0, stage
);
73 struct tree
*lookup_tree(unsigned char *sha1
)
75 struct object
*obj
= lookup_object(sha1
);
77 struct tree
*ret
= malloc(sizeof(struct tree
));
78 memset(ret
, 0, sizeof(struct tree
));
79 created_object(sha1
, &ret
->object
);
82 if (obj
->parsed
&& obj
->type
!= tree_type
) {
83 error("Object %s is a %s, not a tree",
84 sha1_to_hex(sha1
), obj
->type
);
87 return (struct tree
*) obj
;
90 int parse_tree(struct tree
*item
)
93 void *buffer
, *bufptr
;
95 if (item
->object
.parsed
)
97 item
->object
.parsed
= 1;
98 item
->object
.type
= tree_type
;
99 buffer
= bufptr
= read_sha1_file(item
->object
.sha1
, type
, &size
);
101 return error("Could not read %s",
102 sha1_to_hex(item
->object
.sha1
));
103 if (strcmp(type
, tree_type
))
104 return error("Object %s not a tree",
105 sha1_to_hex(item
->object
.sha1
));
108 int len
= 1+strlen(bufptr
);
109 unsigned char *file_sha1
= bufptr
+ len
;
110 char *path
= strchr(bufptr
, ' ');
112 if (size
< len
+ 20 || !path
||
113 sscanf(bufptr
, "%o", &mode
) != 1)
116 /* Warn about trees that don't do the recursive thing.. */
117 if (strchr(path
, '/')) {
118 item
->has_full_path
= 1;
125 obj
= &lookup_tree(file_sha1
)->object
;
127 obj
= &lookup_blob(file_sha1
)->object
;
129 add_ref(&item
->object
, obj
);