notes.c 14.5 KB
Newer Older
schu committed
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
schu committed
3 4 5 6 7 8 9 10 11
 *
 * 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"
Ben Straub committed
14
#include "signature.h"
schu committed
15

16 17 18 19 20 21
static int note_error_notfound(void)
{
	giterr_set(GITERR_INVALID, "Note could not be found");
	return GIT_ENOTFOUND;
}

22 23 24 25 26 27
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
28
{
29
	size_t i;
schu committed
30 31
	const git_tree_entry *entry;

32
	*out = NULL;
33

34
	if (parent == NULL)
35
		return note_error_notfound();
schu committed
36

37 38
	for (i = 0; i < git_tree_entrycount(parent); i++) {
		entry = git_tree_entry_byindex(parent, i);
schu committed
39 40 41 42

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

43
		if (S_ISDIR(git_tree_entry_filemode(entry))
44
			&& strlen(git_tree_entry_name(entry)) == 2
45 46
			&& !strncmp(git_tree_entry_name(entry), annotated_object_sha + fanout, 2))
			return git_tree_lookup(out, repo, git_tree_entry_id(entry));
schu committed
47

48
		/* Not a DIR, so do we have an already existing blob? */
49
		if (!strcmp(git_tree_entry_name(entry), annotated_object_sha + fanout))
50 51
			return GIT_EEXISTS;
	}
schu committed
52

53
	return note_error_notfound();
54
}
schu committed
55

56 57 58 59 60
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
61

62
	*out = NULL;
schu committed
63

64
	error = find_subtree_in_current_level(&subtree, repo, root, target, *fanout);
65
	if (error == GIT_EEXISTS)
66
		return git_tree_lookup(out, repo, git_tree_id(root));
schu committed
67

68
	if (error < 0)
69
		return error;
70 71 72

	*fanout += 2;
	error = find_subtree_r(out, subtree, repo, target, fanout);
73
	git_tree_free(subtree);
74 75

	return error;
schu committed
76 77 78 79
}

static int find_blob(git_oid *blob, git_tree *tree, const char *target)
{
80
	size_t i;
schu committed
81 82 83 84 85 86 87 88 89
	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));
90
			return 0;
schu committed
91 92
		}
	}
93 94

	return note_error_notfound();
schu committed
95 96
}

97 98 99 100 101 102 103
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
104
{
105 106
	int error;
	git_treebuilder *tb = NULL;
107
	const git_tree_entry *entry;
108
	git_oid tree_oid;
schu committed
109

110
	if ((error = git_treebuilder_new(&tb, repo, source_tree)) < 0)
111
		goto cleanup;
schu committed
112

113
	if (object_oid) {
114 115
		if ((error = git_treebuilder_insert(
				&entry, tb, treeentry_name, object_oid, attributes)) < 0)
116 117 118 119 120
			goto cleanup;
	} else {
		if ((error = git_treebuilder_remove(tb, treeentry_name)) < 0)
			goto cleanup;
	}
schu committed
121

122
	if ((error = git_treebuilder_write(&tree_oid, tb)) < 0)
123
		goto cleanup;
schu committed
124

125
	error = git_tree_lookup(out, repo, &tree_oid);
schu committed
126

127 128 129 130
cleanup:
	git_treebuilder_free(tb);
	return error;
}
schu committed
131

132 133 134 135 136 137
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,
138
	int fanout,
139
	int (*note_exists_cb)(
140 141
		git_tree **out,
		git_repository *repo,
142 143 144 145 146 147 148 149 150 151 152 153 154
		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))
155
{
156
	int error;
157
	git_tree *subtree = NULL, *new = NULL;
158
	char subtree_name[3];
schu committed
159

160 161
	error = find_subtree_in_current_level(
		&subtree, repo, parent, annotated_object_sha, fanout);
schu committed
162

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

169
	if (error == GIT_ENOTFOUND) {
170 171
		error = note_notfound_cb(
			out, repo, parent, note_oid, annotated_object_sha, fanout, error);
172 173
		goto cleanup;
	}
schu committed
174

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

178
	/* An existing fanout has been found, let's dig deeper */
179
	error = manipulate_note_in_tree_r(
180
		&new, repo, subtree, note_oid, annotated_object_sha,
181
		fanout + 2, note_exists_cb, note_notfound_cb);
schu committed
182

183 184
	if (error < 0)
		goto cleanup;
schu committed
185

186 187
	strncpy(subtree_name, annotated_object_sha + fanout, 2);
	subtree_name[2] = '\0';
schu committed
188

189
	error = tree_write(out, repo, parent, git_tree_id(new),
nulltoken committed
190
			   subtree_name, GIT_FILEMODE_TREE);
191

192

193
cleanup:
194
	git_tree_free(new);
195 196 197
	git_tree_free(subtree);
	return error;
}
schu committed
198

199 200 201 202 203 204 205 206 207
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)
{
208 209
	GIT_UNUSED(note_oid);
	GIT_UNUSED(current_error);
schu committed
210

211 212
	return tree_write(out, repo, parent, NULL, annotated_object_sha + fanout, 0);
}
schu committed
213

214 215 216 217 218 219 220 221 222
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)
{
223 224 225 226 227
	GIT_UNUSED(out);
	GIT_UNUSED(repo);
	GIT_UNUSED(parent);
	GIT_UNUSED(note_oid);
	GIT_UNUSED(fanout);
schu committed
228

229 230 231
	giterr_set(GITERR_REPOSITORY, "Object '%s' has no note", annotated_object_sha);
	return current_error;
}
schu committed
232

233 234 235 236 237 238 239 240
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)
{
241 242 243 244 245
	GIT_UNUSED(out);
	GIT_UNUSED(repo);
	GIT_UNUSED(parent);
	GIT_UNUSED(note_oid);
	GIT_UNUSED(fanout);
schu committed
246

247 248 249
	giterr_set(GITERR_REPOSITORY, "Note for '%s' exists already", annotated_object_sha);
	return current_error;
}
schu committed
250

251 252 253 254 255 256 257 258
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)
{
259
	GIT_UNUSED(current_error);
schu committed
260

261
	/* No existing fanout at this level, insert in place */
nulltoken committed
262 263 264 265 266 267 268
	return tree_write(
		out,
		repo,
		parent,
		note_oid,
		annotated_object_sha + fanout,
		GIT_FILEMODE_BLOB);
schu committed
269 270
}

271 272
static int note_write(git_oid *out,
	git_repository *repo,
Ben Straub committed
273 274
	const git_signature *author,
	const git_signature *committer,
275 276 277 278
	const char *notes_ref,
	const char *note,
	git_tree *commit_tree,
	const char *target,
279 280
	git_commit **parents,
	int allow_note_overwrite)
schu committed
281
{
282
	int error;
schu committed
283
	git_oid oid;
284
	git_tree *tree = NULL;
285

286 287 288 289
	// TODO: should we apply filters?
	/* create note object */
	if ((error = git_blob_create_frombuffer(&oid, repo, note, strlen(note))) < 0)
		goto cleanup;
schu committed
290

291 292
	if ((error = manipulate_note_in_tree_r(
		&tree, repo, commit_tree, &oid, target, 0,
293 294
		allow_note_overwrite ? insert_note_in_tree_enotfound_cb : insert_note_in_tree_eexists_cb,
		insert_note_in_tree_enotfound_cb)) < 0)
295
		goto cleanup;
schu committed
296

297 298
	if (out)
		git_oid_cpy(out, &oid);
299

300 301 302
	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);
303

304
cleanup:
schu committed
305
	git_tree_free(tree);
306 307
	return error;
}
schu committed
308

309 310 311 312 313
static int note_new(
	git_note **out,
	git_oid *note_oid,
	git_commit *commit,
	git_blob *blob)
314 315
{
	git_note *note = NULL;
schu committed
316

317
	note = git__malloc(sizeof(git_note));
318
	GITERR_CHECK_ALLOC(note);
schu committed
319

320
	git_oid_cpy(&note->id, note_oid);
321 322 323 324 325

	if (git_signature_dup(&note->author, git_commit_author(commit)) < 0 ||
		git_signature_dup(&note->committer, git_commit_committer(commit)) < 0)
		return -1;

Vicent Marti committed
326 327
	note->message = git__strndup(git_blob_rawcontent(blob), git_blob_rawsize(blob));
	GITERR_CHECK_ALLOC(note->message);
schu committed
328 329

	*out = note;
330
	return 0;
schu committed
331 332
}

333
static int note_lookup(
334 335 336 337 338
	git_note **out,
	git_repository *repo,
	git_commit *commit,
	git_tree *tree,
	const char *target)
schu committed
339 340 341
{
	int error, fanout = 0;
	git_oid oid;
342 343 344
	git_blob *blob = NULL;
	git_note *note = NULL;
	git_tree *subtree = NULL;
schu committed
345

346 347
	if ((error = find_subtree_r(&subtree, tree, repo, target, &fanout)) < 0)
		goto cleanup;
schu committed
348

349 350
	if ((error = find_blob(&oid, subtree, target + fanout)) < 0)
		goto cleanup;
schu committed
351

352 353
	if ((error = git_blob_lookup(&blob, repo, &oid)) < 0)
		goto cleanup;
schu committed
354

355
	if ((error = note_new(&note, &oid, commit, blob)) < 0)
356
		goto cleanup;
schu committed
357

358
	*out = note;
schu committed
359

360 361 362 363 364
cleanup:
	git_tree_free(subtree);
	git_blob_free(blob);
	return error;
}
schu committed
365

366
static int note_remove(git_repository *repo,
Ben Straub committed
367 368 369
		const git_signature *author, const git_signature *committer,
		const char *notes_ref, git_tree *tree,
		const char *target, git_commit **parents)
370 371 372 373
{
	int error;
	git_tree *tree_after_removal = NULL;
	git_oid oid;
374

375 376 377
	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)
378
		goto cleanup;
schu committed
379 380

	error = git_commit_create(&oid, repo, notes_ref, author, committer,
381 382 383 384
	  NULL, GIT_NOTES_DEFAULT_MSG_RM,
	  tree_after_removal,
	  *parents == NULL ? 0 : 1,
	  (const git_commit **) parents);
schu committed
385

386 387
cleanup:
	git_tree_free(tree_after_removal);
schu committed
388 389 390
	return error;
}

391
static int note_get_default_ref(char **out, git_repository *repo)
392 393
{
	git_config *cfg;
394
	int ret = git_repository_config__weakptr(&cfg, repo);
395

396 397
	*out = (ret != 0) ? NULL : git_config__get_string_force(
		cfg, "core.notesref", GIT_NOTES_DEFAULT_REF);
398

399
	return ret;
400 401
}

402
static int normalize_namespace(char **out, git_repository *repo, const char *notes_ref)
403
{
404 405 406
	if (notes_ref) {
		*out = git__strdup(notes_ref);
		GITERR_CHECK_ALLOC(*out);
407
		return 0;
408
	}
409

410
	return note_get_default_ref(out, repo);
411 412
}

413 414 415
static int retrieve_note_tree_and_commit(
	git_tree **tree_out,
	git_commit **commit_out,
416
	char **notes_ref_out,
417
	git_repository *repo,
418
	const char *notes_ref)
419
{
420
	int error;
421 422
	git_oid oid;

423
	if ((error = normalize_namespace(notes_ref_out, repo, notes_ref)) < 0)
424
		return error;
425

426
	if ((error = git_reference_name_to_id(&oid, repo, *notes_ref_out)) < 0)
427
		return error;
428

429 430
	if (git_commit_lookup(commit_out, repo, &oid) < 0)
		return error;
431

432 433
	if ((error = git_commit_tree(tree_out, *commit_out)) < 0)
		return error;
434

435
	return 0;
436 437
}

schu committed
438
int git_note_read(git_note **out, git_repository *repo,
439
		  const char *notes_ref_in, const git_oid *oid)
schu committed
440 441
{
	int error;
442
	char *target = NULL, *notes_ref = NULL;
443 444
	git_tree *tree = NULL;
	git_commit *commit = NULL;
schu committed
445 446

	target = git_oid_allocfmt(oid);
447
	GITERR_CHECK_ALLOC(target);
schu committed
448

449
	if (!(error = retrieve_note_tree_and_commit(
450
		      &tree, &commit, &notes_ref, repo, notes_ref_in)))
451
		error = note_lookup(out, repo, commit, tree, target);
schu committed
452

453
	git__free(notes_ref);
schu committed
454
	git__free(target);
455 456
	git_tree_free(tree);
	git_commit_free(commit);
457
	return error;
schu committed
458 459
}

460
int git_note_create(
Ben Straub committed
461 462
	git_oid *out,
	git_repository *repo,
463
	const char *notes_ref_in,
Ben Straub committed
464 465 466
	const git_signature *author,
	const git_signature *committer,
	const git_oid *oid,
467 468
	const char *note,
	int allow_note_overwrite)
schu committed
469
{
470
	int error;
471
	char *target = NULL, *notes_ref = NULL;
schu committed
472
	git_commit *commit = NULL;
473
	git_tree *tree = NULL;
schu committed
474 475

	target = git_oid_allocfmt(oid);
476
	GITERR_CHECK_ALLOC(target);
schu committed
477

478
	error = retrieve_note_tree_and_commit(&tree, &commit, &notes_ref, repo, notes_ref_in);
479 480 481 482

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

schu committed
483
	error = note_write(out, repo, author, committer, notes_ref,
484
			note, tree, target, &commit, allow_note_overwrite);
schu committed
485

486
cleanup:
487
	git__free(notes_ref);
schu committed
488 489
	git__free(target);
	git_commit_free(commit);
490
	git_tree_free(tree);
491
	return error;
schu committed
492 493
}

494
int git_note_remove(git_repository *repo, const char *notes_ref_in,
Ben Straub committed
495 496
		const git_signature *author, const git_signature *committer,
		const git_oid *oid)
schu committed
497 498
{
	int error;
499
	char *target = NULL, *notes_ref;
500 501
	git_commit *commit = NULL;
	git_tree *tree = NULL;
schu committed
502 503

	target = git_oid_allocfmt(oid);
504
	GITERR_CHECK_ALLOC(target);
schu committed
505

506
	if (!(error = retrieve_note_tree_and_commit(
507
		      &tree, &commit, &notes_ref, repo, notes_ref_in)))
508 509
		error = note_remove(
			repo, author, committer, notes_ref, tree, target, &commit);
schu committed
510

511
	git__free(notes_ref);
schu committed
512 513
	git__free(target);
	git_commit_free(commit);
514
	git_tree_free(tree);
515
	return error;
schu committed
516 517
}

518
int git_note_default_ref(git_buf *out, git_repository *repo)
519
{
520 521 522 523 524 525 526 527 528 529 530 531
	char *default_ref;
	int error;

	assert(out && repo);

	git_buf_sanitize(out);

	if ((error = note_get_default_ref(&default_ref, repo)) < 0)
		return error;

	git_buf_attach(out, default_ref, strlen(default_ref));
	return 0;
532 533
}

534 535 536 537 538 539 540 541 542 543 544 545
const git_signature *git_note_committer(const git_note *note)
{
	assert(note);
	return note->committer;
}

const git_signature *git_note_author(const git_note *note)
{
	assert(note);
	return note->author;
}

Ben Straub committed
546
const char * git_note_message(const git_note *note)
schu committed
547 548 549 550 551
{
	assert(note);
	return note->message;
}

552
const git_oid * git_note_id(const git_note *note)
schu committed
553 554
{
	assert(note);
555
	return &note->id;
schu committed
556 557 558 559 560 561 562
}

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

563 564
	git_signature_free(note->committer);
	git_signature_free(note->author);
schu committed
565 566 567
	git__free(note->message);
	git__free(note);
}
568 569 570

static int process_entry_path(
	const char* entry_path,
571
	git_oid *annotated_object_id)
572
{
573
	int error = 0;
574
	size_t i = 0, j = 0, len;
575 576
	git_buf buf = GIT_BUF_INIT;

577
	if ((error = git_buf_puts(&buf, entry_path)) < 0)
578
		goto cleanup;
579

580 581 582 583 584 585 586
	len = git_buf_len(&buf);

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

588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
		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;
	}

608
	error = git_oid_fromstr(annotated_object_id, buf.ptr);
609 610 611 612 613 614 615

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

int git_note_foreach(
Linquize committed
616 617 618 619
	git_repository *repo,
	const char *notes_ref,
	git_note_foreach_cb note_cb,
	void *payload)
620
{
Linquize committed
621 622 623
	int error;
	git_note_iterator *iter = NULL;
	git_oid note_id, annotated_id;
624

Linquize committed
625 626
	if ((error = git_note_iterator_new(&iter, repo, notes_ref)) < 0)
		return error;
627

628 629
	while (!(error = git_note_next(&note_id, &annotated_id, iter))) {
		if ((error = note_cb(&note_id, &annotated_id, payload)) != 0) {
630
			giterr_set_after_callback(error);
631 632 633
			break;
		}
	}
634

Linquize committed
635 636
	if (error == GIT_ITEROVER)
		error = 0;
637

Linquize committed
638 639
	git_note_iterator_free(iter);
	return error;
640
}
641

642

643 644 645 646 647 648 649 650 651
void git_note_iterator_free(git_note_iterator *it)
{
	if (it == NULL)
		return;

	git_iterator_free(it);
}


652
int git_note_iterator_new(
653
	git_note_iterator **it,
654
	git_repository *repo,
655
	const char *notes_ref_in)
656 657 658 659
{
	int error;
	git_commit *commit = NULL;
	git_tree *tree = NULL;
660
	char *notes_ref;
661

662
	error = retrieve_note_tree_and_commit(&tree, &commit, &notes_ref, repo, notes_ref_in);
663 664
	if (error < 0)
		goto cleanup;
665

666
	if ((error = git_iterator_for_tree(it, tree, NULL)) < 0)
667
		git_iterator_free(*it);
668

669
cleanup:
670
	git__free(notes_ref);
671 672 673 674 675 676 677 678 679
	git_tree_free(tree);
	git_commit_free(commit);

	return error;
}

int git_note_next(
	git_oid* note_id,
	git_oid* annotated_id,
680
	git_note_iterator *it)
681 682 683 684
{
	int error;
	const git_index_entry *item;

685
	if ((error = git_iterator_current(&item, it)) < 0)
686
		return error;
687

688
	git_oid_cpy(note_id, &item->id);
689

690 691
	if (!(error = process_entry_path(item->path, annotated_id)))
		git_iterator_advance(NULL, it);
692 693 694

	return error;
}