notes.c 12.9 KB
Newer Older
schu committed
1 2 3 4 5 6 7 8 9 10 11
/*
 * 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 "notes.h"

#include "git2.h"
#include "refs.h"
12
#include "config.h"
13
#include "iterator.h"
schu committed
14

15 16 17 18 19 20
static int find_subtree_in_current_level(
	git_tree **out,
	git_repository *repo,
	git_tree *parent,
	const char *annotated_object_sha,
	int fanout)
schu committed
21 22 23 24
{
	unsigned int i;
	const git_tree_entry *entry;

25 26 27 28
	*out = NULL;
	
	if (parent == NULL)
		return GIT_ENOTFOUND;
schu committed
29

30 31
	for (i = 0; i < git_tree_entrycount(parent); i++) {
		entry = git_tree_entry_byindex(parent, i);
schu committed
32 33 34 35

		if (!git__ishex(git_tree_entry_name(entry)))
			continue;

36
		if (S_ISDIR(git_tree_entry_filemode(entry))
37 38 39
			&& strlen(git_tree_entry_name(entry)) == 2 
			&& !strncmp(git_tree_entry_name(entry), annotated_object_sha + fanout, 2))
			return git_tree_lookup(out, repo, git_tree_entry_id(entry));
schu committed
40

41 42 43 44
		/* Not a DIR, so do we have an already existing blob? */
		if (!strcmp(git_tree_entry_name(entry), annotated_object_sha + fanout))				
			return GIT_EEXISTS;
	}
schu committed
45

46 47
	return GIT_ENOTFOUND;
}
schu committed
48

49 50 51 52 53
static int find_subtree_r(git_tree **out, git_tree *root,
			git_repository *repo, const char *target, int *fanout)
{
	int error;
	git_tree *subtree = NULL;
schu committed
54

55
	*out = NULL;
schu committed
56

57
	error = find_subtree_in_current_level(&subtree, repo, root, target, *fanout);
58
	if (error == GIT_EEXISTS) {
59
		return git_tree_lookup(out, repo, git_tree_id(root));
schu committed
60 61
	}

62
	if (error < 0)
63
		return error;
64 65 66

	*fanout += 2;
	error = find_subtree_r(out, subtree, repo, target, fanout);
67
	git_tree_free(subtree);
68 69

	return error;
schu committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83
}

static int find_blob(git_oid *blob, git_tree *tree, const char *target)
{
	unsigned int i;
	const git_tree_entry *entry;

	for (i=0; i<git_tree_entrycount(tree); i++) {
		entry = git_tree_entry_byindex(tree, i);

		if (!strcmp(git_tree_entry_name(entry), target)) {
			/* found matching note object - return */

			git_oid_cpy(blob, git_tree_entry_id(entry));
84
			return 0;
schu committed
85 86
		}
	}
87
	return GIT_ENOTFOUND;
schu committed
88 89
}

90 91 92 93 94 95 96
static int tree_write(
	git_tree **out,
	git_repository *repo,
	git_tree *source_tree,
	const git_oid *object_oid,
	const char *treeentry_name,
	unsigned int attributes)
schu committed
97
{
98 99
	int error;
	git_treebuilder *tb = NULL;
100
	const git_tree_entry *entry;
101
	git_oid tree_oid;
schu committed
102

103 104
	if ((error = git_treebuilder_create(&tb, source_tree)) < 0)
		goto cleanup;
schu committed
105

106
	if (object_oid) {
107 108
		if ((error = git_treebuilder_insert(
				&entry, tb, treeentry_name, object_oid, attributes)) < 0)
109 110 111 112 113
			goto cleanup;
	} else {
		if ((error = git_treebuilder_remove(tb, treeentry_name)) < 0)
			goto cleanup;
	}
schu committed
114

115 116
	if ((error = git_treebuilder_write(&tree_oid, repo, tb)) < 0)
		goto cleanup;
schu committed
117

118
	error = git_tree_lookup(out, repo, &tree_oid);
schu committed
119

120 121 122 123
cleanup:
	git_treebuilder_free(tb);
	return error;
}
schu committed
124

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
static int manipulate_note_in_tree_r(
	git_tree **out,
	git_repository *repo,
	git_tree *parent,
	git_oid *note_oid,
	const char *annotated_object_sha,
	int fanout, 
	int (*note_exists_cb)(
		git_tree **out, 
		git_repository *repo, 
		git_tree *parent,
		git_oid *note_oid,
		const char *annotated_object_sha,
		int fanout,
		int current_error),
	int (*note_notfound_cb)(
		git_tree **out,
		git_repository *repo,
		git_tree *parent,
		git_oid *note_oid,
		const char *annotated_object_sha,
		int fanout,
		int current_error))
148 149
{
	int error = -1;	
150
	git_tree *subtree = NULL, *new = NULL;
151
	char subtree_name[3];
schu committed
152

153 154
	error = find_subtree_in_current_level(
		&subtree, repo, parent, annotated_object_sha, fanout);
schu committed
155

156
	if (error == GIT_EEXISTS) {
157 158
		error = note_exists_cb(
			out, repo, parent, note_oid, annotated_object_sha, fanout, error);
159 160
		goto cleanup;
	}
schu committed
161

162
	if (error == GIT_ENOTFOUND) {
163 164
		error = note_notfound_cb(
			out, repo, parent, note_oid, annotated_object_sha, fanout, error);
165 166
		goto cleanup;
	}
schu committed
167

168
	if (error < 0)
169
		goto cleanup;
schu committed
170

171
	/* An existing fanout has been found, let's dig deeper */
172
	error = manipulate_note_in_tree_r(
173
		&new, repo, subtree, note_oid, annotated_object_sha,
174
		fanout + 2, note_exists_cb, note_notfound_cb);
schu committed
175

176 177
	if (error < 0)
		goto cleanup;
schu committed
178

179 180
	strncpy(subtree_name, annotated_object_sha + fanout, 2);
	subtree_name[2] = '\0';
schu committed
181

182
	error = tree_write(out, repo, parent, git_tree_id(new),
nulltoken committed
183
			   subtree_name, GIT_FILEMODE_TREE);
184

185

186
cleanup:
187
	git_tree_free(new);
188 189 190
	git_tree_free(subtree);
	return error;
}
schu committed
191

192 193 194 195 196 197 198 199 200
static int remove_note_in_tree_eexists_cb(
	git_tree **out,
	git_repository *repo,
	git_tree *parent,
	git_oid *note_oid,
	const char *annotated_object_sha,
	int fanout,
	int current_error)
{
201 202
	GIT_UNUSED(note_oid);
	GIT_UNUSED(current_error);
schu committed
203

204 205
	return tree_write(out, repo, parent, NULL, annotated_object_sha + fanout, 0);
}
schu committed
206

207 208 209 210 211 212 213 214 215
static int remove_note_in_tree_enotfound_cb(
	git_tree **out,
	git_repository *repo,
	git_tree *parent,
	git_oid *note_oid,
	const char *annotated_object_sha,
	int fanout,
	int current_error)
{
216 217 218 219 220
	GIT_UNUSED(out);
	GIT_UNUSED(repo);
	GIT_UNUSED(parent);
	GIT_UNUSED(note_oid);
	GIT_UNUSED(fanout);
schu committed
221

222 223 224
	giterr_set(GITERR_REPOSITORY, "Object '%s' has no note", annotated_object_sha);
	return current_error;
}
schu committed
225

226 227 228 229 230 231 232 233
static int insert_note_in_tree_eexists_cb(git_tree **out,
	git_repository *repo,
	git_tree *parent,
	git_oid *note_oid,
	const char *annotated_object_sha,
	int fanout,
	int current_error)
{
234 235 236 237 238
	GIT_UNUSED(out);
	GIT_UNUSED(repo);
	GIT_UNUSED(parent);
	GIT_UNUSED(note_oid);
	GIT_UNUSED(fanout);
schu committed
239

240 241 242
	giterr_set(GITERR_REPOSITORY, "Note for '%s' exists already", annotated_object_sha);
	return current_error;
}
schu committed
243

244 245 246 247 248 249 250 251
static int insert_note_in_tree_enotfound_cb(git_tree **out,
	git_repository *repo,
	git_tree *parent,
	git_oid *note_oid,
	const char *annotated_object_sha,
	int fanout,
	int current_error)
{
252
	GIT_UNUSED(current_error);
schu committed
253

254
	/* No existing fanout at this level, insert in place */
nulltoken committed
255 256 257 258 259 260 261
	return tree_write(
		out,
		repo,
		parent,
		note_oid,
		annotated_object_sha + fanout,
		GIT_FILEMODE_BLOB);
schu committed
262 263
}

264 265 266 267 268 269 270 271 272
static int note_write(git_oid *out,
	git_repository *repo,
	git_signature *author,
	git_signature *committer,
	const char *notes_ref,
	const char *note,
	git_tree *commit_tree,
	const char *target,
	git_commit **parents)
schu committed
273
{
274
	int error;
schu committed
275
	git_oid oid;
276 277 278 279 280 281
	git_tree *tree = NULL;
	
	// TODO: should we apply filters?
	/* create note object */
	if ((error = git_blob_create_frombuffer(&oid, repo, note, strlen(note))) < 0)
		goto cleanup;
schu committed
282

283 284 285
	if ((error = manipulate_note_in_tree_r(
		&tree, repo, commit_tree, &oid, target, 0,
		insert_note_in_tree_eexists_cb, insert_note_in_tree_enotfound_cb)) < 0)
286
		goto cleanup;
schu committed
287

288 289
	if (out)
		git_oid_cpy(out, &oid);
290

291 292 293
	error = git_commit_create(&oid, repo, notes_ref, author, committer,
				  NULL, GIT_NOTES_DEFAULT_MSG_ADD,
				  tree, *parents == NULL ? 0 : 1, (const git_commit **) parents);
294

295
cleanup:
schu committed
296
	git_tree_free(tree);
297 298
	return error;
}
schu committed
299

300 301 302
static int note_new(git_note **out, git_oid *note_oid, git_blob *blob)
{
	git_note *note = NULL;
schu committed
303

304
	note = (git_note *)git__malloc(sizeof(git_note));
305
	GITERR_CHECK_ALLOC(note);
schu committed
306

307 308
	git_oid_cpy(&note->oid, note_oid);
	note->message = git__strdup((char *)git_blob_rawcontent(blob));
309
	GITERR_CHECK_ALLOC(note->message);
schu committed
310 311 312

	*out = note;

313
	return 0;
schu committed
314 315
}

316 317
static int note_lookup(git_note **out, git_repository *repo,
		       git_tree *tree, const char *target)
schu committed
318 319 320
{
	int error, fanout = 0;
	git_oid oid;
321 322 323
	git_blob *blob = NULL;
	git_note *note = NULL;
	git_tree *subtree = NULL;
schu committed
324

325 326
	if ((error = find_subtree_r(&subtree, tree, repo, target, &fanout)) < 0)
		goto cleanup;
schu committed
327

328 329
	if ((error = find_blob(&oid, subtree, target + fanout)) < 0)
		goto cleanup;
schu committed
330

331 332
	if ((error = git_blob_lookup(&blob, repo, &oid)) < 0)
		goto cleanup;
schu committed
333

334 335
	if ((error = note_new(&note, &oid, blob)) < 0)
		goto cleanup;
schu committed
336

337
	*out = note;
schu committed
338

339 340 341 342 343
cleanup:
	git_tree_free(subtree);
	git_blob_free(blob);
	return error;
}
schu committed
344

345 346 347 348 349 350 351 352 353
static int note_remove(git_repository *repo,
		       git_signature *author, git_signature *committer,
		       const char *notes_ref, git_tree *tree,
		       const char *target, git_commit **parents)
{
	int error;
	git_tree *tree_after_removal = NULL;
	git_oid oid;
		
354 355 356
	if ((error = manipulate_note_in_tree_r(
		&tree_after_removal, repo, tree, NULL, target, 0,
		remove_note_in_tree_eexists_cb, remove_note_in_tree_enotfound_cb)) < 0)
357
		goto cleanup;
schu committed
358 359

	error = git_commit_create(&oid, repo, notes_ref, author, committer,
360 361 362 363
	  NULL, GIT_NOTES_DEFAULT_MSG_RM,
	  tree_after_removal,
	  *parents == NULL ? 0 : 1,
	  (const git_commit **) parents);
schu committed
364

365 366
cleanup:
	git_tree_free(tree_after_removal);
schu committed
367 368 369
	return error;
}

370 371
static int note_get_default_ref(const char **out, git_repository *repo)
{
372
	int ret;
373 374 375 376 377 378 379
	git_config *cfg;

	*out = NULL;

	if (git_repository_config__weakptr(&cfg, repo) < 0)
		return -1;

380
	ret = git_config_get_string(out, cfg, "core.notesRef");
381
	if (ret == GIT_ENOTFOUND) {
382 383 384 385
		*out = GIT_NOTES_DEFAULT_REF;
		return 0;
	}

386
	return ret;
387 388
}

389 390 391 392 393 394 395 396
static int normalize_namespace(const char **notes_ref, git_repository *repo)
{
	if (*notes_ref)
		return 0;

	return note_get_default_ref(notes_ref, repo);
}

397 398 399 400 401
static int retrieve_note_tree_and_commit(
	git_tree **tree_out,
	git_commit **commit_out,
	git_repository *repo,
	const char **notes_ref)
402
{
403
	int error;
404 405
	git_oid oid;

406 407
	if ((error = normalize_namespace(notes_ref, repo)) < 0)
		return error;
408

409 410
	if ((error = git_reference_name_to_oid(&oid, repo, *notes_ref)) < 0)
		return error;
411

412 413
	if (git_commit_lookup(commit_out, repo, &oid) < 0)
		return error;
414

415 416
	if ((error = git_commit_tree(tree_out, *commit_out)) < 0)
		return error;
417

418
	return 0;
419 420
}

schu committed
421 422 423 424
int git_note_read(git_note **out, git_repository *repo,
		  const char *notes_ref, const git_oid *oid)
{
	int error;
425 426 427
	char *target = NULL;
	git_tree *tree = NULL;
	git_commit *commit = NULL;
schu committed
428 429

	target = git_oid_allocfmt(oid);
430
	GITERR_CHECK_ALLOC(target);
schu committed
431

432 433 434 435
	if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref)) < 0)
		goto cleanup;

	error = note_lookup(out, repo, tree, target);
schu committed
436

437
cleanup:
schu committed
438
	git__free(target);
439 440
	git_tree_free(tree);
	git_commit_free(commit);
441
	return error;
schu committed
442 443
}

444 445 446 447 448
int git_note_create(
	git_oid *out, git_repository *repo,
	git_signature *author, git_signature *committer,
	const char *notes_ref, const git_oid *oid,
	const char *note)
schu committed
449
{
450 451
	int error;
	char *target = NULL;
schu committed
452
	git_commit *commit = NULL;
453
	git_tree *tree = NULL;
schu committed
454 455

	target = git_oid_allocfmt(oid);
456
	GITERR_CHECK_ALLOC(target);
schu committed
457

458 459 460 461 462
	error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref);

	if (error < 0 && error != GIT_ENOTFOUND)
		goto cleanup;

schu committed
463
	error = note_write(out, repo, author, committer, notes_ref,
464
			   note, tree, target, &commit);
schu committed
465

466
cleanup:
schu committed
467 468
	git__free(target);
	git_commit_free(commit);
469
	git_tree_free(tree);
470
	return error;
schu committed
471 472 473 474 475 476 477
}

int git_note_remove(git_repository *repo, const char *notes_ref,
		    git_signature *author, git_signature *committer,
		    const git_oid *oid)
{
	int error;
478 479 480
	char *target = NULL;
	git_commit *commit = NULL;
	git_tree *tree = NULL;
schu committed
481 482

	target = git_oid_allocfmt(oid);
483
	GITERR_CHECK_ALLOC(target);
schu committed
484

485 486 487
	if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref)) < 0)
		goto cleanup;

schu committed
488
	error = note_remove(repo, author, committer, notes_ref,
489
			    tree, target, &commit);
schu committed
490

491
cleanup:
schu committed
492 493
	git__free(target);
	git_commit_free(commit);
494
	git_tree_free(tree);
495
	return error;
schu committed
496 497
}

498 499 500 501 502 503
int git_note_default_ref(const char **out, git_repository *repo)
{
	assert(repo);
	return note_get_default_ref(out, repo);
}

schu committed
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
const char * git_note_message(git_note *note)
{
	assert(note);
	return note->message;
}

const git_oid * git_note_oid(git_note *note)
{
	assert(note);
	return &note->oid;
}

void git_note_free(git_note *note)
{
	if (note == NULL)
		return;

	git__free(note->message);
	git__free(note);
}
524 525 526

static int process_entry_path(
	const char* entry_path,
527 528
	const git_oid *note_oid,
	int (*note_cb)(git_note_data *note_data, void *payload),
529 530
	void *payload)
{
531 532
	int error = -1;
	size_t i = 0, j = 0, len;
533
	git_buf buf = GIT_BUF_INIT;
534
	git_note_data note_data;
535

536
	if ((error = git_buf_puts(&buf, entry_path)) < 0)
537
		goto cleanup;
538

539 540 541 542 543 544 545
	len = git_buf_len(&buf);

	while (i < len) {
		if (buf.ptr[i] == '/') {
			i++;
			continue;
		}
546

547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
		if (git__fromhex(buf.ptr[i]) < 0) {
			/* This is not a note entry */
			goto cleanup;
		}

		if (i != j)
			buf.ptr[j] = buf.ptr[i];

		i++;
		j++;
	}

	buf.ptr[j] = '\0';
	buf.size = j;

	if (j != GIT_OID_HEXSZ) {
		/* This is not a note entry */
		goto cleanup;
	}

567 568 569
	if ((error = git_oid_fromstr(
			&note_data.annotated_object_oid, buf.ptr)) < 0)
		goto cleanup;
570

571 572
	git_oid_cpy(&note_data.blob_oid, note_oid);

573 574
	if (note_cb(&note_data, payload))
		error = GIT_EUSER;
575 576 577 578 579 580 581 582 583

cleanup:
	git_buf_free(&buf);
	return error;
}

int git_note_foreach(
	git_repository *repo,
	const char *notes_ref,
584
	int (*note_cb)(git_note_data *note_data, void *payload),
585 586
	void *payload)
{
587
	int error;
588 589
	git_iterator *iter = NULL;
	git_tree *tree = NULL;
590
	git_commit *commit = NULL;
nulltoken committed
591
	const git_index_entry *item;
592

593 594 595 596
	if (!(error = retrieve_note_tree_and_commit(
			&tree, &commit, repo, &notes_ref)) &&
		!(error = git_iterator_for_tree(&iter, repo, tree)))
		error = git_iterator_current(iter, &item);
597

598 599
	while (!error && item) {
		error = process_entry_path(item->path, &item->oid, note_cb, payload);
600

601 602
		if (!error)
			error = git_iterator_advance(iter, &item);
603 604 605 606
	}

	git_iterator_free(iter);
	git_tree_free(tree);
607
	git_commit_free(commit);
608

609 610
	return error;
}