notes.c 12.8 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"
Ben Straub committed
14
#include "signature.h"
schu committed
15

16 17 18 19 20 21
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
22
{
23
	size_t i;
schu committed
24 25
	const git_tree_entry *entry;

26
	*out = NULL;
27

28 29
	if (parent == NULL)
		return GIT_ENOTFOUND;
schu committed
30

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

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

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

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

47 48
	return GIT_ENOTFOUND;
}
schu committed
49

50 51 52 53 54
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
55

56
	*out = NULL;
schu committed
57

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

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

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

	return error;
schu committed
71 72 73 74
}

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

91 92 93 94 95 96 97
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
98
{
99 100
	int error;
	git_treebuilder *tb = NULL;
101
	const git_tree_entry *entry;
102
	git_oid tree_oid;
schu committed
103

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

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

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

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

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

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
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))
149 150
{
	int error = -1;	
151
	git_tree *subtree = NULL, *new = NULL;
152
	char subtree_name[3];
schu committed
153

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

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

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

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

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

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

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

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

186

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

193 194 195 196 197 198 199 200 201
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)
{
202 203
	GIT_UNUSED(note_oid);
	GIT_UNUSED(current_error);
schu committed
204

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

208 209 210 211 212 213 214 215 216
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)
{
217 218 219 220 221
	GIT_UNUSED(out);
	GIT_UNUSED(repo);
	GIT_UNUSED(parent);
	GIT_UNUSED(note_oid);
	GIT_UNUSED(fanout);
schu committed
222

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

227 228 229 230 231 232 233 234
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)
{
235 236 237 238 239
	GIT_UNUSED(out);
	GIT_UNUSED(repo);
	GIT_UNUSED(parent);
	GIT_UNUSED(note_oid);
	GIT_UNUSED(fanout);
schu committed
240

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

245 246 247 248 249 250 251 252
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)
{
253
	GIT_UNUSED(current_error);
schu committed
254

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

265 266
static int note_write(git_oid *out,
	git_repository *repo,
Ben Straub committed
267 268
	const git_signature *author,
	const git_signature *committer,
269 270 271 272 273
	const char *notes_ref,
	const char *note,
	git_tree *commit_tree,
	const char *target,
	git_commit **parents)
schu committed
274
{
275
	int error;
schu committed
276
	git_oid oid;
277 278 279 280 281 282
	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
283

284 285 286
	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)
287
		goto cleanup;
schu committed
288

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

292 293 294
	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);
295

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

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

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

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

	*out = note;

314
	return 0;
schu committed
315 316
}

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

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

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

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

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

338
	*out = note;
schu committed
339

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

346
static int note_remove(git_repository *repo,
Ben Straub committed
347 348 349
		const git_signature *author, const git_signature *committer,
		const char *notes_ref, git_tree *tree,
		const char *target, git_commit **parents)
350 351 352 353 354
{
	int error;
	git_tree *tree_after_removal = NULL;
	git_oid oid;
		
355 356 357
	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)
358
		goto cleanup;
schu committed
359 360

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

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

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

	*out = NULL;

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

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

387
	return ret;
388 389
}

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

	return note_get_default_ref(notes_ref, repo);
}

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

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

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

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

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

419
	return 0;
420 421
}

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

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

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

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

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

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

	target = git_oid_allocfmt(oid);
460
	GITERR_CHECK_ALLOC(target);
schu committed
461

462 463 464 465 466
	error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref);

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

schu committed
467
	error = note_write(out, repo, author, committer, notes_ref,
Ben Straub committed
468
			note, tree, target, &commit);
schu committed
469

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

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

	target = git_oid_allocfmt(oid);
487
	GITERR_CHECK_ALLOC(target);
schu committed
488

489 490 491
	if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref)) < 0)
		goto cleanup;

schu committed
492
	error = note_remove(repo, author, committer, notes_ref,
493
			    tree, target, &commit);
schu committed
494

495
cleanup:
schu committed
496 497
	git__free(target);
	git_commit_free(commit);
498
	git_tree_free(tree);
499
	return error;
schu committed
500 501
}

502 503 504 505 506 507
int git_note_default_ref(const char **out, git_repository *repo)
{
	assert(repo);
	return note_get_default_ref(out, repo);
}

Ben Straub committed
508
const char * git_note_message(const git_note *note)
schu committed
509 510 511 512 513
{
	assert(note);
	return note->message;
}

Ben Straub committed
514
const git_oid * git_note_oid(const git_note *note)
schu committed
515 516 517 518 519 520 521 522 523 524 525 526 527
{
	assert(note);
	return &note->oid;
}

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

	git__free(note->message);
	git__free(note);
}
528 529 530

static int process_entry_path(
	const char* entry_path,
531
	const git_oid *note_oid,
Ben Straub committed
532
	git_note_foreach_cb note_cb,
533 534
	void *payload)
{
535 536
	int error = -1;
	size_t i = 0, j = 0, len;
537
	git_buf buf = GIT_BUF_INIT;
538
	git_oid annotated_object_id;
539

540
	if ((error = git_buf_puts(&buf, entry_path)) < 0)
541
		goto cleanup;
542

543 544 545 546 547 548 549
	len = git_buf_len(&buf);

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

551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
		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;
	}

571
	if ((error = git_oid_fromstr(&annotated_object_id, buf.ptr)) < 0)
572
		goto cleanup;
573

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

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

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

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

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

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

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

610 611
	return error;
}