Try to make test output look better
[git/jnareb-git.git] / tree.c
blobca800a85f771be1bd10d6575d93ca05bd3fc381c
1 #include "tree.h"
2 #include "blob.h"
3 #include "cache.h"
4 #include <stdlib.h>
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 = xmalloc(size);
14 memset(ce, 0, 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, ADD_CACHE_OK_TO_ADD);
24 static int read_tree_recursive(void *buffer, unsigned long size,
25 const char *base, int baselen, int stage)
27 while (size) {
28 int len = strlen(buffer)+1;
29 unsigned char *sha1 = buffer + len;
30 char *path = strchr(buffer, ' ')+1;
31 unsigned int mode;
33 if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
34 return -1;
36 buffer = sha1 + 20;
37 size -= len + 20;
39 if (S_ISDIR(mode)) {
40 int retval;
41 int pathlen = strlen(path);
42 char *newbase;
43 void *eltbuf;
44 char elttype[20];
45 unsigned long eltsize;
47 eltbuf = read_sha1_file(sha1, elttype, &eltsize);
48 if (!eltbuf || strcmp(elttype, "tree")) {
49 if (eltbuf) free(eltbuf);
50 return -1;
52 newbase = xmalloc(baselen + 1 + pathlen);
53 memcpy(newbase, base, baselen);
54 memcpy(newbase + baselen, path, pathlen);
55 newbase[baselen + pathlen] = '/';
56 retval = read_tree_recursive(eltbuf, eltsize,
57 newbase,
58 baselen + pathlen + 1, stage);
59 free(eltbuf);
60 free(newbase);
61 if (retval)
62 return -1;
63 continue;
65 if (read_one_entry(sha1, base, baselen, path, mode, stage) < 0)
66 return -1;
68 return 0;
71 int read_tree(void *buffer, unsigned long size, int stage)
73 return read_tree_recursive(buffer, size, "", 0, stage);
76 struct tree *lookup_tree(unsigned char *sha1)
78 struct object *obj = lookup_object(sha1);
79 if (!obj) {
80 struct tree *ret = xmalloc(sizeof(struct tree));
81 memset(ret, 0, sizeof(struct tree));
82 created_object(sha1, &ret->object);
83 ret->object.type = tree_type;
84 return ret;
86 if (obj->type != tree_type) {
87 error("Object %s is a %s, not a tree",
88 sha1_to_hex(sha1), obj->type);
89 return NULL;
91 return (struct tree *) obj;
94 int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
96 void *bufptr = buffer;
97 struct tree_entry_list **list_p;
99 if (item->object.parsed)
100 return 0;
101 item->object.parsed = 1;
102 list_p = &item->entries;
103 while (size) {
104 struct object *obj;
105 struct tree_entry_list *entry;
106 int len = 1+strlen(bufptr);
107 unsigned char *file_sha1 = bufptr + len;
108 char *path = strchr(bufptr, ' ');
109 unsigned int mode;
110 if (size < len + 20 || !path ||
111 sscanf(bufptr, "%o", &mode) != 1)
112 return -1;
114 entry = xmalloc(sizeof(struct tree_entry_list));
115 entry->name = strdup(path + 1);
116 entry->directory = S_ISDIR(mode) != 0;
117 entry->executable = (mode & S_IXUSR) != 0;
118 entry->symlink = S_ISLNK(mode) != 0;
119 entry->mode = mode;
120 entry->next = NULL;
122 bufptr += len + 20;
123 size -= len + 20;
125 if (entry->directory) {
126 entry->item.tree = lookup_tree(file_sha1);
127 obj = &entry->item.tree->object;
128 } else {
129 entry->item.blob = lookup_blob(file_sha1);
130 obj = &entry->item.blob->object;
132 if (obj)
133 add_ref(&item->object, obj);
135 *list_p = entry;
136 list_p = &entry->next;
138 return 0;
141 int parse_tree(struct tree *item)
143 char type[20];
144 void *buffer;
145 unsigned long size;
146 int ret;
148 if (item->object.parsed)
149 return 0;
150 buffer = read_sha1_file(item->object.sha1, type, &size);
151 if (!buffer)
152 return error("Could not read %s",
153 sha1_to_hex(item->object.sha1));
154 if (strcmp(type, tree_type)) {
155 free(buffer);
156 return error("Object %s not a tree",
157 sha1_to_hex(item->object.sha1));
159 ret = parse_tree_buffer(item, buffer, size);
160 free(buffer);
161 return ret;