1
+#!/bin/sh
2
+
3
+test_description='git refs delete'
4
+
5
+. ./test-lib.sh
6
+
7
+setup_repo () {
8
+ git init "$1" &&
9
+ test_commit -C "$1" A &&
10
+ test_commit -C "$1" B
11
+}
12
+
13
+test_expect_success 'delete without oldvalue verification' '
14
+ test_when_finished "rm -rf repo" &&
15
+ setup_repo repo &&
16
+ (
17
+ cd repo &&
18
+ A=$(git rev-parse A) &&
19
+ git update-ref refs/heads/foo $A &&
20
+ git refs delete refs/heads/foo &&
21
+ test_must_fail git refs exists refs/heads/foo
22
+ )
23
+'
24
+
25
+test_expect_success 'delete with matching oldvalue' '
26
+ test_when_finished "rm -rf repo" &&
27
+ setup_repo repo &&
28
+ (
29
+ cd repo &&
30
+ A=$(git rev-parse A) &&
31
+ git update-ref refs/heads/foo $A &&
32
+ git refs delete refs/heads/foo $A &&
33
+ test_must_fail git refs exists refs/heads/foo
34
+ )
35
+'
36
+
37
+test_expect_success 'delete with stale oldvalue fails' '
38
+ test_when_finished "rm -rf repo" &&
39
+ setup_repo repo &&
40
+ (
41
+ cd repo &&
42
+ A=$(git rev-parse A) &&
43
+ B=$(git rev-parse B) &&
44
+ git update-ref refs/heads/foo $A &&
45
+ test_must_fail git refs delete refs/heads/foo $B 2>err &&
46
+ test_grep " but expected " err &&
47
+ git refs exists refs/heads/foo
48
+ )
49
+'
50
+
51
+test_expect_success 'delete with null oldvalue fails' '
52
+ test_when_finished "rm -rf repo" &&
53
+ setup_repo repo &&
54
+ (
55
+ cd repo &&
56
+ A=$(git rev-parse A) &&
57
+ git update-ref refs/heads/foo $A &&
58
+ test_must_fail git refs delete refs/heads/foo $ZERO_OID 2>err &&
59
+ test_grep "null old object ID" err &&
60
+ git refs exists refs/heads/foo
61
+ )
62
+'
63
+
64
+test_expect_success 'delete with invalid oldvalue fails' '
65
+ test_when_finished "rm -rf repo" &&
66
+ setup_repo repo &&
67
+ (
68
+ cd repo &&
69
+ A=$(git rev-parse A) &&
70
+ git update-ref refs/heads/foo $A &&
71
+ test_must_fail git refs delete refs/heads/foo invalid-oid 2>err &&
72
+ test_grep "invalid old object ID" err &&
73
+ git refs exists refs/heads/foo
74
+ )
75
+'
76
+
77
+test_expect_success 'delete symref with --no-deref leaves target intact' '
78
+ test_when_finished "rm -rf repo" &&
79
+ setup_repo repo &&
80
+ (
81
+ cd repo &&
82
+ A=$(git rev-parse A) &&
83
+ git update-ref refs/heads/foo $A &&
84
+ git symbolic-ref refs/heads/symref refs/heads/foo &&
85
+ git refs delete --no-deref refs/heads/symref &&
86
+ test_must_fail git refs exists refs/heads/symref &&
87
+ git refs exists refs/heads/foo
88
+ )
89
+'
90
+
91
+test_expect_success 'delete symref with --no-deref verifies target OID' '
92
+ test_when_finished "rm -rf repo" &&
93
+ setup_repo repo &&
94
+ (
95
+ cd repo &&
96
+ A=$(git rev-parse A) &&
97
+ B=$(git rev-parse B) &&
98
+ git update-ref refs/heads/foo $A &&
99
+ git symbolic-ref refs/heads/symref refs/heads/foo &&
100
+
101
+ test_must_fail git refs delete --no-deref refs/heads/symref $B &&
102
+ git refs exists refs/heads/symref &&
103
+
104
+ git refs delete --no-deref refs/heads/symref $A &&
105
+ test_must_fail git refs exists refs/heads/symref &&
106
+ git refs exists refs/heads/foo
107
+ )
108
+'
109
+
110
+test_expect_success 'delete with message records reason in reflog' '
111
+ test_when_finished "rm -rf repo" &&
112
+ setup_repo repo &&
113
+ (
114
+ cd repo &&
115
+ A=$(git rev-parse A) &&
116
+ git update-ref refs/heads/foo $A &&
117
+ git symbolic-ref HEAD refs/heads/foo &&
118
+ git refs delete --message=delete-reason refs/heads/foo &&
119
+ test_must_fail git refs exists refs/heads/foo &&
120
+ test-tool ref-store main for-each-reflog-ent HEAD >actual &&
121
+ test_grep "delete-reason$" actual
122
+ )
123
+'
124
+
125
+test_expect_success 'delete with empty message fails' '
126
+ test_when_finished "rm -rf repo" &&
127
+ setup_repo repo &&
128
+ (
129
+ cd repo &&
130
+ A=$(git rev-parse A) &&
131
+ git update-ref refs/heads/foo $A &&
132
+ test_must_fail git refs delete --message= refs/heads/foo 2>err &&
133
+ test_grep "empty message" err &&
134
+ git refs exists refs/heads/foo
135
+ )
136
+'
137
+
138
+test_expect_success 'delete without arguments fails' '
139
+ test_when_finished "rm -rf repo" &&
140
+ setup_repo repo &&
141
+ test_must_fail git -C repo refs delete 2>err &&
142
+ test_grep "requires reference name" err
143
+'
144
+
145
+test_expect_success 'delete with too many arguments fails' '
146
+ test_when_finished "rm -rf repo" &&
147
+ setup_repo repo &&
148
+ test_must_fail git refs delete one two three 2>err &&
149
+ test_grep "requires reference name" err
150
+'
151
+
152
+test_done