Hash :
b6c93aef
Author :
Date :
2012-02-21T14:46:24
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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
/*
* Copyright (C) 2009-2012 the libgit2 contributors
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "iterator.h"
#include "tree.h"
#include "ignore.h"
#include "buffer.h"
#define IDX_AS_PTR(I) (void *)((uint64_t)(I))
#define PTR_AS_IDX(P) (unsigned int)((uint64_t)(P))
typedef struct {
git_iterator cb;
git_repository *repo;
git_vector tree_stack;
git_vector idx_stack;
git_index_entry entry;
git_buf path;
} git_iterator_tree;
static const git_tree_entry *git_iterator__tree_entry(git_iterator_tree *ti)
{
git_tree *tree;
unsigned int tree_idx;
if ((tree = git_vector_last(&ti->tree_stack)) == NULL)
return NULL;
tree_idx = PTR_AS_IDX(git_vector_last(&ti->idx_stack));
return git_tree_entry_byindex(tree, tree_idx);
}
static int git_iterator__tree_current(
git_iterator *self, const git_index_entry **entry)
{
int error;
git_iterator_tree *ti = (git_iterator_tree *)self;
const git_tree_entry *te = git_iterator__tree_entry(ti);
*entry = NULL;
if (te == NULL)
return GIT_SUCCESS;
ti->entry.mode = te->attr;
git_oid_cpy(&ti->entry.oid, &te->oid);
error = git_buf_joinpath(&ti->path, ti->path.ptr, te->filename);
if (error < GIT_SUCCESS)
return error;
ti->entry.path = ti->path.ptr;
*entry = &ti->entry;
return GIT_SUCCESS;
}
static int git_iterator__tree_at_end(git_iterator *self)
{
git_iterator_tree *ti = (git_iterator_tree *)self;
git_tree *tree;
return ((tree = git_vector_last(&ti->tree_stack)) == NULL ||
git_tree_entry_byindex(
tree, PTR_AS_IDX(git_vector_last(&ti->idx_stack))) == NULL);
}
static int expand_tree_if_needed(git_iterator_tree *ti)
{
int error;
git_tree *tree, *subtree;
unsigned int tree_idx;
const git_tree_entry *te;
while (1) {
tree = git_vector_last(&ti->tree_stack);
tree_idx = PTR_AS_IDX(git_vector_last(&ti->idx_stack));
te = git_tree_entry_byindex(tree, tree_idx);
if (!entry_is_tree(te))
return GIT_SUCCESS;
error = git_tree_lookup(&subtree, ti->repo, &te->oid);
if (error != GIT_SUCCESS)
return error;
if ((error = git_vector_insert(&ti->tree_stack, subtree)) < GIT_SUCCESS ||
(error = git_vector_insert(&ti->idx_stack, IDX_AS_PTR(0))) < GIT_SUCCESS ||
(error = git_buf_joinpath(&ti->path, ti->path.ptr, te->filename)) < GIT_SUCCESS)
{
git_tree_free(subtree);
return error;
}
}
return GIT_SUCCESS;
}
static int git_iterator__tree_advance(git_iterator *self)
{
git_iterator_tree *ti = (git_iterator_tree *)self;
git_tree *tree = git_vector_last(&ti->tree_stack);
unsigned int tree_idx = PTR_AS_IDX(git_vector_last(&ti->idx_stack));
const git_tree_entry *te = git_tree_entry_byindex(tree, tree_idx);
if (te == NULL)
return GIT_SUCCESS;
while (1) {
/* advance this tree */
tree_idx++;
ti->idx_stack.contents[ti->idx_stack.length - 1] = IDX_AS_PTR(tree_idx);
/* remove old entry filename */
git_buf_rtruncate_at_char(&ti->path, '/');
if ((te = git_tree_entry_byindex(tree, tree_idx)) != NULL)
break;
/* no entry - either we are done or we are done with this subtree */
if (ti->tree_stack.length == 1)
return GIT_SUCCESS;
git_tree_free(tree);
git_vector_remove(&ti->tree_stack, ti->tree_stack.length - 1);
git_vector_remove(&ti->idx_stack, ti->idx_stack.length - 1);
git_buf_rtruncate_at_char(&ti->path, '/');
tree = git_vector_last(&ti->tree_stack);
tree_idx = PTR_AS_IDX(git_vector_last(&ti->idx_stack));
}
if (te && entry_is_tree(te))
return expand_tree_if_needed(ti);
return GIT_SUCCESS;
}
static void git_iterator__tree_free(git_iterator *self)
{
git_iterator_tree *ti = (git_iterator_tree *)self;
while (ti->tree_stack.length > 1) {
git_tree *tree = git_vector_last(&ti->tree_stack);
git_tree_free(tree);
git_vector_remove(&ti->tree_stack, ti->tree_stack.length - 1);
}
git_vector_clear(&ti->tree_stack);
git_vector_clear(&ti->idx_stack);
git_buf_free(&ti->path);
}
int git_iterator_for_tree(git_repository *repo, git_tree *tree, git_iterator **iter)
{
int error;
git_iterator_tree *ti = git__calloc(1, sizeof(git_iterator_tree));
if (!ti)
return GIT_ENOMEM;
ti->cb.type = GIT_ITERATOR_TREE;
ti->cb.current = git_iterator__tree_current;
ti->cb.at_end = git_iterator__tree_at_end;
ti->cb.advance = git_iterator__tree_advance;
ti->cb.free = git_iterator__tree_free;
ti->repo = repo;
if (!(error = git_vector_init(&ti->tree_stack, 0, NULL)) &&
!(error = git_vector_insert(&ti->tree_stack, tree)) &&
!(error = git_vector_init(&ti->idx_stack, 0, NULL)))
error = git_vector_insert(&ti->idx_stack, IDX_AS_PTR(0));
if (error == GIT_SUCCESS)
error = expand_tree_if_needed(ti);
if (error != GIT_SUCCESS)
git_iterator_free((git_iterator *)ti);
else
*iter = (git_iterator *)ti;
return error;
}
typedef struct {
git_iterator cb;
git_index *index;
unsigned int current;
} git_iterator_index;
static int git_iterator__index_current(
git_iterator *self, const git_index_entry **entry)
{
git_iterator_index *ii = (git_iterator_index *)self;
*entry = git_index_get(ii->index, ii->current);
return GIT_SUCCESS;
}
static int git_iterator__index_at_end(git_iterator *self)
{
git_iterator_index *ii = (git_iterator_index *)self;
return (ii->current >= git_index_entrycount(ii->index));
}
static int git_iterator__index_advance(git_iterator *self)
{
git_iterator_index *ii = (git_iterator_index *)self;
if (ii->current < git_index_entrycount(ii->index))
ii->current++;
return GIT_SUCCESS;
}
static void git_iterator__index_free(git_iterator *self)
{
git_iterator_index *ii = (git_iterator_index *)self;
git_index_free(ii->index);
ii->index = NULL;
}
int git_iterator_for_index(git_repository *repo, git_iterator **iter)
{
int error;
git_iterator_index *ii = git__calloc(1, sizeof(git_iterator_index));
if (!ii)
return GIT_ENOMEM;
ii->cb.type = GIT_ITERATOR_INDEX;
ii->cb.current = git_iterator__index_current;
ii->cb.at_end = git_iterator__index_at_end;
ii->cb.advance = git_iterator__index_advance;
ii->cb.free = git_iterator__index_free;
ii->current = 0;
if ((error = git_repository_index(&ii->index, repo)) < GIT_SUCCESS)
git__free(ii);
else
*iter = (git_iterator *)ii;
return error;
}
typedef struct {
git_iterator cb;
git_repository *repo;
size_t root_len;
git_vector dir_stack; /* vector of vectors of paths */
git_vector idx_stack;
git_ignores ignores;
git_index_entry entry;
git_buf path;
int is_ignored;
} git_iterator_workdir;
static void free_directory(git_vector *dir)
{
unsigned int i;
char *path;
git_vector_foreach(dir, i, path)
git__free(path);
git_vector_free(dir);
git__free(dir);
}
static int load_workdir_entry(git_iterator_workdir *wi);
static int push_directory(git_iterator_workdir *wi)
{
int error;
git_vector *dir = NULL;
error = git_vector_alloc(&dir, 0, git__strcmp_cb);
if (error < GIT_SUCCESS)
return error;
/* allocate dir entries with extra byte (the "1" param) so later on we
* can suffix directories with a "/" as needed.
*/
error = git_path_dirload(wi->path.ptr, wi->root_len, 1, dir);
if (error < GIT_SUCCESS || dir->length == 0) {
free_directory(dir);
return GIT_ENOTFOUND;
}
if ((error = git_vector_insert(&wi->dir_stack, dir)) ||
(error = git_vector_insert(&wi->idx_stack, IDX_AS_PTR(0))))
{
free_directory(dir);
return error;
}
git_vector_sort(dir);
if (wi->dir_stack.length > 1) {
int slash_pos = git_buf_rfind_next(&wi->path, '/');
(void)git_ignore__push_dir(&wi->ignores, &wi->path.ptr[slash_pos + 1]);
}
return load_workdir_entry(wi);
}
static int git_iterator__workdir_current(
git_iterator *self, const git_index_entry **entry)
{
git_iterator_workdir *wi = (git_iterator_workdir *)self;
*entry = (wi->entry.path == NULL) ? NULL : &wi->entry;
return GIT_SUCCESS;
}
static int git_iterator__workdir_at_end(git_iterator *self)
{
git_iterator_workdir *wi = (git_iterator_workdir *)self;
return (wi->entry.path == NULL);
}
static int git_iterator__workdir_advance(git_iterator *self)
{
git_iterator_workdir *wi = (git_iterator_workdir *)self;
git_vector *dir;
unsigned int pos;
const char *next;
if (wi->entry.path == NULL)
return GIT_SUCCESS;
while (1) {
dir = git_vector_last(&wi->dir_stack);
pos = 1 + PTR_AS_IDX(git_vector_last(&wi->idx_stack));
wi->idx_stack.contents[wi->idx_stack.length - 1] = IDX_AS_PTR(pos);
next = git_vector_get(dir, pos);
if (next != NULL) {
if (strcmp(next, DOT_GIT) == 0)
continue;
/* else found a good entry */
break;
}
memset(&wi->entry, 0, sizeof(wi->entry));
if (wi->dir_stack.length == 1)
return GIT_SUCCESS;
free_directory(dir);
git_vector_remove(&wi->dir_stack, wi->dir_stack.length - 1);
git_vector_remove(&wi->idx_stack, wi->idx_stack.length - 1);
git_ignore__pop_dir(&wi->ignores);
}
return load_workdir_entry(wi);
}
int git_iterator_advance_into_ignored_directory(git_iterator *iter)
{
git_iterator_workdir *wi = (git_iterator_workdir *)iter;
if (iter->type != GIT_ITERATOR_WORKDIR)
return GIT_SUCCESS;
/* Loop because the first entry in the ignored directory could itself be
* an ignored directory, but we want to descend to find an actual entry.
*/
while (wi->entry.path && wi->is_ignored && S_ISDIR(wi->entry.mode)) {
int error = push_directory(wi);
if (error != GIT_SUCCESS)
return error;
}
return GIT_SUCCESS;
}
static void git_iterator__workdir_free(git_iterator *self)
{
git_iterator_workdir *wi = (git_iterator_workdir *)self;
while (wi->dir_stack.length) {
git_vector *dir = git_vector_last(&wi->dir_stack);
free_directory(dir);
git_vector_remove(&wi->dir_stack, wi->dir_stack.length - 1);
}
git_vector_clear(&wi->dir_stack);
git_vector_clear(&wi->idx_stack);
git_ignore__free(&wi->ignores);
git_buf_free(&wi->path);
}
static int load_workdir_entry(git_iterator_workdir *wi)
{
int error;
char *relpath;
git_vector *dir = git_vector_last(&wi->dir_stack);
unsigned int pos = PTR_AS_IDX(git_vector_last(&wi->idx_stack));
struct stat st;
relpath = git_vector_get(dir, pos);
error = git_buf_joinpath(
&wi->path, git_repository_workdir(wi->repo), relpath);
if (error < GIT_SUCCESS)
return error;
memset(&wi->entry, 0, sizeof(wi->entry));
wi->entry.path = relpath;
if (strcmp(relpath, DOT_GIT) == 0)
return git_iterator__workdir_advance((git_iterator *)wi);
/* if there is an error processing the entry, treat as ignored */
wi->is_ignored = 1;
error = git_ignore__lookup(&wi->ignores, wi->entry.path, &wi->is_ignored);
if (error != GIT_SUCCESS)
return GIT_SUCCESS;
if (p_lstat(wi->path.ptr, &st) < 0)
return GIT_SUCCESS;
/* TODO: remove shared code for struct stat conversion with index.c */
wi->entry.ctime.seconds = (git_time_t)st.st_ctime;
wi->entry.mtime.seconds = (git_time_t)st.st_mtime;
wi->entry.dev = st.st_rdev;
wi->entry.ino = st.st_ino;
wi->entry.mode = git_futils_canonical_mode(st.st_mode);
wi->entry.uid = st.st_uid;
wi->entry.gid = st.st_gid;
wi->entry.file_size = st.st_size;
/* if this is a file type we don't handle, treat as ignored */
if (st.st_mode == 0)
return GIT_SUCCESS;
if (S_ISDIR(st.st_mode)) {
if (git_path_contains(&wi->path, DOT_GIT) == GIT_SUCCESS) {
/* create submodule entry */
wi->entry.mode = S_IFGITLINK;
} else if (wi->is_ignored) {
/* create path entry (which is otherwise impossible), but is
* needed in case descent into ignore dir is required.
*/
size_t pathlen = strlen(wi->entry.path);
wi->entry.path[pathlen] = '/';
wi->entry.path[pathlen + 1] = '\0';
wi->entry.mode = S_IFDIR;
} else if ((error = push_directory(wi)) < GIT_SUCCESS) {
/* if there is an error loading the directory or if empty
* then skip over the directory completely.
*/
return git_iterator__workdir_advance((git_iterator *)wi);
}
}
return GIT_SUCCESS;
}
int git_iterator_for_workdir(git_repository *repo, git_iterator **iter)
{
int error;
git_iterator_workdir *wi = git__calloc(1, sizeof(git_iterator_workdir));
if (!wi)
return GIT_ENOMEM;
wi->cb.type = GIT_ITERATOR_WORKDIR;
wi->cb.current = git_iterator__workdir_current;
wi->cb.at_end = git_iterator__workdir_at_end;
wi->cb.advance = git_iterator__workdir_advance;
wi->cb.free = git_iterator__workdir_free;
wi->repo = repo;
if ((error = git_buf_sets(
&wi->path, git_repository_workdir(repo))) < GIT_SUCCESS ||
(error = git_vector_init(&wi->dir_stack, 0, NULL)) < GIT_SUCCESS ||
(error = git_vector_init(&wi->idx_stack, 0, NULL)) < GIT_SUCCESS ||
(error = git_ignore__for_path(repo, "", &wi->ignores)) < GIT_SUCCESS)
{
git__free(wi);
return error;
}
wi->root_len = wi->path.size;
if ((error = push_directory(wi)) < GIT_SUCCESS)
git_iterator_free((git_iterator *)wi);
else
*iter = (git_iterator *)wi;
return error;
}
int git_iterator_current_tree_entry(
git_iterator *iter, const git_tree_entry **tree_entry)
{
if (iter->type != GIT_ITERATOR_TREE)
*tree_entry = NULL;
else
*tree_entry = git_iterator__tree_entry((git_iterator_tree *)iter);
return GIT_SUCCESS;
}
int git_iterator_current_is_ignored(git_iterator *iter)
{
if (iter->type != GIT_ITERATOR_WORKDIR)
return 0;
else
return ((git_iterator_workdir *)iter)->is_ignored;
}