repository.c 12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * 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.
 */
25
#include <stdarg.h>
26

27
#include "git2/object.h"
28

29 30 31 32
#include "common.h"
#include "repository.h"
#include "commit.h"
#include "tag.h"
33
#include "blob.h"
34
#include "fileops.h"
35

36 37
#include "refs.h"

38 39
#define GIT_OBJECTS_INFO_DIR GIT_OBJECTS_DIR "info/"
#define GIT_OBJECTS_PACK_DIR GIT_OBJECTS_DIR "pack/"
40

41 42
#define GIT_BRANCH_MASTER "master"

43
static const int OBJECT_TABLE_SIZE = 32;
44

45
typedef struct {
46
	char *path_repository;
47
	unsigned is_bare:1, has_been_reinit:1;
48
} repo_init;
49

50 51
/*
 * Hash table methods
52
 *
53 54 55
 * Callbacks for the ODB cache, implemented
 * as a hash table
 */
56
uint32_t object_table_hash(const void *key, int hash_id)
57 58 59 60 61
{
	uint32_t r;
	git_oid *id;

	id = (git_oid *)key;
62
	memcpy(&r, id->id + (hash_id * sizeof(uint32_t)), sizeof(r));
63 64 65
	return r;
}

66 67 68 69 70
/*
 * Git repository open methods
 *
 * Open a repository object from its path
 */
71 72
static int assign_repository_dirs(
		git_repository *repo,
73 74 75
		const char *git_dir,
		const char *git_object_directory,
		const char *git_index_file,
76
		const char *git_work_tree)
77 78
{
	char path_aux[GIT_PATH_MAX];
79 80
	size_t git_dir_path_len;
	int error = GIT_SUCCESS;
81 82 83

	assert(repo);

84
	if (git_dir == NULL)
85 86
		return GIT_ENOTFOUND;

87
	error = gitfo_prettify_dir_path(path_aux, git_dir);
88 89
	if (error < GIT_SUCCESS)
		return error;
90

91
	git_dir_path_len = strlen(path_aux);
92

93
	/* store GIT_DIR */
94
	repo->path_repository = git__strdup(path_aux);
95 96
	if (repo->path_repository == NULL)
		return GIT_ENOMEM;
97

98
	/* path to GIT_OBJECT_DIRECTORY */
99
	if (git_object_directory == NULL)
100
		git__joinpath(path_aux, repo->path_repository, GIT_OBJECTS_DIR);
101
	else {
102
		error = gitfo_prettify_dir_path(path_aux, git_object_directory);
103 104 105
		if (error < GIT_SUCCESS)
			return error;
	}
106

107
	/* Store GIT_OBJECT_DIRECTORY */
108
	repo->path_odb = git__strdup(path_aux);
109 110
	if (repo->path_odb == NULL)
		return GIT_ENOMEM;
111

112
	/* path to GIT_WORK_TREE */
113 114
	if (git_work_tree == NULL)
		repo->is_bare = 1;
115
	else {
116
		error = gitfo_prettify_dir_path(path_aux, git_work_tree);
117 118
		if (error < GIT_SUCCESS)
			return error;
119 120

		/* Store GIT_WORK_TREE */
121
		repo->path_workdir = git__strdup(path_aux);
122 123
		if (repo->path_workdir == NULL)
			return GIT_ENOMEM;
124 125 126 127 128 129 130 131 132 133 134 135 136 137

		/* Path to GIT_INDEX_FILE */
		if (git_index_file == NULL)
			git__joinpath(path_aux, repo->path_repository, GIT_INDEX_FILE);
		else {
			error = gitfo_prettify_file_path(path_aux, git_index_file);
			if (error < GIT_SUCCESS)
				return error;
		}

		/* store GIT_INDEX_FILE */
		repo->path_index = git__strdup(path_aux);
		if (repo->path_index == NULL)
			return GIT_ENOMEM;
138 139
	}
	
140 141 142
	return GIT_SUCCESS;
}

143
static int check_repository_dirs(git_repository *repo)
144
{
145
	char path_aux[GIT_PATH_MAX];
146

147 148
	if (gitfo_isdir(repo->path_repository) < GIT_SUCCESS)
		return GIT_ENOTAREPO;
149

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
	/* Ensure GIT_OBJECT_DIRECTORY exists */
	if (gitfo_isdir(repo->path_odb) < GIT_SUCCESS)
		return GIT_ENOTAREPO;

	/* Ensure HEAD file exists */
	git__joinpath(path_aux, repo->path_repository, GIT_HEAD_FILE);
	if (gitfo_exists(path_aux) < 0)
		return GIT_ENOTAREPO;

	return GIT_SUCCESS;
}

static int guess_repository_dirs(git_repository *repo, const char *repository_path)
{
	char buffer[GIT_PATH_MAX];
	const char *path_work_tree = NULL;
166

167
	/* Git directory name */
168
	if (git__basename_r(buffer, sizeof(buffer), repository_path) < 0)
169
		return GIT_EINVALIDPATH;
170

171
	if (strcmp(buffer, DOT_GIT) == 0) {
172
		/* Path to working dir */
173
		if (git__dirname_r(buffer, sizeof(buffer), repository_path) < 0)
174
			return GIT_EINVALIDPATH;
175
		path_work_tree = buffer;
176 177
	}

178
	return assign_repository_dirs(repo, repository_path, NULL, NULL, path_work_tree);
179 180
}

181
static git_repository *repository_alloc()
182 183 184 185 186 187 188 189
{
	git_repository *repo = git__malloc(sizeof(git_repository));
	if (!repo)
		return NULL;

	memset(repo, 0x0, sizeof(git_repository));

	repo->objects = git_hashtable_alloc(
190 191
			OBJECT_TABLE_SIZE, 
			object_table_hash,
192
			(git_hash_keyeq_ptr)git_oid_cmp);
193

194
	if (repo->objects == NULL) { 
195 196 197 198
		free(repo);
		return NULL;
	}

199
	if (git_repository__refcache_init(&repo->references) < GIT_SUCCESS) {
200 201 202 203 204
		git_hashtable_free(repo->objects);
		free(repo);
		return NULL;
	}

205 206 207 208 209 210 211
	if (git_vector_init(&repo->memory_objects, 16, NULL) < GIT_SUCCESS) {
		git_hashtable_free(repo->objects);
		git_repository__refcache_free(&repo->references);
		free(repo);
		return NULL;
	}

212
	repo->gc_enabled = 1;
213 214 215
	return repo;
}

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
static int init_odb(git_repository *repo)
{
	return git_odb_open(&repo->db, repo->path_odb);
}

int git_repository_open3(git_repository **repo_out,
		const char *git_dir,
		git_odb *object_database,
		const char *git_index_file,
		const char *git_work_tree)
{
	git_repository *repo;
	int error = GIT_SUCCESS;

	assert(repo_out);

	if (object_database == NULL)
		return GIT_ERROR;

	repo = repository_alloc();
	if (repo == NULL)
		return GIT_ENOMEM;

239
	error = assign_repository_dirs(repo, 
240 241 242
			git_dir, 
			NULL,
			git_index_file,
243
			git_work_tree);
244 245 246 247

	if (error < GIT_SUCCESS)
		goto cleanup;

248 249 250 251
	error = check_repository_dirs(repo);
	if (error < GIT_SUCCESS)
		goto cleanup;

252 253 254 255 256 257 258 259 260 261 262
	repo->db = object_database;

	*repo_out = repo;
	return GIT_SUCCESS;

cleanup:
	git_repository_free(repo);
	return error;
}


263 264 265 266 267 268 269 270 271 272 273
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);

274
	repo = repository_alloc();
275 276 277
	if (repo == NULL)
		return GIT_ENOMEM;

278
	error = assign_repository_dirs(repo,
279 280 281
			git_dir, 
			git_object_directory,
			git_index_file,
282 283 284 285
			git_work_tree);

	if (error < GIT_SUCCESS)
		goto cleanup;
286

287
	error = check_repository_dirs(repo);
288
	if (error < GIT_SUCCESS)
289 290
		goto cleanup;

291
	error = init_odb(repo);
292
	if (error < GIT_SUCCESS)
293 294 295 296 297 298 299 300 301 302
		goto cleanup;

	*repo_out = repo;
	return GIT_SUCCESS;

cleanup:
	git_repository_free(repo);
	return error;
}

303
int git_repository_open(git_repository **repo_out, const char *path)
304 305
{
	git_repository *repo;
Vicent Marti committed
306 307 308
	int error = GIT_SUCCESS;

	assert(repo_out && path);
309

310
	repo = repository_alloc();
311
	if (repo == NULL)
Vicent Marti committed
312
		return GIT_ENOMEM;
313

314 315 316 317 318
	error = guess_repository_dirs(repo, path);
	if (error < GIT_SUCCESS)
		goto cleanup;

	error = check_repository_dirs(repo);
319
	if (error < GIT_SUCCESS)
Vicent Marti committed
320
		goto cleanup;
321

322
	error = init_odb(repo);
323
	if (error < GIT_SUCCESS)
Vicent Marti committed
324 325 326 327 328 329 330 331
		goto cleanup;

	*repo_out = repo;
	return GIT_SUCCESS;

cleanup:
	git_repository_free(repo);
	return error;
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
static void repository_free(git_repository *repo)
{
	assert(repo);

	free(repo->path_workdir);
	free(repo->path_index);
	free(repo->path_repository);
	free(repo->path_odb);

	git_hashtable_free(repo->objects);
	git_vector_free(&repo->memory_objects);

	git_repository__refcache_free(&repo->references);

	if (repo->db != NULL)
		git_odb_close(repo->db);

	if (repo->index != NULL)
		git_index_free(repo->index);

	free(repo);
}

void git_repository_free__no_gc(git_repository *repo)
358
{
359
	git_object *object;
360 361
	const void *_unused;
	unsigned int i;
362

363 364
	if (repo == NULL)
		return;
Vicent Marti committed
365

366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
	GIT_HASHTABLE_FOREACH(repo->objects, _unused, object,
		object->repo = NULL;
		object->refcount = 0;
	);

	for (i = 0; i < repo->memory_objects.length; ++i) {
		object = git_vector_get(&repo->memory_objects, i);
		object->repo = NULL;
		object->refcount = 0;
	}

	repository_free(repo);
}

void git_repository_free(git_repository *repo)
{
	git_object *object;
	const void *_unused;
	unsigned int i;

	if (repo == NULL)
		return;
388

389
	repo->gc_enabled = 0;
390 391 392 393 394 395 396 397 398 399

	/* force free all the objects */
	GIT_HASHTABLE_FOREACH(repo->objects, _unused, object,
		git_object__free(object);
	);

	for (i = 0; i < repo->memory_objects.length; ++i) {
		object = git_vector_get(&repo->memory_objects, i);
		git_object__free(object);
	}
400

401
	repository_free(repo);
402 403
}

404
int git_repository_index(git_index **index_out, git_repository *repo)
405
{
406 407 408 409
	int error;

	assert(index_out && repo);

410
	if (repo->index == NULL) {
411 412 413
		error = git_index_open_inrepo(&repo->index, repo);
		if (error < GIT_SUCCESS)
			return error;
Vicent Marti committed
414

415
		assert(repo->index != NULL);
416 417
	}

418 419
	*index_out = repo->index;
	return GIT_SUCCESS;
420 421
}

422 423 424 425 426 427
git_odb *git_repository_database(git_repository *repo)
{
	assert(repo);
	return repo->db;
}

428
static int repo_init_reinit(repo_init *results)
429 430
{
	/* TODO: reinit the repository */
431 432 433 434
	results->has_been_reinit = 1;
	return GIT_SUCCESS;
}

435
static int repo_init_createhead(git_repository *repo)
436
{
437 438 439
	git_reference *head_reference;
	return  git_reference_create_symbolic(&head_reference, repo, GIT_HEAD_FILE, GIT_REFS_HEADS_MASTER_FILE);
}
440

441 442 443
static int repo_init_check_head_existence(char * repository_path)
{
	char temp_path[GIT_PATH_MAX];
444

445 446
	git__joinpath(temp_path, repository_path, GIT_HEAD_FILE);
	return gitfo_exists(temp_path);
447 448
}

449
static int repo_init_structure(repo_init *results)
450
{
451
	const int mode = 0755; /* or 0777 ? */
452

453 454
	char temp_path[GIT_PATH_MAX];
	char *git_dir = results->path_repository;
455

456 457 458
	if (gitfo_mkdir_recurs(git_dir, mode))
		return GIT_ERROR;

459
	/* Creates the '/objects/info/' directory */
460 461
	git__joinpath(temp_path, git_dir, GIT_OBJECTS_INFO_DIR);
	if (gitfo_mkdir_recurs(temp_path, mode) < GIT_SUCCESS)
462 463
		return GIT_ERROR;

464
	/* Creates the '/objects/pack/' directory */
465
	git__joinpath(temp_path, git_dir, GIT_OBJECTS_PACK_DIR);
466 467 468
	if (gitfo_mkdir(temp_path, mode))
		return GIT_ERROR;

469
	/* Creates the '/refs/heads/' directory */
470
	git__joinpath(temp_path, git_dir, GIT_REFS_HEADS_DIR);
471
	if (gitfo_mkdir_recurs(temp_path, mode))
472 473 474
		return GIT_ERROR;

	/* Creates the '/refs/tags/' directory */
475
	git__joinpath(temp_path, git_dir, GIT_REFS_TAGS_DIR);
476 477 478
	if (gitfo_mkdir(temp_path, mode))
		return GIT_ERROR;

479
	/* TODO: what's left? templates? */
480 481 482 483

	return GIT_SUCCESS;
}

484
static int repo_init_find_dir(repo_init *results, const char* path)
485 486
{
	char temp_path[GIT_PATH_MAX];
487
	int error = GIT_SUCCESS;
488

489
	error = gitfo_prettify_dir_path(temp_path, path);
490 491
	if (error < GIT_SUCCESS)
		return error;
492

493
	if (!results->is_bare) {
494
		git__joinpath(temp_path, temp_path, GIT_DIR);
495 496 497
	}

	results->path_repository = git__strdup(temp_path);
498 499
	if (results->path_repository == NULL)
		return GIT_ENOMEM;
500 501 502 503

	return GIT_SUCCESS;
}

504
int git_repository_init(git_repository **repo_out, const char *path, unsigned is_bare)
505 506
{
	int error = GIT_SUCCESS;
507
	git_repository *repo = NULL;
508
	repo_init results;
509 510 511
	
	assert(repo_out && path);

512
	results.path_repository = NULL;
513
	results.is_bare = is_bare;
514

515
	error = repo_init_find_dir(&results, path);
516 517 518
	if (error < GIT_SUCCESS)
		goto cleanup;

519 520 521
	if (!repo_init_check_head_existence(results.path_repository))
		return repo_init_reinit(&results);

522
	error = repo_init_structure(&results);
523 524 525
	if (error < GIT_SUCCESS)
		goto cleanup;

526 527 528 529 530 531 532
	repo = repository_alloc();
	if (repo == NULL) {
		error = GIT_ENOMEM;
		goto cleanup;
	}

	error = guess_repository_dirs(repo, results.path_repository);
533 534 535
	if (error < GIT_SUCCESS)
		goto cleanup;

536
	assert(repo->is_bare == is_bare);
537

538 539 540 541 542 543 544 545 546 547 548 549 550 551
	error = init_odb(repo);
	if (error < GIT_SUCCESS)
		goto cleanup;

	error = repo_init_createhead(repo);
	if (error < GIT_SUCCESS)
		goto cleanup;

	/* should never fail */
	assert(check_repository_dirs(repo) == GIT_SUCCESS);

	free(results.path_repository);
	*repo_out = repo;
	return GIT_SUCCESS;
552 553

cleanup:
554
	free(results.path_repository);
555
	git_repository_free(repo);
556
	return error;
557
}
558