PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/t/t1404-update-ref-errors.sh

https://gitlab.com/oyvholm/git
Shell | 407 lines | 374 code | 22 blank | 11 comment | 1 complexity | 1cfb7035411f3315fb5809bcade87bd0 MD5 | raw file
  1. #!/bin/sh
  2. test_description='Test git update-ref error handling'
  3. . ./test-lib.sh
  4. # Create some references, perhaps run pack-refs --all, then try to
  5. # create some more references. Ensure that the second creation fails
  6. # with the correct error message.
  7. # Usage: test_update_rejected <before> <pack> <create> <error>
  8. # <before> is a ws-separated list of refs to create before the test
  9. # <pack> (true or false) tells whether to pack the refs before the test
  10. # <create> is a list of variables to attempt creating
  11. # <error> is a string to look for in the stderr of update-ref.
  12. # All references are created in the namespace specified by the current
  13. # value of $prefix.
  14. test_update_rejected () {
  15. before="$1" &&
  16. pack="$2" &&
  17. create="$3" &&
  18. error="$4" &&
  19. printf "create $prefix/%s $C\n" $before |
  20. git update-ref --stdin &&
  21. git for-each-ref $prefix >unchanged &&
  22. if $pack
  23. then
  24. git pack-refs --all
  25. fi &&
  26. printf "create $prefix/%s $C\n" $create >input &&
  27. test_must_fail git update-ref --stdin <input 2>output.err &&
  28. grep -F "$error" output.err &&
  29. git for-each-ref $prefix >actual &&
  30. test_cmp unchanged actual
  31. }
  32. Q="'"
  33. test_expect_success 'setup' '
  34. git commit --allow-empty -m Initial &&
  35. C=$(git rev-parse HEAD) &&
  36. git commit --allow-empty -m Second &&
  37. D=$(git rev-parse HEAD) &&
  38. git commit --allow-empty -m Third &&
  39. E=$(git rev-parse HEAD)
  40. '
  41. test_expect_success 'existing loose ref is a simple prefix of new' '
  42. prefix=refs/1l &&
  43. test_update_rejected "a c e" false "b c/x d" \
  44. "$Q$prefix/c$Q exists; cannot create $Q$prefix/c/x$Q"
  45. '
  46. test_expect_success 'existing packed ref is a simple prefix of new' '
  47. prefix=refs/1p &&
  48. test_update_rejected "a c e" true "b c/x d" \
  49. "$Q$prefix/c$Q exists; cannot create $Q$prefix/c/x$Q"
  50. '
  51. test_expect_success 'existing loose ref is a deeper prefix of new' '
  52. prefix=refs/2l &&
  53. test_update_rejected "a c e" false "b c/x/y d" \
  54. "$Q$prefix/c$Q exists; cannot create $Q$prefix/c/x/y$Q"
  55. '
  56. test_expect_success 'existing packed ref is a deeper prefix of new' '
  57. prefix=refs/2p &&
  58. test_update_rejected "a c e" true "b c/x/y d" \
  59. "$Q$prefix/c$Q exists; cannot create $Q$prefix/c/x/y$Q"
  60. '
  61. test_expect_success 'new ref is a simple prefix of existing loose' '
  62. prefix=refs/3l &&
  63. test_update_rejected "a c/x e" false "b c d" \
  64. "$Q$prefix/c/x$Q exists; cannot create $Q$prefix/c$Q"
  65. '
  66. test_expect_success 'new ref is a simple prefix of existing packed' '
  67. prefix=refs/3p &&
  68. test_update_rejected "a c/x e" true "b c d" \
  69. "$Q$prefix/c/x$Q exists; cannot create $Q$prefix/c$Q"
  70. '
  71. test_expect_success 'new ref is a deeper prefix of existing loose' '
  72. prefix=refs/4l &&
  73. test_update_rejected "a c/x/y e" false "b c d" \
  74. "$Q$prefix/c/x/y$Q exists; cannot create $Q$prefix/c$Q"
  75. '
  76. test_expect_success 'new ref is a deeper prefix of existing packed' '
  77. prefix=refs/4p &&
  78. test_update_rejected "a c/x/y e" true "b c d" \
  79. "$Q$prefix/c/x/y$Q exists; cannot create $Q$prefix/c$Q"
  80. '
  81. test_expect_success 'one new ref is a simple prefix of another' '
  82. prefix=refs/5 &&
  83. test_update_rejected "a e" false "b c c/x d" \
  84. "cannot process $Q$prefix/c$Q and $Q$prefix/c/x$Q at the same time"
  85. '
  86. test_expect_success 'empty directory should not fool rev-parse' '
  87. prefix=refs/e-rev-parse &&
  88. git update-ref $prefix/foo $C &&
  89. git pack-refs --all &&
  90. mkdir -p .git/$prefix/foo/bar/baz &&
  91. echo "$C" >expected &&
  92. git rev-parse $prefix/foo >actual &&
  93. test_cmp expected actual
  94. '
  95. test_expect_success 'empty directory should not fool for-each-ref' '
  96. prefix=refs/e-for-each-ref &&
  97. git update-ref $prefix/foo $C &&
  98. git for-each-ref $prefix >expected &&
  99. git pack-refs --all &&
  100. mkdir -p .git/$prefix/foo/bar/baz &&
  101. git for-each-ref $prefix >actual &&
  102. test_cmp expected actual
  103. '
  104. test_expect_success 'empty directory should not fool create' '
  105. prefix=refs/e-create &&
  106. mkdir -p .git/$prefix/foo/bar/baz &&
  107. printf "create %s $C\n" $prefix/foo |
  108. git update-ref --stdin
  109. '
  110. test_expect_success 'empty directory should not fool verify' '
  111. prefix=refs/e-verify &&
  112. git update-ref $prefix/foo $C &&
  113. git pack-refs --all &&
  114. mkdir -p .git/$prefix/foo/bar/baz &&
  115. printf "verify %s $C\n" $prefix/foo |
  116. git update-ref --stdin
  117. '
  118. test_expect_success 'empty directory should not fool 1-arg update' '
  119. prefix=refs/e-update-1 &&
  120. git update-ref $prefix/foo $C &&
  121. git pack-refs --all &&
  122. mkdir -p .git/$prefix/foo/bar/baz &&
  123. printf "update %s $D\n" $prefix/foo |
  124. git update-ref --stdin
  125. '
  126. test_expect_success 'empty directory should not fool 2-arg update' '
  127. prefix=refs/e-update-2 &&
  128. git update-ref $prefix/foo $C &&
  129. git pack-refs --all &&
  130. mkdir -p .git/$prefix/foo/bar/baz &&
  131. printf "update %s $D $C\n" $prefix/foo |
  132. git update-ref --stdin
  133. '
  134. test_expect_success 'empty directory should not fool 0-arg delete' '
  135. prefix=refs/e-delete-0 &&
  136. git update-ref $prefix/foo $C &&
  137. git pack-refs --all &&
  138. mkdir -p .git/$prefix/foo/bar/baz &&
  139. printf "delete %s\n" $prefix/foo |
  140. git update-ref --stdin
  141. '
  142. test_expect_success 'empty directory should not fool 1-arg delete' '
  143. prefix=refs/e-delete-1 &&
  144. git update-ref $prefix/foo $C &&
  145. git pack-refs --all &&
  146. mkdir -p .git/$prefix/foo/bar/baz &&
  147. printf "delete %s $C\n" $prefix/foo |
  148. git update-ref --stdin
  149. '
  150. # Test various errors when reading the old values of references...
  151. test_expect_success 'missing old value blocks update' '
  152. prefix=refs/missing-update &&
  153. cat >expected <<-EOF &&
  154. fatal: cannot lock ref $Q$prefix/foo$Q: unable to resolve reference $Q$prefix/foo$Q
  155. EOF
  156. printf "%s\n" "update $prefix/foo $E $D" |
  157. test_must_fail git update-ref --stdin 2>output.err &&
  158. test_cmp expected output.err
  159. '
  160. test_expect_success 'incorrect old value blocks update' '
  161. prefix=refs/incorrect-update &&
  162. git update-ref $prefix/foo $C &&
  163. cat >expected <<-EOF &&
  164. fatal: cannot lock ref $Q$prefix/foo$Q: is at $C but expected $D
  165. EOF
  166. printf "%s\n" "update $prefix/foo $E $D" |
  167. test_must_fail git update-ref --stdin 2>output.err &&
  168. test_cmp expected output.err
  169. '
  170. test_expect_success 'existing old value blocks create' '
  171. prefix=refs/existing-create &&
  172. git update-ref $prefix/foo $C &&
  173. cat >expected <<-EOF &&
  174. fatal: cannot lock ref $Q$prefix/foo$Q: reference already exists
  175. EOF
  176. printf "%s\n" "create $prefix/foo $E" |
  177. test_must_fail git update-ref --stdin 2>output.err &&
  178. test_cmp expected output.err
  179. '
  180. test_expect_success 'incorrect old value blocks delete' '
  181. prefix=refs/incorrect-delete &&
  182. git update-ref $prefix/foo $C &&
  183. cat >expected <<-EOF &&
  184. fatal: cannot lock ref $Q$prefix/foo$Q: is at $C but expected $D
  185. EOF
  186. printf "%s\n" "delete $prefix/foo $D" |
  187. test_must_fail git update-ref --stdin 2>output.err &&
  188. test_cmp expected output.err
  189. '
  190. test_expect_success 'missing old value blocks indirect update' '
  191. prefix=refs/missing-indirect-update &&
  192. git symbolic-ref $prefix/symref $prefix/foo &&
  193. cat >expected <<-EOF &&
  194. fatal: cannot lock ref $Q$prefix/symref$Q: unable to resolve reference $Q$prefix/foo$Q
  195. EOF
  196. printf "%s\n" "update $prefix/symref $E $D" |
  197. test_must_fail git update-ref --stdin 2>output.err &&
  198. test_cmp expected output.err
  199. '
  200. test_expect_success 'incorrect old value blocks indirect update' '
  201. prefix=refs/incorrect-indirect-update &&
  202. git symbolic-ref $prefix/symref $prefix/foo &&
  203. git update-ref $prefix/foo $C &&
  204. cat >expected <<-EOF &&
  205. fatal: cannot lock ref $Q$prefix/symref$Q: is at $C but expected $D
  206. EOF
  207. printf "%s\n" "update $prefix/symref $E $D" |
  208. test_must_fail git update-ref --stdin 2>output.err &&
  209. test_cmp expected output.err
  210. '
  211. test_expect_success 'existing old value blocks indirect create' '
  212. prefix=refs/existing-indirect-create &&
  213. git symbolic-ref $prefix/symref $prefix/foo &&
  214. git update-ref $prefix/foo $C &&
  215. cat >expected <<-EOF &&
  216. fatal: cannot lock ref $Q$prefix/symref$Q: reference already exists
  217. EOF
  218. printf "%s\n" "create $prefix/symref $E" |
  219. test_must_fail git update-ref --stdin 2>output.err &&
  220. test_cmp expected output.err
  221. '
  222. test_expect_success 'incorrect old value blocks indirect delete' '
  223. prefix=refs/incorrect-indirect-delete &&
  224. git symbolic-ref $prefix/symref $prefix/foo &&
  225. git update-ref $prefix/foo $C &&
  226. cat >expected <<-EOF &&
  227. fatal: cannot lock ref $Q$prefix/symref$Q: is at $C but expected $D
  228. EOF
  229. printf "%s\n" "delete $prefix/symref $D" |
  230. test_must_fail git update-ref --stdin 2>output.err &&
  231. test_cmp expected output.err
  232. '
  233. test_expect_success 'missing old value blocks indirect no-deref update' '
  234. prefix=refs/missing-noderef-update &&
  235. git symbolic-ref $prefix/symref $prefix/foo &&
  236. cat >expected <<-EOF &&
  237. fatal: cannot lock ref $Q$prefix/symref$Q: reference is missing but expected $D
  238. EOF
  239. printf "%s\n" "option no-deref" "update $prefix/symref $E $D" |
  240. test_must_fail git update-ref --stdin 2>output.err &&
  241. test_cmp expected output.err
  242. '
  243. test_expect_success 'incorrect old value blocks indirect no-deref update' '
  244. prefix=refs/incorrect-noderef-update &&
  245. git symbolic-ref $prefix/symref $prefix/foo &&
  246. git update-ref $prefix/foo $C &&
  247. cat >expected <<-EOF &&
  248. fatal: cannot lock ref $Q$prefix/symref$Q: is at $C but expected $D
  249. EOF
  250. printf "%s\n" "option no-deref" "update $prefix/symref $E $D" |
  251. test_must_fail git update-ref --stdin 2>output.err &&
  252. test_cmp expected output.err
  253. '
  254. test_expect_success 'existing old value blocks indirect no-deref create' '
  255. prefix=refs/existing-noderef-create &&
  256. git symbolic-ref $prefix/symref $prefix/foo &&
  257. git update-ref $prefix/foo $C &&
  258. cat >expected <<-EOF &&
  259. fatal: cannot lock ref $Q$prefix/symref$Q: reference already exists
  260. EOF
  261. printf "%s\n" "option no-deref" "create $prefix/symref $E" |
  262. test_must_fail git update-ref --stdin 2>output.err &&
  263. test_cmp expected output.err
  264. '
  265. test_expect_success 'incorrect old value blocks indirect no-deref delete' '
  266. prefix=refs/incorrect-noderef-delete &&
  267. git symbolic-ref $prefix/symref $prefix/foo &&
  268. git update-ref $prefix/foo $C &&
  269. cat >expected <<-EOF &&
  270. fatal: cannot lock ref $Q$prefix/symref$Q: is at $C but expected $D
  271. EOF
  272. printf "%s\n" "option no-deref" "delete $prefix/symref $D" |
  273. test_must_fail git update-ref --stdin 2>output.err &&
  274. test_cmp expected output.err
  275. '
  276. test_expect_success 'non-empty directory blocks create' '
  277. prefix=refs/ne-create &&
  278. mkdir -p .git/$prefix/foo/bar &&
  279. : >.git/$prefix/foo/bar/baz.lock &&
  280. test_when_finished "rm -f .git/$prefix/foo/bar/baz.lock" &&
  281. cat >expected <<-EOF &&
  282. fatal: cannot lock ref $Q$prefix/foo$Q: there is a non-empty directory $Q.git/$prefix/foo$Q blocking reference $Q$prefix/foo$Q
  283. EOF
  284. printf "%s\n" "update $prefix/foo $C" |
  285. test_must_fail git update-ref --stdin 2>output.err &&
  286. test_cmp expected output.err &&
  287. cat >expected <<-EOF &&
  288. fatal: cannot lock ref $Q$prefix/foo$Q: unable to resolve reference $Q$prefix/foo$Q
  289. EOF
  290. printf "%s\n" "update $prefix/foo $D $C" |
  291. test_must_fail git update-ref --stdin 2>output.err &&
  292. test_cmp expected output.err
  293. '
  294. test_expect_success 'broken reference blocks create' '
  295. prefix=refs/broken-create &&
  296. mkdir -p .git/$prefix &&
  297. echo "gobbledigook" >.git/$prefix/foo &&
  298. test_when_finished "rm -f .git/$prefix/foo" &&
  299. cat >expected <<-EOF &&
  300. fatal: cannot lock ref $Q$prefix/foo$Q: unable to resolve reference $Q$prefix/foo$Q: reference broken
  301. EOF
  302. printf "%s\n" "update $prefix/foo $C" |
  303. test_must_fail git update-ref --stdin 2>output.err &&
  304. test_cmp expected output.err &&
  305. cat >expected <<-EOF &&
  306. fatal: cannot lock ref $Q$prefix/foo$Q: unable to resolve reference $Q$prefix/foo$Q: reference broken
  307. EOF
  308. printf "%s\n" "update $prefix/foo $D $C" |
  309. test_must_fail git update-ref --stdin 2>output.err &&
  310. test_cmp expected output.err
  311. '
  312. test_expect_success 'non-empty directory blocks indirect create' '
  313. prefix=refs/ne-indirect-create &&
  314. git symbolic-ref $prefix/symref $prefix/foo &&
  315. mkdir -p .git/$prefix/foo/bar &&
  316. : >.git/$prefix/foo/bar/baz.lock &&
  317. test_when_finished "rm -f .git/$prefix/foo/bar/baz.lock" &&
  318. cat >expected <<-EOF &&
  319. fatal: cannot lock ref $Q$prefix/symref$Q: there is a non-empty directory $Q.git/$prefix/foo$Q blocking reference $Q$prefix/foo$Q
  320. EOF
  321. printf "%s\n" "update $prefix/symref $C" |
  322. test_must_fail git update-ref --stdin 2>output.err &&
  323. test_cmp expected output.err &&
  324. cat >expected <<-EOF &&
  325. fatal: cannot lock ref $Q$prefix/symref$Q: unable to resolve reference $Q$prefix/foo$Q
  326. EOF
  327. printf "%s\n" "update $prefix/symref $D $C" |
  328. test_must_fail git update-ref --stdin 2>output.err &&
  329. test_cmp expected output.err
  330. '
  331. test_expect_success 'broken reference blocks indirect create' '
  332. prefix=refs/broken-indirect-create &&
  333. git symbolic-ref $prefix/symref $prefix/foo &&
  334. echo "gobbledigook" >.git/$prefix/foo &&
  335. test_when_finished "rm -f .git/$prefix/foo" &&
  336. cat >expected <<-EOF &&
  337. fatal: cannot lock ref $Q$prefix/symref$Q: unable to resolve reference $Q$prefix/foo$Q: reference broken
  338. EOF
  339. printf "%s\n" "update $prefix/symref $C" |
  340. test_must_fail git update-ref --stdin 2>output.err &&
  341. test_cmp expected output.err &&
  342. cat >expected <<-EOF &&
  343. fatal: cannot lock ref $Q$prefix/symref$Q: unable to resolve reference $Q$prefix/foo$Q: reference broken
  344. EOF
  345. printf "%s\n" "update $prefix/symref $D $C" |
  346. test_must_fail git update-ref --stdin 2>output.err &&
  347. test_cmp expected output.err
  348. '
  349. test_done