Commit f4a881ce8b200e5283247799c39abd2b578b8e75

Stefan Sperling 2018-11-17T16:04:05

more support for tag objects; new code is not yet reachable

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
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
diff --git a/include/got_object.h b/include/got_object.h
index 6ba2563..c24e7e0 100644
--- a/include/got_object.h
+++ b/include/got_object.h
@@ -18,6 +18,7 @@ struct got_object_id;
 
 struct got_blob_object;
 struct got_tree_object;
+struct got_tag_object;
 
 struct got_tree_entry {
 	SIMPLEQ_ENTRY(got_tree_entry) entry;
@@ -224,6 +225,17 @@ const struct got_error *got_object_blob_read_block(size_t *,
 const struct got_error *got_object_blob_dump_to_file(size_t *, size_t *,
     FILE *, struct got_blob_object *);
 
+/*
+ * Attempt to open a tag object in a repository.
+ * The provided object must be of type GOT_OBJ_TYPE_TAG.
+ * The caller must dispose of the tree with got_object_tag_close().
+ */
+const struct got_error *got_object_tag_open(struct got_tag_object **,
+    struct got_repository *, struct got_object *);
+
+/* Dispose of a tag object. */
+void got_object_tag_close(struct got_tag_object *);
+
 const struct got_error *
 got_object_open_as_commit(struct got_commit_object **,
     struct got_repository *, struct got_object_id *);
@@ -233,6 +245,8 @@ got_object_open_as_tree(struct got_tree_object **,
 const struct got_error *
 got_object_open_as_blob(struct got_blob_object **,
     struct got_repository *, struct got_object_id *, size_t);
+const struct got_error *got_object_open_as_tag(struct got_tag_object **,
+    struct got_repository *, struct got_object_id *);
 
 const struct got_error *got_object_commit_add_parent(struct got_commit_object *,
     const char *);
diff --git a/lib/got_lib_object.h b/lib/got_lib_object.h
index fc8b2ee..c261919 100644
--- a/lib/got_lib_object.h
+++ b/lib/got_lib_object.h
@@ -48,3 +48,14 @@ struct got_blob_object {
 	uint8_t *read_buf;
 	struct got_object_id id;
 };
+
+struct got_tag_object {
+	struct got_object_id id;
+	int obj_type;
+	char *tag;
+	time_t tagger_time;
+	time_t tagger_gmtoff;
+	char *tagger;
+	char *tagmsg;
+	int refcnt;		/* > 0 if open and/or cached */
+};
diff --git a/lib/got_lib_object_cache.h b/lib/got_lib_object_cache.h
index 067f6f0..a10ff39 100644
--- a/lib/got_lib_object_cache.h
+++ b/lib/got_lib_object_cache.h
@@ -18,6 +18,7 @@ enum got_object_cache_type {
 	GOT_OBJECT_CACHE_TYPE_OBJ,
 	GOT_OBJECT_CACHE_TYPE_TREE,
 	GOT_OBJECT_CACHE_TYPE_COMMIT,
+	GOT_OBJECT_CACHE_TYPE_TAG,
 };
 
 struct got_object_cache_entry {
@@ -26,6 +27,7 @@ struct got_object_cache_entry {
 		struct got_object *obj;
 		struct got_tree_object *tree;
 		struct got_commit_object *commit;
+		struct got_tag_object *tag;
 	} data;
 };
 
diff --git a/lib/got_lib_object_parse.h b/lib/got_lib_object_parse.h
index f537052..dcc7620 100644
--- a/lib/got_lib_object_parse.h
+++ b/lib/got_lib_object_parse.h
@@ -26,11 +26,15 @@ const struct got_error *got_object_read_commit_privsep(
     struct got_repository *);
 const struct got_error *got_object_read_tree_privsep(struct got_tree_object **,
     struct got_object *, int, struct got_repository *);
+const struct got_error *got_object_read_tag_privsep(struct got_tag_object **,
+    struct got_object *, int, struct got_repository *);
 
 const struct got_error *got_object_parse_commit(struct got_commit_object **,
     char *, size_t);
 const struct got_error *got_object_parse_tree(struct got_tree_object **,
     uint8_t *, size_t);
+const struct got_error *got_object_parse_tag(struct got_tag_object **,
+    uint8_t *, size_t);
 const struct got_error *got_read_file_to_mem(uint8_t **, size_t *, FILE *);
 
 void got_object_tree_entry_close(struct got_tree_entry *);
@@ -45,3 +49,5 @@ const struct got_error *got_object_read_packed_commit_privsep(
     struct got_commit_object **, struct got_object *, struct got_pack *);
 const struct got_error *got_object_read_packed_tree_privsep(
     struct got_tree_object **, struct got_object *, struct got_pack *);
+const struct got_error *got_object_read_packed_tag_privsep(
+    struct got_tag_object **, struct got_object *, struct got_pack *);
diff --git a/lib/got_lib_privsep.h b/lib/got_lib_privsep.h
index a396e2c..871454f 100644
--- a/lib/got_lib_privsep.h
+++ b/lib/got_lib_privsep.h
@@ -39,6 +39,7 @@
 #define GOT_PROG_READ_TREE	got-read-tree
 #define GOT_PROG_READ_COMMIT	got-read-commit
 #define GOT_PROG_READ_BLOB	got-read-blob
+#define GOT_PROG_READ_TAG	got-read-tag
 #define GOT_PROG_READ_PACK	got-read-pack
 
 #define GOT_STRINGIFY(x) #x
@@ -53,6 +54,8 @@
 	GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_COMMIT)
 #define GOT_PATH_PROG_READ_BLOB \
 	GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_BLOB)
+#define GOT_PATH_PROG_READ_TAG \
+	GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_TAG)
 #define GOT_PATH_PROG_READ_PACK \
 	GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_PACK)
 
@@ -88,6 +91,9 @@ enum got_imsg_type {
 	GOT_IMSG_BLOB_REQUEST,
 	GOT_IMSG_BLOB_OUTFD,
 	GOT_IMSG_BLOB,
+	GOT_IMSG_TAG_REQUEST,
+	GOT_IMSG_TAG,
+	GOT_IMSG_TAG_TAGMSG,
 
 	/* Messages related to pack files. */
 	GOT_IMSG_PACKIDX,
@@ -162,6 +168,26 @@ struct got_imsg_blob {
 	size_t size;
 };
 
+/* Structure for GOT_IMSG_TAG data. */
+struct got_imsg_tag_object {
+	uint8_t id[SHA1_DIGEST_LENGTH];
+	int obj_type;
+	size_t tag_len;
+	size_t tagger_len;
+	time_t tagger_time;
+	time_t tagger_gmtoff;
+	size_t tagmsg_len;
+
+	/*
+	 * Followed by tag_len + tagger_len data bytes
+	 */
+
+	/*
+	 * Followed by 'tagmsg_len' bytes of tag message data in
+	 * one or more GOT_IMSG_TAG_TAGMSG messages.
+	 */
+} __attribute__((__packed__));
+
 /* Structure for GOT_IMSG_PACKIDX. */
 struct got_imsg_packidx {
 	size_t len;
@@ -212,6 +238,10 @@ const struct got_error *got_privsep_send_tree(struct imsgbuf *,
     struct got_tree_object *);
 const struct got_error *got_privsep_send_blob(struct imsgbuf *, size_t);
 const struct got_error *got_privsep_recv_blob(size_t *, struct imsgbuf *);
+const struct got_error *got_privsep_send_tag(struct imsgbuf *,
+    struct got_tag_object *);
+const struct got_error *got_privsep_recv_tag(struct got_tag_object **,
+    struct imsgbuf *);
 const struct got_error *got_privsep_init_pack_child(struct imsgbuf *,
     struct got_pack *, struct got_packidx *);
 const struct got_error *got_privsep_send_packed_obj_req(struct imsgbuf *, int,
diff --git a/lib/got_lib_repository.h b/lib/got_lib_repository.h
index feabca1..4379222 100644
--- a/lib/got_lib_repository.h
+++ b/lib/got_lib_repository.h
@@ -28,16 +28,18 @@ struct got_repository {
 	struct got_pack packs[GOT_PACK_CACHE_SIZE];
 
 	/* Handles to child processes for reading loose objects. */
-	 struct got_privsep_child privsep_children[4];
+	 struct got_privsep_child privsep_children[5];
 #define GOT_REPO_PRIVSEP_CHILD_OBJECT	0
 #define GOT_REPO_PRIVSEP_CHILD_COMMIT	1
 #define GOT_REPO_PRIVSEP_CHILD_TREE	2
 #define GOT_REPO_PRIVSEP_CHILD_BLOB	3
+#define GOT_REPO_PRIVSEP_CHILD_TAG	4
 
 	/* Caches for open objects. */
 	struct got_object_cache objcache;
 	struct got_object_cache treecache;
 	struct got_object_cache commitcache;
+	struct got_object_cache tagcache;
 };
 
 const struct got_error*got_repo_cache_object(struct got_repository *,
@@ -52,6 +54,10 @@ const struct got_error*got_repo_cache_commit(struct got_repository *,
     struct got_object_id *, struct got_commit_object *);
 struct got_commit_object *got_repo_get_cached_commit(struct got_repository *,
     struct got_object_id *);
+const struct got_error*got_repo_cache_tag(struct got_repository *,
+    struct got_object_id *, struct got_tag_object *);
+struct got_tag_object *got_repo_get_cached_tag(struct got_repository *,
+    struct got_object_id *);
 const struct got_error *got_repo_cache_packidx(struct got_repository *,
     struct got_packidx *);
 const struct got_error *got_repo_search_packidx(struct got_packidx **, int *,
diff --git a/lib/object.c b/lib/object.c
index 6bb2eb4..641b24f 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -702,6 +702,85 @@ got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
 	return NULL;
 }
 
+static const struct got_error *
+open_tag(struct got_tag_object **tag,
+    struct got_repository *repo, struct got_object *obj, int check_cache)
+{
+	const struct got_error *err = NULL;
+
+	if (check_cache) {
+		*tag = got_repo_get_cached_tag(repo, &obj->id);
+		if (*tag != NULL) {
+			(*tag)->refcnt++;
+			return NULL;
+		}
+	} else
+		*tag = NULL;
+
+	if (obj->type != GOT_OBJ_TYPE_TAG)
+		return got_error(GOT_ERR_OBJ_TYPE);
+
+	if (obj->flags & GOT_OBJ_FLAG_PACKED) {
+		struct got_pack *pack;
+		pack = got_repo_get_cached_pack(repo, obj->path_packfile);
+		if (pack == NULL) {
+			err = got_repo_cache_pack(&pack, repo,
+			    obj->path_packfile, NULL);
+			if (err)
+				return err;
+		}
+		err = got_object_read_packed_tag_privsep(tag, obj, pack);
+	} else {
+		int fd;
+		err = open_loose_object(&fd, obj, repo);
+		if (err)
+			return err;
+		err = got_object_read_tag_privsep(tag, obj, fd, repo);
+		close(fd);
+	}
+
+	if (err == NULL) {
+		(*tag)->refcnt++;
+		err = got_repo_cache_tag(repo, &obj->id, *tag);
+	}
+
+	return err;
+}
+
+const struct got_error *
+got_object_open_as_tag(struct got_tag_object **tag,
+    struct got_repository *repo, struct got_object_id *id)
+{
+	const struct got_error *err;
+	struct got_object *obj;
+
+	*tag = got_repo_get_cached_tag(repo, id);
+	if (*tag != NULL) {
+		(*tag)->refcnt++;
+		return NULL;
+	}
+
+	err = got_object_open(&obj, repo, id);
+	if (err)
+		return err;
+	if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
+		err = got_error(GOT_ERR_OBJ_TYPE);
+		goto done;
+	}
+
+	err = open_tag(tag, repo, obj, 0);
+done:
+	got_object_close(obj);
+	return err;
+}
+
+const struct got_error *
+got_object_tag_open(struct got_tag_object **tag,
+    struct got_repository *repo, struct got_object *obj)
+{
+	return open_tag(tag, repo, obj, 1);
+}
+
 static struct got_tree_entry *
 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
 {
@@ -1074,7 +1153,6 @@ done:
 	return err;
 }
 
-
 static const struct got_error *
 request_commit(struct got_commit_object **commit, struct got_repository *repo,
     struct got_object *obj, int fd)
@@ -1277,3 +1355,69 @@ got_object_read_blob_privsep(size_t *size, int outfd, int infd,
 
 	return request_blob(size, outfd, infd, ibuf);
 }
+
+static const struct got_error *
+request_tag(struct got_tag_object **tag, struct got_repository *repo,
+    struct got_object *obj, int fd)
+{
+	const struct got_error *err = NULL;
+	struct imsgbuf *ibuf;
+
+	ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
+
+	err = got_privsep_send_obj_req(ibuf, fd, obj);
+	if (err)
+		return err;
+
+	return got_privsep_recv_tag(tag, ibuf);
+}
+
+const struct got_error *
+got_object_read_packed_tag_privsep(struct got_tag_object **tag,
+    struct got_object *obj, struct got_pack *pack)
+{
+	const struct got_error *err = NULL;
+
+	err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
+	if (err)
+		return err;
+
+	return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
+}
+
+const struct got_error *
+got_object_read_tag_privsep(struct got_tag_object **tag,
+    struct got_object *obj, int obj_fd, struct got_repository *repo)
+{
+	int imsg_fds[2];
+	pid_t pid;
+	struct imsgbuf *ibuf;
+
+	if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
+		return request_tag(tag, repo, obj, obj_fd);
+
+	ibuf = calloc(1, sizeof(*ibuf));
+	if (ibuf == NULL)
+		return got_error_from_errno();
+
+	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
+		return got_error_from_errno();
+
+	pid = fork();
+	if (pid == -1)
+		return got_error_from_errno();
+	else if (pid == 0) {
+		exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
+		    repo->path);
+		/* not reached */
+	}
+
+	close(imsg_fds[1]);
+	repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
+	    imsg_fds[0];
+	repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
+	imsg_init(ibuf, imsg_fds[0]);
+	repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
+
+	return request_tag(tag, repo, obj, obj_fd);
+}
diff --git a/lib/object_cache.c b/lib/object_cache.c
index 3325c9f..63b2ca2 100644
--- a/lib/object_cache.c
+++ b/lib/object_cache.c
@@ -35,6 +35,7 @@
 #define GOT_OBJECT_CACHE_SIZE_OBJ	256
 #define GOT_OBJECT_CACHE_SIZE_TREE	256
 #define GOT_OBJECT_CACHE_SIZE_COMMIT	64
+#define GOT_OBJECT_CACHE_SIZE_TAG	32
 
 const struct got_error *
 got_object_cache_init(struct got_object_cache *cache,
@@ -57,6 +58,9 @@ got_object_cache_init(struct got_object_cache *cache,
 	case GOT_OBJECT_CACHE_TYPE_COMMIT:
 		cache->size = GOT_OBJECT_CACHE_SIZE_COMMIT;
 		break;
+	case GOT_OBJECT_CACHE_TYPE_TAG:
+		cache->size = GOT_OBJECT_CACHE_SIZE_TAG;
+		break;
 	}
 	return NULL;
 }
@@ -84,6 +88,9 @@ got_object_cache_add(struct got_object_cache *cache, struct got_object_id *id, v
 		case GOT_OBJECT_CACHE_TYPE_COMMIT:
 			got_object_commit_close(ce->data.commit);
 			break;
+		case GOT_OBJECT_CACHE_TYPE_TAG:
+			got_object_tag_close(ce->data.tag);
+			break;
 		}
 		free(ce);
 		cache->cache_evict++;
@@ -103,6 +110,9 @@ got_object_cache_add(struct got_object_cache *cache, struct got_object_id *id, v
 	case GOT_OBJECT_CACHE_TYPE_COMMIT:
 		ce->data.commit = (struct got_commit_object *)item;
 		break;
+	case GOT_OBJECT_CACHE_TYPE_TAG:
+		ce->data.tag = (struct got_tag_object *)item;
+		break;
 	}
 
 	err = got_object_idset_add(cache->idset, id, ce);
@@ -131,6 +141,8 @@ got_object_cache_get(struct got_object_cache *cache, struct got_object_id *id)
 			return ce->data.tree;
 		case GOT_OBJECT_CACHE_TYPE_COMMIT:
 			return ce->data.commit;
+		case GOT_OBJECT_CACHE_TYPE_TAG:
+			return ce->data.tag;
 		}
 	}
 
@@ -157,6 +169,7 @@ check_refcount(struct got_object_id *id, void *data, void *arg)
 	struct got_object *obj;
 	struct got_tree_object *tree;
 	struct got_commit_object *commit;
+	struct got_tag_object *tag;
 	char *id_str;
 
 	if (got_object_id_str(&id_str, id) != NULL)
@@ -184,6 +197,13 @@ check_refcount(struct got_object_id *id, void *data, void *arg)
 		fprintf(stderr, "commit %s has %d unclaimed references\n",
 		    id_str, commit->refcnt - 1);
 		break;
+	case GOT_OBJECT_CACHE_TYPE_TAG:
+		tag = ce->data.tag;
+		if (tag->refcnt == 1)
+			break;
+		fprintf(stderr, "tag %s has %d unclaimed references\n",
+		    id_str, tag->refcnt - 1);
+		break;
 	}
 	free(id_str);
 	return NULL;
diff --git a/lib/object_parse.c b/lib/object_parse.c
index 0c8445c..433a659 100644
--- a/lib/object_parse.c
+++ b/lib/object_parse.c
@@ -53,11 +53,21 @@
 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
 #endif
 
+#define GOT_OBJ_TAG_COMMIT	"commit"
+#define GOT_OBJ_TAG_TREE	"tree"
+#define GOT_OBJ_TAG_BLOB	"blob"
+#define GOT_OBJ_TAG_TAG		"tag"
+
 #define GOT_COMMIT_TAG_TREE		"tree "
 #define GOT_COMMIT_TAG_PARENT		"parent "
 #define GOT_COMMIT_TAG_AUTHOR		"author "
 #define GOT_COMMIT_TAG_COMMITTER	"committer "
 
+#define GOT_TAG_TAG_OBJECT		"object "
+#define GOT_TAG_TAG_TYPE		"type "
+#define GOT_TAG_TAG_TAG			"tag "
+#define GOT_TAG_TAG_TAGGER		"tagger "
+
 int
 got_object_id_cmp(const struct got_object_id *id1,
     const struct got_object_id *id2)
@@ -518,6 +528,187 @@ got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
 	return NULL;
 }
 
+void
+got_object_tag_close(struct got_tag_object *tag)
+{
+	free(tag->tag);
+	free(tag->tagger);
+	free(tag->tagmsg);
+	free(tag);
+}
+
+const struct got_error *
+got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
+{
+	const struct got_error *err = NULL;
+	size_t remain = len;
+	char *s = buf;
+	size_t tlen;
+
+	*tag = calloc(1, sizeof(**tag));
+	if (*tag == NULL)
+		return got_error_from_errno();
+
+	tlen = strlen(GOT_TAG_TAG_OBJECT);
+	if (strncmp(s, GOT_TAG_TAG_OBJECT, tlen) == 0) {
+		remain -= tlen;
+		if (remain < SHA1_DIGEST_STRING_LENGTH) {
+			err = got_error(GOT_ERR_BAD_OBJ_DATA);
+			goto done;
+		}
+		s += tlen;
+		if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
+			err = got_error(GOT_ERR_BAD_OBJ_DATA);
+			goto done;
+		}
+		remain -= SHA1_DIGEST_STRING_LENGTH;
+		s += SHA1_DIGEST_STRING_LENGTH;
+	} else {
+		err = got_error(GOT_ERR_BAD_OBJ_DATA);
+		goto done;
+	}
+
+	if (remain <= 0) {
+		err = got_error(GOT_ERR_BAD_OBJ_DATA);
+		goto done;
+	}
+
+	tlen = strlen(GOT_TAG_TAG_TYPE);
+	if (strncmp(s, GOT_TAG_TAG_TYPE, tlen) == 0) {
+		remain -= tlen;
+		if (remain <= 0) {
+			err = got_error(GOT_ERR_BAD_OBJ_DATA);
+			goto done;
+		}
+		s += tlen;
+		if (strncmp(s, GOT_OBJ_TAG_COMMIT,
+		    strlen(GOT_OBJ_TAG_COMMIT)) == 0) {
+			(*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
+			tlen = strlen(GOT_OBJ_TAG_COMMIT);
+			s += tlen;
+			remain -= tlen;
+		} else if (strncmp(s, GOT_OBJ_TAG_TREE,
+		    strlen(GOT_OBJ_TAG_TREE)) == 0) {
+			(*tag)->obj_type = GOT_OBJ_TYPE_TREE;
+			tlen = strlen(GOT_OBJ_TAG_TREE);
+			s += tlen;
+			remain -= tlen;
+		} else if (strncmp(s, GOT_OBJ_TAG_BLOB,
+		    strlen(GOT_OBJ_TAG_BLOB)) == 0) {
+			(*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
+			tlen = strlen(GOT_OBJ_TAG_BLOB);
+			s += tlen;
+			remain -= tlen;
+		} else if (strncmp(s, GOT_OBJ_TAG_TAG,
+		    strlen(GOT_OBJ_TAG_TAG)) == 0) {
+			(*tag)->obj_type = GOT_OBJ_TYPE_TAG;
+			tlen = strlen(GOT_OBJ_TAG_TAG);
+			s += tlen;
+			remain -= tlen;
+		} else {
+			err = got_error(GOT_ERR_BAD_OBJ_DATA);
+			goto done;
+		}
+
+		if (remain <= 0 || *s != '\n') {
+			err = got_error(GOT_ERR_BAD_OBJ_DATA);
+			goto done;
+		}
+		s++;
+		remain--;
+		if (remain <= 0) {
+			err = got_error(GOT_ERR_BAD_OBJ_DATA);
+			goto done;
+		}
+	} else {
+		err = got_error(GOT_ERR_BAD_OBJ_DATA);
+		goto done;
+	}
+
+	tlen = strlen(GOT_TAG_TAG_TAG);
+	if (strncmp(s, GOT_TAG_TAG_TAG, tlen) == 0) {
+		char *p;
+		size_t slen;
+		remain -= tlen;
+		if (remain <= 0) {
+			err = got_error(GOT_ERR_BAD_OBJ_DATA);
+			goto done;
+		}
+		s += tlen;
+		p = strchr(s, '\n');
+		if (p == NULL) {
+			err = got_error(GOT_ERR_BAD_OBJ_DATA);
+			goto done;
+		}
+		*p = '\0';
+		slen = strlen(s);
+		(*tag)->tag = strndup(s, slen);
+		if ((*tag)->tag == NULL) {
+			err = got_error_from_errno();
+			goto done;
+		}
+		s += slen + 1;
+		remain -= slen + 1;
+		if (remain <= 0) {
+			err = got_error(GOT_ERR_BAD_OBJ_DATA);
+			goto done;
+		}
+	} else {
+		err = got_error(GOT_ERR_BAD_OBJ_DATA);
+		goto done;
+	}
+
+	tlen = strlen(GOT_TAG_TAG_TAGGER);
+	if (strncmp(s, GOT_TAG_TAG_TAGGER, tlen) == 0) {
+		char *p;
+		size_t slen;
+
+		remain -= tlen;
+		if (remain <= 0) {
+			err = got_error(GOT_ERR_BAD_OBJ_DATA);
+			goto done;
+		}
+		s += tlen;
+		p = strchr(s, '\n');
+		if (p == NULL) {
+			err = got_error(GOT_ERR_BAD_OBJ_DATA);
+			goto done;
+		}
+		*p = '\0';
+		slen = strlen(s);
+		err = parse_commit_time(&(*tag)->tagger_time,
+		    &(*tag)->tagger_gmtoff, s);
+		if (err)
+			goto done;
+		(*tag)->tagger = strdup(s);
+		if ((*tag)->tagger == NULL) {
+			err = got_error_from_errno();
+			goto done;
+		}
+		s += slen + 1;
+		remain -= slen + 1;
+		if (remain <= 0) {
+			err = got_error(GOT_ERR_BAD_OBJ_DATA);
+			goto done;
+		}
+	} else {
+		err = got_error(GOT_ERR_BAD_OBJ_DATA);
+		goto done;
+	}
+
+	(*tag)->tagmsg = strndup(s, remain);
+	if ((*tag)->tagmsg == NULL) {
+		err = got_error_from_errno();
+		goto done;
+	}
+done:
+	if (err) {
+		got_object_tag_close(*tag);
+		*tag = NULL;
+	}
+	return err;
+}
+
 const struct got_error *
 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
 {
diff --git a/lib/privsep.c b/lib/privsep.c
index 28b8501..d95f501 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -238,6 +238,9 @@ got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd, struct got_object *obj)
 		case GOT_OBJ_TYPE_BLOB:
 			imsg_code = GOT_IMSG_BLOB_REQUEST;
 			break;
+		case GOT_OBJ_TYPE_TAG:
+			imsg_code = GOT_IMSG_TAG_REQUEST;
+			break;
 		default:
 			return got_error(GOT_ERR_OBJ_TYPE);
 		}
@@ -825,6 +828,211 @@ got_privsep_recv_blob(size_t *size, struct imsgbuf *ibuf)
 	return err;
 }
 
+static const struct got_error *
+send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
+{
+	const struct got_error *err = NULL;
+	size_t offset, remain;
+
+	offset = 0;
+	remain = tagmsg_len;
+	while (remain > 0) {
+		size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
+
+		if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
+		    tag->tagmsg + offset, n) == -1) {
+			err = got_error_from_errno();
+			break;
+		}
+
+		err = flush_imsg(ibuf);
+		if (err)
+			break;
+
+		offset += n;
+		remain -= n;
+	}
+
+	return err;
+}
+
+const struct got_error *
+got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
+{
+	const struct got_error *err = NULL;
+	struct got_imsg_tag_object *itag;
+	uint8_t *buf;
+	size_t len, total;
+	size_t tag_len = strlen(tag->tag);
+	size_t tagger_len = strlen(tag->tagger);
+	size_t tagmsg_len = strlen(tag->tagmsg);
+
+	total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
+
+	buf = malloc(total);
+	if (buf == NULL)
+		return got_error_from_errno();
+
+	itag = (struct got_imsg_tag_object *)buf;
+	memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
+	itag->obj_type = tag->obj_type;
+	itag->tag_len = tag_len;
+	itag->tagger_len = tagger_len;
+	itag->tagger_time = tag->tagger_time;
+	itag->tagger_gmtoff = tag->tagger_gmtoff;
+	itag->tagmsg_len = tagmsg_len;
+
+	len = sizeof(*itag);
+	memcpy(buf + len, tag->tag, tag_len);
+	len += tag_len;
+	memcpy(buf + len, tag->tagger, tagger_len);
+	len += tagger_len;
+
+	if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
+		err = got_error_from_errno();
+		goto done;
+	}
+
+	if (tagmsg_len == 0 ||
+	    tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
+		err = flush_imsg(ibuf);
+		if (err)
+			goto done;
+	}
+	err = send_tagmsg(ibuf, tag, tagmsg_len);
+done:
+	free(buf);
+	return err;
+}
+
+const struct got_error *
+got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
+{
+	const struct got_error *err = NULL;
+	struct imsg imsg;
+	struct got_imsg_tag_object *itag;
+	size_t len, datalen;
+	const size_t min_datalen =
+	    MIN(sizeof(struct got_imsg_error),
+	    sizeof(struct got_imsg_tag_object));
+
+	*tag = NULL;
+
+	err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
+	if (err)
+		return err;
+
+	datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
+	len = 0;
+
+	switch (imsg.hdr.type) {
+	case GOT_IMSG_TAG:
+		if (datalen < sizeof(*itag)) {
+			err = got_error(GOT_ERR_PRIVSEP_LEN);
+			break;
+		}
+		itag = imsg.data;
+		if (datalen != sizeof(*itag) + itag->tag_len +
+		    itag->tagger_len) {
+			err = got_error(GOT_ERR_PRIVSEP_LEN);
+			break;
+		}
+		len += sizeof(*itag);
+
+		*tag = calloc(1, sizeof(**tag));
+		if (*tag == NULL) {
+			err = got_error_from_errno();
+			break;
+		}
+
+		memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
+
+		if (itag->tag_len == 0) {
+			(*tag)->tag = strdup("");
+			if ((*tag)->tag == NULL) {
+				err = got_error_from_errno();
+				break;
+			}
+		} else {
+			(*tag)->tag = malloc(itag->tag_len + 1);
+			if ((*tag)->tag == NULL) {
+				err = got_error_from_errno();
+				break;
+			}
+			memcpy((*tag)->tag, imsg.data + len,
+			    itag->tag_len);
+			(*tag)->tag[itag->tag_len] = '\0';
+		}
+		len += itag->tag_len;
+
+		(*tag)->obj_type = itag->obj_type;
+		(*tag)->tagger_time = itag->tagger_time;
+		(*tag)->tagger_gmtoff = itag->tagger_gmtoff;
+
+		if (itag->tagger_len == 0) {
+			(*tag)->tagger = strdup("");
+			if ((*tag)->tagger == NULL) {
+				err = got_error_from_errno();
+				break;
+			}
+		} else {
+			(*tag)->tagger = malloc(itag->tagger_len + 1);
+			if ((*tag)->tagger == NULL) {
+				err = got_error_from_errno();
+				break;
+			}
+			memcpy((*tag)->tagger, imsg.data + len,
+			    itag->tagger_len);
+			(*tag)->tagger[itag->tagger_len] = '\0';
+		}
+		len += itag->tagger_len;
+
+		if (itag->tagmsg_len == 0) {
+			(*tag)->tagmsg = strdup("");
+			if ((*tag)->tagmsg == NULL) {
+				err = got_error_from_errno();
+				break;
+			}
+		} else {
+			size_t offset = 0, remain = itag->tagmsg_len;
+
+			(*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
+			if ((*tag)->tagmsg == NULL) {
+				err = got_error_from_errno();
+				break;
+			}
+			while (remain > 0) {
+				struct imsg imsg_log;
+				size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
+				    remain);
+
+				err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
+				if (err)
+					return err;
+
+				if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
+					return got_error(GOT_ERR_PRIVSEP_MSG);
+
+				memcpy((*tag)->tagmsg + offset, imsg_log.data,
+				    n);
+				imsg_free(&imsg_log);
+				offset += n;
+				remain -= n;
+			}
+			(*tag)->tagmsg[itag->tagmsg_len] = '\0';
+		}
+
+		break;
+	default:
+		err = got_error(GOT_ERR_PRIVSEP_MSG);
+		break;
+	}
+
+	imsg_free(&imsg);
+
+	return err;
+}
+
 const struct got_error *
 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
     struct got_packidx *packidx)
diff --git a/lib/repository.c b/lib/repository.c
index 3becb1c..941438e 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -232,6 +232,27 @@ got_repo_get_cached_commit(struct got_repository *repo,
 }
 
 const struct got_error *
+got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
+    struct got_tag_object *tag)
+{
+#ifndef GOT_NO_OBJ_CACHE
+	const struct got_error *err = NULL;
+	err = got_object_cache_add(&repo->tagcache, id, tag);
+	if (err)
+		return err;
+	tag->refcnt++;
+#endif
+	return NULL;
+}
+
+struct got_tag_object *
+got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
+{
+	return (struct got_tag_object *)got_object_cache_get(
+	    &repo->tagcache, id);
+}
+
+const struct got_error *
 open_repo(struct got_repository *repo, const char *path)
 {
 	const struct got_error *err = NULL;
@@ -354,6 +375,10 @@ got_repo_open(struct got_repository **repop, const char *path)
 	    GOT_OBJECT_CACHE_TYPE_COMMIT);
 	if (err)
 		goto done;
+	err = got_object_cache_init(&repo->tagcache,
+	    GOT_OBJECT_CACHE_TYPE_TAG);
+	if (err)
+		goto done;
 
 	normpath = got_path_normalize(abspath);
 	if (normpath == NULL) {
@@ -413,6 +438,7 @@ got_repo_close(struct got_repository *repo)
 	got_object_cache_close(&repo->objcache);
 	got_object_cache_close(&repo->treecache);
 	got_object_cache_close(&repo->commitcache);
+	got_object_cache_close(&repo->tagcache);
 
 	for (i = 0; i < nitems(repo->privsep_children); i++) {
 		if (repo->privsep_children[i].imsg_fd == -1)
diff --git a/libexec/got-read-object/got-read-object.c b/libexec/got-read-object/got-read-object.c
index 7738a57..ae3722c 100644
--- a/libexec/got-read-object/got-read-object.c
+++ b/libexec/got-read-object/got-read-object.c
@@ -45,6 +45,7 @@
 #define GOT_OBJ_TAG_COMMIT	"commit"
 #define GOT_OBJ_TAG_TREE	"tree"
 #define GOT_OBJ_TAG_BLOB	"blob"
+#define GOT_OBJ_TAG_TAG		"tag"
 
 static volatile sig_atomic_t sigint_received;
 
@@ -60,12 +61,14 @@ parse_object_header(struct got_object **obj, char *buf, size_t len)
 	const char *obj_tags[] = {
 		GOT_OBJ_TAG_COMMIT,
 		GOT_OBJ_TAG_TREE,
-		GOT_OBJ_TAG_BLOB
+		GOT_OBJ_TAG_BLOB,
+		GOT_OBJ_TAG_TAG,
 	};
 	const int obj_types[] = {
 		GOT_OBJ_TYPE_COMMIT,
 		GOT_OBJ_TYPE_TREE,
 		GOT_OBJ_TYPE_BLOB,
+		GOT_OBJ_TYPE_TAG,
 	};
 	int type = 0;
 	size_t size = 0, hdrlen = 0;
diff --git a/libexec/got-read-pack/got-read-pack.c b/libexec/got-read-pack/got-read-pack.c
index 24f359a..e5d58ee 100644
--- a/libexec/got-read-pack/got-read-pack.c
+++ b/libexec/got-read-pack/got-read-pack.c
@@ -272,6 +272,43 @@ done:
 }
 
 static const struct got_error *
+tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
+    struct got_packidx *packidx, struct got_object_cache *objcache)
+{
+	const struct got_error *err = NULL;
+	struct got_object *obj = NULL;
+	struct got_tag_object *tag = NULL;
+	uint8_t *buf;
+	size_t len;
+
+	err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
+	    GOT_OBJ_TYPE_TAG);
+	if (err)
+		return err;
+
+	err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
+	if (err)
+		return err;
+
+	obj->size = len;
+	err = got_object_parse_tag(&tag, buf, len);
+	free(buf);
+
+	err = got_privsep_send_tag(ibuf, tag);
+	if (obj)
+		got_object_close(obj);
+	got_object_tag_close(tag);
+	if (err) {
+		if (err->code == GOT_ERR_PRIVSEP_PIPE)
+			err = NULL;
+		else
+			got_privsep_send_error(ibuf, err);
+	}
+
+	return err;
+}
+
+static const struct got_error *
 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
 {
 	const struct got_error *err = NULL;
@@ -488,6 +525,10 @@ main(int argc, char *argv[])
 			err = blob_request(&imsg, &ibuf, pack, packidx,
 			   &objcache);
 			break;
+		case GOT_IMSG_TAG_REQUEST:
+			err = tag_request(&imsg, &ibuf, pack, packidx,
+			   &objcache);
+			break;
 		default:
 			err = got_error(GOT_ERR_PRIVSEP_MSG);
 			break;