| 1 | #include "../git-compat-util.h" |
| 2 | #include "../abspath.h" |
| 3 | #include "../chdir-notify.h" |
| 4 | #include "../config.h" |
| 5 | #include "../dir.h" |
| 6 | #include "../environment.h" |
| 7 | #include "../fsck.h" |
| 8 | #include "../gettext.h" |
| 9 | #include "../hash.h" |
| 10 | #include "../hex.h" |
| 11 | #include "../ident.h" |
| 12 | #include "../iterator.h" |
| 13 | #include "../parse.h" |
| 14 | #include "../path.h" |
| 15 | #include "../refs.h" |
| 16 | #include "../reftable/reftable-basics.h" |
| 17 | #include "../reftable/reftable-error.h" |
| 18 | #include "../reftable/reftable-fsck.h" |
| 19 | #include "../reftable/reftable-iterator.h" |
| 20 | #include "../reftable/reftable-record.h" |
| 21 | #include "../reftable/reftable-stack.h" |
| 22 | #include "../repo-settings.h" |
| 23 | #include "../setup.h" |
| 24 | #include "../strmap.h" |
| 25 | #include "../trace2.h" |
| 26 | #include "../worktree.h" |
| 27 | #include "../write-or-die.h" |
| 28 | #include "refs-internal.h" |
| 29 | |
| 30 | /* |
| 31 | * Used as a flag in ref_update::flags when the ref_update was via an |
| 32 | * update to HEAD. |
| 33 | */ |
| 34 | #define REF_UPDATE_VIA_HEAD (1 << 8) |
| 35 | |
| 36 | struct reftable_backend { |
| 37 | struct reftable_stack *stack; |
| 38 | struct reftable_iterator it; |
| 39 | }; |
| 40 | |
| 41 | static void reftable_backend_on_reload(void *payload) |
| 42 | { |
| 43 | struct reftable_backend *be = payload; |
| 44 | reftable_iterator_destroy(&be->it); |
| 45 | } |
| 46 | |
| 47 | static int reftable_backend_init(struct reftable_backend *be, |
| 48 | const char *path, |
| 49 | const struct reftable_stack_options *_opts) |
| 50 | { |
| 51 | struct reftable_stack_options opts = *_opts; |
| 52 | opts.on_reload = reftable_backend_on_reload; |
| 53 | opts.on_reload_payload = be; |
| 54 | return reftable_new_stack(&be->stack, path, &opts); |
| 55 | } |
| 56 | |
| 57 | static void reftable_backend_release(struct reftable_backend *be) |
| 58 | { |
| 59 | reftable_stack_destroy(be->stack); |
| 60 | be->stack = NULL; |
| 61 | reftable_iterator_destroy(&be->it); |
| 62 | } |
| 63 | |
| 64 | static int reftable_backend_read_ref(struct reftable_backend *be, |
| 65 | const char *refname, |
| 66 | struct object_id *oid, |
| 67 | struct strbuf *referent, |
| 68 | unsigned int *type) |
| 69 | { |
| 70 | struct reftable_ref_record ref = {0}; |
| 71 | int ret; |
| 72 | |
| 73 | if (!be->it.ops) { |
| 74 | ret = reftable_stack_init_ref_iterator(be->stack, &be->it); |
| 75 | if (ret) |
| 76 | goto done; |
| 77 | } |
| 78 | |
| 79 | ret = reftable_iterator_seek_ref(&be->it, refname); |
| 80 | if (ret) |
| 81 | goto done; |
| 82 | |
| 83 | ret = reftable_iterator_next_ref(&be->it, &ref); |
| 84 | if (ret) |
| 85 | goto done; |
| 86 | |
| 87 | if (strcmp(ref.refname, refname) || |
| 88 | reftable_ref_record_is_deletion(&ref)) { |
| 89 | ret = 1; |
| 90 | goto done; |
| 91 | } |
| 92 | |
| 93 | if (ref.value_type == REFTABLE_REF_SYMREF) { |
| 94 | strbuf_reset(referent); |
| 95 | strbuf_addstr(referent, ref.value.symref); |
| 96 | *type |= REF_ISSYMREF; |
| 97 | } else if (reftable_ref_record_val1(&ref)) { |
| 98 | unsigned int hash_id; |
| 99 | |
| 100 | switch (reftable_stack_hash_id(be->stack)) { |
| 101 | case REFTABLE_HASH_SHA1: |
| 102 | hash_id = GIT_HASH_SHA1; |
| 103 | break; |
| 104 | case REFTABLE_HASH_SHA256: |
| 105 | hash_id = GIT_HASH_SHA256; |
| 106 | break; |
| 107 | default: |
| 108 | BUG("unhandled hash ID %d", reftable_stack_hash_id(be->stack)); |
| 109 | } |
| 110 | |
| 111 | oidread(oid, reftable_ref_record_val1(&ref), |
| 112 | &hash_algos[hash_id]); |
| 113 | } else { |
| 114 | BUG("unhandled reference value type %d", ref.value_type); |
| 115 | } |
| 116 | |
| 117 | done: |
| 118 | assert(ret != REFTABLE_API_ERROR); |
| 119 | reftable_ref_record_release(&ref); |
| 120 | return ret; |
| 121 | } |
| 122 | |
| 123 | struct reftable_ref_store { |
| 124 | struct ref_store base; |
| 125 | |
| 126 | /* |
| 127 | * The main backend refers to the common dir and thus contains common |
| 128 | * refs as well as refs of the main repository. |
| 129 | */ |
| 130 | struct reftable_backend main_backend; |
| 131 | /* |
| 132 | * The worktree backend refers to the gitdir in case the refdb is opened |
| 133 | * via a worktree. It thus contains the per-worktree refs. |
| 134 | */ |
| 135 | struct reftable_backend worktree_backend; |
| 136 | /* |
| 137 | * Map of worktree backends by their respective worktree names. The map |
| 138 | * is populated lazily when we try to resolve `worktrees/$worktree` refs. |
| 139 | */ |
| 140 | struct strmap worktree_backends; |
| 141 | struct reftable_stack_options stack_options; |
| 142 | |
| 143 | /* |
| 144 | * Options used when writing to or compacting the reftable stacks. |
| 145 | * These are parsed from the configuration lazily on first use via |
| 146 | * `reftable_be_write_options()` so that we don't have to access the |
| 147 | * configuration when initializing the ref store. Do not access these |
| 148 | * fields directly, but use the accessor instead. |
| 149 | */ |
| 150 | struct reftable_be_write_options { |
| 151 | struct reftable_write_options opts; |
| 152 | enum log_refs_config log_all_ref_updates; |
| 153 | bool initialized; |
| 154 | } write_opts_lazy_loaded; |
| 155 | |
| 156 | unsigned int store_flags; |
| 157 | int err; |
| 158 | }; |
| 159 | |
| 160 | /* |
| 161 | * Downcast ref_store to reftable_ref_store. Die if ref_store is not a |
| 162 | * reftable_ref_store. required_flags is compared with ref_store's store_flags |
| 163 | * to ensure the ref_store has all required capabilities. "caller" is used in |
| 164 | * any necessary error messages. |
| 165 | */ |
| 166 | static struct reftable_ref_store *reftable_be_downcast(struct ref_store *ref_store, |
| 167 | unsigned int required_flags, |
| 168 | const char *caller) |
| 169 | { |
| 170 | struct reftable_ref_store *refs; |
| 171 | |
| 172 | if (ref_store->be != &refs_be_reftable) |
| 173 | BUG("ref_store is type \"%s\" not \"reftables\" in %s", |
| 174 | ref_store->be->name, caller); |
| 175 | |
| 176 | refs = (struct reftable_ref_store *)ref_store; |
| 177 | |
| 178 | if ((refs->store_flags & required_flags) != required_flags) |
| 179 | BUG("operation %s requires abilities 0x%x, but only have 0x%x", |
| 180 | caller, required_flags, refs->store_flags); |
| 181 | |
| 182 | return refs; |
| 183 | } |
| 184 | |
| 185 | static int backend_for_worktree(struct reftable_backend **out, |
| 186 | struct reftable_ref_store *store, |
| 187 | const char *worktree_name) |
| 188 | { |
| 189 | struct strbuf worktree_dir = STRBUF_INIT; |
| 190 | int ret; |
| 191 | |
| 192 | *out = strmap_get(&store->worktree_backends, worktree_name); |
| 193 | if (*out) { |
| 194 | ret = 0; |
| 195 | goto out; |
| 196 | } |
| 197 | |
| 198 | strbuf_addf(&worktree_dir, "%s/worktrees/%s/reftable", |
| 199 | store->base.repo->commondir, worktree_name); |
| 200 | |
| 201 | CALLOC_ARRAY(*out, 1); |
| 202 | store->err = ret = reftable_backend_init(*out, worktree_dir.buf, |
| 203 | &store->stack_options); |
| 204 | if (ret < 0) { |
| 205 | free(*out); |
| 206 | goto out; |
| 207 | } |
| 208 | |
| 209 | strmap_put(&store->worktree_backends, worktree_name, *out); |
| 210 | |
| 211 | out: |
| 212 | strbuf_release(&worktree_dir); |
| 213 | return ret; |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Some refs are global to the repository (refs/heads/{*}), while others are |
| 218 | * local to the worktree (eg. HEAD, refs/bisect/{*}). We solve this by having |
| 219 | * multiple separate databases (ie. multiple reftable/ directories), one for |
| 220 | * the shared refs, one for the current worktree refs, and one for each |
| 221 | * additional worktree. For reading, we merge the view of both the shared and |
| 222 | * the current worktree's refs, when necessary. |
| 223 | * |
| 224 | * This function also optionally assigns the rewritten reference name that is |
| 225 | * local to the stack. This translation is required when using worktree refs |
| 226 | * like `worktrees/$worktree/refs/heads/foo` as worktree stacks will store |
| 227 | * those references in their normalized form. |
| 228 | */ |
| 229 | static int backend_for(struct reftable_backend **out, |
| 230 | struct reftable_ref_store *store, |
| 231 | const char *refname, |
| 232 | const char **rewritten_ref, |
| 233 | int reload) |
| 234 | { |
| 235 | const char *wtname; |
| 236 | int wtname_len; |
| 237 | int ret; |
| 238 | |
| 239 | if (!refname) { |
| 240 | *out = &store->main_backend; |
| 241 | ret = 0; |
| 242 | goto out; |
| 243 | } |
| 244 | |
| 245 | switch (parse_worktree_ref(refname, &wtname, &wtname_len, rewritten_ref)) { |
| 246 | case REF_WORKTREE_OTHER: { |
| 247 | static struct strbuf wtname_buf = STRBUF_INIT; |
| 248 | |
| 249 | /* |
| 250 | * We're using a static buffer here so that we don't need to |
| 251 | * allocate the worktree name whenever we look up a reference. |
| 252 | * This could be avoided if the strmap interface knew how to |
| 253 | * handle keys with a length. |
| 254 | */ |
| 255 | strbuf_reset(&wtname_buf); |
| 256 | strbuf_add(&wtname_buf, wtname, wtname_len); |
| 257 | |
| 258 | /* |
| 259 | * There is an edge case here: when the worktree references the |
| 260 | * current worktree, then we set up the stack once via |
| 261 | * `worktree_backends` and once via `worktree_backend`. This is |
| 262 | * wasteful, but in the reading case it shouldn't matter. And |
| 263 | * in the writing case we would notice that the stack is locked |
| 264 | * already and error out when trying to write a reference via |
| 265 | * both stacks. |
| 266 | */ |
| 267 | ret = backend_for_worktree(out, store, wtname_buf.buf); |
| 268 | |
| 269 | goto out; |
| 270 | } |
| 271 | case REF_WORKTREE_CURRENT: |
| 272 | /* |
| 273 | * If there is no worktree stack then we're currently in the |
| 274 | * main worktree. We thus return the main stack in that case. |
| 275 | */ |
| 276 | if (!store->worktree_backend.stack) |
| 277 | *out = &store->main_backend; |
| 278 | else |
| 279 | *out = &store->worktree_backend; |
| 280 | ret = 0; |
| 281 | goto out; |
| 282 | case REF_WORKTREE_MAIN: |
| 283 | case REF_WORKTREE_SHARED: |
| 284 | *out = &store->main_backend; |
| 285 | ret = 0; |
| 286 | goto out; |
| 287 | default: |
| 288 | BUG("unhandled worktree reference type"); |
| 289 | } |
| 290 | |
| 291 | out: |
| 292 | if (reload && !ret) |
| 293 | ret = reftable_stack_reload((*out)->stack); |
| 294 | return ret; |
| 295 | } |
| 296 | |
| 297 | static void fill_reftable_log_record(struct reftable_log_record *log, const struct ident_split *split) |
| 298 | { |
| 299 | const char *tz_begin; |
| 300 | int sign = 1; |
| 301 | |
| 302 | reftable_log_record_release(log); |
| 303 | log->value_type = REFTABLE_LOG_UPDATE; |
| 304 | log->value.update.name = |
| 305 | xstrndup(split->name_begin, split->name_end - split->name_begin); |
| 306 | log->value.update.email = |
| 307 | xstrndup(split->mail_begin, split->mail_end - split->mail_begin); |
| 308 | log->value.update.time = atol(split->date_begin); |
| 309 | |
| 310 | tz_begin = split->tz_begin; |
| 311 | if (*tz_begin == '-') { |
| 312 | sign = -1; |
| 313 | tz_begin++; |
| 314 | } |
| 315 | if (*tz_begin == '+') { |
| 316 | sign = 1; |
| 317 | tz_begin++; |
| 318 | } |
| 319 | |
| 320 | log->value.update.tz_offset = sign * atoi(tz_begin); |
| 321 | } |
| 322 | |
| 323 | static int reftable_be_config(const char *var, const char *value, |
| 324 | const struct config_context *ctx, |
| 325 | void *payload) |
| 326 | { |
| 327 | struct reftable_ref_store *refs = payload; |
| 328 | struct reftable_be_write_options *opts = &refs->write_opts_lazy_loaded; |
| 329 | |
| 330 | if (!strcmp(var, "reftable.blocksize")) { |
| 331 | unsigned long block_size = git_config_ulong(var, value, ctx->kvi); |
| 332 | if (block_size > 16777215) |
| 333 | die("reftable block size cannot exceed 16MB"); |
| 334 | opts->opts.block_size = block_size; |
| 335 | } else if (!strcmp(var, "reftable.restartinterval")) { |
| 336 | unsigned long restart_interval = git_config_ulong(var, value, ctx->kvi); |
| 337 | if (restart_interval > UINT16_MAX) |
| 338 | die("reftable block size cannot exceed %u", (unsigned)UINT16_MAX); |
| 339 | opts->opts.restart_interval = restart_interval; |
| 340 | } else if (!strcmp(var, "reftable.indexobjects")) { |
| 341 | opts->opts.skip_index_objects = !git_config_bool(var, value); |
| 342 | } else if (!strcmp(var, "reftable.geometricfactor")) { |
| 343 | unsigned long factor = git_config_ulong(var, value, ctx->kvi); |
| 344 | if (factor > UINT8_MAX) |
| 345 | die("reftable geometric factor cannot exceed %u", (unsigned)UINT8_MAX); |
| 346 | opts->opts.auto_compaction_factor = factor; |
| 347 | } else if (!strcmp(var, "reftable.locktimeout")) { |
| 348 | int64_t lock_timeout = git_config_int64(var, value, ctx->kvi); |
| 349 | if (lock_timeout > LONG_MAX) |
| 350 | die("reftable lock timeout cannot exceed %"PRIdMAX, (intmax_t)LONG_MAX); |
| 351 | if (lock_timeout < 0 && lock_timeout != -1) |
| 352 | die("reftable lock timeout does not support negative values other than -1"); |
| 353 | opts->opts.lock_timeout_ms = lock_timeout; |
| 354 | } else if (!strcmp(var, "core.logallrefupdates")) { |
| 355 | opts->log_all_ref_updates = refs_parse_log_all_ref_updates_config(value); |
| 356 | } |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | static const struct reftable_be_write_options *reftable_be_write_options(struct reftable_ref_store *refs) |
| 362 | { |
| 363 | struct reftable_be_write_options *opts = &refs->write_opts_lazy_loaded; |
| 364 | mode_t mask; |
| 365 | |
| 366 | if (opts->initialized) |
| 367 | return opts; |
| 368 | |
| 369 | mask = umask(0); |
| 370 | umask(mask); |
| 371 | |
| 372 | opts->opts.default_permissions = calc_shared_perm(refs->base.repo, 0666 & ~mask); |
| 373 | opts->opts.disable_auto_compact = |
| 374 | !git_env_bool("GIT_TEST_REFTABLE_AUTOCOMPACTION", 1); |
| 375 | opts->opts.lock_timeout_ms = 100; |
| 376 | opts->log_all_ref_updates = LOG_REFS_UNSET; |
| 377 | |
| 378 | repo_config(refs->base.repo, reftable_be_config, refs); |
| 379 | |
| 380 | /* |
| 381 | * It is somewhat unfortunate that we have to mirror the default block |
| 382 | * size of the reftable library here. But given that the write options |
| 383 | * wouldn't be updated by the library here, and given that we require |
| 384 | * the proper block size to trim reflog message so that they fit, we |
| 385 | * must set up a proper value here. |
| 386 | */ |
| 387 | if (!opts->opts.block_size) |
| 388 | opts->opts.block_size = 4096; |
| 389 | |
| 390 | opts->initialized = true; |
| 391 | return opts; |
| 392 | } |
| 393 | |
| 394 | static void reftable_be_reparent(const char *name UNUSED, |
| 395 | const char *old_cwd, |
| 396 | const char *new_cwd, |
| 397 | void *payload) |
| 398 | { |
| 399 | struct reftable_ref_store *refs = payload; |
| 400 | char *tmp; |
| 401 | |
| 402 | tmp = reparent_relative_path(old_cwd, new_cwd, refs->base.gitdir); |
| 403 | free(refs->base.gitdir); |
| 404 | refs->base.gitdir = tmp; |
| 405 | } |
| 406 | |
| 407 | static struct ref_store *reftable_be_init(struct repository *repo, |
| 408 | const char *payload, |
| 409 | const char *gitdir, |
| 410 | const struct ref_store_init_options *opts) |
| 411 | { |
| 412 | struct reftable_ref_store *refs = xcalloc(1, sizeof(*refs)); |
| 413 | struct strbuf ref_common_dir = STRBUF_INIT; |
| 414 | struct strbuf refdir = STRBUF_INIT; |
| 415 | struct strbuf path = STRBUF_INIT; |
| 416 | bool is_worktree; |
| 417 | |
| 418 | refs_compute_filesystem_location(gitdir, payload, &is_worktree, &refdir, |
| 419 | &ref_common_dir); |
| 420 | |
| 421 | base_ref_store_init(&refs->base, repo, refdir.buf, &refs_be_reftable); |
| 422 | strmap_init(&refs->worktree_backends); |
| 423 | refs->store_flags = opts->access_flags; |
| 424 | |
| 425 | switch (repo->hash_algo->format_id) { |
| 426 | case GIT_SHA1_FORMAT_ID: |
| 427 | refs->stack_options.hash_id = REFTABLE_HASH_SHA1; |
| 428 | break; |
| 429 | case GIT_SHA256_FORMAT_ID: |
| 430 | refs->stack_options.hash_id = REFTABLE_HASH_SHA256; |
| 431 | break; |
| 432 | default: |
| 433 | BUG("unknown hash algorithm %d", repo->hash_algo->format_id); |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | * Set up the main reftable stack that is hosted in GIT_COMMON_DIR. |
| 438 | * This stack contains both the shared and the main worktree refs. |
| 439 | */ |
| 440 | strbuf_addbuf(&path, &ref_common_dir); |
| 441 | if (!is_worktree) { |
| 442 | strbuf_reset(&path); |
| 443 | strbuf_realpath(&path, ref_common_dir.buf, 0); |
| 444 | } |
| 445 | strbuf_addstr(&path, "/reftable"); |
| 446 | refs->err = reftable_backend_init(&refs->main_backend, path.buf, |
| 447 | &refs->stack_options); |
| 448 | if (refs->err) |
| 449 | goto done; |
| 450 | |
| 451 | /* |
| 452 | * If we're in a worktree we also need to set up the worktree reftable |
| 453 | * stack that is contained in the per-worktree GIT_DIR. |
| 454 | * |
| 455 | * Ideally, we would also add the stack to our worktree stack map. But |
| 456 | * we have no way to figure out the worktree name here and thus can't |
| 457 | * do it efficiently. |
| 458 | */ |
| 459 | if (is_worktree) { |
| 460 | strbuf_addstr(&refdir, "/reftable"); |
| 461 | |
| 462 | refs->err = reftable_backend_init(&refs->worktree_backend, refdir.buf, |
| 463 | &refs->stack_options); |
| 464 | if (refs->err) |
| 465 | goto done; |
| 466 | } |
| 467 | |
| 468 | chdir_notify_register(NULL, reftable_be_reparent, refs); |
| 469 | |
| 470 | done: |
| 471 | assert(refs->err != REFTABLE_API_ERROR); |
| 472 | strbuf_release(&ref_common_dir); |
| 473 | strbuf_release(&refdir); |
| 474 | strbuf_release(&path); |
| 475 | return &refs->base; |
| 476 | } |
| 477 | |
| 478 | static void reftable_be_release(struct ref_store *ref_store) |
| 479 | { |
| 480 | struct reftable_ref_store *refs = reftable_be_downcast(ref_store, 0, "release"); |
| 481 | struct strmap_entry *entry; |
| 482 | struct hashmap_iter iter; |
| 483 | |
| 484 | if (refs->main_backend.stack) |
| 485 | reftable_backend_release(&refs->main_backend); |
| 486 | if (refs->worktree_backend.stack) |
| 487 | reftable_backend_release(&refs->worktree_backend); |
| 488 | |
| 489 | strmap_for_each_entry(&refs->worktree_backends, &iter, entry) { |
| 490 | struct reftable_backend *be = entry->value; |
| 491 | reftable_backend_release(be); |
| 492 | free(be); |
| 493 | } |
| 494 | strmap_clear(&refs->worktree_backends, 0); |
| 495 | chdir_notify_unregister(NULL, reftable_be_reparent, refs); |
| 496 | } |
| 497 | |
| 498 | static int reftable_be_create_on_disk(struct ref_store *ref_store, |
| 499 | int flags UNUSED, |
| 500 | struct strbuf *err UNUSED) |
| 501 | { |
| 502 | struct reftable_ref_store *refs = |
| 503 | reftable_be_downcast(ref_store, REF_STORE_WRITE, "create"); |
| 504 | struct strbuf sb = STRBUF_INIT; |
| 505 | |
| 506 | strbuf_addf(&sb, "%s/reftable", refs->base.gitdir); |
| 507 | safe_create_dir(ref_store->repo, sb.buf, 1); |
| 508 | strbuf_reset(&sb); |
| 509 | |
| 510 | strbuf_release(&sb); |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | static int reftable_be_remove_on_disk(struct ref_store *ref_store, |
| 515 | struct strbuf *err) |
| 516 | { |
| 517 | struct reftable_ref_store *refs = |
| 518 | reftable_be_downcast(ref_store, REF_STORE_WRITE, "remove"); |
| 519 | struct strbuf sb = STRBUF_INIT; |
| 520 | int ret = 0; |
| 521 | |
| 522 | /* |
| 523 | * Release the ref store such that all stacks are closed. This is |
| 524 | * required so that the "tables.list" file is not open anymore, which |
| 525 | * would otherwise make it impossible to remove the file on Windows. |
| 526 | */ |
| 527 | reftable_be_release(ref_store); |
| 528 | |
| 529 | strbuf_addf(&sb, "%s/reftable", refs->base.gitdir); |
| 530 | if (remove_dir_recursively(&sb, 0) < 0) { |
| 531 | strbuf_addf(err, "could not delete reftables: %s", |
| 532 | strerror(errno)); |
| 533 | ret = -1; |
| 534 | } |
| 535 | |
| 536 | strbuf_release(&sb); |
| 537 | return ret; |
| 538 | } |
| 539 | |
| 540 | struct reftable_ref_iterator { |
| 541 | struct ref_iterator base; |
| 542 | struct reftable_ref_store *refs; |
| 543 | struct reftable_iterator iter; |
| 544 | struct reftable_ref_record ref; |
| 545 | struct object_id oid; |
| 546 | struct object_id peeled_oid; |
| 547 | |
| 548 | char *prefix; |
| 549 | size_t prefix_len; |
| 550 | char **exclude_patterns; |
| 551 | size_t exclude_patterns_index; |
| 552 | size_t exclude_patterns_strlen; |
| 553 | unsigned int flags; |
| 554 | int err; |
| 555 | }; |
| 556 | |
| 557 | /* |
| 558 | * Handle exclude patterns. Returns either `1`, which tells the caller that the |
| 559 | * current reference shall not be shown. Or `0`, which indicates that it should |
| 560 | * be shown. |
| 561 | */ |
| 562 | static int should_exclude_current_ref(struct reftable_ref_iterator *iter) |
| 563 | { |
| 564 | while (iter->exclude_patterns[iter->exclude_patterns_index]) { |
| 565 | const char *pattern = iter->exclude_patterns[iter->exclude_patterns_index]; |
| 566 | char *ref_after_pattern; |
| 567 | int cmp; |
| 568 | |
| 569 | /* |
| 570 | * Lazily cache the pattern length so that we don't have to |
| 571 | * recompute it every time this function is called. |
| 572 | */ |
| 573 | if (!iter->exclude_patterns_strlen) |
| 574 | iter->exclude_patterns_strlen = strlen(pattern); |
| 575 | |
| 576 | /* |
| 577 | * When the reference name is lexicographically bigger than the |
| 578 | * current exclude pattern we know that it won't ever match any |
| 579 | * of the following references, either. We thus advance to the |
| 580 | * next pattern and re-check whether it matches. |
| 581 | * |
| 582 | * Otherwise, if it's smaller, then we do not have a match and |
| 583 | * thus want to show the current reference. |
| 584 | */ |
| 585 | cmp = strncmp(iter->ref.refname, pattern, |
| 586 | iter->exclude_patterns_strlen); |
| 587 | if (cmp > 0) { |
| 588 | iter->exclude_patterns_index++; |
| 589 | iter->exclude_patterns_strlen = 0; |
| 590 | continue; |
| 591 | } |
| 592 | if (cmp < 0) |
| 593 | return 0; |
| 594 | |
| 595 | /* |
| 596 | * The reference shares a prefix with the exclude pattern and |
| 597 | * shall thus be omitted. We skip all references that match the |
| 598 | * pattern by seeking to the first reference after the block of |
| 599 | * matches. |
| 600 | * |
| 601 | * This is done by appending the highest possible character to |
| 602 | * the pattern. Consequently, all references that have the |
| 603 | * pattern as prefix and whose suffix starts with anything in |
| 604 | * the range [0x00, 0xfe] are skipped. And given that 0xff is a |
| 605 | * non-printable character that shouldn't ever be in a ref name, |
| 606 | * we'd not yield any such record, either. |
| 607 | * |
| 608 | * Note that the seeked-to reference may also be excluded. This |
| 609 | * is not handled here though, but the caller is expected to |
| 610 | * loop and re-verify the next reference for us. |
| 611 | */ |
| 612 | ref_after_pattern = xstrfmt("%s%c", pattern, 0xff); |
| 613 | iter->err = reftable_iterator_seek_ref(&iter->iter, ref_after_pattern); |
| 614 | iter->exclude_patterns_index++; |
| 615 | iter->exclude_patterns_strlen = 0; |
| 616 | trace2_counter_add(TRACE2_COUNTER_ID_REFTABLE_RESEEKS, 1); |
| 617 | |
| 618 | free(ref_after_pattern); |
| 619 | return 1; |
| 620 | } |
| 621 | |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | static int reftable_ref_iterator_advance(struct ref_iterator *ref_iterator) |
| 626 | { |
| 627 | struct reftable_ref_iterator *iter = |
| 628 | (struct reftable_ref_iterator *)ref_iterator; |
| 629 | struct reftable_ref_store *refs = iter->refs; |
| 630 | const char *referent = NULL; |
| 631 | |
| 632 | while (!iter->err) { |
| 633 | int flags = 0; |
| 634 | |
| 635 | iter->err = reftable_iterator_next_ref(&iter->iter, &iter->ref); |
| 636 | if (iter->err) |
| 637 | break; |
| 638 | |
| 639 | /* |
| 640 | * The files backend only lists references contained in "refs/" unless |
| 641 | * the root refs are to be included. We emulate the same behaviour here. |
| 642 | */ |
| 643 | if (!starts_with(iter->ref.refname, "refs/") && |
| 644 | !(iter->flags & REFS_FOR_EACH_INCLUDE_ROOT_REFS && |
| 645 | is_root_ref(iter->ref.refname))) { |
| 646 | continue; |
| 647 | } |
| 648 | |
| 649 | if (iter->prefix_len && |
| 650 | strncmp(iter->prefix, iter->ref.refname, iter->prefix_len)) { |
| 651 | iter->err = 1; |
| 652 | break; |
| 653 | } |
| 654 | |
| 655 | if (iter->ref.value_type == REFTABLE_REF_DELETION) |
| 656 | continue; |
| 657 | |
| 658 | if (iter->exclude_patterns && should_exclude_current_ref(iter)) |
| 659 | continue; |
| 660 | |
| 661 | if (iter->flags & REFS_FOR_EACH_PER_WORKTREE_ONLY && |
| 662 | parse_worktree_ref(iter->ref.refname, NULL, NULL, NULL) != |
| 663 | REF_WORKTREE_CURRENT) |
| 664 | continue; |
| 665 | |
| 666 | switch (iter->ref.value_type) { |
| 667 | case REFTABLE_REF_VAL1: |
| 668 | oidread(&iter->oid, iter->ref.value.val1, |
| 669 | refs->base.repo->hash_algo); |
| 670 | break; |
| 671 | case REFTABLE_REF_VAL2: |
| 672 | oidread(&iter->oid, iter->ref.value.val2.value, |
| 673 | refs->base.repo->hash_algo); |
| 674 | oidread(&iter->peeled_oid, iter->ref.value.val2.target_value, |
| 675 | refs->base.repo->hash_algo); |
| 676 | break; |
| 677 | case REFTABLE_REF_SYMREF: |
| 678 | referent = refs_resolve_ref_unsafe(&iter->refs->base, |
| 679 | iter->ref.refname, |
| 680 | RESOLVE_REF_READING, |
| 681 | &iter->oid, &flags); |
| 682 | if (!referent) |
| 683 | oidclr(&iter->oid, refs->base.repo->hash_algo); |
| 684 | break; |
| 685 | default: |
| 686 | BUG("unhandled reference value type %d", iter->ref.value_type); |
| 687 | } |
| 688 | |
| 689 | if (is_null_oid(&iter->oid)) |
| 690 | flags |= REF_ISBROKEN; |
| 691 | |
| 692 | if (check_refname_format(iter->ref.refname, REFNAME_ALLOW_ONELEVEL)) { |
| 693 | if (!refname_is_safe(iter->ref.refname)) |
| 694 | die(_("refname is dangerous: %s"), iter->ref.refname); |
| 695 | oidclr(&iter->oid, refs->base.repo->hash_algo); |
| 696 | flags |= REF_BAD_NAME | REF_ISBROKEN; |
| 697 | } |
| 698 | |
| 699 | if (iter->flags & REFS_FOR_EACH_OMIT_DANGLING_SYMREFS && |
| 700 | flags & REF_ISSYMREF && |
| 701 | flags & REF_ISBROKEN) |
| 702 | continue; |
| 703 | |
| 704 | if (!(iter->flags & REFS_FOR_EACH_INCLUDE_BROKEN) && |
| 705 | !ref_resolves_to_object(iter->ref.refname, refs->base.repo, |
| 706 | &iter->oid, flags)) |
| 707 | continue; |
| 708 | |
| 709 | memset(&iter->base.ref, 0, sizeof(iter->base.ref)); |
| 710 | iter->base.ref.name = iter->ref.refname; |
| 711 | iter->base.ref.target = referent; |
| 712 | iter->base.ref.oid = &iter->oid; |
| 713 | if (iter->ref.value_type == REFTABLE_REF_VAL2) |
| 714 | iter->base.ref.peeled_oid = &iter->peeled_oid; |
| 715 | iter->base.ref.flags = flags; |
| 716 | |
| 717 | break; |
| 718 | } |
| 719 | |
| 720 | if (iter->err > 0) |
| 721 | return ITER_DONE; |
| 722 | if (iter->err < 0) |
| 723 | return ITER_ERROR; |
| 724 | return ITER_OK; |
| 725 | } |
| 726 | |
| 727 | static int reftable_ref_iterator_seek(struct ref_iterator *ref_iterator, |
| 728 | const char *refname, unsigned int flags) |
| 729 | { |
| 730 | struct reftable_ref_iterator *iter = |
| 731 | (struct reftable_ref_iterator *)ref_iterator; |
| 732 | |
| 733 | /* Unset any previously set prefix */ |
| 734 | FREE_AND_NULL(iter->prefix); |
| 735 | iter->prefix_len = 0; |
| 736 | |
| 737 | if (flags & REF_ITERATOR_SEEK_SET_PREFIX) { |
| 738 | iter->prefix = xstrdup_or_null(refname); |
| 739 | iter->prefix_len = refname ? strlen(refname) : 0; |
| 740 | } |
| 741 | iter->err = reftable_iterator_seek_ref(&iter->iter, refname); |
| 742 | |
| 743 | return iter->err; |
| 744 | } |
| 745 | |
| 746 | static void reftable_ref_iterator_release(struct ref_iterator *ref_iterator) |
| 747 | { |
| 748 | struct reftable_ref_iterator *iter = |
| 749 | (struct reftable_ref_iterator *)ref_iterator; |
| 750 | reftable_ref_record_release(&iter->ref); |
| 751 | reftable_iterator_destroy(&iter->iter); |
| 752 | if (iter->exclude_patterns) { |
| 753 | for (size_t i = 0; iter->exclude_patterns[i]; i++) |
| 754 | free(iter->exclude_patterns[i]); |
| 755 | free(iter->exclude_patterns); |
| 756 | } |
| 757 | free(iter->prefix); |
| 758 | } |
| 759 | |
| 760 | static struct ref_iterator_vtable reftable_ref_iterator_vtable = { |
| 761 | .advance = reftable_ref_iterator_advance, |
| 762 | .seek = reftable_ref_iterator_seek, |
| 763 | .release = reftable_ref_iterator_release, |
| 764 | }; |
| 765 | |
| 766 | static int qsort_strcmp(const void *va, const void *vb) |
| 767 | { |
| 768 | const char *a = *(const char **)va; |
| 769 | const char *b = *(const char **)vb; |
| 770 | return strcmp(a, b); |
| 771 | } |
| 772 | |
| 773 | static char **filter_exclude_patterns(const char **exclude_patterns) |
| 774 | { |
| 775 | size_t filtered_size = 0, filtered_alloc = 0; |
| 776 | char **filtered = NULL; |
| 777 | |
| 778 | if (!exclude_patterns) |
| 779 | return NULL; |
| 780 | |
| 781 | for (size_t i = 0; ; i++) { |
| 782 | const char *exclude_pattern = exclude_patterns[i]; |
| 783 | int has_glob = 0; |
| 784 | |
| 785 | if (!exclude_pattern) |
| 786 | break; |
| 787 | |
| 788 | for (const char *p = exclude_pattern; *p; p++) { |
| 789 | has_glob = is_glob_special(*p); |
| 790 | if (has_glob) |
| 791 | break; |
| 792 | } |
| 793 | if (has_glob) |
| 794 | continue; |
| 795 | |
| 796 | ALLOC_GROW(filtered, filtered_size + 1, filtered_alloc); |
| 797 | filtered[filtered_size++] = xstrdup(exclude_pattern); |
| 798 | } |
| 799 | |
| 800 | if (filtered_size) { |
| 801 | QSORT(filtered, filtered_size, qsort_strcmp); |
| 802 | ALLOC_GROW(filtered, filtered_size + 1, filtered_alloc); |
| 803 | filtered[filtered_size++] = NULL; |
| 804 | } |
| 805 | |
| 806 | return filtered; |
| 807 | } |
| 808 | |
| 809 | static struct reftable_ref_iterator *ref_iterator_for_stack(struct reftable_ref_store *refs, |
| 810 | struct reftable_stack *stack, |
| 811 | const char *prefix, |
| 812 | const char **exclude_patterns, |
| 813 | int flags) |
| 814 | { |
| 815 | struct reftable_ref_iterator *iter; |
| 816 | int ret; |
| 817 | |
| 818 | iter = xcalloc(1, sizeof(*iter)); |
| 819 | base_ref_iterator_init(&iter->base, &reftable_ref_iterator_vtable); |
| 820 | iter->base.ref.oid = &iter->oid; |
| 821 | iter->flags = flags; |
| 822 | iter->refs = refs; |
| 823 | iter->exclude_patterns = filter_exclude_patterns(exclude_patterns); |
| 824 | |
| 825 | ret = refs->err; |
| 826 | if (ret) |
| 827 | goto done; |
| 828 | |
| 829 | ret = reftable_stack_reload(stack); |
| 830 | if (ret) |
| 831 | goto done; |
| 832 | |
| 833 | ret = reftable_stack_init_ref_iterator(stack, &iter->iter); |
| 834 | if (ret) |
| 835 | goto done; |
| 836 | |
| 837 | ret = reftable_ref_iterator_seek(&iter->base, prefix, |
| 838 | REF_ITERATOR_SEEK_SET_PREFIX); |
| 839 | if (ret) |
| 840 | goto done; |
| 841 | |
| 842 | done: |
| 843 | iter->err = ret; |
| 844 | return iter; |
| 845 | } |
| 846 | |
| 847 | static struct ref_iterator *reftable_be_iterator_begin(struct ref_store *ref_store, |
| 848 | const char *prefix, |
| 849 | const char **exclude_patterns, |
| 850 | unsigned int flags) |
| 851 | { |
| 852 | struct reftable_ref_iterator *main_iter, *worktree_iter; |
| 853 | struct reftable_ref_store *refs; |
| 854 | unsigned int required_flags = REF_STORE_READ; |
| 855 | |
| 856 | if (!(flags & REFS_FOR_EACH_INCLUDE_BROKEN)) |
| 857 | required_flags |= REF_STORE_ODB; |
| 858 | refs = reftable_be_downcast(ref_store, required_flags, "ref_iterator_begin"); |
| 859 | |
| 860 | main_iter = ref_iterator_for_stack(refs, refs->main_backend.stack, prefix, |
| 861 | exclude_patterns, flags); |
| 862 | |
| 863 | /* |
| 864 | * The worktree stack is only set when we're in an actual worktree |
| 865 | * right now. If we aren't, then we return the common reftable |
| 866 | * iterator, only. |
| 867 | */ |
| 868 | if (!refs->worktree_backend.stack) |
| 869 | return &main_iter->base; |
| 870 | |
| 871 | /* |
| 872 | * Otherwise we merge both the common and the per-worktree refs into a |
| 873 | * single iterator. |
| 874 | */ |
| 875 | worktree_iter = ref_iterator_for_stack(refs, refs->worktree_backend.stack, prefix, |
| 876 | exclude_patterns, flags); |
| 877 | return merge_ref_iterator_begin(&worktree_iter->base, &main_iter->base, |
| 878 | ref_iterator_select, NULL); |
| 879 | } |
| 880 | |
| 881 | static int reftable_be_read_raw_ref(struct ref_store *ref_store, |
| 882 | const char *refname, |
| 883 | struct object_id *oid, |
| 884 | struct strbuf *referent, |
| 885 | unsigned int *type, |
| 886 | int *failure_errno) |
| 887 | { |
| 888 | struct reftable_ref_store *refs = |
| 889 | reftable_be_downcast(ref_store, REF_STORE_READ, "read_raw_ref"); |
| 890 | struct reftable_backend *be; |
| 891 | int ret; |
| 892 | |
| 893 | if (refs->err < 0) |
| 894 | return refs->err; |
| 895 | |
| 896 | ret = backend_for(&be, refs, refname, &refname, 1); |
| 897 | if (ret) |
| 898 | return ret; |
| 899 | |
| 900 | ret = reftable_backend_read_ref(be, refname, oid, referent, type); |
| 901 | if (ret < 0) |
| 902 | return ret; |
| 903 | if (ret > 0) { |
| 904 | *failure_errno = ENOENT; |
| 905 | return -1; |
| 906 | } |
| 907 | |
| 908 | return 0; |
| 909 | } |
| 910 | |
| 911 | static int reftable_be_read_symbolic_ref(struct ref_store *ref_store, |
| 912 | const char *refname, |
| 913 | struct strbuf *referent) |
| 914 | { |
| 915 | struct reftable_ref_store *refs = |
| 916 | reftable_be_downcast(ref_store, REF_STORE_READ, "read_symbolic_ref"); |
| 917 | struct reftable_backend *be; |
| 918 | struct object_id oid; |
| 919 | unsigned int type = 0; |
| 920 | int ret; |
| 921 | |
| 922 | ret = backend_for(&be, refs, refname, &refname, 1); |
| 923 | if (ret) |
| 924 | return ret; |
| 925 | |
| 926 | ret = reftable_backend_read_ref(be, refname, &oid, referent, &type); |
| 927 | if (ret) |
| 928 | ret = -1; |
| 929 | else if (type == REF_ISSYMREF) |
| 930 | ; /* happy */ |
| 931 | else |
| 932 | ret = NOT_A_SYMREF; |
| 933 | return ret; |
| 934 | } |
| 935 | |
| 936 | struct reftable_transaction_update { |
| 937 | struct ref_update *update; |
| 938 | struct object_id current_oid; |
| 939 | }; |
| 940 | |
| 941 | struct write_transaction_table_arg { |
| 942 | struct reftable_ref_store *refs; |
| 943 | struct reftable_backend *be; |
| 944 | struct reftable_addition *addition; |
| 945 | struct reftable_transaction_update *updates; |
| 946 | size_t updates_nr; |
| 947 | size_t updates_alloc; |
| 948 | size_t updates_expected; |
| 949 | uint64_t max_index; |
| 950 | }; |
| 951 | |
| 952 | struct reftable_transaction_data { |
| 953 | struct write_transaction_table_arg *args; |
| 954 | size_t args_nr, args_alloc; |
| 955 | }; |
| 956 | |
| 957 | static void free_transaction_data(struct reftable_transaction_data *tx_data) |
| 958 | { |
| 959 | if (!tx_data) |
| 960 | return; |
| 961 | for (size_t i = 0; i < tx_data->args_nr; i++) { |
| 962 | reftable_addition_destroy(tx_data->args[i].addition); |
| 963 | free(tx_data->args[i].updates); |
| 964 | } |
| 965 | free(tx_data->args); |
| 966 | free(tx_data); |
| 967 | } |
| 968 | |
| 969 | /* |
| 970 | * Prepare transaction update for the given reference update. This will cause |
| 971 | * us to lock the corresponding reftable stack for concurrent modification. |
| 972 | */ |
| 973 | static int prepare_transaction_update(struct write_transaction_table_arg **out, |
| 974 | struct reftable_ref_store *refs, |
| 975 | struct reftable_transaction_data *tx_data, |
| 976 | struct ref_update *update, |
| 977 | struct strbuf *err) |
| 978 | { |
| 979 | struct write_transaction_table_arg *arg = NULL; |
| 980 | struct reftable_backend *be; |
| 981 | size_t i; |
| 982 | int ret; |
| 983 | |
| 984 | /* |
| 985 | * This function gets called in a loop, and we don't want to repeatedly |
| 986 | * reload the stack for every single ref update. Instead, we manually |
| 987 | * reload further down in the case where we haven't yet prepared the |
| 988 | * specific `reftable_backend`. |
| 989 | */ |
| 990 | ret = backend_for(&be, refs, update->refname, NULL, 0); |
| 991 | if (ret) |
| 992 | return ret; |
| 993 | |
| 994 | /* |
| 995 | * Search for a preexisting stack update. If there is one then we add |
| 996 | * the update to it, otherwise we set up a new stack update. |
| 997 | */ |
| 998 | for (i = 0; !arg && i < tx_data->args_nr; i++) |
| 999 | if (tx_data->args[i].be == be) |
| 1000 | arg = &tx_data->args[i]; |
| 1001 | |
| 1002 | if (!arg) { |
| 1003 | struct reftable_addition *addition; |
| 1004 | |
| 1005 | ret = reftable_stack_new_addition(&addition, be->stack, |
| 1006 | &reftable_be_write_options(refs)->opts, |
| 1007 | REFTABLE_STACK_NEW_ADDITION_RELOAD); |
| 1008 | if (ret) { |
| 1009 | if (ret == REFTABLE_LOCK_ERROR) |
| 1010 | strbuf_addstr(err, "cannot lock references"); |
| 1011 | return ret; |
| 1012 | } |
| 1013 | |
| 1014 | ALLOC_GROW(tx_data->args, tx_data->args_nr + 1, |
| 1015 | tx_data->args_alloc); |
| 1016 | arg = &tx_data->args[tx_data->args_nr++]; |
| 1017 | arg->refs = refs; |
| 1018 | arg->be = be; |
| 1019 | arg->addition = addition; |
| 1020 | arg->updates = NULL; |
| 1021 | arg->updates_nr = 0; |
| 1022 | arg->updates_alloc = 0; |
| 1023 | arg->updates_expected = 0; |
| 1024 | arg->max_index = 0; |
| 1025 | } |
| 1026 | |
| 1027 | arg->updates_expected++; |
| 1028 | |
| 1029 | if (out) |
| 1030 | *out = arg; |
| 1031 | |
| 1032 | return 0; |
| 1033 | } |
| 1034 | |
| 1035 | /* |
| 1036 | * Queue a reference update for the correct stack. We potentially need to |
| 1037 | * handle multiple stack updates in a single transaction when it spans across |
| 1038 | * multiple worktrees. |
| 1039 | */ |
| 1040 | static int queue_transaction_update(struct reftable_ref_store *refs, |
| 1041 | struct reftable_transaction_data *tx_data, |
| 1042 | struct ref_update *update, |
| 1043 | struct object_id *current_oid, |
| 1044 | struct strbuf *err) |
| 1045 | { |
| 1046 | struct write_transaction_table_arg *arg = NULL; |
| 1047 | int ret; |
| 1048 | |
| 1049 | if (update->backend_data) |
| 1050 | BUG("reference update queued more than once"); |
| 1051 | |
| 1052 | ret = prepare_transaction_update(&arg, refs, tx_data, update, err); |
| 1053 | if (ret < 0) |
| 1054 | return ret; |
| 1055 | |
| 1056 | ALLOC_GROW(arg->updates, arg->updates_nr + 1, |
| 1057 | arg->updates_alloc); |
| 1058 | arg->updates[arg->updates_nr].update = update; |
| 1059 | oidcpy(&arg->updates[arg->updates_nr].current_oid, current_oid); |
| 1060 | update->backend_data = &arg->updates[arg->updates_nr++]; |
| 1061 | |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
| 1065 | static enum ref_transaction_error prepare_single_update(struct reftable_ref_store *refs, |
| 1066 | struct reftable_transaction_data *tx_data, |
| 1067 | struct ref_transaction *transaction, |
| 1068 | struct reftable_backend *be, |
| 1069 | struct ref_update *u, |
| 1070 | size_t update_idx, |
| 1071 | struct string_list *refnames_to_check, |
| 1072 | unsigned int head_type, |
| 1073 | struct strbuf *head_referent, |
| 1074 | struct strbuf *referent, |
| 1075 | struct strbuf *err) |
| 1076 | { |
| 1077 | enum ref_transaction_error ret = 0; |
| 1078 | struct object_id current_oid = {0}; |
| 1079 | const char *rewritten_ref; |
| 1080 | |
| 1081 | /* |
| 1082 | * There is no need to reload the respective backends here as |
| 1083 | * we have already reloaded them when preparing the transaction |
| 1084 | * update. And given that the stacks have been locked there |
| 1085 | * shouldn't have been any concurrent modifications of the |
| 1086 | * stack. |
| 1087 | */ |
| 1088 | ret = backend_for(&be, refs, u->refname, &rewritten_ref, 0); |
| 1089 | if (ret) |
| 1090 | return REF_TRANSACTION_ERROR_GENERIC; |
| 1091 | |
| 1092 | if (u->flags & REF_LOG_USE_PROVIDED_OIDS) { |
| 1093 | if (!(u->flags & REF_HAVE_OLD) || |
| 1094 | !(u->flags & REF_HAVE_NEW) || |
| 1095 | !(u->flags & REF_LOG_ONLY)) { |
| 1096 | strbuf_addf(err, _("trying to write reflog for '%s' " |
| 1097 | "with incomplete values"), u->refname); |
| 1098 | return REF_TRANSACTION_ERROR_GENERIC; |
| 1099 | } |
| 1100 | |
| 1101 | if (queue_transaction_update(refs, tx_data, u, &u->old_oid, err)) |
| 1102 | return REF_TRANSACTION_ERROR_GENERIC; |
| 1103 | return 0; |
| 1104 | } |
| 1105 | |
| 1106 | /* |
| 1107 | * When we update the reference that HEAD points to we enqueue |
| 1108 | * a second log-only update for HEAD so that its reflog is |
| 1109 | * updated accordingly. |
| 1110 | */ |
| 1111 | if (head_type == REF_ISSYMREF && |
| 1112 | !(u->flags & REF_LOG_ONLY) && |
| 1113 | !(u->flags & REF_UPDATE_VIA_HEAD) && |
| 1114 | !strcmp(rewritten_ref, head_referent->buf)) { |
| 1115 | /* |
| 1116 | * First make sure that HEAD is not already in the |
| 1117 | * transaction. This check is O(lg N) in the transaction |
| 1118 | * size, but it happens at most once per transaction. |
| 1119 | */ |
| 1120 | if (string_list_has_string(&transaction->refnames, "HEAD")) { |
| 1121 | /* An entry already existed */ |
| 1122 | strbuf_addf(err, |
| 1123 | _("multiple updates for 'HEAD' (including one " |
| 1124 | "via its referent '%s') are not allowed"), |
| 1125 | u->refname); |
| 1126 | return REF_TRANSACTION_ERROR_NAME_CONFLICT; |
| 1127 | } |
| 1128 | |
| 1129 | ref_transaction_add_update( |
| 1130 | transaction, "HEAD", |
| 1131 | u->flags | REF_LOG_ONLY | REF_NO_DEREF, |
| 1132 | &u->new_oid, &u->old_oid, &u->peeled, NULL, NULL, |
| 1133 | NULL, u->msg); |
| 1134 | } |
| 1135 | |
| 1136 | ret = reftable_backend_read_ref(be, rewritten_ref, |
| 1137 | ¤t_oid, referent, &u->type); |
| 1138 | if (ret < 0) |
| 1139 | return REF_TRANSACTION_ERROR_GENERIC; |
| 1140 | if (ret > 0 && !ref_update_expects_existing_old_ref(u)) { |
| 1141 | struct string_list_item *item; |
| 1142 | /* |
| 1143 | * The reference does not exist, and we either have no |
| 1144 | * old object ID or expect the reference to not exist. |
| 1145 | * We can thus skip below safety checks as well as the |
| 1146 | * symref splitting. But we do want to verify that |
| 1147 | * there is no conflicting reference here so that we |
| 1148 | * can output a proper error message instead of failing |
| 1149 | * at a later point. |
| 1150 | */ |
| 1151 | item = string_list_append(refnames_to_check, u->refname); |
| 1152 | item->util = xmalloc(sizeof(update_idx)); |
| 1153 | memcpy(item->util, &update_idx, sizeof(update_idx)); |
| 1154 | |
| 1155 | /* |
| 1156 | * There is no need to write the reference deletion |
| 1157 | * when the reference in question doesn't exist. |
| 1158 | */ |
| 1159 | if ((u->flags & REF_HAVE_NEW) && !ref_update_has_null_new_value(u)) { |
| 1160 | ret = queue_transaction_update(refs, tx_data, u, |
| 1161 | ¤t_oid, err); |
| 1162 | if (ret) |
| 1163 | return REF_TRANSACTION_ERROR_GENERIC; |
| 1164 | } |
| 1165 | |
| 1166 | return 0; |
| 1167 | } |
| 1168 | if (ret > 0) { |
| 1169 | /* The reference does not exist, but we expected it to. */ |
| 1170 | strbuf_addf(err, _("cannot lock ref '%s': " |
| 1171 | "unable to resolve reference '%s'"), |
| 1172 | ref_update_original_update_refname(u), u->refname); |
| 1173 | return REF_TRANSACTION_ERROR_NONEXISTENT_REF; |
| 1174 | } |
| 1175 | |
| 1176 | if (u->type & REF_ISSYMREF) { |
| 1177 | /* |
| 1178 | * The reftable stack is locked at this point already, |
| 1179 | * so it is safe to call `refs_resolve_ref_unsafe()` |
| 1180 | * here without causing races. |
| 1181 | */ |
| 1182 | const char *resolved = refs_resolve_ref_unsafe(&refs->base, u->refname, 0, |
| 1183 | ¤t_oid, NULL); |
| 1184 | |
| 1185 | if (u->flags & REF_NO_DEREF) { |
| 1186 | if (u->flags & REF_HAVE_OLD && !resolved) { |
| 1187 | strbuf_addf(err, _("cannot lock ref '%s': " |
| 1188 | "error reading reference"), u->refname); |
| 1189 | return REF_TRANSACTION_ERROR_GENERIC; |
| 1190 | } |
| 1191 | } else { |
| 1192 | struct ref_update *new_update; |
| 1193 | int new_flags; |
| 1194 | |
| 1195 | new_flags = u->flags; |
| 1196 | if (!strcmp(rewritten_ref, "HEAD")) |
| 1197 | new_flags |= REF_UPDATE_VIA_HEAD; |
| 1198 | |
| 1199 | if (string_list_has_string(&transaction->refnames, referent->buf)) { |
| 1200 | strbuf_addf(err, |
| 1201 | _("multiple updates for '%s' (including one " |
| 1202 | "via symref '%s') are not allowed"), |
| 1203 | referent->buf, u->refname); |
| 1204 | return REF_TRANSACTION_ERROR_NAME_CONFLICT; |
| 1205 | } |
| 1206 | |
| 1207 | /* |
| 1208 | * If we are updating a symref (eg. HEAD), we should also |
| 1209 | * update the branch that the symref points to. |
| 1210 | * |
| 1211 | * This is generic functionality, and would be better |
| 1212 | * done in refs.c, but the current implementation is |
| 1213 | * intertwined with the locking in files-backend.c. |
| 1214 | */ |
| 1215 | new_update = ref_transaction_add_update( |
| 1216 | transaction, referent->buf, new_flags, |
| 1217 | u->new_target ? NULL : &u->new_oid, |
| 1218 | u->old_target ? NULL : &u->old_oid, |
| 1219 | &u->peeled, u->new_target, u->old_target, |
| 1220 | u->committer_info, u->msg); |
| 1221 | |
| 1222 | new_update->parent_update = u; |
| 1223 | |
| 1224 | /* Change the symbolic ref update to log only. */ |
| 1225 | u->flags |= REF_LOG_ONLY | REF_NO_DEREF; |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | /* |
| 1230 | * Verify that the old object matches our expectations. Note |
| 1231 | * that the error messages here do not make a lot of sense in |
| 1232 | * the context of the reftable backend as we never lock |
| 1233 | * individual refs. But the error messages match what the files |
| 1234 | * backend returns, which keeps our tests happy. |
| 1235 | */ |
| 1236 | if (u->old_target) { |
| 1237 | if (!(u->type & REF_ISSYMREF)) { |
| 1238 | strbuf_addf(err, _("cannot lock ref '%s': " |
| 1239 | "expected symref with target '%s': " |
| 1240 | "but is a regular ref"), |
| 1241 | ref_update_original_update_refname(u), |
| 1242 | u->old_target); |
| 1243 | return REF_TRANSACTION_ERROR_EXPECTED_SYMREF; |
| 1244 | } |
| 1245 | |
| 1246 | ret = ref_update_check_old_target(referent->buf, u, err); |
| 1247 | if (ret) |
| 1248 | return ret; |
| 1249 | } else if ((u->flags & (REF_LOG_ONLY | REF_HAVE_OLD)) == REF_HAVE_OLD) { |
| 1250 | if (oideq(¤t_oid, &u->old_oid)) { |
| 1251 | /* |
| 1252 | * Normally matching the expected old oid is enough. Either we |
| 1253 | * found the ref at the expected state, or we are creating and |
| 1254 | * expect the null oid (and likewise found nothing). |
| 1255 | * |
| 1256 | * But there is one exception for the null oid: if we found a |
| 1257 | * symref pointing to nothing we'll also get the null oid. In |
| 1258 | * regular recursive mode, that's good (we'll write to what the |
| 1259 | * symref points to, which doesn't exist). But in no-deref |
| 1260 | * mode, it means we'll clobber the symref, even though the |
| 1261 | * caller asked for this to be a creation event. So flag |
| 1262 | * that case to preserve the dangling symref. |
| 1263 | * |
| 1264 | * Everything else is OK and we can fall through to the |
| 1265 | * end of the conditional chain. |
| 1266 | */ |
| 1267 | if ((u->flags & REF_NO_DEREF) && |
| 1268 | referent->len && |
| 1269 | is_null_oid(&u->old_oid)) { |
| 1270 | strbuf_addf(err, _("cannot lock ref '%s': " |
| 1271 | "dangling symref already exists"), |
| 1272 | ref_update_original_update_refname(u)); |
| 1273 | return REF_TRANSACTION_ERROR_CREATE_EXISTS; |
| 1274 | } |
| 1275 | } else if (is_null_oid(&u->old_oid)) { |
| 1276 | strbuf_addf(err, _("cannot lock ref '%s': " |
| 1277 | "reference already exists"), |
| 1278 | ref_update_original_update_refname(u)); |
| 1279 | return REF_TRANSACTION_ERROR_CREATE_EXISTS; |
| 1280 | } else if (is_null_oid(¤t_oid)) { |
| 1281 | strbuf_addf(err, _("cannot lock ref '%s': " |
| 1282 | "reference is missing but expected %s"), |
| 1283 | ref_update_original_update_refname(u), |
| 1284 | oid_to_hex(&u->old_oid)); |
| 1285 | return REF_TRANSACTION_ERROR_NONEXISTENT_REF; |
| 1286 | } else { |
| 1287 | strbuf_addf(err, _("cannot lock ref '%s': " |
| 1288 | "is at %s but expected %s"), |
| 1289 | ref_update_original_update_refname(u), |
| 1290 | oid_to_hex(¤t_oid), |
| 1291 | oid_to_hex(&u->old_oid)); |
| 1292 | return REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE; |
| 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | /* |
| 1297 | * If all of the following conditions are true: |
| 1298 | * |
| 1299 | * - We're not about to write a symref. |
| 1300 | * - We're not about to write a log-only entry. |
| 1301 | * - Old and new object ID are different. |
| 1302 | * |
| 1303 | * Then we're essentially doing a no-op update that can be |
| 1304 | * skipped. This is not only for the sake of efficiency, but |
| 1305 | * also skips writing unneeded reflog entries. |
| 1306 | */ |
| 1307 | if ((u->type & REF_ISSYMREF) || |
| 1308 | (u->flags & REF_LOG_ONLY) || |
| 1309 | (u->flags & REF_HAVE_NEW && !oideq(¤t_oid, &u->new_oid))) |
| 1310 | if (queue_transaction_update(refs, tx_data, u, ¤t_oid, err)) |
| 1311 | return REF_TRANSACTION_ERROR_GENERIC; |
| 1312 | |
| 1313 | return 0; |
| 1314 | } |
| 1315 | |
| 1316 | static int reftable_be_transaction_prepare(struct ref_store *ref_store, |
| 1317 | struct ref_transaction *transaction, |
| 1318 | struct strbuf *err) |
| 1319 | { |
| 1320 | struct reftable_ref_store *refs = |
| 1321 | reftable_be_downcast(ref_store, REF_STORE_WRITE|REF_STORE_MAIN, "ref_transaction_prepare"); |
| 1322 | struct strbuf referent = STRBUF_INIT, head_referent = STRBUF_INIT; |
| 1323 | struct string_list refnames_to_check = STRING_LIST_INIT_NODUP; |
| 1324 | struct reftable_transaction_data *tx_data = NULL; |
| 1325 | struct reftable_backend *be; |
| 1326 | struct object_id head_oid; |
| 1327 | unsigned int head_type = 0; |
| 1328 | size_t i; |
| 1329 | int ret; |
| 1330 | |
| 1331 | ret = refs->err; |
| 1332 | if (ret < 0) |
| 1333 | goto done; |
| 1334 | |
| 1335 | tx_data = xcalloc(1, sizeof(*tx_data)); |
| 1336 | |
| 1337 | /* |
| 1338 | * Preprocess all updates. For one we check that there are no duplicate |
| 1339 | * reference updates in this transaction. Second, we lock all stacks |
| 1340 | * that will be modified during the transaction. |
| 1341 | */ |
| 1342 | for (i = 0; i < transaction->nr; i++) { |
| 1343 | ret = prepare_transaction_update(NULL, refs, tx_data, |
| 1344 | transaction->updates[i], err); |
| 1345 | if (ret) |
| 1346 | goto done; |
| 1347 | } |
| 1348 | |
| 1349 | /* |
| 1350 | * Now that we have counted updates per stack we can preallocate their |
| 1351 | * arrays. This avoids having to reallocate many times. |
| 1352 | */ |
| 1353 | for (i = 0; i < tx_data->args_nr; i++) { |
| 1354 | CALLOC_ARRAY(tx_data->args[i].updates, tx_data->args[i].updates_expected); |
| 1355 | tx_data->args[i].updates_alloc = tx_data->args[i].updates_expected; |
| 1356 | } |
| 1357 | |
| 1358 | /* |
| 1359 | * TODO: it's dubious whether we should reload the stack that "HEAD" |
| 1360 | * belongs to or not. In theory, it may happen that we only modify |
| 1361 | * stacks which are _not_ part of the "HEAD" stack. In that case we |
| 1362 | * wouldn't have prepared any transaction for its stack and would not |
| 1363 | * have reloaded it, which may mean that it is stale. |
| 1364 | * |
| 1365 | * On the other hand, reloading that stack without locking it feels |
| 1366 | * wrong, too, as the value of "HEAD" could be modified concurrently at |
| 1367 | * any point in time. |
| 1368 | */ |
| 1369 | ret = backend_for(&be, refs, "HEAD", NULL, 0); |
| 1370 | if (ret) |
| 1371 | goto done; |
| 1372 | |
| 1373 | ret = reftable_backend_read_ref(be, "HEAD", &head_oid, |
| 1374 | &head_referent, &head_type); |
| 1375 | if (ret < 0) |
| 1376 | goto done; |
| 1377 | ret = 0; |
| 1378 | |
| 1379 | for (i = 0; i < transaction->nr; i++) { |
| 1380 | ret = prepare_single_update(refs, tx_data, transaction, be, |
| 1381 | transaction->updates[i], i, |
| 1382 | &refnames_to_check, head_type, |
| 1383 | &head_referent, &referent, err); |
| 1384 | if (ret) { |
| 1385 | if (ref_transaction_maybe_set_rejected(transaction, i, |
| 1386 | ret, err)) { |
| 1387 | ret = 0; |
| 1388 | continue; |
| 1389 | } |
| 1390 | goto done; |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | ret = refs_verify_refnames_available(ref_store, &refnames_to_check, |
| 1395 | &transaction->refnames, NULL, |
| 1396 | transaction, |
| 1397 | transaction->flags & REF_TRANSACTION_FLAG_INITIAL, |
| 1398 | err); |
| 1399 | if (ret < 0) |
| 1400 | goto done; |
| 1401 | |
| 1402 | transaction->backend_data = tx_data; |
| 1403 | transaction->state = REF_TRANSACTION_PREPARED; |
| 1404 | |
| 1405 | done: |
| 1406 | if (ret < 0) { |
| 1407 | free_transaction_data(tx_data); |
| 1408 | transaction->state = REF_TRANSACTION_CLOSED; |
| 1409 | if (!err->len) |
| 1410 | strbuf_addf(err, _("reftable: transaction prepare: %s"), |
| 1411 | reftable_error_str(ret)); |
| 1412 | } |
| 1413 | strbuf_release(&referent); |
| 1414 | strbuf_release(&head_referent); |
| 1415 | string_list_clear(&refnames_to_check, 1); |
| 1416 | |
| 1417 | return ret; |
| 1418 | } |
| 1419 | |
| 1420 | static int reftable_be_transaction_abort(struct ref_store *ref_store UNUSED, |
| 1421 | struct ref_transaction *transaction, |
| 1422 | struct strbuf *err UNUSED) |
| 1423 | { |
| 1424 | struct reftable_transaction_data *tx_data = transaction->backend_data; |
| 1425 | free_transaction_data(tx_data); |
| 1426 | transaction->state = REF_TRANSACTION_CLOSED; |
| 1427 | return 0; |
| 1428 | } |
| 1429 | |
| 1430 | static int transaction_update_cmp(const void *a, const void *b) |
| 1431 | { |
| 1432 | struct reftable_transaction_update *update_a = (struct reftable_transaction_update *)a; |
| 1433 | struct reftable_transaction_update *update_b = (struct reftable_transaction_update *)b; |
| 1434 | |
| 1435 | /* |
| 1436 | * If there is an index set, it should take preference (default is 0). |
| 1437 | * This ensures that updates with indexes are sorted amongst themselves. |
| 1438 | */ |
| 1439 | if (update_a->update->index || update_b->update->index) |
| 1440 | return update_a->update->index - update_b->update->index; |
| 1441 | |
| 1442 | return strcmp(update_a->update->refname, update_b->update->refname); |
| 1443 | } |
| 1444 | |
| 1445 | static int should_write_log(struct reftable_ref_store *refs, const char *refname) |
| 1446 | { |
| 1447 | enum log_refs_config log_refs_cfg = reftable_be_write_options(refs)->log_all_ref_updates; |
| 1448 | if (log_refs_cfg == LOG_REFS_UNSET) |
| 1449 | log_refs_cfg = is_bare_repository(refs->base.repo) ? LOG_REFS_NONE : LOG_REFS_NORMAL; |
| 1450 | |
| 1451 | switch (log_refs_cfg) { |
| 1452 | case LOG_REFS_NONE: |
| 1453 | return refs_reflog_exists(&refs->base, refname); |
| 1454 | case LOG_REFS_ALWAYS: |
| 1455 | return 1; |
| 1456 | case LOG_REFS_NORMAL: |
| 1457 | if (should_autocreate_reflog(log_refs_cfg, refname)) |
| 1458 | return 1; |
| 1459 | return refs_reflog_exists(&refs->base, refname); |
| 1460 | default: |
| 1461 | BUG("unhandled core.logAllRefUpdates value %d", log_refs_cfg); |
| 1462 | } |
| 1463 | } |
| 1464 | |
| 1465 | static int write_transaction_table(struct reftable_writer *writer, void *cb_data) |
| 1466 | { |
| 1467 | struct write_transaction_table_arg *arg = cb_data; |
| 1468 | uint64_t ts = reftable_stack_next_update_index(arg->be->stack); |
| 1469 | struct reftable_log_record *logs = NULL; |
| 1470 | struct ident_split committer_ident = {0}; |
| 1471 | size_t logs_nr = 0, logs_alloc = 0, i; |
| 1472 | const char *committer_info; |
| 1473 | int ret = 0; |
| 1474 | |
| 1475 | committer_info = git_committer_info(0); |
| 1476 | if (split_ident_line(&committer_ident, committer_info, strlen(committer_info))) |
| 1477 | BUG("failed splitting committer info"); |
| 1478 | |
| 1479 | QSORT(arg->updates, arg->updates_nr, transaction_update_cmp); |
| 1480 | |
| 1481 | /* |
| 1482 | * During reflog migration, we add indexes for a single reflog with |
| 1483 | * multiple entries. Each entry will contain a different update_index, |
| 1484 | * so set the limits accordingly. |
| 1485 | */ |
| 1486 | ret = reftable_writer_set_limits(writer, ts, ts + arg->max_index); |
| 1487 | if (ret < 0) |
| 1488 | goto done; |
| 1489 | |
| 1490 | for (i = 0; i < arg->updates_nr; i++) { |
| 1491 | struct reftable_transaction_update *tx_update = &arg->updates[i]; |
| 1492 | struct ref_update *u = tx_update->update; |
| 1493 | |
| 1494 | if (u->rejection_err) |
| 1495 | continue; |
| 1496 | |
| 1497 | /* |
| 1498 | * Write a reflog entry when updating a ref to point to |
| 1499 | * something new in either of the following cases: |
| 1500 | * |
| 1501 | * - The reference is about to be deleted. We always want to |
| 1502 | * delete the reflog in that case. |
| 1503 | * - REF_FORCE_CREATE_REFLOG is set, asking us to always create |
| 1504 | * the reflog entry. |
| 1505 | * - `core.logAllRefUpdates` tells us to create the reflog for |
| 1506 | * the given ref. |
| 1507 | */ |
| 1508 | if ((u->flags & REF_HAVE_NEW) && |
| 1509 | !(u->type & REF_ISSYMREF) && |
| 1510 | ref_update_has_null_new_value(u)) { |
| 1511 | struct reftable_log_record log = {0}; |
| 1512 | struct reftable_iterator it = {0}; |
| 1513 | |
| 1514 | ret = reftable_stack_init_log_iterator(arg->be->stack, &it); |
| 1515 | if (ret < 0) |
| 1516 | goto done; |
| 1517 | |
| 1518 | /* |
| 1519 | * When deleting refs we also delete all reflog entries |
| 1520 | * with them. While it is not strictly required to |
| 1521 | * delete reflogs together with their refs, this |
| 1522 | * matches the behaviour of the files backend. |
| 1523 | * |
| 1524 | * Unfortunately, we have no better way than to delete |
| 1525 | * all reflog entries one by one. |
| 1526 | */ |
| 1527 | ret = reftable_iterator_seek_log(&it, u->refname); |
| 1528 | while (ret == 0) { |
| 1529 | struct reftable_log_record *tombstone; |
| 1530 | |
| 1531 | ret = reftable_iterator_next_log(&it, &log); |
| 1532 | if (ret < 0) |
| 1533 | break; |
| 1534 | if (ret > 0 || strcmp(log.refname, u->refname)) { |
| 1535 | ret = 0; |
| 1536 | break; |
| 1537 | } |
| 1538 | if (reftable_log_record_is_deletion(&log)) |
| 1539 | continue; |
| 1540 | |
| 1541 | ALLOC_GROW(logs, logs_nr + 1, logs_alloc); |
| 1542 | tombstone = &logs[logs_nr++]; |
| 1543 | tombstone->refname = xstrdup(u->refname); |
| 1544 | tombstone->value_type = REFTABLE_LOG_DELETION; |
| 1545 | tombstone->update_index = log.update_index; |
| 1546 | } |
| 1547 | |
| 1548 | reftable_log_record_release(&log); |
| 1549 | reftable_iterator_destroy(&it); |
| 1550 | |
| 1551 | if (ret) |
| 1552 | goto done; |
| 1553 | } else if (!(u->flags & REF_SKIP_CREATE_REFLOG) && |
| 1554 | (u->flags & REF_HAVE_NEW) && |
| 1555 | (u->flags & REF_FORCE_CREATE_REFLOG || |
| 1556 | should_write_log(arg->refs, u->refname))) { |
| 1557 | struct reftable_log_record *log; |
| 1558 | int create_reflog = 1; |
| 1559 | |
| 1560 | if (u->new_target) { |
| 1561 | if (!refs_resolve_ref_unsafe(&arg->refs->base, u->new_target, |
| 1562 | RESOLVE_REF_READING, &u->new_oid, NULL)) { |
| 1563 | /* |
| 1564 | * TODO: currently we skip creating reflogs for dangling |
| 1565 | * symref updates. It would be nice to capture this as |
| 1566 | * zero oid updates however. |
| 1567 | */ |
| 1568 | create_reflog = 0; |
| 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | if (create_reflog) { |
| 1573 | struct ident_split c; |
| 1574 | |
| 1575 | ALLOC_GROW(logs, logs_nr + 1, logs_alloc); |
| 1576 | log = &logs[logs_nr++]; |
| 1577 | memset(log, 0, sizeof(*log)); |
| 1578 | |
| 1579 | if (u->committer_info) { |
| 1580 | if (split_ident_line(&c, u->committer_info, |
| 1581 | strlen(u->committer_info))) |
| 1582 | BUG("failed splitting committer info"); |
| 1583 | } else { |
| 1584 | c = committer_ident; |
| 1585 | } |
| 1586 | |
| 1587 | fill_reftable_log_record(log, &c); |
| 1588 | |
| 1589 | /* |
| 1590 | * Updates are sorted by the writer. So updates for the same |
| 1591 | * refname need to contain different update indices. |
| 1592 | */ |
| 1593 | log->update_index = ts + u->index; |
| 1594 | |
| 1595 | log->refname = xstrdup(u->refname); |
| 1596 | memcpy(log->value.update.new_hash, |
| 1597 | u->new_oid.hash, GIT_MAX_RAWSZ); |
| 1598 | memcpy(log->value.update.old_hash, |
| 1599 | tx_update->current_oid.hash, GIT_MAX_RAWSZ); |
| 1600 | log->value.update.message = |
| 1601 | xstrndup(u->msg, reftable_be_write_options(arg->refs)->opts.block_size / 2); |
| 1602 | } |
| 1603 | } |
| 1604 | |
| 1605 | if (u->flags & REF_LOG_ONLY) |
| 1606 | continue; |
| 1607 | |
| 1608 | if (u->new_target) { |
| 1609 | struct reftable_ref_record ref = { |
| 1610 | .refname = (char *)u->refname, |
| 1611 | .value_type = REFTABLE_REF_SYMREF, |
| 1612 | .value.symref = (char *)u->new_target, |
| 1613 | .update_index = ts, |
| 1614 | }; |
| 1615 | |
| 1616 | ret = reftable_writer_add_ref(writer, &ref); |
| 1617 | if (ret < 0) |
| 1618 | goto done; |
| 1619 | } else if ((u->flags & REF_HAVE_NEW) && ref_update_has_null_new_value(u)) { |
| 1620 | struct reftable_ref_record ref = { |
| 1621 | .refname = (char *)u->refname, |
| 1622 | .update_index = ts, |
| 1623 | .value_type = REFTABLE_REF_DELETION, |
| 1624 | }; |
| 1625 | |
| 1626 | ret = reftable_writer_add_ref(writer, &ref); |
| 1627 | if (ret < 0) |
| 1628 | goto done; |
| 1629 | } else if (u->flags & REF_HAVE_NEW) { |
| 1630 | struct reftable_ref_record ref = {0}; |
| 1631 | |
| 1632 | ref.refname = (char *)u->refname; |
| 1633 | ref.update_index = ts; |
| 1634 | |
| 1635 | if (u->flags & REF_HAVE_PEELED) { |
| 1636 | ref.value_type = REFTABLE_REF_VAL2; |
| 1637 | memcpy(ref.value.val2.target_value, u->peeled.hash, GIT_MAX_RAWSZ); |
| 1638 | memcpy(ref.value.val2.value, u->new_oid.hash, GIT_MAX_RAWSZ); |
| 1639 | } else if (!is_null_oid(&u->new_oid)) { |
| 1640 | ref.value_type = REFTABLE_REF_VAL1; |
| 1641 | memcpy(ref.value.val1, u->new_oid.hash, GIT_MAX_RAWSZ); |
| 1642 | } |
| 1643 | |
| 1644 | ret = reftable_writer_add_ref(writer, &ref); |
| 1645 | if (ret < 0) |
| 1646 | goto done; |
| 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | /* |
| 1651 | * Logs are written at the end so that we do not have intermixed ref |
| 1652 | * and log blocks. |
| 1653 | */ |
| 1654 | if (logs) { |
| 1655 | ret = reftable_writer_add_logs(writer, logs, logs_nr); |
| 1656 | if (ret < 0) |
| 1657 | goto done; |
| 1658 | } |
| 1659 | |
| 1660 | done: |
| 1661 | assert(ret != REFTABLE_API_ERROR); |
| 1662 | for (i = 0; i < logs_nr; i++) |
| 1663 | reftable_log_record_release(&logs[i]); |
| 1664 | free(logs); |
| 1665 | return ret; |
| 1666 | } |
| 1667 | |
| 1668 | static int reftable_be_transaction_finish(struct ref_store *ref_store UNUSED, |
| 1669 | struct ref_transaction *transaction, |
| 1670 | struct strbuf *err) |
| 1671 | { |
| 1672 | struct reftable_transaction_data *tx_data = transaction->backend_data; |
| 1673 | int ret = 0; |
| 1674 | |
| 1675 | for (size_t i = 0; i < tx_data->args_nr; i++) { |
| 1676 | tx_data->args[i].max_index = transaction->max_index; |
| 1677 | |
| 1678 | ret = reftable_addition_add(tx_data->args[i].addition, |
| 1679 | write_transaction_table, &tx_data->args[i]); |
| 1680 | if (ret < 0) |
| 1681 | goto done; |
| 1682 | |
| 1683 | ret = reftable_addition_commit(tx_data->args[i].addition); |
| 1684 | if (ret < 0) |
| 1685 | goto done; |
| 1686 | } |
| 1687 | |
| 1688 | done: |
| 1689 | assert(ret != REFTABLE_API_ERROR); |
| 1690 | free_transaction_data(tx_data); |
| 1691 | transaction->state = REF_TRANSACTION_CLOSED; |
| 1692 | |
| 1693 | if (ret) { |
| 1694 | strbuf_addf(err, _("reftable: transaction failure: %s"), |
| 1695 | reftable_error_str(ret)); |
| 1696 | return -1; |
| 1697 | } |
| 1698 | return ret; |
| 1699 | } |
| 1700 | |
| 1701 | static int reftable_be_optimize(struct ref_store *ref_store, |
| 1702 | struct refs_optimize_opts *opts) |
| 1703 | { |
| 1704 | struct reftable_ref_store *refs = |
| 1705 | reftable_be_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB, "optimize_refs"); |
| 1706 | struct reftable_stack *stack; |
| 1707 | int ret; |
| 1708 | |
| 1709 | if (refs->err) |
| 1710 | return refs->err; |
| 1711 | |
| 1712 | stack = refs->worktree_backend.stack; |
| 1713 | if (!stack) |
| 1714 | stack = refs->main_backend.stack; |
| 1715 | |
| 1716 | if (opts->flags & REFS_OPTIMIZE_AUTO) |
| 1717 | ret = reftable_stack_auto_compact(stack, &reftable_be_write_options(refs)->opts); |
| 1718 | else |
| 1719 | ret = reftable_stack_compact_all(stack, &reftable_be_write_options(refs)->opts, NULL); |
| 1720 | if (ret < 0) { |
| 1721 | ret = error(_("unable to compact stack: %s"), |
| 1722 | reftable_error_str(ret)); |
| 1723 | goto out; |
| 1724 | } |
| 1725 | |
| 1726 | ret = reftable_stack_clean(stack); |
| 1727 | if (ret) |
| 1728 | goto out; |
| 1729 | |
| 1730 | out: |
| 1731 | return ret; |
| 1732 | } |
| 1733 | |
| 1734 | static int reftable_be_optimize_required(struct ref_store *ref_store, |
| 1735 | struct refs_optimize_opts *opts, |
| 1736 | bool *required) |
| 1737 | { |
| 1738 | struct reftable_ref_store *refs = reftable_be_downcast(ref_store, REF_STORE_READ, |
| 1739 | "optimize_refs_required"); |
| 1740 | struct reftable_stack *stack; |
| 1741 | bool use_heuristics = false; |
| 1742 | |
| 1743 | if (refs->err) |
| 1744 | return refs->err; |
| 1745 | |
| 1746 | stack = refs->worktree_backend.stack; |
| 1747 | if (!stack) |
| 1748 | stack = refs->main_backend.stack; |
| 1749 | |
| 1750 | if (opts->flags & REFS_OPTIMIZE_AUTO) |
| 1751 | use_heuristics = true; |
| 1752 | |
| 1753 | return reftable_stack_compaction_required(stack, &reftable_be_write_options(refs)->opts, |
| 1754 | use_heuristics, required); |
| 1755 | } |
| 1756 | |
| 1757 | struct write_create_symref_arg { |
| 1758 | struct reftable_ref_store *refs; |
| 1759 | struct reftable_stack *stack; |
| 1760 | struct strbuf *err; |
| 1761 | const char *refname; |
| 1762 | const char *target; |
| 1763 | const char *logmsg; |
| 1764 | }; |
| 1765 | |
| 1766 | struct write_copy_arg { |
| 1767 | struct reftable_ref_store *refs; |
| 1768 | struct reftable_backend *be; |
| 1769 | const char *oldname; |
| 1770 | const char *newname; |
| 1771 | const char *logmsg; |
| 1772 | int delete_old; |
| 1773 | }; |
| 1774 | |
| 1775 | static int write_copy_table(struct reftable_writer *writer, void *cb_data) |
| 1776 | { |
| 1777 | struct write_copy_arg *arg = cb_data; |
| 1778 | uint64_t deletion_ts, creation_ts; |
| 1779 | struct reftable_ref_record old_ref = {0}, refs[2] = {0}; |
| 1780 | struct reftable_log_record old_log = {0}, *logs = NULL; |
| 1781 | struct reftable_iterator it = {0}; |
| 1782 | struct string_list skip = STRING_LIST_INIT_NODUP; |
| 1783 | struct ident_split committer_ident = {0}; |
| 1784 | struct strbuf errbuf = STRBUF_INIT; |
| 1785 | size_t logs_nr = 0, logs_alloc = 0, i; |
| 1786 | const char *committer_info; |
| 1787 | int ret; |
| 1788 | |
| 1789 | committer_info = git_committer_info(0); |
| 1790 | if (split_ident_line(&committer_ident, committer_info, strlen(committer_info))) |
| 1791 | BUG("failed splitting committer info"); |
| 1792 | |
| 1793 | if (reftable_stack_read_ref(arg->be->stack, arg->oldname, &old_ref)) { |
| 1794 | ret = error(_("refname %s not found"), arg->oldname); |
| 1795 | goto done; |
| 1796 | } |
| 1797 | if (old_ref.value_type == REFTABLE_REF_SYMREF) { |
| 1798 | ret = error(_("refname %s is a symbolic ref, copying it is not supported"), |
| 1799 | arg->oldname); |
| 1800 | goto done; |
| 1801 | } |
| 1802 | |
| 1803 | /* |
| 1804 | * There's nothing to do in case the old and new name are the same, so |
| 1805 | * we exit early in that case. |
| 1806 | */ |
| 1807 | if (!strcmp(arg->oldname, arg->newname)) { |
| 1808 | ret = 0; |
| 1809 | goto done; |
| 1810 | } |
| 1811 | |
| 1812 | /* |
| 1813 | * Verify that the new refname is available. |
| 1814 | */ |
| 1815 | if (arg->delete_old) |
| 1816 | string_list_insert(&skip, arg->oldname); |
| 1817 | ret = refs_verify_refname_available(&arg->refs->base, arg->newname, |
| 1818 | NULL, &skip, 0, &errbuf); |
| 1819 | if (ret < 0) { |
| 1820 | error("%s", errbuf.buf); |
| 1821 | goto done; |
| 1822 | } |
| 1823 | |
| 1824 | /* |
| 1825 | * When deleting the old reference we have to use two update indices: |
| 1826 | * once to delete the old ref and its reflog, and once to create the |
| 1827 | * new ref and its reflog. They need to be staged with two separate |
| 1828 | * indices because the new reflog needs to encode both the deletion of |
| 1829 | * the old branch and the creation of the new branch, and we cannot do |
| 1830 | * two changes to a reflog in a single update. |
| 1831 | */ |
| 1832 | deletion_ts = creation_ts = reftable_stack_next_update_index(arg->be->stack); |
| 1833 | if (arg->delete_old) |
| 1834 | creation_ts++; |
| 1835 | ret = reftable_writer_set_limits(writer, deletion_ts, creation_ts); |
| 1836 | if (ret < 0) |
| 1837 | goto done; |
| 1838 | |
| 1839 | /* |
| 1840 | * Add the new reference. If this is a rename then we also delete the |
| 1841 | * old reference. |
| 1842 | */ |
| 1843 | refs[0] = old_ref; |
| 1844 | refs[0].refname = xstrdup(arg->newname); |
| 1845 | refs[0].update_index = creation_ts; |
| 1846 | if (arg->delete_old) { |
| 1847 | refs[1].refname = xstrdup(arg->oldname); |
| 1848 | refs[1].value_type = REFTABLE_REF_DELETION; |
| 1849 | refs[1].update_index = deletion_ts; |
| 1850 | } |
| 1851 | ret = reftable_writer_add_refs(writer, refs, arg->delete_old ? 2 : 1); |
| 1852 | if (ret < 0) |
| 1853 | goto done; |
| 1854 | |
| 1855 | /* |
| 1856 | * When deleting the old branch we need to create a reflog entry on the |
| 1857 | * new branch name that indicates that the old branch has been deleted |
| 1858 | * and then recreated. This is a tad weird, but matches what the files |
| 1859 | * backend does. |
| 1860 | */ |
| 1861 | if (arg->delete_old) { |
| 1862 | struct strbuf head_referent = STRBUF_INIT; |
| 1863 | struct object_id head_oid; |
| 1864 | int append_head_reflog; |
| 1865 | unsigned head_type = 0; |
| 1866 | |
| 1867 | ALLOC_GROW(logs, logs_nr + 1, logs_alloc); |
| 1868 | memset(&logs[logs_nr], 0, sizeof(logs[logs_nr])); |
| 1869 | fill_reftable_log_record(&logs[logs_nr], &committer_ident); |
| 1870 | logs[logs_nr].refname = xstrdup(arg->newname); |
| 1871 | logs[logs_nr].update_index = deletion_ts; |
| 1872 | logs[logs_nr].value.update.message = |
| 1873 | xstrndup(arg->logmsg, reftable_be_write_options(arg->refs)->opts.block_size / 2); |
| 1874 | memcpy(logs[logs_nr].value.update.old_hash, old_ref.value.val1, GIT_MAX_RAWSZ); |
| 1875 | logs_nr++; |
| 1876 | |
| 1877 | ret = reftable_backend_read_ref(arg->be, "HEAD", &head_oid, |
| 1878 | &head_referent, &head_type); |
| 1879 | if (ret < 0) |
| 1880 | goto done; |
| 1881 | append_head_reflog = (head_type & REF_ISSYMREF) && !strcmp(head_referent.buf, arg->oldname); |
| 1882 | strbuf_release(&head_referent); |
| 1883 | |
| 1884 | /* |
| 1885 | * The files backend uses `refs_delete_ref()` to delete the old |
| 1886 | * branch name, which will append a reflog entry for HEAD in |
| 1887 | * case it points to the old branch. |
| 1888 | */ |
| 1889 | if (append_head_reflog) { |
| 1890 | ALLOC_GROW(logs, logs_nr + 1, logs_alloc); |
| 1891 | logs[logs_nr] = logs[logs_nr - 1]; |
| 1892 | logs[logs_nr].refname = xstrdup("HEAD"); |
| 1893 | logs[logs_nr].value.update.name = |
| 1894 | xstrdup(logs[logs_nr].value.update.name); |
| 1895 | logs[logs_nr].value.update.email = |
| 1896 | xstrdup(logs[logs_nr].value.update.email); |
| 1897 | logs[logs_nr].value.update.message = |
| 1898 | xstrdup(logs[logs_nr].value.update.message); |
| 1899 | logs_nr++; |
| 1900 | } |
| 1901 | } |
| 1902 | |
| 1903 | /* |
| 1904 | * Create the reflog entry for the newly created branch. |
| 1905 | */ |
| 1906 | ALLOC_GROW(logs, logs_nr + 1, logs_alloc); |
| 1907 | memset(&logs[logs_nr], 0, sizeof(logs[logs_nr])); |
| 1908 | fill_reftable_log_record(&logs[logs_nr], &committer_ident); |
| 1909 | logs[logs_nr].refname = xstrdup(arg->newname); |
| 1910 | logs[logs_nr].update_index = creation_ts; |
| 1911 | logs[logs_nr].value.update.message = |
| 1912 | xstrndup(arg->logmsg, reftable_be_write_options(arg->refs)->opts.block_size / 2); |
| 1913 | memcpy(logs[logs_nr].value.update.new_hash, old_ref.value.val1, GIT_MAX_RAWSZ); |
| 1914 | logs_nr++; |
| 1915 | |
| 1916 | /* |
| 1917 | * In addition to writing the reflog entry for the new branch, we also |
| 1918 | * copy over all log entries from the old reflog. Last but not least, |
| 1919 | * when renaming we also have to delete all the old reflog entries. |
| 1920 | */ |
| 1921 | ret = reftable_stack_init_log_iterator(arg->be->stack, &it); |
| 1922 | if (ret < 0) |
| 1923 | goto done; |
| 1924 | |
| 1925 | ret = reftable_iterator_seek_log(&it, arg->oldname); |
| 1926 | if (ret < 0) |
| 1927 | goto done; |
| 1928 | |
| 1929 | while (1) { |
| 1930 | ret = reftable_iterator_next_log(&it, &old_log); |
| 1931 | if (ret < 0) |
| 1932 | goto done; |
| 1933 | if (ret > 0 || strcmp(old_log.refname, arg->oldname)) { |
| 1934 | ret = 0; |
| 1935 | break; |
| 1936 | } |
| 1937 | if (reftable_log_record_is_deletion(&old_log)) |
| 1938 | continue; |
| 1939 | |
| 1940 | free(old_log.refname); |
| 1941 | |
| 1942 | /* |
| 1943 | * Copy over the old reflog entry with the new refname. |
| 1944 | */ |
| 1945 | ALLOC_GROW(logs, logs_nr + 1, logs_alloc); |
| 1946 | logs[logs_nr] = old_log; |
| 1947 | logs[logs_nr].refname = xstrdup(arg->newname); |
| 1948 | logs_nr++; |
| 1949 | |
| 1950 | /* |
| 1951 | * Delete the old reflog entry in case we are renaming. |
| 1952 | */ |
| 1953 | if (arg->delete_old) { |
| 1954 | ALLOC_GROW(logs, logs_nr + 1, logs_alloc); |
| 1955 | memset(&logs[logs_nr], 0, sizeof(logs[logs_nr])); |
| 1956 | logs[logs_nr].refname = xstrdup(arg->oldname); |
| 1957 | logs[logs_nr].value_type = REFTABLE_LOG_DELETION; |
| 1958 | logs[logs_nr].update_index = old_log.update_index; |
| 1959 | logs_nr++; |
| 1960 | } |
| 1961 | |
| 1962 | /* |
| 1963 | * Transfer ownership of the log record we're iterating over to |
| 1964 | * the array of log records. Otherwise, the pointers would get |
| 1965 | * free'd or reallocated by the iterator. |
| 1966 | */ |
| 1967 | memset(&old_log, 0, sizeof(old_log)); |
| 1968 | } |
| 1969 | |
| 1970 | ret = reftable_writer_add_logs(writer, logs, logs_nr); |
| 1971 | if (ret < 0) |
| 1972 | goto done; |
| 1973 | |
| 1974 | done: |
| 1975 | assert(ret != REFTABLE_API_ERROR); |
| 1976 | reftable_iterator_destroy(&it); |
| 1977 | string_list_clear(&skip, 0); |
| 1978 | strbuf_release(&errbuf); |
| 1979 | for (i = 0; i < logs_nr; i++) |
| 1980 | reftable_log_record_release(&logs[i]); |
| 1981 | free(logs); |
| 1982 | for (i = 0; i < ARRAY_SIZE(refs); i++) |
| 1983 | reftable_ref_record_release(&refs[i]); |
| 1984 | reftable_ref_record_release(&old_ref); |
| 1985 | reftable_log_record_release(&old_log); |
| 1986 | return ret; |
| 1987 | } |
| 1988 | |
| 1989 | static int reftable_be_rename_ref(struct ref_store *ref_store, |
| 1990 | const char *oldrefname, |
| 1991 | const char *newrefname, |
| 1992 | const char *logmsg) |
| 1993 | { |
| 1994 | struct reftable_ref_store *refs = |
| 1995 | reftable_be_downcast(ref_store, REF_STORE_WRITE, "rename_ref"); |
| 1996 | struct write_copy_arg arg = { |
| 1997 | .refs = refs, |
| 1998 | .oldname = oldrefname, |
| 1999 | .newname = newrefname, |
| 2000 | .logmsg = logmsg, |
| 2001 | .delete_old = 1, |
| 2002 | }; |
| 2003 | int ret; |
| 2004 | |
| 2005 | ret = refs->err; |
| 2006 | if (ret < 0) |
| 2007 | goto done; |
| 2008 | |
| 2009 | ret = backend_for(&arg.be, refs, newrefname, &newrefname, 1); |
| 2010 | if (ret) |
| 2011 | goto done; |
| 2012 | ret = reftable_stack_add(arg.be->stack, &write_copy_table, &arg, |
| 2013 | &reftable_be_write_options(refs)->opts, |
| 2014 | REFTABLE_STACK_NEW_ADDITION_RELOAD); |
| 2015 | |
| 2016 | done: |
| 2017 | assert(ret != REFTABLE_API_ERROR); |
| 2018 | return ret; |
| 2019 | } |
| 2020 | |
| 2021 | static int reftable_be_copy_ref(struct ref_store *ref_store, |
| 2022 | const char *oldrefname, |
| 2023 | const char *newrefname, |
| 2024 | const char *logmsg) |
| 2025 | { |
| 2026 | struct reftable_ref_store *refs = |
| 2027 | reftable_be_downcast(ref_store, REF_STORE_WRITE, "copy_ref"); |
| 2028 | struct write_copy_arg arg = { |
| 2029 | .refs = refs, |
| 2030 | .oldname = oldrefname, |
| 2031 | .newname = newrefname, |
| 2032 | .logmsg = logmsg, |
| 2033 | }; |
| 2034 | int ret; |
| 2035 | |
| 2036 | ret = refs->err; |
| 2037 | if (ret < 0) |
| 2038 | goto done; |
| 2039 | |
| 2040 | ret = backend_for(&arg.be, refs, newrefname, &newrefname, 1); |
| 2041 | if (ret) |
| 2042 | goto done; |
| 2043 | ret = reftable_stack_add(arg.be->stack, &write_copy_table, &arg, |
| 2044 | &reftable_be_write_options(refs)->opts, |
| 2045 | REFTABLE_STACK_NEW_ADDITION_RELOAD); |
| 2046 | |
| 2047 | done: |
| 2048 | assert(ret != REFTABLE_API_ERROR); |
| 2049 | return ret; |
| 2050 | } |
| 2051 | |
| 2052 | struct reftable_reflog_iterator { |
| 2053 | struct ref_iterator base; |
| 2054 | struct reftable_ref_store *refs; |
| 2055 | struct reftable_iterator iter; |
| 2056 | struct reftable_log_record log; |
| 2057 | struct strbuf last_name; |
| 2058 | int err; |
| 2059 | }; |
| 2060 | |
| 2061 | static int reftable_reflog_iterator_advance(struct ref_iterator *ref_iterator) |
| 2062 | { |
| 2063 | struct reftable_reflog_iterator *iter = |
| 2064 | (struct reftable_reflog_iterator *)ref_iterator; |
| 2065 | |
| 2066 | while (!iter->err) { |
| 2067 | iter->err = reftable_iterator_next_log(&iter->iter, &iter->log); |
| 2068 | if (iter->err) |
| 2069 | break; |
| 2070 | |
| 2071 | if (reftable_log_record_is_deletion(&iter->log)) |
| 2072 | continue; |
| 2073 | |
| 2074 | /* |
| 2075 | * We want the refnames that we have reflogs for, so we skip if |
| 2076 | * we've already produced this name. This could be faster by |
| 2077 | * seeking directly to reflog@update_index==0. |
| 2078 | */ |
| 2079 | if (!strcmp(iter->log.refname, iter->last_name.buf)) |
| 2080 | continue; |
| 2081 | |
| 2082 | if (check_refname_format(iter->log.refname, |
| 2083 | REFNAME_ALLOW_ONELEVEL)) |
| 2084 | continue; |
| 2085 | |
| 2086 | strbuf_reset(&iter->last_name); |
| 2087 | strbuf_addstr(&iter->last_name, iter->log.refname); |
| 2088 | iter->base.ref.name = iter->log.refname; |
| 2089 | |
| 2090 | break; |
| 2091 | } |
| 2092 | |
| 2093 | if (iter->err > 0) |
| 2094 | return ITER_DONE; |
| 2095 | if (iter->err < 0) |
| 2096 | return ITER_ERROR; |
| 2097 | return ITER_OK; |
| 2098 | } |
| 2099 | |
| 2100 | static int reftable_reflog_iterator_seek(struct ref_iterator *ref_iterator UNUSED, |
| 2101 | const char *refname UNUSED, |
| 2102 | unsigned int flags UNUSED) |
| 2103 | { |
| 2104 | BUG("reftable reflog iterator cannot be seeked"); |
| 2105 | return -1; |
| 2106 | } |
| 2107 | |
| 2108 | static void reftable_reflog_iterator_release(struct ref_iterator *ref_iterator) |
| 2109 | { |
| 2110 | struct reftable_reflog_iterator *iter = |
| 2111 | (struct reftable_reflog_iterator *)ref_iterator; |
| 2112 | reftable_log_record_release(&iter->log); |
| 2113 | reftable_iterator_destroy(&iter->iter); |
| 2114 | strbuf_release(&iter->last_name); |
| 2115 | } |
| 2116 | |
| 2117 | static struct ref_iterator_vtable reftable_reflog_iterator_vtable = { |
| 2118 | .advance = reftable_reflog_iterator_advance, |
| 2119 | .seek = reftable_reflog_iterator_seek, |
| 2120 | .release = reftable_reflog_iterator_release, |
| 2121 | }; |
| 2122 | |
| 2123 | static struct reftable_reflog_iterator *reflog_iterator_for_stack(struct reftable_ref_store *refs, |
| 2124 | struct reftable_stack *stack) |
| 2125 | { |
| 2126 | struct reftable_reflog_iterator *iter; |
| 2127 | int ret; |
| 2128 | |
| 2129 | iter = xcalloc(1, sizeof(*iter)); |
| 2130 | base_ref_iterator_init(&iter->base, &reftable_reflog_iterator_vtable); |
| 2131 | strbuf_init(&iter->last_name, 0); |
| 2132 | iter->refs = refs; |
| 2133 | |
| 2134 | ret = refs->err; |
| 2135 | if (ret) |
| 2136 | goto done; |
| 2137 | |
| 2138 | ret = reftable_stack_reload(stack); |
| 2139 | if (ret < 0) |
| 2140 | goto done; |
| 2141 | |
| 2142 | ret = reftable_stack_init_log_iterator(stack, &iter->iter); |
| 2143 | if (ret < 0) |
| 2144 | goto done; |
| 2145 | |
| 2146 | ret = reftable_iterator_seek_log(&iter->iter, ""); |
| 2147 | if (ret < 0) |
| 2148 | goto done; |
| 2149 | |
| 2150 | done: |
| 2151 | iter->err = ret; |
| 2152 | return iter; |
| 2153 | } |
| 2154 | |
| 2155 | static struct ref_iterator *reftable_be_reflog_iterator_begin(struct ref_store *ref_store) |
| 2156 | { |
| 2157 | struct reftable_ref_store *refs = |
| 2158 | reftable_be_downcast(ref_store, REF_STORE_READ, "reflog_iterator_begin"); |
| 2159 | struct reftable_reflog_iterator *main_iter, *worktree_iter; |
| 2160 | |
| 2161 | main_iter = reflog_iterator_for_stack(refs, refs->main_backend.stack); |
| 2162 | if (!refs->worktree_backend.stack) |
| 2163 | return &main_iter->base; |
| 2164 | |
| 2165 | worktree_iter = reflog_iterator_for_stack(refs, refs->worktree_backend.stack); |
| 2166 | |
| 2167 | return merge_ref_iterator_begin(&worktree_iter->base, &main_iter->base, |
| 2168 | ref_iterator_select, NULL); |
| 2169 | } |
| 2170 | |
| 2171 | static int yield_log_record(struct reftable_ref_store *refs, |
| 2172 | struct reftable_log_record *log, |
| 2173 | each_reflog_ent_fn fn, |
| 2174 | void *cb_data) |
| 2175 | { |
| 2176 | struct object_id old_oid, new_oid; |
| 2177 | const char *full_committer; |
| 2178 | |
| 2179 | oidread(&old_oid, log->value.update.old_hash, refs->base.repo->hash_algo); |
| 2180 | oidread(&new_oid, log->value.update.new_hash, refs->base.repo->hash_algo); |
| 2181 | |
| 2182 | /* |
| 2183 | * When both the old object ID and the new object ID are null |
| 2184 | * then this is the reflog existence marker. The caller must |
| 2185 | * not be aware of it. |
| 2186 | */ |
| 2187 | if (is_null_oid(&old_oid) && is_null_oid(&new_oid)) |
| 2188 | return 0; |
| 2189 | |
| 2190 | full_committer = fmt_ident(log->value.update.name, log->value.update.email, |
| 2191 | WANT_COMMITTER_IDENT, NULL, IDENT_NO_DATE); |
| 2192 | return fn(log->refname, &old_oid, &new_oid, full_committer, |
| 2193 | log->value.update.time, log->value.update.tz_offset, |
| 2194 | log->value.update.message, cb_data); |
| 2195 | } |
| 2196 | |
| 2197 | static int reftable_be_for_each_reflog_ent_reverse(struct ref_store *ref_store, |
| 2198 | const char *refname, |
| 2199 | each_reflog_ent_fn fn, |
| 2200 | void *cb_data) |
| 2201 | { |
| 2202 | struct reftable_ref_store *refs = |
| 2203 | reftable_be_downcast(ref_store, REF_STORE_READ, "for_each_reflog_ent_reverse"); |
| 2204 | struct reftable_log_record log = {0}; |
| 2205 | struct reftable_iterator it = {0}; |
| 2206 | struct reftable_backend *be; |
| 2207 | int ret; |
| 2208 | |
| 2209 | if (refs->err < 0) |
| 2210 | return refs->err; |
| 2211 | |
| 2212 | /* |
| 2213 | * TODO: we should adapt this callsite to reload the stack. There is no |
| 2214 | * obvious reason why we shouldn't. |
| 2215 | */ |
| 2216 | ret = backend_for(&be, refs, refname, &refname, 0); |
| 2217 | if (ret) |
| 2218 | goto done; |
| 2219 | |
| 2220 | ret = reftable_stack_init_log_iterator(be->stack, &it); |
| 2221 | if (ret < 0) |
| 2222 | goto done; |
| 2223 | |
| 2224 | ret = reftable_iterator_seek_log(&it, refname); |
| 2225 | while (!ret) { |
| 2226 | ret = reftable_iterator_next_log(&it, &log); |
| 2227 | if (ret < 0) |
| 2228 | break; |
| 2229 | if (ret > 0 || strcmp(log.refname, refname)) { |
| 2230 | ret = 0; |
| 2231 | break; |
| 2232 | } |
| 2233 | if (reftable_log_record_is_deletion(&log)) |
| 2234 | continue; |
| 2235 | |
| 2236 | ret = yield_log_record(refs, &log, fn, cb_data); |
| 2237 | if (ret) |
| 2238 | break; |
| 2239 | } |
| 2240 | |
| 2241 | done: |
| 2242 | reftable_log_record_release(&log); |
| 2243 | reftable_iterator_destroy(&it); |
| 2244 | return ret; |
| 2245 | } |
| 2246 | |
| 2247 | static int reftable_be_for_each_reflog_ent(struct ref_store *ref_store, |
| 2248 | const char *refname, |
| 2249 | each_reflog_ent_fn fn, |
| 2250 | void *cb_data) |
| 2251 | { |
| 2252 | struct reftable_ref_store *refs = |
| 2253 | reftable_be_downcast(ref_store, REF_STORE_READ, "for_each_reflog_ent"); |
| 2254 | struct reftable_log_record *logs = NULL; |
| 2255 | struct reftable_iterator it = {0}; |
| 2256 | struct reftable_backend *be; |
| 2257 | size_t logs_alloc = 0, logs_nr = 0, i; |
| 2258 | int ret; |
| 2259 | |
| 2260 | if (refs->err < 0) |
| 2261 | return refs->err; |
| 2262 | |
| 2263 | /* |
| 2264 | * TODO: we should adapt this callsite to reload the stack. There is no |
| 2265 | * obvious reason why we shouldn't. |
| 2266 | */ |
| 2267 | ret = backend_for(&be, refs, refname, &refname, 0); |
| 2268 | if (ret) |
| 2269 | goto done; |
| 2270 | |
| 2271 | ret = reftable_stack_init_log_iterator(be->stack, &it); |
| 2272 | if (ret < 0) |
| 2273 | goto done; |
| 2274 | |
| 2275 | ret = reftable_iterator_seek_log(&it, refname); |
| 2276 | while (!ret) { |
| 2277 | struct reftable_log_record log = {0}; |
| 2278 | |
| 2279 | ret = reftable_iterator_next_log(&it, &log); |
| 2280 | if (ret < 0) |
| 2281 | goto done; |
| 2282 | if (ret > 0 || strcmp(log.refname, refname)) { |
| 2283 | reftable_log_record_release(&log); |
| 2284 | ret = 0; |
| 2285 | break; |
| 2286 | } |
| 2287 | if (reftable_log_record_is_deletion(&log)) { |
| 2288 | reftable_log_record_release(&log); |
| 2289 | continue; |
| 2290 | } |
| 2291 | |
| 2292 | ALLOC_GROW(logs, logs_nr + 1, logs_alloc); |
| 2293 | logs[logs_nr++] = log; |
| 2294 | } |
| 2295 | |
| 2296 | for (i = logs_nr; i--;) { |
| 2297 | ret = yield_log_record(refs, &logs[i], fn, cb_data); |
| 2298 | if (ret) |
| 2299 | goto done; |
| 2300 | } |
| 2301 | |
| 2302 | done: |
| 2303 | reftable_iterator_destroy(&it); |
| 2304 | for (i = 0; i < logs_nr; i++) |
| 2305 | reftable_log_record_release(&logs[i]); |
| 2306 | free(logs); |
| 2307 | return ret; |
| 2308 | } |
| 2309 | |
| 2310 | static int reftable_be_reflog_exists(struct ref_store *ref_store, |
| 2311 | const char *refname) |
| 2312 | { |
| 2313 | struct reftable_ref_store *refs = |
| 2314 | reftable_be_downcast(ref_store, REF_STORE_READ, "reflog_exists"); |
| 2315 | struct reftable_log_record log = {0}; |
| 2316 | struct reftable_iterator it = {0}; |
| 2317 | struct reftable_backend *be; |
| 2318 | int ret; |
| 2319 | |
| 2320 | ret = refs->err; |
| 2321 | if (ret < 0) |
| 2322 | goto done; |
| 2323 | |
| 2324 | ret = backend_for(&be, refs, refname, &refname, 1); |
| 2325 | if (ret < 0) |
| 2326 | goto done; |
| 2327 | |
| 2328 | ret = reftable_stack_init_log_iterator(be->stack, &it); |
| 2329 | if (ret < 0) |
| 2330 | goto done; |
| 2331 | |
| 2332 | ret = reftable_iterator_seek_log(&it, refname); |
| 2333 | if (ret < 0) |
| 2334 | goto done; |
| 2335 | |
| 2336 | /* |
| 2337 | * Check whether we get at least one non-deleted log record for the |
| 2338 | * given ref name. If so, the reflog exists, otherwise it doesn't. |
| 2339 | */ |
| 2340 | while (1) { |
| 2341 | ret = reftable_iterator_next_log(&it, &log); |
| 2342 | if (ret < 0) |
| 2343 | goto done; |
| 2344 | if (ret > 0) { |
| 2345 | ret = 0; |
| 2346 | goto done; |
| 2347 | } |
| 2348 | if (strcmp(log.refname, refname)) { |
| 2349 | ret = 0; |
| 2350 | goto done; |
| 2351 | } |
| 2352 | if (!reftable_log_record_is_deletion(&log)) |
| 2353 | break; |
| 2354 | } |
| 2355 | |
| 2356 | ret = 1; |
| 2357 | |
| 2358 | done: |
| 2359 | reftable_iterator_destroy(&it); |
| 2360 | reftable_log_record_release(&log); |
| 2361 | if (ret < 0) |
| 2362 | ret = 0; |
| 2363 | return ret; |
| 2364 | } |
| 2365 | |
| 2366 | struct write_reflog_existence_arg { |
| 2367 | struct reftable_ref_store *refs; |
| 2368 | const char *refname; |
| 2369 | struct reftable_stack *stack; |
| 2370 | }; |
| 2371 | |
| 2372 | static int write_reflog_existence_table(struct reftable_writer *writer, |
| 2373 | void *cb_data) |
| 2374 | { |
| 2375 | struct write_reflog_existence_arg *arg = cb_data; |
| 2376 | uint64_t ts = reftable_stack_next_update_index(arg->stack); |
| 2377 | struct reftable_log_record log = {0}; |
| 2378 | int ret; |
| 2379 | |
| 2380 | ret = reftable_stack_read_log(arg->stack, arg->refname, &log); |
| 2381 | if (ret <= 0) |
| 2382 | goto done; |
| 2383 | |
| 2384 | ret = reftable_writer_set_limits(writer, ts, ts); |
| 2385 | if (ret < 0) |
| 2386 | goto done; |
| 2387 | |
| 2388 | /* |
| 2389 | * The existence entry has both old and new object ID set to the |
| 2390 | * null object ID. Our iterators are aware of this and will not present |
| 2391 | * them to their callers. |
| 2392 | */ |
| 2393 | log.refname = xstrdup(arg->refname); |
| 2394 | log.update_index = ts; |
| 2395 | log.value_type = REFTABLE_LOG_UPDATE; |
| 2396 | ret = reftable_writer_add_log(writer, &log); |
| 2397 | |
| 2398 | done: |
| 2399 | assert(ret != REFTABLE_API_ERROR); |
| 2400 | reftable_log_record_release(&log); |
| 2401 | return ret; |
| 2402 | } |
| 2403 | |
| 2404 | static int reftable_be_create_reflog(struct ref_store *ref_store, |
| 2405 | const char *refname, |
| 2406 | struct strbuf *errmsg UNUSED) |
| 2407 | { |
| 2408 | struct reftable_ref_store *refs = |
| 2409 | reftable_be_downcast(ref_store, REF_STORE_WRITE, "create_reflog"); |
| 2410 | struct reftable_backend *be; |
| 2411 | struct write_reflog_existence_arg arg = { |
| 2412 | .refs = refs, |
| 2413 | .refname = refname, |
| 2414 | }; |
| 2415 | int ret; |
| 2416 | |
| 2417 | ret = refs->err; |
| 2418 | if (ret < 0) |
| 2419 | goto done; |
| 2420 | |
| 2421 | ret = backend_for(&be, refs, refname, &refname, 1); |
| 2422 | if (ret) |
| 2423 | goto done; |
| 2424 | arg.stack = be->stack; |
| 2425 | |
| 2426 | ret = reftable_stack_add(be->stack, &write_reflog_existence_table, &arg, |
| 2427 | &reftable_be_write_options(refs)->opts, |
| 2428 | REFTABLE_STACK_NEW_ADDITION_RELOAD); |
| 2429 | |
| 2430 | done: |
| 2431 | return ret; |
| 2432 | } |
| 2433 | |
| 2434 | struct write_reflog_delete_arg { |
| 2435 | struct reftable_stack *stack; |
| 2436 | const char *refname; |
| 2437 | }; |
| 2438 | |
| 2439 | static int write_reflog_delete_table(struct reftable_writer *writer, void *cb_data) |
| 2440 | { |
| 2441 | struct write_reflog_delete_arg *arg = cb_data; |
| 2442 | struct reftable_log_record log = {0}, tombstone = {0}; |
| 2443 | struct reftable_iterator it = {0}; |
| 2444 | uint64_t ts = reftable_stack_next_update_index(arg->stack); |
| 2445 | int ret; |
| 2446 | |
| 2447 | ret = reftable_writer_set_limits(writer, ts, ts); |
| 2448 | if (ret < 0) |
| 2449 | goto out; |
| 2450 | |
| 2451 | ret = reftable_stack_init_log_iterator(arg->stack, &it); |
| 2452 | if (ret < 0) |
| 2453 | goto out; |
| 2454 | |
| 2455 | /* |
| 2456 | * In order to delete a table we need to delete all reflog entries one |
| 2457 | * by one. This is inefficient, but the reftable format does not have a |
| 2458 | * better marker right now. |
| 2459 | */ |
| 2460 | ret = reftable_iterator_seek_log(&it, arg->refname); |
| 2461 | while (ret == 0) { |
| 2462 | ret = reftable_iterator_next_log(&it, &log); |
| 2463 | if (ret < 0) |
| 2464 | break; |
| 2465 | if (ret > 0 || strcmp(log.refname, arg->refname)) { |
| 2466 | ret = 0; |
| 2467 | break; |
| 2468 | } |
| 2469 | if (reftable_log_record_is_deletion(&log)) |
| 2470 | continue; |
| 2471 | |
| 2472 | tombstone.refname = (char *)arg->refname; |
| 2473 | tombstone.value_type = REFTABLE_LOG_DELETION; |
| 2474 | tombstone.update_index = log.update_index; |
| 2475 | |
| 2476 | ret = reftable_writer_add_log(writer, &tombstone); |
| 2477 | } |
| 2478 | |
| 2479 | out: |
| 2480 | reftable_log_record_release(&log); |
| 2481 | reftable_iterator_destroy(&it); |
| 2482 | return ret; |
| 2483 | } |
| 2484 | |
| 2485 | static int reftable_be_delete_reflog(struct ref_store *ref_store, |
| 2486 | const char *refname) |
| 2487 | { |
| 2488 | struct reftable_ref_store *refs = |
| 2489 | reftable_be_downcast(ref_store, REF_STORE_WRITE, "delete_reflog"); |
| 2490 | struct reftable_backend *be; |
| 2491 | struct write_reflog_delete_arg arg = { |
| 2492 | .refname = refname, |
| 2493 | }; |
| 2494 | int ret; |
| 2495 | |
| 2496 | ret = backend_for(&be, refs, refname, &refname, 1); |
| 2497 | if (ret) |
| 2498 | return ret; |
| 2499 | arg.stack = be->stack; |
| 2500 | |
| 2501 | ret = reftable_stack_add(be->stack, &write_reflog_delete_table, &arg, |
| 2502 | &reftable_be_write_options(refs)->opts, |
| 2503 | REFTABLE_STACK_NEW_ADDITION_RELOAD); |
| 2504 | |
| 2505 | assert(ret != REFTABLE_API_ERROR); |
| 2506 | return ret; |
| 2507 | } |
| 2508 | |
| 2509 | struct reflog_expiry_arg { |
| 2510 | struct reftable_ref_store *refs; |
| 2511 | struct reftable_stack *stack; |
| 2512 | struct reftable_log_record *records; |
| 2513 | struct object_id update_oid; |
| 2514 | const char *refname; |
| 2515 | size_t len; |
| 2516 | }; |
| 2517 | |
| 2518 | static int write_reflog_expiry_table(struct reftable_writer *writer, void *cb_data) |
| 2519 | { |
| 2520 | struct reflog_expiry_arg *arg = cb_data; |
| 2521 | uint64_t ts = reftable_stack_next_update_index(arg->stack); |
| 2522 | uint64_t live_records = 0; |
| 2523 | size_t i; |
| 2524 | int ret; |
| 2525 | |
| 2526 | for (i = 0; i < arg->len; i++) |
| 2527 | if (arg->records[i].value_type == REFTABLE_LOG_UPDATE) |
| 2528 | live_records++; |
| 2529 | |
| 2530 | ret = reftable_writer_set_limits(writer, ts, ts); |
| 2531 | if (ret < 0) |
| 2532 | return ret; |
| 2533 | |
| 2534 | if (!is_null_oid(&arg->update_oid)) { |
| 2535 | struct reftable_ref_record ref = {0}; |
| 2536 | struct object_id peeled; |
| 2537 | |
| 2538 | ref.refname = (char *)arg->refname; |
| 2539 | ref.update_index = ts; |
| 2540 | |
| 2541 | if (!peel_object(arg->refs->base.repo, &arg->update_oid, &peeled, 0)) { |
| 2542 | ref.value_type = REFTABLE_REF_VAL2; |
| 2543 | memcpy(ref.value.val2.target_value, peeled.hash, GIT_MAX_RAWSZ); |
| 2544 | memcpy(ref.value.val2.value, arg->update_oid.hash, GIT_MAX_RAWSZ); |
| 2545 | } else { |
| 2546 | ref.value_type = REFTABLE_REF_VAL1; |
| 2547 | memcpy(ref.value.val1, arg->update_oid.hash, GIT_MAX_RAWSZ); |
| 2548 | } |
| 2549 | |
| 2550 | ret = reftable_writer_add_ref(writer, &ref); |
| 2551 | if (ret < 0) |
| 2552 | return ret; |
| 2553 | } |
| 2554 | |
| 2555 | /* |
| 2556 | * When there are no more entries left in the reflog we empty it |
| 2557 | * completely, but write a placeholder reflog entry that indicates that |
| 2558 | * the reflog still exists. |
| 2559 | */ |
| 2560 | if (!live_records) { |
| 2561 | struct reftable_log_record log = { |
| 2562 | .refname = (char *)arg->refname, |
| 2563 | .value_type = REFTABLE_LOG_UPDATE, |
| 2564 | .update_index = ts, |
| 2565 | }; |
| 2566 | |
| 2567 | ret = reftable_writer_add_log(writer, &log); |
| 2568 | if (ret) |
| 2569 | return ret; |
| 2570 | } |
| 2571 | |
| 2572 | for (i = 0; i < arg->len; i++) { |
| 2573 | ret = reftable_writer_add_log(writer, &arg->records[i]); |
| 2574 | if (ret) |
| 2575 | return ret; |
| 2576 | } |
| 2577 | |
| 2578 | return 0; |
| 2579 | } |
| 2580 | |
| 2581 | static int reftable_be_reflog_expire(struct ref_store *ref_store, |
| 2582 | const char *refname, |
| 2583 | unsigned int flags, |
| 2584 | reflog_expiry_prepare_fn prepare_fn, |
| 2585 | reflog_expiry_should_prune_fn should_prune_fn, |
| 2586 | reflog_expiry_cleanup_fn cleanup_fn, |
| 2587 | void *policy_cb_data) |
| 2588 | { |
| 2589 | /* |
| 2590 | * For log expiry, we write tombstones for every single reflog entry |
| 2591 | * that is to be expired. This means that the entries are still |
| 2592 | * retrievable by delving into the stack, and expiring entries |
| 2593 | * paradoxically takes extra memory. This memory is only reclaimed when |
| 2594 | * compacting the reftable stack. |
| 2595 | * |
| 2596 | * It would be better if the refs backend supported an API that sets a |
| 2597 | * criterion for all refs, passing the criterion to pack_refs(). |
| 2598 | * |
| 2599 | * On the plus side, because we do the expiration per ref, we can easily |
| 2600 | * insert the reflog existence dummies. |
| 2601 | */ |
| 2602 | struct reftable_ref_store *refs = |
| 2603 | reftable_be_downcast(ref_store, REF_STORE_WRITE, "reflog_expire"); |
| 2604 | struct reftable_log_record *logs = NULL; |
| 2605 | struct reftable_log_record *rewritten = NULL; |
| 2606 | struct reftable_iterator it = {0}; |
| 2607 | struct reftable_addition *add = NULL; |
| 2608 | struct reflog_expiry_arg arg = {0}; |
| 2609 | struct reftable_backend *be; |
| 2610 | struct object_id oid = {0}; |
| 2611 | struct strbuf referent = STRBUF_INIT; |
| 2612 | uint8_t *last_hash = NULL; |
| 2613 | size_t logs_nr = 0, logs_alloc = 0, i; |
| 2614 | unsigned int type = 0; |
| 2615 | int ret; |
| 2616 | |
| 2617 | if (refs->err < 0) |
| 2618 | return refs->err; |
| 2619 | |
| 2620 | ret = backend_for(&be, refs, refname, &refname, 1); |
| 2621 | if (ret < 0) |
| 2622 | goto done; |
| 2623 | |
| 2624 | ret = reftable_stack_new_addition(&add, be->stack, |
| 2625 | &reftable_be_write_options(refs)->opts, |
| 2626 | REFTABLE_STACK_NEW_ADDITION_RELOAD); |
| 2627 | if (ret < 0) |
| 2628 | goto done; |
| 2629 | |
| 2630 | ret = reftable_stack_init_log_iterator(be->stack, &it); |
| 2631 | if (ret < 0) |
| 2632 | goto done; |
| 2633 | |
| 2634 | ret = reftable_iterator_seek_log(&it, refname); |
| 2635 | if (ret < 0) |
| 2636 | goto done; |
| 2637 | |
| 2638 | ret = reftable_backend_read_ref(be, refname, &oid, &referent, &type); |
| 2639 | if (ret < 0) |
| 2640 | goto done; |
| 2641 | prepare_fn(refname, &oid, policy_cb_data); |
| 2642 | |
| 2643 | while (1) { |
| 2644 | struct reftable_log_record log = {0}; |
| 2645 | struct object_id old_oid, new_oid; |
| 2646 | |
| 2647 | ret = reftable_iterator_next_log(&it, &log); |
| 2648 | if (ret < 0) |
| 2649 | goto done; |
| 2650 | if (ret > 0 || strcmp(log.refname, refname)) { |
| 2651 | reftable_log_record_release(&log); |
| 2652 | break; |
| 2653 | } |
| 2654 | if (reftable_log_record_is_deletion(&log)) { |
| 2655 | reftable_log_record_release(&log); |
| 2656 | continue; |
| 2657 | } |
| 2658 | |
| 2659 | oidread(&old_oid, log.value.update.old_hash, |
| 2660 | ref_store->repo->hash_algo); |
| 2661 | oidread(&new_oid, log.value.update.new_hash, |
| 2662 | ref_store->repo->hash_algo); |
| 2663 | |
| 2664 | /* |
| 2665 | * Skip over the reflog existence marker. We will add it back |
| 2666 | * in when there are no live reflog records. |
| 2667 | */ |
| 2668 | if (is_null_oid(&old_oid) && is_null_oid(&new_oid)) { |
| 2669 | reftable_log_record_release(&log); |
| 2670 | continue; |
| 2671 | } |
| 2672 | |
| 2673 | ALLOC_GROW(logs, logs_nr + 1, logs_alloc); |
| 2674 | logs[logs_nr++] = log; |
| 2675 | } |
| 2676 | |
| 2677 | /* |
| 2678 | * We need to rewrite all reflog entries according to the pruning |
| 2679 | * callback function: |
| 2680 | * |
| 2681 | * - If a reflog entry shall be pruned we mark the record for |
| 2682 | * deletion. |
| 2683 | * |
| 2684 | * - Otherwise we may have to rewrite the chain of reflog entries so |
| 2685 | * that gaps created by just-deleted records get backfilled. |
| 2686 | */ |
| 2687 | CALLOC_ARRAY(rewritten, logs_nr); |
| 2688 | for (i = logs_nr; i--;) { |
| 2689 | struct reftable_log_record *dest = &rewritten[i]; |
| 2690 | struct object_id old_oid, new_oid; |
| 2691 | |
| 2692 | *dest = logs[i]; |
| 2693 | oidread(&old_oid, logs[i].value.update.old_hash, |
| 2694 | ref_store->repo->hash_algo); |
| 2695 | oidread(&new_oid, logs[i].value.update.new_hash, |
| 2696 | ref_store->repo->hash_algo); |
| 2697 | |
| 2698 | if (should_prune_fn(&old_oid, &new_oid, logs[i].value.update.email, |
| 2699 | (timestamp_t)logs[i].value.update.time, |
| 2700 | logs[i].value.update.tz_offset, |
| 2701 | logs[i].value.update.message, |
| 2702 | policy_cb_data)) { |
| 2703 | dest->value_type = REFTABLE_LOG_DELETION; |
| 2704 | } else { |
| 2705 | if ((flags & EXPIRE_REFLOGS_REWRITE) && last_hash) |
| 2706 | memcpy(dest->value.update.old_hash, last_hash, GIT_MAX_RAWSZ); |
| 2707 | last_hash = logs[i].value.update.new_hash; |
| 2708 | } |
| 2709 | } |
| 2710 | |
| 2711 | if (flags & EXPIRE_REFLOGS_UPDATE_REF && last_hash && !is_null_oid(&oid)) |
| 2712 | oidread(&arg.update_oid, last_hash, ref_store->repo->hash_algo); |
| 2713 | |
| 2714 | arg.refs = refs; |
| 2715 | arg.records = rewritten; |
| 2716 | arg.len = logs_nr; |
| 2717 | arg.stack = be->stack; |
| 2718 | arg.refname = refname; |
| 2719 | |
| 2720 | ret = reftable_addition_add(add, &write_reflog_expiry_table, &arg); |
| 2721 | if (ret < 0) |
| 2722 | goto done; |
| 2723 | |
| 2724 | /* |
| 2725 | * Future improvement: we could skip writing records that were |
| 2726 | * not changed. |
| 2727 | */ |
| 2728 | if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) |
| 2729 | ret = reftable_addition_commit(add); |
| 2730 | |
| 2731 | done: |
| 2732 | if (add) |
| 2733 | cleanup_fn(policy_cb_data); |
| 2734 | assert(ret != REFTABLE_API_ERROR); |
| 2735 | |
| 2736 | reftable_iterator_destroy(&it); |
| 2737 | reftable_addition_destroy(add); |
| 2738 | for (i = 0; i < logs_nr; i++) |
| 2739 | reftable_log_record_release(&logs[i]); |
| 2740 | strbuf_release(&referent); |
| 2741 | free(logs); |
| 2742 | free(rewritten); |
| 2743 | return ret; |
| 2744 | } |
| 2745 | |
| 2746 | static void reftable_fsck_verbose_handler(const char *msg, void *cb_data) |
| 2747 | { |
| 2748 | struct fsck_options *o = cb_data; |
| 2749 | |
| 2750 | if (o->verbose) |
| 2751 | fprintf_ln(stderr, "%s", msg); |
| 2752 | } |
| 2753 | |
| 2754 | static const enum fsck_msg_id fsck_msg_id_map[] = { |
| 2755 | [REFTABLE_FSCK_ERROR_TABLE_NAME] = FSCK_MSG_BAD_REFTABLE_TABLE_NAME, |
| 2756 | }; |
| 2757 | |
| 2758 | static int reftable_fsck_error_handler(struct reftable_fsck_info *info, |
| 2759 | void *cb_data) |
| 2760 | { |
| 2761 | struct fsck_ref_report report = { .path = info->path }; |
| 2762 | struct fsck_options *o = cb_data; |
| 2763 | enum fsck_msg_id msg_id; |
| 2764 | |
| 2765 | if (info->error < 0 || info->error >= REFTABLE_FSCK_MAX_VALUE) |
| 2766 | BUG("unknown fsck error: %d", (int)info->error); |
| 2767 | |
| 2768 | msg_id = fsck_msg_id_map[info->error]; |
| 2769 | |
| 2770 | if (!msg_id) |
| 2771 | BUG("fsck_msg_id value missing for reftable error: %d", (int)info->error); |
| 2772 | |
| 2773 | return fsck_report_ref(o, &report, msg_id, "%s", info->msg); |
| 2774 | } |
| 2775 | |
| 2776 | static int reftable_be_fsck(struct ref_store *ref_store, struct fsck_options *o, |
| 2777 | struct worktree *wt) |
| 2778 | { |
| 2779 | struct reftable_ref_store *refs = |
| 2780 | reftable_be_downcast(ref_store, REF_STORE_READ, "fsck"); |
| 2781 | struct reftable_ref_iterator *iter = NULL; |
| 2782 | struct reftable_ref_record ref = { 0 }; |
| 2783 | struct fsck_ref_report report = { 0 }; |
| 2784 | struct strbuf refname = STRBUF_INIT; |
| 2785 | struct reftable_backend *backend; |
| 2786 | int ret, errors = 0; |
| 2787 | |
| 2788 | if (is_main_worktree(wt)) { |
| 2789 | backend = &refs->main_backend; |
| 2790 | } else { |
| 2791 | ret = backend_for_worktree(&backend, refs, wt->id); |
| 2792 | if (ret < 0) { |
| 2793 | ret = error(_("reftable stack for worktree '%s' is broken"), |
| 2794 | wt->id); |
| 2795 | goto out; |
| 2796 | } |
| 2797 | } |
| 2798 | |
| 2799 | errors |= reftable_fsck_check(backend->stack, reftable_fsck_error_handler, |
| 2800 | reftable_fsck_verbose_handler, o); |
| 2801 | |
| 2802 | iter = ref_iterator_for_stack(refs, backend->stack, "", NULL, 0); |
| 2803 | if (!iter) { |
| 2804 | ret = error(_("could not create iterator for worktree '%s'"), wt->id); |
| 2805 | goto out; |
| 2806 | } |
| 2807 | |
| 2808 | while (1) { |
| 2809 | ret = reftable_iterator_next_ref(&iter->iter, &ref); |
| 2810 | if (ret > 0) |
| 2811 | break; |
| 2812 | if (ret < 0) { |
| 2813 | ret = error(_("could not read record for worktree '%s'"), wt->id); |
| 2814 | goto out; |
| 2815 | } |
| 2816 | |
| 2817 | strbuf_reset(&refname); |
| 2818 | if (!is_main_worktree(wt)) |
| 2819 | strbuf_addf(&refname, "worktrees/%s/", wt->id); |
| 2820 | strbuf_addstr(&refname, ref.refname); |
| 2821 | report.path = refname.buf; |
| 2822 | |
| 2823 | switch (ref.value_type) { |
| 2824 | case REFTABLE_REF_DELETION: |
| 2825 | continue; |
| 2826 | case REFTABLE_REF_VAL1: |
| 2827 | case REFTABLE_REF_VAL2: { |
| 2828 | struct object_id oid; |
| 2829 | unsigned hash_id; |
| 2830 | |
| 2831 | switch (reftable_stack_hash_id(backend->stack)) { |
| 2832 | case REFTABLE_HASH_SHA1: |
| 2833 | hash_id = GIT_HASH_SHA1; |
| 2834 | break; |
| 2835 | case REFTABLE_HASH_SHA256: |
| 2836 | hash_id = GIT_HASH_SHA256; |
| 2837 | break; |
| 2838 | default: |
| 2839 | BUG("unhandled hash ID %d", |
| 2840 | reftable_stack_hash_id(backend->stack)); |
| 2841 | } |
| 2842 | |
| 2843 | oidread(&oid, reftable_ref_record_val1(&ref), |
| 2844 | &hash_algos[hash_id]); |
| 2845 | |
| 2846 | errors |= refs_fsck_ref(ref_store, o, &report, ref.refname, &oid); |
| 2847 | break; |
| 2848 | } |
| 2849 | case REFTABLE_REF_SYMREF: |
| 2850 | errors |= refs_fsck_symref(ref_store, o, &report, ref.refname, |
| 2851 | ref.value.symref); |
| 2852 | break; |
| 2853 | default: |
| 2854 | BUG("unhandled reference value type %d", ref.value_type); |
| 2855 | } |
| 2856 | } |
| 2857 | |
| 2858 | ret = errors ? -1 : 0; |
| 2859 | |
| 2860 | out: |
| 2861 | if (iter) |
| 2862 | ref_iterator_free(&iter->base); |
| 2863 | reftable_ref_record_release(&ref); |
| 2864 | strbuf_release(&refname); |
| 2865 | return ret; |
| 2866 | } |
| 2867 | |
| 2868 | struct ref_storage_be refs_be_reftable = { |
| 2869 | .name = "reftable", |
| 2870 | .init = reftable_be_init, |
| 2871 | .release = reftable_be_release, |
| 2872 | .create_on_disk = reftable_be_create_on_disk, |
| 2873 | .remove_on_disk = reftable_be_remove_on_disk, |
| 2874 | |
| 2875 | .transaction_prepare = reftable_be_transaction_prepare, |
| 2876 | .transaction_finish = reftable_be_transaction_finish, |
| 2877 | .transaction_abort = reftable_be_transaction_abort, |
| 2878 | |
| 2879 | .optimize = reftable_be_optimize, |
| 2880 | .optimize_required = reftable_be_optimize_required, |
| 2881 | |
| 2882 | .rename_ref = reftable_be_rename_ref, |
| 2883 | .copy_ref = reftable_be_copy_ref, |
| 2884 | |
| 2885 | .iterator_begin = reftable_be_iterator_begin, |
| 2886 | .read_raw_ref = reftable_be_read_raw_ref, |
| 2887 | .read_symbolic_ref = reftable_be_read_symbolic_ref, |
| 2888 | |
| 2889 | .reflog_iterator_begin = reftable_be_reflog_iterator_begin, |
| 2890 | .for_each_reflog_ent = reftable_be_for_each_reflog_ent, |
| 2891 | .for_each_reflog_ent_reverse = reftable_be_for_each_reflog_ent_reverse, |
| 2892 | .reflog_exists = reftable_be_reflog_exists, |
| 2893 | .create_reflog = reftable_be_create_reflog, |
| 2894 | .delete_reflog = reftable_be_delete_reflog, |
| 2895 | .reflog_expire = reftable_be_reflog_expire, |
| 2896 | |
| 2897 | .fsck = reftable_be_fsck, |
| 2898 | }; |