| 1 | /* |
| 2 | * The backend-independent part of the reference module. |
| 3 | */ |
| 4 | |
| 5 | #include "git-compat-util.h" |
| 6 | #include "abspath.h" |
| 7 | #include "advice.h" |
| 8 | #include "config.h" |
| 9 | #include "environment.h" |
| 10 | #include "strmap.h" |
| 11 | #include "gettext.h" |
| 12 | #include "hex.h" |
| 13 | #include "lockfile.h" |
| 14 | #include "iterator.h" |
| 15 | #include "refs.h" |
| 16 | #include "refs/refs-internal.h" |
| 17 | #include "hook.h" |
| 18 | #include "object-name.h" |
| 19 | #include "odb.h" |
| 20 | #include "object.h" |
| 21 | #include "path.h" |
| 22 | #include "submodule.h" |
| 23 | #include "worktree.h" |
| 24 | #include "strvec.h" |
| 25 | #include "repo-settings.h" |
| 26 | #include "setup.h" |
| 27 | #include "date.h" |
| 28 | #include "commit.h" |
| 29 | #include "wildmatch.h" |
| 30 | #include "ident.h" |
| 31 | #include "fsck.h" |
| 32 | |
| 33 | /* |
| 34 | * List of all available backends |
| 35 | */ |
| 36 | static const struct ref_storage_be *refs_backends[] = { |
| 37 | [REF_STORAGE_FORMAT_FILES] = &refs_be_files, |
| 38 | [REF_STORAGE_FORMAT_REFTABLE] = &refs_be_reftable, |
| 39 | }; |
| 40 | |
| 41 | static const struct ref_storage_be *find_ref_storage_backend( |
| 42 | enum ref_storage_format ref_storage_format) |
| 43 | { |
| 44 | if (ref_storage_format < ARRAY_SIZE(refs_backends)) |
| 45 | return refs_backends[ref_storage_format]; |
| 46 | return NULL; |
| 47 | } |
| 48 | |
| 49 | enum ref_storage_format ref_storage_format_by_name(const char *name) |
| 50 | { |
| 51 | for (unsigned int i = 0; i < ARRAY_SIZE(refs_backends); i++) |
| 52 | if (refs_backends[i] && !strcmp(refs_backends[i]->name, name)) |
| 53 | return i; |
| 54 | return REF_STORAGE_FORMAT_UNKNOWN; |
| 55 | } |
| 56 | |
| 57 | const char *ref_storage_format_to_name(enum ref_storage_format ref_storage_format) |
| 58 | { |
| 59 | const struct ref_storage_be *be = find_ref_storage_backend(ref_storage_format); |
| 60 | if (!be) |
| 61 | return "unknown"; |
| 62 | return be->name; |
| 63 | } |
| 64 | |
| 65 | static const char *abort_by_ref_transaction_hook = |
| 66 | N_("in '%s' phase, update aborted by the reference-transaction hook"); |
| 67 | |
| 68 | /* |
| 69 | * How to handle various characters in refnames: |
| 70 | * 0: An acceptable character for refs |
| 71 | * 1: End-of-component |
| 72 | * 2: ., look for a preceding . to reject .. in refs |
| 73 | * 3: {, look for a preceding @ to reject @{ in refs |
| 74 | * 4: A bad character: ASCII control characters, and |
| 75 | * ":", "?", "[", "\", "^", "~", SP, or TAB |
| 76 | * 5: *, reject unless REFNAME_REFSPEC_PATTERN is set |
| 77 | */ |
| 78 | static unsigned char refname_disposition[256] = { |
| 79 | 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
| 80 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
| 81 | 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 1, |
| 82 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4, |
| 83 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 84 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 4, 0, |
| 85 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 86 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4 |
| 87 | }; |
| 88 | |
| 89 | struct ref_namespace_info ref_namespace[] = { |
| 90 | [NAMESPACE_HEAD] = { |
| 91 | .ref = "HEAD", |
| 92 | .decoration = DECORATION_REF_HEAD, |
| 93 | .exact = 1, |
| 94 | }, |
| 95 | [NAMESPACE_BRANCHES] = { |
| 96 | .ref = "refs/heads/", |
| 97 | .decoration = DECORATION_REF_LOCAL, |
| 98 | }, |
| 99 | [NAMESPACE_TAGS] = { |
| 100 | .ref = "refs/tags/", |
| 101 | .decoration = DECORATION_REF_TAG, |
| 102 | }, |
| 103 | [NAMESPACE_REMOTE_REFS] = { |
| 104 | /* |
| 105 | * The default refspec for new remotes copies refs from |
| 106 | * refs/heads/ on the remote into refs/remotes/<remote>/. |
| 107 | * As such, "refs/remotes/" has special handling. |
| 108 | */ |
| 109 | .ref = "refs/remotes/", |
| 110 | .decoration = DECORATION_REF_REMOTE, |
| 111 | }, |
| 112 | [NAMESPACE_STASH] = { |
| 113 | /* |
| 114 | * The single ref "refs/stash" stores the latest stash. |
| 115 | * Older stashes can be found in the reflog. |
| 116 | */ |
| 117 | .ref = "refs/stash", |
| 118 | .exact = 1, |
| 119 | .decoration = DECORATION_REF_STASH, |
| 120 | }, |
| 121 | [NAMESPACE_REPLACE] = { |
| 122 | /* |
| 123 | * This namespace allows Git to act as if one object ID |
| 124 | * points to the content of another. Unlike the other |
| 125 | * ref namespaces, this one can be changed by the |
| 126 | * GIT_REPLACE_REF_BASE environment variable. This |
| 127 | * .namespace value will be overwritten during repository |
| 128 | * setup. |
| 129 | */ |
| 130 | .ref = "refs/replace/", |
| 131 | .decoration = DECORATION_GRAFTED, |
| 132 | }, |
| 133 | [NAMESPACE_NOTES] = { |
| 134 | /* |
| 135 | * The refs/notes/commit ref points to the tip of a |
| 136 | * parallel commit history that adds metadata to commits |
| 137 | * in the normal history. This ref can be overwritten |
| 138 | * by the core.notesRef config variable or the |
| 139 | * GIT_NOTES_REFS environment variable. |
| 140 | */ |
| 141 | .ref = "refs/notes/commit", |
| 142 | .exact = 1, |
| 143 | }, |
| 144 | [NAMESPACE_PREFETCH] = { |
| 145 | /* |
| 146 | * Prefetch refs are written by the background 'fetch' |
| 147 | * maintenance task. It allows faster foreground fetches |
| 148 | * by advertising these previously-downloaded tips without |
| 149 | * updating refs/remotes/ without user intervention. |
| 150 | */ |
| 151 | .ref = "refs/prefetch/", |
| 152 | }, |
| 153 | [NAMESPACE_REWRITTEN] = { |
| 154 | /* |
| 155 | * Rewritten refs are used by the 'label' command in the |
| 156 | * sequencer. These are particularly useful during an |
| 157 | * interactive rebase that uses the 'merge' command. |
| 158 | */ |
| 159 | .ref = "refs/rewritten/", |
| 160 | }, |
| 161 | }; |
| 162 | |
| 163 | void update_ref_namespace(enum ref_namespace namespace, char *ref) |
| 164 | { |
| 165 | struct ref_namespace_info *info = &ref_namespace[namespace]; |
| 166 | if (info->ref_updated) |
| 167 | free((char *)info->ref); |
| 168 | info->ref = ref; |
| 169 | info->ref_updated = 1; |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Try to read one refname component from the front of refname. |
| 174 | * Return the length of the component found, or -1 if the component is |
| 175 | * not legal. It is legal if it is something reasonable to have under |
| 176 | * ".git/refs/"; We do not like it if: |
| 177 | * |
| 178 | * - it begins with ".", or |
| 179 | * - it has double dots "..", or |
| 180 | * - it has ASCII control characters, or |
| 181 | * - it has ":", "?", "[", "\", "^", "~", SP, or TAB anywhere, or |
| 182 | * - it has "*" anywhere unless REFNAME_REFSPEC_PATTERN is set, or |
| 183 | * - it ends with a "/", or |
| 184 | * - it ends with ".lock", or |
| 185 | * - it contains a "@{" portion |
| 186 | * |
| 187 | * When sanitized is not NULL, instead of rejecting the input refname |
| 188 | * as an error, try to come up with a usable replacement for the input |
| 189 | * refname in it. |
| 190 | */ |
| 191 | static int check_refname_component(const char *refname, int *flags, |
| 192 | struct strbuf *sanitized) |
| 193 | { |
| 194 | const char *cp; |
| 195 | char last = '\0'; |
| 196 | size_t component_start = 0; /* garbage - not a reasonable initial value */ |
| 197 | |
| 198 | if (sanitized) |
| 199 | component_start = sanitized->len; |
| 200 | |
| 201 | for (cp = refname; ; cp++) { |
| 202 | int ch = *cp & 255; |
| 203 | unsigned char disp = refname_disposition[ch]; |
| 204 | |
| 205 | if (sanitized && disp != 1) |
| 206 | strbuf_addch(sanitized, ch); |
| 207 | |
| 208 | switch (disp) { |
| 209 | case 1: |
| 210 | goto out; |
| 211 | case 2: |
| 212 | if (last == '.') { /* Refname contains "..". */ |
| 213 | if (sanitized) |
| 214 | /* collapse ".." to single "." */ |
| 215 | strbuf_setlen(sanitized, sanitized->len - 1); |
| 216 | else |
| 217 | return -1; |
| 218 | } |
| 219 | break; |
| 220 | case 3: |
| 221 | if (last == '@') { /* Refname contains "@{". */ |
| 222 | if (sanitized) |
| 223 | sanitized->buf[sanitized->len-1] = '-'; |
| 224 | else |
| 225 | return -1; |
| 226 | } |
| 227 | break; |
| 228 | case 4: |
| 229 | /* forbidden char */ |
| 230 | if (sanitized) |
| 231 | sanitized->buf[sanitized->len-1] = '-'; |
| 232 | else |
| 233 | return -1; |
| 234 | break; |
| 235 | case 5: |
| 236 | if (!(*flags & REFNAME_REFSPEC_PATTERN)) { |
| 237 | /* refspec can't be a pattern */ |
| 238 | if (sanitized) |
| 239 | sanitized->buf[sanitized->len-1] = '-'; |
| 240 | else |
| 241 | return -1; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * Unset the pattern flag so that we only accept |
| 246 | * a single asterisk for one side of refspec. |
| 247 | */ |
| 248 | *flags &= ~ REFNAME_REFSPEC_PATTERN; |
| 249 | break; |
| 250 | } |
| 251 | last = ch; |
| 252 | } |
| 253 | out: |
| 254 | if (cp == refname) |
| 255 | return 0; /* Component has zero length. */ |
| 256 | |
| 257 | if (refname[0] == '.') { /* Component starts with '.'. */ |
| 258 | if (sanitized) |
| 259 | sanitized->buf[component_start] = '-'; |
| 260 | else |
| 261 | return -1; |
| 262 | } |
| 263 | if (cp - refname >= LOCK_SUFFIX_LEN && |
| 264 | !memcmp(cp - LOCK_SUFFIX_LEN, LOCK_SUFFIX, LOCK_SUFFIX_LEN)) { |
| 265 | if (!sanitized) |
| 266 | return -1; |
| 267 | /* Refname ends with ".lock". */ |
| 268 | while (strbuf_strip_suffix(sanitized, LOCK_SUFFIX)) { |
| 269 | /* try again in case we have .lock.lock */ |
| 270 | } |
| 271 | } |
| 272 | return cp - refname; |
| 273 | } |
| 274 | |
| 275 | static int check_or_sanitize_refname(const char *refname, int flags, |
| 276 | struct strbuf *sanitized) |
| 277 | { |
| 278 | int component_len, component_count = 0; |
| 279 | |
| 280 | if (!strcmp(refname, "@")) { |
| 281 | /* Refname is a single character '@'. */ |
| 282 | if (sanitized) |
| 283 | strbuf_addch(sanitized, '-'); |
| 284 | else |
| 285 | return -1; |
| 286 | } |
| 287 | |
| 288 | while (1) { |
| 289 | if (sanitized && sanitized->len) |
| 290 | strbuf_complete(sanitized, '/'); |
| 291 | |
| 292 | /* We are at the start of a path component. */ |
| 293 | component_len = check_refname_component(refname, &flags, |
| 294 | sanitized); |
| 295 | if (sanitized && component_len == 0) |
| 296 | ; /* OK, omit empty component */ |
| 297 | else if (component_len <= 0) |
| 298 | return -1; |
| 299 | |
| 300 | component_count++; |
| 301 | if (refname[component_len] == '\0') |
| 302 | break; |
| 303 | /* Skip to next component. */ |
| 304 | refname += component_len + 1; |
| 305 | } |
| 306 | |
| 307 | if (refname[component_len - 1] == '.') { |
| 308 | /* Refname ends with '.'. */ |
| 309 | if (sanitized) |
| 310 | ; /* omit ending dot */ |
| 311 | else |
| 312 | return -1; |
| 313 | } |
| 314 | if (!(flags & REFNAME_ALLOW_ONELEVEL) && component_count < 2) |
| 315 | return -1; /* Refname has only one component. */ |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | int check_refname_format(const char *refname, int flags) |
| 320 | { |
| 321 | return check_or_sanitize_refname(refname, flags, NULL); |
| 322 | } |
| 323 | |
| 324 | int refs_fsck_ref(struct ref_store *refs UNUSED, struct fsck_options *o, |
| 325 | struct fsck_ref_report *report, |
| 326 | const char *refname UNUSED, const struct object_id *oid) |
| 327 | { |
| 328 | if (is_null_oid(oid)) |
| 329 | return fsck_report_ref(o, report, FSCK_MSG_BAD_REF_OID, |
| 330 | "points to invalid object ID '%s'", |
| 331 | oid_to_hex(oid)); |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | int refs_fsck_symref(struct ref_store *refs UNUSED, struct fsck_options *o, |
| 337 | struct fsck_ref_report *report, |
| 338 | const char *refname, const char *target) |
| 339 | { |
| 340 | const char *stripped_refname; |
| 341 | |
| 342 | parse_worktree_ref(refname, NULL, NULL, &stripped_refname); |
| 343 | |
| 344 | if (!strcmp(stripped_refname, "HEAD") && |
| 345 | !starts_with(target, "refs/heads/") && |
| 346 | fsck_report_ref(o, report, FSCK_MSG_BAD_HEAD_TARGET, |
| 347 | "HEAD points to non-branch '%s'", target)) |
| 348 | return -1; |
| 349 | |
| 350 | if (is_root_ref(target)) |
| 351 | return 0; |
| 352 | |
| 353 | if (check_refname_format(target, 0) && |
| 354 | fsck_report_ref(o, report, FSCK_MSG_BAD_REFERENT_NAME, |
| 355 | "points to invalid refname '%s'", target)) |
| 356 | return -1; |
| 357 | |
| 358 | if (!starts_with(target, "refs/") && |
| 359 | !starts_with(target, "worktrees/") && |
| 360 | fsck_report_ref(o, report, FSCK_MSG_SYMREF_TARGET_IS_NOT_A_REF, |
| 361 | "points to non-ref target '%s'", target)) |
| 362 | return -1; |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | int refs_fsck(struct ref_store *refs, struct fsck_options *o, |
| 368 | struct worktree *wt) |
| 369 | { |
| 370 | if (o->verbose) |
| 371 | fprintf_ln(stderr, _("Checking references consistency")); |
| 372 | |
| 373 | return refs->be->fsck(refs, o, wt); |
| 374 | } |
| 375 | |
| 376 | void sanitize_refname_component(const char *refname, struct strbuf *out) |
| 377 | { |
| 378 | if (check_or_sanitize_refname(refname, REFNAME_ALLOW_ONELEVEL, out)) |
| 379 | BUG("sanitizing refname '%s' check returned error", refname); |
| 380 | } |
| 381 | |
| 382 | int refname_is_safe(const char *refname) |
| 383 | { |
| 384 | const char *rest; |
| 385 | |
| 386 | if (skip_prefix(refname, "refs/", &rest)) { |
| 387 | char *buf; |
| 388 | int result; |
| 389 | size_t restlen = strlen(rest); |
| 390 | |
| 391 | /* rest must not be empty, or start or end with "/" */ |
| 392 | if (!restlen || *rest == '/' || rest[restlen - 1] == '/') |
| 393 | return 0; |
| 394 | |
| 395 | /* |
| 396 | * Does the refname try to escape refs/? |
| 397 | * For example: refs/foo/../bar is safe but refs/foo/../../bar |
| 398 | * is not. |
| 399 | */ |
| 400 | buf = xmallocz(restlen); |
| 401 | result = !normalize_path_copy(buf, rest) && !strcmp(buf, rest); |
| 402 | free(buf); |
| 403 | return result; |
| 404 | } |
| 405 | |
| 406 | do { |
| 407 | if (!isupper(*refname) && *refname != '_') |
| 408 | return 0; |
| 409 | refname++; |
| 410 | } while (*refname); |
| 411 | return 1; |
| 412 | } |
| 413 | |
| 414 | /* |
| 415 | * Return true if refname, which has the specified oid and flags, can |
| 416 | * be resolved to an object in the database. If the referred-to object |
| 417 | * does not exist, emit a warning and return false. |
| 418 | */ |
| 419 | int ref_resolves_to_object(const char *refname, |
| 420 | struct repository *repo, |
| 421 | const struct object_id *oid, |
| 422 | unsigned int flags) |
| 423 | { |
| 424 | if (flags & REF_ISBROKEN) |
| 425 | return 0; |
| 426 | if (!odb_has_object(repo->objects, oid, |
| 427 | ODB_HAS_OBJECT_RECHECK_PACKED | ODB_HAS_OBJECT_FETCH_PROMISOR)) { |
| 428 | error(_("%s does not point to a valid object!"), refname); |
| 429 | return 0; |
| 430 | } |
| 431 | return 1; |
| 432 | } |
| 433 | |
| 434 | char *refs_resolve_refdup(struct ref_store *refs, |
| 435 | const char *refname, int resolve_flags, |
| 436 | struct object_id *oid, int *flags) |
| 437 | { |
| 438 | const char *result; |
| 439 | |
| 440 | result = refs_resolve_ref_unsafe(refs, refname, resolve_flags, |
| 441 | oid, flags); |
| 442 | return xstrdup_or_null(result); |
| 443 | } |
| 444 | |
| 445 | /* The argument to for_each_filter_refs */ |
| 446 | struct for_each_ref_filter { |
| 447 | const char *pattern; |
| 448 | size_t trim_prefix; |
| 449 | refs_for_each_cb *fn; |
| 450 | void *cb_data; |
| 451 | }; |
| 452 | |
| 453 | int refs_read_ref_full(struct ref_store *refs, const char *refname, |
| 454 | int resolve_flags, struct object_id *oid, int *flags) |
| 455 | { |
| 456 | if (refs_resolve_ref_unsafe(refs, refname, resolve_flags, |
| 457 | oid, flags)) |
| 458 | return 0; |
| 459 | return -1; |
| 460 | } |
| 461 | |
| 462 | int refs_read_ref(struct ref_store *refs, const char *refname, struct object_id *oid) |
| 463 | { |
| 464 | return refs_read_ref_full(refs, refname, RESOLVE_REF_READING, oid, NULL); |
| 465 | } |
| 466 | |
| 467 | int refs_ref_exists(struct ref_store *refs, const char *refname) |
| 468 | { |
| 469 | return !!refs_resolve_ref_unsafe(refs, refname, RESOLVE_REF_READING, |
| 470 | NULL, NULL); |
| 471 | } |
| 472 | |
| 473 | static int for_each_filter_refs(const struct reference *ref, void *data) |
| 474 | { |
| 475 | struct for_each_ref_filter *filter = data; |
| 476 | |
| 477 | if (wildmatch(filter->pattern, ref->name, 0)) |
| 478 | return 0; |
| 479 | if (filter->trim_prefix) { |
| 480 | struct reference skipped = *ref; |
| 481 | if (strlen(skipped.name) <= filter->trim_prefix) |
| 482 | BUG("attempt to trim too many characters"); |
| 483 | skipped.name += filter->trim_prefix; |
| 484 | return filter->fn(&skipped, filter->cb_data); |
| 485 | } else { |
| 486 | return filter->fn(ref, filter->cb_data); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | struct warn_if_dangling_data { |
| 491 | struct ref_store *refs; |
| 492 | FILE *fp; |
| 493 | const struct string_list *refnames; |
| 494 | const char *indent; |
| 495 | int dry_run; |
| 496 | }; |
| 497 | |
| 498 | static int warn_if_dangling_symref(const struct reference *ref, void *cb_data) |
| 499 | { |
| 500 | struct warn_if_dangling_data *d = cb_data; |
| 501 | const char *resolves_to, *msg; |
| 502 | |
| 503 | if (!(ref->flags & REF_ISSYMREF)) |
| 504 | return 0; |
| 505 | |
| 506 | resolves_to = refs_resolve_ref_unsafe(d->refs, ref->name, 0, NULL, NULL); |
| 507 | if (!resolves_to |
| 508 | || !string_list_has_string(d->refnames, resolves_to)) { |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | msg = d->dry_run |
| 513 | ? _("%s%s will become dangling after %s is deleted\n") |
| 514 | : _("%s%s has become dangling after %s was deleted\n"); |
| 515 | fprintf(d->fp, msg, d->indent, ref->name, resolves_to); |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | void refs_warn_dangling_symrefs(struct ref_store *refs, FILE *fp, |
| 520 | const char *indent, int dry_run, |
| 521 | const struct string_list *refnames) |
| 522 | { |
| 523 | struct warn_if_dangling_data data = { |
| 524 | .refs = refs, |
| 525 | .fp = fp, |
| 526 | .refnames = refnames, |
| 527 | .indent = indent, |
| 528 | .dry_run = dry_run, |
| 529 | }; |
| 530 | struct refs_for_each_ref_options opts = { |
| 531 | .flags = REFS_FOR_EACH_INCLUDE_BROKEN, |
| 532 | }; |
| 533 | refs_for_each_ref_ext(refs, warn_if_dangling_symref, &data, &opts); |
| 534 | } |
| 535 | |
| 536 | int refs_for_each_tag_ref(struct ref_store *refs, refs_for_each_cb cb, void *cb_data) |
| 537 | { |
| 538 | struct refs_for_each_ref_options opts = { |
| 539 | .prefix = "refs/tags/", |
| 540 | .trim_prefix = strlen("refs/tags/"), |
| 541 | }; |
| 542 | return refs_for_each_ref_ext(refs, cb, cb_data, &opts); |
| 543 | } |
| 544 | |
| 545 | int refs_for_each_branch_ref(struct ref_store *refs, refs_for_each_cb cb, void *cb_data) |
| 546 | { |
| 547 | struct refs_for_each_ref_options opts = { |
| 548 | .prefix = "refs/heads/", |
| 549 | .trim_prefix = strlen("refs/heads/"), |
| 550 | }; |
| 551 | return refs_for_each_ref_ext(refs, cb, cb_data, &opts); |
| 552 | } |
| 553 | |
| 554 | int refs_for_each_remote_ref(struct ref_store *refs, refs_for_each_cb cb, void *cb_data) |
| 555 | { |
| 556 | struct refs_for_each_ref_options opts = { |
| 557 | .prefix = "refs/remotes/", |
| 558 | .trim_prefix = strlen("refs/remotes/"), |
| 559 | }; |
| 560 | return refs_for_each_ref_ext(refs, cb, cb_data, &opts); |
| 561 | } |
| 562 | |
| 563 | int refs_head_ref_namespaced(struct ref_store *refs, refs_for_each_cb fn, void *cb_data) |
| 564 | { |
| 565 | struct strbuf buf = STRBUF_INIT; |
| 566 | int ret = 0; |
| 567 | struct object_id oid; |
| 568 | int flag; |
| 569 | |
| 570 | strbuf_addf(&buf, "%sHEAD", get_git_namespace()); |
| 571 | if (!refs_read_ref_full(refs, buf.buf, RESOLVE_REF_READING, &oid, &flag)) { |
| 572 | struct reference ref = { |
| 573 | .name = buf.buf, |
| 574 | .oid = &oid, |
| 575 | .flags = flag, |
| 576 | }; |
| 577 | |
| 578 | ret = fn(&ref, cb_data); |
| 579 | } |
| 580 | strbuf_release(&buf); |
| 581 | |
| 582 | return ret; |
| 583 | } |
| 584 | |
| 585 | void normalize_glob_ref(struct string_list_item *item, const char *prefix, |
| 586 | const char *pattern) |
| 587 | { |
| 588 | struct strbuf normalized_pattern = STRBUF_INIT; |
| 589 | |
| 590 | if (*pattern == '/') |
| 591 | BUG("pattern must not start with '/'"); |
| 592 | |
| 593 | if (prefix) |
| 594 | strbuf_addstr(&normalized_pattern, prefix); |
| 595 | else if (!starts_with(pattern, "refs/") && |
| 596 | strcmp(pattern, "HEAD")) |
| 597 | strbuf_addstr(&normalized_pattern, "refs/"); |
| 598 | /* |
| 599 | * NEEDSWORK: Special case other symrefs such as REBASE_HEAD, |
| 600 | * MERGE_HEAD, etc. |
| 601 | */ |
| 602 | |
| 603 | strbuf_addstr(&normalized_pattern, pattern); |
| 604 | strbuf_strip_suffix(&normalized_pattern, "/"); |
| 605 | |
| 606 | item->string = strbuf_detach(&normalized_pattern, NULL); |
| 607 | item->util = has_glob_specials(pattern) ? NULL : item->string; |
| 608 | strbuf_release(&normalized_pattern); |
| 609 | } |
| 610 | |
| 611 | const char *prettify_refname(const char *name) |
| 612 | { |
| 613 | if (skip_prefix(name, "refs/heads/", &name) || |
| 614 | skip_prefix(name, "refs/tags/", &name) || |
| 615 | skip_prefix(name, "refs/remotes/", &name)) |
| 616 | ; /* nothing */ |
| 617 | return name; |
| 618 | } |
| 619 | |
| 620 | static const char *ref_rev_parse_rules[] = { |
| 621 | "%.*s", |
| 622 | "refs/%.*s", |
| 623 | "refs/tags/%.*s", |
| 624 | "refs/heads/%.*s", |
| 625 | "refs/remotes/%.*s", |
| 626 | "refs/remotes/%.*s/HEAD", |
| 627 | NULL |
| 628 | }; |
| 629 | |
| 630 | #define NUM_REV_PARSE_RULES (ARRAY_SIZE(ref_rev_parse_rules) - 1) |
| 631 | |
| 632 | /* |
| 633 | * Is it possible that the caller meant full_name with abbrev_name? |
| 634 | * If so return a non-zero value to signal "yes"; the magnitude of |
| 635 | * the returned value gives the precedence used for disambiguation. |
| 636 | * |
| 637 | * If abbrev_name cannot mean full_name, return 0. |
| 638 | */ |
| 639 | int refname_match(const char *abbrev_name, const char *full_name) |
| 640 | { |
| 641 | const char **p; |
| 642 | const int abbrev_name_len = strlen(abbrev_name); |
| 643 | const int num_rules = NUM_REV_PARSE_RULES; |
| 644 | |
| 645 | for (p = ref_rev_parse_rules; *p; p++) |
| 646 | if (!strcmp(full_name, mkpath(*p, abbrev_name_len, abbrev_name))) |
| 647 | return &ref_rev_parse_rules[num_rules] - p; |
| 648 | |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | /* |
| 653 | * Given a 'prefix' expand it by the rules in 'ref_rev_parse_rules' and add |
| 654 | * the results to 'prefixes' |
| 655 | */ |
| 656 | void expand_ref_prefix(struct strvec *prefixes, const char *prefix) |
| 657 | { |
| 658 | const char **p; |
| 659 | int len = strlen(prefix); |
| 660 | |
| 661 | for (p = ref_rev_parse_rules; *p; p++) |
| 662 | strvec_pushf(prefixes, *p, len, prefix); |
| 663 | } |
| 664 | |
| 665 | #ifndef WITH_BREAKING_CHANGES |
| 666 | static const char default_branch_name_advice[] = N_( |
| 667 | "Using '%s' as the name for the initial branch. This default branch name\n" |
| 668 | "will change to \"main\" in Git 3.0. To configure the initial branch name\n" |
| 669 | "to use in all of your new repositories, which will suppress this warning,\n" |
| 670 | "call:\n" |
| 671 | "\n" |
| 672 | "\tgit config --global init.defaultBranch <name>\n" |
| 673 | "\n" |
| 674 | "Names commonly chosen instead of 'master' are 'main', 'trunk' and\n" |
| 675 | "'development'. The just-created branch can be renamed via this command:\n" |
| 676 | "\n" |
| 677 | "\tgit branch -m <name>\n" |
| 678 | ); |
| 679 | #else |
| 680 | static const char default_branch_name_advice[] = N_( |
| 681 | "Using '%s' as the name for the initial branch since Git 3.0.\n" |
| 682 | "If you expected Git to create 'master', the just-created\n" |
| 683 | "branch can be renamed via this command:\n" |
| 684 | "\n" |
| 685 | "\tgit branch -m master\n" |
| 686 | ); |
| 687 | #endif /* WITH_BREAKING_CHANGES */ |
| 688 | |
| 689 | char *repo_default_branch_name(struct repository *r, int quiet) |
| 690 | { |
| 691 | const char *config_key = "init.defaultbranch"; |
| 692 | const char *config_display_key = "init.defaultBranch"; |
| 693 | char *ret = NULL, *full_ref; |
| 694 | const char *env = getenv("GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME"); |
| 695 | |
| 696 | if (env && *env) |
| 697 | ret = xstrdup(env); |
| 698 | if (!ret && repo_config_get_string(r, config_key, &ret) < 0) |
| 699 | die(_("could not retrieve `%s`"), config_display_key); |
| 700 | |
| 701 | if (!ret) { |
| 702 | #ifdef WITH_BREAKING_CHANGES |
| 703 | ret = xstrdup("main"); |
| 704 | #else |
| 705 | ret = xstrdup("master"); |
| 706 | #endif /* WITH_BREAKING_CHANGES */ |
| 707 | if (!quiet) |
| 708 | advise_if_enabled(ADVICE_DEFAULT_BRANCH_NAME, |
| 709 | _(default_branch_name_advice), ret); |
| 710 | } |
| 711 | |
| 712 | full_ref = xstrfmt("refs/heads/%s", ret); |
| 713 | if (check_refname_format(full_ref, 0)) |
| 714 | die(_("invalid branch name: %s = %s"), config_display_key, ret); |
| 715 | free(full_ref); |
| 716 | |
| 717 | return ret; |
| 718 | } |
| 719 | |
| 720 | /* |
| 721 | * *string and *len will only be substituted, and *string returned (for |
| 722 | * later free()ing) if the string passed in is a magic short-hand form |
| 723 | * to name a branch. |
| 724 | */ |
| 725 | static char *substitute_branch_name(struct repository *r, |
| 726 | const char **string, int *len, |
| 727 | int nonfatal_dangling_mark) |
| 728 | { |
| 729 | struct strbuf buf = STRBUF_INIT; |
| 730 | struct interpret_branch_name_options options = { |
| 731 | .nonfatal_dangling_mark = nonfatal_dangling_mark |
| 732 | }; |
| 733 | int ret = repo_interpret_branch_name(r, *string, *len, &buf, &options); |
| 734 | |
| 735 | if (ret == *len) { |
| 736 | size_t size; |
| 737 | *string = strbuf_detach(&buf, &size); |
| 738 | *len = size; |
| 739 | return (char *)*string; |
| 740 | } |
| 741 | |
| 742 | return NULL; |
| 743 | } |
| 744 | |
| 745 | void copy_branchname(struct repository *repo, |
| 746 | struct strbuf *sb, const char *name, |
| 747 | enum interpret_branch_kind allowed) |
| 748 | { |
| 749 | int len = strlen(name); |
| 750 | struct interpret_branch_name_options options = { |
| 751 | .allowed = allowed |
| 752 | }; |
| 753 | int used = repo_interpret_branch_name(repo, name, len, sb, |
| 754 | &options); |
| 755 | |
| 756 | if (used < 0) |
| 757 | used = 0; |
| 758 | strbuf_add(sb, name + used, len - used); |
| 759 | } |
| 760 | |
| 761 | int check_branch_ref(struct repository *repo, struct strbuf *sb, const char *name) |
| 762 | { |
| 763 | if (startup_info->have_repository) |
| 764 | copy_branchname(repo, sb, name, INTERPRET_BRANCH_LOCAL); |
| 765 | else |
| 766 | strbuf_addstr(sb, name); |
| 767 | |
| 768 | /* |
| 769 | * This splice must be done even if we end up rejecting the |
| 770 | * name; builtin/branch.c::copy_or_rename_branch() still wants |
| 771 | * to see what the name expanded to so that "branch -m" can be |
| 772 | * used as a tool to correct earlier mistakes. |
| 773 | */ |
| 774 | strbuf_splice(sb, 0, 0, "refs/heads/", 11); |
| 775 | |
| 776 | if (*name == '-' || |
| 777 | !strcmp(sb->buf, "refs/heads/HEAD")) |
| 778 | return -1; |
| 779 | |
| 780 | return check_refname_format(sb->buf, 0); |
| 781 | } |
| 782 | |
| 783 | int check_tag_ref(struct strbuf *sb, const char *name) |
| 784 | { |
| 785 | if (name[0] == '-' || !strcmp(name, "HEAD")) |
| 786 | return -1; |
| 787 | |
| 788 | strbuf_reset(sb); |
| 789 | strbuf_addf(sb, "refs/tags/%s", name); |
| 790 | |
| 791 | return check_refname_format(sb->buf, 0); |
| 792 | } |
| 793 | |
| 794 | int repo_dwim_ref(struct repository *r, const char *str, int len, |
| 795 | struct object_id *oid, char **ref, int nonfatal_dangling_mark) |
| 796 | { |
| 797 | char *last_branch = substitute_branch_name(r, &str, &len, |
| 798 | nonfatal_dangling_mark); |
| 799 | int refs_found = expand_ref(r, str, len, oid, ref); |
| 800 | free(last_branch); |
| 801 | return refs_found; |
| 802 | } |
| 803 | |
| 804 | int expand_ref(struct repository *repo, const char *str, int len, |
| 805 | struct object_id *oid, char **ref) |
| 806 | { |
| 807 | const char **p, *r; |
| 808 | int refs_found = 0; |
| 809 | struct strbuf fullref = STRBUF_INIT; |
| 810 | |
| 811 | *ref = NULL; |
| 812 | for (p = ref_rev_parse_rules; *p; p++) { |
| 813 | struct object_id oid_from_ref; |
| 814 | struct object_id *this_result; |
| 815 | int flag; |
| 816 | struct ref_store *refs = get_main_ref_store(repo); |
| 817 | |
| 818 | this_result = refs_found ? &oid_from_ref : oid; |
| 819 | strbuf_reset(&fullref); |
| 820 | strbuf_addf(&fullref, *p, len, str); |
| 821 | r = refs_resolve_ref_unsafe(refs, fullref.buf, |
| 822 | RESOLVE_REF_READING, |
| 823 | this_result, &flag); |
| 824 | if (r) { |
| 825 | if (!refs_found++) |
| 826 | *ref = xstrdup(r); |
| 827 | if (!repo_settings_get_warn_ambiguous_refs(repo)) |
| 828 | break; |
| 829 | } else if ((flag & REF_ISSYMREF) && strcmp(fullref.buf, "HEAD")) { |
| 830 | warning(_("ignoring dangling symref %s"), fullref.buf); |
| 831 | } else if ((flag & REF_ISBROKEN) && strchr(fullref.buf, '/')) { |
| 832 | warning(_("ignoring broken ref %s"), fullref.buf); |
| 833 | } |
| 834 | } |
| 835 | strbuf_release(&fullref); |
| 836 | return refs_found; |
| 837 | } |
| 838 | |
| 839 | int repo_dwim_log(struct repository *r, const char *str, int len, |
| 840 | struct object_id *oid, char **log) |
| 841 | { |
| 842 | struct ref_store *refs = get_main_ref_store(r); |
| 843 | char *last_branch = substitute_branch_name(r, &str, &len, 0); |
| 844 | const char **p; |
| 845 | int logs_found = 0; |
| 846 | struct strbuf path = STRBUF_INIT; |
| 847 | |
| 848 | *log = NULL; |
| 849 | for (p = ref_rev_parse_rules; *p; p++) { |
| 850 | struct object_id hash; |
| 851 | const char *ref, *it; |
| 852 | |
| 853 | strbuf_reset(&path); |
| 854 | strbuf_addf(&path, *p, len, str); |
| 855 | ref = refs_resolve_ref_unsafe(refs, path.buf, |
| 856 | RESOLVE_REF_READING, |
| 857 | oid ? &hash : NULL, NULL); |
| 858 | if (!ref) |
| 859 | continue; |
| 860 | if (refs_reflog_exists(refs, path.buf)) |
| 861 | it = path.buf; |
| 862 | else if (strcmp(ref, path.buf) && |
| 863 | refs_reflog_exists(refs, ref)) |
| 864 | it = ref; |
| 865 | else |
| 866 | continue; |
| 867 | if (!logs_found++) { |
| 868 | *log = xstrdup(it); |
| 869 | if (oid) |
| 870 | oidcpy(oid, &hash); |
| 871 | } |
| 872 | if (!repo_settings_get_warn_ambiguous_refs(r)) |
| 873 | break; |
| 874 | } |
| 875 | strbuf_release(&path); |
| 876 | free(last_branch); |
| 877 | return logs_found; |
| 878 | } |
| 879 | |
| 880 | int is_per_worktree_ref(const char *refname) |
| 881 | { |
| 882 | return starts_with(refname, "refs/worktree/") || |
| 883 | starts_with(refname, "refs/bisect/") || |
| 884 | starts_with(refname, "refs/rewritten/"); |
| 885 | } |
| 886 | |
| 887 | int is_pseudo_ref(const char *refname) |
| 888 | { |
| 889 | static const char * const pseudo_refs[] = { |
| 890 | "FETCH_HEAD", |
| 891 | "MERGE_HEAD", |
| 892 | }; |
| 893 | size_t i; |
| 894 | |
| 895 | for (i = 0; i < ARRAY_SIZE(pseudo_refs); i++) |
| 896 | if (!strcmp(refname, pseudo_refs[i])) |
| 897 | return 1; |
| 898 | |
| 899 | return 0; |
| 900 | } |
| 901 | |
| 902 | static int is_root_ref_syntax(const char *refname) |
| 903 | { |
| 904 | const char *c; |
| 905 | |
| 906 | for (c = refname; *c; c++) { |
| 907 | if (!isupper(*c) && *c != '-' && *c != '_') |
| 908 | return 0; |
| 909 | } |
| 910 | |
| 911 | return 1; |
| 912 | } |
| 913 | |
| 914 | int is_root_ref(const char *refname) |
| 915 | { |
| 916 | static const char *const irregular_root_refs[] = { |
| 917 | "HEAD", |
| 918 | "AUTO_MERGE", |
| 919 | "BISECT_EXPECTED_REV", |
| 920 | "NOTES_MERGE_PARTIAL", |
| 921 | "NOTES_MERGE_REF", |
| 922 | "MERGE_AUTOSTASH", |
| 923 | }; |
| 924 | size_t i; |
| 925 | |
| 926 | if (!is_root_ref_syntax(refname) || |
| 927 | is_pseudo_ref(refname)) |
| 928 | return 0; |
| 929 | |
| 930 | if (ends_with(refname, "_HEAD")) |
| 931 | return 1; |
| 932 | |
| 933 | for (i = 0; i < ARRAY_SIZE(irregular_root_refs); i++) |
| 934 | if (!strcmp(refname, irregular_root_refs[i])) |
| 935 | return 1; |
| 936 | |
| 937 | return 0; |
| 938 | } |
| 939 | |
| 940 | static int is_current_worktree_ref(const char *ref) { |
| 941 | return is_root_ref_syntax(ref) || is_per_worktree_ref(ref); |
| 942 | } |
| 943 | |
| 944 | enum ref_worktree_type parse_worktree_ref(const char *maybe_worktree_ref, |
| 945 | const char **worktree_name, int *worktree_name_length, |
| 946 | const char **bare_refname) |
| 947 | { |
| 948 | const char *name_dummy; |
| 949 | int name_length_dummy; |
| 950 | const char *ref_dummy; |
| 951 | |
| 952 | if (!worktree_name) |
| 953 | worktree_name = &name_dummy; |
| 954 | if (!worktree_name_length) |
| 955 | worktree_name_length = &name_length_dummy; |
| 956 | if (!bare_refname) |
| 957 | bare_refname = &ref_dummy; |
| 958 | |
| 959 | if (skip_prefix(maybe_worktree_ref, "worktrees/", bare_refname)) { |
| 960 | const char *slash = strchr(*bare_refname, '/'); |
| 961 | |
| 962 | *worktree_name = *bare_refname; |
| 963 | if (!slash) { |
| 964 | *worktree_name_length = strlen(*worktree_name); |
| 965 | |
| 966 | /* This is an error condition, and the caller tell because the bare_refname is "" */ |
| 967 | *bare_refname = *worktree_name + *worktree_name_length; |
| 968 | return REF_WORKTREE_OTHER; |
| 969 | } |
| 970 | |
| 971 | *worktree_name_length = slash - *bare_refname; |
| 972 | *bare_refname = slash + 1; |
| 973 | |
| 974 | if (is_current_worktree_ref(*bare_refname)) |
| 975 | return REF_WORKTREE_OTHER; |
| 976 | } |
| 977 | |
| 978 | *worktree_name = NULL; |
| 979 | *worktree_name_length = 0; |
| 980 | |
| 981 | if (skip_prefix(maybe_worktree_ref, "main-worktree/", bare_refname) |
| 982 | && is_current_worktree_ref(*bare_refname)) |
| 983 | return REF_WORKTREE_MAIN; |
| 984 | |
| 985 | *bare_refname = maybe_worktree_ref; |
| 986 | if (is_current_worktree_ref(maybe_worktree_ref)) |
| 987 | return REF_WORKTREE_CURRENT; |
| 988 | |
| 989 | return REF_WORKTREE_SHARED; |
| 990 | } |
| 991 | |
| 992 | long get_files_ref_lock_timeout_ms(struct repository *repo) |
| 993 | { |
| 994 | static int configured = 0; |
| 995 | |
| 996 | /* The default timeout is 100 ms: */ |
| 997 | static int timeout_ms = 100; |
| 998 | |
| 999 | if (!configured) { |
| 1000 | repo_config_get_int(repo, "core.filesreflocktimeout", &timeout_ms); |
| 1001 | configured = 1; |
| 1002 | } |
| 1003 | |
| 1004 | return timeout_ms; |
| 1005 | } |
| 1006 | |
| 1007 | int refs_delete_ref(struct ref_store *refs, const char *msg, |
| 1008 | const char *refname, |
| 1009 | const struct object_id *old_oid, |
| 1010 | unsigned int flags) |
| 1011 | { |
| 1012 | struct ref_transaction *transaction; |
| 1013 | struct strbuf err = STRBUF_INIT; |
| 1014 | |
| 1015 | transaction = ref_store_transaction_begin(refs, 0, &err); |
| 1016 | if (!transaction || |
| 1017 | ref_transaction_delete(transaction, refname, old_oid, |
| 1018 | NULL, flags, msg, &err) || |
| 1019 | ref_transaction_commit(transaction, &err)) { |
| 1020 | error("%s", err.buf); |
| 1021 | ref_transaction_free(transaction); |
| 1022 | strbuf_release(&err); |
| 1023 | return 1; |
| 1024 | } |
| 1025 | ref_transaction_free(transaction); |
| 1026 | strbuf_release(&err); |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
| 1030 | static void copy_reflog_msg(struct strbuf *sb, const char *msg) |
| 1031 | { |
| 1032 | char c; |
| 1033 | int wasspace = 1; |
| 1034 | |
| 1035 | while ((c = *msg++)) { |
| 1036 | if (wasspace && isspace(c)) |
| 1037 | continue; |
| 1038 | wasspace = isspace(c); |
| 1039 | if (wasspace) |
| 1040 | c = ' '; |
| 1041 | strbuf_addch(sb, c); |
| 1042 | } |
| 1043 | strbuf_rtrim(sb); |
| 1044 | } |
| 1045 | |
| 1046 | static char *normalize_reflog_message(const char *msg) |
| 1047 | { |
| 1048 | struct strbuf sb = STRBUF_INIT; |
| 1049 | |
| 1050 | if (msg && *msg) |
| 1051 | copy_reflog_msg(&sb, msg); |
| 1052 | return strbuf_detach(&sb, NULL); |
| 1053 | } |
| 1054 | |
| 1055 | enum log_refs_config refs_parse_log_all_ref_updates_config(const char *value) |
| 1056 | { |
| 1057 | if (value && !strcasecmp(value, "always")) |
| 1058 | return LOG_REFS_ALWAYS; |
| 1059 | else if (git_config_bool("core.logallrefupdates", value)) |
| 1060 | return LOG_REFS_NORMAL; |
| 1061 | return LOG_REFS_NONE; |
| 1062 | } |
| 1063 | |
| 1064 | int should_autocreate_reflog(enum log_refs_config log_all_ref_updates, |
| 1065 | const char *refname) |
| 1066 | { |
| 1067 | switch (log_all_ref_updates) { |
| 1068 | case LOG_REFS_ALWAYS: |
| 1069 | return 1; |
| 1070 | case LOG_REFS_NORMAL: |
| 1071 | return starts_with(refname, "refs/heads/") || |
| 1072 | starts_with(refname, "refs/remotes/") || |
| 1073 | starts_with(refname, "refs/notes/") || |
| 1074 | !strcmp(refname, "HEAD"); |
| 1075 | default: |
| 1076 | return 0; |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | int is_branch(const char *refname) |
| 1081 | { |
| 1082 | return !strcmp(refname, "HEAD") || starts_with(refname, "refs/heads/"); |
| 1083 | } |
| 1084 | |
| 1085 | struct read_ref_at_cb { |
| 1086 | timestamp_t at_time; |
| 1087 | int cnt; |
| 1088 | int reccnt; |
| 1089 | struct object_id *oid; |
| 1090 | int found_it; |
| 1091 | |
| 1092 | struct object_id ooid; |
| 1093 | struct object_id noid; |
| 1094 | int tz; |
| 1095 | timestamp_t date; |
| 1096 | char **msg; |
| 1097 | timestamp_t *cutoff_time; |
| 1098 | int *cutoff_tz; |
| 1099 | int *cutoff_cnt; |
| 1100 | }; |
| 1101 | |
| 1102 | static void set_read_ref_cutoffs(struct read_ref_at_cb *cb, |
| 1103 | timestamp_t timestamp, int tz, const char *message) |
| 1104 | { |
| 1105 | if (cb->msg) |
| 1106 | *cb->msg = xstrdup(message); |
| 1107 | if (cb->cutoff_time) |
| 1108 | *cb->cutoff_time = timestamp; |
| 1109 | if (cb->cutoff_tz) |
| 1110 | *cb->cutoff_tz = tz; |
| 1111 | if (cb->cutoff_cnt) |
| 1112 | *cb->cutoff_cnt = cb->reccnt; |
| 1113 | } |
| 1114 | |
| 1115 | static int read_ref_at_ent(const char *refname, |
| 1116 | struct object_id *ooid, struct object_id *noid, |
| 1117 | const char *email UNUSED, |
| 1118 | timestamp_t timestamp, int tz, |
| 1119 | const char *message, void *cb_data) |
| 1120 | { |
| 1121 | struct read_ref_at_cb *cb = cb_data; |
| 1122 | |
| 1123 | cb->tz = tz; |
| 1124 | cb->date = timestamp; |
| 1125 | |
| 1126 | if (timestamp <= cb->at_time || cb->cnt == 0) { |
| 1127 | set_read_ref_cutoffs(cb, timestamp, tz, message); |
| 1128 | /* |
| 1129 | * we have not yet updated cb->[n|o]oid so they still |
| 1130 | * hold the values for the previous record. |
| 1131 | */ |
| 1132 | if (!is_null_oid(&cb->ooid)) { |
| 1133 | oidcpy(cb->oid, noid); |
| 1134 | if (!oideq(&cb->ooid, noid)) |
| 1135 | warning(_("log for ref %s has gap after %s"), |
| 1136 | refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822))); |
| 1137 | } |
| 1138 | else if (cb->date == cb->at_time) |
| 1139 | oidcpy(cb->oid, noid); |
| 1140 | else if (!oideq(noid, cb->oid)) |
| 1141 | warning(_("log for ref %s unexpectedly ended on %s"), |
| 1142 | refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822))); |
| 1143 | cb->reccnt++; |
| 1144 | oidcpy(&cb->ooid, ooid); |
| 1145 | oidcpy(&cb->noid, noid); |
| 1146 | cb->found_it = 1; |
| 1147 | return 1; |
| 1148 | } |
| 1149 | cb->reccnt++; |
| 1150 | oidcpy(&cb->ooid, ooid); |
| 1151 | oidcpy(&cb->noid, noid); |
| 1152 | if (cb->cnt > 0) |
| 1153 | cb->cnt--; |
| 1154 | return 0; |
| 1155 | } |
| 1156 | |
| 1157 | static int read_ref_at_ent_oldest(const char *refname UNUSED, |
| 1158 | struct object_id *ooid, struct object_id *noid, |
| 1159 | const char *email UNUSED, |
| 1160 | timestamp_t timestamp, int tz, |
| 1161 | const char *message, void *cb_data) |
| 1162 | { |
| 1163 | struct read_ref_at_cb *cb = cb_data; |
| 1164 | |
| 1165 | set_read_ref_cutoffs(cb, timestamp, tz, message); |
| 1166 | oidcpy(cb->oid, ooid); |
| 1167 | if (cb->at_time && is_null_oid(cb->oid)) |
| 1168 | oidcpy(cb->oid, noid); |
| 1169 | /* We just want the first entry */ |
| 1170 | return 1; |
| 1171 | } |
| 1172 | |
| 1173 | int read_ref_at(struct ref_store *refs, const char *refname, |
| 1174 | unsigned int flags, timestamp_t at_time, int cnt, |
| 1175 | struct object_id *oid, char **msg, |
| 1176 | timestamp_t *cutoff_time, int *cutoff_tz, int *cutoff_cnt) |
| 1177 | { |
| 1178 | struct read_ref_at_cb cb; |
| 1179 | |
| 1180 | memset(&cb, 0, sizeof(cb)); |
| 1181 | cb.at_time = at_time; |
| 1182 | cb.cnt = cnt; |
| 1183 | cb.msg = msg; |
| 1184 | cb.cutoff_time = cutoff_time; |
| 1185 | cb.cutoff_tz = cutoff_tz; |
| 1186 | cb.cutoff_cnt = cutoff_cnt; |
| 1187 | cb.oid = oid; |
| 1188 | |
| 1189 | refs_for_each_reflog_ent_reverse(refs, refname, read_ref_at_ent, &cb); |
| 1190 | |
| 1191 | if (!cb.reccnt) { |
| 1192 | if (cnt == 0) { |
| 1193 | /* |
| 1194 | * The caller asked for ref@{0}, and we had no entries. |
| 1195 | * It's a bit subtle, but in practice all callers have |
| 1196 | * prepped the "oid" field with the current value of |
| 1197 | * the ref, which is the most reasonable fallback. |
| 1198 | * |
| 1199 | * We'll put dummy values into the out-parameters (so |
| 1200 | * they're not just uninitialized garbage), and the |
| 1201 | * caller can take our return value as a hint that |
| 1202 | * we did not find any such reflog. |
| 1203 | */ |
| 1204 | set_read_ref_cutoffs(&cb, 0, 0, "empty reflog"); |
| 1205 | return 1; |
| 1206 | } |
| 1207 | if (flags & GET_OID_QUIETLY) |
| 1208 | exit(128); |
| 1209 | else |
| 1210 | die(_("log for %s is empty"), refname); |
| 1211 | } |
| 1212 | if (cb.found_it) |
| 1213 | return 0; |
| 1214 | |
| 1215 | refs_for_each_reflog_ent(refs, refname, read_ref_at_ent_oldest, &cb); |
| 1216 | |
| 1217 | return 1; |
| 1218 | } |
| 1219 | |
| 1220 | struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs, |
| 1221 | unsigned int flags, |
| 1222 | struct strbuf *err) |
| 1223 | { |
| 1224 | struct ref_transaction *tr; |
| 1225 | assert(err); |
| 1226 | |
| 1227 | CALLOC_ARRAY(tr, 1); |
| 1228 | tr->ref_store = refs; |
| 1229 | tr->flags = flags; |
| 1230 | string_list_init_dup(&tr->refnames); |
| 1231 | |
| 1232 | if (flags & REF_TRANSACTION_ALLOW_FAILURE) |
| 1233 | CALLOC_ARRAY(tr->rejections, 1); |
| 1234 | |
| 1235 | return tr; |
| 1236 | } |
| 1237 | |
| 1238 | void ref_transaction_free(struct ref_transaction *transaction) |
| 1239 | { |
| 1240 | size_t i; |
| 1241 | |
| 1242 | if (!transaction) |
| 1243 | return; |
| 1244 | |
| 1245 | switch (transaction->state) { |
| 1246 | case REF_TRANSACTION_OPEN: |
| 1247 | case REF_TRANSACTION_CLOSED: |
| 1248 | /* OK */ |
| 1249 | break; |
| 1250 | case REF_TRANSACTION_PREPARED: |
| 1251 | BUG("free called on a prepared reference transaction"); |
| 1252 | break; |
| 1253 | default: |
| 1254 | BUG("unexpected reference transaction state"); |
| 1255 | break; |
| 1256 | } |
| 1257 | |
| 1258 | for (i = 0; i < transaction->nr; i++) { |
| 1259 | free(transaction->updates[i]->msg); |
| 1260 | free(transaction->updates[i]->committer_info); |
| 1261 | free((char *)transaction->updates[i]->new_target); |
| 1262 | free((char *)transaction->updates[i]->old_target); |
| 1263 | free((char *)transaction->updates[i]->rejection_details); |
| 1264 | free(transaction->updates[i]); |
| 1265 | } |
| 1266 | |
| 1267 | if (transaction->rejections) |
| 1268 | free(transaction->rejections->update_indices); |
| 1269 | free(transaction->rejections); |
| 1270 | |
| 1271 | string_list_clear(&transaction->refnames, 0); |
| 1272 | free(transaction->updates); |
| 1273 | free(transaction); |
| 1274 | } |
| 1275 | |
| 1276 | int ref_transaction_maybe_set_rejected(struct ref_transaction *transaction, |
| 1277 | size_t update_idx, |
| 1278 | enum ref_transaction_error err, |
| 1279 | struct strbuf *details) |
| 1280 | { |
| 1281 | if (update_idx >= transaction->nr) |
| 1282 | BUG("trying to set rejection on invalid update index"); |
| 1283 | |
| 1284 | if (!(transaction->flags & REF_TRANSACTION_ALLOW_FAILURE)) |
| 1285 | return 0; |
| 1286 | |
| 1287 | if (!transaction->rejections) |
| 1288 | BUG("transaction not initialized with failure support"); |
| 1289 | |
| 1290 | /* |
| 1291 | * Don't accept generic errors, since these errors are not user |
| 1292 | * input related. |
| 1293 | */ |
| 1294 | if (err == REF_TRANSACTION_ERROR_GENERIC) |
| 1295 | return 0; |
| 1296 | |
| 1297 | /* |
| 1298 | * Rejected refnames shouldn't be considered in the availability |
| 1299 | * checks, so remove them from the list. |
| 1300 | */ |
| 1301 | string_list_remove(&transaction->refnames, |
| 1302 | transaction->updates[update_idx]->refname, 0); |
| 1303 | |
| 1304 | transaction->updates[update_idx]->rejection_err = err; |
| 1305 | transaction->updates[update_idx]->rejection_details = strbuf_detach(details, NULL); |
| 1306 | ALLOC_GROW(transaction->rejections->update_indices, |
| 1307 | transaction->rejections->nr + 1, |
| 1308 | transaction->rejections->alloc); |
| 1309 | transaction->rejections->update_indices[transaction->rejections->nr++] = update_idx; |
| 1310 | |
| 1311 | return 1; |
| 1312 | } |
| 1313 | |
| 1314 | struct ref_update *ref_transaction_add_update( |
| 1315 | struct ref_transaction *transaction, |
| 1316 | const char *refname, unsigned int flags, |
| 1317 | const struct object_id *new_oid, |
| 1318 | const struct object_id *old_oid, |
| 1319 | const struct object_id *peeled, |
| 1320 | const char *new_target, const char *old_target, |
| 1321 | const char *committer_info, |
| 1322 | const char *msg) |
| 1323 | { |
| 1324 | struct string_list_item *item; |
| 1325 | struct ref_update *update; |
| 1326 | |
| 1327 | if (transaction->state != REF_TRANSACTION_OPEN) |
| 1328 | BUG("update called for transaction that is not open"); |
| 1329 | |
| 1330 | if (old_oid && old_target) |
| 1331 | BUG("only one of old_oid and old_target should be non NULL"); |
| 1332 | if (new_oid && new_target) |
| 1333 | BUG("only one of new_oid and new_target should be non NULL"); |
| 1334 | |
| 1335 | FLEX_ALLOC_STR(update, refname, refname); |
| 1336 | ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc); |
| 1337 | transaction->updates[transaction->nr++] = update; |
| 1338 | |
| 1339 | update->flags = flags; |
| 1340 | update->rejection_err = 0; |
| 1341 | |
| 1342 | update->new_target = xstrdup_or_null(new_target); |
| 1343 | update->old_target = xstrdup_or_null(old_target); |
| 1344 | if ((flags & REF_HAVE_NEW) && new_oid) |
| 1345 | oidcpy(&update->new_oid, new_oid); |
| 1346 | if ((flags & REF_HAVE_OLD) && old_oid) |
| 1347 | oidcpy(&update->old_oid, old_oid); |
| 1348 | if (!(flags & REF_SKIP_CREATE_REFLOG)) { |
| 1349 | update->committer_info = xstrdup_or_null(committer_info); |
| 1350 | update->msg = normalize_reflog_message(msg); |
| 1351 | } |
| 1352 | if (flags & REF_HAVE_PEELED) |
| 1353 | oidcpy(&update->peeled, peeled); |
| 1354 | |
| 1355 | /* |
| 1356 | * This list is generally used by the backends to avoid duplicates. |
| 1357 | * But we do support multiple log updates for a given refname within |
| 1358 | * a single transaction. |
| 1359 | */ |
| 1360 | if (!(update->flags & REF_LOG_ONLY)) { |
| 1361 | item = string_list_append(&transaction->refnames, refname); |
| 1362 | item->util = update; |
| 1363 | } |
| 1364 | |
| 1365 | return update; |
| 1366 | } |
| 1367 | |
| 1368 | static int transaction_refname_valid(const char *refname, |
| 1369 | const struct object_id *new_oid, |
| 1370 | unsigned int flags, struct strbuf *err) |
| 1371 | { |
| 1372 | if (flags & REF_SKIP_REFNAME_VERIFICATION) |
| 1373 | return 1; |
| 1374 | |
| 1375 | if (is_pseudo_ref(refname)) { |
| 1376 | const char *refusal_msg; |
| 1377 | if (flags & REF_LOG_ONLY) |
| 1378 | refusal_msg = _("refusing to update reflog for pseudoref '%s'"); |
| 1379 | else |
| 1380 | refusal_msg = _("refusing to update pseudoref '%s'"); |
| 1381 | strbuf_addf(err, refusal_msg, refname); |
| 1382 | return 0; |
| 1383 | } else if ((new_oid && !is_null_oid(new_oid)) ? |
| 1384 | check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) : |
| 1385 | !refname_is_safe(refname)) { |
| 1386 | const char *refusal_msg; |
| 1387 | if (flags & REF_LOG_ONLY) |
| 1388 | refusal_msg = _("refusing to update reflog with bad name '%s'"); |
| 1389 | else |
| 1390 | refusal_msg = _("refusing to update ref with bad name '%s'"); |
| 1391 | strbuf_addf(err, refusal_msg, refname); |
| 1392 | return 0; |
| 1393 | } |
| 1394 | |
| 1395 | return 1; |
| 1396 | } |
| 1397 | |
| 1398 | enum ref_transaction_error ref_transaction_update(struct ref_transaction *transaction, |
| 1399 | const char *refname, |
| 1400 | const struct object_id *new_oid, |
| 1401 | const struct object_id *old_oid, |
| 1402 | const char *new_target, |
| 1403 | const char *old_target, |
| 1404 | unsigned int flags, const char *msg, |
| 1405 | struct strbuf *err) |
| 1406 | { |
| 1407 | struct object_id peeled; |
| 1408 | |
| 1409 | assert(err); |
| 1410 | |
| 1411 | if ((flags & REF_FORCE_CREATE_REFLOG) && |
| 1412 | (flags & REF_SKIP_CREATE_REFLOG)) { |
| 1413 | strbuf_addstr(err, _("refusing to force and skip creation of reflog")); |
| 1414 | return REF_TRANSACTION_ERROR_GENERIC; |
| 1415 | } |
| 1416 | |
| 1417 | if (!transaction_refname_valid(refname, new_oid, flags, err)) |
| 1418 | return REF_TRANSACTION_ERROR_GENERIC; |
| 1419 | |
| 1420 | if (flags & ~REF_TRANSACTION_UPDATE_ALLOWED_FLAGS) |
| 1421 | BUG("illegal flags 0x%x passed to ref_transaction_update()", flags); |
| 1422 | |
| 1423 | /* |
| 1424 | * Clear flags outside the allowed set; this should be a noop because |
| 1425 | * of the BUG() check above, but it works around a -Wnonnull warning |
| 1426 | * with some versions of "gcc -O3". |
| 1427 | */ |
| 1428 | flags &= REF_TRANSACTION_UPDATE_ALLOWED_FLAGS; |
| 1429 | |
| 1430 | flags |= (new_oid ? REF_HAVE_NEW : 0) | (old_oid ? REF_HAVE_OLD : 0); |
| 1431 | flags |= (new_target ? REF_HAVE_NEW : 0) | (old_target ? REF_HAVE_OLD : 0); |
| 1432 | |
| 1433 | if ((flags & REF_HAVE_NEW) && !new_target && !is_null_oid(new_oid) && |
| 1434 | !(flags & REF_SKIP_OID_VERIFICATION) && !(flags & REF_LOG_ONLY)) { |
| 1435 | struct object *o = parse_object(transaction->ref_store->repo, new_oid); |
| 1436 | |
| 1437 | if (!o) { |
| 1438 | strbuf_addf(err, |
| 1439 | _("trying to write ref '%s' with nonexistent object %s"), |
| 1440 | refname, oid_to_hex(new_oid)); |
| 1441 | return REF_TRANSACTION_ERROR_INVALID_NEW_VALUE; |
| 1442 | } |
| 1443 | |
| 1444 | if (o->type != OBJ_COMMIT && is_branch(refname)) { |
| 1445 | strbuf_addf(err, _("trying to write non-commit object %s to branch '%s'"), |
| 1446 | oid_to_hex(new_oid), refname); |
| 1447 | return REF_TRANSACTION_ERROR_INVALID_NEW_VALUE; |
| 1448 | } |
| 1449 | |
| 1450 | if (o->type == OBJ_TAG) { |
| 1451 | if (!peel_object(transaction->ref_store->repo, new_oid, &peeled, |
| 1452 | PEEL_OBJECT_VERIFY_TAGGED_OBJECT_TYPE)) |
| 1453 | flags |= REF_HAVE_PEELED; |
| 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | ref_transaction_add_update(transaction, refname, flags, |
| 1458 | new_oid, old_oid, &peeled, new_target, |
| 1459 | old_target, NULL, msg); |
| 1460 | |
| 1461 | return 0; |
| 1462 | } |
| 1463 | |
| 1464 | int ref_transaction_update_reflog(struct ref_transaction *transaction, |
| 1465 | const char *refname, |
| 1466 | const struct object_id *new_oid, |
| 1467 | const struct object_id *old_oid, |
| 1468 | const char *committer_info, |
| 1469 | const char *msg, |
| 1470 | uint64_t index, |
| 1471 | struct strbuf *err) |
| 1472 | { |
| 1473 | struct ref_update *update; |
| 1474 | unsigned int flags; |
| 1475 | |
| 1476 | assert(err); |
| 1477 | |
| 1478 | flags = REF_HAVE_OLD | REF_HAVE_NEW | REF_LOG_ONLY | REF_FORCE_CREATE_REFLOG | REF_NO_DEREF | |
| 1479 | REF_LOG_USE_PROVIDED_OIDS; |
| 1480 | |
| 1481 | if (!transaction_refname_valid(refname, new_oid, flags, err)) |
| 1482 | return -1; |
| 1483 | |
| 1484 | update = ref_transaction_add_update(transaction, refname, flags, |
| 1485 | new_oid, old_oid, NULL, NULL, NULL, |
| 1486 | committer_info, msg); |
| 1487 | update->index = index; |
| 1488 | |
| 1489 | /* |
| 1490 | * Reference backends may need to know the max index to optimize |
| 1491 | * their writes. So we store the max_index on the transaction level. |
| 1492 | */ |
| 1493 | if (index > transaction->max_index) |
| 1494 | transaction->max_index = index; |
| 1495 | |
| 1496 | return 0; |
| 1497 | } |
| 1498 | |
| 1499 | int ref_transaction_create(struct ref_transaction *transaction, |
| 1500 | const char *refname, |
| 1501 | const struct object_id *new_oid, |
| 1502 | const char *new_target, |
| 1503 | unsigned int flags, const char *msg, |
| 1504 | struct strbuf *err) |
| 1505 | { |
| 1506 | if (new_oid && new_target) |
| 1507 | BUG("create called with both new_oid and new_target set"); |
| 1508 | if ((!new_oid || is_null_oid(new_oid)) && !new_target) { |
| 1509 | strbuf_addf(err, "'%s' has neither a valid OID nor a target", refname); |
| 1510 | return 1; |
| 1511 | } |
| 1512 | return ref_transaction_update(transaction, refname, new_oid, |
| 1513 | null_oid(transaction->ref_store->repo->hash_algo), new_target, NULL, flags, |
| 1514 | msg, err); |
| 1515 | } |
| 1516 | |
| 1517 | int ref_transaction_delete(struct ref_transaction *transaction, |
| 1518 | const char *refname, |
| 1519 | const struct object_id *old_oid, |
| 1520 | const char *old_target, |
| 1521 | unsigned int flags, |
| 1522 | const char *msg, |
| 1523 | struct strbuf *err) |
| 1524 | { |
| 1525 | if (old_oid && is_null_oid(old_oid)) |
| 1526 | BUG("delete called with old_oid set to zeros"); |
| 1527 | if (old_oid && old_target) |
| 1528 | BUG("delete called with both old_oid and old_target set"); |
| 1529 | if (old_target && !(flags & REF_NO_DEREF)) |
| 1530 | BUG("delete cannot operate on symrefs with deref mode"); |
| 1531 | return ref_transaction_update(transaction, refname, |
| 1532 | null_oid(transaction->ref_store->repo->hash_algo), old_oid, |
| 1533 | NULL, old_target, flags, |
| 1534 | msg, err); |
| 1535 | } |
| 1536 | |
| 1537 | int ref_transaction_verify(struct ref_transaction *transaction, |
| 1538 | const char *refname, |
| 1539 | const struct object_id *old_oid, |
| 1540 | const char *old_target, |
| 1541 | unsigned int flags, |
| 1542 | struct strbuf *err) |
| 1543 | { |
| 1544 | if (!old_target && !old_oid) |
| 1545 | BUG("verify called with old_oid and old_target set to NULL"); |
| 1546 | if (old_oid && old_target) |
| 1547 | BUG("verify called with both old_oid and old_target set"); |
| 1548 | if (old_target && !(flags & REF_NO_DEREF)) |
| 1549 | BUG("verify cannot operate on symrefs with deref mode"); |
| 1550 | return ref_transaction_update(transaction, refname, |
| 1551 | NULL, old_oid, |
| 1552 | NULL, old_target, |
| 1553 | flags, NULL, err); |
| 1554 | } |
| 1555 | |
| 1556 | int refs_update_ref(struct ref_store *refs, const char *msg, |
| 1557 | const char *refname, const struct object_id *new_oid, |
| 1558 | const struct object_id *old_oid, unsigned int flags, |
| 1559 | enum action_on_err onerr) |
| 1560 | { |
| 1561 | struct ref_transaction *t = NULL; |
| 1562 | struct strbuf err = STRBUF_INIT; |
| 1563 | int ret = 0; |
| 1564 | |
| 1565 | t = ref_store_transaction_begin(refs, 0, &err); |
| 1566 | if (!t || |
| 1567 | ref_transaction_update(t, refname, new_oid, old_oid, NULL, NULL, |
| 1568 | flags, msg, &err) || |
| 1569 | ref_transaction_commit(t, &err)) { |
| 1570 | ret = 1; |
| 1571 | ref_transaction_free(t); |
| 1572 | } |
| 1573 | if (ret) { |
| 1574 | const char *str = _("update_ref failed for ref '%s': %s"); |
| 1575 | |
| 1576 | switch (onerr) { |
| 1577 | case UPDATE_REFS_MSG_ON_ERR: |
| 1578 | error(str, refname, err.buf); |
| 1579 | break; |
| 1580 | case UPDATE_REFS_DIE_ON_ERR: |
| 1581 | die(str, refname, err.buf); |
| 1582 | break; |
| 1583 | case UPDATE_REFS_QUIET_ON_ERR: |
| 1584 | break; |
| 1585 | } |
| 1586 | strbuf_release(&err); |
| 1587 | return 1; |
| 1588 | } |
| 1589 | strbuf_release(&err); |
| 1590 | if (t) |
| 1591 | ref_transaction_free(t); |
| 1592 | return 0; |
| 1593 | } |
| 1594 | |
| 1595 | /* |
| 1596 | * Check that the string refname matches a rule of the form |
| 1597 | * "{prefix}%.*s{suffix}". So "foo/bar/baz" would match the rule |
| 1598 | * "foo/%.*s/baz", and return the string "bar". |
| 1599 | */ |
| 1600 | static const char *match_parse_rule(const char *refname, const char *rule, |
| 1601 | size_t *len) |
| 1602 | { |
| 1603 | /* |
| 1604 | * Check that rule matches refname up to the first percent in the rule. |
| 1605 | * We can bail immediately if not, but otherwise we leave "rule" at the |
| 1606 | * %-placeholder, and "refname" at the start of the potential matched |
| 1607 | * name. |
| 1608 | */ |
| 1609 | while (*rule != '%') { |
| 1610 | if (!*rule) |
| 1611 | BUG("rev-parse rule did not have percent"); |
| 1612 | if (*refname++ != *rule++) |
| 1613 | return NULL; |
| 1614 | } |
| 1615 | |
| 1616 | /* |
| 1617 | * Check that our "%" is the expected placeholder. This assumes there |
| 1618 | * are no other percents (placeholder or quoted) in the string, but |
| 1619 | * that is sufficient for our rev-parse rules. |
| 1620 | */ |
| 1621 | if (!skip_prefix(rule, "%.*s", &rule)) |
| 1622 | return NULL; |
| 1623 | |
| 1624 | /* |
| 1625 | * And now check that our suffix (if any) matches. |
| 1626 | */ |
| 1627 | if (!strip_suffix(refname, rule, len)) |
| 1628 | return NULL; |
| 1629 | |
| 1630 | return refname; /* len set by strip_suffix() */ |
| 1631 | } |
| 1632 | |
| 1633 | char *refs_shorten_unambiguous_ref(struct ref_store *refs, |
| 1634 | const char *refname, int strict) |
| 1635 | { |
| 1636 | int i; |
| 1637 | struct strbuf resolved_buf = STRBUF_INIT; |
| 1638 | |
| 1639 | /* skip first rule, it will always match */ |
| 1640 | for (i = NUM_REV_PARSE_RULES - 1; i > 0 ; --i) { |
| 1641 | int j; |
| 1642 | int rules_to_fail = i; |
| 1643 | const char *short_name; |
| 1644 | size_t short_name_len; |
| 1645 | |
| 1646 | short_name = match_parse_rule(refname, ref_rev_parse_rules[i], |
| 1647 | &short_name_len); |
| 1648 | if (!short_name) |
| 1649 | continue; |
| 1650 | |
| 1651 | /* |
| 1652 | * in strict mode, all (except the matched one) rules |
| 1653 | * must fail to resolve to a valid non-ambiguous ref |
| 1654 | */ |
| 1655 | if (strict) |
| 1656 | rules_to_fail = NUM_REV_PARSE_RULES; |
| 1657 | |
| 1658 | /* |
| 1659 | * check if the short name resolves to a valid ref, |
| 1660 | * but use only rules prior to the matched one |
| 1661 | */ |
| 1662 | for (j = 0; j < rules_to_fail; j++) { |
| 1663 | const char *rule = ref_rev_parse_rules[j]; |
| 1664 | |
| 1665 | /* skip matched rule */ |
| 1666 | if (i == j) |
| 1667 | continue; |
| 1668 | |
| 1669 | /* |
| 1670 | * the short name is ambiguous, if it resolves |
| 1671 | * (with this previous rule) to a valid ref |
| 1672 | * read_ref() returns 0 on success |
| 1673 | */ |
| 1674 | strbuf_reset(&resolved_buf); |
| 1675 | strbuf_addf(&resolved_buf, rule, |
| 1676 | cast_size_t_to_int(short_name_len), |
| 1677 | short_name); |
| 1678 | if (refs_ref_exists(refs, resolved_buf.buf)) |
| 1679 | break; |
| 1680 | } |
| 1681 | |
| 1682 | /* |
| 1683 | * short name is non-ambiguous if all previous rules |
| 1684 | * haven't resolved to a valid ref |
| 1685 | */ |
| 1686 | if (j == rules_to_fail) { |
| 1687 | strbuf_release(&resolved_buf); |
| 1688 | return xmemdupz(short_name, short_name_len); |
| 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | strbuf_release(&resolved_buf); |
| 1693 | return xstrdup(refname); |
| 1694 | } |
| 1695 | |
| 1696 | int parse_hide_refs_config(const char *var, const char *value, const char *section, |
| 1697 | struct strvec *hide_refs) |
| 1698 | { |
| 1699 | const char *key; |
| 1700 | if (!strcmp("transfer.hiderefs", var) || |
| 1701 | (!parse_config_key(var, section, NULL, NULL, &key) && |
| 1702 | !strcmp(key, "hiderefs"))) { |
| 1703 | char *ref; |
| 1704 | int len; |
| 1705 | |
| 1706 | if (!value) |
| 1707 | return config_error_nonbool(var); |
| 1708 | |
| 1709 | /* drop const to remove trailing '/' characters */ |
| 1710 | ref = (char *)strvec_push(hide_refs, value); |
| 1711 | len = strlen(ref); |
| 1712 | while (len && ref[len - 1] == '/') |
| 1713 | ref[--len] = '\0'; |
| 1714 | } |
| 1715 | return 0; |
| 1716 | } |
| 1717 | |
| 1718 | int ref_is_hidden(const char *refname, const char *refname_full, |
| 1719 | const struct strvec *hide_refs) |
| 1720 | { |
| 1721 | int i; |
| 1722 | |
| 1723 | for (i = hide_refs->nr - 1; i >= 0; i--) { |
| 1724 | const char *match = hide_refs->v[i]; |
| 1725 | const char *subject; |
| 1726 | int neg = 0; |
| 1727 | const char *p; |
| 1728 | |
| 1729 | if (*match == '!') { |
| 1730 | neg = 1; |
| 1731 | match++; |
| 1732 | } |
| 1733 | |
| 1734 | if (*match == '^') { |
| 1735 | subject = refname_full; |
| 1736 | match++; |
| 1737 | } else { |
| 1738 | subject = refname; |
| 1739 | } |
| 1740 | |
| 1741 | /* refname can be NULL when namespaces are used. */ |
| 1742 | if (subject && |
| 1743 | skip_prefix(subject, match, &p) && |
| 1744 | (!*p || *p == '/')) |
| 1745 | return !neg; |
| 1746 | } |
| 1747 | return 0; |
| 1748 | } |
| 1749 | |
| 1750 | const char **hidden_refs_to_excludes(const struct strvec *hide_refs) |
| 1751 | { |
| 1752 | const char **pattern; |
| 1753 | for (pattern = hide_refs->v; *pattern; pattern++) { |
| 1754 | /* |
| 1755 | * We can't feed any excludes from hidden refs config |
| 1756 | * sections, since later rules may override previous |
| 1757 | * ones. For example, with rules "refs/foo" and |
| 1758 | * "!refs/foo/bar", we should show "refs/foo/bar" (and |
| 1759 | * everything underneath it), but the earlier exclusion |
| 1760 | * would cause us to skip all of "refs/foo". We |
| 1761 | * likewise don't implement the namespace stripping |
| 1762 | * required for '^' rules. |
| 1763 | * |
| 1764 | * Both are possible to do, but complicated, so avoid |
| 1765 | * populating the jump list at all if we see either of |
| 1766 | * these patterns. |
| 1767 | */ |
| 1768 | if (**pattern == '!' || **pattern == '^') |
| 1769 | return NULL; |
| 1770 | } |
| 1771 | return hide_refs->v; |
| 1772 | } |
| 1773 | |
| 1774 | const char **get_namespaced_exclude_patterns(const char **exclude_patterns, |
| 1775 | const char *namespace, |
| 1776 | struct strvec *out) |
| 1777 | { |
| 1778 | if (!namespace || !*namespace || !exclude_patterns || !*exclude_patterns) |
| 1779 | return exclude_patterns; |
| 1780 | |
| 1781 | for (size_t i = 0; exclude_patterns[i]; i++) |
| 1782 | strvec_pushf(out, "%s%s", namespace, exclude_patterns[i]); |
| 1783 | |
| 1784 | return out->v; |
| 1785 | } |
| 1786 | |
| 1787 | const char *find_descendant_ref(const char *dirname, |
| 1788 | const struct string_list *extras, |
| 1789 | const struct string_list *skip) |
| 1790 | { |
| 1791 | if (!extras) |
| 1792 | return NULL; |
| 1793 | |
| 1794 | /* |
| 1795 | * Look at the place where dirname would be inserted into |
| 1796 | * extras. If there is an entry at that position that starts |
| 1797 | * with dirname (remember, dirname includes the trailing |
| 1798 | * slash) and is not in skip, then we have a conflict. |
| 1799 | */ |
| 1800 | for (size_t pos = string_list_find_insert_index(extras, dirname, NULL); |
| 1801 | pos < extras->nr; pos++) { |
| 1802 | const char *extra_refname = extras->items[pos].string; |
| 1803 | |
| 1804 | if (!starts_with(extra_refname, dirname)) |
| 1805 | break; |
| 1806 | |
| 1807 | if (!skip || !string_list_has_string(skip, extra_refname)) |
| 1808 | return extra_refname; |
| 1809 | } |
| 1810 | return NULL; |
| 1811 | } |
| 1812 | |
| 1813 | int refs_head_ref(struct ref_store *refs, refs_for_each_cb fn, void *cb_data) |
| 1814 | { |
| 1815 | struct object_id oid; |
| 1816 | int flag; |
| 1817 | |
| 1818 | if (refs_resolve_ref_unsafe(refs, "HEAD", RESOLVE_REF_READING, |
| 1819 | &oid, &flag)) { |
| 1820 | struct reference ref = { |
| 1821 | .name = "HEAD", |
| 1822 | .oid = &oid, |
| 1823 | .flags = flag, |
| 1824 | }; |
| 1825 | |
| 1826 | return fn(&ref, cb_data); |
| 1827 | } |
| 1828 | |
| 1829 | return 0; |
| 1830 | } |
| 1831 | |
| 1832 | struct ref_iterator *refs_ref_iterator_begin( |
| 1833 | struct ref_store *refs, |
| 1834 | const char *prefix, |
| 1835 | const char **exclude_patterns, |
| 1836 | int trim, |
| 1837 | enum refs_for_each_flag flags) |
| 1838 | { |
| 1839 | struct ref_iterator *iter; |
| 1840 | struct strvec normalized_exclude_patterns = STRVEC_INIT; |
| 1841 | |
| 1842 | if (exclude_patterns) { |
| 1843 | for (size_t i = 0; exclude_patterns[i]; i++) { |
| 1844 | const char *pattern = exclude_patterns[i]; |
| 1845 | size_t len = strlen(pattern); |
| 1846 | if (!len) |
| 1847 | continue; |
| 1848 | |
| 1849 | if (pattern[len - 1] == '/') |
| 1850 | strvec_push(&normalized_exclude_patterns, pattern); |
| 1851 | else |
| 1852 | strvec_pushf(&normalized_exclude_patterns, "%s/", |
| 1853 | pattern); |
| 1854 | } |
| 1855 | |
| 1856 | exclude_patterns = normalized_exclude_patterns.v; |
| 1857 | } |
| 1858 | |
| 1859 | if (!(flags & REFS_FOR_EACH_INCLUDE_BROKEN)) { |
| 1860 | static int ref_paranoia = -1; |
| 1861 | |
| 1862 | if (ref_paranoia < 0) |
| 1863 | ref_paranoia = git_env_bool("GIT_REF_PARANOIA", 1); |
| 1864 | if (ref_paranoia) { |
| 1865 | flags |= REFS_FOR_EACH_INCLUDE_BROKEN; |
| 1866 | flags |= REFS_FOR_EACH_OMIT_DANGLING_SYMREFS; |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | iter = refs->be->iterator_begin(refs, prefix, exclude_patterns, flags); |
| 1871 | /* |
| 1872 | * `iterator_begin()` already takes care of prefix, but we |
| 1873 | * might need to do some trimming: |
| 1874 | */ |
| 1875 | if (trim) |
| 1876 | iter = prefix_ref_iterator_begin(iter, "", trim); |
| 1877 | |
| 1878 | strvec_clear(&normalized_exclude_patterns); |
| 1879 | |
| 1880 | return iter; |
| 1881 | } |
| 1882 | |
| 1883 | int refs_for_each_ref_ext(struct ref_store *refs, |
| 1884 | refs_for_each_cb cb, void *cb_data, |
| 1885 | const struct refs_for_each_ref_options *opts) |
| 1886 | { |
| 1887 | struct strvec namespaced_exclude_patterns = STRVEC_INIT; |
| 1888 | struct strbuf namespaced_prefix = STRBUF_INIT; |
| 1889 | struct strbuf real_pattern = STRBUF_INIT; |
| 1890 | struct for_each_ref_filter filter; |
| 1891 | struct ref_iterator *iter; |
| 1892 | size_t trim_prefix = opts->trim_prefix; |
| 1893 | const char **exclude_patterns; |
| 1894 | const char *prefix; |
| 1895 | int ret; |
| 1896 | |
| 1897 | if (!refs) |
| 1898 | BUG("no ref store passed"); |
| 1899 | |
| 1900 | if (opts->trim_prefix) { |
| 1901 | size_t prefix_len; |
| 1902 | |
| 1903 | if (!opts->prefix) |
| 1904 | BUG("trimming only allowed with a prefix"); |
| 1905 | |
| 1906 | prefix_len = strlen(opts->prefix); |
| 1907 | if (prefix_len == opts->trim_prefix && opts->prefix[prefix_len - 1] != '/') |
| 1908 | BUG("ref pattern must end in a trailing slash when trimming"); |
| 1909 | } |
| 1910 | |
| 1911 | if (opts->pattern) { |
| 1912 | if (!opts->prefix && !starts_with(opts->pattern, "refs/")) |
| 1913 | strbuf_addstr(&real_pattern, "refs/"); |
| 1914 | else if (opts->prefix) |
| 1915 | strbuf_addstr(&real_pattern, opts->prefix); |
| 1916 | strbuf_addstr(&real_pattern, opts->pattern); |
| 1917 | |
| 1918 | if (!has_glob_specials(opts->pattern)) { |
| 1919 | /* Append implied '/' '*' if not present. */ |
| 1920 | strbuf_complete(&real_pattern, '/'); |
| 1921 | /* No need to check for '*', there is none. */ |
| 1922 | strbuf_addch(&real_pattern, '*'); |
| 1923 | } |
| 1924 | |
| 1925 | filter.pattern = real_pattern.buf; |
| 1926 | filter.trim_prefix = opts->trim_prefix; |
| 1927 | filter.fn = cb; |
| 1928 | filter.cb_data = cb_data; |
| 1929 | |
| 1930 | /* |
| 1931 | * We need to trim the prefix in the callback function as the |
| 1932 | * pattern is expected to match on the full refname. |
| 1933 | */ |
| 1934 | trim_prefix = 0; |
| 1935 | |
| 1936 | cb = for_each_filter_refs; |
| 1937 | cb_data = &filter; |
| 1938 | } |
| 1939 | |
| 1940 | if (opts->namespace) { |
| 1941 | strbuf_addstr(&namespaced_prefix, opts->namespace); |
| 1942 | if (opts->prefix) |
| 1943 | strbuf_addstr(&namespaced_prefix, opts->prefix); |
| 1944 | else |
| 1945 | strbuf_addstr(&namespaced_prefix, "refs/"); |
| 1946 | |
| 1947 | prefix = namespaced_prefix.buf; |
| 1948 | exclude_patterns = get_namespaced_exclude_patterns(opts->exclude_patterns, |
| 1949 | opts->namespace, |
| 1950 | &namespaced_exclude_patterns); |
| 1951 | } else { |
| 1952 | prefix = opts->prefix ? opts->prefix : ""; |
| 1953 | exclude_patterns = opts->exclude_patterns; |
| 1954 | } |
| 1955 | |
| 1956 | iter = refs_ref_iterator_begin(refs, prefix, exclude_patterns, |
| 1957 | trim_prefix, opts->flags); |
| 1958 | |
| 1959 | ret = do_for_each_ref_iterator(iter, cb, cb_data); |
| 1960 | |
| 1961 | strvec_clear(&namespaced_exclude_patterns); |
| 1962 | strbuf_release(&namespaced_prefix); |
| 1963 | strbuf_release(&real_pattern); |
| 1964 | return ret; |
| 1965 | } |
| 1966 | |
| 1967 | int refs_for_each_ref(struct ref_store *refs, refs_for_each_cb cb, void *cb_data) |
| 1968 | { |
| 1969 | struct refs_for_each_ref_options opts = { 0 }; |
| 1970 | return refs_for_each_ref_ext(refs, cb, cb_data, &opts); |
| 1971 | } |
| 1972 | |
| 1973 | int refs_for_each_replace_ref(struct ref_store *refs, refs_for_each_cb cb, void *cb_data) |
| 1974 | { |
| 1975 | const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref; |
| 1976 | struct refs_for_each_ref_options opts = { |
| 1977 | .prefix = git_replace_ref_base, |
| 1978 | .trim_prefix = strlen(git_replace_ref_base), |
| 1979 | .flags = REFS_FOR_EACH_INCLUDE_BROKEN, |
| 1980 | }; |
| 1981 | return refs_for_each_ref_ext(refs, cb, cb_data, &opts); |
| 1982 | } |
| 1983 | |
| 1984 | static int qsort_strcmp(const void *va, const void *vb) |
| 1985 | { |
| 1986 | const char *a = *(const char **)va; |
| 1987 | const char *b = *(const char **)vb; |
| 1988 | |
| 1989 | return strcmp(a, b); |
| 1990 | } |
| 1991 | |
| 1992 | static void find_longest_prefixes_1(struct string_list *out, |
| 1993 | struct strbuf *prefix, |
| 1994 | const char **patterns, size_t nr) |
| 1995 | { |
| 1996 | size_t i; |
| 1997 | |
| 1998 | for (i = 0; i < nr; i++) { |
| 1999 | char c = patterns[i][prefix->len]; |
| 2000 | if (!c || is_glob_special(c)) { |
| 2001 | string_list_append(out, prefix->buf); |
| 2002 | return; |
| 2003 | } |
| 2004 | } |
| 2005 | |
| 2006 | i = 0; |
| 2007 | while (i < nr) { |
| 2008 | size_t end; |
| 2009 | |
| 2010 | /* |
| 2011 | * Set "end" to the index of the element _after_ the last one |
| 2012 | * in our group. |
| 2013 | */ |
| 2014 | for (end = i + 1; end < nr; end++) { |
| 2015 | if (patterns[i][prefix->len] != patterns[end][prefix->len]) |
| 2016 | break; |
| 2017 | } |
| 2018 | |
| 2019 | strbuf_addch(prefix, patterns[i][prefix->len]); |
| 2020 | find_longest_prefixes_1(out, prefix, patterns + i, end - i); |
| 2021 | strbuf_setlen(prefix, prefix->len - 1); |
| 2022 | |
| 2023 | i = end; |
| 2024 | } |
| 2025 | } |
| 2026 | |
| 2027 | static void find_longest_prefixes(struct string_list *out, |
| 2028 | const char **patterns) |
| 2029 | { |
| 2030 | struct strvec sorted = STRVEC_INIT; |
| 2031 | struct strbuf prefix = STRBUF_INIT; |
| 2032 | |
| 2033 | strvec_pushv(&sorted, patterns); |
| 2034 | QSORT(sorted.v, sorted.nr, qsort_strcmp); |
| 2035 | |
| 2036 | find_longest_prefixes_1(out, &prefix, sorted.v, sorted.nr); |
| 2037 | |
| 2038 | strvec_clear(&sorted); |
| 2039 | strbuf_release(&prefix); |
| 2040 | } |
| 2041 | |
| 2042 | int refs_for_each_ref_in_prefixes(struct ref_store *ref_store, |
| 2043 | const char **prefixes, |
| 2044 | const struct refs_for_each_ref_options *opts, |
| 2045 | refs_for_each_cb cb, void *cb_data) |
| 2046 | { |
| 2047 | struct string_list longest_prefixes = STRING_LIST_INIT_DUP; |
| 2048 | struct string_list_item *prefix; |
| 2049 | int ret = 0; |
| 2050 | |
| 2051 | if (opts->prefix) |
| 2052 | BUG("refs_for_each_ref_in_prefixes called with specific prefix"); |
| 2053 | |
| 2054 | find_longest_prefixes(&longest_prefixes, prefixes); |
| 2055 | |
| 2056 | for_each_string_list_item(prefix, &longest_prefixes) { |
| 2057 | struct refs_for_each_ref_options prefix_opts = *opts; |
| 2058 | prefix_opts.prefix = prefix->string; |
| 2059 | |
| 2060 | ret = refs_for_each_ref_ext(ref_store, cb, cb_data, |
| 2061 | &prefix_opts); |
| 2062 | if (ret) |
| 2063 | break; |
| 2064 | } |
| 2065 | |
| 2066 | string_list_clear(&longest_prefixes, 0); |
| 2067 | return ret; |
| 2068 | } |
| 2069 | |
| 2070 | static int refs_read_special_head(struct ref_store *ref_store, |
| 2071 | const char *refname, struct object_id *oid, |
| 2072 | struct strbuf *referent, unsigned int *type, |
| 2073 | int *failure_errno) |
| 2074 | { |
| 2075 | struct strbuf full_path = STRBUF_INIT; |
| 2076 | struct strbuf content = STRBUF_INIT; |
| 2077 | int result = -1; |
| 2078 | strbuf_addf(&full_path, "%s/%s", ref_store->gitdir, refname); |
| 2079 | |
| 2080 | if (strbuf_read_file(&content, full_path.buf, 0) < 0) { |
| 2081 | *failure_errno = errno; |
| 2082 | goto done; |
| 2083 | } |
| 2084 | |
| 2085 | result = parse_loose_ref_contents(ref_store->repo->hash_algo, content.buf, |
| 2086 | oid, referent, type, NULL, failure_errno); |
| 2087 | |
| 2088 | done: |
| 2089 | strbuf_release(&full_path); |
| 2090 | strbuf_release(&content); |
| 2091 | return result; |
| 2092 | } |
| 2093 | |
| 2094 | int refs_read_raw_ref(struct ref_store *ref_store, const char *refname, |
| 2095 | struct object_id *oid, struct strbuf *referent, |
| 2096 | unsigned int *type, int *failure_errno) |
| 2097 | { |
| 2098 | assert(failure_errno); |
| 2099 | if (is_pseudo_ref(refname)) |
| 2100 | return refs_read_special_head(ref_store, refname, oid, referent, |
| 2101 | type, failure_errno); |
| 2102 | |
| 2103 | return ref_store->be->read_raw_ref(ref_store, refname, oid, referent, |
| 2104 | type, failure_errno); |
| 2105 | } |
| 2106 | |
| 2107 | int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname, |
| 2108 | struct strbuf *referent) |
| 2109 | { |
| 2110 | return ref_store->be->read_symbolic_ref(ref_store, refname, referent); |
| 2111 | } |
| 2112 | |
| 2113 | const char *refs_resolve_ref_unsafe(struct ref_store *refs, |
| 2114 | const char *refname, |
| 2115 | int resolve_flags, |
| 2116 | struct object_id *oid, |
| 2117 | int *flags) |
| 2118 | { |
| 2119 | static struct strbuf sb_refname = STRBUF_INIT; |
| 2120 | struct object_id unused_oid; |
| 2121 | int unused_flags; |
| 2122 | int symref_count; |
| 2123 | |
| 2124 | if (!oid) |
| 2125 | oid = &unused_oid; |
| 2126 | if (!flags) |
| 2127 | flags = &unused_flags; |
| 2128 | |
| 2129 | *flags = 0; |
| 2130 | |
| 2131 | if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { |
| 2132 | if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) || |
| 2133 | !refname_is_safe(refname)) |
| 2134 | return NULL; |
| 2135 | |
| 2136 | /* |
| 2137 | * repo_dwim_ref() uses REF_ISBROKEN to distinguish between |
| 2138 | * missing refs and refs that were present but invalid, |
| 2139 | * to complain about the latter to stderr. |
| 2140 | * |
| 2141 | * We don't know whether the ref exists, so don't set |
| 2142 | * REF_ISBROKEN yet. |
| 2143 | */ |
| 2144 | *flags |= REF_BAD_NAME; |
| 2145 | } |
| 2146 | |
| 2147 | for (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) { |
| 2148 | unsigned int read_flags = 0; |
| 2149 | int failure_errno; |
| 2150 | |
| 2151 | if (refs_read_raw_ref(refs, refname, oid, &sb_refname, |
| 2152 | &read_flags, &failure_errno)) { |
| 2153 | *flags |= read_flags; |
| 2154 | |
| 2155 | /* In reading mode, refs must eventually resolve */ |
| 2156 | if (resolve_flags & RESOLVE_REF_READING) |
| 2157 | return NULL; |
| 2158 | |
| 2159 | /* |
| 2160 | * Otherwise a missing ref is OK. But the files backend |
| 2161 | * may show errors besides ENOENT if there are |
| 2162 | * similarly-named refs. |
| 2163 | */ |
| 2164 | if (failure_errno != ENOENT && |
| 2165 | failure_errno != EISDIR && |
| 2166 | failure_errno != ENOTDIR) |
| 2167 | return NULL; |
| 2168 | |
| 2169 | oidclr(oid, refs->repo->hash_algo); |
| 2170 | if (*flags & REF_BAD_NAME) |
| 2171 | *flags |= REF_ISBROKEN; |
| 2172 | return refname; |
| 2173 | } |
| 2174 | |
| 2175 | *flags |= read_flags; |
| 2176 | |
| 2177 | if (!(read_flags & REF_ISSYMREF)) { |
| 2178 | if (*flags & REF_BAD_NAME) { |
| 2179 | oidclr(oid, refs->repo->hash_algo); |
| 2180 | *flags |= REF_ISBROKEN; |
| 2181 | } |
| 2182 | return refname; |
| 2183 | } |
| 2184 | |
| 2185 | refname = sb_refname.buf; |
| 2186 | if (resolve_flags & RESOLVE_REF_NO_RECURSE) { |
| 2187 | oidclr(oid, refs->repo->hash_algo); |
| 2188 | return refname; |
| 2189 | } |
| 2190 | if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { |
| 2191 | if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) || |
| 2192 | !refname_is_safe(refname)) |
| 2193 | return NULL; |
| 2194 | |
| 2195 | *flags |= REF_ISBROKEN | REF_BAD_NAME; |
| 2196 | } |
| 2197 | } |
| 2198 | |
| 2199 | return NULL; |
| 2200 | } |
| 2201 | |
| 2202 | void refs_create_refdir_stubs(struct repository *repo, const char *refdir, |
| 2203 | const char *refs_heads_content) |
| 2204 | { |
| 2205 | struct strbuf path = STRBUF_INIT; |
| 2206 | |
| 2207 | strbuf_addf(&path, "%s/HEAD", refdir); |
| 2208 | write_file(path.buf, "ref: refs/heads/.invalid"); |
| 2209 | adjust_shared_perm(repo, path.buf); |
| 2210 | |
| 2211 | strbuf_reset(&path); |
| 2212 | strbuf_addf(&path, "%s/refs", refdir); |
| 2213 | safe_create_dir(repo, path.buf, 1); |
| 2214 | |
| 2215 | if (refs_heads_content) { |
| 2216 | strbuf_reset(&path); |
| 2217 | strbuf_addf(&path, "%s/refs/heads", refdir); |
| 2218 | write_file(path.buf, "%s", refs_heads_content); |
| 2219 | adjust_shared_perm(repo, path.buf); |
| 2220 | } |
| 2221 | |
| 2222 | strbuf_release(&path); |
| 2223 | } |
| 2224 | |
| 2225 | /* backend functions */ |
| 2226 | int ref_store_create_on_disk(struct ref_store *refs, int flags, struct strbuf *err) |
| 2227 | { |
| 2228 | int ret = refs->be->create_on_disk(refs, flags, err); |
| 2229 | |
| 2230 | if (!ret) { |
| 2231 | /* Creation of stubs for linked worktrees are handled in the worktree code. */ |
| 2232 | if (!(flags & REF_STORE_CREATE_ON_DISK_IS_WORKTREE) && refs->repo->ref_storage_payload) { |
| 2233 | refs_create_refdir_stubs(refs->repo, refs->repo->gitdir, |
| 2234 | "repository uses alternate refs storage"); |
| 2235 | } else if (ref_storage_format_by_name(refs->be->name) != REF_STORAGE_FORMAT_FILES) { |
| 2236 | struct strbuf msg = STRBUF_INIT; |
| 2237 | strbuf_addf(&msg, "this repository uses the %s format", refs->be->name); |
| 2238 | refs_create_refdir_stubs(refs->repo, refs->gitdir, msg.buf); |
| 2239 | strbuf_release(&msg); |
| 2240 | } |
| 2241 | } |
| 2242 | |
| 2243 | return ret; |
| 2244 | } |
| 2245 | |
| 2246 | int ref_store_remove_on_disk(struct ref_store *refs, struct strbuf *err) |
| 2247 | { |
| 2248 | int ret = refs->be->remove_on_disk(refs, err); |
| 2249 | |
| 2250 | if (!ret) { |
| 2251 | enum ref_storage_format format = ref_storage_format_by_name(refs->be->name); |
| 2252 | struct strbuf sb = STRBUF_INIT; |
| 2253 | |
| 2254 | /* Backends apart from the files backend create stubs. */ |
| 2255 | if (format == REF_STORAGE_FORMAT_FILES) |
| 2256 | return ret; |
| 2257 | |
| 2258 | /* Alternate refs backend require stubs in the gitdir. */ |
| 2259 | if (refs->repo->ref_storage_payload) |
| 2260 | return ret; |
| 2261 | |
| 2262 | strbuf_addf(&sb, "%s/HEAD", refs->gitdir); |
| 2263 | if (unlink(sb.buf) < 0) { |
| 2264 | strbuf_addf(err, "could not delete stub HEAD: %s", |
| 2265 | strerror(errno)); |
| 2266 | ret = -1; |
| 2267 | } |
| 2268 | strbuf_reset(&sb); |
| 2269 | |
| 2270 | strbuf_addf(&sb, "%s/refs/heads", refs->gitdir); |
| 2271 | if (unlink(sb.buf) < 0) { |
| 2272 | strbuf_addf(err, "could not delete stub heads: %s", |
| 2273 | strerror(errno)); |
| 2274 | ret = -1; |
| 2275 | } |
| 2276 | strbuf_reset(&sb); |
| 2277 | |
| 2278 | strbuf_addf(&sb, "%s/refs", refs->gitdir); |
| 2279 | if (rmdir(sb.buf) < 0) { |
| 2280 | strbuf_addf(err, "could not delete refs directory: %s", |
| 2281 | strerror(errno)); |
| 2282 | ret = -1; |
| 2283 | } |
| 2284 | |
| 2285 | strbuf_release(&sb); |
| 2286 | } |
| 2287 | |
| 2288 | return ret; |
| 2289 | } |
| 2290 | |
| 2291 | int repo_resolve_gitlink_ref(struct repository *r, |
| 2292 | const char *submodule, const char *refname, |
| 2293 | struct object_id *oid) |
| 2294 | { |
| 2295 | struct ref_store *refs; |
| 2296 | int flags; |
| 2297 | |
| 2298 | refs = repo_get_submodule_ref_store(r, submodule); |
| 2299 | if (!refs) |
| 2300 | return -1; |
| 2301 | |
| 2302 | if (!refs_resolve_ref_unsafe(refs, refname, 0, oid, &flags) || |
| 2303 | is_null_oid(oid)) |
| 2304 | return -1; |
| 2305 | return 0; |
| 2306 | } |
| 2307 | |
| 2308 | /* |
| 2309 | * Look up a ref store by name. If that ref_store hasn't been |
| 2310 | * registered yet, return NULL. |
| 2311 | */ |
| 2312 | static struct ref_store *lookup_ref_store_map(struct strmap *map, |
| 2313 | const char *name) |
| 2314 | { |
| 2315 | struct strmap_entry *entry; |
| 2316 | |
| 2317 | if (!map->map.tablesize) |
| 2318 | /* It's initialized on demand in register_ref_store(). */ |
| 2319 | return NULL; |
| 2320 | |
| 2321 | entry = strmap_get_entry(map, name); |
| 2322 | return entry ? entry->value : NULL; |
| 2323 | } |
| 2324 | |
| 2325 | /* |
| 2326 | * Create, record, and return a ref_store instance for the specified |
| 2327 | * gitdir using the given ref storage format. |
| 2328 | */ |
| 2329 | static struct ref_store *ref_store_init(struct repository *repo, |
| 2330 | enum ref_storage_format format, |
| 2331 | const char *gitdir, |
| 2332 | unsigned int flags) |
| 2333 | { |
| 2334 | const struct ref_storage_be *be; |
| 2335 | struct ref_store *refs; |
| 2336 | struct ref_store_init_options opts = { |
| 2337 | .access_flags = flags, |
| 2338 | }; |
| 2339 | |
| 2340 | be = find_ref_storage_backend(format); |
| 2341 | if (!be) |
| 2342 | BUG("reference backend is unknown"); |
| 2343 | |
| 2344 | /* |
| 2345 | * TODO Send in a 'struct worktree' instead of a 'gitdir', and |
| 2346 | * allow the backend to handle how it wants to deal with worktrees. |
| 2347 | */ |
| 2348 | refs = be->init(repo, repo->ref_storage_payload, gitdir, &opts); |
| 2349 | |
| 2350 | return refs; |
| 2351 | } |
| 2352 | |
| 2353 | void ref_store_release(struct ref_store *ref_store) |
| 2354 | { |
| 2355 | ref_store->be->release(ref_store); |
| 2356 | free(ref_store->gitdir); |
| 2357 | } |
| 2358 | |
| 2359 | struct ref_store *get_main_ref_store(struct repository *r) |
| 2360 | { |
| 2361 | static bool initializing; |
| 2362 | |
| 2363 | if (r->refs_private) |
| 2364 | return r->refs_private; |
| 2365 | |
| 2366 | if (!r->gitdir) |
| 2367 | BUG("attempting to get main_ref_store outside of repository"); |
| 2368 | if (initializing) |
| 2369 | BUG("initialization of main ref store is recursing"); |
| 2370 | |
| 2371 | initializing = true; |
| 2372 | r->refs_private = ref_store_init(r, r->ref_storage_format, |
| 2373 | r->gitdir, REF_STORE_ALL_CAPS); |
| 2374 | r->refs_private = maybe_debug_wrap_ref_store(r->gitdir, r->refs_private); |
| 2375 | initializing = false; |
| 2376 | |
| 2377 | return r->refs_private; |
| 2378 | } |
| 2379 | |
| 2380 | /* |
| 2381 | * Associate a ref store with a name. It is a fatal error to call this |
| 2382 | * function twice for the same name. |
| 2383 | */ |
| 2384 | static void register_ref_store_map(struct strmap *map, |
| 2385 | const char *type, |
| 2386 | struct ref_store *refs, |
| 2387 | const char *name) |
| 2388 | { |
| 2389 | if (!map->map.tablesize) |
| 2390 | strmap_init(map); |
| 2391 | if (strmap_put(map, name, refs)) |
| 2392 | BUG("%s ref_store '%s' initialized twice", type, name); |
| 2393 | } |
| 2394 | |
| 2395 | struct ref_store *repo_get_submodule_ref_store(struct repository *repo, |
| 2396 | const char *submodule) |
| 2397 | { |
| 2398 | struct strbuf submodule_sb = STRBUF_INIT; |
| 2399 | struct ref_store *refs; |
| 2400 | char *to_free = NULL; |
| 2401 | size_t len; |
| 2402 | struct repository *subrepo; |
| 2403 | |
| 2404 | if (!submodule) |
| 2405 | return NULL; |
| 2406 | |
| 2407 | len = strlen(submodule); |
| 2408 | while (len && is_dir_sep(submodule[len - 1])) |
| 2409 | len--; |
| 2410 | if (!len) |
| 2411 | return NULL; |
| 2412 | |
| 2413 | if (submodule[len]) |
| 2414 | /* We need to strip off one or more trailing slashes */ |
| 2415 | submodule = to_free = xmemdupz(submodule, len); |
| 2416 | |
| 2417 | refs = lookup_ref_store_map(&repo->submodule_ref_stores, submodule); |
| 2418 | if (refs) |
| 2419 | goto done; |
| 2420 | |
| 2421 | strbuf_addstr(&submodule_sb, submodule); |
| 2422 | if (!is_nonbare_repository_dir(&submodule_sb)) |
| 2423 | goto done; |
| 2424 | |
| 2425 | if (submodule_to_gitdir(repo, &submodule_sb, submodule)) |
| 2426 | goto done; |
| 2427 | |
| 2428 | subrepo = xmalloc(sizeof(*subrepo)); |
| 2429 | |
| 2430 | if (repo_submodule_init(subrepo, repo, submodule, |
| 2431 | null_oid(repo->hash_algo))) { |
| 2432 | free(subrepo); |
| 2433 | goto done; |
| 2434 | } |
| 2435 | refs = ref_store_init(subrepo, subrepo->ref_storage_format, |
| 2436 | submodule_sb.buf, |
| 2437 | REF_STORE_READ | REF_STORE_ODB); |
| 2438 | register_ref_store_map(&repo->submodule_ref_stores, "submodule", |
| 2439 | refs, submodule); |
| 2440 | |
| 2441 | done: |
| 2442 | strbuf_release(&submodule_sb); |
| 2443 | free(to_free); |
| 2444 | |
| 2445 | return refs; |
| 2446 | } |
| 2447 | |
| 2448 | struct ref_store *get_worktree_ref_store(const struct worktree *wt) |
| 2449 | { |
| 2450 | struct ref_store *refs; |
| 2451 | const char *id; |
| 2452 | |
| 2453 | if (wt->is_current) |
| 2454 | return get_main_ref_store(wt->repo); |
| 2455 | |
| 2456 | id = wt->id ? wt->id : "/"; |
| 2457 | refs = lookup_ref_store_map(&wt->repo->worktree_ref_stores, id); |
| 2458 | if (refs) |
| 2459 | return refs; |
| 2460 | |
| 2461 | if (wt->id) { |
| 2462 | struct strbuf common_path = STRBUF_INIT; |
| 2463 | repo_common_path_append(wt->repo, &common_path, |
| 2464 | "worktrees/%s", wt->id); |
| 2465 | refs = ref_store_init(wt->repo, wt->repo->ref_storage_format, |
| 2466 | common_path.buf, REF_STORE_ALL_CAPS); |
| 2467 | strbuf_release(&common_path); |
| 2468 | } else { |
| 2469 | refs = ref_store_init(wt->repo, wt->repo->ref_storage_format, |
| 2470 | wt->repo->commondir, REF_STORE_ALL_CAPS); |
| 2471 | } |
| 2472 | |
| 2473 | if (refs) |
| 2474 | register_ref_store_map(&wt->repo->worktree_ref_stores, |
| 2475 | "worktree", refs, id); |
| 2476 | |
| 2477 | return refs; |
| 2478 | } |
| 2479 | |
| 2480 | void base_ref_store_init(struct ref_store *refs, struct repository *repo, |
| 2481 | const char *path, const struct ref_storage_be *be) |
| 2482 | { |
| 2483 | refs->be = be; |
| 2484 | refs->repo = repo; |
| 2485 | refs->gitdir = xstrdup(path); |
| 2486 | } |
| 2487 | |
| 2488 | int refs_optimize(struct ref_store *refs, struct refs_optimize_opts *opts) |
| 2489 | { |
| 2490 | return refs->be->optimize(refs, opts); |
| 2491 | } |
| 2492 | |
| 2493 | int refs_optimize_required(struct ref_store *refs, |
| 2494 | struct refs_optimize_opts *opts, |
| 2495 | bool *required) |
| 2496 | { |
| 2497 | return refs->be->optimize_required(refs, opts, required); |
| 2498 | } |
| 2499 | |
| 2500 | int reference_get_peeled_oid(struct repository *repo, |
| 2501 | const struct reference *ref, |
| 2502 | struct object_id *peeled_oid) |
| 2503 | { |
| 2504 | if (ref->peeled_oid) { |
| 2505 | oidcpy(peeled_oid, ref->peeled_oid); |
| 2506 | return 0; |
| 2507 | } |
| 2508 | |
| 2509 | return peel_object(repo, ref->oid, peeled_oid, 0) ? -1 : 0; |
| 2510 | } |
| 2511 | |
| 2512 | int refs_update_symref(struct ref_store *refs, const char *ref, |
| 2513 | const char *target, const char *logmsg) |
| 2514 | { |
| 2515 | return refs_update_symref_extended(refs, ref, target, logmsg, NULL, 0); |
| 2516 | } |
| 2517 | |
| 2518 | int refs_update_symref_extended(struct ref_store *refs, const char *ref, |
| 2519 | const char *target, const char *logmsg, |
| 2520 | struct strbuf *referent, int create_only) |
| 2521 | { |
| 2522 | struct ref_transaction *transaction; |
| 2523 | struct strbuf err = STRBUF_INIT; |
| 2524 | int ret = 0, prepret = 0; |
| 2525 | |
| 2526 | transaction = ref_store_transaction_begin(refs, 0, &err); |
| 2527 | if (!transaction) { |
| 2528 | error_return: |
| 2529 | ret = error("%s", err.buf); |
| 2530 | goto cleanup; |
| 2531 | } |
| 2532 | if (create_only) { |
| 2533 | if (ref_transaction_create(transaction, ref, NULL, target, |
| 2534 | REF_NO_DEREF, logmsg, &err)) |
| 2535 | goto error_return; |
| 2536 | prepret = ref_transaction_prepare(transaction, &err); |
| 2537 | if (prepret && prepret != REF_TRANSACTION_ERROR_CREATE_EXISTS) |
| 2538 | goto error_return; |
| 2539 | } else { |
| 2540 | if (ref_transaction_update(transaction, ref, NULL, NULL, |
| 2541 | target, NULL, REF_NO_DEREF, |
| 2542 | logmsg, &err) || |
| 2543 | ref_transaction_prepare(transaction, &err)) |
| 2544 | goto error_return; |
| 2545 | } |
| 2546 | |
| 2547 | if (referent && refs_read_symbolic_ref(refs, ref, referent) == NOT_A_SYMREF) { |
| 2548 | struct object_id oid; |
| 2549 | if (!refs_read_ref(refs, ref, &oid)) { |
| 2550 | strbuf_add_oid_hex(referent, &oid); |
| 2551 | ret = NOT_A_SYMREF; |
| 2552 | } |
| 2553 | } |
| 2554 | |
| 2555 | if (prepret == REF_TRANSACTION_ERROR_CREATE_EXISTS) |
| 2556 | goto cleanup; |
| 2557 | |
| 2558 | if (ref_transaction_commit(transaction, &err)) |
| 2559 | goto error_return; |
| 2560 | |
| 2561 | cleanup: |
| 2562 | strbuf_release(&err); |
| 2563 | if (transaction) |
| 2564 | ref_transaction_free(transaction); |
| 2565 | |
| 2566 | return ret; |
| 2567 | } |
| 2568 | |
| 2569 | /* |
| 2570 | * Write an error to `err` and return a nonzero value iff the same |
| 2571 | * refname appears multiple times in `refnames`. `refnames` must be |
| 2572 | * sorted on entry to this function. |
| 2573 | */ |
| 2574 | static int ref_update_reject_duplicates(struct string_list *refnames, |
| 2575 | struct strbuf *err) |
| 2576 | { |
| 2577 | size_t i, n = refnames->nr; |
| 2578 | |
| 2579 | assert(err); |
| 2580 | |
| 2581 | for (i = 1; i < n; i++) { |
| 2582 | int cmp = strcmp(refnames->items[i - 1].string, |
| 2583 | refnames->items[i].string); |
| 2584 | |
| 2585 | if (!cmp) { |
| 2586 | strbuf_addf(err, |
| 2587 | _("multiple updates for ref '%s' not allowed"), |
| 2588 | refnames->items[i].string); |
| 2589 | return 1; |
| 2590 | } else if (cmp > 0) { |
| 2591 | BUG("ref_update_reject_duplicates() received unsorted list"); |
| 2592 | } |
| 2593 | } |
| 2594 | return 0; |
| 2595 | } |
| 2596 | |
| 2597 | struct transaction_feed_cb_data { |
| 2598 | size_t index; |
| 2599 | struct strbuf buf; |
| 2600 | }; |
| 2601 | |
| 2602 | static int transaction_hook_feed_stdin(int hook_stdin_fd, void *pp_cb, void *pp_task_cb) |
| 2603 | { |
| 2604 | struct hook_cb_data *hook_cb = pp_cb; |
| 2605 | struct ref_transaction *transaction = hook_cb->options->feed_pipe_ctx; |
| 2606 | struct transaction_feed_cb_data *feed_cb_data = pp_task_cb; |
| 2607 | struct strbuf *buf = &feed_cb_data->buf; |
| 2608 | struct ref_update *update; |
| 2609 | size_t i = feed_cb_data->index++; |
| 2610 | int ret; |
| 2611 | |
| 2612 | if (i >= transaction->nr) |
| 2613 | return 1; /* No more refs to process */ |
| 2614 | |
| 2615 | update = transaction->updates[i]; |
| 2616 | |
| 2617 | if (update->flags & REF_LOG_ONLY) |
| 2618 | return 0; |
| 2619 | |
| 2620 | strbuf_reset(buf); |
| 2621 | |
| 2622 | if (!(update->flags & REF_HAVE_OLD)) |
| 2623 | strbuf_addf(buf, "%s ", oid_to_hex(null_oid(transaction->ref_store->repo->hash_algo))); |
| 2624 | else if (update->old_target) |
| 2625 | strbuf_addf(buf, "ref:%s ", update->old_target); |
| 2626 | else |
| 2627 | strbuf_addf(buf, "%s ", oid_to_hex(&update->old_oid)); |
| 2628 | |
| 2629 | if (!(update->flags & REF_HAVE_NEW)) |
| 2630 | strbuf_addf(buf, "%s ", oid_to_hex(null_oid(transaction->ref_store->repo->hash_algo))); |
| 2631 | else if (update->new_target) |
| 2632 | strbuf_addf(buf, "ref:%s ", update->new_target); |
| 2633 | else |
| 2634 | strbuf_addf(buf, "%s ", oid_to_hex(&update->new_oid)); |
| 2635 | |
| 2636 | strbuf_addf(buf, "%s\n", update->refname); |
| 2637 | |
| 2638 | ret = write_in_full(hook_stdin_fd, buf->buf, buf->len); |
| 2639 | if (ret < 0 && errno != EPIPE) |
| 2640 | return ret; |
| 2641 | |
| 2642 | return 0; /* no more input to feed */ |
| 2643 | } |
| 2644 | |
| 2645 | static void *transaction_feed_cb_data_alloc(void *feed_pipe_ctx UNUSED) |
| 2646 | { |
| 2647 | struct transaction_feed_cb_data *data; |
| 2648 | CALLOC_ARRAY(data, 1); |
| 2649 | strbuf_init(&data->buf, 0); |
| 2650 | data->index = 0; |
| 2651 | return data; |
| 2652 | } |
| 2653 | |
| 2654 | static void transaction_feed_cb_data_free(void *data) |
| 2655 | { |
| 2656 | struct transaction_feed_cb_data *d = data; |
| 2657 | if (!d) |
| 2658 | return; |
| 2659 | strbuf_release(&d->buf); |
| 2660 | free(d); |
| 2661 | } |
| 2662 | |
| 2663 | static int run_transaction_hook(struct ref_transaction *transaction, |
| 2664 | const char *state) |
| 2665 | { |
| 2666 | struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; |
| 2667 | int ret = 0; |
| 2668 | |
| 2669 | strvec_push(&opt.args, state); |
| 2670 | |
| 2671 | opt.feed_pipe = transaction_hook_feed_stdin; |
| 2672 | opt.feed_pipe_ctx = transaction; |
| 2673 | opt.feed_pipe_cb_data_alloc = transaction_feed_cb_data_alloc; |
| 2674 | opt.feed_pipe_cb_data_free = transaction_feed_cb_data_free; |
| 2675 | |
| 2676 | ret = run_hooks_opt(transaction->ref_store->repo, "reference-transaction", &opt); |
| 2677 | |
| 2678 | return ret; |
| 2679 | } |
| 2680 | |
| 2681 | int ref_transaction_prepare(struct ref_transaction *transaction, |
| 2682 | struct strbuf *err) |
| 2683 | { |
| 2684 | struct ref_store *refs = transaction->ref_store; |
| 2685 | int ret; |
| 2686 | |
| 2687 | switch (transaction->state) { |
| 2688 | case REF_TRANSACTION_OPEN: |
| 2689 | /* Good. */ |
| 2690 | break; |
| 2691 | case REF_TRANSACTION_PREPARED: |
| 2692 | BUG("prepare called twice on reference transaction"); |
| 2693 | break; |
| 2694 | case REF_TRANSACTION_CLOSED: |
| 2695 | BUG("prepare called on a closed reference transaction"); |
| 2696 | break; |
| 2697 | default: |
| 2698 | BUG("unexpected reference transaction state"); |
| 2699 | break; |
| 2700 | } |
| 2701 | |
| 2702 | if (refs->repo->disable_ref_updates) { |
| 2703 | strbuf_addstr(err, |
| 2704 | _("ref updates forbidden inside quarantine environment")); |
| 2705 | return -1; |
| 2706 | } |
| 2707 | |
| 2708 | string_list_sort(&transaction->refnames); |
| 2709 | if (ref_update_reject_duplicates(&transaction->refnames, err)) |
| 2710 | return REF_TRANSACTION_ERROR_GENERIC; |
| 2711 | |
| 2712 | /* Preparing checks before locking references */ |
| 2713 | ret = run_transaction_hook(transaction, "preparing"); |
| 2714 | if (ret) { |
| 2715 | ref_transaction_abort(transaction, err); |
| 2716 | die(_(abort_by_ref_transaction_hook), "preparing"); |
| 2717 | } |
| 2718 | |
| 2719 | ret = refs->be->transaction_prepare(refs, transaction, err); |
| 2720 | if (ret) |
| 2721 | return ret; |
| 2722 | |
| 2723 | ret = run_transaction_hook(transaction, "prepared"); |
| 2724 | if (ret) { |
| 2725 | ref_transaction_abort(transaction, err); |
| 2726 | die(_(abort_by_ref_transaction_hook), "prepared"); |
| 2727 | } |
| 2728 | |
| 2729 | return 0; |
| 2730 | } |
| 2731 | |
| 2732 | int ref_transaction_abort(struct ref_transaction *transaction, |
| 2733 | struct strbuf *err) |
| 2734 | { |
| 2735 | struct ref_store *refs = transaction->ref_store; |
| 2736 | int ret = 0; |
| 2737 | |
| 2738 | switch (transaction->state) { |
| 2739 | case REF_TRANSACTION_OPEN: |
| 2740 | /* No need to abort explicitly. */ |
| 2741 | break; |
| 2742 | case REF_TRANSACTION_PREPARED: |
| 2743 | ret = refs->be->transaction_abort(refs, transaction, err); |
| 2744 | break; |
| 2745 | case REF_TRANSACTION_CLOSED: |
| 2746 | BUG("abort called on a closed reference transaction"); |
| 2747 | break; |
| 2748 | default: |
| 2749 | BUG("unexpected reference transaction state"); |
| 2750 | break; |
| 2751 | } |
| 2752 | |
| 2753 | run_transaction_hook(transaction, "aborted"); |
| 2754 | |
| 2755 | ref_transaction_free(transaction); |
| 2756 | return ret; |
| 2757 | } |
| 2758 | |
| 2759 | int ref_transaction_commit(struct ref_transaction *transaction, |
| 2760 | struct strbuf *err) |
| 2761 | { |
| 2762 | struct ref_store *refs = transaction->ref_store; |
| 2763 | int ret; |
| 2764 | |
| 2765 | switch (transaction->state) { |
| 2766 | case REF_TRANSACTION_OPEN: |
| 2767 | /* Need to prepare first. */ |
| 2768 | ret = ref_transaction_prepare(transaction, err); |
| 2769 | if (ret) |
| 2770 | return ret; |
| 2771 | break; |
| 2772 | case REF_TRANSACTION_PREPARED: |
| 2773 | /* Fall through to finish. */ |
| 2774 | break; |
| 2775 | case REF_TRANSACTION_CLOSED: |
| 2776 | BUG("commit called on a closed reference transaction"); |
| 2777 | break; |
| 2778 | default: |
| 2779 | BUG("unexpected reference transaction state"); |
| 2780 | break; |
| 2781 | } |
| 2782 | |
| 2783 | ret = refs->be->transaction_finish(refs, transaction, err); |
| 2784 | if (!ret && !(transaction->flags & REF_TRANSACTION_FLAG_INITIAL)) |
| 2785 | run_transaction_hook(transaction, "committed"); |
| 2786 | return ret; |
| 2787 | } |
| 2788 | |
| 2789 | enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs, |
| 2790 | const struct string_list *refnames, |
| 2791 | const struct string_list *extras, |
| 2792 | const struct string_list *skip, |
| 2793 | struct ref_transaction *transaction, |
| 2794 | unsigned int initial_transaction, |
| 2795 | struct strbuf *err) |
| 2796 | { |
| 2797 | struct strbuf dirname = STRBUF_INIT; |
| 2798 | struct strbuf referent = STRBUF_INIT; |
| 2799 | struct string_list_item *item; |
| 2800 | struct ref_iterator *iter = NULL; |
| 2801 | struct strset conflicting_dirnames; |
| 2802 | struct strset dirnames; |
| 2803 | int ret = REF_TRANSACTION_ERROR_NAME_CONFLICT; |
| 2804 | |
| 2805 | /* |
| 2806 | * For the sake of comments in this function, suppose that |
| 2807 | * refname is "refs/foo/bar". |
| 2808 | */ |
| 2809 | |
| 2810 | assert(err); |
| 2811 | |
| 2812 | strset_init(&conflicting_dirnames); |
| 2813 | strset_init(&dirnames); |
| 2814 | |
| 2815 | for_each_string_list_item(item, refnames) { |
| 2816 | const size_t *update_idx = (size_t *)item->util; |
| 2817 | const char *refname = item->string; |
| 2818 | const char *extra_refname; |
| 2819 | struct object_id oid; |
| 2820 | unsigned int type; |
| 2821 | const char *slash; |
| 2822 | |
| 2823 | strbuf_reset(&dirname); |
| 2824 | |
| 2825 | for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) { |
| 2826 | /* |
| 2827 | * Just saying "Is a directory" when we e.g. can't |
| 2828 | * lock some multi-level ref isn't very informative, |
| 2829 | * the user won't be told *what* is a directory, so |
| 2830 | * let's not use strerror() below. |
| 2831 | */ |
| 2832 | int ignore_errno; |
| 2833 | |
| 2834 | /* Expand dirname to the new prefix, not including the trailing slash: */ |
| 2835 | strbuf_add(&dirname, refname + dirname.len, slash - refname - dirname.len); |
| 2836 | |
| 2837 | /* |
| 2838 | * We are still at a leading dir of the refname (e.g., |
| 2839 | * "refs/foo"; if there is a reference with that name, |
| 2840 | * it is a conflict, *unless* it is in skip. |
| 2841 | */ |
| 2842 | if (skip && string_list_has_string(skip, dirname.buf)) |
| 2843 | continue; |
| 2844 | |
| 2845 | /* |
| 2846 | * If we've already seen the directory we don't need to |
| 2847 | * process it again. Skip it to avoid checking common |
| 2848 | * prefixes like "refs/heads/" repeatedly. |
| 2849 | */ |
| 2850 | if (!strset_add(&dirnames, dirname.buf)) |
| 2851 | continue; |
| 2852 | |
| 2853 | if (!initial_transaction && |
| 2854 | (strset_contains(&conflicting_dirnames, dirname.buf) || |
| 2855 | !refs_read_raw_ref(refs, dirname.buf, &oid, &referent, |
| 2856 | &type, &ignore_errno))) { |
| 2857 | |
| 2858 | strbuf_addf(err, _("'%s' exists; cannot create '%s'"), |
| 2859 | dirname.buf, refname); |
| 2860 | |
| 2861 | if (transaction && ref_transaction_maybe_set_rejected( |
| 2862 | transaction, *update_idx, |
| 2863 | REF_TRANSACTION_ERROR_NAME_CONFLICT, err)) { |
| 2864 | strset_remove(&dirnames, dirname.buf); |
| 2865 | strset_add(&conflicting_dirnames, dirname.buf); |
| 2866 | goto next_ref; |
| 2867 | } |
| 2868 | |
| 2869 | goto cleanup; |
| 2870 | } |
| 2871 | |
| 2872 | if (extras && string_list_has_string(extras, dirname.buf)) { |
| 2873 | strbuf_addf(err, _("cannot process '%s' and '%s' at the same time"), |
| 2874 | refname, dirname.buf); |
| 2875 | |
| 2876 | if (transaction && ref_transaction_maybe_set_rejected( |
| 2877 | transaction, *update_idx, |
| 2878 | REF_TRANSACTION_ERROR_NAME_CONFLICT, err)) { |
| 2879 | strset_remove(&dirnames, dirname.buf); |
| 2880 | goto next_ref; |
| 2881 | } |
| 2882 | |
| 2883 | goto cleanup; |
| 2884 | } |
| 2885 | } |
| 2886 | |
| 2887 | /* |
| 2888 | * We are at the leaf of our refname (e.g., "refs/foo/bar"). |
| 2889 | * There is no point in searching for a reference with that |
| 2890 | * name, because a refname isn't considered to conflict with |
| 2891 | * itself. But we still need to check for references whose |
| 2892 | * names are in the "refs/foo/bar/" namespace, because they |
| 2893 | * *do* conflict. |
| 2894 | */ |
| 2895 | strbuf_addstr(&dirname, refname + dirname.len); |
| 2896 | strbuf_addch(&dirname, '/'); |
| 2897 | |
| 2898 | if (!initial_transaction) { |
| 2899 | int ok; |
| 2900 | |
| 2901 | if (!iter) |
| 2902 | iter = refs_ref_iterator_begin(refs, dirname.buf, NULL, 0, |
| 2903 | REFS_FOR_EACH_INCLUDE_BROKEN); |
| 2904 | else if (ref_iterator_seek(iter, dirname.buf, |
| 2905 | REF_ITERATOR_SEEK_SET_PREFIX) < 0) |
| 2906 | goto cleanup; |
| 2907 | |
| 2908 | while ((ok = ref_iterator_advance(iter)) == ITER_OK) { |
| 2909 | if (skip && |
| 2910 | string_list_has_string(skip, iter->ref.name)) |
| 2911 | continue; |
| 2912 | strbuf_addf(err, _("'%s' exists; cannot create '%s'"), |
| 2913 | iter->ref.name, refname); |
| 2914 | |
| 2915 | if (transaction && ref_transaction_maybe_set_rejected( |
| 2916 | transaction, *update_idx, |
| 2917 | REF_TRANSACTION_ERROR_NAME_CONFLICT, err)) |
| 2918 | goto next_ref; |
| 2919 | |
| 2920 | goto cleanup; |
| 2921 | } |
| 2922 | |
| 2923 | if (ok != ITER_DONE) |
| 2924 | BUG("error while iterating over references"); |
| 2925 | } |
| 2926 | |
| 2927 | extra_refname = find_descendant_ref(dirname.buf, extras, skip); |
| 2928 | if (extra_refname) { |
| 2929 | strbuf_addf(err, _("cannot process '%s' and '%s' at the same time"), |
| 2930 | refname, extra_refname); |
| 2931 | |
| 2932 | if (transaction && ref_transaction_maybe_set_rejected( |
| 2933 | transaction, *update_idx, |
| 2934 | REF_TRANSACTION_ERROR_NAME_CONFLICT, err)) |
| 2935 | goto next_ref; |
| 2936 | |
| 2937 | goto cleanup; |
| 2938 | } |
| 2939 | next_ref:; |
| 2940 | } |
| 2941 | |
| 2942 | ret = 0; |
| 2943 | |
| 2944 | cleanup: |
| 2945 | strbuf_release(&referent); |
| 2946 | strbuf_release(&dirname); |
| 2947 | strset_clear(&conflicting_dirnames); |
| 2948 | strset_clear(&dirnames); |
| 2949 | ref_iterator_free(iter); |
| 2950 | return ret; |
| 2951 | } |
| 2952 | |
| 2953 | enum ref_transaction_error refs_verify_refname_available( |
| 2954 | struct ref_store *refs, |
| 2955 | const char *refname, |
| 2956 | const struct string_list *extras, |
| 2957 | const struct string_list *skip, |
| 2958 | unsigned int initial_transaction, |
| 2959 | struct strbuf *err) |
| 2960 | { |
| 2961 | struct string_list_item item = { .string = (char *) refname }; |
| 2962 | struct string_list refnames = { |
| 2963 | .items = &item, |
| 2964 | .nr = 1, |
| 2965 | }; |
| 2966 | |
| 2967 | return refs_verify_refnames_available(refs, &refnames, extras, skip, |
| 2968 | NULL, initial_transaction, err); |
| 2969 | } |
| 2970 | |
| 2971 | struct do_for_each_reflog_help { |
| 2972 | each_reflog_fn *fn; |
| 2973 | void *cb_data; |
| 2974 | }; |
| 2975 | |
| 2976 | static int do_for_each_reflog_helper(const struct reference *ref, void *cb_data) |
| 2977 | { |
| 2978 | struct do_for_each_reflog_help *hp = cb_data; |
| 2979 | return hp->fn(ref->name, hp->cb_data); |
| 2980 | } |
| 2981 | |
| 2982 | int refs_for_each_reflog(struct ref_store *refs, each_reflog_fn fn, void *cb_data) |
| 2983 | { |
| 2984 | struct ref_iterator *iter; |
| 2985 | struct do_for_each_reflog_help hp = { fn, cb_data }; |
| 2986 | |
| 2987 | iter = refs->be->reflog_iterator_begin(refs); |
| 2988 | |
| 2989 | return do_for_each_ref_iterator(iter, do_for_each_reflog_helper, &hp); |
| 2990 | } |
| 2991 | |
| 2992 | int refs_for_each_reflog_ent_reverse(struct ref_store *refs, |
| 2993 | const char *refname, |
| 2994 | each_reflog_ent_fn fn, |
| 2995 | void *cb_data) |
| 2996 | { |
| 2997 | return refs->be->for_each_reflog_ent_reverse(refs, refname, |
| 2998 | fn, cb_data); |
| 2999 | } |
| 3000 | |
| 3001 | int refs_for_each_reflog_ent(struct ref_store *refs, const char *refname, |
| 3002 | each_reflog_ent_fn fn, void *cb_data) |
| 3003 | { |
| 3004 | return refs->be->for_each_reflog_ent(refs, refname, fn, cb_data); |
| 3005 | } |
| 3006 | |
| 3007 | int refs_reflog_exists(struct ref_store *refs, const char *refname) |
| 3008 | { |
| 3009 | return refs->be->reflog_exists(refs, refname); |
| 3010 | } |
| 3011 | |
| 3012 | int refs_create_reflog(struct ref_store *refs, const char *refname, |
| 3013 | struct strbuf *err) |
| 3014 | { |
| 3015 | return refs->be->create_reflog(refs, refname, err); |
| 3016 | } |
| 3017 | |
| 3018 | int refs_delete_reflog(struct ref_store *refs, const char *refname) |
| 3019 | { |
| 3020 | return refs->be->delete_reflog(refs, refname); |
| 3021 | } |
| 3022 | |
| 3023 | int refs_reflog_expire(struct ref_store *refs, |
| 3024 | const char *refname, |
| 3025 | unsigned int flags, |
| 3026 | reflog_expiry_prepare_fn prepare_fn, |
| 3027 | reflog_expiry_should_prune_fn should_prune_fn, |
| 3028 | reflog_expiry_cleanup_fn cleanup_fn, |
| 3029 | void *policy_cb_data) |
| 3030 | { |
| 3031 | return refs->be->reflog_expire(refs, refname, flags, |
| 3032 | prepare_fn, should_prune_fn, |
| 3033 | cleanup_fn, policy_cb_data); |
| 3034 | } |
| 3035 | |
| 3036 | void ref_transaction_for_each_queued_update(struct ref_transaction *transaction, |
| 3037 | ref_transaction_for_each_queued_update_fn cb, |
| 3038 | void *cb_data) |
| 3039 | { |
| 3040 | for (size_t i = 0; i < transaction->nr; i++) { |
| 3041 | struct ref_update *update = transaction->updates[i]; |
| 3042 | |
| 3043 | cb(update->refname, |
| 3044 | (update->flags & REF_HAVE_OLD) ? &update->old_oid : NULL, |
| 3045 | (update->flags & REF_HAVE_NEW) ? &update->new_oid : NULL, |
| 3046 | cb_data); |
| 3047 | } |
| 3048 | } |
| 3049 | |
| 3050 | void ref_transaction_for_each_rejected_update(struct ref_transaction *transaction, |
| 3051 | ref_transaction_for_each_rejected_update_fn cb, |
| 3052 | void *cb_data) |
| 3053 | { |
| 3054 | if (!transaction->rejections) |
| 3055 | return; |
| 3056 | |
| 3057 | for (size_t i = 0; i < transaction->rejections->nr; i++) { |
| 3058 | size_t update_index = transaction->rejections->update_indices[i]; |
| 3059 | struct ref_update *update = transaction->updates[update_index]; |
| 3060 | |
| 3061 | if (!update->rejection_err) |
| 3062 | continue; |
| 3063 | |
| 3064 | cb(update->refname, |
| 3065 | (update->flags & REF_HAVE_OLD) ? &update->old_oid : NULL, |
| 3066 | (update->flags & REF_HAVE_NEW) ? &update->new_oid : NULL, |
| 3067 | update->old_target, update->new_target, |
| 3068 | update->rejection_err, update->rejection_details, cb_data); |
| 3069 | } |
| 3070 | } |
| 3071 | |
| 3072 | int refs_delete_refs(struct ref_store *refs, const char *logmsg, |
| 3073 | struct string_list *refnames, unsigned int flags) |
| 3074 | { |
| 3075 | struct ref_transaction *transaction; |
| 3076 | struct strbuf err = STRBUF_INIT; |
| 3077 | struct string_list_item *item; |
| 3078 | int ret = 0, failures = 0; |
| 3079 | char *msg; |
| 3080 | |
| 3081 | if (!refnames->nr) |
| 3082 | return 0; |
| 3083 | |
| 3084 | msg = normalize_reflog_message(logmsg); |
| 3085 | |
| 3086 | /* |
| 3087 | * Since we don't check the references' old_oids, the |
| 3088 | * individual updates can't fail, so we can pack all of the |
| 3089 | * updates into a single transaction. |
| 3090 | */ |
| 3091 | transaction = ref_store_transaction_begin(refs, 0, &err); |
| 3092 | if (!transaction) { |
| 3093 | ret = error("%s", err.buf); |
| 3094 | goto out; |
| 3095 | } |
| 3096 | |
| 3097 | for_each_string_list_item(item, refnames) { |
| 3098 | ret = ref_transaction_delete(transaction, item->string, |
| 3099 | NULL, NULL, flags, msg, &err); |
| 3100 | if (ret) { |
| 3101 | warning(_("could not delete reference %s: %s"), |
| 3102 | item->string, err.buf); |
| 3103 | strbuf_reset(&err); |
| 3104 | failures = 1; |
| 3105 | } |
| 3106 | } |
| 3107 | |
| 3108 | ret = ref_transaction_commit(transaction, &err); |
| 3109 | if (ret) { |
| 3110 | if (refnames->nr == 1) |
| 3111 | error(_("could not delete reference %s: %s"), |
| 3112 | refnames->items[0].string, err.buf); |
| 3113 | else |
| 3114 | error(_("could not delete references: %s"), err.buf); |
| 3115 | } |
| 3116 | |
| 3117 | out: |
| 3118 | if (!ret && failures) |
| 3119 | ret = -1; |
| 3120 | ref_transaction_free(transaction); |
| 3121 | strbuf_release(&err); |
| 3122 | free(msg); |
| 3123 | return ret; |
| 3124 | } |
| 3125 | |
| 3126 | int refs_rename_ref(struct ref_store *refs, const char *oldref, |
| 3127 | const char *newref, const char *logmsg) |
| 3128 | { |
| 3129 | char *msg; |
| 3130 | int retval; |
| 3131 | |
| 3132 | msg = normalize_reflog_message(logmsg); |
| 3133 | retval = refs->be->rename_ref(refs, oldref, newref, msg); |
| 3134 | free(msg); |
| 3135 | return retval; |
| 3136 | } |
| 3137 | |
| 3138 | int refs_copy_existing_ref(struct ref_store *refs, const char *oldref, |
| 3139 | const char *newref, const char *logmsg) |
| 3140 | { |
| 3141 | char *msg; |
| 3142 | int retval; |
| 3143 | |
| 3144 | msg = normalize_reflog_message(logmsg); |
| 3145 | retval = refs->be->copy_ref(refs, oldref, newref, msg); |
| 3146 | free(msg); |
| 3147 | return retval; |
| 3148 | } |
| 3149 | |
| 3150 | const char *ref_update_original_update_refname(struct ref_update *update) |
| 3151 | { |
| 3152 | while (update->parent_update) |
| 3153 | update = update->parent_update; |
| 3154 | |
| 3155 | return update->refname; |
| 3156 | } |
| 3157 | |
| 3158 | int ref_update_has_null_new_value(struct ref_update *update) |
| 3159 | { |
| 3160 | return !update->new_target && is_null_oid(&update->new_oid); |
| 3161 | } |
| 3162 | |
| 3163 | enum ref_transaction_error ref_update_check_old_target(const char *referent, |
| 3164 | struct ref_update *update, |
| 3165 | struct strbuf *err) |
| 3166 | { |
| 3167 | if (!update->old_target) |
| 3168 | BUG("called without old_target set"); |
| 3169 | |
| 3170 | if (!strcmp(referent, update->old_target)) |
| 3171 | return 0; |
| 3172 | |
| 3173 | if (!strcmp(referent, "")) { |
| 3174 | strbuf_addf(err, "verifying symref target: '%s': " |
| 3175 | "reference is missing but expected %s", |
| 3176 | ref_update_original_update_refname(update), |
| 3177 | update->old_target); |
| 3178 | return REF_TRANSACTION_ERROR_NONEXISTENT_REF; |
| 3179 | } |
| 3180 | |
| 3181 | strbuf_addf(err, "verifying symref target: '%s': is at %s but expected %s", |
| 3182 | ref_update_original_update_refname(update), |
| 3183 | referent, update->old_target); |
| 3184 | return REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE; |
| 3185 | } |
| 3186 | |
| 3187 | struct migration_data { |
| 3188 | struct ref_store *old_refs; |
| 3189 | struct ref_transaction *transaction; |
| 3190 | struct strbuf *errbuf; |
| 3191 | struct strbuf sb, name, mail; |
| 3192 | uint64_t index; |
| 3193 | }; |
| 3194 | |
| 3195 | static int migrate_one_ref(const struct reference *ref, void *cb_data) |
| 3196 | { |
| 3197 | struct migration_data *data = cb_data; |
| 3198 | const struct git_hash_algo *hash_algo = data->transaction->ref_store->repo->hash_algo; |
| 3199 | struct strbuf symref_target = STRBUF_INIT; |
| 3200 | int ret; |
| 3201 | |
| 3202 | if (ref->flags & REF_ISSYMREF) { |
| 3203 | ret = refs_read_symbolic_ref(data->old_refs, ref->name, &symref_target); |
| 3204 | if (ret < 0) |
| 3205 | goto done; |
| 3206 | |
| 3207 | ret = ref_transaction_update(data->transaction, ref->name, NULL, null_oid(hash_algo), |
| 3208 | symref_target.buf, NULL, |
| 3209 | REF_SKIP_CREATE_REFLOG | REF_NO_DEREF, NULL, data->errbuf); |
| 3210 | if (ret < 0) |
| 3211 | goto done; |
| 3212 | } else { |
| 3213 | ret = ref_transaction_create(data->transaction, ref->name, ref->oid, NULL, |
| 3214 | REF_SKIP_CREATE_REFLOG | REF_SKIP_OID_VERIFICATION, |
| 3215 | NULL, data->errbuf); |
| 3216 | if (ret < 0) |
| 3217 | goto done; |
| 3218 | } |
| 3219 | |
| 3220 | done: |
| 3221 | strbuf_release(&symref_target); |
| 3222 | return ret; |
| 3223 | } |
| 3224 | |
| 3225 | static int migrate_one_reflog_entry(const char *refname, |
| 3226 | struct object_id *old_oid, |
| 3227 | struct object_id *new_oid, |
| 3228 | const char *committer, |
| 3229 | timestamp_t timestamp, int tz, |
| 3230 | const char *msg, void *cb_data) |
| 3231 | { |
| 3232 | struct migration_data *data = cb_data; |
| 3233 | struct ident_split ident; |
| 3234 | const char *date; |
| 3235 | int ret; |
| 3236 | |
| 3237 | if (split_ident_line(&ident, committer, strlen(committer)) < 0) |
| 3238 | return -1; |
| 3239 | |
| 3240 | strbuf_reset(&data->name); |
| 3241 | strbuf_add(&data->name, ident.name_begin, ident.name_end - ident.name_begin); |
| 3242 | strbuf_reset(&data->mail); |
| 3243 | strbuf_add(&data->mail, ident.mail_begin, ident.mail_end - ident.mail_begin); |
| 3244 | |
| 3245 | date = show_date(timestamp, tz, DATE_MODE(NORMAL)); |
| 3246 | strbuf_reset(&data->sb); |
| 3247 | strbuf_addstr(&data->sb, fmt_ident(data->name.buf, data->mail.buf, WANT_BLANK_IDENT, date, 0)); |
| 3248 | |
| 3249 | ret = ref_transaction_update_reflog(data->transaction, refname, |
| 3250 | new_oid, old_oid, data->sb.buf, |
| 3251 | msg, data->index++, data->errbuf); |
| 3252 | return ret; |
| 3253 | } |
| 3254 | |
| 3255 | static int migrate_one_reflog(const char *refname, void *cb_data) |
| 3256 | { |
| 3257 | struct migration_data *migration_data = cb_data; |
| 3258 | return refs_for_each_reflog_ent(migration_data->old_refs, refname, |
| 3259 | migrate_one_reflog_entry, migration_data); |
| 3260 | } |
| 3261 | |
| 3262 | static int move_files(const char *from_path, const char *to_path, struct strbuf *errbuf) |
| 3263 | { |
| 3264 | struct strbuf from_buf = STRBUF_INIT, to_buf = STRBUF_INIT; |
| 3265 | size_t from_len, to_len; |
| 3266 | DIR *from_dir; |
| 3267 | int ret; |
| 3268 | |
| 3269 | from_dir = opendir(from_path); |
| 3270 | if (!from_dir) { |
| 3271 | strbuf_addf(errbuf, "could not open source directory '%s': %s", |
| 3272 | from_path, strerror(errno)); |
| 3273 | ret = -1; |
| 3274 | goto done; |
| 3275 | } |
| 3276 | |
| 3277 | strbuf_addstr(&from_buf, from_path); |
| 3278 | strbuf_complete(&from_buf, '/'); |
| 3279 | from_len = from_buf.len; |
| 3280 | |
| 3281 | strbuf_addstr(&to_buf, to_path); |
| 3282 | strbuf_complete(&to_buf, '/'); |
| 3283 | to_len = to_buf.len; |
| 3284 | |
| 3285 | while (1) { |
| 3286 | struct dirent *ent; |
| 3287 | |
| 3288 | errno = 0; |
| 3289 | ent = readdir(from_dir); |
| 3290 | if (!ent) |
| 3291 | break; |
| 3292 | |
| 3293 | if (!strcmp(ent->d_name, ".") || |
| 3294 | !strcmp(ent->d_name, "..")) |
| 3295 | continue; |
| 3296 | |
| 3297 | strbuf_setlen(&from_buf, from_len); |
| 3298 | strbuf_addstr(&from_buf, ent->d_name); |
| 3299 | |
| 3300 | strbuf_setlen(&to_buf, to_len); |
| 3301 | strbuf_addstr(&to_buf, ent->d_name); |
| 3302 | |
| 3303 | ret = rename(from_buf.buf, to_buf.buf); |
| 3304 | if (ret < 0) { |
| 3305 | strbuf_addf(errbuf, "could not link file '%s' to '%s': %s", |
| 3306 | from_buf.buf, to_buf.buf, strerror(errno)); |
| 3307 | goto done; |
| 3308 | } |
| 3309 | } |
| 3310 | |
| 3311 | if (errno) { |
| 3312 | strbuf_addf(errbuf, "could not read entry from directory '%s': %s", |
| 3313 | from_path, strerror(errno)); |
| 3314 | ret = -1; |
| 3315 | goto done; |
| 3316 | } |
| 3317 | |
| 3318 | ret = 0; |
| 3319 | |
| 3320 | done: |
| 3321 | strbuf_release(&from_buf); |
| 3322 | strbuf_release(&to_buf); |
| 3323 | if (from_dir) |
| 3324 | closedir(from_dir); |
| 3325 | return ret; |
| 3326 | } |
| 3327 | |
| 3328 | static int has_worktrees(struct repository *repo) |
| 3329 | { |
| 3330 | struct worktree **worktrees = get_worktrees(repo); |
| 3331 | int ret = 0; |
| 3332 | size_t i; |
| 3333 | |
| 3334 | for (i = 0; worktrees[i]; i++) { |
| 3335 | if (is_main_worktree(worktrees[i])) |
| 3336 | continue; |
| 3337 | ret = 1; |
| 3338 | } |
| 3339 | |
| 3340 | free_worktrees(worktrees); |
| 3341 | return ret; |
| 3342 | } |
| 3343 | |
| 3344 | int repo_migrate_ref_storage_format(struct repository *repo, |
| 3345 | enum ref_storage_format format, |
| 3346 | unsigned int flags, |
| 3347 | struct strbuf *errbuf) |
| 3348 | { |
| 3349 | struct ref_store *old_refs = NULL, *new_refs = NULL; |
| 3350 | struct refs_for_each_ref_options for_each_ref_opts = { |
| 3351 | .flags = REFS_FOR_EACH_INCLUDE_ROOT_REFS | REFS_FOR_EACH_INCLUDE_BROKEN, |
| 3352 | }; |
| 3353 | struct ref_transaction *transaction = NULL; |
| 3354 | struct strbuf new_gitdir = STRBUF_INIT; |
| 3355 | struct migration_data data = { |
| 3356 | .sb = STRBUF_INIT, |
| 3357 | .name = STRBUF_INIT, |
| 3358 | .mail = STRBUF_INIT, |
| 3359 | }; |
| 3360 | int did_migrate_refs = 0; |
| 3361 | int ret; |
| 3362 | |
| 3363 | if (repo->ref_storage_format == format) { |
| 3364 | strbuf_addstr(errbuf, "current and new ref storage format are equal"); |
| 3365 | ret = -1; |
| 3366 | goto done; |
| 3367 | } |
| 3368 | |
| 3369 | old_refs = get_main_ref_store(repo); |
| 3370 | |
| 3371 | /* |
| 3372 | * Worktrees complicate the migration because every worktree has a |
| 3373 | * separate ref storage. While it should be feasible to implement, this |
| 3374 | * is pushed out to a future iteration. |
| 3375 | */ |
| 3376 | if (has_worktrees(repo)) { |
| 3377 | strbuf_addstr(errbuf, "migrating repositories with worktrees is not supported yet"); |
| 3378 | ret = -1; |
| 3379 | goto done; |
| 3380 | } |
| 3381 | |
| 3382 | /* |
| 3383 | * The overall logic looks like this: |
| 3384 | * |
| 3385 | * 1. Set up a new temporary directory and initialize it with the new |
| 3386 | * format. This is where all refs will be migrated into. |
| 3387 | * |
| 3388 | * 2. Enumerate all refs and write them into the new ref storage. |
| 3389 | * This operation is safe as we do not yet modify the main |
| 3390 | * repository. |
| 3391 | * |
| 3392 | * 3. Enumerate all reflogs and write them into the new ref storage. |
| 3393 | * This operation is safe as we do not yet modify the main |
| 3394 | * repository. |
| 3395 | * |
| 3396 | * 4. If we're in dry-run mode then we are done and can hand over the |
| 3397 | * directory to the caller for inspection. If not, we now start |
| 3398 | * with the destructive part. |
| 3399 | * |
| 3400 | * 5. Delete the old ref storage from disk. As we have a copy of refs |
| 3401 | * in the new ref storage it's okay(ish) if we now get interrupted |
| 3402 | * as there is an equivalent copy of all refs available. |
| 3403 | * |
| 3404 | * 6. Move the new ref storage files into place. |
| 3405 | * |
| 3406 | * 7. Change the repository format to the new ref format. |
| 3407 | */ |
| 3408 | strbuf_addf(&new_gitdir, "%s/%s", old_refs->gitdir, "ref_migration.XXXXXX"); |
| 3409 | if (!mkdtemp(new_gitdir.buf)) { |
| 3410 | strbuf_addf(errbuf, "cannot create migration directory: %s", |
| 3411 | strerror(errno)); |
| 3412 | ret = -1; |
| 3413 | goto done; |
| 3414 | } |
| 3415 | |
| 3416 | new_refs = ref_store_init(repo, format, new_gitdir.buf, |
| 3417 | REF_STORE_ALL_CAPS); |
| 3418 | ret = ref_store_create_on_disk(new_refs, 0, errbuf); |
| 3419 | if (ret < 0) |
| 3420 | goto done; |
| 3421 | |
| 3422 | transaction = ref_store_transaction_begin(new_refs, REF_TRANSACTION_FLAG_INITIAL, |
| 3423 | errbuf); |
| 3424 | if (!transaction) |
| 3425 | goto done; |
| 3426 | |
| 3427 | data.old_refs = old_refs; |
| 3428 | data.transaction = transaction; |
| 3429 | data.errbuf = errbuf; |
| 3430 | |
| 3431 | /* |
| 3432 | * We need to use `refs_for_each_ref_ext()` here so that we can |
| 3433 | * also include broken refs and symrefs. These would otherwise be |
| 3434 | * skipped silently. |
| 3435 | * |
| 3436 | * Ideally, we would do this call while locking the old ref storage |
| 3437 | * such that there cannot be any concurrent modifications. We do not |
| 3438 | * have the infra for that though, and the "files" backend does not |
| 3439 | * allow for a central lock due to its design. It's thus on the user to |
| 3440 | * ensure that there are no concurrent writes. |
| 3441 | */ |
| 3442 | ret = refs_for_each_ref_ext(old_refs, migrate_one_ref, &data, &for_each_ref_opts); |
| 3443 | if (ret < 0) |
| 3444 | goto done; |
| 3445 | |
| 3446 | if (!(flags & REPO_MIGRATE_REF_STORAGE_FORMAT_SKIP_REFLOG)) { |
| 3447 | ret = refs_for_each_reflog(old_refs, migrate_one_reflog, &data); |
| 3448 | if (ret < 0) |
| 3449 | goto done; |
| 3450 | } |
| 3451 | |
| 3452 | ret = ref_transaction_commit(transaction, errbuf); |
| 3453 | if (ret < 0) |
| 3454 | goto done; |
| 3455 | did_migrate_refs = 1; |
| 3456 | |
| 3457 | if (flags & REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN) { |
| 3458 | printf(_("Finished dry-run migration of refs, " |
| 3459 | "the result can be found at '%s'\n"), new_gitdir.buf); |
| 3460 | ret = 0; |
| 3461 | goto done; |
| 3462 | } |
| 3463 | |
| 3464 | /* |
| 3465 | * Release the new ref store such that any potentially-open files will |
| 3466 | * be closed. This is required for platforms like Cygwin, where |
| 3467 | * renaming an open file results in EPERM. |
| 3468 | */ |
| 3469 | ref_store_release(new_refs); |
| 3470 | FREE_AND_NULL(new_refs); |
| 3471 | |
| 3472 | /* |
| 3473 | * Until now we were in the non-destructive phase, where we only |
| 3474 | * populated the new ref store. From hereon though we are about |
| 3475 | * to get hands by deleting the old ref store and then moving |
| 3476 | * the new one into place. |
| 3477 | * |
| 3478 | * Assuming that there were no concurrent writes, the new ref |
| 3479 | * store should have all information. So if we fail from hereon |
| 3480 | * we may be in an in-between state, but it would still be able |
| 3481 | * to recover by manually moving remaining files from the |
| 3482 | * temporary migration directory into place. |
| 3483 | */ |
| 3484 | ret = ref_store_remove_on_disk(old_refs, errbuf); |
| 3485 | if (ret < 0) |
| 3486 | goto done; |
| 3487 | |
| 3488 | ret = move_files(new_gitdir.buf, old_refs->gitdir, errbuf); |
| 3489 | if (ret < 0) |
| 3490 | goto done; |
| 3491 | |
| 3492 | if (rmdir(new_gitdir.buf) < 0) |
| 3493 | warning_errno(_("could not remove temporary migration directory '%s'"), |
| 3494 | new_gitdir.buf); |
| 3495 | |
| 3496 | /* |
| 3497 | * We have migrated the repository, so we now need to adjust the |
| 3498 | * repository format so that clients will use the new ref store. |
| 3499 | * We also need to swap out the repository's main ref store. |
| 3500 | */ |
| 3501 | initialize_repository_version(repo, hash_algo_by_ptr(repo->hash_algo), format, 1); |
| 3502 | |
| 3503 | /* |
| 3504 | * Unset the old ref store and release it. `get_main_ref_store()` will |
| 3505 | * make sure to lazily re-initialize the repository's ref store with |
| 3506 | * the new format. |
| 3507 | */ |
| 3508 | ref_store_release(old_refs); |
| 3509 | FREE_AND_NULL(old_refs); |
| 3510 | repo->refs_private = NULL; |
| 3511 | |
| 3512 | ret = 0; |
| 3513 | |
| 3514 | done: |
| 3515 | if (ret && did_migrate_refs) { |
| 3516 | strbuf_complete(errbuf, '\n'); |
| 3517 | strbuf_addf(errbuf, _("migrated refs can be found at '%s'"), |
| 3518 | new_gitdir.buf); |
| 3519 | } |
| 3520 | |
| 3521 | if (new_refs) { |
| 3522 | ref_store_release(new_refs); |
| 3523 | free(new_refs); |
| 3524 | } |
| 3525 | ref_transaction_free(transaction); |
| 3526 | strbuf_release(&new_gitdir); |
| 3527 | strbuf_release(&data.sb); |
| 3528 | strbuf_release(&data.name); |
| 3529 | strbuf_release(&data.mail); |
| 3530 | return ret; |
| 3531 | } |
| 3532 | |
| 3533 | int ref_update_expects_existing_old_ref(struct ref_update *update) |
| 3534 | { |
| 3535 | if (update->flags & REF_LOG_ONLY) |
| 3536 | return 0; |
| 3537 | |
| 3538 | return (update->flags & REF_HAVE_OLD) && |
| 3539 | (!is_null_oid(&update->old_oid) || update->old_target); |
| 3540 | } |
| 3541 | |
| 3542 | const char *ref_transaction_error_msg(enum ref_transaction_error err) |
| 3543 | { |
| 3544 | switch (err) { |
| 3545 | case REF_TRANSACTION_ERROR_NAME_CONFLICT: |
| 3546 | return "refname conflict"; |
| 3547 | case REF_TRANSACTION_ERROR_CREATE_EXISTS: |
| 3548 | return "reference already exists"; |
| 3549 | case REF_TRANSACTION_ERROR_NONEXISTENT_REF: |
| 3550 | return "reference does not exist"; |
| 3551 | case REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE: |
| 3552 | return "incorrect old value provided"; |
| 3553 | case REF_TRANSACTION_ERROR_INVALID_NEW_VALUE: |
| 3554 | return "invalid new value provided"; |
| 3555 | case REF_TRANSACTION_ERROR_EXPECTED_SYMREF: |
| 3556 | return "expected symref but found regular ref"; |
| 3557 | case REF_TRANSACTION_ERROR_CASE_CONFLICT: |
| 3558 | return "reference conflict due to case-insensitive filesystem"; |
| 3559 | default: |
| 3560 | return "unknown failure"; |
| 3561 | } |
| 3562 | } |
| 3563 | |
| 3564 | void refs_compute_filesystem_location(const char *gitdir, const char *payload, |
| 3565 | bool *is_worktree, struct strbuf *refdir, |
| 3566 | struct strbuf *ref_common_dir) |
| 3567 | { |
| 3568 | struct strbuf sb = STRBUF_INIT; |
| 3569 | |
| 3570 | *is_worktree = get_common_dir_noenv(ref_common_dir, gitdir); |
| 3571 | |
| 3572 | if (!payload) { |
| 3573 | /* |
| 3574 | * We can use the 'gitdir' as the 'refdir' without appending the |
| 3575 | * worktree path, as the 'gitdir' here is already the worktree |
| 3576 | * path and is different from 'commondir' denoted by 'ref_common_dir'. |
| 3577 | */ |
| 3578 | strbuf_addstr(refdir, gitdir); |
| 3579 | return; |
| 3580 | } |
| 3581 | |
| 3582 | if (!is_absolute_path(payload)) { |
| 3583 | strbuf_addf(&sb, "%s/%s", ref_common_dir->buf, payload); |
| 3584 | strbuf_realpath(ref_common_dir, sb.buf, 1); |
| 3585 | } else { |
| 3586 | strbuf_realpath(ref_common_dir, payload, 1); |
| 3587 | } |
| 3588 | |
| 3589 | strbuf_addbuf(refdir, ref_common_dir); |
| 3590 | |
| 3591 | if (*is_worktree) { |
| 3592 | const char *wt_id = strrchr(gitdir, '/'); |
| 3593 | if (!wt_id) |
| 3594 | BUG("worktree path does not contain slash"); |
| 3595 | strbuf_addf(refdir, "/worktrees/%s", wt_id + 1); |
| 3596 | } |
| 3597 | |
| 3598 | strbuf_release(&sb); |
| 3599 | } |