summaryrefslogtreecommitdiff
path: root/t/t1421-reflog-write.sh
blob: 46df64c1761b40d18366c81a0fabc010e1d40750 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/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 &&
		test_reflog_matches . refs/heads/something <<-EOF
		$ZERO_OID $COMMIT_OID $SIGNATURE	first
		$COMMIT_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