Skip to content

Commit e9706a1

Browse files
hanwengitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0464d0a commit e9706a1

File tree

5 files changed

+43
-27
lines changed

5 files changed

+43
-27
lines changed

refs.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,12 +615,18 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err);
615615
*/
616616
#define REF_FORCE_CREATE_REFLOG (1 << 1)
617617

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+
618624
/*
619625
* Bitmask of all of the flags that are allowed to be passed in to
620626
* ref_transaction_update() and friends:
621627
*/
622628
#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)
624630

625631
/*
626632
* Add a reference update to transaction. `new_oid` is the value that

refs/files-backend.c

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,7 +1353,8 @@ static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname)
13531353
}
13541354

13551355
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);
13571358
static int commit_ref_update(struct files_ref_store *refs,
13581359
struct ref_lock *lock,
13591360
const struct object_id *oid, const char *logmsg,
@@ -1500,7 +1501,7 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
15001501
}
15011502
oidcpy(&lock->old_oid, &orig_oid);
15021503

1503-
if (write_ref_to_lockfile(lock, &orig_oid, &err) ||
1504+
if (write_ref_to_lockfile(lock, &orig_oid, 0, &err) ||
15041505
commit_ref_update(refs, lock, &orig_oid, logmsg, &err)) {
15051506
error("unable to write current sha1 into %s: %s", newrefname, err.buf);
15061507
strbuf_release(&err);
@@ -1520,7 +1521,7 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
15201521

15211522
flag = log_all_ref_updates;
15221523
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) ||
15241525
commit_ref_update(refs, lock, &orig_oid, NULL, &err)) {
15251526
error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
15261527
strbuf_release(&err);
@@ -1756,26 +1757,31 @@ static int files_log_ref_write(struct files_ref_store *refs,
17561757
* errors, rollback the lockfile, fill in *err and return -1.
17571758
*/
17581759
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)
17601762
{
17611763
static char term = '\n';
17621764
struct object *o;
17631765
int fd;
17641766

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+
}
17791785
}
17801786
fd = get_lock_file_fd(&lock->lk);
17811787
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)
21892195
}
21902196

21912197
static int files_reflog_iterator_peel(struct ref_iterator *ref_iterator,
2192-
struct object_id *peeled)
2198+
struct object_id *peeled)
21932199
{
21942200
BUG("ref_iterator_peel() called for reflog_iterator");
21952201
}
@@ -2575,8 +2581,10 @@ static int lock_ref_for_update(struct files_ref_store *refs,
25752581
* The reference already has the desired
25762582
* value, so we don't need to write it.
25772583
*/
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)) {
25802588
char *write_err = strbuf_detach(err, NULL);
25812589

25822590
/*

t/helper/test-ref-store.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ static int cmd_create_symref(struct ref_store *refs, const char **argv)
130130
static struct flag_definition transaction_flags[] = {
131131
FLAG_DEF(REF_NO_DEREF),
132132
FLAG_DEF(REF_FORCE_CREATE_REFLOG),
133+
FLAG_DEF(REF_SKIP_OID_VERIFICATION),
133134
{ NULL, 0 }
134135
};
135136

t/t1006-cat-file.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,8 @@ test_expect_success 'the --allow-unknown-type option does not consider replaceme
452452
# Create it manually, as "git replace" will die on bogus
453453
# types.
454454
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 &&
458457
459458
cat >expect <<-EOF &&
460459
commit

t/t3800-mktag.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ check_verify_failure () {
7272
7373
# Manually create the broken, we cannot do it with
7474
# 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 &&
7677
7778
# Unlike fsck-ing unreachable content above, this
7879
# will always fail.
@@ -83,7 +84,8 @@ check_verify_failure () {
8384
# Make sure the earlier test created it for us
8485
git rev-parse "$bad_tag" &&
8586
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 &&
8789
8890
printf "%s tag\t%s\n" "$bad_tag" "$tag_ref" >expected &&
8991
git -C bad-tag for-each-ref "$tag_ref" >actual &&

0 commit comments

Comments
 (0)