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