Add loading and parsing of tag objects Tag objects are now properly loaded from the revision pool. New test t0801 checks for loading a parsing a series of tags, including the tag of a tag. Signed-off-by: Vicent Marti <tanoku@gmail.com>
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
diff --git a/src/git/tag.h b/src/git/tag.h
new file mode 100644
index 0000000..509d2f0
--- /dev/null
+++ b/src/git/tag.h
@@ -0,0 +1,89 @@
+#ifndef INCLUDE_git_tag_h__
+#define INCLUDE_git_tag_h__
+
+#include "common.h"
+#include "oid.h"
+#include "tree.h"
+
+/**
+ * @file git/tag.h
+ * @brief Git tag parsing routines
+ * @defgroup git_tag Git tag management
+ * @ingroup Git
+ * @{
+ */
+GIT_BEGIN_DECL
+
+/** Parsed representation of a tag object. */
+typedef struct git_tag git_tag;
+
+/**
+ * Locate a reference to a tag without loading it.
+ * The generated tag object is owned by the revision
+ * pool and shall not be freed by the user.
+ *
+ * @param pool the pool to use when locating the tag.
+ * @param id identity of the tag to locate.
+ * @return the tag; NULL if the tag could not be created
+ */
+GIT_EXTERN(git_tag *) git_tag_lookup(git_revpool *pool, const git_oid *id);
+
+/**
+ * Locate a reference to a tag, and try to load and parse it it from
+ * the object cache or the object database.
+ * The generated tag object is owned by the revision
+ * pool and shall not be freed by the user.
+ *
+ * @param pool the pool to use when parsing/caching the tag.
+ * @param id identity of the tag to locate.
+ * @return the tag; NULL if the tag does not exist in the
+ * pool's git_odb, or if the tag is present but is
+ * too malformed to be parsed successfully.
+ */
+GIT_EXTERN(git_tag *) git_tag_parse(git_revpool *pool, const git_oid *id);
+
+/**
+ * Get the id of a tag.
+ * @param tag a previously loaded tag.
+ * @return object identity for the tag.
+ */
+GIT_EXTERN(const git_oid *) git_tag_id(git_tag *tag);
+
+/**
+ * Get the tagged object of a tag
+ * @param tag a previously loaded tag.
+ * @return reference to a repository object
+ */
+GIT_EXTERN(const git_repository_object *) git_tag_target(git_tag *t);
+
+/**
+ * Get the type of a tag's tagged object
+ * @param tag a previously loaded tag.
+ * @return type of the tagged object
+ */
+GIT_EXTERN(git_otype) git_tag_type(git_tag *t);
+
+/**
+ * Get the name of a tag
+ * @param tag a previously loaded tag.
+ * @return name of the tag
+ */
+GIT_EXTERN(const char *) git_tag_name(git_tag *t);
+
+/**
+ * Get the tagger (author) of a tag
+ * @param tag a previously loaded tag.
+ * @return reference to the tag's author
+ */
+GIT_EXTERN(const git_person *) git_tag_tagger(git_tag *t);
+
+/**
+ * Get the message of a tag
+ * @param tag a previously loaded tag.
+ * @return message of the tag
+ */
+GIT_EXTERN(const char *) git_tag_message(git_tag *t);
+
+/** @} */
+GIT_END_DECL
+#endif
diff --git a/src/tag.c b/src/tag.c
new file mode 100644
index 0000000..803cfa1
--- /dev/null
+++ b/src/tag.c
@@ -0,0 +1,228 @@
+/*
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2,
+ * as published by the Free Software Foundation.
+ *
+ * In addition to the permissions in the GNU General Public License,
+ * the authors give you unlimited permission to link the compiled
+ * version of this file into combinations with other programs,
+ * and to distribute those combinations without any restriction
+ * coming from the use of this file. (The General Public License
+ * restrictions do apply in other respects; for example, they cover
+ * modification of the file, and distribution when not linked into
+ * a combined executable.)
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include "common.h"
+#include "commit.h"
+#include "tag.h"
+#include "revwalk.h"
+#include "git/odb.h"
+
+
+void git_tag__free(git_tag *tag)
+{
+ free(tag->message);
+ free(tag->tag_name);
+ free(tag->tagger);
+ free(tag);
+}
+
+const git_oid *git_tag_id(git_tag *t)
+{
+ return &t->object.id;
+}
+
+const git_revpool_object *git_tag_target(git_tag *t)
+{
+ if (t->target)
+ return t->target;
+
+ git_tag__parse(t);
+ return t->target;
+}
+
+git_otype git_tag_type(git_tag *t)
+{
+ if (t->type)
+ return t->type;
+
+ git_tag__parse(t);
+ return t->type;
+}
+
+
+const char *git_tag_name(git_tag *t)
+{
+ if (t->tag_name)
+ return t->tag_name;
+
+ git_tag__parse(t);
+ return t->tag_name;
+}
+
+const git_person *git_tag_tagger(git_tag *t)
+{
+ if (t->tagger)
+ return t->tagger;
+
+ git_tag__parse(t);
+ return t->tagger;
+}
+
+const char *git_tag_message(git_tag *t)
+{
+ if (t->message)
+ return t->message;
+
+ git_tag__parse(t);
+ return t->message;
+}
+
+static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
+{
+ static const char *tag_types[] = {
+ NULL, "commit\n", "tree\n", "blob\n", "tag\n"
+ };
+
+ git_oid target_oid;
+ unsigned int i, text_len;
+ char *search;
+
+ if (git__parse_oid(&target_oid, &buffer, buffer_end, "object ") < 0)
+ return GIT_EOBJCORRUPTED;
+
+ if (buffer + 5 >= buffer_end)
+ return GIT_EOBJCORRUPTED;
+
+ if (memcmp(buffer, "type ", 5) != 0)
+ return GIT_EOBJCORRUPTED;
+ buffer += 5;
+
+ tag->type = GIT_OBJ_BAD;
+
+ for (i = 1; i < ARRAY_SIZE(tag_types); ++i) {
+ size_t type_length = strlen(tag_types[i]);
+
+ if (buffer + type_length >= buffer_end)
+ return GIT_EOBJCORRUPTED;
+
+ if (memcmp(buffer, tag_types[i], type_length) == 0) {
+ tag->type = i;
+ buffer += type_length;
+ break;
+ }
+ }
+
+ if (tag->type == GIT_OBJ_BAD)
+ return GIT_EOBJCORRUPTED;
+
+ /* Lookup the tagged object once we know its type */
+ tag->target =
+ git_repository_lookup(tag->object.repo, &target_oid, tag->type);
+
+ /* FIXME: is the tag file really corrupted if the tagged object
+ * cannot be found on the database? */
+ /* if (tag->target == NULL)
+ return GIT_EOBJCORRUPTED; */
+
+ if (buffer + 4 >= buffer_end)
+ return GIT_EOBJCORRUPTED;
+
+ if (memcmp(buffer, "tag ", 4) != 0)
+ return GIT_EOBJCORRUPTED;
+ buffer += 4;
+
+ search = memchr(buffer, '\n', buffer_end - buffer);
+ if (search == NULL)
+ return GIT_EOBJCORRUPTED;
+
+ text_len = search - buffer;
+
+ if (tag->tag_name != NULL)
+ free(tag->tag_name);
+
+ tag->tag_name = git__malloc(text_len + 1);
+ memcpy(tag->tag_name, buffer, text_len);
+ tag->tag_name[text_len] = '\0';
+
+ buffer = search + 1;
+
+ if (tag->tagger != NULL)
+ free(tag->tagger);
+
+ tag->tagger = git__malloc(sizeof(git_person));
+
+ if (git__parse_person(tag->tagger, &buffer, buffer_end, "tagger ") != 0)
+ return GIT_EOBJCORRUPTED;
+
+ text_len = buffer_end - buffer;
+
+ if (tag->message != NULL)
+ free(tag->message);
+
+ tag->message = git__malloc(text_len + 1);
+ memcpy(tag->message, buffer, text_len);
+ tag->message[text_len] = '\0';
+
+ return 0;
+}
+
+int git_tag__parse(git_tag *tag)
+{
+ int error = 0;
+ git_obj odb_object;
+
+ error = git_odb_read(&odb_object, tag->object.pool->db, &tag->object.id);
+ if (error < 0)
+ return error;
+
+ if (odb_object.type != GIT_OBJ_TAG) {
+ error = GIT_EOBJTYPE;
+ goto cleanup;
+ }
+
+ error = parse_tag_buffer(tag, odb_object.data, odb_object.data + odb_object.len);
+
+cleanup:
+ git_obj_close(&odb_object);
+ return error;
+}
+
+git_tag *git_tag_lookup(git_revpool *pool, const git_oid *id)
+{
+ git_tag *tag = NULL;
+
+ if (pool == NULL)
+ return NULL;
+
+ tag = (git_tag *)git_revpool_table_lookup(pool->objects, id);
+ if (tag != NULL)
+ return tag;
+
+ tag = git__malloc(sizeof(git_tag));
+
+ if (tag == NULL)
+ return NULL;
+
+ memset(tag, 0x0, sizeof(git_tag));
+
+ /* Initialize parent object */
+ git_oid_cpy(&tag->object.id, id);
+ tag->object.pool = pool;
+ tag->object.type = GIT_OBJ_TAG;
+
+ git_revpool_table_insert(pool->objects, (git_revpool_object *)tag);
+
+ return tag;
+}
diff --git a/src/tag.h b/src/tag.h
new file mode 100644
index 0000000..5b1f5e2
--- /dev/null
+++ b/src/tag.h
@@ -0,0 +1,20 @@
+#ifndef INCLUDE_tag_h__
+#define INCLUDE_tag_h__
+
+#include "git/tag.h"
+#include "revobject.h"
+
+struct git_tag {
+ git_revpool_object object;
+
+ git_revpool_object *target;
+ git_otype type;
+ char *tag_name;
+ git_person *tagger;
+ char *message;
+};
+
+void git_tag__free(git_tag *tag);
+int git_tag__parse(git_tag *tag);
+
+#endif
diff --git a/tests/resources/pack-odb/7b/4384978d2493e851f9cca7858815fac9b10980 b/tests/resources/pack-odb/7b/4384978d2493e851f9cca7858815fac9b10980
new file mode 100644
index 0000000..23c462f
Binary files /dev/null and b/tests/resources/pack-odb/7b/4384978d2493e851f9cca7858815fac9b10980 differ
diff --git a/tests/resources/pack-odb/b2/5fa35b38051e4ae45d4222e795f9df2e43f1d1 b/tests/resources/pack-odb/b2/5fa35b38051e4ae45d4222e795f9df2e43f1d1
new file mode 100644
index 0000000..f460f25
--- /dev/null
+++ b/tests/resources/pack-odb/b2/5fa35b38051e4ae45d4222e795f9df2e43f1d1
@@ -0,0 +1,2 @@
+xA
+0a9I p'1Ѷv\x{cVpvWgǎ0x[]"g#{rD
Cot N U$?9-p+1^Qx9O\C m'D
{mV(+l,
\ No newline at end of file
diff --git a/tests/t0801-readtag.c b/tests/t0801-readtag.c
new file mode 100644
index 0000000..9e675e8
--- /dev/null
+++ b/tests/t0801-readtag.c
@@ -0,0 +1,48 @@
+#include "test_lib.h"
+#include "test_helpers.h"
+#include "commit.h"
+
+#include <git/odb.h>
+#include <git/commit.h>
+#include <git/tag.h>
+
+static const char *odb_dir = "../resources/pack-odb";
+static const char *tag1_id = "b25fa35b38051e4ae45d4222e795f9df2e43f1d1";
+static const char *tag2_id = "7b4384978d2493e851f9cca7858815fac9b10980";
+static const char *tagged_commit = "e90810b8df3e80c413d903f631643c716887138d";
+
+BEGIN_TEST(readtag)
+ git_odb *db;
+ git_repository *repo;
+ git_tag *tag1, *tag2;
+ git_commit *commit;
+ git_oid id1, id2, id_commit;
+
+ must_pass(git_odb_open(&db, odb_dir));
+
+ repo = git_repository_alloc(db);
+ must_be_true(repo != NULL);
+
+ git_oid_mkstr(&id1, tag1_id);
+ git_oid_mkstr(&id2, tag2_id);
+ git_oid_mkstr(&id_commit, tagged_commit);
+
+ tag1 = git_tag_lookup(repo, &id1);
+ must_be_true(tag1 != NULL);
+
+ 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_be_true(tag2 != NULL);
+
+ must_be_true(git_oid_cmp(&id2, git_tag_id(tag2)) == 0);
+
+ commit = (git_commit *)git_tag_target(tag2);
+ must_be_true(commit != NULL);
+
+ must_be_true(git_oid_cmp(&id_commit, git_commit_id(commit)) == 0);
+
+ git_repository_free(repo);
+ git_odb_close(db);
+END_TEST