[PATCH] Anal retentive 'const unsigned char *sha1'
[git/git.git] / tree.c
CommitLineData
175785e5
DB
1#include "tree.h"
2#include "blob.h"
3#include "cache.h"
4#include <stdlib.h>
5
6const char *tree_type = "tree";
7
94537c78
LT
8static int read_one_entry(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
9{
10 int len = strlen(pathname);
11 unsigned int size = cache_entry_size(baselen + len);
812666c8 12 struct cache_entry *ce = xmalloc(size);
94537c78
LT
13
14 memset(ce, 0, size);
15
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);
192268c1 21 return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
94537c78
LT
22}
23
24static int read_tree_recursive(void *buffer, unsigned long size,
25 const char *base, int baselen, int stage)
26{
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;
32
33 if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
34 return -1;
35
36 buffer = sha1 + 20;
37 size -= len + 20;
38
39 if (S_ISDIR(mode)) {
40 int retval;
41 int pathlen = strlen(path);
1c9da46d 42 char *newbase;
94537c78
LT
43 void *eltbuf;
44 char elttype[20];
45 unsigned long eltsize;
46
47 eltbuf = read_sha1_file(sha1, elttype, &eltsize);
1c9da46d
JF
48 if (!eltbuf || strcmp(elttype, "tree")) {
49 if (eltbuf) free(eltbuf);
94537c78 50 return -1;
1c9da46d
JF
51 }
52 newbase = xmalloc(baselen + 1 + pathlen);
94537c78
LT
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;
64 }
65 if (read_one_entry(sha1, base, baselen, path, mode, stage) < 0)
66 return -1;
67 }
68 return 0;
69}
70
71int read_tree(void *buffer, unsigned long size, int stage)
72{
73 return read_tree_recursive(buffer, size, "", 0, stage);
74}
75
5d6ccf5c 76struct tree *lookup_tree(const unsigned char *sha1)
175785e5
DB
77{
78 struct object *obj = lookup_object(sha1);
79 if (!obj) {
812666c8 80 struct tree *ret = xmalloc(sizeof(struct tree));
175785e5
DB
81 memset(ret, 0, sizeof(struct tree));
82 created_object(sha1, &ret->object);
d32987be 83 ret->object.type = tree_type;
175785e5
DB
84 return ret;
85 }
d1af002d
NP
86 if (!obj->type)
87 obj->type = tree_type;
c35dfe85 88 if (obj->type != tree_type) {
175785e5
DB
89 error("Object %s is a %s, not a tree",
90 sha1_to_hex(sha1), obj->type);
91 return NULL;
92 }
93 return (struct tree *) obj;
94}
95
bd2c39f5 96int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
175785e5 97{
bd2c39f5 98 void *bufptr = buffer;
08692164 99 struct tree_entry_list **list_p;
bd2c39f5 100
175785e5
DB
101 if (item->object.parsed)
102 return 0;
103 item->object.parsed = 1;
08692164 104 list_p = &item->entries;
175785e5
DB
105 while (size) {
106 struct object *obj;
08692164 107 struct tree_entry_list *entry;
175785e5
DB
108 int len = 1+strlen(bufptr);
109 unsigned char *file_sha1 = bufptr + len;
110 char *path = strchr(bufptr, ' ');
111 unsigned int mode;
112 if (size < len + 20 || !path ||
bd2c39f5 113 sscanf(bufptr, "%o", &mode) != 1)
175785e5
DB
114 return -1;
115
812666c8 116 entry = xmalloc(sizeof(struct tree_entry_list));
08692164 117 entry->name = strdup(path + 1);
42ea9cb2
LT
118 entry->directory = S_ISDIR(mode) != 0;
119 entry->executable = (mode & S_IXUSR) != 0;
120 entry->symlink = S_ISLNK(mode) != 0;
121 entry->mode = mode;
08692164
DB
122 entry->next = NULL;
123
175785e5
DB
124 bufptr += len + 20;
125 size -= len + 20;
126
08692164
DB
127 if (entry->directory) {
128 entry->item.tree = lookup_tree(file_sha1);
129 obj = &entry->item.tree->object;
175785e5 130 } else {
08692164
DB
131 entry->item.blob = lookup_blob(file_sha1);
132 obj = &entry->item.blob->object;
175785e5 133 }
235ac407
LT
134 if (obj)
135 add_ref(&item->object, obj);
6af1f019 136 entry->parent = NULL; /* needs to be filled by the user */
08692164
DB
137 *list_p = entry;
138 list_p = &entry->next;
175785e5
DB
139 }
140 return 0;
141}
bd2c39f5
NP
142
143int parse_tree(struct tree *item)
144{
145 char type[20];
146 void *buffer;
147 unsigned long size;
148 int ret;
149
150 if (item->object.parsed)
151 return 0;
152 buffer = read_sha1_file(item->object.sha1, type, &size);
153 if (!buffer)
154 return error("Could not read %s",
155 sha1_to_hex(item->object.sha1));
156 if (strcmp(type, tree_type)) {
157 free(buffer);
158 return error("Object %s not a tree",
159 sha1_to_hex(item->object.sha1));
160 }
161 ret = parse_tree_buffer(item, buffer, size);
162 free(buffer);
163 return ret;
164}