Files
git/t/t1421-reflog-write.sh
Patrick Steinhardt 7aa619c36f builtin/reflog: implement subcommand to write new entries
While we provide a couple of subcommands in git-reflog(1) to remove
reflog entries, we don't provide any to write new entries. Obviously
this is not an operation that really would be needed for many use cases
out there, or otherwise people would have complained that such a command
does not exist yet. But the introduction of the "reftable" backend
changes the picture a bit, as it is now basically impossible to manually
append a reflog entry if one wanted to do so due to the binary format.

Plug this gap by introducing a simple "write" subcommand. For now, all
this command does is to append a single new reflog entry with the given
object IDs and message to the reflog. More specifically, it is not yet
possible to:

  - Write multiple reflog entries at once.

  - Insert reflog entries at arbitrary indices.

  - Specify the date of the reflog entry.

  - Insert reflog entries that refer to nonexistent objects.

If required, those features can be added at a future point in time. For
now though, the new command aims to fulfill the most basic use cases
while being as strict as possible when it comes to verifying parameters.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2025-08-06 07:36:30 -07:00

129 lines
3.4 KiB
Bash
Executable File

#!/bin/sh
test_description='Manually write reflog entries'
. ./test-lib.sh
SIGNATURE="C O Mitter <committer@example.com> 1112911993 -0700"
test_reflog_matches () {
repo="$1" &&
refname="$2" &&
cat >actual &&
test-tool -C "$repo" ref-store main for-each-reflog-ent "$refname" >expected &&
test_cmp expected actual
}
test_expect_success 'invalid number of arguments' '
test_when_finished "rm -rf repo" &&
git init repo &&
(
cd repo &&
for args in "" "1" "1 2" "1 2 3" "1 2 3 4 5"
do
test_must_fail git reflog write $args 2>err &&
test_grep "usage: git reflog write" err || return 1
done
)
'
test_expect_success 'invalid refname' '
test_when_finished "rm -rf repo" &&
git init repo &&
(
cd repo &&
test_must_fail git reflog write "refs/heads/ invalid" $ZERO_OID $ZERO_OID first 2>err &&
test_grep "invalid reference name: " err
)
'
test_expect_success 'unqualified refname is rejected' '
test_when_finished "rm -rf repo" &&
git init repo &&
(
cd repo &&
test_must_fail git reflog write unqualified $ZERO_OID $ZERO_OID first 2>err &&
test_grep "invalid reference name: " err
)
'
test_expect_success 'nonexistent object IDs' '
test_when_finished "rm -rf repo" &&
git init repo &&
(
cd repo &&
test_must_fail git reflog write refs/heads/something $(test_oid deadbeef) $ZERO_OID old-object-id 2>err &&
test_grep "old object .* does not exist" err &&
test_must_fail git reflog write refs/heads/something $ZERO_OID $(test_oid deadbeef) new-object-id 2>err &&
test_grep "new object .* does not exist" err
)
'
test_expect_success 'abbreviated object IDs' '
test_when_finished "rm -rf repo" &&
git init repo &&
(
cd repo &&
test_commit initial &&
abbreviated_oid=$(git rev-parse HEAD | test_copy_bytes 8) &&
test_must_fail git reflog write refs/heads/something $abbreviated_oid $ZERO_OID old-object-id 2>err &&
test_grep "invalid old object ID" err &&
test_must_fail git reflog write refs/heads/something $ZERO_OID $abbreviated_oid new-object-id 2>err &&
test_grep "invalid new object ID" err
)
'
test_expect_success 'reflog message gets normalized' '
test_when_finished "rm -rf repo" &&
git init repo &&
(
cd repo &&
test_commit initial &&
COMMIT_OID=$(git rev-parse HEAD) &&
git reflog write HEAD $COMMIT_OID $COMMIT_OID "$(printf "message\nwith\nnewlines")" &&
git reflog show -1 --format=%gs HEAD >actual &&
echo "message with newlines" >expected &&
test_cmp expected actual
)
'
test_expect_success 'simple writes' '
test_when_finished "rm -rf repo" &&
git init repo &&
(
cd repo &&
test_commit initial &&
COMMIT_OID=$(git rev-parse HEAD) &&
git reflog write refs/heads/something $ZERO_OID $COMMIT_OID first &&
test_reflog_matches . refs/heads/something <<-EOF &&
$ZERO_OID $COMMIT_OID $SIGNATURE first
EOF
git reflog write refs/heads/something $COMMIT_OID $COMMIT_OID second &&
# Note: the old object ID of the second reflog entry is broken.
# This will be fixed in subsequent commits.
test_reflog_matches . refs/heads/something <<-EOF
$ZERO_OID $COMMIT_OID $SIGNATURE first
$ZERO_OID $COMMIT_OID $SIGNATURE second
EOF
)
'
test_expect_success 'can write to root ref' '
test_when_finished "rm -rf repo" &&
git init repo &&
(
cd repo &&
test_commit initial &&
COMMIT_OID=$(git rev-parse HEAD) &&
git reflog write ROOT_REF_HEAD $ZERO_OID $COMMIT_OID first &&
test_reflog_matches . ROOT_REF_HEAD <<-EOF
$ZERO_OID $COMMIT_OID $SIGNATURE first
EOF
)
'
test_done