Hash :
2b5af615
Author :
Date :
2011-07-07T13:47:45
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
/*
* 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, const 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 git__rethrow(error, "Failed to parse tag. Object field invalid");
if (buffer + 5 >= buffer_end)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse tag. Object too short");
if (memcmp(buffer, "type ", 5) != 0)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse tag. Type field not found");
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__throw(GIT_EOBJCORRUPTED, "Failed to parse tag. Object too short");
if (memcmp(buffer, tag_types[i], type_length) == 0) {
tag->type = i;
buffer += type_length;
break;
}
}
if (tag->type == GIT_OBJ_BAD)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse tag. Invalid object type");
if (buffer + 4 >= buffer_end)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse tag. Object too short");
if (memcmp(buffer, "tag ", 4) != 0)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse tag. Tag field not found");
buffer += 4;
search = memchr(buffer, '\n', buffer_end - buffer);
if (search == NULL)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse tag. Object too short");
text_len = search - buffer;
tag->tag_name = git__malloc(text_len + 1);
if (tag->tag_name == NULL)
return GIT_ENOMEM;
memcpy(tag->tag_name, buffer, text_len);
tag->tag_name[text_len] = '\0';
buffer = search + 1;
tag->tagger = git__malloc(sizeof(git_signature));
if (tag->tagger == NULL)
return GIT_ENOMEM;
if ((error = git_signature__parse(tag->tagger, &buffer, buffer_end, "tagger ")) != 0) {
free(tag->tag_name);
git_signature_free(tag->tagger);
return git__rethrow(error, "Failed to parse tag");
}
if( *buffer != '\n' )
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse tag. No new line before message");
text_len = buffer_end - ++buffer;
tag->message = git__malloc(text_len + 1);
if (tag->message == NULL)
return GIT_ENOMEM;
memcpy(tag->message, buffer, text_len);
tag->message[text_len] = '\0';
return GIT_SUCCESS;
}
static int retreive_tag_reference(git_reference **tag_reference_out, char *ref_name_out, git_repository *repo, const char *tag_name)
{
git_reference *tag_ref;
int error;
git_path_join(ref_name_out, GIT_REFS_TAGS_DIR, tag_name);
error = git_reference_lookup(&tag_ref, repo, ref_name_out);
if (error < GIT_SUCCESS)
return git__rethrow(error, "Failed to retrieve tag reference");
*tag_reference_out = tag_ref;
return GIT_SUCCESS;
}
int git_tag_create(
git_oid *oid,
git_repository *repo,
const char *tag_name,
const git_object *target,
const git_signature *tagger,
const char *message,
int allow_ref_overwrite)
{
size_t final_size = 0;
git_odb_stream *stream;
const char *type_str;
char *tagger_str;
git_reference *new_ref = NULL;
char ref_name[GIT_REFNAME_MAX];
int type_str_len, tag_name_len, tagger_str_len, message_len;
int error, should_update_ref = 0;
if (git_object_owner(target) != repo)
return git__throw(GIT_EINVALIDARGS, "The given target does not belong to this repository");
error = retreive_tag_reference(&new_ref, ref_name, repo, tag_name);
switch (error) {
case GIT_SUCCESS:
case GIT_ENOTFOUND:
break;
default:
return git__rethrow(error, "Failed to create tag");
}
/** Ensure the tag name doesn't conflict with an already existing
* reference unless overwriting has explictly been requested **/
if (new_ref != NULL) {
if (!allow_ref_overwrite) {
git_oid_cpy(oid, git_reference_oid(new_ref));
return git__throw(GIT_EEXISTS, "Tag already exists");
} else {
should_update_ref = 1;
}
}
type_str = git_object_type2string(git_object_type(target));
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 git__rethrow(error, "Failed to create tag");
git__write_oid(stream, "object", git_object_id(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)
return git__rethrow(error, "Failed to create tag");
if (!should_update_ref)
error = git_reference_create_oid(&new_ref, repo, ref_name, oid, 0);
else
error = git_reference_set_oid(new_ref, oid);
return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to create tag");
}
int git_tag_create_frombuffer(git_oid *oid, git_repository *repo, const char *buffer, int allow_ref_overwrite)
{
git_tag tag;
int error, should_update_ref = 0;
git_odb_stream *stream;
git_odb_object *target_obj;
git_reference *new_ref;
char ref_name[GIT_REFNAME_MAX];
assert(oid && buffer);
memset(&tag, 0, sizeof(tag));
/* validate the buffer */
if ((error = parse_tag_buffer(&tag, buffer, buffer + strlen(buffer))) < GIT_SUCCESS)
return git__rethrow(error, "Failed to create tag");
/* validate the target */
if ((error = git_odb_read(&target_obj, repo->db, &tag.target)) < GIT_SUCCESS)
return git__rethrow(error, "Failed to create tag");
if (tag.type != target_obj->raw.type)
return git__throw(error, "The type for the given target is invalid");
git_odb_object_close(target_obj);
error = retreive_tag_reference(&new_ref, ref_name, repo, tag.tag_name);
switch (error) {
case GIT_SUCCESS:
case GIT_ENOTFOUND:
break;
default:
return git__rethrow(error, "Failed to create tag");
}
/** Ensure the tag name doesn't conflict with an already existing
* reference unless overwriting has explictly been requested **/
if (new_ref != NULL) {
if (!allow_ref_overwrite) {
git_oid_cpy(oid, git_reference_oid(new_ref));
return git__throw(GIT_EEXISTS, "Tag already exists");
} else {
should_update_ref = 1;
}
}
/* write the buffer */
if ((error = git_odb_open_wstream(&stream, repo->db, strlen(buffer), GIT_OBJ_TAG)) < GIT_SUCCESS)
return git__rethrow(error, "Failed to create tag");
stream->write(stream, buffer, strlen(buffer));
error = stream->finalize_write(oid, stream);
stream->free(stream);
if (error < GIT_SUCCESS)
return git__rethrow(error, "Failed to create tag");
if (!should_update_ref)
error = git_reference_create_oid(&new_ref, repo, ref_name, oid, 0);
else
error = git_reference_set_oid(new_ref, oid);
git_signature_free(tag.tagger);
free(tag.tag_name);
free(tag.message);
return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to create tag");
}
int git_tag_delete(git_repository *repo, const char *tag_name)
{
int error;
git_reference *tag_ref;
char ref_name[GIT_REFNAME_MAX];
error = retreive_tag_reference(&tag_ref, ref_name, repo, tag_name);
if (error < GIT_SUCCESS)
return git__rethrow(error, "Failed to delete tag");
return git_reference_delete(tag_ref);
}
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);
}
typedef struct {
git_vector *taglist;
const char *pattern;
} tag_filter_data;
#define GIT_REFS_TAGS_DIR_LEN STRLEN(GIT_REFS_TAGS_DIR)
static int tag_list_cb(const char *tag_name, void *payload)
{
tag_filter_data *filter;
if (git__prefixcmp(tag_name, GIT_REFS_TAGS_DIR) != 0)
return GIT_SUCCESS;
filter = (tag_filter_data *)payload;
if (!*filter->pattern || p_fnmatch(filter->pattern, tag_name + GIT_REFS_TAGS_DIR_LEN, 0) == GIT_SUCCESS)
return git_vector_insert(filter->taglist, git__strdup(tag_name));
return GIT_SUCCESS;
}
int git_tag_list_match(git_strarray *tag_names, const char *pattern, git_repository *repo)
{
int error;
tag_filter_data filter;
git_vector taglist;
assert(tag_names && repo && pattern);
if (git_vector_init(&taglist, 8, NULL) < GIT_SUCCESS)
return GIT_ENOMEM;
filter.taglist = &taglist;
filter.pattern = pattern;
error = git_reference_foreach(repo, GIT_REF_OID|GIT_REF_PACKED, &tag_list_cb, (void *)&filter);
if (error < GIT_SUCCESS) {
git_vector_free(&taglist);
return git__rethrow(error, "Failed to list tags");
}
tag_names->strings = (char **)taglist.contents;
tag_names->count = taglist.length;
return GIT_SUCCESS;
}
int git_tag_list(git_strarray *tag_names, git_repository *repo)
{
return git_tag_list_match(tag_names, "", repo);
}