repository.c 12.9 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
typedef struct {
44
	char *path_repository;
45
	unsigned is_bare:1, has_been_reinit:1;
46
} repo_init;
47

48 49 50 51 52
/*
 * Git repository open methods
 *
 * Open a repository object from its path
 */
53 54
static int assign_repository_dirs(
		git_repository *repo,
55 56 57
		const char *git_dir,
		const char *git_object_directory,
		const char *git_index_file,
58
		const char *git_work_tree)
59 60
{
	char path_aux[GIT_PATH_MAX];
61
	int error = GIT_SUCCESS;
62 63 64

	assert(repo);

65
	if (git_dir == NULL)
66
		return git__throw(GIT_ENOTFOUND, "Failed to open repository. Git dir not found");
67

68
	error = gitfo_prettify_dir_path(path_aux, sizeof(path_aux), git_dir);
69
	if (error < GIT_SUCCESS)
70
		return git__rethrow(error, "Failed to open repository");
71

72
	/* store GIT_DIR */
73
	repo->path_repository = git__strdup(path_aux);
74 75
	if (repo->path_repository == NULL)
		return GIT_ENOMEM;
76

77
	/* path to GIT_OBJECT_DIRECTORY */
78
	if (git_object_directory == NULL)
79
		git__joinpath(path_aux, repo->path_repository, GIT_OBJECTS_DIR);
80
	else {
81
		error = gitfo_prettify_dir_path(path_aux, sizeof(path_aux), git_object_directory);
82
		if (error < GIT_SUCCESS)
83
			return git__rethrow(error, "Failed to open repository");
84
	}
85

86
	/* Store GIT_OBJECT_DIRECTORY */
87
	repo->path_odb = git__strdup(path_aux);
88 89
	if (repo->path_odb == NULL)
		return GIT_ENOMEM;
90

91
	/* path to GIT_WORK_TREE */
92 93
	if (git_work_tree == NULL)
		repo->is_bare = 1;
94
	else {
95
		error = gitfo_prettify_dir_path(path_aux, sizeof(path_aux), git_work_tree);
96
		if (error < GIT_SUCCESS)
97
			return git__rethrow(error, "Failed to open repository");
98 99

		/* Store GIT_WORK_TREE */
100
		repo->path_workdir = git__strdup(path_aux);
101 102
		if (repo->path_workdir == NULL)
			return GIT_ENOMEM;
103 104 105 106 107

		/* Path to GIT_INDEX_FILE */
		if (git_index_file == NULL)
			git__joinpath(path_aux, repo->path_repository, GIT_INDEX_FILE);
		else {
108
			error = gitfo_prettify_file_path(path_aux, sizeof(path_aux), git_index_file);
109
			if (error < GIT_SUCCESS)
110
				return git__rethrow(error, "Failed to open repository");
111 112 113 114 115 116
		}

		/* store GIT_INDEX_FILE */
		repo->path_index = git__strdup(path_aux);
		if (repo->path_index == NULL)
			return GIT_ENOMEM;
117 118
	}
	
119 120 121
	return GIT_SUCCESS;
}

122
static int check_repository_dirs(git_repository *repo)
123
{
124
	char path_aux[GIT_PATH_MAX];
125

126
	if (gitfo_isdir(repo->path_repository) < GIT_SUCCESS)
127
		return git__throw(GIT_ENOTAREPO, "`%s` is not a folder", repo->path_repository);
128

129 130
	/* Ensure GIT_OBJECT_DIRECTORY exists */
	if (gitfo_isdir(repo->path_odb) < GIT_SUCCESS)
131
		return git__throw(GIT_ENOTAREPO, "`%s` does not exist", repo->path_odb);
132 133 134 135

	/* Ensure HEAD file exists */
	git__joinpath(path_aux, repo->path_repository, GIT_HEAD_FILE);
	if (gitfo_exists(path_aux) < 0)
136
		return git__throw(GIT_ENOTAREPO, "HEAD file is missing");
137 138 139 140 141 142 143 144

	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;
145

146
	/* Git directory name */
147
	if (git__basename_r(buffer, sizeof(buffer), repository_path) < 0)
148
		return git__throw(GIT_EINVALIDPATH, "Unable to parse folder name from `%s`", repository_path);
149

150
	if (strcmp(buffer, DOT_GIT) == 0) {
151
		/* Path to working dir */
152
		if (git__dirname_r(buffer, sizeof(buffer), repository_path) < 0)
153
			return git__throw(GIT_EINVALIDPATH, "Unable to parse parent folder name from `%s`", repository_path);
154
		path_work_tree = buffer;
155 156
	}

157
	return assign_repository_dirs(repo, repository_path, NULL, NULL, path_work_tree);
158 159
}

160
static git_repository *repository_alloc()
161
{
162 163
	int error;

164 165 166 167 168 169
	git_repository *repo = git__malloc(sizeof(git_repository));
	if (!repo)
		return NULL;

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

170 171 172 173 174
	error = git_cache_init(&repo->objects, GIT_DEFAULT_CACHE_SIZE, &git_object__free);
	if (error < GIT_SUCCESS) {
		free(repo);
		return NULL;
	}
175

176
	if (git_repository__refcache_init(&repo->references) < GIT_SUCCESS) {
177 178 179 180
		free(repo);
		return NULL;
	}

181 182 183
	return repo;
}

184 185
static int init_odb(git_repository *repo)
{
186
	return git_odb_open(&repo->db, repo->path_odb);	/* TODO: Move odb.c to new error handling */
187 188 189 190 191 192 193 194 195 196 197 198 199 200
}

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)
201
		return git__throw(GIT_EINVALIDARGS, "Failed to open repository. `object_database` can't be null");
202 203 204 205 206

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

207
	error = assign_repository_dirs(repo, 
208 209 210
			git_dir, 
			NULL,
			git_index_file,
211
			git_work_tree);
212 213 214 215

	if (error < GIT_SUCCESS)
		goto cleanup;

216 217 218 219
	error = check_repository_dirs(repo);
	if (error < GIT_SUCCESS)
		goto cleanup;

220 221 222 223 224 225 226
	repo->db = object_database;

	*repo_out = repo;
	return GIT_SUCCESS;

cleanup:
	git_repository_free(repo);
227
	return git__rethrow(error, "Failed to open repository");
228 229 230
}


231 232 233 234 235 236 237 238 239 240 241
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);

242
	repo = repository_alloc();
243 244 245
	if (repo == NULL)
		return GIT_ENOMEM;

246
	error = assign_repository_dirs(repo,
247 248 249
			git_dir, 
			git_object_directory,
			git_index_file,
250 251 252 253
			git_work_tree);

	if (error < GIT_SUCCESS)
		goto cleanup;
254

255
	error = check_repository_dirs(repo);
256
	if (error < GIT_SUCCESS)
257 258
		goto cleanup;

259
	error = init_odb(repo);
260
	if (error < GIT_SUCCESS)
261 262 263 264 265 266 267
		goto cleanup;

	*repo_out = repo;
	return GIT_SUCCESS;

cleanup:
	git_repository_free(repo);
268
	return git__rethrow(error, "Failed to open repository");
269 270
}

271
int git_repository_open(git_repository **repo_out, const char *path)
272 273
{
	git_repository *repo;
Vicent Marti committed
274 275 276
	int error = GIT_SUCCESS;

	assert(repo_out && path);
277

278
	repo = repository_alloc();
279
	if (repo == NULL)
Vicent Marti committed
280
		return GIT_ENOMEM;
281

282 283 284 285 286
	error = guess_repository_dirs(repo, path);
	if (error < GIT_SUCCESS)
		goto cleanup;

	error = check_repository_dirs(repo);
287
	if (error < GIT_SUCCESS)
Vicent Marti committed
288
		goto cleanup;
289

290
	error = init_odb(repo);
291
	if (error < GIT_SUCCESS)
Vicent Marti committed
292 293 294 295 296 297 298
		goto cleanup;

	*repo_out = repo;
	return GIT_SUCCESS;

cleanup:
	git_repository_free(repo);
299
	return git__rethrow(error, "Failed to open repository");
300 301
}

302 303 304 305
void git_repository_free(git_repository *repo)
{
	if (repo == NULL)
		return;
306

Vicent Marti committed
307 308
	git_cache_free(&repo->objects);
	git_repository__refcache_free(&repo->references);
309

310 311 312 313 314 315 316 317
	free(repo->path_workdir);
	free(repo->path_index);
	free(repo->path_repository);
	free(repo->path_odb);

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

318 319
	if (repo->index != NULL) {
		repo->index->repository = NULL;
320
		git_index_free(repo->index);
321
	}
322 323

	free(repo);
324 325
}

326
int git_repository_index(git_index **index_out, git_repository *repo)
327
{
328 329 330 331
	int error;

	assert(index_out && repo);

332
	if (repo->index == NULL) {
333
		error = git_index_open_inrepo(&repo->index, repo);	/* TODO: move index.c to new error handling */
334
		if (error < GIT_SUCCESS)
335
			return git__rethrow(error, "Failed to open repository index");
Vicent Marti committed
336

337
		assert(repo->index != NULL);
338 339
	}

340 341
	*index_out = repo->index;
	return GIT_SUCCESS;
342 343
}

344 345 346 347 348 349
git_odb *git_repository_database(git_repository *repo)
{
	assert(repo);
	return repo->db;
}

350
static int repo_init_reinit(repo_init *results)
351 352
{
	/* TODO: reinit the repository */
353
	results->has_been_reinit = 1;
354
	return git__throw(GIT_ENOTIMPLEMENTED, "Failed to reinitialize the repository. This feature is not yet implemented");
355 356
}

357
static int repo_init_createhead(git_repository *repo)
358
{
359
	git_reference *head_reference;
360
	return git_reference_create_symbolic(&head_reference, repo, GIT_HEAD_FILE, GIT_REFS_HEADS_MASTER_FILE);	/* TODO: finalize moving refs.c to new error handling */
361
}
362

363 364 365
static int repo_init_check_head_existence(char * repository_path)
{
	char temp_path[GIT_PATH_MAX];
366

367 368
	git__joinpath(temp_path, repository_path, GIT_HEAD_FILE);
	return gitfo_exists(temp_path);
369 370
}

371
static int repo_init_structure(repo_init *results)
372
{
373
	const int mode = 0755; /* or 0777 ? */
374
	int error;
375

376 377
	char temp_path[GIT_PATH_MAX];
	char *git_dir = results->path_repository;
378

379
	if (gitfo_mkdir_recurs(git_dir, mode))
380
		return git__throw(GIT_ERROR, "Failed to initialize repository structure. Could not mkdir");
381

382
	/* Creates the '/objects/info/' directory */
383
	git__joinpath(temp_path, git_dir, GIT_OBJECTS_INFO_DIR);
384 385
	error = gitfo_mkdir_recurs(temp_path, mode);
	if (error < GIT_SUCCESS)
386
		return git__rethrow(error, "Failed to initialize repository structure");
387

388
	/* Creates the '/objects/pack/' directory */
389
	git__joinpath(temp_path, git_dir, GIT_OBJECTS_PACK_DIR);
390 391 392
	error = gitfo_mkdir(temp_path, mode);
	if (error < GIT_SUCCESS)
		return git__throw(error, "Unable to create `%s` folder", temp_path);
393

394
	/* Creates the '/refs/heads/' directory */
395
	git__joinpath(temp_path, git_dir, GIT_REFS_HEADS_DIR);
396 397
	error = gitfo_mkdir_recurs(temp_path, mode);
	if (error < GIT_SUCCESS)
398
		return git__rethrow(error, "Failed to initialize repository structure");
399 400

	/* Creates the '/refs/tags/' directory */
401
	git__joinpath(temp_path, git_dir, GIT_REFS_TAGS_DIR);
402 403 404
	error = gitfo_mkdir(temp_path, mode);
	if (error < GIT_SUCCESS)
		return git__throw(error, "Unable to create `%s` folder", temp_path);
405

406
	/* TODO: what's left? templates? */
407 408 409 410

	return GIT_SUCCESS;
}

411
static int repo_init_find_dir(repo_init *results, const char* path)
412 413
{
	char temp_path[GIT_PATH_MAX];
414
	int error = GIT_SUCCESS;
415

416
	error = gitfo_prettify_dir_path(temp_path, sizeof(temp_path), path);
417
	if (error < GIT_SUCCESS)
418
		return git__rethrow(error, "Failed to find directory to initialize repository");
419

420
	if (!results->is_bare) {
421
		git__joinpath(temp_path, temp_path, GIT_DIR);
422 423 424
	}

	results->path_repository = git__strdup(temp_path);
425 426
	if (results->path_repository == NULL)
		return GIT_ENOMEM;
427 428 429 430

	return GIT_SUCCESS;
}

431
int git_repository_init(git_repository **repo_out, const char *path, unsigned is_bare)
432 433
{
	int error = GIT_SUCCESS;
434
	git_repository *repo = NULL;
435
	repo_init results;
436 437 438
	
	assert(repo_out && path);

439
	results.path_repository = NULL;
440
	results.is_bare = is_bare;
441

442
	error = repo_init_find_dir(&results, path);
443 444 445
	if (error < GIT_SUCCESS)
		goto cleanup;

446 447 448
	if (!repo_init_check_head_existence(results.path_repository))
		return repo_init_reinit(&results);

449
	error = repo_init_structure(&results);
450 451 452
	if (error < GIT_SUCCESS)
		goto cleanup;

453 454 455 456 457 458 459
	repo = repository_alloc();
	if (repo == NULL) {
		error = GIT_ENOMEM;
		goto cleanup;
	}

	error = guess_repository_dirs(repo, results.path_repository);
460 461 462
	if (error < GIT_SUCCESS)
		goto cleanup;

463
	assert(repo->is_bare == is_bare);
464

465 466 467 468 469 470 471 472 473 474 475 476 477 478
	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;
479 480

cleanup:
481
	free(results.path_repository);
482
	git_repository_free(repo);
483
	return git__rethrow(error, "Failed to (re)init the repository `%s`", path);
484
}
485

486 487 488 489 490 491 492
int git_repository_is_empty(git_repository *repo)
{
	git_reference *head, *branch;
	int error;

	error = git_reference_lookup(&head, repo, "HEAD");
	if (error < GIT_SUCCESS)
493
		return git__throw(error, "Failed to determine the emptiness of the repository. An error occured while retrieving the HEAD reference");
494 495

	if (git_reference_type(head) != GIT_REF_SYMBOLIC)
496
		return git__throw(GIT_EOBJCORRUPTED, "Failed to determine the emptiness of the repository. HEAD is probably in detached state");
497 498 499 500

	return git_reference_resolve(&branch, head) == GIT_SUCCESS ? 0 : 1;
}

501 502 503 504 505 506 507 508 509 510 511
const char *git_repository_path(git_repository *repo)
{
	assert(repo);
	return repo->path_repository;
}

const char *git_repository_workdir(git_repository *repo)
{
	assert(repo);
	return repo->path_workdir;
}