Skip to content

Commit 0e729c7

Browse files
mhaggergitster
authored andcommitted
update-ref: fix "verify" command with missing <oldvalue>
If "git update-ref --stdin" was given a "verify" command with no "<newvalue>" at all (not even zeros), the code was mistakenly setting have_old=0 (and leaving old_sha1 uninitialized). But this is incorrect: this command is supposed to verify that the reference doesn't exist. So in this case we really need old_sha1 to be set to null_sha1 and have_old to be set to 1. Moreover, since have_old was being set to zero, *no* check of the old value was being done, so the new value of the reference was being set unconditionally to the value in new_sha1. new_sha1, in turn, was set to null_sha1 in the expectation that that was the old value and it shouldn't be changed. But because the precondition was not being checked, the result was that the reference was being deleted unconditionally. So, if <oldvalue> is missing, set have_old unconditionally and set old_sha1 to null_sha1. Signed-off-by: Michael Haggerty <[email protected]> Acked-by: Brad King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a46e41f commit 0e729c7

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

builtin/update-ref.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -282,26 +282,22 @@ static const char *parse_cmd_verify(struct ref_transaction *transaction,
282282
char *refname;
283283
unsigned char new_sha1[20];
284284
unsigned char old_sha1[20];
285-
int have_old;
286285

287286
refname = parse_refname(input, &next);
288287
if (!refname)
289288
die("verify: missing <ref>");
290289

291290
if (parse_next_sha1(input, &next, old_sha1, "verify", refname,
292-
PARSE_SHA1_OLD)) {
293-
hashclr(new_sha1);
294-
have_old = 0;
295-
} else {
296-
hashcpy(new_sha1, old_sha1);
297-
have_old = 1;
298-
}
291+
PARSE_SHA1_OLD))
292+
hashclr(old_sha1);
293+
294+
hashcpy(new_sha1, old_sha1);
299295

300296
if (*next != line_termination)
301297
die("verify %s: extra input: %s", refname, next);
302298

303299
if (ref_transaction_update(transaction, refname, new_sha1, old_sha1,
304-
update_flags, have_old, msg, &err))
300+
update_flags, 1, msg, &err))
305301
die("%s", err.buf);
306302

307303
update_flags = 0;

t/t1400-update-ref.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ test_expect_success 'stdin verify fails for mistaken null value' '
655655
test_cmp expect actual
656656
'
657657

658-
test_expect_failure 'stdin verify fails for mistaken empty value' '
658+
test_expect_success 'stdin verify fails for mistaken empty value' '
659659
M=$(git rev-parse $m) &&
660660
test_when_finished "git update-ref $m $M" &&
661661
git rev-parse $m >expect &&
@@ -1020,7 +1020,7 @@ test_expect_success 'stdin -z verify fails for mistaken null value' '
10201020
test_cmp expect actual
10211021
'
10221022

1023-
test_expect_failure 'stdin -z verify fails for mistaken empty value' '
1023+
test_expect_success 'stdin -z verify fails for mistaken empty value' '
10241024
M=$(git rev-parse $m) &&
10251025
test_when_finished "git update-ref $m $M" &&
10261026
git rev-parse $m >expect &&

0 commit comments

Comments
 (0)