Commit 6b2a19418cb08e7bccefc2362f290a67555594a0

Vicent Marti 2011-03-12T23:09:16

Fix the retarded object interdependency system It's no longer retarded. All object interdependencies are stored as OIDs instead of actual objects. This should be hundreds of times faster, specially on big repositories. Heck, who knows, maye it doesn't even segfault -- wouldn't that be awesome? What has changed on the API? `git_commit_parent`, `git_commit_tree`, `git_tag_target` now return their values through a pointer-to-pointer, and have an error code. `git_commit_set_tree` and `git_tag_set_target` now return an error code and may fail. `git_repository_free__no_gc` has been deprecated because it's stupid. Since there are no longer any interdependencies between objects, we don't need internal reference counting, and GC never fails or double-free's pointers. `git_object_close` now does a very sane thing: marks an object as unused. Closed objects will be eventually free'd from the object cache based on LRU. Please use `git_object_close` from the garbage collector `destroy` method on your bindings. It's 100% safe. `git_repository_gc` is a new method that forces a garbage collector pass through the repo, to free as many LRU objects as possible. This is useful if we are running out of memory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
diff --git a/include/git2/commit.h b/include/git2/commit.h
index 21836db..1556e52 100644
--- a/include/git2/commit.h
+++ b/include/git2/commit.h
@@ -122,10 +122,11 @@ GIT_EXTERN(const git_signature *) git_commit_author(git_commit *commit);
 
 /**
  * Get the tree pointed to by a commit.
+ * @param tree_out pointer where to store the tree object
  * @param commit a previously loaded commit.
- * @return the tree of a commit
+ * @return 0 on success; error code otherwise
  */
-GIT_EXTERN(const git_tree *) git_commit_tree(git_commit *commit);
+GIT_EXTERN(int) git_commit_tree(git_tree **tree_out, git_commit *commit);
 
 /**
  * Get the number of parents of this commit
@@ -137,11 +138,13 @@ GIT_EXTERN(unsigned int) git_commit_parentcount(git_commit *commit);
 
 /**
  * Get the specified parent of the commit.
+ *
+ * @param parent Pointer where to store the parent commit
  * @param commit a previously loaded commit.
- * @param n the position of the entry
- * @return a pointer to the commit; NULL if out of bounds
+ * @param n the position of the parent (from 0 to `parentcount`)
+ * @return 0 on success; error code otherwise
  */
-GIT_EXTERN(git_commit *) git_commit_parent(git_commit *commit, unsigned int n);
+GIT_EXTERN(int) git_commit_parent(git_commit **parent, git_commit *commit, unsigned int n);
 
 /**
  * Add a new parent commit to an existing commit
@@ -176,8 +179,9 @@ GIT_EXTERN(void) git_commit_set_author(git_commit *commit, const git_signature *
  * Set the tree which is pointed to by a commit
  * @param commit the commit object
  * @param tree the new tree
+ * @param 0 on success; error code otherwise
  */
-GIT_EXTERN(void) git_commit_set_tree(git_commit *commit, git_tree *tree);
+GIT_EXTERN(int) git_commit_set_tree(git_commit *commit, git_tree *tree);
 
 /** @} */
 GIT_END_DECL
diff --git a/include/git2/object.h b/include/git2/object.h
index af0f014..748386f 100644
--- a/include/git2/object.h
+++ b/include/git2/object.h
@@ -131,10 +131,10 @@ GIT_EXTERN(git_repository *) git_object_owner(const git_object *obj);
  * Close an open object
  *
  * This method instructs the library to close an existing
- * object; note that git_objects are owned by the repository
- * and are reference counted, so the object may or may not be
- * freed after this library call, depending on whether any other 
- * objects still depend on it.
+ * object; note that git_objects are owned and cached by the repository
+ * so the object may or may not be freed after this library call,
+ * depending on how agressive is the caching mechanism used
+ * by the repository.
  *
  * IMPORTANT:
  * It is *not* necessary to call this method when you stop using
diff --git a/include/git2/repository.h b/include/git2/repository.h
index 5327f8c..d5f6cf5 100644
--- a/include/git2/repository.h
+++ b/include/git2/repository.h
@@ -154,12 +154,20 @@ GIT_EXTERN(int) git_repository_index(git_index **index, git_repository *repo);
 
 /**
  * Free a previously allocated repository
+ *
  * @param repo repository handle to close. If NULL nothing occurs.
  */
 GIT_EXTERN(void) git_repository_free(git_repository *repo);
 
-
-GIT_EXTERN(void) git_repository_free__no_gc(git_repository *repo);
+/**
+ * Force a garbage collector pass on the repository
+ *
+ * This will force-free any cached objects that have been
+ * previously marked by the user as closed (`git_object_close`).
+ *
+ * @param repo repository handle to collect. If NULL nothing occurs.
+ */
+GIT_EXTERN(void) git_repository_close(git_repository *repo);
 
 /**
  * Creates a new Git repository in the given folder.
diff --git a/include/git2/tag.h b/include/git2/tag.h
index 2ca25c8..f1669eb 100644
--- a/include/git2/tag.h
+++ b/include/git2/tag.h
@@ -79,10 +79,18 @@ GIT_EXTERN(const git_oid *) git_tag_id(git_tag *tag);
 
 /**
  * Get the tagged object of a tag
+ * @param target pointer where to store the target
  * @param tag a previously loaded tag.
- * @return reference to a repository object
+ * @return 0 on success; error code otherwise
+ */
+GIT_EXTERN(int) git_tag_target(git_object **target, git_tag *t);
+
+/**
+ * Get the OID of the tagged object of a tag
+ * @param tag a previously loaded tag.
+ * @return pointer to the OID
  */
-GIT_EXTERN(const git_object *) git_tag_target(git_tag *t);
+GIT_EXTERN(const git_oid *) git_tag_target_oid(git_tag *t);
 
 /**
  * Get the type of a tag's tagged object
@@ -117,7 +125,7 @@ GIT_EXTERN(const char *) git_tag_message(git_tag *t);
  * @param tag The tag to modify
  * @param target the new tagged target
  */
-GIT_EXTERN(void) git_tag_set_target(git_tag *tag, git_object *target);
+GIT_EXTERN(int) git_tag_set_target(git_tag *tag, git_object *target);
 
 /**
  * Set the name of a tag
@@ -130,6 +138,7 @@ GIT_EXTERN(void) git_tag_set_name(git_tag *tag, const char *name);
  * Set the tagger of a tag
  * @param tag The tag to modify
  * @param tagger_sig signature of the tagging action
+ * @return 0 on success; error code otherwise
  */
 GIT_EXTERN(void) git_tag_set_tagger(git_tag *tag, const git_signature *tagger_sig);
 
diff --git a/src/blob.c b/src/blob.c
index f157f47..1e03b6b 100644
--- a/src/blob.c
+++ b/src/blob.c
@@ -130,9 +130,7 @@ int git_blob_writefile(git_oid *written_id, git_repository *repo, const char *pa
 
 	git_oid_cpy(written_id, git_object_id((git_object *)blob));
 
-	/* FIXME: maybe we don't want to free this already?
-	 * the user may want to access it again */
-	GIT_OBJECT_DECREF(repo, blob);
+	git_object_close((git_object*)blob);
 	return GIT_SUCCESS;
 }
 
diff --git a/src/commit.c b/src/commit.c
index 974999a..1c5cddf 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -46,24 +46,22 @@ static void clear_parents(git_commit *commit)
 {
 	unsigned int i;
 
-	for (i = 0; i < commit->parents.length; ++i) {
-		git_commit *parent = git_vector_get(&commit->parents, i);
-		GIT_OBJECT_DECREF(commit->object.repo, parent);
+	for (i = 0; i < commit->parent_oids.length; ++i) {
+		git_oid *parent = git_vector_get(&commit->parent_oids, i);
+		free(parent);
 	}
 
-	git_vector_clear(&commit->parents);
+	git_vector_clear(&commit->parent_oids);
 }
 
 void git_commit__free(git_commit *commit)
 {
 	clear_parents(commit);
-	git_vector_free(&commit->parents);
+	git_vector_free(&commit->parent_oids);
 
 	git_signature_free(commit->author);
 	git_signature_free(commit->committer);
 
-	GIT_OBJECT_DECREF(commit->object.repo, commit->tree);
-
 	free(commit->message);
 	free(commit->message_short);
 	free(commit);
@@ -78,16 +76,13 @@ int git_commit__writeback(git_commit *commit, git_odb_source *src)
 {
 	unsigned int i;
 
-	if (commit->tree == NULL)
-		return GIT_EMISSINGOBJDATA;
-
-	git__write_oid(src, "tree", git_tree_id(commit->tree));
+	git__write_oid(src, "tree", &commit->tree_oid);
 
-	for (i = 0; i < commit->parents.length; ++i) {
-		git_commit *parent;
+	for (i = 0; i < commit->parent_oids.length; ++i) {
+		git_oid *parent_oid;
 
-		parent = git_vector_get(&commit->parents, i);
-		git__write_oid(src, "parent", git_commit_id(parent));
+		parent_oid = git_vector_get(&commit->parent_oids, i);
+		git__write_oid(src, "parent", parent_oid);
 	}
 
 	if (commit->author == NULL)
@@ -103,66 +98,49 @@ int git_commit__writeback(git_commit *commit, git_odb_source *src)
 	if (commit->message != NULL) {
 		git__source_write(src, "\n", 1);
 		git__source_write(src, commit->message, strlen(commit->message));
-        }
-
-	/* Mark the commit as having all attributes */
-	commit->full_parse = 1;
+	}
 
 	return GIT_SUCCESS;
 }
 
-int commit_parse_buffer(git_commit *commit, void *data, size_t len, unsigned int parse_flags)
+int commit_parse_buffer(git_commit *commit, void *data, size_t len)
 {
 	char *buffer = (char *)data;
 	const char *buffer_end = (char *)data + len;
 
-	git_oid oid;
+	git_oid parent_oid;
 	int error;
 
 	/* first parse; the vector hasn't been initialized yet */
-	if (commit->parents.contents == NULL) {
-		git_vector_init(&commit->parents, 4, NULL);
+	if (commit->parent_oids.contents == NULL) {
+		git_vector_init(&commit->parent_oids, 4, NULL);
 	}
 
 	clear_parents(commit);
 
-
-	if ((error = git__parse_oid(&oid, &buffer, buffer_end, "tree ")) < GIT_SUCCESS)
-		return error;
-
-	GIT_OBJECT_DECREF(commit->object.repo, commit->tree);
-	if ((error = git_object_lookup((git_object **)&commit->tree, commit->object.repo, &oid, GIT_OBJ_TREE)) < GIT_SUCCESS)
+	if ((error = git__parse_oid(&commit->tree_oid, &buffer, buffer_end, "tree ")) < GIT_SUCCESS)
 		return error;
 
 	/*
 	 * TODO: commit grafts!
 	 */
 
-	while (git__parse_oid(&oid, &buffer, buffer_end, "parent ") == GIT_SUCCESS) {
-		git_commit *parent;
+	while (git__parse_oid(&parent_oid, &buffer, buffer_end, "parent ") == GIT_SUCCESS) {
+		git_oid *new_oid;
 
-		if ((error = git_object_lookup((git_object **)&parent, commit->object.repo, &oid, GIT_OBJ_COMMIT)) < GIT_SUCCESS)
-			return error;
+		new_oid = git__malloc(sizeof(git_oid));
+		git_oid_cpy(new_oid, &parent_oid);
 
-		if (git_vector_insert(&commit->parents, parent) < GIT_SUCCESS)
+		if (git_vector_insert(&commit->parent_oids, new_oid) < GIT_SUCCESS)
 			return GIT_ENOMEM;
 	}
 
+	if (commit->author)
+		git_signature_free(commit->author);
 
-	if (parse_flags & COMMIT_FULL_PARSE) {
-		if (commit->author)
-			git_signature_free(commit->author);
-
-		commit->author = git__malloc(sizeof(git_signature));
-		if ((error = git_signature__parse(commit->author, &buffer, buffer_end, "author ")) < GIT_SUCCESS)
-			return error;
-
-	} else {
-		if ((buffer = memchr(buffer, '\n', buffer_end - buffer)) == NULL)
-			return GIT_EOBJCORRUPTED;
-
-		buffer++;
-	}
+	commit->author = git__malloc(sizeof(git_signature));
+	if ((error = git_signature__parse(commit->author, &buffer, buffer_end, "author ")) < GIT_SUCCESS)
+		return error;
 
 	/* Always parse the committer; we need the commit time */
 	if (commit->committer)
@@ -176,7 +154,7 @@ int commit_parse_buffer(git_commit *commit, void *data, size_t len, unsigned int
 	while (buffer <= buffer_end && *buffer == '\n')
 		buffer++;
 
-	if (parse_flags & COMMIT_FULL_PARSE && buffer < buffer_end) {
+	if (buffer < buffer_end) {
 		const char *line_end;
 		size_t message_len = buffer_end - buffer;
 
@@ -203,106 +181,81 @@ int git_commit__parse(git_commit *commit)
 {
 	assert(commit && commit->object.source.open);
 	return commit_parse_buffer(commit,
-			commit->object.source.raw.data, commit->object.source.raw.len, COMMIT_BASIC_PARSE);
+			commit->object.source.raw.data, commit->object.source.raw.len);
 }
 
-int git_commit__parse_full(git_commit *commit)
-{
-	int error;
-
-	if (commit->full_parse)
-		return GIT_SUCCESS;
-
-	if ((error = git_object__source_open((git_object *)commit)) < GIT_SUCCESS)
-		return error;
-
-	error = commit_parse_buffer(commit,
-			commit->object.source.raw.data, commit->object.source.raw.len, COMMIT_FULL_PARSE);
-
-	git_object__source_close((git_object *)commit);
-
-	commit->full_parse = 1;
-	return error;
-}
-
-
-
-#define GIT_COMMIT_GETTER(_rvalue, _name) \
-	const _rvalue git_commit_##_name(git_commit *commit) \
+#define GIT_COMMIT_GETTER(_rvalue, _name, _return) \
+	_rvalue git_commit_##_name(git_commit *commit) \
 	{\
 		assert(commit); \
-		if (commit->_name) \
-			return commit->_name; \
-		if (!commit->object.in_memory) \
-			git_commit__parse_full(commit); \
-		return commit->_name; \
+		return _return; \
 	}
 
-#define CHECK_FULL_PARSE() \
-	if (!commit->object.in_memory && !commit->full_parse)\
-		git_commit__parse_full(commit); 
+GIT_COMMIT_GETTER(const git_signature *, author, commit->author)
+GIT_COMMIT_GETTER(const git_signature *, committer, commit->committer)
+GIT_COMMIT_GETTER(const char *, message, commit->message)
+GIT_COMMIT_GETTER(const char *, message_short, commit->message_short)
+GIT_COMMIT_GETTER(time_t, time, commit->committer->when.time)
+GIT_COMMIT_GETTER(int, time_offset, commit->committer->when.offset)
+GIT_COMMIT_GETTER(unsigned int, parentcount, commit->parent_oids.length)
+
+
+int git_commit_tree(git_tree **tree_out, git_commit *commit)
+{
+	assert(commit);
+	return git_tree_lookup(tree_out, commit->object.repo, &commit->tree_oid);
+}
 
-const git_tree *git_commit_tree(git_commit *commit)
+int git_commit_parent(git_commit **parent, git_commit *commit, unsigned int n)
 {
+	git_oid *parent_oid;
 	assert(commit);
 
-	if (!commit->object.in_memory && commit->tree == NULL)
-		git_commit__parse_full(commit);
+	parent_oid = git_vector_get(&commit->parent_oids, n);
+	if (parent_oid == NULL)
+		return GIT_ENOTFOUND;
 
-	GIT_OBJECT_INCREF(commit->object.repo, commit->tree);
-	return commit->tree;
+	return git_commit_lookup(parent, commit->object.repo, parent_oid);
 }
 
-GIT_COMMIT_GETTER(git_signature *, author)
-GIT_COMMIT_GETTER(git_signature *, committer)
-GIT_COMMIT_GETTER(char *, message)
-GIT_COMMIT_GETTER(char *, message_short)
 
-time_t git_commit_time(git_commit *commit)
-{
-	assert(commit && commit->committer);
-	return commit->committer->when.time;
-}
 
-int git_commit_time_offset(git_commit *commit)
+int git_commit_set_tree(git_commit *commit, git_tree *tree)
 {
-	assert(commit && commit->committer);
-	return commit->committer->when.offset;
-}
+	const git_oid *oid;
 
-unsigned int git_commit_parentcount(git_commit *commit)
-{
-	assert(commit);
-	return commit->parents.length;
+	assert(commit && tree);
+
+	if ((oid = git_object_id((git_object *)tree)) == NULL)
+		return GIT_EMISSINGOBJDATA;
+
+	commit->object.modified = 1;
+	git_oid_cpy(&commit->tree_oid, oid);
+	return GIT_SUCCESS;
 }
 
-git_commit *git_commit_parent(git_commit *commit, unsigned int n)
+int git_commit_add_parent(git_commit *commit, git_commit *new_parent)
 {
-	git_commit *parent;
+	const git_oid *parent_oid;
+	git_oid *new_oid;
+	assert(commit && new_parent);
 
-	assert(commit);
+	if ((parent_oid = git_object_id((git_object *)new_parent)) == NULL)
+		return GIT_EMISSINGOBJDATA;
 
-	parent = git_vector_get(&commit->parents, n);
-	GIT_OBJECT_INCREF(commit->object.repo, parent);
-	return parent;
-}
+	new_oid = git__malloc(sizeof(git_oid));
+	if (new_oid == NULL)
+		return GIT_ENOMEM;
 
-void git_commit_set_tree(git_commit *commit, git_tree *tree)
-{
-	assert(commit && tree);
 	commit->object.modified = 1;
-	CHECK_FULL_PARSE();
-
-	GIT_OBJECT_DECREF(commit->object.repo, commit->tree);
-	GIT_OBJECT_INCREF(commit->object.repo, tree);
-	commit->tree = tree;
+	git_oid_cpy(new_oid, parent_oid);
+	return git_vector_insert(&commit->parent_oids, new_oid);
 }
 
 void git_commit_set_author(git_commit *commit, const git_signature *author_sig)
 {
 	assert(commit && author_sig);
 	commit->object.modified = 1;
-	CHECK_FULL_PARSE();
 
 	git_signature_free(commit->author);
 	commit->author = git_signature_dup(author_sig);
@@ -312,7 +265,6 @@ void git_commit_set_committer(git_commit *commit, const git_signature *committer
 {
 	assert(commit && committer_sig);
 	commit->object.modified = 1;
-	CHECK_FULL_PARSE();
 
 	git_signature_free(commit->committer);
 	commit->committer = git_signature_dup(committer_sig);
@@ -324,7 +276,6 @@ void git_commit_set_message(git_commit *commit, const char *message)
 	size_t message_len;
 
 	commit->object.modified = 1;
-	CHECK_FULL_PARSE();
 
 	if (commit->message)
 		free(commit->message);
@@ -347,12 +298,3 @@ void git_commit_set_message(git_commit *commit, const char *message)
 	commit->message_short[message_len] = 0;
 }
 
-int git_commit_add_parent(git_commit *commit, git_commit *new_parent)
-{
-	assert(commit && new_parent);
-
-	CHECK_FULL_PARSE();
-	commit->object.modified = 1;
-	GIT_OBJECT_INCREF(commit->object.repo, new_parent);
-	return git_vector_insert(&commit->parents, new_parent);
-}
diff --git a/src/commit.h b/src/commit.h
index b53ee9b..aaf349c 100644
--- a/src/commit.h
+++ b/src/commit.h
@@ -11,21 +11,18 @@
 struct git_commit {
 	git_object object;
 
-	git_vector parents;
+	git_vector parent_oids;
+	git_oid tree_oid;
 
-	git_tree *tree;
 	git_signature *author;
 	git_signature *committer;
 
 	char *message;
 	char *message_short;
-
-	unsigned full_parse:1;
 };
 
 void git_commit__free(git_commit *c);
 int git_commit__parse(git_commit *commit);
-int git_commit__parse_full(git_commit *commit);
 
 int git_commit__writeback(git_commit *commit, git_odb_source *src);
 
diff --git a/src/object.c b/src/object.c
index 7891893..fce9915 100644
--- a/src/object.c
+++ b/src/object.c
@@ -261,7 +261,7 @@ int git_object_new(git_object **object_out, git_repository *repo, git_otype type
 
 	object->source.raw.type = type;
 
-	object->refcount++;
+	object->lru = ++repo->lru_counter;
 	*object_out = object;
 	return GIT_SUCCESS;
 }
@@ -277,7 +277,8 @@ int git_object_lookup(git_object **object_out, git_repository *repo, const git_o
 	object = git_hashtable_lookup(repo->objects, id);
 	if (object != NULL) {
 		*object_out = object;
-		GIT_OBJECT_INCREF(repo, object);
+		object->lru = ++repo->lru_counter;
+		object->can_free = 0;
 		return GIT_SUCCESS;
 	}
 
@@ -330,7 +331,7 @@ int git_object_lookup(git_object **object_out, git_repository *repo, const git_o
 	git_object__source_close(object);
 	git_hashtable_insert(repo->objects, &object->id, object);
 
-	GIT_OBJECT_INCREF(repo, object);
+	object->lru = ++repo->lru_counter;
 	*object_out = object;
 	return GIT_SUCCESS;
 }
@@ -412,17 +413,12 @@ void git_object_close(git_object *object)
 	if (object == NULL)
 		return;
 
-	if (--object->refcount <= 0) {
-		if (object->repo != NULL) {
-			if (object->in_memory) {
-				int idx = git_vector_search(&object->repo->memory_objects, object);
-				git_vector_remove(&object->repo->memory_objects, idx);
-			} else {
-				git_hashtable_remove(object->repo->objects, &object->id);
-			}
-		}
-
+	if (object->in_memory) {
+		int idx = git_vector_search(&object->repo->memory_objects, object);
+		git_vector_remove(&object->repo->memory_objects, idx);
 		git_object__free(object);
+	} else {
+		object->can_free = 1;
 	}
 }
 
diff --git a/src/refs.c b/src/refs.c
index 93897a7..1ed6b56 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -681,7 +681,6 @@ static int packed_write_ref(reference_oid *ref, git_filebuf *file)
 static int packed_find_peel(reference_oid *ref)
 {
 	git_tag *tag;
-	const git_object *peeled_target;
 	int error;
 
 	if (ref->ref.type & GIT_REF_HAS_PEEL)
@@ -706,11 +705,7 @@ static int packed_find_peel(reference_oid *ref)
 	/*
 	 * Find the object pointed at by this tag
 	 */
-	peeled_target = git_tag_target(tag);
-	if (peeled_target == NULL)
-		return GIT_EOBJCORRUPTED;
-
-	git_oid_cpy(&ref->peel_target, git_object_id(peeled_target));
+	git_oid_cpy(&ref->peel_target, git_tag_target_oid(tag));
 	ref->ref.type |= GIT_REF_HAS_PEEL;
 
 	/* 
diff --git a/src/repository.c b/src/repository.c
index c245606..37aa447 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -209,7 +209,6 @@ static git_repository *repository_alloc()
 		return NULL;
 	}
 
-	repo->gc_enabled = 1;
 	return repo;
 }
 
@@ -331,50 +330,20 @@ cleanup:
 	return error;
 }
 
-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)
+int git_repository_gc(git_repository *repo)
 {
+	int collected = 0;
 	git_object *object;
 	const void *_unused;
-	unsigned int i;
-
-	if (repo == NULL)
-		return;
 
 	GIT_HASHTABLE_FOREACH(repo->objects, _unused, object,
-		object->repo = NULL;
-		object->refcount = 0;
+		if (object->can_free) {
+			git_object__free(object);
+			collected++;
+		}
 	);
 
-	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);
+	return collected;
 }
 
 void git_repository_free(git_repository *repo)
@@ -386,8 +355,6 @@ void git_repository_free(git_repository *repo)
 	if (repo == NULL)
 		return;
 
-	repo->gc_enabled = 0;
-
 	/* force free all the objects */
 	GIT_HASHTABLE_FOREACH(repo->objects, _unused, object,
 		git_object__free(object);
@@ -398,7 +365,23 @@ void git_repository_free(git_repository *repo)
 		git_object__free(object);
 	}
 
-	repository_free(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);
 }
 
 int git_repository_index(git_index **index_out, git_repository *repo)
diff --git a/src/repository.h b/src/repository.h
index 5318ed4..48b8dae 100644
--- a/src/repository.h
+++ b/src/repository.h
@@ -27,8 +27,8 @@ struct git_object {
 	git_oid id;
 	git_repository *repo;
 	git_odb_source source;
-	unsigned short refcount;
-	unsigned char in_memory, modified;
+	unsigned int lru;
+	unsigned char in_memory, modified, can_free, _pad;
 };
 
 struct git_repository {
@@ -45,7 +45,8 @@ struct git_repository {
 	char *path_odb;
 	char *path_workdir;
 
-	unsigned is_bare:1, gc_enabled:1;
+	unsigned is_bare:1;
+	unsigned int lru_counter;
 };
 
 int git_object__source_open(git_object *object);
@@ -61,19 +62,4 @@ int git__source_write(git_odb_source *source, const void *bytes, size_t len);
 int git__parse_oid(git_oid *oid, char **buffer_out, const char *buffer_end, const char *header);
 int git__write_oid(git_odb_source *src, const char *header, const git_oid *oid);
 
-#define GIT_OBJECT_INCREF(repo, ob) git_object__incref((repo), (git_object *)(ob))
-#define GIT_OBJECT_DECREF(repo, ob) git_object__decref((repo), (git_object *)(ob)) 
-
-GIT_INLINE(void) git_object__incref(git_repository *repo, struct git_object *object)
-{
-	if (repo && repo->gc_enabled && object)
-		object->refcount++;
-}
-
-GIT_INLINE(void) git_object__decref(git_repository *repo, struct git_object *object)
-{
-	if (repo && repo->gc_enabled && object)
-		git_object_close(object);
-}
-
 #endif
diff --git a/src/tag.c b/src/tag.c
index 1ac2067..1379425 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -35,7 +35,6 @@
 void git_tag__free(git_tag *tag)
 {
 	git_signature_free(tag->tagger);
-	GIT_OBJECT_DECREF(tag->object.repo, tag->target);
 	free(tag->message);
 	free(tag->tag_name);
 	free(tag);
@@ -46,23 +45,31 @@ const git_oid *git_tag_id(git_tag *c)
 	return git_object_id((git_object *)c);
 }
 
-const git_object *git_tag_target(git_tag *t)
+int git_tag_target(git_object **target, git_tag *t)
 {
 	assert(t);
-	GIT_OBJECT_INCREF(t->object.repo, t->target);
-	return t->target;
+	return git_object_lookup(target, t->object.repo, &t->target, t->type);
 }
 
-void git_tag_set_target(git_tag *tag, git_object *target)
+const git_oid *git_tag_target_oid(git_tag *t)
 {
+	assert(t);
+	return &t->target;
+}
+
+int git_tag_set_target(git_tag *tag, git_object *target)
+{
+	const git_oid *oid;
+
 	assert(tag && target);
 
-	GIT_OBJECT_DECREF(tag->object.repo, tag->target);
-	GIT_OBJECT_INCREF(tag->object.repo, target);
+	if ((oid = git_object_id(target)) == NULL)
+		return GIT_EMISSINGOBJDATA;
 
 	tag->object.modified = 1;
-	tag->target = target;
+	git_oid_cpy(&tag->target, oid);
 	tag->type = git_object_type(target);
+	return GIT_SUCCESS;
 }
 
 git_otype git_tag_type(git_tag *t)
@@ -81,7 +88,6 @@ void git_tag_set_name(git_tag *tag, const char *name)
 {
 	assert(tag && name);
 
-	/* TODO: sanity check? no newlines in message */
 	tag->object.modified = 1;
 
 	if (tag->tag_name)
@@ -128,12 +134,11 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
 		NULL, "commit\n", "tree\n", "blob\n", "tag\n"
 	};
 
-	git_oid target_oid;
 	unsigned int i, text_len;
 	char *search;
 	int error;
 
-	if ((error = git__parse_oid(&target_oid, &buffer, buffer_end, "object ")) < 0)
+	if ((error = git__parse_oid(&tag->target, &buffer, buffer_end, "object ")) < 0)
 		return error;
 
 	if (buffer + 5 >= buffer_end)
@@ -161,11 +166,6 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
 	if (tag->type == GIT_OBJ_BAD)
 		return GIT_EOBJCORRUPTED;
 
-	git_object_close(tag->target);
-	error = git_object_lookup(&tag->target, tag->object.repo, &target_oid, tag->type);
-	if (error < 0)
-		return error;
-
 	if (buffer + 4 >= buffer_end)
 		return GIT_EOBJCORRUPTED;
 
@@ -210,10 +210,10 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
 
 int git_tag__writeback(git_tag *tag, git_odb_source *src)
 {
-	if (tag->target == NULL || tag->tag_name == NULL || tag->tagger == NULL)
+	if (tag->tag_name == NULL || tag->tagger == NULL)
 		return GIT_EMISSINGOBJDATA;
 
-	git__write_oid(src, "object", git_object_id(tag->target));
+	git__write_oid(src, "object", &tag->target);
 	git__source_printf(src, "type %s\n", git_object_type2string(tag->type));
 	git__source_printf(src, "tag %s\n", tag->tag_name);
 	git_signature__write(src, "tagger", tag->tagger);
diff --git a/src/tag.h b/src/tag.h
index 624fcc6..a1782d0 100644
--- a/src/tag.h
+++ b/src/tag.h
@@ -7,8 +7,9 @@
 struct git_tag {
 	git_object object;
 
-	git_object *target;
+	git_oid target;
 	git_otype type;
+
 	char *tag_name;
 	git_signature *tagger;
 	char *message;
diff --git a/tests/t04-commit.c b/tests/t04-commit.c
index 8e62759..855cf98 100644
--- a/tests/t04-commit.c
+++ b/tests/t04-commit.c
@@ -390,11 +390,11 @@ BEGIN_TEST(details0, "query the details on a parsed commit")
 		must_be_true(commit_time > 0);
 		must_be_true(parents <= 2);
 		for (p = 0;p < parents;p++) {
-			parent = git_commit_parent(commit, p);
+			must_pass(git_commit_parent(&parent, commit, p));
 			must_be_true(parent != NULL);
 			must_be_true(git_commit_author(parent) != NULL); // is it really a commit?
 		}
-		must_be_true(git_commit_parent(commit, parents) == NULL);
+		must_fail(git_commit_parent(&parent, commit, parents));
 	}
 
 	git_repository_free(repo);
diff --git a/tests/t08-tag.c b/tests/t08-tag.c
index 0afdf71..c678926 100644
--- a/tests/t08-tag.c
+++ b/tests/t08-tag.c
@@ -48,12 +48,12 @@ BEGIN_TEST(read0, "read and parse a tag from the repository")
 	must_be_true(strcmp(git_tag_name(tag1), "test") == 0);
 	must_be_true(git_tag_type(tag1) == GIT_OBJ_TAG);
 
-	tag2 = (git_tag *)git_tag_target(tag1);
+	must_pass(git_tag_target((git_object **)&tag2, tag1));
 	must_be_true(tag2 != NULL);
 
 	must_be_true(git_oid_cmp(&id2, git_tag_id(tag2)) == 0);
 
-	commit = (git_commit *)git_tag_target(tag2);
+	must_pass(git_tag_target((git_object **)&commit, tag2));
 	must_be_true(commit != NULL);
 
 	must_be_true(git_oid_cmp(&id_commit, git_commit_id(commit)) == 0);