Allow forcing the creation or renaming of references Add internal reference create and rename functions which take a force parameter, telling them to overwrite an existing reference if it exists. These functions try to update the reference if it's of the same type as the one it's going to be replaced by. Otherwise the old reference becomes invalid. Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
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
diff --git a/include/git2/refs.h b/include/git2/refs.h
index da55eaa..9ecf772 100644
--- a/include/git2/refs.h
+++ b/include/git2/refs.h
@@ -69,6 +69,27 @@ GIT_EXTERN(int) git_reference_lookup(git_reference **reference_out, git_reposito
GIT_EXTERN(int) git_reference_create_symbolic(git_reference **ref_out, git_repository *repo, const char *name, const char *target);
/**
+ * Create a new symbolic reference, overwriting an existing one with
+ * the same name, if it exists.
+ *
+ * If the new reference isn't a symbolic one, any pointers to the old
+ * reference become invalid.
+ *
+ * The reference will be created in the repository and written
+ * to the disk.
+ *
+ * This reference is owned by the repository and shall not
+ * be free'd by the user.
+ *
+ * @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 target The target of the reference
+ * @return 0 on success; error code otherwise
+ */
+GIT_EXTERN(int) git_reference_create_symbolic_force(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
@@ -86,6 +107,27 @@ GIT_EXTERN(int) git_reference_create_symbolic(git_reference **ref_out, git_repos
GIT_EXTERN(int) git_reference_create_oid(git_reference **ref_out, git_repository *repo, const char *name, const git_oid *id);
/**
+ * Create a new object id reference, overwriting an existing one with
+ * the same name, if it exists.
+ *
+ * If the new reference isn't a symbolic one, any pointers to the old
+ * reference become invalid.
+ *
+ * The reference will be created in the repository and written
+ * to the disk.
+ *
+ * This reference is owned by the repository and shall not
+ * be free'd by the user.
+ *
+ * @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_force(git_reference **ref_out, git_repository *repo, const char *name, const git_oid *id);
+
+/**
* Get the OID pointed to by a reference.
*
* Only available if the reference is direct (i.e. not symbolic)
@@ -190,6 +232,20 @@ GIT_EXTERN(int) git_reference_set_oid(git_reference *ref, const git_oid *id);
GIT_EXTERN(int) git_reference_rename(git_reference *ref, const char *new_name);
/**
+ * Rename an existing reference, overwriting an existing one with the
+ * same name, if it exists.
+ *
+ * This method works for both direct and symbolic references.
+ * The new name will be checked for validity and may be
+ * modified into a normalized form.
+ *
+ * The refernece will be immediately renamed in-memory
+ * and on disk.
+ *
+ */
+GIT_EXTERN(int) git_reference_rename_force(git_reference *ref, const char *new_name);
+
+/**
* Delete an existing reference
*
* This method works for both direct and symbolic references.
diff --git a/src/refs.c b/src/refs.c
index 16bd741..20c1d00 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -915,8 +915,238 @@ cleanup:
return error;
}
+/*****************************************
+ * Internal methods - reference creation
+ *****************************************/
+
+int git_reference_create_symbolic_internal(git_reference **ref_out, git_repository *repo, const char *name, const char *target, int force)
+{
+ char normalized[MAX_GITDIR_TREE_STRUCTURE_PATH_LENGTH];
+ int error = GIT_SUCCESS, updated = 0;
+ git_reference *ref = NULL, *old_ref = NULL;
+
+ if (git_reference_lookup(&ref, repo, name) == GIT_SUCCESS && !force)
+ return GIT_EEXISTS;
+
+ /*
+ * If they old ref was of the same type, then we can just update
+ * it (once we've checked that the target is valid). Otherwise we
+ * need a new reference because we can't make a symbolic ref out
+ * of an oid one.
+ * If if didn't exist, then we need to create a new one anyway.
+ */
+ if (ref && ref->type & GIT_REF_SYMBOLIC){
+ updated = 1;
+ } else {
+ 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 = normalize_name(normalized, target, 0);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ /* set the target; this will write the reference on disk */
+ error = git_reference_set_target(ref, normalized);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ /*
+ * If we didn't update the ref, then we need to insert or replace
+ * it in the loose cache. If we replaced a ref, free it.
+ */
+ if (!updated){
+ error = git_hashtable_insert2(repo->references.loose_cache, ref->name, ref, (void **) &old_ref);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ if(old_ref)
+ reference_free(old_ref);
+ }
+
+ *ref_out = ref;
+
+ return error;
+
+cleanup:
+ reference_free(ref);
+ return error;
+}
+
+int git_reference_create_oid_internal(git_reference **ref_out, git_repository *repo, const char *name, const git_oid *id, int force)
+{
+ int error = GIT_SUCCESS, updated = 0;
+ git_reference *ref = NULL, *old_ref = NULL;
+
+ if(git_reference_lookup(&ref, repo, name) == GIT_SUCCESS && !force)
+ return GIT_EEXISTS;
+
+ /*
+ * If they old ref was of the same type, then we can just update
+ * it (once we've checked that the target is valid). Otherwise we
+ * need a new reference because we can't make a symbolic ref out
+ * of an oid one.
+ * If if didn't exist, then we need to create a new one anyway.
+ */
+ if (ref && ref-> type & GIT_REF_OID){
+ updated = 1;
+ } else {
+ ref = NULL;
+ error = reference_create(&ref, repo, name, GIT_REF_OID);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+ }
+
+ /* set the oid; this will write the reference on disk */
+ error = git_reference_set_oid(ref, id);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ if(!updated){
+ error = git_hashtable_insert2(repo->references.loose_cache, ref->name, ref, (void **) &old_ref);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ if(old_ref)
+ reference_free(old_ref);
+ }
+
+ *ref_out = ref;
+
+ return error;
+
+cleanup:
+ reference_free(ref);
+ return error;
+}
+
+/*
+ * Rename a reference
+ *
+ * If the reference is packed, we need to rewrite the
+ * packfile to remove the reference from it and create
+ * the reference back as a loose one.
+ *
+ * If the reference is loose, we just rename it on
+ * the filesystem.
+ *
+ * We also need to re-insert the reference on its corresponding
+ * in-memory cache, since the caches are indexed by refname.
+ */
+int git_reference_rename_internal(git_reference *ref, const char *new_name, int force)
+{
+ int error;
+ char *old_name;
+ char old_path[GIT_PATH_MAX], new_path[GIT_PATH_MAX], normalized_name[MAX_GITDIR_TREE_STRUCTURE_PATH_LENGTH];
+ git_reference *looked_up_ref, *old_ref = NULL;
+
+ assert(ref);
+
+ /* Ensure the name is valid */
+ error = normalize_name(normalized_name, new_name, ref->type & GIT_REF_OID);
+ if (error < GIT_SUCCESS)
+ return error;
+
+ /* Ensure we're not going to overwrite an existing reference */
+ error = git_reference_lookup(&looked_up_ref, ref->owner, new_name);
+ if (error == GIT_SUCCESS && !force)
+ return GIT_EEXISTS;
+
+ if (error != GIT_ENOTFOUND)
+ return error;
+
+
+ old_name = ref->name;
+ ref->name = git__strdup(new_name);
+
+ if (ref->name == NULL) {
+ ref->name = old_name;
+ return GIT_ENOMEM;
+ }
+
+ if (ref->type & GIT_REF_PACKED) {
+ /* write the packfile to disk; note
+ * that the state of the in-memory cache is not
+ * consistent, because the reference is indexed
+ * by its old name but it already has the new one.
+ * This doesn't affect writing, though, and allows
+ * us to rollback if writing fails
+ */
+
+ ref->type &= ~GIT_REF_PACKED;
+
+ /* Create the loose ref under its new name */
+ error = loose_write(ref);
+ if (error < GIT_SUCCESS) {
+ ref->type |= GIT_REF_PACKED;
+ goto cleanup;
+ }
+
+ /* Remove from the packfile cache in order to avoid packing it back
+ * Note : we do not rely on git_reference_delete() because this would
+ * invalidate the reference.
+ */
+ git_hashtable_remove(ref->owner->references.packfile, old_name);
+
+ /* Recreate the packed-refs file without the reference */
+ error = packed_write(ref->owner);
+ if (error < GIT_SUCCESS)
+ goto rename_loose_to_old_name;
+
+ } else {
+ git__joinpath(old_path, ref->owner->path_repository, old_name);
+ git__joinpath(new_path, ref->owner->path_repository, ref->name);
+
+ error = gitfo_mv_force(old_path, new_path);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
+
+ /* Once succesfully renamed, remove from the cache the reference known by its old name*/
+ git_hashtable_remove(ref->owner->references.loose_cache, old_name);
+ }
+
+ /* Store the renamed reference into the loose ref cache */
+ error = git_hashtable_insert2(ref->owner->references.loose_cache, ref->name, ref, (void **) &old_ref);
+
+ /* If we force-replaced, we need to free the old reference */
+ if(old_ref)
+ reference_free(old_ref);
+
+ free(old_name);
+ return error;
+
+cleanup:
+ /* restore the old name if this failed */
+ free(ref->name);
+ ref->name = old_name;
+ return error;
+
+rename_loose_to_old_name:
+ /* If we hit this point. Something *bad* happened! Think "Ghostbusters
+ * crossing the streams" definition of bad.
+ * Either the packed-refs has been correctly generated and something else
+ * has gone wrong, or the writing of the new packed-refs has failed, and
+ * we're stuck with the old one. As a loose ref always takes priority over
+ * a packed ref, we'll eventually try and rename the generated loose ref to
+ * its former name. It even that fails, well... we might have lost the reference
+ * for good. :-/
+ */
+
+ git__joinpath(old_path, ref->owner->path_repository, ref->name);
+ git__joinpath(new_path, ref->owner->path_repository, old_name);
+ /* No error checking. We'll return the initial error */
+ gitfo_mv_force(old_path, new_path);
+ /* restore the old name */
+ free(ref->name);
+ ref->name = old_name;
+
+ return error;
+}
/*****************************************
* External Library API
@@ -975,64 +1205,23 @@ int git_reference_lookup(git_reference **ref_out, git_repository *repo, const ch
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 = normalize_name(normalized, target, 0);
- if (error < GIT_SUCCESS)
- goto cleanup;
-
- /* set the target; this will write the reference on disk */
- error = git_reference_set_target(ref, normalized);
- if (error < GIT_SUCCESS)
- goto cleanup;
-
- error = git_hashtable_insert(repo->references.loose_cache, ref->name, ref);
- if (error < GIT_SUCCESS)
- goto cleanup;
-
- *ref_out = ref;
-
- return error;
+ return git_reference_create_symbolic_internal(ref_out, repo, name, target, 0);
+}
-cleanup:
- reference_free(ref);
- return error;
+int git_reference_create_symbolic_force(git_reference **ref_out, git_repository *repo, const char *name, const char *target)
+{
+ return git_reference_create_symbolic_internal(ref_out, repo, name, target, 1);
}
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;
-
- /* set the oid; this will write the reference on disk */
- error = git_reference_set_oid(ref, id);
- if (error < GIT_SUCCESS)
- goto cleanup;
-
- error = git_hashtable_insert(repo->references.loose_cache, ref->name, ref);
- if (error < GIT_SUCCESS)
- goto cleanup;
-
- *ref_out = ref;
-
- return error;
-
-cleanup:
- reference_free(ref);
- return error;
+ return git_reference_create_oid_internal(ref_out, repo, name, id, 0);
}
+int git_reference_create_oid_force(git_reference **ref_out, git_repository *repo, const char *name, const git_oid *id)
+{
+ return git_reference_create_oid_internal(ref_out, repo, name, id, 1);
+}
/**
* Getters
@@ -1240,125 +1429,14 @@ cleanup:
return error;
}
-/*
- * Rename a reference
- *
- * If the reference is packed, we need to rewrite the
- * packfile to remove the reference from it and create
- * the reference back as a loose one.
- *
- * If the reference is loose, we just rename it on
- * the filesystem.
- *
- * We also need to re-insert the reference on its corresponding
- * in-memory cache, since the caches are indexed by refname.
- */
int git_reference_rename(git_reference *ref, const char *new_name)
{
- int error;
- char *old_name;
- char old_path[GIT_PATH_MAX], new_path[GIT_PATH_MAX], normalized_name[MAX_GITDIR_TREE_STRUCTURE_PATH_LENGTH];
- git_reference *looked_up_ref;
-
- assert(ref);
-
- /* Ensure the name is valid */
- error = normalize_name(normalized_name, new_name, ref->type & GIT_REF_OID);
- if (error < GIT_SUCCESS)
- return error;
-
- /* Ensure we're not going to overwrite an existing reference */
- error = git_reference_lookup(&looked_up_ref, ref->owner, new_name);
- if (error == GIT_SUCCESS)
- return GIT_EINVALIDREFNAME;
-
- if (error != GIT_ENOTFOUND)
- return error;
-
-
- old_name = ref->name;
- ref->name = git__strdup(new_name);
-
- if (ref->name == NULL) {
- ref->name = old_name;
- return GIT_ENOMEM;
- }
-
- if (ref->type & GIT_REF_PACKED) {
- /* write the packfile to disk; note
- * that the state of the in-memory cache is not
- * consistent, because the reference is indexed
- * by its old name but it already has the new one.
- * This doesn't affect writing, though, and allows
- * us to rollback if writing fails
- */
-
- ref->type &= ~GIT_REF_PACKED;
-
- /* Create the loose ref under its new name */
- error = loose_write(ref);
- if (error < GIT_SUCCESS) {
- ref->type |= GIT_REF_PACKED;
- goto cleanup;
- }
-
- /* Remove from the packfile cache in order to avoid packing it back
- * Note : we do not rely on git_reference_delete() because this would
- * invalidate the reference.
- */
- git_hashtable_remove(ref->owner->references.packfile, old_name);
-
- /* Recreate the packed-refs file without the reference */
- error = packed_write(ref->owner);
- if (error < GIT_SUCCESS)
- goto rename_loose_to_old_name;
-
- } else {
- git__joinpath(old_path, ref->owner->path_repository, old_name);
- git__joinpath(new_path, ref->owner->path_repository, ref->name);
-
- error = gitfo_mv_force(old_path, new_path);
- if (error < GIT_SUCCESS)
- goto cleanup;
-
- /* Once succesfully renamed, remove from the cache the reference known by its old name*/
- git_hashtable_remove(ref->owner->references.loose_cache, old_name);
- }
-
- /* Store the renamed reference into the loose ref cache */
- error = git_hashtable_insert(ref->owner->references.loose_cache, ref->name, ref);
-
- free(old_name);
- return error;
-
-cleanup:
- /* restore the old name if this failed */
- free(ref->name);
- ref->name = old_name;
- return error;
-
-rename_loose_to_old_name:
- /* If we hit this point. Something *bad* happened! Think "Ghostbusters
- * crossing the streams" definition of bad.
- * Either the packed-refs has been correctly generated and something else
- * has gone wrong, or the writing of the new packed-refs has failed, and
- * we're stuck with the old one. As a loose ref always takes priority over
- * a packed ref, we'll eventually try and rename the generated loose ref to
- * its former name. It even that fails, well... we might have lost the reference
- * for good. :-/
- */
-
- git__joinpath(old_path, ref->owner->path_repository, ref->name);
- git__joinpath(new_path, ref->owner->path_repository, old_name);
-
- /* No error checking. We'll return the initial error */
- gitfo_mv_force(old_path, new_path);
-
- /* restore the old name */
- free(ref->name);
- ref->name = old_name;
+ return git_reference_rename_internal(ref, new_name, 0);
+}
- return error;
+int git_reference_rename_force(git_reference *ref, const char *new_name)
+{
+ return git_reference_rename_internal(ref, new_name, 1);
}
int git_reference_resolve(git_reference **resolved_ref, git_reference *ref)