send-pack: handle partial pushes correctly.
[git.git] / tree.c
1 #include "tree.h"
2 #include "blob.h"
3 #include "cache.h"
4 #include <stdlib.h>
5
6 const char *tree_type = "tree";
7
8 static 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);
12         struct cache_entry *ce = xmalloc(size);
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);
21         return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
22 }
23
24 static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, const char **paths)
25 {
26         const char *match;
27         int pathlen;
28
29         if (!paths)
30                 return 1;
31         pathlen = strlen(path);
32         while ((match = *paths++) != NULL) {
33                 int matchlen = strlen(match);
34
35                 if (baselen >= matchlen) {
36                         /* If it doesn't match, move along... */
37                         if (strncmp(base, match, matchlen))
38                                 continue;
39                         /* The base is a subdirectory of a path which was specified. */
40                         return 1;
41                 }
42
43                 /* Does the base match? */
44                 if (strncmp(base, match, baselen))
45                         continue;
46
47                 match += baselen;
48                 matchlen -= baselen;
49
50                 if (pathlen > matchlen)
51                         continue;
52
53                 if (matchlen > pathlen) {
54                         if (match[pathlen] != '/')
55                                 continue;
56                         if (!S_ISDIR(mode))
57                                 continue;
58                 }
59
60                 if (strncmp(path, match, pathlen))
61                         continue;
62
63                 return 1;
64         }
65         return 0;
66 }
67
68 static int read_tree_recursive(void *buffer, unsigned long size,
69                                const char *base, int baselen,
70                                int stage, const char **match)
71 {
72         while (size) {
73                 int len = strlen(buffer)+1;
74                 unsigned char *sha1 = buffer + len;
75                 char *path = strchr(buffer, ' ')+1;
76                 unsigned int mode;
77
78                 if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
79                         return -1;
80
81                 buffer = sha1 + 20;
82                 size -= len + 20;
83
84                 if (!match_tree_entry(base, baselen, path, mode, match))
85                         continue;
86
87                 if (S_ISDIR(mode)) {
88                         int retval;
89                         int pathlen = strlen(path);
90                         char *newbase;
91                         void *eltbuf;
92                         char elttype[20];
93                         unsigned long eltsize;
94
95                         eltbuf = read_sha1_file(sha1, elttype, &eltsize);
96                         if (!eltbuf || strcmp(elttype, "tree")) {
97                                 if (eltbuf) free(eltbuf);
98                                 return -1;
99                         }
100                         newbase = xmalloc(baselen + 1 + pathlen);
101                         memcpy(newbase, base, baselen);
102                         memcpy(newbase + baselen, path, pathlen);
103                         newbase[baselen + pathlen] = '/';
104                         retval = read_tree_recursive(eltbuf, eltsize,
105                                                      newbase,
106                                                      baselen + pathlen + 1,
107                                                      stage, match);
108                         free(eltbuf);
109                         free(newbase);
110                         if (retval)
111                                 return -1;
112                         continue;
113                 }
114                 if (read_one_entry(sha1, base, baselen, path, mode, stage) < 0)
115                         return -1;
116         }
117         return 0;
118 }
119
120 int read_tree(void *buffer, unsigned long size, int stage, const char **match)
121 {
122         return read_tree_recursive(buffer, size, "", 0, stage, match);
123 }
124
125 struct tree *lookup_tree(const unsigned char *sha1)
126 {
127         struct object *obj = lookup_object(sha1);
128         if (!obj) {
129                 struct tree *ret = xmalloc(sizeof(struct tree));
130                 memset(ret, 0, sizeof(struct tree));
131                 created_object(sha1, &ret->object);
132                 ret->object.type = tree_type;
133                 return ret;
134         }
135         if (!obj->type)
136                 obj->type = tree_type;
137         if (obj->type != tree_type) {
138                 error("Object %s is a %s, not a tree", 
139                       sha1_to_hex(sha1), obj->type);
140                 return NULL;
141         }
142         return (struct tree *) obj;
143 }
144
145 int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
146 {
147         void *bufptr = buffer;
148         struct tree_entry_list **list_p;
149
150         if (item->object.parsed)
151                 return 0;
152         item->object.parsed = 1;
153         list_p = &item->entries;
154         while (size) {
155                 struct object *obj;
156                 struct tree_entry_list *entry;
157                 int len = 1+strlen(bufptr);
158                 unsigned char *file_sha1 = bufptr + len;
159                 char *path = strchr(bufptr, ' ');
160                 unsigned int mode;
161                 if (size < len + 20 || !path || 
162                     sscanf(bufptr, "%o", &mode) != 1)
163                         return -1;
164
165                 entry = xmalloc(sizeof(struct tree_entry_list));
166                 entry->name = strdup(path + 1);
167                 entry->directory = S_ISDIR(mode) != 0;
168                 entry->executable = (mode & S_IXUSR) != 0;
169                 entry->symlink = S_ISLNK(mode) != 0;
170                 entry->zeropad = *(char *)bufptr == '0';
171                 entry->mode = mode;
172                 entry->next = NULL;
173
174                 bufptr += len + 20;
175                 size -= len + 20;
176
177                 if (entry->directory) {
178                         entry->item.tree = lookup_tree(file_sha1);
179                         obj = &entry->item.tree->object;
180                 } else {
181                         entry->item.blob = lookup_blob(file_sha1);
182                         obj = &entry->item.blob->object;
183                 }
184                 if (obj)
185                         add_ref(&item->object, obj);
186                 entry->parent = NULL; /* needs to be filled by the user */
187                 *list_p = entry;
188                 list_p = &entry->next;
189         }
190         return 0;
191 }
192
193 int parse_tree(struct tree *item)
194 {
195          char type[20];
196          void *buffer;
197          unsigned long size;
198          int ret;
199
200         if (item->object.parsed)
201                 return 0;
202         buffer = read_sha1_file(item->object.sha1, type, &size);
203         if (!buffer)
204                 return error("Could not read %s",
205                              sha1_to_hex(item->object.sha1));
206         if (strcmp(type, tree_type)) {
207                 free(buffer);
208                 return error("Object %s not a tree",
209                              sha1_to_hex(item->object.sha1));
210         }
211         ret = parse_tree_buffer(item, buffer, size);
212         free(buffer);
213         return ret;
214 }