Refactored the reference creation API.
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
diff --git a/src/git2/refs.h b/src/git2/refs.h
index 36031ec..57d3985 100644
--- a/src/git2/refs.h
+++ b/src/git2/refs.h
@@ -39,18 +39,32 @@
GIT_BEGIN_DECL
/**
- * Create a new reference.
+ * Create a new symbolic reference.
*
- * The reference will be empty and exclusively
- * in-memory until it is filled with the setter
- * methods and written back to disk using
- * `git_reference_write`.
+ * The reference will be created in the repository and written
+ * to the disk.
*
* @param ref_out Pointer to the newly created reference
- * @param repo Repository where that reference exists
+ * @param repo Repository where that reference will live
+ * @param name The name of the reference
+ * @param target The target of the reference
* @return 0 on success; error code otherwise
*/
-GIT_EXTERN(int) git_reference_new(git_reference **ref_out, git_repository *repo);
+GIT_EXTERN(int) git_reference_create_symbolic(git_reference **ref_out, git_repository *repo, const char *name, const char *target);
+
+/**
+ * Create a new object id reference.
+ *
+ * The reference will be created in the repository and written
+ * to the disk.
+ *
+ * @param ref_out Pointer to the newly created reference
+ * @param repo Repository where that reference will live
+ * @param name The name of the reference
+ * @param id The object id pointed to by the reference.
+ * @return 0 on success; error code otherwise
+ */
+GIT_EXTERN(int) git_reference_create_oid(git_reference **ref_out, git_repository *repo, const char *name, const git_oid *id);
/**
* Get the OID pointed to by a reference.
@@ -130,18 +144,6 @@ GIT_EXTERN(int) git_reference_write(git_reference *ref);
GIT_EXTERN(git_repository *) git_reference_owner(git_reference *ref);
/**
- * Set the name of a reference.
- *
- * This marks the reference as modified; changes
- * won't take effect until it is manually written back
- * to disk.
- *
- * @param ref The reference
- * @param name The new name for the reference
- */
-GIT_EXTERN(void) git_reference_set_name(git_reference *ref, const char *name);
-
-/**
* Set the target reference of a reference.
*
* This converts the reference into a symbolic
@@ -153,8 +155,9 @@ GIT_EXTERN(void) git_reference_set_name(git_reference *ref, const char *name);
*
* @param ref The reference
* @param target The new target for the reference
+ * @return 0 on success; error code otherwise
*/
-GIT_EXTERN(void) git_reference_set_target(git_reference *ref, const char *target);
+GIT_EXTERN(int) git_reference_set_target(git_reference *ref, const char *target);
/**
* Set the OID target of a reference.
@@ -168,8 +171,9 @@ GIT_EXTERN(void) git_reference_set_target(git_reference *ref, const char *target
*
* @param ref The reference
* @param target The new target OID for the reference
+ * @return 0 on success; error code otherwise
*/
-GIT_EXTERN(void) git_reference_set_oid(git_reference *ref, const git_oid *id);
+GIT_EXTERN(int) git_reference_set_oid(git_reference *ref, const git_oid *id);
/** @} */
GIT_END_DECL
diff --git a/src/refs.c b/src/refs.c
index 05c4e10..0cae6ee 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -68,22 +68,99 @@ static void reference_free(git_reference *reference)
free(reference);
}
-int git_reference_new(git_reference **ref_out, git_repository *repo)
-{
+static int reference__create(git_reference **ref_out, git_repository *repo, const char *name, git_rtype type) {
+ char normalized[MAX_GITDIR_TREE_STRUCTURE_PATH_LENGTH];
+ int error = GIT_SUCCESS;
git_reference *reference = NULL;
- assert(ref_out && repo);
+ assert(ref_out && repo && name);
+
+ if (type != GIT_REF_SYMBOLIC && type != GIT_REF_OID)
+ return GIT_EMISSINGOBJDATA;
reference = git__malloc(sizeof(git_reference));
if (reference == NULL)
return GIT_ENOMEM;
memset(reference, 0x0, sizeof(git_reference));
- reference->type = GIT_REF_INVALID;
reference->owner = repo;
+ reference->type = type;
+
+ error = git_reference__normalize_name(normalized, name, type);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ reference->name = git__strdup(normalized);
+ if (reference->name == NULL) {
+ error = GIT_ENOMEM;
+ goto cleanup;
+ }
*ref_out = reference;
- return GIT_SUCCESS;
+
+ return error;
+
+cleanup:
+ reference_free(reference);
+ return error;
+}
+
+int git_reference_create_symbolic(git_reference **ref_out, git_repository *repo, const char *name, const char *target)
+{
+ char normalized[MAX_GITDIR_TREE_STRUCTURE_PATH_LENGTH];
+ int error = GIT_SUCCESS;
+ git_reference *ref = NULL;
+
+ error = reference__create(&ref, repo, name, GIT_REF_SYMBOLIC);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ /* The target can aither be the name of an object id reference or the name of another symbolic reference */
+ error = git_reference__normalize_name(normalized, target, GIT_REF_ANY);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ error = git_reference_set_target(ref, normalized);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ error = git_reference_write(ref);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ *ref_out = ref;
+
+ return error;
+
+cleanup:
+ reference_free(ref);
+ return error;
+}
+
+int git_reference_create_oid(git_reference **ref_out, git_repository *repo, const char *name, const git_oid *id)
+{
+ int error = GIT_SUCCESS;
+ git_reference *ref = NULL;
+
+ error = reference__create(&ref, repo, name, GIT_REF_OID);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ error = git_reference_set_oid(ref, id);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ error = git_reference_write(ref);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ *ref_out = ref;
+
+ return error;
+
+cleanup:
+ reference_free(ref);
+ return error;
}
static int parse_sym_ref(git_reference *ref, gitfo_buf *file_content)
@@ -91,6 +168,7 @@ static int parse_sym_ref(git_reference *ref, gitfo_buf *file_content)
const unsigned int header_len = strlen(GIT_SYMREF);
const char *refname_start;
char *eol;
+ int error;
refname_start = (const char *)file_content->data;
@@ -104,9 +182,9 @@ static int parse_sym_ref(git_reference *ref, gitfo_buf *file_content)
refname_start += header_len;
- ref->target.ref = git__strdup(refname_start);
- if (ref->target.ref == NULL)
- return GIT_ENOMEM;
+ error = git_reference_set_target(ref, refname_start);
+ if (error < GIT_SUCCESS)
+ return error;
/* remove newline at the end of file */
eol = strchr(ref->target.ref, '\n');
@@ -117,14 +195,14 @@ static int parse_sym_ref(git_reference *ref, gitfo_buf *file_content)
if (eol[-1] == '\r')
eol[-1] = '\0';
- ref->type = GIT_REF_SYMBOLIC;
-
return GIT_SUCCESS;
}
static int parse_oid_ref(git_reference *ref, gitfo_buf *file_content)
{
char *buffer;
+ git_oid id;
+ int error;
buffer = (char *)file_content->data;
@@ -132,9 +210,13 @@ static int parse_oid_ref(git_reference *ref, gitfo_buf *file_content)
if (file_content->len < GIT_OID_HEXSZ + 1)
return GIT_EREFCORRUPTED;
- if (git_oid_mkstr(&ref->target.oid, buffer) < GIT_SUCCESS)
+ if (git_oid_mkstr(&id, buffer) < GIT_SUCCESS)
return GIT_EREFCORRUPTED;
+ error = git_reference_set_oid(ref, &id);
+ if (error < GIT_SUCCESS)
+ return error;
+
buffer = buffer + GIT_OID_HEXSZ;
if (*buffer == '\r')
buffer++;
@@ -142,7 +224,6 @@ static int parse_oid_ref(git_reference *ref, gitfo_buf *file_content)
if (*buffer != '\n')
return GIT_EREFCORRUPTED;
- ref->type = GIT_REF_OID;
return GIT_SUCCESS;
}
@@ -183,20 +264,21 @@ static int lookup_loose_ref(
if (error < GIT_SUCCESS)
goto cleanup;
- error = git_reference_new(&ref, repo);
- if (error < GIT_SUCCESS)
- goto cleanup;
-
- ref->name = git__strdup(name);
- if (ref->name == NULL) {
- error = GIT_ENOMEM;
- goto cleanup;
- }
+ if (git__prefixcmp((const char *)(ref_file.data), GIT_SYMREF) == 0) {
+ error = reference__create(&ref, repo, name, GIT_REF_SYMBOLIC);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
- if (git__prefixcmp((const char *)(ref_file.data), GIT_SYMREF) == 0)
error = parse_sym_ref(ref, &ref_file);
- else
+ } else {
+ error = reference__create(&ref, repo, name, GIT_REF_OID);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
error = parse_oid_ref(ref, &ref_file);
+ }
+
+ ref->modified = 0;
if (error < GIT_SUCCESS)
goto cleanup;
@@ -287,10 +369,8 @@ static int parse_packed_line(
int error = GIT_SUCCESS;
int refname_len;
-
- error = git_reference_new(&ref, repo);
- if (error < GIT_SUCCESS)
- goto cleanup;
+ char refname[MAX_GITDIR_TREE_STRUCTURE_PATH_LENGTH];
+ git_oid id;
refname_begin = (buffer + GIT_OID_HEXSZ + 1);
if (refname_begin >= buffer_end ||
@@ -300,7 +380,7 @@ static int parse_packed_line(
}
/* Is this a valid object id? */
- if ((error = git_oid_mkstr(&ref->target.oid, buffer)) < GIT_SUCCESS)
+ if ((error = git_oid_mkstr(&id, buffer)) < GIT_SUCCESS)
goto cleanup;
refname_end = memchr(refname_begin, '\n', buffer_end - refname_begin);
@@ -311,20 +391,22 @@ static int parse_packed_line(
refname_len = refname_end - refname_begin;
- ref->name = git__malloc(refname_len + 1);
- if (ref->name == NULL) {
- error = GIT_ENOMEM;
- goto cleanup;
- }
+ memcpy(refname, refname_begin, refname_len);
+ refname[refname_len] = 0;
- memcpy(ref->name, refname_begin, refname_len);
- ref->name[refname_len] = 0;
+ if (refname[refname_len - 1] == '\r')
+ refname[refname_len - 1] = 0;
- if (ref->name[refname_len - 1] == '\r')
- ref->name[refname_len - 1] = 0;
+ error = reference__create(&ref, repo, refname, GIT_REF_OID);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ error = git_reference_set_oid(ref, &id);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
- ref->type = GIT_REF_OID;
ref->packed = 1;
+ ref->modified = 0;
*ref_out = ref;
*buffer_out = refname_end + 1;
@@ -407,39 +489,31 @@ cleanup:
return error;
}
-void git_reference_set_oid(git_reference *ref, const git_oid *id)
+int git_reference_set_oid(git_reference *ref, const git_oid *id)
{
- if (ref->type == GIT_REF_SYMBOLIC)
- free(ref->target.ref);
+ if (ref->type != GIT_REF_OID)
+ return GIT_EINVALIDREFSTATE;
git_oid_cpy(&ref->target.oid, id);
- ref->type = GIT_REF_OID;
ref->modified = 1;
+
+ return GIT_SUCCESS;
}
-void git_reference_set_target(git_reference *ref, const char *target)
+int git_reference_set_target(git_reference *ref, const char *target)
{
- if (ref->type == GIT_REF_SYMBOLIC)
- free(ref->target.ref);
+ if (ref->type != GIT_REF_SYMBOLIC)
+ return GIT_EINVALIDREFSTATE;
+ free(ref->target.ref);
ref->target.ref = git__strdup(target);
- ref->type = GIT_REF_SYMBOLIC;
+ if (ref->target.ref == NULL)
+ return GIT_ENOMEM;
ref->modified = 1;
-}
-
-void git_reference_set_name(git_reference *ref, const char *name)
-{
- if (ref->name != NULL) {
- git_hashtable_remove(ref->owner->references.cache, ref->name);
- free(ref->name);
- }
- ref->name = git__strdup(name);
- git_hashtable_insert(ref->owner->references.cache, ref->name, ref);
-
- ref->modified = 1;
+ return GIT_SUCCESS;
}
const git_oid *git_reference_oid(git_reference *ref)
@@ -505,20 +579,17 @@ int git_reference_resolve(git_reference **resolved_ref, git_reference *ref)
int git_reference_write(git_reference *ref)
{
git_filebuf file;
+ git_reference *looked_up_reference;
char ref_path[GIT_PATH_MAX];
int error, contents_size;
char *ref_contents = NULL;
- if (ref->type == GIT_REF_INVALID ||
- ref->name == NULL)
- return GIT_EMISSINGOBJDATA;
+ if (ref->type == GIT_REF_INVALID || ref->type == GIT_REF_ANY)
+ return GIT_EINVALIDREFSTATE;
- if (ref->modified == 0)
+ if (!ref->modified)
return GIT_SUCCESS;
- if ((error = check_refname(ref->name)) < GIT_SUCCESS)
- return error;
-
git__joinpath(ref_path, ref->owner->path_repository, ref->name);
if ((error = git_filebuf_open(&file, ref_path, 0)) < GIT_SUCCESS)
@@ -530,11 +601,10 @@ int git_reference_write(git_reference *ref)
ref_contents = git__malloc(contents_size);
if (ref_contents == NULL) {
error = GIT_ENOMEM;
- goto error_cleanup;
+ goto unlock;
}
git_oid_fmt(ref_contents, &ref->target.oid);
- ref_contents[contents_size - 1] = '\n';
} else { /* GIT_REF_SYMBOLIC */
@@ -542,29 +612,35 @@ int git_reference_write(git_reference *ref)
ref_contents = git__malloc(contents_size);
if (ref_contents == NULL) {
error = GIT_ENOMEM;
- goto error_cleanup;
+ goto unlock;
}
strcpy(ref_contents, GIT_SYMREF);
strcat(ref_contents, ref->target.ref);
- ref_contents[contents_size - 1] = '\n';
}
+ ref_contents[contents_size - 1] = '\n';
+
if ((error = git_filebuf_write(&file, ref_contents, contents_size)) < GIT_SUCCESS)
goto error_cleanup;
- free(ref_contents);
-
error = git_filebuf_commit(&file);
- if (error == GIT_SUCCESS)
- ref->modified = 0;
+ looked_up_reference = git_hashtable_lookup(ref->owner->references.cache, ref->name);
- return error;
+ if (looked_up_reference == NULL) {
+ error = git_hashtable_insert(ref->owner->references.cache, ref->name, ref);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+ }
-error_cleanup:
+ goto cleanup;
+
+unlock:
+ git_filelock_unlock(&lock);
+
+cleanup:
free(ref_contents);
- git_filebuf_cleanup(&file);
return error;
}
diff --git a/tests/t10-refs.c b/tests/t10-refs.c
index 6dada0a..04fa926 100644
--- a/tests/t10-refs.c
+++ b/tests/t10-refs.c
@@ -204,10 +204,7 @@ BEGIN_TEST("createref", create_new_symbolic_ref)
git__joinpath(ref_path, repo->path_repository, new_head_tracker);
/* Create and write the new symbolic reference */
- must_pass(git_reference_new(&new_reference, repo));
- git_reference_set_target(new_reference, current_head_target);
- git_reference_set_name(new_reference, new_head_tracker);
- must_pass(git_reference_write(new_reference));
+ must_pass(git_reference_create_symbolic(&new_reference, repo, new_head_tracker, current_head_target));
/* Ensure the reference can be looked-up... */
must_pass(git_repository_lookup_ref(&looked_up_ref, repo, new_head_tracker));
@@ -252,10 +249,7 @@ BEGIN_TEST("createref", create_new_object_id_ref)
git__joinpath(ref_path, repo->path_repository, new_head);
/* Create and write the new object id reference */
- must_pass(git_reference_new(&new_reference, repo));
- git_reference_set_oid(new_reference, &id);
- git_reference_set_name(new_reference, new_head);
- must_pass(git_reference_write(new_reference));
+ must_pass(git_reference_create_oid(&new_reference, repo, new_head, &id));
/* Ensure the reference can be looked-up... */
must_pass(git_repository_lookup_ref(&looked_up_ref, repo, new_head));