Hash :
58519018
Author :
Date :
2010-10-28T02:07:18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
/*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2,
* as published by the Free Software Foundation.
*
* In addition to the permissions in the GNU General Public License,
* the authors give you unlimited permission to link the compiled
* version of this file into combinations with other programs,
* and to distribute those combinations without any restriction
* coming from the use of this file. (The General Public License
* restrictions do apply in other respects; for example, they cover
* modification of the file, and distribution when not linked into
* a combined executable.)
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "common.h"
#include "commit.h"
#include "revwalk.h"
#include "tree.h"
#include "git/repository.h"
static int resize_tree_array(git_tree *tree)
{
git_tree_entry **new_entries;
tree->array_size *= 2;
if (tree->array_size == 0)
tree->array_size = 8;
new_entries = git__malloc(tree->array_size * sizeof(git_tree_entry *));
if (new_entries == NULL)
return GIT_ENOMEM;
memcpy(new_entries, tree->entries, tree->entry_count * sizeof(git_tree_entry *));
free(tree->entries);
tree->entries = new_entries;
return GIT_SUCCESS;
}
int entry_cmp(const void *key, const void *array_member)
{
const char *filename = (const char *)key;
const git_tree_entry *entry = *(const git_tree_entry **)(array_member);
return strcmp(filename, entry->filename);
}
int entry_sort_cmp(const void *a, const void *b)
{
const git_tree_entry *entry_a = *(const git_tree_entry **)(a);
const git_tree_entry *entry_b = *(const git_tree_entry **)(b);
return strcmp(entry_a->filename, entry_b->filename);
}
static void entry_resort(git_tree *tree)
{
qsort(tree->entries, tree->entry_count, sizeof(git_tree_entry *), entry_sort_cmp);
}
static void free_tree_entries(git_tree *tree)
{
size_t i;
if (tree == NULL)
return;
for (i = 0; i < tree->entry_count; ++i) {
free(tree->entries[i]->filename);
free(tree->entries[i]);
}
free(tree->entries);
}
void git_tree__free(git_tree *tree)
{
free_tree_entries(tree);
free(tree);
}
git_tree *git_tree_new(git_repository *repo)
{
return (git_tree *)git_object_new(repo, GIT_OBJ_TREE);
}
const git_oid *git_tree_id(git_tree *c)
{
return git_object_id((git_object *)c);
}
git_tree *git_tree_lookup(git_repository *repo, const git_oid *id)
{
return (git_tree *)git_repository_lookup(repo, id, GIT_OBJ_TREE);
}
void git_tree_entry_set_attributes(git_tree_entry *entry, int attr)
{
assert(entry && entry->owner);
entry->attr = attr;
entry->owner->object.modified = 1;
}
void git_tree_entry_set_name(git_tree_entry *entry, const char *name)
{
assert(entry && entry->owner);
free(entry->filename);
entry->filename = git__strdup(name);
entry_resort(entry->owner);
entry->owner->object.modified = 1;
}
void git_tree_entry_set_id(git_tree_entry *entry, const git_oid *oid)
{
assert(entry && entry->owner);
git_oid_cpy(&entry->oid, oid);
entry->owner->object.modified = 1;
}
unsigned int git_tree_entry_attributes(git_tree_entry *entry)
{
return entry->attr;
}
const char *git_tree_entry_name(git_tree_entry *entry)
{
assert(entry);
return entry->filename;
}
const git_oid *git_tree_entry_id(git_tree_entry *entry)
{
assert(entry);
return &entry->oid;
}
git_object *git_tree_entry_2object(git_tree_entry *entry)
{
assert(entry);
return git_repository_lookup(entry->owner->object.repo, &entry->oid, GIT_OBJ_ANY);
}
git_tree_entry *git_tree_entry_byname(git_tree *tree, const char *filename)
{
git_tree_entry **found;
assert(tree && filename);
found = bsearch(filename, tree->entries, tree->entry_count, sizeof(git_tree_entry *), entry_cmp);
return found ? *found : NULL;
}
git_tree_entry *git_tree_entry_byindex(git_tree *tree, int idx)
{
assert(tree);
if (tree->entries == NULL)
return NULL;
return (idx >= 0 && idx < (int)tree->entry_count) ? tree->entries[idx] : NULL;
}
size_t git_tree_entrycount(git_tree *tree)
{
assert(tree);
return tree->entry_count;
}
int git_tree_add_entry(git_tree *tree, const git_oid *id, const char *filename, int attributes)
{
git_tree_entry *entry;
assert(tree && id && filename);
if (tree->entry_count >= tree->array_size)
if (resize_tree_array(tree) < 0)
return GIT_ENOMEM;
if ((entry = git__malloc(sizeof(git_tree_entry))) == NULL)
return GIT_ENOMEM;
memset(entry, 0x0, sizeof(git_tree_entry));
entry->filename = git__strdup(filename);
git_oid_cpy(&entry->oid, id);
entry->attr = attributes;
entry->owner = tree;
tree->entries[tree->entry_count++] = entry;
entry_resort(tree);
tree->object.modified = 1;
return GIT_SUCCESS;
}
int git_tree_remove_entry_byindex(git_tree *tree, int idx)
{
git_tree_entry *remove_ptr;
assert(tree);
if (idx < 0 || idx >= (int)tree->entry_count)
return GIT_ENOTFOUND;
remove_ptr = tree->entries[idx];
tree->entries[idx] = tree->entries[--tree->entry_count];
free(remove_ptr->filename);
free(remove_ptr);
entry_resort(tree);
tree->object.modified = 1;
return GIT_SUCCESS;
}
int git_tree_remove_entry_byname(git_tree *tree, const char *filename)
{
git_tree_entry **entry_ptr;
int idx;
assert(tree && filename);
entry_ptr = bsearch(filename, tree->entries, tree->entry_count, sizeof(git_tree_entry *), entry_cmp);
if (entry_ptr == NULL)
return GIT_ENOTFOUND;
idx = (int)(entry_ptr - tree->entries);
return git_tree_remove_entry_byindex(tree, idx);
}
int git_tree__writeback(git_tree *tree, git_odb_source *src)
{
size_t i;
char filemode[8];
assert(tree && src);
if (tree->entries == NULL)
return GIT_ERROR;
entry_resort(tree);
for (i = 0; i < tree->entry_count; ++i) {
git_tree_entry *entry;
entry = tree->entries[i];
sprintf(filemode, "%06o ", entry->attr);
git__source_write(src, filemode, strlen(filemode));
git__source_write(src, entry->filename, strlen(entry->filename) + 1);
git__source_write(src, entry->oid.id, GIT_OID_RAWSZ);
}
return GIT_SUCCESS;
}
static int tree_parse_buffer(git_tree *tree, char *buffer, char *buffer_end)
{
static const size_t avg_entry_size = 40;
int error = 0;
free_tree_entries(tree);
tree->entry_count = 0;
tree->array_size = (tree->object.source.raw.len / avg_entry_size) + 1;
tree->entries = git__malloc(tree->array_size * sizeof(git_tree_entry *));
if (tree->entries == NULL)
return GIT_ENOMEM;
while (buffer < buffer_end) {
git_tree_entry *entry;
if (tree->entry_count >= tree->array_size)
if (resize_tree_array(tree) < 0)
return GIT_ENOMEM;
entry = git__malloc(sizeof(git_tree_entry));
if (entry == NULL) {
error = GIT_ENOMEM;
break;
}
tree->entries[tree->entry_count++] = entry;
entry->owner = tree;
entry->attr = strtol(buffer, &buffer, 8);
if (*buffer++ != ' ') {
error = GIT_EOBJCORRUPTED;
break;
}
if (memchr(buffer, 0, buffer_end - buffer) == NULL) {
error = GIT_EOBJCORRUPTED;
break;
}
entry->filename = git__strdup(buffer);
while (buffer < buffer_end && *buffer != 0)
buffer++;
buffer++;
git_oid_mkraw(&entry->oid, (const unsigned char *)buffer);
buffer += GIT_OID_RAWSZ;
}
return error;
}
int git_tree__parse(git_tree *tree)
{
char *buffer, *buffer_end;
assert(tree && tree->object.source.open);
assert(!tree->object.in_memory);
buffer = tree->object.source.raw.data;
buffer_end = buffer + tree->object.source.raw.len;
return tree_parse_buffer(tree, buffer, buffer_end);
}