refs: introduce REF_SKIP_OID_VERIFICATION flag

This lets the ref-store test helper write non-existent or unparsable objects into the ref storage. Use this to make t1006 and t3800 independent of the files storage backend. Signed-off-by: Han-Wen Nienhuys <hanwen@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Han-Wen Nienhuys committed Dec 7, 2021 at 13:38 UTC e9706a188f8598ea4d8afac1f70360abb77d4d8d
5 files changed +43 -27
refs.h
+7 -1
@@ -615,12 +615,18 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err);
615 */
616 #define REF_FORCE_CREATE_REFLOG (1 << 1)
617
618 +/*
619 + * Blindly write an object_id. This is useful for testing data corruption
620 + * scenarios.
621 + */
622 +#define REF_SKIP_OID_VERIFICATION (1 << 10)
623 +
624 /*
625 * Bitmask of all of the flags that are allowed to be passed in to
626 * ref_transaction_update() and friends:
627 */
628 #define REF_TRANSACTION_UPDATE_ALLOWED_FLAGS \
623 - (REF_NO_DEREF | REF_FORCE_CREATE_REFLOG)
629 + (REF_NO_DEREF | REF_FORCE_CREATE_REFLOG | REF_SKIP_OID_VERIFICATION)
630
631 /*
632 * Add a reference update to transaction. `new_oid` is the value that
refs/files-backend.c
+29 -21
@@ -1353,7 +1353,8 @@ static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname)
1353 }
1354
1355 static int write_ref_to_lockfile(struct ref_lock *lock,
1356 - const struct object_id *oid, struct strbuf *err);
1356 + const struct object_id *oid,
1357 + int skip_oid_verification, struct strbuf *err);
1358 static int commit_ref_update(struct files_ref_store *refs,
1359 struct ref_lock *lock,
1360 const struct object_id *oid, const char *logmsg,
@@ -1500,7 +1501,7 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
1501 }
1502 oidcpy(&lock->old_oid, &orig_oid);
1503
1503 - if (write_ref_to_lockfile(lock, &orig_oid, &err) ||
1504 + if (write_ref_to_lockfile(lock, &orig_oid, 0, &err) ||
1505 commit_ref_update(refs, lock, &orig_oid, logmsg, &err)) {
1506 error("unable to write current sha1 into %s: %s", newrefname, err.buf);
1507 strbuf_release(&err);
@@ -1520,7 +1521,7 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
1521
1522 flag = log_all_ref_updates;
1523 log_all_ref_updates = LOG_REFS_NONE;
1523 - if (write_ref_to_lockfile(lock, &orig_oid, &err) ||
1524 + if (write_ref_to_lockfile(lock, &orig_oid, 0, &err) ||
1525 commit_ref_update(refs, lock, &orig_oid, NULL, &err)) {
1526 error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
1527 strbuf_release(&err);
@@ -1756,26 +1757,31 @@ static int files_log_ref_write(struct files_ref_store *refs,
1757 * errors, rollback the lockfile, fill in *err and return -1.
1758 */
1759 static int write_ref_to_lockfile(struct ref_lock *lock,
1759 - const struct object_id *oid, struct strbuf *err)
1760 + const struct object_id *oid,
1761 + int skip_oid_verification, struct strbuf *err)
1762 {
1763 static char term = '\n';
1764 struct object *o;
1765 int fd;
1766
1765 - o = parse_object(the_repository, oid);
1766 - if (!o) {
1767 - strbuf_addf(err,
1768 - "trying to write ref '%s' with nonexistent object %s",
1769 - lock->ref_name, oid_to_hex(oid));
1770 - unlock_ref(lock);
1771 - return -1;
1772 - }
1773 - if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) {
1774 - strbuf_addf(err,
1775 - "trying to write non-commit object %s to branch '%s'",
1776 - oid_to_hex(oid), lock->ref_name);
1777 - unlock_ref(lock);
1778 - return -1;
1767 + if (!skip_oid_verification) {
1768 + o = parse_object(the_repository, oid);
1769 + if (!o) {
1770 + strbuf_addf(
1771 + err,
1772 + "trying to write ref '%s' with nonexistent object %s",
1773 + lock->ref_name, oid_to_hex(oid));
1774 + unlock_ref(lock);
1775 + return -1;
1776 + }
1777 + if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) {
1778 + strbuf_addf(
1779 + err,
1780 + "trying to write non-commit object %s to branch '%s'",
1781 + oid_to_hex(oid), lock->ref_name);
1782 + unlock_ref(lock);
1783 + return -1;
1784 + }
1785 }
1786 fd = get_lock_file_fd(&lock->lk);
1787 if (write_in_full(fd, oid_to_hex(oid), the_hash_algo->hexsz) < 0 ||
@@ -2189,7 +2195,7 @@ static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator)
2195 }
2196
2197 static int files_reflog_iterator_peel(struct ref_iterator *ref_iterator,
2192 - struct object_id *peeled)
2198 + struct object_id *peeled)
2199 {
2200 BUG("ref_iterator_peel() called for reflog_iterator");
2201 }
@@ -2575,8 +2581,10 @@ static int lock_ref_for_update(struct files_ref_store *refs,
2581 * The reference already has the desired
2582 * value, so we don't need to write it.
2583 */
2578 - } else if (write_ref_to_lockfile(lock, &update->new_oid,
2579 - err)) {
2584 + } else if (write_ref_to_lockfile(
2585 + lock, &update->new_oid,
2586 + update->flags & REF_SKIP_OID_VERIFICATION,
2587 + err)) {
2588 char *write_err = strbuf_detach(err, NULL);
2589
2590 /*
t/helper/test-ref-store.c
+1
@@ -130,6 +130,7 @@ static int cmd_create_symref(struct ref_store *refs, const char **argv)
130 static struct flag_definition transaction_flags[] = {
131 FLAG_DEF(REF_NO_DEREF),
132 FLAG_DEF(REF_FORCE_CREATE_REFLOG),
133 + FLAG_DEF(REF_SKIP_OID_VERIFICATION),
134 { NULL, 0 }
135 };
136
t/t1006-cat-file.sh
+2 -3
@@ -452,9 +452,8 @@ test_expect_success 'the --allow-unknown-type option does not consider replaceme
452 # Create it manually, as "git replace" will die on bogus
453 # types.
454 head=$(git rev-parse --verify HEAD) &&
455 - test_when_finished "rm -rf .git/refs/replace" &&
456 - mkdir -p .git/refs/replace &&
457 - echo $head >.git/refs/replace/$bogus_short_sha1 &&
455 + test_when_finished "test-tool ref-store main delete-refs 0 msg refs/replace/$bogus_short_sha1" &&
456 + test-tool ref-store main update-ref msg "refs/replace/$bogus_short_sha1" $head $ZERO_OID REF_SKIP_OID_VERIFICATION &&
457
458 cat >expect <<-EOF &&
459 commit
t/t3800-mktag.sh
+4 -2
@@ -72,7 +72,8 @@ check_verify_failure () {
72
73 # Manually create the broken, we cannot do it with
74 # update-ref
75 - echo "$bad_tag" >"bad-tag/$tag_ref" &&
75 + test-tool -C bad-tag ref-store main delete-refs 0 msg "$tag_ref" &&
76 + test-tool -C bad-tag ref-store main update-ref msg "$tag_ref" $bad_tag $ZERO_OID REF_SKIP_OID_VERIFICATION &&
77
78 # Unlike fsck-ing unreachable content above, this
79 # will always fail.
@@ -83,7 +84,8 @@ check_verify_failure () {
84 # Make sure the earlier test created it for us
85 git rev-parse "$bad_tag" &&
86
86 - echo "$bad_tag" >"bad-tag/$tag_ref" &&
87 + test-tool -C bad-tag ref-store main delete-refs 0 msg "$tag_ref" &&
88 + test-tool -C bad-tag ref-store main update-ref msg "$tag_ref" $bad_tag $ZERO_OID REF_SKIP_OID_VERIFICATION &&
89
90 printf "%s tag\t%s\n" "$bad_tag" "$tag_ref" >expected &&
91 git -C bad-tag for-each-ref "$tag_ref" >actual &&