]> git.ipfire.org Git - thirdparty/git.git/commitdiff
refs: introduce REF_SKIP_OID_VERIFICATION flag
authorHan-Wen Nienhuys <hanwen@google.com>
Tue, 7 Dec 2021 13:38:17 +0000 (13:38 +0000)
committerJunio C Hamano <gitster@pobox.com>
Tue, 7 Dec 2021 21:15:19 +0000 (13:15 -0800)
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>
refs.h
refs/files-backend.c
t/helper/test-ref-store.c
t/t1006-cat-file.sh
t/t3800-mktag.sh

diff --git a/refs.h b/refs.h
index 45c34e99e3afaa232392728ae3d59b250a81020d..76efc589ccaefdee26b1652010f261855bf91f8c 100644 (file)
--- a/refs.h
+++ b/refs.h
@@ -615,12 +615,18 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err);
  */
 #define REF_FORCE_CREATE_REFLOG (1 << 1)
 
+/*
+ * Blindly write an object_id. This is useful for testing data corruption
+ * scenarios.
+ */
+#define REF_SKIP_OID_VERIFICATION (1 << 10)
+
 /*
  * Bitmask of all of the flags that are allowed to be passed in to
  * ref_transaction_update() and friends:
  */
 #define REF_TRANSACTION_UPDATE_ALLOWED_FLAGS \
-       (REF_NO_DEREF | REF_FORCE_CREATE_REFLOG)
+       (REF_NO_DEREF | REF_FORCE_CREATE_REFLOG | REF_SKIP_OID_VERIFICATION)
 
 /*
  * Add a reference update to transaction. `new_oid` is the value that
index 37329b98cca8efc70bb97c23a9744394eb1d86be..d0019fcd8b7fd95c3e9075d5b088c1fb6dc5fa29 100644 (file)
@@ -1353,7 +1353,8 @@ static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname)
 }
 
 static int write_ref_to_lockfile(struct ref_lock *lock,
-                                const struct object_id *oid, struct strbuf *err);
+                                const struct object_id *oid,
+                                int skip_oid_verification, struct strbuf *err);
 static int commit_ref_update(struct files_ref_store *refs,
                             struct ref_lock *lock,
                             const struct object_id *oid, const char *logmsg,
@@ -1500,7 +1501,7 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
        }
        oidcpy(&lock->old_oid, &orig_oid);
 
-       if (write_ref_to_lockfile(lock, &orig_oid, &err) ||
+       if (write_ref_to_lockfile(lock, &orig_oid, 0, &err) ||
            commit_ref_update(refs, lock, &orig_oid, logmsg, &err)) {
                error("unable to write current sha1 into %s: %s", newrefname, err.buf);
                strbuf_release(&err);
@@ -1520,7 +1521,7 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
 
        flag = log_all_ref_updates;
        log_all_ref_updates = LOG_REFS_NONE;
-       if (write_ref_to_lockfile(lock, &orig_oid, &err) ||
+       if (write_ref_to_lockfile(lock, &orig_oid, 0, &err) ||
            commit_ref_update(refs, lock, &orig_oid, NULL, &err)) {
                error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
                strbuf_release(&err);
@@ -1756,26 +1757,31 @@ static int files_log_ref_write(struct files_ref_store *refs,
  * errors, rollback the lockfile, fill in *err and return -1.
  */
 static int write_ref_to_lockfile(struct ref_lock *lock,
-                                const struct object_id *oid, struct strbuf *err)
+                                const struct object_id *oid,
+                                int skip_oid_verification, struct strbuf *err)
 {
        static char term = '\n';
        struct object *o;
        int fd;
 
-       o = parse_object(the_repository, oid);
-       if (!o) {
-               strbuf_addf(err,
-                           "trying to write ref '%s' with nonexistent object %s",
-                           lock->ref_name, oid_to_hex(oid));
-               unlock_ref(lock);
-               return -1;
-       }
-       if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) {
-               strbuf_addf(err,
-                           "trying to write non-commit object %s to branch '%s'",
-                           oid_to_hex(oid), lock->ref_name);
-               unlock_ref(lock);
-               return -1;
+       if (!skip_oid_verification) {
+               o = parse_object(the_repository, oid);
+               if (!o) {
+                       strbuf_addf(
+                               err,
+                               "trying to write ref '%s' with nonexistent object %s",
+                               lock->ref_name, oid_to_hex(oid));
+                       unlock_ref(lock);
+                       return -1;
+               }
+               if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) {
+                       strbuf_addf(
+                               err,
+                               "trying to write non-commit object %s to branch '%s'",
+                               oid_to_hex(oid), lock->ref_name);
+                       unlock_ref(lock);
+                       return -1;
+               }
        }
        fd = get_lock_file_fd(&lock->lk);
        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)
 }
 
 static int files_reflog_iterator_peel(struct ref_iterator *ref_iterator,
-                                  struct object_id *peeled)
+                                     struct object_id *peeled)
 {
        BUG("ref_iterator_peel() called for reflog_iterator");
 }
@@ -2575,8 +2581,10 @@ static int lock_ref_for_update(struct files_ref_store *refs,
                         * The reference already has the desired
                         * value, so we don't need to write it.
                         */
-               } else if (write_ref_to_lockfile(lock, &update->new_oid,
-                                                err)) {
+               } else if (write_ref_to_lockfile(
+                                  lock, &update->new_oid,
+                                  update->flags & REF_SKIP_OID_VERIFICATION,
+                                  err)) {
                        char *write_err = strbuf_detach(err, NULL);
 
                        /*
index 8fee3cfce1aa674a23ac7513358e204155d81af2..0f3aa84c9f0ea78c6a65bb5a7f555e7b75564521 100644 (file)
@@ -130,6 +130,7 @@ static int cmd_create_symref(struct ref_store *refs, const char **argv)
 static struct flag_definition transaction_flags[] = {
        FLAG_DEF(REF_NO_DEREF),
        FLAG_DEF(REF_FORCE_CREATE_REFLOG),
+       FLAG_DEF(REF_SKIP_OID_VERIFICATION),
        { NULL, 0 }
 };
 
index 658628375c85b17e44aa501c7624daddff4053f6..0d4c55f74ec6470f7241a0929a7727e567738d89 100755 (executable)
@@ -452,9 +452,8 @@ test_expect_success 'the --allow-unknown-type option does not consider replaceme
        # Create it manually, as "git replace" will die on bogus
        # types.
        head=$(git rev-parse --verify HEAD) &&
-       test_when_finished "rm -rf .git/refs/replace" &&
-       mkdir -p .git/refs/replace &&
-       echo $head >.git/refs/replace/$bogus_short_sha1 &&
+       test_when_finished "test-tool ref-store main delete-refs 0 msg refs/replace/$bogus_short_sha1" &&
+       test-tool ref-store main update-ref msg "refs/replace/$bogus_short_sha1" $head $ZERO_OID REF_SKIP_OID_VERIFICATION &&
 
        cat >expect <<-EOF &&
        commit
index 0544d58a6ea5478f046b571f7e249a5942da37e2..e3cf0ffbe59c449b218cafedd74bd3cf07e82ff1 100755 (executable)
@@ -72,7 +72,8 @@ check_verify_failure () {
 
                # Manually create the broken, we cannot do it with
                # update-ref
-               echo "$bad_tag" >"bad-tag/$tag_ref" &&
+               test-tool -C bad-tag ref-store main delete-refs 0 msg "$tag_ref" &&
+               test-tool -C bad-tag ref-store main update-ref msg "$tag_ref" $bad_tag $ZERO_OID REF_SKIP_OID_VERIFICATION &&
 
                # Unlike fsck-ing unreachable content above, this
                # will always fail.
@@ -83,7 +84,8 @@ check_verify_failure () {
                # Make sure the earlier test created it for us
                git rev-parse "$bad_tag" &&
 
-               echo "$bad_tag" >"bad-tag/$tag_ref" &&
+               test-tool -C bad-tag ref-store main delete-refs 0 msg "$tag_ref" &&
+               test-tool -C bad-tag ref-store main update-ref msg "$tag_ref" $bad_tag $ZERO_OID REF_SKIP_OID_VERIFICATION &&
 
                printf "%s tag\t%s\n" "$bad_tag" "$tag_ref" >expected &&
                git -C bad-tag for-each-ref "$tag_ref" >actual &&