Hash :
e52ed7a5
Author :
Date :
2011-01-03T22:34:27
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 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
/*
* 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 <stdarg.h>
#include "git2/object.h"
#include "common.h"
#include "repository.h"
#include "commit.h"
#include "tag.h"
#include "blob.h"
#include "fileops.h"
#define GIT_DIR "/.git/"
#define GIT_OBJECTS_DIR "objects/"
#define GIT_OBJECTS_INFO_DIR GIT_OBJECTS_DIR "info/"
#define GIT_OBJECTS_PACK_DIR GIT_OBJECTS_DIR "pack/"
#define GIT_REFS_DIR "refs/"
#define GIT_REFS_HEADS_DIR GIT_REFS_DIR "heads/"
#define GIT_REFS_TAGS_DIR GIT_REFS_DIR "tags/"
#define GIT_INDEX_FILE "index"
#define GIT_HEAD_FILE "HEAD"
#define GIT_SYMREF "ref: "
#define GIT_BRANCH_MASTER "master"
static const int OBJECT_TABLE_SIZE = 32;
typedef struct {
char *path_repository;
unsigned is_bare:1, has_been_reinit:1;
} repo_init;
/*
* Hash table methods
*
* Callbacks for the ODB cache, implemented
* as a hash table
*/
uint32_t object_table_hash(const void *key)
{
uint32_t r;
git_oid *id;
id = (git_oid *)key;
memcpy(&r, id->id, sizeof(r));
return r;
}
int object_table_hashkey(void *object, const void *key)
{
git_object *obj;
git_oid *oid;
obj = (git_object *)object;
oid = (git_oid *)key;
return (git_oid_cmp(oid, &obj->id) == 0);
}
/*
* Git repository open methods
*
* Open a repository object from its path
*/
static int assign_repository_DIRs(git_repository *repo,
const char *git_dir,
const char *git_object_directory,
const char *git_index_file,
const char *git_work_tree)
{
char path_aux[GIT_PATH_MAX];
size_t path_len;
assert(repo);
if (git_dir == NULL || gitfo_isdir(git_dir) < GIT_SUCCESS)
return GIT_ENOTFOUND;
/* store GIT_DIR */
path_len = strlen(git_dir);
strcpy(path_aux, git_dir);
if (path_aux[path_len - 1] != '/') {
path_aux[path_len] = '/';
path_aux[path_len + 1] = 0;
path_len = path_len + 1;
}
repo->path_repository = git__strdup(path_aux);
/* store GIT_OBJECT_DIRECTORY */
if (git_object_directory == NULL)
strcpy(path_aux + path_len, GIT_OBJECTS_DIR);
else
strcpy(path_aux, git_object_directory);
if (gitfo_isdir(path_aux) < GIT_SUCCESS)
return GIT_ENOTFOUND;
repo->path_odb = git__strdup(path_aux);
/* store GIT_INDEX_FILE */
if (git_index_file == NULL)
strcpy(path_aux + path_len, GIT_INDEX_FILE);
else
strcpy(path_aux, git_index_file);
if (gitfo_exists(path_aux) < 0)
return GIT_ENOTFOUND;
repo->path_index = git__strdup(path_aux);
/* store GIT_WORK_TREE */
if (git_work_tree == NULL)
repo->is_bare = 1;
else
repo->path_workdir = git__strdup(git_work_tree);
return GIT_SUCCESS;
}
static int guess_repository_DIRs(git_repository *repo, const char *repository_path)
{
char path_aux[GIT_PATH_MAX], *last_DIR;
int path_len;
if (gitfo_isdir(repository_path) < GIT_SUCCESS)
return GIT_ENOTAREPO;
path_len = strlen(repository_path);
strcpy(path_aux, repository_path);
if (path_aux[path_len - 1] != '/') {
path_aux[path_len] = '/';
path_aux[path_len + 1] = 0;
path_len = path_len + 1;
}
repo->path_repository = git__strdup(path_aux);
/* objects database */
strcpy(path_aux + path_len, GIT_OBJECTS_DIR);
if (gitfo_isdir(path_aux) < GIT_SUCCESS)
return GIT_ENOTAREPO;
repo->path_odb = git__strdup(path_aux);
/* HEAD file */
strcpy(path_aux + path_len, GIT_HEAD_FILE);
if (gitfo_exists(path_aux) < 0)
return GIT_ENOTAREPO;
path_aux[path_len] = 0;
last_DIR = (path_aux + path_len - 2);
while (*last_DIR != '/')
last_DIR--;
if (strcmp(last_DIR, GIT_DIR) == 0) {
repo->is_bare = 0;
/* index file */
strcpy(path_aux + path_len, GIT_INDEX_FILE);
repo->path_index = git__strdup(path_aux);
/* working dir */
*(last_DIR + 1) = 0;
repo->path_workdir = git__strdup(path_aux);
} else {
repo->is_bare = 1;
repo->path_workdir = NULL;
}
return GIT_SUCCESS;
}
static git_repository *repository_alloc()
{
git_repository *repo = git__malloc(sizeof(git_repository));
if (!repo)
return NULL;
memset(repo, 0x0, sizeof(git_repository));
repo->objects = git_hashtable_alloc(
OBJECT_TABLE_SIZE,
object_table_hash,
object_table_hashkey);
if (repo->objects == NULL) {
free(repo);
return NULL;
}
return repo;
}
int git_repository_open2(git_repository **repo_out,
const char *git_dir,
const char *git_object_directory,
const char *git_index_file,
const char *git_work_tree)
{
git_repository *repo;
int error = GIT_SUCCESS;
assert(repo_out);
repo = repository_alloc();
if (repo == NULL)
return GIT_ENOMEM;
error = assign_repository_DIRs(repo,
git_dir,
git_object_directory,
git_index_file,
git_work_tree);
if (error < GIT_SUCCESS)
goto cleanup;
error = git_odb_open(&repo->db, repo->path_odb);
if (error < GIT_SUCCESS)
goto cleanup;
*repo_out = repo;
return GIT_SUCCESS;
cleanup:
git_repository_free(repo);
return error;
}
int git_repository_open(git_repository **repo_out, const char *path)
{
git_repository *repo;
int error = GIT_SUCCESS;
assert(repo_out && path);
repo = repository_alloc();
if (repo == NULL)
return GIT_ENOMEM;
error = guess_repository_DIRs(repo, path);
if (error < GIT_SUCCESS)
goto cleanup;
error = git_odb_open(&repo->db, repo->path_odb);
if (error < GIT_SUCCESS)
goto cleanup;
*repo_out = repo;
return GIT_SUCCESS;
cleanup:
git_repository_free(repo);
return error;
}
void git_repository_free(git_repository *repo)
{
git_hashtable_iterator it;
git_object *object;
if (repo == NULL)
return;
free(repo->path_workdir);
free(repo->path_index);
free(repo->path_repository);
free(repo->path_odb);
git_hashtable_iterator_init(repo->objects, &it);
while ((object = (git_object *)
git_hashtable_iterator_next(&it)) != NULL)
git_object_free(object);
git_hashtable_free(repo->objects);
if (repo->db != NULL)
git_odb_close(repo->db);
if (repo->index != NULL)
git_index_free(repo->index);
free(repo);
}
git_index *git_repository_index(git_repository *repo)
{
if (repo->index == NULL) {
if (git_index_open_inrepo(&repo->index, repo) < GIT_SUCCESS)
return NULL;
assert(repo->index);
}
return repo->index;
}
git_odb *git_repository_database(git_repository *repo)
{
assert(repo);
return repo->db;
}
int git_repository_newobject(git_object **object_out, git_repository *repo, git_otype type)
{
git_object *object = NULL;
assert(object_out && repo);
*object_out = NULL;
switch (type) {
case GIT_OBJ_COMMIT:
case GIT_OBJ_TAG:
case GIT_OBJ_TREE:
case GIT_OBJ_BLOB:
break;
default:
return GIT_EINVALIDTYPE;
}
object = git__malloc(git_object__size(type));
if (object == NULL)
return GIT_ENOMEM;
memset(object, 0x0, git_object__size(type));
object->repo = repo;
object->in_memory = 1;
object->modified = 1;
object->source.raw.type = type;
*object_out = object;
return GIT_SUCCESS;
}
int git_repository_lookup(git_object **object_out, git_repository *repo, const git_oid *id, git_otype type)
{
git_object *object = NULL;
git_rawobj obj_file;
int error = GIT_SUCCESS;
assert(repo && object_out && id);
object = git_hashtable_lookup(repo->objects, id);
if (object != NULL) {
*object_out = object;
return GIT_SUCCESS;
}
error = git_odb_read(&obj_file, repo->db, id);
if (error < GIT_SUCCESS)
return error;
if (type != GIT_OBJ_ANY && type != obj_file.type) {
git_rawobj_close(&obj_file);
return GIT_EINVALIDTYPE;
}
type = obj_file.type;
object = git__malloc(git_object__size(type));
if (object == NULL)
return GIT_ENOMEM;
memset(object, 0x0, git_object__size(type));
/* Initialize parent object */
git_oid_cpy(&object->id, id);
object->repo = repo;
memcpy(&object->source.raw, &obj_file, sizeof(git_rawobj));
object->source.open = 1;
switch (type) {
case GIT_OBJ_COMMIT:
error = git_commit__parse((git_commit *)object);
break;
case GIT_OBJ_TREE:
error = git_tree__parse((git_tree *)object);
break;
case GIT_OBJ_TAG:
error = git_tag__parse((git_tag *)object);
break;
case GIT_OBJ_BLOB:
error = git_blob__parse((git_blob *)object);
break;
default:
break;
}
if (error < GIT_SUCCESS) {
git_object_free(object);
return error;
}
git_object__source_close(object);
git_hashtable_insert(repo->objects, &object->id, object);
*object_out = object;
return GIT_SUCCESS;
}
static int repo_init_reinit(repo_init *results)
{
/* TODO: reinit the repository */
results->has_been_reinit = 1;
return GIT_SUCCESS;
}
static int repo_init_createhead(const char *head_path)
{
git_file fd;
int error = GIT_SUCCESS;
char head_symlink[50];
int len;
len = sprintf(head_symlink, "%s %s%s\n", GIT_SYMREF, GIT_REFS_HEADS_DIR, GIT_BRANCH_MASTER);
if ((fd = gitfo_creat(head_path, S_IREAD | S_IWRITE)) < GIT_SUCCESS)
return GIT_ERROR;
error = gitfo_write(fd, (void*)head_symlink, strlen(head_symlink));
gitfo_close(fd);
return error;
}
static int repo_init_structure(repo_init *results)
{
const int mode = 0755; /* or 0777 ? */
char temp_path[GIT_PATH_MAX];
int path_len;
char *git_dir = results->path_repository;
if (gitfo_mkdir_recurs(git_dir, mode))
return GIT_ERROR;
path_len = strlen(git_dir);
strcpy(temp_path, git_dir);
/* Does HEAD file already exist ? */
strcpy(temp_path + path_len, GIT_HEAD_FILE);
if (gitfo_exists(temp_path) == GIT_SUCCESS)
return repo_init_reinit(results);
if (repo_init_createhead(temp_path) < GIT_SUCCESS)
return GIT_ERROR;
/* Creates the '/objects/info/' directory */
strcpy(temp_path + path_len, GIT_OBJECTS_INFO_DIR);
if (gitfo_mkdir_recurs(temp_path, mode))
return GIT_ERROR;
/* Creates the '/objects/pack/' directory */
strcpy(temp_path + path_len, GIT_OBJECTS_PACK_DIR);
if (gitfo_mkdir(temp_path, mode))
return GIT_ERROR;
/* Creates the '/refs/heads/' directory */
strcpy(temp_path + path_len, GIT_REFS_HEADS_DIR);
if (gitfo_mkdir_recurs(temp_path, mode))
return GIT_ERROR;
/* Creates the '/refs/tags/' directory */
strcpy(temp_path + path_len, GIT_REFS_TAGS_DIR);
if (gitfo_mkdir(temp_path, mode))
return GIT_ERROR;
/* TODO: what's left? templates? */
return GIT_SUCCESS;
}
static int repo_init_find_dir(repo_init *results, const char* path)
{
const int MAX_GITDIR_TREE_STRUCTURE_PATH_LENGTH = 66;
char temp_path[GIT_PATH_MAX];
int path_len;
path_len = strlen(path);
strcpy(temp_path, path);
/* Ensure path has a trailing slash */
if (temp_path[path_len - 1] != '/') {
temp_path[path_len] = '/';
temp_path[path_len + 1] = 0;
path_len = path_len + 1;
}
if (!results->is_bare) {
strcpy(temp_path + path_len - 1, GIT_DIR);
path_len = path_len + strlen(GIT_DIR) - 1; /* Skip the leading slash from the constant */
}
if (path_len >= GIT_PATH_MAX - MAX_GITDIR_TREE_STRUCTURE_PATH_LENGTH)
return GIT_ENOTAREPO;
results->path_repository = git__strdup(temp_path);
return GIT_SUCCESS;
}
int git_repository_init(git_repository **repo_out, const char *path, unsigned is_bare)
{
int error = GIT_SUCCESS;
repo_init results;
assert(repo_out && path);
results.path_repository = NULL;
results.is_bare = is_bare;
error = repo_init_find_dir(&results, path);
if (error < GIT_SUCCESS)
goto cleanup;
error = repo_init_structure(&results);
if (error < GIT_SUCCESS)
goto cleanup;
error = git_repository_open(repo_out, results.path_repository);
cleanup:
free(results.path_repository);
return error;
}