libgit2/src/tag.c
Vicent Marti 72a3fe42fb I broke your bindings
Hey. Apologies in advance -- I broke your bindings.

This is a major commit that includes a long-overdue redesign of the
whole object-database structure. This is expected to be the last major
external API redesign of the library until the first non-alpha release.

Please get your bindings up to date with these changes. They will be
included in the next minor release. Sorry again!

Major features include:

	- Real caching and refcounting on parsed objects
	- Real caching and refcounting on objects read from the ODB
	- Streaming writes & reads from the ODB
	- Single-method writes for all object types
	- The external API is now partially thread-safe

The speed increases are significant in all aspects, specially when
reading an object several times from the ODB (revwalking) and when
writing big objects to the ODB.

Here's a full changelog for the external API:

blob.h
------

	- Remove `git_blob_new`
	- Remove `git_blob_set_rawcontent`
	- Remove `git_blob_set_rawcontent_fromfile`
	- Rename `git_blob_writefile` -> `git_blob_create_fromfile`
	- Change `git_blob_create_fromfile`:
		The `path` argument is now relative to the repository's working dir
	- Add `git_blob_create_frombuffer`

commit.h
--------

	- Remove `git_commit_new`
	- Remove `git_commit_add_parent`
	- Remove `git_commit_set_message`
	- Remove `git_commit_set_committer`
	- Remove `git_commit_set_author`
	- Remove `git_commit_set_tree`

	- Add `git_commit_create`
	- Add `git_commit_create_v`
	- Add `git_commit_create_o`
	- Add `git_commit_create_ov`

tag.h
-----

	- Remove `git_tag_new`
	- Remove `git_tag_set_target`
	- Remove `git_tag_set_name`
	- Remove `git_tag_set_tagger`
	- Remove `git_tag_set_message`

	- Add `git_tag_create`
	- Add `git_tag_create_o`

tree.h
------

	- Change `git_tree_entry_2object`:
		New signature is `(git_object **object_out, git_repository *repo, git_tree_entry *entry)`

	- Remove `git_tree_new`
	- Remove `git_tree_add_entry`
	- Remove `git_tree_remove_entry_byindex`
	- Remove `git_tree_remove_entry_byname`
	- Remove `git_tree_clearentries`
	- Remove `git_tree_entry_set_id`
	- Remove `git_tree_entry_set_name`
	- Remove `git_tree_entry_set_attributes`

object.h
------------

	- Remove `git_object_new
	- Remove `git_object_write`

	- Change `git_object_close`:
		This method is now *mandatory*. Not closing an object causes a
		memory leak.

odb.h
-----

	- Remove type `git_rawobj`
	- Remove `git_rawobj_close`
	- Rename `git_rawobj_hash` -> `git_odb_hash`
	- Change `git_odb_hash`:
		New signature is `(git_oid *id, const void *data, size_t len, git_otype type)`

	- Add type `git_odb_object`
	- Add `git_odb_object_close`

	- Change `git_odb_read`:
		New signature is `(git_odb_object **out, git_odb *db, const git_oid *id)`
	- Change `git_odb_read_header`:
		New signature is `(size_t *len_p, git_otype *type_p, git_odb *db, const git_oid *id)`
	- Remove `git_odb_write`
	- Add `git_odb_open_wstream`
	- Add `git_odb_open_rstream`

odb_backend.h
-------------

	- Change type `git_odb_backend`:
		New internal signatures are as follows

			int (* read)(void **, size_t *, git_otype *, struct git_odb_backend *, const git_oid *)
			int (* read_header)(size_t *, git_otype *, struct git_odb_backend *, const git_oid *)
			int (* writestream)(struct git_odb_stream **, struct git_odb_backend *, size_t, git_otype)
			int (* readstream)( struct git_odb_stream **, struct git_odb_backend *, const git_oid *)

	- Add type `git_odb_stream`
	- Add enum `git_odb_streammode`

Signed-off-by: Vicent Marti <tanoku@gmail.com>
2011-03-20 21:45:11 +02:00

243 lines
5.6 KiB
C

/*
* 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 "signature.h"
#include "git2/object.h"
#include "git2/repository.h"
#include "git2/signature.h"
void git_tag__free(git_tag *tag)
{
git_signature_free(tag->tagger);
free(tag->message);
free(tag->tag_name);
free(tag);
}
const git_oid *git_tag_id(git_tag *c)
{
return git_object_id((git_object *)c);
}
int git_tag_target(git_object **target, git_tag *t)
{
assert(t);
return git_object_lookup(target, t->object.repo, &t->target, t->type);
}
const git_oid *git_tag_target_oid(git_tag *t)
{
assert(t);
return &t->target;
}
git_otype git_tag_type(git_tag *t)
{
assert(t);
return t->type;
}
const char *git_tag_name(git_tag *t)
{
assert(t);
return t->tag_name;
}
const git_signature *git_tag_tagger(git_tag *t)
{
return t->tagger;
}
const char *git_tag_message(git_tag *t)
{
assert(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"
};
unsigned int i, text_len;
char *search;
int error;
if ((error = git__parse_oid(&tag->target, &buffer, buffer_end, "object ")) < 0)
return error;
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;
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;
tag->tagger = git__malloc(sizeof(git_signature));
if ((error = git_signature__parse(tag->tagger, &buffer, buffer_end, "tagger ")) != 0)
return error;
text_len = buffer_end - ++buffer;
tag->message = git__malloc(text_len + 1);
memcpy(tag->message, buffer, text_len);
tag->message[text_len] = '\0';
return GIT_SUCCESS;
}
int git_tag_create_o(
git_oid *oid,
git_repository *repo,
const char *tag_name,
const git_object *target,
const git_signature *tagger,
const char *message)
{
return git_tag_create(
oid, repo, tag_name,
git_object_id(target),
git_object_type(target),
tagger, message);
}
int git_tag_create(
git_oid *oid,
git_repository *repo,
const char *tag_name,
const git_oid *target,
git_otype target_type,
const git_signature *tagger,
const char *message)
{
size_t final_size = 0;
git_odb_stream *stream;
const char *type_str;
char *tagger_str;
int type_str_len, tag_name_len, tagger_str_len, message_len;
int error;
type_str = git_object_type2string(target_type);
tagger_str_len = git_signature__write(&tagger_str, "tagger", tagger);
type_str_len = strlen(type_str);
tag_name_len = strlen(tag_name);
message_len = strlen(message);
final_size += GIT_OID_LINE_LENGTH("object");
final_size += STRLEN("type ") + type_str_len + 1;
final_size += STRLEN("tag ") + tag_name_len + 1;
final_size += tagger_str_len;
final_size += 1 + message_len;
if ((error = git_odb_open_wstream(&stream, repo->db, final_size, GIT_OBJ_TAG)) < GIT_SUCCESS)
return error;
git__write_oid(stream, "object", target);
stream->write(stream, "type ", STRLEN("type "));
stream->write(stream, type_str, type_str_len);
stream->write(stream, "\ntag ", STRLEN("\ntag "));
stream->write(stream, tag_name, tag_name_len);
stream->write(stream, "\n", 1);
stream->write(stream, tagger_str, tagger_str_len);
free(tagger_str);
stream->write(stream, "\n", 1);
stream->write(stream, message, message_len);
error = stream->finalize_write(oid, stream);
stream->free(stream);
if (error == GIT_SUCCESS) {
char ref_name[512];
git_reference *new_ref;
git__joinpath(ref_name, GIT_REFS_TAGS_DIR, tag_name);
error = git_reference_create_oid(&new_ref, repo, ref_name, oid);
}
return error;
}
int git_tag__parse(git_tag *tag, git_odb_object *obj)
{
assert(tag);
return parse_tag_buffer(tag, obj->raw.data, (char *)obj->raw.data + obj->raw.len);
}