rugged 0.19.0 → 0.21.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (453) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +1 -1
  3. data/README.md +151 -25
  4. data/ext/rugged/extconf.rb +48 -27
  5. data/ext/rugged/rugged.c +107 -35
  6. data/ext/rugged/rugged.h +44 -8
  7. data/ext/rugged/rugged_blame.c +292 -0
  8. data/ext/rugged/rugged_blob.c +84 -31
  9. data/ext/rugged/rugged_branch.c +90 -223
  10. data/ext/rugged/rugged_branch_collection.c +445 -0
  11. data/ext/rugged/rugged_commit.c +190 -9
  12. data/ext/rugged/rugged_config.c +1 -1
  13. data/ext/rugged/rugged_cred.c +125 -0
  14. data/ext/rugged/rugged_diff.c +267 -94
  15. data/ext/rugged/rugged_diff_delta.c +14 -3
  16. data/ext/rugged/rugged_diff_hunk.c +31 -28
  17. data/ext/rugged/rugged_diff_line.c +21 -17
  18. data/ext/rugged/rugged_index.c +326 -6
  19. data/ext/rugged/rugged_note.c +39 -34
  20. data/ext/rugged/rugged_object.c +9 -9
  21. data/ext/rugged/rugged_patch.c +245 -0
  22. data/ext/rugged/rugged_reference.c +67 -332
  23. data/ext/rugged/rugged_reference_collection.c +490 -0
  24. data/ext/rugged/rugged_remote.c +447 -351
  25. data/ext/rugged/rugged_remote_collection.c +285 -0
  26. data/ext/rugged/rugged_repo.c +752 -203
  27. data/ext/rugged/rugged_revwalk.c +119 -27
  28. data/ext/rugged/rugged_settings.c +48 -1
  29. data/ext/rugged/rugged_signature.c +30 -16
  30. data/ext/rugged/rugged_tag.c +86 -191
  31. data/ext/rugged/rugged_tag_collection.c +279 -0
  32. data/ext/rugged/rugged_tree.c +159 -22
  33. data/lib/rugged/branch.rb +8 -17
  34. data/lib/rugged/credentials.rb +43 -0
  35. data/lib/rugged/diff/delta.rb +1 -2
  36. data/lib/rugged/diff/hunk.rb +4 -9
  37. data/lib/rugged/diff/line.rb +23 -3
  38. data/lib/rugged/diff.rb +3 -1
  39. data/lib/rugged/patch.rb +26 -0
  40. data/lib/rugged/reference.rb +1 -3
  41. data/lib/rugged/remote.rb +0 -9
  42. data/lib/rugged/repository.rb +70 -13
  43. data/lib/rugged/tag.rb +23 -18
  44. data/lib/rugged/tree.rb +7 -0
  45. data/lib/rugged/version.rb +1 -1
  46. data/lib/rugged.rb +8 -1
  47. data/vendor/libgit2/AUTHORS +75 -0
  48. data/vendor/libgit2/CMakeLists.txt +490 -0
  49. data/vendor/libgit2/COPYING +920 -0
  50. data/vendor/libgit2/Makefile.embed +25 -7
  51. data/vendor/libgit2/cmake/Modules/AddCFlagIfSupported.cmake +16 -0
  52. data/vendor/libgit2/cmake/Modules/FindHTTP_Parser.cmake +39 -0
  53. data/vendor/libgit2/cmake/Modules/FindIconv.cmake +43 -0
  54. data/vendor/libgit2/cmake/Modules/FindLIBSSH2.cmake +44 -0
  55. data/vendor/libgit2/deps/http-parser/LICENSE-MIT +23 -0
  56. data/vendor/libgit2/deps/regex/regex.c +10 -3
  57. data/vendor/libgit2/include/git2/attr.h +2 -1
  58. data/vendor/libgit2/include/git2/blame.h +213 -0
  59. data/vendor/libgit2/include/git2/blob.h +57 -29
  60. data/vendor/libgit2/include/git2/branch.h +56 -44
  61. data/vendor/libgit2/include/git2/buffer.h +112 -0
  62. data/vendor/libgit2/include/git2/checkout.h +72 -37
  63. data/vendor/libgit2/include/git2/cherrypick.h +87 -0
  64. data/vendor/libgit2/include/git2/clone.h +153 -56
  65. data/vendor/libgit2/include/git2/commit.h +82 -12
  66. data/vendor/libgit2/include/git2/common.h +31 -19
  67. data/vendor/libgit2/include/git2/config.h +124 -19
  68. data/vendor/libgit2/include/git2/cred_helpers.h +1 -1
  69. data/vendor/libgit2/include/git2/diff.h +636 -494
  70. data/vendor/libgit2/include/git2/errors.h +48 -15
  71. data/vendor/libgit2/include/git2/filter.h +149 -0
  72. data/vendor/libgit2/include/git2/graph.h +14 -0
  73. data/vendor/libgit2/include/git2/index.h +95 -54
  74. data/vendor/libgit2/include/git2/indexer.h +15 -9
  75. data/vendor/libgit2/include/git2/merge.h +402 -39
  76. data/vendor/libgit2/include/git2/message.h +9 -14
  77. data/vendor/libgit2/include/git2/net.h +5 -0
  78. data/vendor/libgit2/include/git2/notes.h +6 -6
  79. data/vendor/libgit2/include/git2/object.h +34 -2
  80. data/vendor/libgit2/include/git2/odb.h +77 -14
  81. data/vendor/libgit2/include/git2/odb_backend.h +50 -6
  82. data/vendor/libgit2/include/git2/oid.h +4 -8
  83. data/vendor/libgit2/include/git2/pack.h +58 -4
  84. data/vendor/libgit2/include/git2/patch.h +277 -0
  85. data/vendor/libgit2/include/git2/pathspec.h +263 -0
  86. data/vendor/libgit2/include/git2/push.h +55 -5
  87. data/vendor/libgit2/include/git2/reflog.h +11 -8
  88. data/vendor/libgit2/include/git2/refs.h +219 -33
  89. data/vendor/libgit2/include/git2/refspec.h +3 -4
  90. data/vendor/libgit2/include/git2/remote.h +216 -76
  91. data/vendor/libgit2/include/git2/repository.h +85 -40
  92. data/vendor/libgit2/include/git2/reset.h +15 -4
  93. data/vendor/libgit2/include/git2/revert.h +86 -0
  94. data/vendor/libgit2/include/git2/revparse.h +27 -16
  95. data/vendor/libgit2/include/git2/revwalk.h +44 -6
  96. data/vendor/libgit2/include/git2/signature.h +17 -3
  97. data/vendor/libgit2/include/git2/stash.h +8 -12
  98. data/vendor/libgit2/include/git2/status.h +67 -18
  99. data/vendor/libgit2/include/git2/submodule.h +100 -85
  100. data/vendor/libgit2/include/git2/sys/commit.h +38 -4
  101. data/vendor/libgit2/include/git2/sys/config.h +44 -3
  102. data/vendor/libgit2/include/git2/sys/diff.h +91 -0
  103. data/vendor/libgit2/include/git2/sys/filter.h +301 -0
  104. data/vendor/libgit2/include/git2/sys/index.h +0 -2
  105. data/vendor/libgit2/include/git2/sys/mempack.h +85 -0
  106. data/vendor/libgit2/include/git2/sys/odb_backend.h +33 -11
  107. data/vendor/libgit2/include/git2/sys/refdb_backend.h +51 -5
  108. data/vendor/libgit2/include/git2/sys/reflog.h +21 -0
  109. data/vendor/libgit2/include/git2/sys/refs.h +2 -2
  110. data/vendor/libgit2/include/git2/sys/repository.h +19 -1
  111. data/vendor/libgit2/include/git2/transport.h +216 -75
  112. data/vendor/libgit2/include/git2/tree.h +32 -11
  113. data/vendor/libgit2/include/git2/types.h +124 -10
  114. data/vendor/libgit2/include/git2/version.h +4 -2
  115. data/vendor/libgit2/include/git2.h +41 -40
  116. data/vendor/libgit2/libgit2.pc.in +10 -0
  117. data/vendor/libgit2/src/array.h +16 -8
  118. data/vendor/libgit2/src/attr.c +140 -402
  119. data/vendor/libgit2/src/attr.h +1 -33
  120. data/vendor/libgit2/src/attr_file.c +298 -135
  121. data/vendor/libgit2/src/attr_file.h +61 -22
  122. data/vendor/libgit2/src/attrcache.c +449 -0
  123. data/vendor/libgit2/src/attrcache.h +38 -6
  124. data/vendor/libgit2/src/bitvec.h +75 -0
  125. data/vendor/libgit2/src/blame.c +488 -0
  126. data/vendor/libgit2/src/blame.h +93 -0
  127. data/vendor/libgit2/src/blame_git.c +624 -0
  128. data/vendor/libgit2/src/blame_git.h +20 -0
  129. data/vendor/libgit2/src/blob.c +137 -92
  130. data/vendor/libgit2/src/blob.h +9 -0
  131. data/vendor/libgit2/src/branch.c +175 -156
  132. data/vendor/libgit2/src/buf_text.c +29 -14
  133. data/vendor/libgit2/src/buf_text.h +5 -4
  134. data/vendor/libgit2/src/buffer.c +158 -15
  135. data/vendor/libgit2/src/buffer.h +33 -20
  136. data/vendor/libgit2/src/cc-compat.h +8 -2
  137. data/vendor/libgit2/src/checkout.c +1081 -287
  138. data/vendor/libgit2/src/checkout.h +1 -1
  139. data/vendor/libgit2/src/cherrypick.c +226 -0
  140. data/vendor/libgit2/src/clone.c +297 -245
  141. data/vendor/libgit2/src/{compress.h → clone.h} +4 -8
  142. data/vendor/libgit2/src/commit.c +313 -101
  143. data/vendor/libgit2/src/commit.h +6 -3
  144. data/vendor/libgit2/src/commit_list.c +3 -3
  145. data/vendor/libgit2/src/commit_list.h +1 -1
  146. data/vendor/libgit2/src/common.h +74 -1
  147. data/vendor/libgit2/src/config.c +570 -145
  148. data/vendor/libgit2/src/config.h +39 -6
  149. data/vendor/libgit2/src/config_cache.c +32 -18
  150. data/vendor/libgit2/src/config_file.c +711 -424
  151. data/vendor/libgit2/src/config_file.h +4 -3
  152. data/vendor/libgit2/src/crlf.c +149 -109
  153. data/vendor/libgit2/src/date.c +35 -7
  154. data/vendor/libgit2/src/delta.c +1 -1
  155. data/vendor/libgit2/src/diff.c +719 -414
  156. data/vendor/libgit2/src/diff.h +52 -7
  157. data/vendor/libgit2/src/diff_driver.c +183 -85
  158. data/vendor/libgit2/src/diff_driver.h +1 -1
  159. data/vendor/libgit2/src/diff_file.c +65 -84
  160. data/vendor/libgit2/src/diff_file.h +12 -10
  161. data/vendor/libgit2/src/diff_patch.c +274 -333
  162. data/vendor/libgit2/src/diff_patch.h +10 -9
  163. data/vendor/libgit2/src/diff_print.c +381 -179
  164. data/vendor/libgit2/src/diff_stats.c +336 -0
  165. data/vendor/libgit2/src/diff_tform.c +393 -215
  166. data/vendor/libgit2/src/diff_xdiff.c +117 -42
  167. data/vendor/libgit2/src/errors.c +59 -4
  168. data/vendor/libgit2/src/fetch.c +40 -34
  169. data/vendor/libgit2/src/fetch.h +2 -5
  170. data/vendor/libgit2/src/fetchhead.c +14 -7
  171. data/vendor/libgit2/src/filebuf.c +13 -24
  172. data/vendor/libgit2/src/filebuf.h +3 -3
  173. data/vendor/libgit2/src/fileops.c +104 -296
  174. data/vendor/libgit2/src/fileops.h +23 -94
  175. data/vendor/libgit2/src/filter.c +642 -43
  176. data/vendor/libgit2/src/filter.h +7 -66
  177. data/vendor/libgit2/src/fnmatch.c +46 -3
  178. data/vendor/libgit2/src/fnmatch.h +24 -3
  179. data/vendor/libgit2/src/global.c +158 -42
  180. data/vendor/libgit2/src/global.h +9 -0
  181. data/vendor/libgit2/src/graph.c +34 -20
  182. data/vendor/libgit2/src/hash/hash_generic.h +0 -1
  183. data/vendor/libgit2/src/hash/hash_openssl.h +0 -1
  184. data/vendor/libgit2/src/hash/hash_win32.c +14 -29
  185. data/vendor/libgit2/src/hash.h +0 -2
  186. data/vendor/libgit2/src/hashsig.c +97 -117
  187. data/vendor/libgit2/src/ident.c +125 -0
  188. data/vendor/libgit2/src/ignore.c +159 -110
  189. data/vendor/libgit2/src/ignore.h +13 -3
  190. data/vendor/libgit2/src/index.c +803 -445
  191. data/vendor/libgit2/src/index.h +43 -6
  192. data/vendor/libgit2/src/indexer.c +475 -157
  193. data/vendor/libgit2/src/iterator.c +198 -55
  194. data/vendor/libgit2/src/iterator.h +28 -4
  195. data/vendor/libgit2/src/map.h +1 -0
  196. data/vendor/libgit2/src/merge.c +849 -142
  197. data/vendor/libgit2/src/merge.h +11 -4
  198. data/vendor/libgit2/src/merge_file.c +183 -78
  199. data/vendor/libgit2/src/merge_file.h +0 -57
  200. data/vendor/libgit2/src/message.c +4 -28
  201. data/vendor/libgit2/src/mwindow.c +117 -8
  202. data/vendor/libgit2/src/mwindow.h +9 -1
  203. data/vendor/libgit2/src/netops.c +164 -59
  204. data/vendor/libgit2/src/netops.h +37 -1
  205. data/vendor/libgit2/src/notes.c +9 -18
  206. data/vendor/libgit2/src/notes.h +1 -1
  207. data/vendor/libgit2/src/object.c +78 -2
  208. data/vendor/libgit2/src/odb.c +191 -59
  209. data/vendor/libgit2/src/odb.h +2 -1
  210. data/vendor/libgit2/src/odb_loose.c +66 -51
  211. data/vendor/libgit2/src/odb_mempack.c +182 -0
  212. data/vendor/libgit2/src/odb_pack.c +151 -61
  213. data/vendor/libgit2/src/oid.c +30 -19
  214. data/vendor/libgit2/src/oid.h +13 -10
  215. data/vendor/libgit2/src/pack-objects.c +198 -147
  216. data/vendor/libgit2/src/pack-objects.h +7 -0
  217. data/vendor/libgit2/src/pack.c +272 -101
  218. data/vendor/libgit2/src/pack.h +15 -1
  219. data/vendor/libgit2/src/path.c +359 -117
  220. data/vendor/libgit2/src/path.h +110 -20
  221. data/vendor/libgit2/src/pathspec.c +583 -57
  222. data/vendor/libgit2/src/pathspec.h +36 -15
  223. data/vendor/libgit2/src/pool.c +4 -5
  224. data/vendor/libgit2/src/posix.c +45 -2
  225. data/vendor/libgit2/src/posix.h +13 -5
  226. data/vendor/libgit2/src/pqueue.c +73 -119
  227. data/vendor/libgit2/src/pqueue.h +35 -82
  228. data/vendor/libgit2/src/push.c +116 -48
  229. data/vendor/libgit2/src/push.h +5 -0
  230. data/vendor/libgit2/src/refdb.c +45 -6
  231. data/vendor/libgit2/src/refdb.h +13 -3
  232. data/vendor/libgit2/src/refdb_fs.c +1130 -551
  233. data/vendor/libgit2/src/reflog.c +36 -327
  234. data/vendor/libgit2/src/reflog.h +6 -1
  235. data/vendor/libgit2/src/refs.c +345 -142
  236. data/vendor/libgit2/src/refs.h +9 -2
  237. data/vendor/libgit2/src/refspec.c +90 -62
  238. data/vendor/libgit2/src/refspec.h +7 -24
  239. data/vendor/libgit2/src/remote.c +815 -415
  240. data/vendor/libgit2/src/remote.h +3 -4
  241. data/vendor/libgit2/src/repository.c +360 -207
  242. data/vendor/libgit2/src/repository.h +16 -10
  243. data/vendor/libgit2/src/reset.c +28 -13
  244. data/vendor/libgit2/src/revert.c +228 -0
  245. data/vendor/libgit2/src/revparse.c +29 -30
  246. data/vendor/libgit2/src/revwalk.c +141 -96
  247. data/vendor/libgit2/src/revwalk.h +6 -1
  248. data/vendor/libgit2/src/settings.c +140 -0
  249. data/vendor/libgit2/src/sha1_lookup.c +71 -0
  250. data/vendor/libgit2/src/sha1_lookup.h +5 -0
  251. data/vendor/libgit2/src/signature.c +38 -10
  252. data/vendor/libgit2/src/sortedcache.c +378 -0
  253. data/vendor/libgit2/src/sortedcache.h +178 -0
  254. data/vendor/libgit2/src/stash.c +98 -116
  255. data/vendor/libgit2/src/status.c +88 -60
  256. data/vendor/libgit2/src/status.h +2 -2
  257. data/vendor/libgit2/src/strmap.c +32 -0
  258. data/vendor/libgit2/src/strmap.h +14 -1
  259. data/vendor/libgit2/src/strnlen.h +23 -0
  260. data/vendor/libgit2/src/submodule.c +1073 -615
  261. data/vendor/libgit2/src/submodule.h +89 -21
  262. data/vendor/libgit2/src/sysdir.c +252 -0
  263. data/vendor/libgit2/src/sysdir.h +101 -0
  264. data/vendor/libgit2/src/tag.c +31 -20
  265. data/vendor/libgit2/src/thread-utils.h +98 -17
  266. data/vendor/libgit2/src/trace.h +0 -2
  267. data/vendor/libgit2/src/transport.c +76 -6
  268. data/vendor/libgit2/src/transports/cred.c +164 -61
  269. data/vendor/libgit2/src/transports/git.c +41 -48
  270. data/vendor/libgit2/src/transports/http.c +65 -109
  271. data/vendor/libgit2/src/transports/local.c +88 -65
  272. data/vendor/libgit2/src/transports/smart.c +91 -19
  273. data/vendor/libgit2/src/transports/smart.h +13 -5
  274. data/vendor/libgit2/src/transports/smart_pkt.c +24 -14
  275. data/vendor/libgit2/src/transports/smart_protocol.c +268 -113
  276. data/vendor/libgit2/src/transports/ssh.c +284 -186
  277. data/vendor/libgit2/src/transports/winhttp.c +279 -198
  278. data/vendor/libgit2/src/tree-cache.c +21 -23
  279. data/vendor/libgit2/src/tree-cache.h +1 -0
  280. data/vendor/libgit2/src/tree.c +109 -92
  281. data/vendor/libgit2/src/tree.h +2 -3
  282. data/vendor/libgit2/src/unix/map.c +7 -1
  283. data/vendor/libgit2/src/unix/posix.h +0 -1
  284. data/vendor/libgit2/src/userdiff.h +208 -0
  285. data/vendor/libgit2/src/util.c +16 -112
  286. data/vendor/libgit2/src/util.h +107 -3
  287. data/vendor/libgit2/src/vector.c +72 -17
  288. data/vendor/libgit2/src/vector.h +32 -5
  289. data/vendor/libgit2/src/win32/dir.c +15 -40
  290. data/vendor/libgit2/src/win32/dir.h +3 -2
  291. data/vendor/libgit2/src/win32/error.c +5 -31
  292. data/vendor/libgit2/src/win32/findfile.c +47 -66
  293. data/vendor/libgit2/src/win32/findfile.h +1 -12
  294. data/vendor/libgit2/src/win32/git2.rc +40 -0
  295. data/vendor/libgit2/src/win32/map.c +7 -1
  296. data/vendor/libgit2/src/win32/mingw-compat.h +3 -0
  297. data/vendor/libgit2/src/win32/posix.h +17 -11
  298. data/vendor/libgit2/src/win32/posix_w32.c +424 -292
  299. data/vendor/libgit2/src/win32/precompiled.h +6 -2
  300. data/vendor/libgit2/src/win32/pthread.c +141 -18
  301. data/vendor/libgit2/src/win32/pthread.h +50 -8
  302. data/vendor/libgit2/src/win32/reparse.h +57 -0
  303. data/vendor/libgit2/src/win32/utf-conv.c +117 -60
  304. data/vendor/libgit2/src/win32/utf-conv.h +81 -6
  305. data/vendor/libgit2/src/win32/version.h +21 -4
  306. data/vendor/libgit2/src/win32/w32_util.c +139 -0
  307. data/vendor/libgit2/src/win32/w32_util.h +54 -0
  308. data/vendor/libgit2/src/zstream.c +156 -0
  309. data/vendor/libgit2/src/zstream.h +39 -0
  310. metadata +84 -167
  311. data/Rakefile +0 -61
  312. data/ext/rugged/rugged_diff_patch.c +0 -169
  313. data/lib/rugged/diff/patch.rb +0 -28
  314. data/test/blob_test.rb +0 -341
  315. data/test/branch_test.rb +0 -199
  316. data/test/commit_test.rb +0 -104
  317. data/test/config_test.rb +0 -45
  318. data/test/coverage/cover.rb +0 -133
  319. data/test/diff_test.rb +0 -777
  320. data/test/errors_test.rb +0 -34
  321. data/test/fixtures/alternate/objects/14/6ae76773c91e3b1d00cf7a338ec55ae58297e2 +0 -0
  322. data/test/fixtures/alternate/objects/14/9c32d47e99d0a3572ff1e70a2e0051bbf347a9 +0 -0
  323. data/test/fixtures/alternate/objects/14/fb3108588f9421bf764041e5e3ac305eb6277f +0 -0
  324. data/test/fixtures/archive.tar.gz +0 -0
  325. data/test/fixtures/attr/attr0 +0 -1
  326. data/test/fixtures/attr/attr1 +0 -29
  327. data/test/fixtures/attr/attr2 +0 -21
  328. data/test/fixtures/attr/attr3 +0 -4
  329. data/test/fixtures/attr/binfile +0 -1
  330. data/test/fixtures/attr/dir/file +0 -0
  331. data/test/fixtures/attr/file +0 -1
  332. data/test/fixtures/attr/gitattributes +0 -29
  333. data/test/fixtures/attr/gitignore +0 -2
  334. data/test/fixtures/attr/ign +0 -1
  335. data/test/fixtures/attr/macro_bad +0 -1
  336. data/test/fixtures/attr/macro_test +0 -1
  337. data/test/fixtures/attr/root_test1 +0 -1
  338. data/test/fixtures/attr/root_test2 +0 -6
  339. data/test/fixtures/attr/root_test3 +0 -19
  340. data/test/fixtures/attr/root_test4.txt +0 -14
  341. data/test/fixtures/attr/sub/abc +0 -37
  342. data/test/fixtures/attr/sub/dir/file +0 -0
  343. data/test/fixtures/attr/sub/file +0 -1
  344. data/test/fixtures/attr/sub/ign/file +0 -1
  345. data/test/fixtures/attr/sub/ign/sub/file +0 -1
  346. data/test/fixtures/attr/sub/sub/dir +0 -0
  347. data/test/fixtures/attr/sub/sub/file +0 -1
  348. data/test/fixtures/attr/sub/sub/subsub.txt +0 -1
  349. data/test/fixtures/attr/sub/subdir_test1 +0 -2
  350. data/test/fixtures/attr/sub/subdir_test2.txt +0 -1
  351. data/test/fixtures/diff/another.txt +0 -38
  352. data/test/fixtures/diff/readme.txt +0 -36
  353. data/test/fixtures/mergedrepo/conflicts-one.txt +0 -5
  354. data/test/fixtures/mergedrepo/conflicts-two.txt +0 -5
  355. data/test/fixtures/mergedrepo/one.txt +0 -10
  356. data/test/fixtures/mergedrepo/two.txt +0 -12
  357. data/test/fixtures/status/current_file +0 -1
  358. data/test/fixtures/status/ignored_file +0 -1
  359. data/test/fixtures/status/modified_file +0 -2
  360. data/test/fixtures/status/new_file +0 -1
  361. data/test/fixtures/status/staged_changes +0 -2
  362. data/test/fixtures/status/staged_changes_modified_file +0 -3
  363. data/test/fixtures/status/staged_delete_modified_file +0 -1
  364. data/test/fixtures/status/staged_new_file +0 -1
  365. data/test/fixtures/status/staged_new_file_modified_file +0 -2
  366. data/test/fixtures/status/subdir/current_file +0 -1
  367. data/test/fixtures/status/subdir/modified_file +0 -2
  368. data/test/fixtures/status/subdir/new_file +0 -1
  369. data/test/fixtures/status/subdir.txt +0 -2
  370. data/test/fixtures/status//350/277/231 +0 -1
  371. data/test/fixtures/testrepo.git/HEAD +0 -1
  372. data/test/fixtures/testrepo.git/config +0 -13
  373. data/test/fixtures/testrepo.git/description +0 -1
  374. data/test/fixtures/testrepo.git/index +0 -0
  375. data/test/fixtures/testrepo.git/info/exclude +0 -6
  376. data/test/fixtures/testrepo.git/logs/HEAD +0 -3
  377. data/test/fixtures/testrepo.git/logs/refs/heads/master +0 -3
  378. data/test/fixtures/testrepo.git/logs/refs/notes/commits +0 -1
  379. data/test/fixtures/testrepo.git/objects/0c/37a5391bbff43c37f0d0371823a5509eed5b1d +0 -0
  380. data/test/fixtures/testrepo.git/objects/13/85f264afb75a56a5bec74243be9b367ba4ca08 +0 -0
  381. data/test/fixtures/testrepo.git/objects/18/1037049a54a1eb5fab404658a3a250b44335d7 +0 -0
  382. data/test/fixtures/testrepo.git/objects/18/10dff58d8a660512d4832e740f692884338ccd +0 -0
  383. data/test/fixtures/testrepo.git/objects/2d/2eff63372b08adf0a9eb84109ccf7d19e2f3a2 +0 -0
  384. data/test/fixtures/testrepo.git/objects/36/060c58702ed4c2a40832c51758d5344201d89a +0 -2
  385. data/test/fixtures/testrepo.git/objects/44/1034f860c1d5d90e4188d11ae0d325176869a8 +0 -1
  386. data/test/fixtures/testrepo.git/objects/45/b983be36b73c0788dc9cbcb76cbb80fc7bb057 +0 -0
  387. data/test/fixtures/testrepo.git/objects/4a/202b346bb0fb0db7eff3cffeb3c70babbd2045 +0 -2
  388. data/test/fixtures/testrepo.git/objects/5b/5b025afb0b4c913b4c338a42934a3863bf3644 +0 -2
  389. data/test/fixtures/testrepo.git/objects/60/d415052a33de2150bf68757f6461df4f563ae4 +0 -0
  390. data/test/fixtures/testrepo.git/objects/61/9f9935957e010c419cb9d15621916ddfcc0b96 +0 -0
  391. data/test/fixtures/testrepo.git/objects/68/8a8f4ef7496901d15322972f96e212a9e466cc +0 -1
  392. data/test/fixtures/testrepo.git/objects/75/057dd4114e74cca1d750d0aee1647c903cb60a +0 -0
  393. data/test/fixtures/testrepo.git/objects/77/71329dfa3002caf8c61a0ceb62a31d09023f37 +0 -0
  394. data/test/fixtures/testrepo.git/objects/81/4889a078c031f61ed08ab5fa863aea9314344d +0 -0
  395. data/test/fixtures/testrepo.git/objects/84/96071c1b46c854b31185ea97743be6a8774479 +0 -0
  396. data/test/fixtures/testrepo.git/objects/94/eca2de348d5f672faf56b0decafa5937e3235e +0 -0
  397. data/test/fixtures/testrepo.git/objects/9b/7384fe1676186192842f5d3e129457b62db9e3 +0 -0
  398. data/test/fixtures/testrepo.git/objects/9f/d738e8f7967c078dceed8190330fc8648ee56a +0 -3
  399. data/test/fixtures/testrepo.git/objects/a4/a7dce85cf63874e984719f4fdd239f5145052f +0 -2
  400. data/test/fixtures/testrepo.git/objects/a7/1586c1dfe8a71c6cbf6c129f404c5642ff31bd +0 -0
  401. data/test/fixtures/testrepo.git/objects/a8/233120f6ad708f843d861ce2b7228ec4e3dec6 +0 -0
  402. data/test/fixtures/testrepo.git/objects/b7/4713326bc972cc15751ed504dca6f6f3b91f7a +0 -3
  403. data/test/fixtures/testrepo.git/objects/be/3563ae3f795b2b4353bcce3a527ad0a4f7f644 +0 -3
  404. data/test/fixtures/testrepo.git/objects/c4/7800c7266a2be04c571c04d5a6614691ea99bd +0 -3
  405. data/test/fixtures/testrepo.git/objects/c4/dc1555e4d4fa0e0c9c3fc46734c7c35b3ce90b +0 -0
  406. data/test/fixtures/testrepo.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 +0 -0
  407. data/test/fixtures/testrepo.git/objects/f6/0079018b664e4e79329a7ef9559c8d9e0378d1 +0 -0
  408. data/test/fixtures/testrepo.git/objects/fa/49b077972391ad58037050f2a75f74e3671e92 +0 -0
  409. data/test/fixtures/testrepo.git/objects/fd/093bff70906175335656e6ce6ae05783708765 +0 -0
  410. data/test/fixtures/testrepo.git/objects/pack/pack-d7c6adf9f61318f041845b01440d09aa7a91e1b5.idx +0 -0
  411. data/test/fixtures/testrepo.git/objects/pack/pack-d7c6adf9f61318f041845b01440d09aa7a91e1b5.pack +0 -0
  412. data/test/fixtures/testrepo.git/packed-refs +0 -2
  413. data/test/fixtures/testrepo.git/refs/heads/master +0 -1
  414. data/test/fixtures/testrepo.git/refs/notes/commits +0 -1
  415. data/test/fixtures/testrepo.git/refs/tags/v0.9 +0 -1
  416. data/test/fixtures/testrepo.git/refs/tags/v1.0 +0 -1
  417. data/test/fixtures/text_file.md +0 -464
  418. data/test/fixtures/unsymlinked.git/HEAD +0 -1
  419. data/test/fixtures/unsymlinked.git/config +0 -6
  420. data/test/fixtures/unsymlinked.git/description +0 -1
  421. data/test/fixtures/unsymlinked.git/info/exclude +0 -2
  422. data/test/fixtures/unsymlinked.git/objects/08/8b64704e0d6b8bd061dea879418cb5442a3fbf +0 -0
  423. data/test/fixtures/unsymlinked.git/objects/13/a5e939bca25940c069fd2169d993dba328e30b +0 -0
  424. data/test/fixtures/unsymlinked.git/objects/19/bf568e59e3a0b363cafb4106226e62d4a4c41c +0 -0
  425. data/test/fixtures/unsymlinked.git/objects/58/1fadd35b4cf320d102a152f918729011604773 +0 -0
  426. data/test/fixtures/unsymlinked.git/objects/5c/87b6791e8b13da658a14d1ef7e09b5dc3bac8c +0 -0
  427. data/test/fixtures/unsymlinked.git/objects/6f/e5f5398af85fb3de8a6aba0339b6d3bfa26a27 +0 -0
  428. data/test/fixtures/unsymlinked.git/objects/7f/ccd75616ec188b8f1b23d67506a334cc34a49d +0 -0
  429. data/test/fixtures/unsymlinked.git/objects/80/6999882bf91d24241e4077906b9017605eb1f3 +0 -0
  430. data/test/fixtures/unsymlinked.git/objects/83/7d176303c5005505ec1e4a30231c40930c0230 +0 -0
  431. data/test/fixtures/unsymlinked.git/objects/a8/595ccca04f40818ae0155c8f9c77a230e597b6 +0 -2
  432. data/test/fixtures/unsymlinked.git/objects/cf/8f1cf5cce859c438d6cc067284cb5e161206e7 +0 -0
  433. data/test/fixtures/unsymlinked.git/objects/d5/278d05c8607ec420bfee4cf219fbc0eeebfd6a +0 -0
  434. data/test/fixtures/unsymlinked.git/objects/f4/e16fb76536591a41454194058d048d8e4dd2e9 +0 -0
  435. data/test/fixtures/unsymlinked.git/objects/f9/e65619d93fdf2673882e0a261c5e93b1a84006 +0 -0
  436. data/test/fixtures/unsymlinked.git/refs/heads/exe-file +0 -1
  437. data/test/fixtures/unsymlinked.git/refs/heads/master +0 -1
  438. data/test/fixtures/unsymlinked.git/refs/heads/reg-file +0 -1
  439. data/test/index_test.rb +0 -333
  440. data/test/lib_test.rb +0 -127
  441. data/test/note_test.rb +0 -158
  442. data/test/object_test.rb +0 -43
  443. data/test/reference_test.rb +0 -207
  444. data/test/remote_test.rb +0 -324
  445. data/test/repo_pack_test.rb +0 -24
  446. data/test/repo_reset_test.rb +0 -82
  447. data/test/repo_test.rb +0 -402
  448. data/test/tag_test.rb +0 -68
  449. data/test/test_helper.rb +0 -92
  450. data/test/tree_test.rb +0 -91
  451. data/test/walker_test.rb +0 -88
  452. data/vendor/libgit2/src/amiga/map.c +0 -48
  453. data/vendor/libgit2/src/compress.c +0 -53
@@ -15,12 +15,16 @@
15
15
  #include "refdb.h"
16
16
  #include "refdb_fs.h"
17
17
  #include "iterator.h"
18
+ #include "sortedcache.h"
19
+ #include "signature.h"
18
20
 
19
21
  #include <git2/tag.h>
20
22
  #include <git2/object.h>
21
23
  #include <git2/refdb.h>
24
+ #include <git2/branch.h>
22
25
  #include <git2/sys/refdb_backend.h>
23
26
  #include <git2/sys/refs.h>
27
+ #include <git2/sys/reflog.h>
24
28
 
25
29
  GIT__USE_STRMAP;
26
30
 
@@ -53,243 +57,151 @@ typedef struct refdb_fs_backend {
53
57
  git_repository *repo;
54
58
  char *path;
55
59
 
56
- git_refcache refcache;
60
+ git_sortedcache *refcache;
57
61
  int peeling_mode;
62
+ git_iterator_flag_t iterator_flags;
63
+ uint32_t direach_flags;
58
64
  } refdb_fs_backend;
59
65
 
60
- static int reference_read(
61
- git_buf *file_content,
62
- time_t *mtime,
63
- const char *repo_path,
64
- const char *ref_name,
65
- int *updated)
66
- {
67
- git_buf path = GIT_BUF_INIT;
68
- int result;
69
-
70
- assert(file_content && repo_path && ref_name);
71
-
72
- /* Determine the full path of the file */
73
- if (git_buf_joinpath(&path, repo_path, ref_name) < 0)
74
- return -1;
75
-
76
- result = git_futils_readbuffer_updated(file_content, path.ptr, mtime, NULL, updated);
77
- git_buf_free(&path);
78
-
79
- return result;
80
- }
81
-
82
- static int packed_parse_oid(
83
- struct packref **ref_out,
84
- const char **buffer_out,
85
- const char *buffer_end)
86
- {
87
- struct packref *ref = NULL;
88
-
89
- const char *buffer = *buffer_out;
90
- const char *refname_begin, *refname_end;
91
-
92
- size_t refname_len;
93
- git_oid id;
94
-
95
- refname_begin = (buffer + GIT_OID_HEXSZ + 1);
96
- if (refname_begin >= buffer_end || refname_begin[-1] != ' ')
97
- goto corrupt;
98
-
99
- /* Is this a valid object id? */
100
- if (git_oid_fromstr(&id, buffer) < 0)
101
- goto corrupt;
102
-
103
- refname_end = memchr(refname_begin, '\n', buffer_end - refname_begin);
104
- if (refname_end == NULL)
105
- refname_end = buffer_end;
106
-
107
- if (refname_end[-1] == '\r')
108
- refname_end--;
109
-
110
- refname_len = refname_end - refname_begin;
111
-
112
- ref = git__calloc(1, sizeof(struct packref) + refname_len + 1);
113
- GITERR_CHECK_ALLOC(ref);
114
-
115
- memcpy(ref->name, refname_begin, refname_len);
116
- ref->name[refname_len] = 0;
117
-
118
- git_oid_cpy(&ref->oid, &id);
119
-
120
- *ref_out = ref;
121
- *buffer_out = refname_end + 1;
122
- return 0;
123
-
124
- corrupt:
125
- git__free(ref);
126
- giterr_set(GITERR_REFERENCE, "The packed references file is corrupted");
127
- return -1;
128
- }
129
-
130
- static int packed_parse_peel(
131
- struct packref *tag_ref,
132
- const char **buffer_out,
133
- const char *buffer_end)
66
+ static int packref_cmp(const void *a_, const void *b_)
134
67
  {
135
- const char *buffer = *buffer_out + 1;
136
-
137
- assert(buffer[-1] == '^');
138
-
139
- /* Ensure it's not the first entry of the file */
140
- if (tag_ref == NULL)
141
- goto corrupt;
142
-
143
- if (buffer + GIT_OID_HEXSZ > buffer_end)
144
- goto corrupt;
145
-
146
- /* Is this a valid object id? */
147
- if (git_oid_fromstr(&tag_ref->peel, buffer) < 0)
148
- goto corrupt;
149
-
150
- buffer = buffer + GIT_OID_HEXSZ;
151
- if (*buffer == '\r')
152
- buffer++;
153
-
154
- if (buffer != buffer_end) {
155
- if (*buffer == '\n')
156
- buffer++;
157
- else
158
- goto corrupt;
159
- }
160
-
161
- tag_ref->flags |= PACKREF_HAS_PEEL;
162
- *buffer_out = buffer;
163
- return 0;
164
-
165
- corrupt:
166
- giterr_set(GITERR_REFERENCE, "The packed references file is corrupted");
167
- return -1;
68
+ const struct packref *a = a_, *b = b_;
69
+ return strcmp(a->name, b->name);
168
70
  }
169
71
 
170
- static int packed_load(refdb_fs_backend *backend)
72
+ static int packed_reload(refdb_fs_backend *backend)
171
73
  {
172
- int result, updated;
173
- git_buf packfile = GIT_BUF_INIT;
174
- const char *buffer_start, *buffer_end;
175
- git_refcache *ref_cache = &backend->refcache;
176
-
177
- /* First we make sure we have allocated the hash table */
178
- if (ref_cache->packfile == NULL) {
179
- ref_cache->packfile = git_strmap_alloc();
180
- GITERR_CHECK_ALLOC(ref_cache->packfile);
181
- }
74
+ int error;
75
+ git_buf packedrefs = GIT_BUF_INIT;
76
+ char *scan, *eof, *eol;
182
77
 
183
- if (backend->path == NULL)
78
+ if (!backend->path)
184
79
  return 0;
185
80
 
186
- result = reference_read(&packfile, &ref_cache->packfile_time,
187
- backend->path, GIT_PACKEDREFS_FILE, &updated);
81
+ error = git_sortedcache_lockandload(backend->refcache, &packedrefs);
188
82
 
189
83
  /*
190
- * If we couldn't find the file, we need to clear the table and
191
- * return. On any other error, we return that error. If everything
192
- * went fine and the file wasn't updated, then there's nothing new
193
- * for us here, so just return. Anything else means we need to
194
- * refresh the packed refs.
84
+ * If we can't find the packed-refs, clear table and return.
85
+ * Any other error just gets passed through.
86
+ * If no error, and file wasn't changed, just return.
87
+ * Anything else means we need to refresh the packed refs.
195
88
  */
196
- if (result == GIT_ENOTFOUND) {
197
- git_strmap_clear(ref_cache->packfile);
198
- return 0;
89
+ if (error <= 0) {
90
+ if (error == GIT_ENOTFOUND) {
91
+ git_sortedcache_clear(backend->refcache, true);
92
+ giterr_clear();
93
+ error = 0;
94
+ }
95
+ return error;
199
96
  }
200
97
 
201
- if (result < 0)
202
- return -1;
203
-
204
- if (!updated)
205
- return 0;
98
+ /* At this point, refresh the packed refs from the loaded buffer. */
206
99
 
207
- /*
208
- * At this point, we want to refresh the packed refs. We already
209
- * have the contents in our buffer.
210
- */
211
- git_strmap_clear(ref_cache->packfile);
100
+ git_sortedcache_clear(backend->refcache, false);
212
101
 
213
- buffer_start = (const char *)packfile.ptr;
214
- buffer_end = (const char *)(buffer_start) + packfile.size;
102
+ scan = (char *)packedrefs.ptr;
103
+ eof = scan + packedrefs.size;
215
104
 
216
105
  backend->peeling_mode = PEELING_NONE;
217
106
 
218
- if (buffer_start[0] == '#') {
107
+ if (*scan == '#') {
219
108
  static const char *traits_header = "# pack-refs with: ";
220
109
 
221
- if (git__prefixcmp(buffer_start, traits_header) == 0) {
222
- char *traits = (char *)buffer_start + strlen(traits_header);
223
- char *traits_end = strchr(traits, '\n');
110
+ if (git__prefixcmp(scan, traits_header) == 0) {
111
+ scan += strlen(traits_header);
112
+ eol = strchr(scan, '\n');
224
113
 
225
- if (traits_end == NULL)
114
+ if (!eol)
226
115
  goto parse_failed;
116
+ *eol = '\0';
227
117
 
228
- *traits_end = '\0';
229
-
230
- if (strstr(traits, " fully-peeled ") != NULL) {
118
+ if (strstr(scan, " fully-peeled ") != NULL) {
231
119
  backend->peeling_mode = PEELING_FULL;
232
- } else if (strstr(traits, " peeled ") != NULL) {
120
+ } else if (strstr(scan, " peeled ") != NULL) {
233
121
  backend->peeling_mode = PEELING_STANDARD;
234
122
  }
235
123
 
236
- buffer_start = traits_end + 1;
124
+ scan = eol + 1;
237
125
  }
238
126
  }
239
127
 
240
- while (buffer_start < buffer_end && buffer_start[0] == '#') {
241
- buffer_start = strchr(buffer_start, '\n');
242
- if (buffer_start == NULL)
128
+ while (scan < eof && *scan == '#') {
129
+ if (!(eol = strchr(scan, '\n')))
243
130
  goto parse_failed;
244
-
245
- buffer_start++;
131
+ scan = eol + 1;
246
132
  }
247
133
 
248
- while (buffer_start < buffer_end) {
249
- int err;
250
- struct packref *ref = NULL;
134
+ while (scan < eof) {
135
+ struct packref *ref;
136
+ git_oid oid;
137
+
138
+ /* parse "<OID> <refname>\n" */
251
139
 
252
- if (packed_parse_oid(&ref, &buffer_start, buffer_end) < 0)
140
+ if (git_oid_fromstr(&oid, scan) < 0)
253
141
  goto parse_failed;
142
+ scan += GIT_OID_HEXSZ;
254
143
 
255
- if (buffer_start[0] == '^') {
256
- if (packed_parse_peel(ref, &buffer_start, buffer_end) < 0)
257
- goto parse_failed;
258
- } else if (backend->peeling_mode == PEELING_FULL ||
259
- (backend->peeling_mode == PEELING_STANDARD &&
260
- git__prefixcmp(ref->name, GIT_REFS_TAGS_DIR) == 0)) {
261
- ref->flags |= PACKREF_CANNOT_PEEL;
262
- }
144
+ if (*scan++ != ' ')
145
+ goto parse_failed;
146
+ if (!(eol = strchr(scan, '\n')))
147
+ goto parse_failed;
148
+ *eol = '\0';
149
+ if (eol[-1] == '\r')
150
+ eol[-1] = '\0';
263
151
 
264
- git_strmap_insert(ref_cache->packfile, ref->name, ref, err);
265
- if (err < 0)
152
+ if (git_sortedcache_upsert((void **)&ref, backend->refcache, scan) < 0)
266
153
  goto parse_failed;
154
+ scan = eol + 1;
155
+
156
+ git_oid_cpy(&ref->oid, &oid);
157
+
158
+ /* look for optional "^<OID>\n" */
159
+
160
+ if (*scan == '^') {
161
+ if (git_oid_fromstr(&oid, scan + 1) < 0)
162
+ goto parse_failed;
163
+ scan += GIT_OID_HEXSZ + 1;
164
+
165
+ if (scan < eof) {
166
+ if (!(eol = strchr(scan, '\n')))
167
+ goto parse_failed;
168
+ scan = eol + 1;
169
+ }
170
+
171
+ git_oid_cpy(&ref->peel, &oid);
172
+ ref->flags |= PACKREF_HAS_PEEL;
173
+ }
174
+ else if (backend->peeling_mode == PEELING_FULL ||
175
+ (backend->peeling_mode == PEELING_STANDARD &&
176
+ git__prefixcmp(ref->name, GIT_REFS_TAGS_DIR) == 0))
177
+ ref->flags |= PACKREF_CANNOT_PEEL;
267
178
  }
268
179
 
269
- git_buf_free(&packfile);
180
+ git_sortedcache_wunlock(backend->refcache);
181
+ git_buf_free(&packedrefs);
182
+
270
183
  return 0;
271
184
 
272
185
  parse_failed:
273
- git_strmap_free(ref_cache->packfile);
274
- ref_cache->packfile = NULL;
275
- git_buf_free(&packfile);
186
+ giterr_set(GITERR_REFERENCE, "Corrupted packed references file");
187
+
188
+ git_sortedcache_clear(backend->refcache, false);
189
+ git_sortedcache_wunlock(backend->refcache);
190
+ git_buf_free(&packedrefs);
191
+
276
192
  return -1;
277
193
  }
278
194
 
279
- static int loose_parse_oid(git_oid *oid, const char *filename, git_buf *file_content)
195
+ static int loose_parse_oid(
196
+ git_oid *oid, const char *filename, git_buf *file_content)
280
197
  {
281
- size_t len;
282
- const char *str;
198
+ const char *str = git_buf_cstr(file_content);
283
199
 
284
- len = git_buf_len(file_content);
285
- if (len < GIT_OID_HEXSZ)
200
+ if (git_buf_len(file_content) < GIT_OID_HEXSZ)
286
201
  goto corrupted;
287
202
 
288
- /* str is guranteed to be zero-terminated */
289
- str = git_buf_cstr(file_content);
290
-
291
203
  /* we need to get 40 OID characters from the file */
292
- if (git_oid_fromstr(oid, git_buf_cstr(file_content)) < 0)
204
+ if (git_oid_fromstr(oid, str) < 0)
293
205
  goto corrupted;
294
206
 
295
207
  /* If the file is longer than 40 chars, the 41st must be a space */
@@ -302,68 +214,80 @@ corrupted:
302
214
  return -1;
303
215
  }
304
216
 
305
- static int loose_lookup_to_packfile(
306
- struct packref **ref_out,
307
- refdb_fs_backend *backend,
308
- const char *name)
217
+ static int loose_readbuffer(git_buf *buf, const char *base, const char *path)
218
+ {
219
+ int error;
220
+
221
+ /* build full path to file */
222
+ if ((error = git_buf_joinpath(buf, base, path)) < 0 ||
223
+ (error = git_futils_readbuffer(buf, buf->ptr)) < 0)
224
+ git_buf_free(buf);
225
+
226
+ return error;
227
+ }
228
+
229
+ static int loose_lookup_to_packfile(refdb_fs_backend *backend, const char *name)
309
230
  {
231
+ int error = 0;
310
232
  git_buf ref_file = GIT_BUF_INIT;
311
233
  struct packref *ref = NULL;
312
- size_t name_len;
234
+ git_oid oid;
313
235
 
314
- *ref_out = NULL;
236
+ /* if we fail to load the loose reference, assume someone changed
237
+ * the filesystem under us and skip it...
238
+ */
239
+ if (loose_readbuffer(&ref_file, backend->path, name) < 0) {
240
+ giterr_clear();
241
+ goto done;
242
+ }
315
243
 
316
- if (reference_read(&ref_file, NULL, backend->path, name, NULL) < 0)
317
- return -1;
244
+ /* skip symbolic refs */
245
+ if (!git__prefixcmp(git_buf_cstr(&ref_file), GIT_SYMREF))
246
+ goto done;
318
247
 
319
- git_buf_rtrim(&ref_file);
248
+ /* parse OID from file */
249
+ if ((error = loose_parse_oid(&oid, name, &ref_file)) < 0)
250
+ goto done;
320
251
 
321
- name_len = strlen(name);
322
- ref = git__calloc(1, sizeof(struct packref) + name_len + 1);
323
- GITERR_CHECK_ALLOC(ref);
252
+ git_sortedcache_wlock(backend->refcache);
324
253
 
325
- memcpy(ref->name, name, name_len);
326
- ref->name[name_len] = 0;
254
+ if (!(error = git_sortedcache_upsert(
255
+ (void **)&ref, backend->refcache, name))) {
327
256
 
328
- if (loose_parse_oid(&ref->oid, name, &ref_file) < 0) {
329
- git_buf_free(&ref_file);
330
- git__free(ref);
331
- return -1;
257
+ git_oid_cpy(&ref->oid, &oid);
258
+ ref->flags = PACKREF_WAS_LOOSE;
332
259
  }
333
260
 
334
- ref->flags = PACKREF_WAS_LOOSE;
261
+ git_sortedcache_wunlock(backend->refcache);
335
262
 
336
- *ref_out = ref;
263
+ done:
337
264
  git_buf_free(&ref_file);
338
- return 0;
265
+ return error;
339
266
  }
340
267
 
341
-
342
- static int _dirent_loose_load(void *data, git_buf *full_path)
268
+ static int _dirent_loose_load(void *payload, git_buf *full_path)
343
269
  {
344
- refdb_fs_backend *backend = (refdb_fs_backend *)data;
345
- void *old_ref = NULL;
346
- struct packref *ref;
270
+ refdb_fs_backend *backend = payload;
347
271
  const char *file_path;
348
- int err;
349
-
350
- if (git_path_isdir(full_path->ptr) == true)
351
- return git_path_direach(full_path, _dirent_loose_load, backend);
352
272
 
353
- file_path = full_path->ptr + strlen(backend->path);
273
+ if (git__suffixcmp(full_path->ptr, ".lock") == 0)
274
+ return 0;
354
275
 
355
- if (loose_lookup_to_packfile(&ref, backend, file_path) < 0)
356
- return -1;
276
+ if (git_path_isdir(full_path->ptr)) {
277
+ int error = git_path_direach(
278
+ full_path, backend->direach_flags, _dirent_loose_load, backend);
279
+ /* Race with the filesystem, ignore it */
280
+ if (error == GIT_ENOTFOUND) {
281
+ giterr_clear();
282
+ return 0;
283
+ }
357
284
 
358
- git_strmap_insert2(
359
- backend->refcache.packfile, ref->name, ref, old_ref, err);
360
- if (err < 0) {
361
- git__free(ref);
362
- return -1;
285
+ return error;
363
286
  }
364
287
 
365
- git__free(old_ref);
366
- return 0;
288
+ file_path = full_path->ptr + strlen(backend->path);
289
+
290
+ return loose_lookup_to_packfile(backend, file_path);
367
291
  }
368
292
 
369
293
  /*
@@ -374,11 +298,8 @@ static int _dirent_loose_load(void *data, git_buf *full_path)
374
298
  */
375
299
  static int packed_loadloose(refdb_fs_backend *backend)
376
300
  {
301
+ int error;
377
302
  git_buf refs_path = GIT_BUF_INIT;
378
- int result;
379
-
380
- /* the packfile must have been previously loaded! */
381
- assert(backend->refcache.packfile);
382
303
 
383
304
  if (git_buf_joinpath(&refs_path, backend->path, GIT_REFS_DIR) < 0)
384
305
  return -1;
@@ -388,10 +309,12 @@ static int packed_loadloose(refdb_fs_backend *backend)
388
309
  * This will overwrite any old packed entries with their
389
310
  * updated loose versions
390
311
  */
391
- result = git_path_direach(&refs_path, _dirent_loose_load, backend);
312
+ error = git_path_direach(
313
+ &refs_path, backend->direach_flags, _dirent_loose_load, backend);
314
+
392
315
  git_buf_free(&refs_path);
393
316
 
394
- return result;
317
+ return error;
395
318
  }
396
319
 
397
320
  static int refdb_fs_backend__exists(
@@ -399,23 +322,17 @@ static int refdb_fs_backend__exists(
399
322
  git_refdb_backend *_backend,
400
323
  const char *ref_name)
401
324
  {
402
- refdb_fs_backend *backend;
325
+ refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
403
326
  git_buf ref_path = GIT_BUF_INIT;
404
327
 
405
- assert(_backend);
406
- backend = (refdb_fs_backend *)_backend;
407
-
408
- if (packed_load(backend) < 0)
409
- return -1;
328
+ assert(backend);
410
329
 
411
- if (git_buf_joinpath(&ref_path, backend->path, ref_name) < 0)
330
+ if (packed_reload(backend) < 0 ||
331
+ git_buf_joinpath(&ref_path, backend->path, ref_name) < 0)
412
332
  return -1;
413
333
 
414
- if (git_path_isfile(ref_path.ptr) == true ||
415
- git_strmap_exists(backend->refcache.packfile, ref_path.ptr))
416
- *exists = 1;
417
- else
418
- *exists = 0;
334
+ *exists = git_path_isfile(ref_path.ptr) ||
335
+ (git_sortedcache_lookup(backend->refcache, ref_name) != NULL);
419
336
 
420
337
  git_buf_free(&ref_path);
421
338
  return 0;
@@ -447,64 +364,39 @@ static int loose_lookup(
447
364
  refdb_fs_backend *backend,
448
365
  const char *ref_name)
449
366
  {
450
- const char *target;
451
- git_oid oid;
452
367
  git_buf ref_file = GIT_BUF_INIT;
453
368
  int error = 0;
454
369
 
455
- error = reference_read(&ref_file, NULL, backend->path, ref_name, NULL);
370
+ if (out)
371
+ *out = NULL;
456
372
 
457
- if (error < 0)
458
- goto done;
373
+ if ((error = loose_readbuffer(&ref_file, backend->path, ref_name)) < 0)
374
+ /* cannot read loose ref file - gah */;
375
+ else if (git__prefixcmp(git_buf_cstr(&ref_file), GIT_SYMREF) == 0) {
376
+ const char *target;
459
377
 
460
- if (git__prefixcmp((const char *)(ref_file.ptr), GIT_SYMREF) == 0) {
461
378
  git_buf_rtrim(&ref_file);
462
379
 
463
- if ((target = loose_parse_symbolic(&ref_file)) == NULL) {
380
+ if (!(target = loose_parse_symbolic(&ref_file)))
464
381
  error = -1;
465
- goto done;
466
- }
467
-
468
- *out = git_reference__alloc_symbolic(ref_name, target);
382
+ else if (out != NULL)
383
+ *out = git_reference__alloc_symbolic(ref_name, target);
469
384
  } else {
470
- if ((error = loose_parse_oid(&oid, ref_name, &ref_file)) < 0)
471
- goto done;
385
+ git_oid oid;
472
386
 
473
- *out = git_reference__alloc(ref_name, &oid, NULL);
387
+ if (!(error = loose_parse_oid(&oid, ref_name, &ref_file)) &&
388
+ out != NULL)
389
+ *out = git_reference__alloc(ref_name, &oid, NULL);
474
390
  }
475
391
 
476
- if (*out == NULL)
477
- error = -1;
478
-
479
- done:
480
392
  git_buf_free(&ref_file);
481
393
  return error;
482
394
  }
483
395
 
484
- static int packed_map_entry(
485
- struct packref **entry,
486
- khiter_t *pos,
487
- refdb_fs_backend *backend,
488
- const char *ref_name)
396
+ static int ref_error_notfound(const char *name)
489
397
  {
490
- git_strmap *packfile_refs;
491
-
492
- if (packed_load(backend) < 0)
493
- return -1;
494
-
495
- /* Look up on the packfile */
496
- packfile_refs = backend->refcache.packfile;
497
-
498
- *pos = git_strmap_lookup_index(packfile_refs, ref_name);
499
-
500
- if (!git_strmap_valid_index(packfile_refs, *pos)) {
501
- giterr_set(GITERR_REFERENCE, "Reference '%s' not found", ref_name);
502
- return GIT_ENOTFOUND;
503
- }
504
-
505
- *entry = git_strmap_value_at(packfile_refs, *pos);
506
-
507
- return 0;
398
+ giterr_set(GITERR_REFERENCE, "Reference '%s' not found", name);
399
+ return GIT_ENOTFOUND;
508
400
  }
509
401
 
510
402
  static int packed_lookup(
@@ -512,18 +404,27 @@ static int packed_lookup(
512
404
  refdb_fs_backend *backend,
513
405
  const char *ref_name)
514
406
  {
515
- struct packref *entry;
516
- khiter_t pos;
517
407
  int error = 0;
408
+ struct packref *entry;
518
409
 
519
- if ((error = packed_map_entry(&entry, &pos, backend, ref_name)) < 0)
520
- return error;
410
+ if (packed_reload(backend) < 0)
411
+ return -1;
521
412
 
522
- if ((*out = git_reference__alloc(ref_name,
523
- &entry->oid, &entry->peel)) == NULL)
413
+ if (git_sortedcache_rlock(backend->refcache) < 0)
524
414
  return -1;
525
415
 
526
- return 0;
416
+ entry = git_sortedcache_lookup(backend->refcache, ref_name);
417
+ if (!entry) {
418
+ error = ref_error_notfound(ref_name);
419
+ } else {
420
+ *out = git_reference__alloc(ref_name, &entry->oid, &entry->peel);
421
+ if (!*out)
422
+ error = -1;
423
+ }
424
+
425
+ git_sortedcache_runlock(backend->refcache);
426
+
427
+ return error;
527
428
  }
528
429
 
529
430
  static int refdb_fs_backend__lookup(
@@ -531,73 +432,70 @@ static int refdb_fs_backend__lookup(
531
432
  git_refdb_backend *_backend,
532
433
  const char *ref_name)
533
434
  {
534
- refdb_fs_backend *backend;
535
- int result;
536
-
537
- assert(_backend);
435
+ refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
436
+ int error;
538
437
 
539
- backend = (refdb_fs_backend *)_backend;
438
+ assert(backend);
540
439
 
541
- if ((result = loose_lookup(out, backend, ref_name)) == 0)
440
+ if (!(error = loose_lookup(out, backend, ref_name)))
542
441
  return 0;
543
442
 
544
443
  /* only try to lookup this reference on the packfile if it
545
444
  * wasn't found on the loose refs; not if there was a critical error */
546
- if (result == GIT_ENOTFOUND) {
445
+ if (error == GIT_ENOTFOUND) {
547
446
  giterr_clear();
548
- result = packed_lookup(out, backend, ref_name);
447
+ error = packed_lookup(out, backend, ref_name);
549
448
  }
550
449
 
551
- return result;
450
+ return error;
552
451
  }
553
452
 
554
453
  typedef struct {
555
454
  git_reference_iterator parent;
556
455
 
557
456
  char *glob;
457
+
458
+ git_pool pool;
558
459
  git_vector loose;
559
- unsigned int loose_pos;
560
- khiter_t packed_pos;
460
+
461
+ git_sortedcache *cache;
462
+ size_t loose_pos;
463
+ size_t packed_pos;
561
464
  } refdb_fs_iter;
562
465
 
563
466
  static void refdb_fs_backend__iterator_free(git_reference_iterator *_iter)
564
467
  {
565
468
  refdb_fs_iter *iter = (refdb_fs_iter *) _iter;
566
- char *loose_path;
567
- size_t i;
568
-
569
- git_vector_foreach(&iter->loose, i, loose_path) {
570
- git__free(loose_path);
571
- }
572
469
 
573
470
  git_vector_free(&iter->loose);
574
-
575
- git__free(iter->glob);
471
+ git_pool_clear(&iter->pool);
472
+ git_sortedcache_free(iter->cache);
576
473
  git__free(iter);
577
474
  }
578
475
 
579
476
  static int iter_load_loose_paths(refdb_fs_backend *backend, refdb_fs_iter *iter)
580
477
  {
581
- git_strmap *packfile = backend->refcache.packfile;
478
+ int error = 0;
582
479
  git_buf path = GIT_BUF_INIT;
583
- git_iterator *fsit;
480
+ git_iterator *fsit = NULL;
584
481
  const git_index_entry *entry = NULL;
585
482
 
586
483
  if (!backend->path) /* do nothing if no path for loose refs */
587
484
  return 0;
588
485
 
589
- if (git_buf_printf(&path, "%s/refs", backend->path) < 0)
590
- return -1;
591
-
592
- if (git_iterator_for_filesystem(&fsit, git_buf_cstr(&path), 0, NULL, NULL) < 0)
593
- return -1;
486
+ if ((error = git_buf_printf(&path, "%s/refs", backend->path)) < 0 ||
487
+ (error = git_iterator_for_filesystem(
488
+ &fsit, path.ptr, backend->iterator_flags, NULL, NULL)) < 0) {
489
+ git_buf_free(&path);
490
+ return error;
491
+ }
594
492
 
595
- git_vector_init(&iter->loose, 8, NULL);
596
- git_buf_sets(&path, GIT_REFS_DIR);
493
+ error = git_buf_sets(&path, GIT_REFS_DIR);
597
494
 
598
- while (!git_iterator_advance(&entry, fsit)) {
495
+ while (!error && !git_iterator_advance(&entry, fsit)) {
599
496
  const char *ref_name;
600
- khiter_t pos;
497
+ struct packref *ref;
498
+ char *ref_dup;
601
499
 
602
500
  git_buf_truncate(&path, strlen(GIT_REFS_DIR));
603
501
  git_buf_puts(&path, entry->path);
@@ -607,27 +505,32 @@ static int iter_load_loose_paths(refdb_fs_backend *backend, refdb_fs_iter *iter)
607
505
  (iter->glob && p_fnmatch(iter->glob, ref_name, 0) != 0))
608
506
  continue;
609
507
 
610
- pos = git_strmap_lookup_index(packfile, ref_name);
611
- if (git_strmap_valid_index(packfile, pos)) {
612
- struct packref *ref = git_strmap_value_at(packfile, pos);
508
+ git_sortedcache_rlock(backend->refcache);
509
+ ref = git_sortedcache_lookup(backend->refcache, ref_name);
510
+ if (ref)
613
511
  ref->flags |= PACKREF_SHADOWED;
614
- }
512
+ git_sortedcache_runlock(backend->refcache);
615
513
 
616
- git_vector_insert(&iter->loose, git__strdup(ref_name));
514
+ ref_dup = git_pool_strdup(&iter->pool, ref_name);
515
+ if (!ref_dup)
516
+ error = -1;
517
+ else
518
+ error = git_vector_insert(&iter->loose, ref_dup);
617
519
  }
618
520
 
619
521
  git_iterator_free(fsit);
620
522
  git_buf_free(&path);
621
523
 
622
- return 0;
524
+ return error;
623
525
  }
624
526
 
625
527
  static int refdb_fs_backend__iterator_next(
626
528
  git_reference **out, git_reference_iterator *_iter)
627
529
  {
530
+ int error = GIT_ITEROVER;
628
531
  refdb_fs_iter *iter = (refdb_fs_iter *)_iter;
629
532
  refdb_fs_backend *backend = (refdb_fs_backend *)iter->parent.db->backend;
630
- git_strmap *packfile = backend->refcache.packfile;
533
+ struct packref *ref;
631
534
 
632
535
  while (iter->loose_pos < iter->loose.length) {
633
536
  const char *path = git_vector_get(&iter->loose, iter->loose_pos++);
@@ -638,99 +541,108 @@ static int refdb_fs_backend__iterator_next(
638
541
  giterr_clear();
639
542
  }
640
543
 
641
- while (iter->packed_pos < kh_end(packfile)) {
642
- struct packref *ref = NULL;
643
-
644
- while (!kh_exist(packfile, iter->packed_pos)) {
645
- iter->packed_pos++;
646
- if (iter->packed_pos == kh_end(packfile))
647
- return GIT_ITEROVER;
648
- }
544
+ if (!iter->cache) {
545
+ if ((error = git_sortedcache_copy(&iter->cache, backend->refcache, 1, NULL, NULL)) < 0)
546
+ return error;
547
+ }
649
548
 
650
- ref = kh_val(packfile, iter->packed_pos);
651
- iter->packed_pos++;
549
+ error = GIT_ITEROVER;
550
+ while (iter->packed_pos < git_sortedcache_entrycount(iter->cache)) {
551
+ ref = git_sortedcache_entry(iter->cache, iter->packed_pos++);
552
+ if (!ref) /* stop now if another thread deleted refs and we past end */
553
+ break;
652
554
 
653
555
  if (ref->flags & PACKREF_SHADOWED)
654
556
  continue;
655
-
656
557
  if (iter->glob && p_fnmatch(iter->glob, ref->name, 0) != 0)
657
558
  continue;
658
559
 
659
560
  *out = git_reference__alloc(ref->name, &ref->oid, &ref->peel);
660
- if (*out == NULL)
661
- return -1;
662
-
663
- return 0;
561
+ error = (*out != NULL) ? 0 : -1;
562
+ break;
664
563
  }
665
564
 
666
- return GIT_ITEROVER;
565
+ return error;
667
566
  }
668
567
 
669
568
  static int refdb_fs_backend__iterator_next_name(
670
569
  const char **out, git_reference_iterator *_iter)
671
570
  {
571
+ int error = GIT_ITEROVER;
672
572
  refdb_fs_iter *iter = (refdb_fs_iter *)_iter;
673
573
  refdb_fs_backend *backend = (refdb_fs_backend *)iter->parent.db->backend;
674
- git_strmap *packfile = backend->refcache.packfile;
574
+ struct packref *ref;
675
575
 
676
576
  while (iter->loose_pos < iter->loose.length) {
677
577
  const char *path = git_vector_get(&iter->loose, iter->loose_pos++);
678
578
 
679
- if (git_strmap_exists(packfile, path))
680
- continue;
579
+ if (loose_lookup(NULL, backend, path) == 0) {
580
+ *out = path;
581
+ return 0;
582
+ }
681
583
 
682
- *out = path;
683
- return 0;
584
+ giterr_clear();
684
585
  }
685
586
 
686
- while (iter->packed_pos < kh_end(packfile)) {
687
- while (!kh_exist(packfile, iter->packed_pos)) {
688
- iter->packed_pos++;
689
- if (iter->packed_pos == kh_end(packfile))
690
- return GIT_ITEROVER;
691
- }
587
+ if (!iter->cache) {
588
+ if ((error = git_sortedcache_copy(&iter->cache, backend->refcache, 1, NULL, NULL)) < 0)
589
+ return error;
590
+ }
692
591
 
693
- *out = kh_key(packfile, iter->packed_pos);
694
- iter->packed_pos++;
592
+ error = GIT_ITEROVER;
593
+ while (iter->packed_pos < git_sortedcache_entrycount(iter->cache)) {
594
+ ref = git_sortedcache_entry(iter->cache, iter->packed_pos++);
595
+ if (!ref) /* stop now if another thread deleted refs and we past end */
596
+ break;
695
597
 
696
- if (iter->glob && p_fnmatch(iter->glob, *out, 0) != 0)
598
+ if (ref->flags & PACKREF_SHADOWED)
599
+ continue;
600
+ if (iter->glob && p_fnmatch(iter->glob, ref->name, 0) != 0)
697
601
  continue;
698
602
 
699
- return 0;
603
+ *out = ref->name;
604
+ error = 0;
605
+ break;
700
606
  }
701
607
 
702
- return GIT_ITEROVER;
608
+ return error;
703
609
  }
704
610
 
705
611
  static int refdb_fs_backend__iterator(
706
612
  git_reference_iterator **out, git_refdb_backend *_backend, const char *glob)
707
613
  {
708
614
  refdb_fs_iter *iter;
709
- refdb_fs_backend *backend;
615
+ refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
710
616
 
711
- assert(_backend);
712
- backend = (refdb_fs_backend *)_backend;
617
+ assert(backend);
713
618
 
714
- if (packed_load(backend) < 0)
619
+ if (packed_reload(backend) < 0)
715
620
  return -1;
716
621
 
717
622
  iter = git__calloc(1, sizeof(refdb_fs_iter));
718
623
  GITERR_CHECK_ALLOC(iter);
719
624
 
720
- if (glob != NULL)
721
- iter->glob = git__strdup(glob);
625
+ if (git_pool_init(&iter->pool, 1, 0) < 0 ||
626
+ git_vector_init(&iter->loose, 8, NULL) < 0)
627
+ goto fail;
628
+
629
+ if (glob != NULL &&
630
+ (iter->glob = git_pool_strdup(&iter->pool, glob)) == NULL)
631
+ goto fail;
722
632
 
723
633
  iter->parent.next = refdb_fs_backend__iterator_next;
724
634
  iter->parent.next_name = refdb_fs_backend__iterator_next_name;
725
635
  iter->parent.free = refdb_fs_backend__iterator_free;
726
636
 
727
- if (iter_load_loose_paths(backend, iter) < 0) {
728
- refdb_fs_backend__iterator_free((git_reference_iterator *)iter);
729
- return -1;
730
- }
637
+ if (iter_load_loose_paths(backend, iter) < 0)
638
+ goto fail;
731
639
 
732
640
  *out = (git_reference_iterator *)iter;
733
641
  return 0;
642
+
643
+ fail:
644
+ refdb_fs_backend__iterator_free((git_reference_iterator *)iter);
645
+ return -1;
734
646
  }
735
647
 
736
648
  static bool ref_is_available(
@@ -756,80 +668,81 @@ static int reference_path_available(
756
668
  const char* old_ref,
757
669
  int force)
758
670
  {
759
- struct packref *this_ref;
671
+ size_t i;
760
672
 
761
- if (packed_load(backend) < 0)
673
+ if (packed_reload(backend) < 0)
762
674
  return -1;
763
675
 
764
676
  if (!force) {
765
677
  int exists;
766
678
 
767
- if (refdb_fs_backend__exists(&exists, (git_refdb_backend *)backend, new_ref) < 0)
679
+ if (refdb_fs_backend__exists(
680
+ &exists, (git_refdb_backend *)backend, new_ref) < 0)
768
681
  return -1;
769
682
 
770
683
  if (exists) {
771
684
  giterr_set(GITERR_REFERENCE,
772
685
  "Failed to write reference '%s': a reference with "
773
- " that name already exists.", new_ref);
686
+ "that name already exists.", new_ref);
774
687
  return GIT_EEXISTS;
775
688
  }
776
689
  }
777
690
 
778
- git_strmap_foreach_value(backend->refcache.packfile, this_ref, {
779
- if (!ref_is_available(old_ref, new_ref, this_ref->name)) {
691
+ git_sortedcache_rlock(backend->refcache);
692
+
693
+ for (i = 0; i < git_sortedcache_entrycount(backend->refcache); ++i) {
694
+ struct packref *ref = git_sortedcache_entry(backend->refcache, i);
695
+
696
+ if (ref && !ref_is_available(old_ref, new_ref, ref->name)) {
697
+ git_sortedcache_runlock(backend->refcache);
780
698
  giterr_set(GITERR_REFERENCE,
781
- "The path to reference '%s' collides with an existing one", new_ref);
699
+ "Path to reference '%s' collides with existing one", new_ref);
782
700
  return -1;
783
701
  }
784
- });
785
-
702
+ }
703
+
704
+ git_sortedcache_runlock(backend->refcache);
786
705
  return 0;
787
706
  }
788
707
 
789
- static int loose_write(refdb_fs_backend *backend, const git_reference *ref)
708
+ static int loose_lock(git_filebuf *file, refdb_fs_backend *backend, const char *name)
790
709
  {
791
- git_filebuf file = GIT_FILEBUF_INIT;
710
+ int error;
792
711
  git_buf ref_path = GIT_BUF_INIT;
793
712
 
713
+ assert(file && backend && name);
714
+
794
715
  /* Remove a possibly existing empty directory hierarchy
795
716
  * which name would collide with the reference name
796
717
  */
797
- if (git_futils_rmdir_r(ref->name, backend->path, GIT_RMDIR_SKIP_NONEMPTY) < 0)
718
+ if (git_futils_rmdir_r(name, backend->path, GIT_RMDIR_SKIP_NONEMPTY) < 0)
798
719
  return -1;
799
720
 
800
- if (git_buf_joinpath(&ref_path, backend->path, ref->name) < 0)
721
+ if (git_buf_joinpath(&ref_path, backend->path, name) < 0)
801
722
  return -1;
802
723
 
803
- if (git_filebuf_open(&file, ref_path.ptr, GIT_FILEBUF_FORCE) < 0) {
804
- git_buf_free(&ref_path);
805
- return -1;
806
- }
724
+ error = git_filebuf_open(file, ref_path.ptr, GIT_FILEBUF_FORCE, GIT_REFS_FILE_MODE);
807
725
 
808
726
  git_buf_free(&ref_path);
727
+ return error;
728
+ }
729
+
730
+ static int loose_commit(git_filebuf *file, const git_reference *ref)
731
+ {
732
+ assert(file && ref);
809
733
 
810
734
  if (ref->type == GIT_REF_OID) {
811
735
  char oid[GIT_OID_HEXSZ + 1];
736
+ git_oid_nfmt(oid, sizeof(oid), &ref->target.oid);
812
737
 
813
- git_oid_fmt(oid, &ref->target.oid);
814
- oid[GIT_OID_HEXSZ] = '\0';
815
-
816
- git_filebuf_printf(&file, "%s\n", oid);
817
-
738
+ git_filebuf_printf(file, "%s\n", oid);
818
739
  } else if (ref->type == GIT_REF_SYMBOLIC) {
819
- git_filebuf_printf(&file, GIT_SYMREF "%s\n", ref->target.symbolic);
740
+ git_filebuf_printf(file, GIT_SYMREF "%s\n", ref->target.symbolic);
820
741
  } else {
821
742
  assert(0); /* don't let this happen */
822
743
  }
823
744
 
824
- return git_filebuf_commit(&file, GIT_REFS_FILE_MODE);
825
- }
826
-
827
- static int packed_sort(const void *a, const void *b)
828
- {
829
- const struct packref *ref_a = (const struct packref *)a;
830
- const struct packref *ref_b = (const struct packref *)b;
831
-
832
- return strcmp(ref_a->name, ref_b->name);
745
+ return git_filebuf_commit(file);
833
746
  }
834
747
 
835
748
  /*
@@ -884,9 +797,7 @@ static int packed_find_peel(refdb_fs_backend *backend, struct packref *ref)
884
797
  static int packed_write_ref(struct packref *ref, git_filebuf *file)
885
798
  {
886
799
  char oid[GIT_OID_HEXSZ + 1];
887
-
888
- git_oid_fmt(oid, &ref->oid);
889
- oid[GIT_OID_HEXSZ] = 0;
800
+ git_oid_nfmt(oid, sizeof(oid), &ref->oid);
890
801
 
891
802
  /*
892
803
  * For references that peel to an object in the repo, we must
@@ -900,8 +811,7 @@ static int packed_write_ref(struct packref *ref, git_filebuf *file)
900
811
  */
901
812
  if (ref->flags & PACKREF_HAS_PEEL) {
902
813
  char peel[GIT_OID_HEXSZ + 1];
903
- git_oid_fmt(peel, &ref->peel);
904
- peel[GIT_OID_HEXSZ] = 0;
814
+ git_oid_nfmt(peel, sizeof(peel), &ref->peel);
905
815
 
906
816
  if (git_filebuf_printf(file, "%s %s\n^%s\n", oid, ref->name, peel) < 0)
907
817
  return -1;
@@ -924,31 +834,30 @@ static int packed_write_ref(struct packref *ref, git_filebuf *file)
924
834
  * is well-written, because we are destructing references
925
835
  * here otherwise.
926
836
  */
927
- static int packed_remove_loose(
928
- refdb_fs_backend *backend,
929
- git_vector *packing_list)
837
+ static int packed_remove_loose(refdb_fs_backend *backend)
930
838
  {
931
839
  size_t i;
932
840
  git_buf full_path = GIT_BUF_INIT;
933
841
  int failed = 0;
934
842
 
935
- for (i = 0; i < packing_list->length; ++i) {
936
- struct packref *ref = git_vector_get(packing_list, i);
843
+ /* backend->refcache is already locked when this is called */
844
+
845
+ for (i = 0; i < git_sortedcache_entrycount(backend->refcache); ++i) {
846
+ struct packref *ref = git_sortedcache_entry(backend->refcache, i);
937
847
 
938
- if ((ref->flags & PACKREF_WAS_LOOSE) == 0)
848
+ if (!ref || !(ref->flags & PACKREF_WAS_LOOSE))
939
849
  continue;
940
850
 
941
851
  if (git_buf_joinpath(&full_path, backend->path, ref->name) < 0)
942
852
  return -1; /* critical; do not try to recover on oom */
943
853
 
944
- if (git_path_exists(full_path.ptr) == true && p_unlink(full_path.ptr) < 0) {
854
+ if (git_path_exists(full_path.ptr) && p_unlink(full_path.ptr) < 0) {
945
855
  if (failed)
946
856
  continue;
947
857
 
948
858
  giterr_set(GITERR_REFERENCE,
949
859
  "Failed to remove loose reference '%s' after packing: %s",
950
860
  full_path.ptr, strerror(errno));
951
-
952
861
  failed = 1;
953
862
  }
954
863
 
@@ -969,206 +878,396 @@ static int packed_remove_loose(
969
878
  */
970
879
  static int packed_write(refdb_fs_backend *backend)
971
880
  {
881
+ git_sortedcache *refcache = backend->refcache;
972
882
  git_filebuf pack_file = GIT_FILEBUF_INIT;
973
883
  size_t i;
974
- git_buf pack_file_path = GIT_BUF_INIT;
975
- git_vector packing_list;
976
- unsigned int total_refs;
977
-
978
- assert(backend && backend->refcache.packfile);
979
884
 
980
- total_refs =
981
- (unsigned int)git_strmap_num_entries(backend->refcache.packfile);
982
-
983
- if (git_vector_init(&packing_list, total_refs, packed_sort) < 0)
885
+ /* lock the cache to updates while we do this */
886
+ if (git_sortedcache_wlock(refcache) < 0)
984
887
  return -1;
985
888
 
986
- /* Load all the packfile into a vector */
987
- {
988
- struct packref *reference;
989
-
990
- /* cannot fail: vector already has the right size */
991
- git_strmap_foreach_value(backend->refcache.packfile, reference, {
992
- git_vector_insert(&packing_list, reference);
993
- });
994
- }
995
-
996
- /* sort the vector so the entries appear sorted on the packfile */
997
- git_vector_sort(&packing_list);
998
-
999
- /* Now we can open the file! */
1000
- if (git_buf_joinpath(&pack_file_path,
1001
- backend->path, GIT_PACKEDREFS_FILE) < 0)
1002
- goto cleanup_memory;
1003
-
1004
- if (git_filebuf_open(&pack_file, pack_file_path.ptr, 0) < 0)
1005
- goto cleanup_packfile;
889
+ /* Open the file! */
890
+ if (git_filebuf_open(&pack_file, git_sortedcache_path(refcache), 0, GIT_PACKEDREFS_FILE_MODE) < 0)
891
+ goto fail;
1006
892
 
1007
893
  /* Packfiles have a header... apparently
1008
894
  * This is in fact not required, but we might as well print it
1009
895
  * just for kicks */
1010
896
  if (git_filebuf_printf(&pack_file, "%s\n", GIT_PACKEDREFS_HEADER) < 0)
1011
- goto cleanup_packfile;
897
+ goto fail;
1012
898
 
1013
- for (i = 0; i < packing_list.length; ++i) {
1014
- struct packref *ref = (struct packref *)git_vector_get(&packing_list, i);
899
+ for (i = 0; i < git_sortedcache_entrycount(refcache); ++i) {
900
+ struct packref *ref = git_sortedcache_entry(refcache, i);
1015
901
 
1016
902
  if (packed_find_peel(backend, ref) < 0)
1017
- goto cleanup_packfile;
903
+ goto fail;
1018
904
 
1019
905
  if (packed_write_ref(ref, &pack_file) < 0)
1020
- goto cleanup_packfile;
906
+ goto fail;
1021
907
  }
1022
908
 
1023
909
  /* if we've written all the references properly, we can commit
1024
910
  * the packfile to make the changes effective */
1025
- if (git_filebuf_commit(&pack_file, GIT_PACKEDREFS_FILE_MODE) < 0)
1026
- goto cleanup_memory;
911
+ if (git_filebuf_commit(&pack_file) < 0)
912
+ goto fail;
1027
913
 
1028
914
  /* when and only when the packfile has been properly written,
1029
915
  * we can go ahead and remove the loose refs */
1030
- if (packed_remove_loose(backend, &packing_list) < 0)
1031
- goto cleanup_memory;
916
+ if (packed_remove_loose(backend) < 0)
917
+ goto fail;
1032
918
 
1033
- {
1034
- struct stat st;
1035
- if (p_stat(pack_file_path.ptr, &st) == 0)
1036
- backend->refcache.packfile_time = st.st_mtime;
1037
- }
1038
-
1039
- git_vector_free(&packing_list);
1040
- git_buf_free(&pack_file_path);
919
+ git_sortedcache_updated(refcache);
920
+ git_sortedcache_wunlock(refcache);
1041
921
 
1042
922
  /* we're good now */
1043
923
  return 0;
1044
924
 
1045
- cleanup_packfile:
925
+ fail:
1046
926
  git_filebuf_cleanup(&pack_file);
1047
-
1048
- cleanup_memory:
1049
- git_vector_free(&packing_list);
1050
- git_buf_free(&pack_file_path);
927
+ git_sortedcache_wunlock(refcache);
1051
928
 
1052
929
  return -1;
1053
930
  }
1054
931
 
1055
- static int refdb_fs_backend__write(
1056
- git_refdb_backend *_backend,
1057
- const git_reference *ref,
1058
- int force)
1059
- {
1060
- refdb_fs_backend *backend;
1061
- int error;
932
+ static int reflog_append(refdb_fs_backend *backend, const git_reference *ref, const git_oid *old, const git_oid *new, const git_signature *author, const char *message);
933
+ static int has_reflog(git_repository *repo, const char *name);
1062
934
 
1063
- assert(_backend);
1064
- backend = (refdb_fs_backend *)_backend;
935
+ /* We only write if it's under heads/, remotes/ or notes/ or if it already has a log */
936
+ static int should_write_reflog(int *write, git_repository *repo, const char *name)
937
+ {
938
+ int error, logall;
1065
939
 
1066
- error = reference_path_available(backend, ref->name, NULL, force);
940
+ error = git_repository__cvar(&logall, repo, GIT_CVAR_LOGALLREFUPDATES);
1067
941
  if (error < 0)
1068
942
  return error;
1069
943
 
1070
- return loose_write(backend, ref);
944
+ /* Defaults to the opposite of the repo being bare */
945
+ if (logall == GIT_LOGALLREFUPDATES_UNSET)
946
+ logall = !git_repository_is_bare(repo);
947
+
948
+ if (!logall) {
949
+ *write = 0;
950
+ } else if (has_reflog(repo, name)) {
951
+ *write = 1;
952
+ } else if (!git__prefixcmp(name, GIT_REFS_HEADS_DIR) ||
953
+ !git__strcmp(name, GIT_HEAD_FILE) ||
954
+ !git__prefixcmp(name, GIT_REFS_REMOTES_DIR) ||
955
+ !git__prefixcmp(name, GIT_REFS_NOTES_DIR)) {
956
+ *write = 1;
957
+ } else {
958
+ *write = 0;
959
+ }
960
+
961
+ return 0;
1071
962
  }
1072
963
 
1073
- static int refdb_fs_backend__delete(
1074
- git_refdb_backend *_backend,
1075
- const char *ref_name)
964
+ static int cmp_old_ref(int *cmp, git_refdb_backend *backend, const char *name,
965
+ const git_oid *old_id, const char *old_target)
1076
966
  {
1077
- refdb_fs_backend *backend;
1078
- git_buf loose_path = GIT_BUF_INIT;
1079
- struct packref *pack_ref;
1080
- khiter_t pack_ref_pos;
1081
967
  int error = 0;
1082
- bool loose_deleted = 0;
1083
-
1084
- assert(_backend);
1085
- assert(ref_name);
968
+ git_reference *old_ref = NULL;
1086
969
 
1087
- backend = (refdb_fs_backend *)_backend;
970
+ *cmp = 0;
971
+ /* It "matches" if there is no old value to compare against */
972
+ if (!old_id && !old_target)
973
+ return 0;
1088
974
 
1089
- /* If a loose reference exists, remove it from the filesystem */
1090
- if (git_buf_joinpath(&loose_path, backend->path, ref_name) < 0)
1091
- return -1;
975
+ if ((error = refdb_fs_backend__lookup(&old_ref, backend, name)) < 0)
976
+ goto out;
1092
977
 
1093
- if (git_path_isfile(loose_path.ptr)) {
1094
- error = p_unlink(loose_path.ptr);
1095
- loose_deleted = 1;
978
+ /* If the types don't match, there's no way the values do */
979
+ if (old_id && old_ref->type != GIT_REF_OID) {
980
+ *cmp = -1;
981
+ goto out;
982
+ }
983
+ if (old_target && old_ref->type != GIT_REF_SYMBOLIC) {
984
+ *cmp = 1;
985
+ goto out;
1096
986
  }
1097
987
 
1098
- git_buf_free(&loose_path);
988
+ if (old_id && old_ref->type == GIT_REF_OID)
989
+ *cmp = git_oid_cmp(old_id, &old_ref->target.oid);
1099
990
 
1100
- if (error != 0)
1101
- return error;
991
+ if (old_target && old_ref->type == GIT_REF_SYMBOLIC)
992
+ *cmp = git__strcmp(old_target, old_ref->target.symbolic);
1102
993
 
1103
- /* If a packed reference exists, remove it from the packfile and repack */
1104
- error = packed_map_entry(&pack_ref, &pack_ref_pos, backend, ref_name);
994
+ out:
995
+ git_reference_free(old_ref);
1105
996
 
1106
- if (error == GIT_ENOTFOUND)
1107
- return loose_deleted ? 0 : GIT_ENOTFOUND;
997
+ return error;
998
+ }
999
+
1000
+ /*
1001
+ * The git.git comment regarding this, for your viewing pleasure:
1002
+ *
1003
+ * Special hack: If a branch is updated directly and HEAD
1004
+ * points to it (may happen on the remote side of a push
1005
+ * for example) then logically the HEAD reflog should be
1006
+ * updated too.
1007
+ * A generic solution implies reverse symref information,
1008
+ * but finding all symrefs pointing to the given branch
1009
+ * would be rather costly for this rare event (the direct
1010
+ * update of a branch) to be worth it. So let's cheat and
1011
+ * check with HEAD only which should cover 99% of all usage
1012
+ * scenarios (even 100% of the default ones).
1013
+ */
1014
+ static int maybe_append_head(refdb_fs_backend *backend, const git_reference *ref, const git_signature *who, const char *message)
1015
+ {
1016
+ int error;
1017
+ git_oid old_id = {{0}};
1018
+ git_reference *tmp = NULL, *head = NULL, *peeled = NULL;
1019
+ const char *name;
1020
+
1021
+ if (ref->type == GIT_REF_SYMBOLIC)
1022
+ return 0;
1023
+
1024
+ /* if we can't resolve, we use {0}*40 as old id */
1025
+ git_reference_name_to_id(&old_id, backend->repo, ref->name);
1108
1026
 
1109
- if (error == 0) {
1110
- git_strmap_delete_at(backend->refcache.packfile, pack_ref_pos);
1111
- git__free(pack_ref);
1112
- error = packed_write(backend);
1027
+ if ((error = git_reference_lookup(&head, backend->repo, GIT_HEAD_FILE)) < 0)
1028
+ return error;
1029
+
1030
+ if (git_reference_type(head) == GIT_REF_OID)
1031
+ goto cleanup;
1032
+
1033
+ if ((error = git_reference_lookup(&tmp, backend->repo, GIT_HEAD_FILE)) < 0)
1034
+ goto cleanup;
1035
+
1036
+ /* Go down the symref chain until we find the branch */
1037
+ while (git_reference_type(tmp) == GIT_REF_SYMBOLIC) {
1038
+ error = git_reference_lookup(&peeled, backend->repo, git_reference_symbolic_target(tmp));
1039
+ if (error < 0)
1040
+ break;
1041
+
1042
+ git_reference_free(tmp);
1043
+ tmp = peeled;
1113
1044
  }
1114
1045
 
1046
+ if (error == GIT_ENOTFOUND) {
1047
+ error = 0;
1048
+ name = git_reference_symbolic_target(tmp);
1049
+ } else if (error < 0) {
1050
+ goto cleanup;
1051
+ } else {
1052
+ name = git_reference_name(tmp);
1053
+ }
1054
+
1055
+ if (strcmp(name, ref->name))
1056
+ goto cleanup;
1057
+
1058
+ error = reflog_append(backend, head, &old_id, git_reference_target(ref), who, message);
1059
+
1060
+ cleanup:
1061
+ git_reference_free(tmp);
1062
+ git_reference_free(head);
1115
1063
  return error;
1116
1064
  }
1117
1065
 
1066
+
1067
+ static int refdb_fs_backend__write(
1068
+ git_refdb_backend *_backend,
1069
+ const git_reference *ref,
1070
+ int force,
1071
+ const git_signature *who,
1072
+ const char *message,
1073
+ const git_oid *old_id,
1074
+ const char *old_target)
1075
+ {
1076
+ refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
1077
+ git_filebuf file = GIT_FILEBUF_INIT;
1078
+ int error = 0, cmp = 0, should_write;
1079
+ const char *new_target = NULL;
1080
+ const git_oid *new_id = NULL;
1081
+
1082
+ assert(backend);
1083
+
1084
+ error = reference_path_available(backend, ref->name, NULL, force);
1085
+ if (error < 0)
1086
+ return error;
1087
+
1088
+ /* We need to perform the reflog append and old value check under the ref's lock */
1089
+ if ((error = loose_lock(&file, backend, ref->name)) < 0)
1090
+ return error;
1091
+
1092
+ if ((error = cmp_old_ref(&cmp, _backend, ref->name, old_id, old_target)) < 0)
1093
+ goto on_error;
1094
+
1095
+ if (cmp) {
1096
+ giterr_set(GITERR_REFERENCE, "old reference value does not match");
1097
+ error = GIT_EMODIFIED;
1098
+ goto on_error;
1099
+ }
1100
+
1101
+ if (ref->type == GIT_REF_SYMBOLIC)
1102
+ new_target = ref->target.symbolic;
1103
+ else
1104
+ new_id = &ref->target.oid;
1105
+
1106
+ error = cmp_old_ref(&cmp, _backend, ref->name, new_id, new_target);
1107
+ if (error < 0 && error != GIT_ENOTFOUND)
1108
+ goto on_error;
1109
+
1110
+ /* Don't update if we have the same value */
1111
+ if (!error && !cmp) {
1112
+ error = 0;
1113
+ goto on_error; /* not really error */
1114
+ }
1115
+
1116
+ if ((error = should_write_reflog(&should_write, backend->repo, ref->name)) < 0)
1117
+ goto on_error;
1118
+
1119
+ if (should_write) {
1120
+ if ((error = reflog_append(backend, ref, NULL, NULL, who, message)) < 0)
1121
+ goto on_error;
1122
+ if ((error = maybe_append_head(backend, ref, who, message)) < 0)
1123
+ goto on_error;
1124
+ }
1125
+
1126
+ return loose_commit(&file, ref);
1127
+
1128
+ on_error:
1129
+ git_filebuf_cleanup(&file);
1130
+ return error;
1131
+ }
1132
+
1133
+ static int refdb_fs_backend__delete(
1134
+ git_refdb_backend *_backend,
1135
+ const char *ref_name,
1136
+ const git_oid *old_id, const char *old_target)
1137
+ {
1138
+ refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
1139
+ git_buf loose_path = GIT_BUF_INIT;
1140
+ size_t pack_pos;
1141
+ git_filebuf file = GIT_FILEBUF_INIT;
1142
+ int error = 0, cmp = 0;
1143
+ bool loose_deleted = 0;
1144
+
1145
+ assert(backend && ref_name);
1146
+
1147
+ if ((error = loose_lock(&file, backend, ref_name)) < 0)
1148
+ return error;
1149
+
1150
+ error = cmp_old_ref(&cmp, _backend, ref_name, old_id, old_target);
1151
+ if (error < 0)
1152
+ goto cleanup;
1153
+
1154
+ if (cmp) {
1155
+ giterr_set(GITERR_REFERENCE, "old reference value does not match");
1156
+ error = GIT_EMODIFIED;
1157
+ goto cleanup;
1158
+ }
1159
+
1160
+ /* If a loose reference exists, remove it from the filesystem */
1161
+ if (git_buf_joinpath(&loose_path, backend->path, ref_name) < 0)
1162
+ return -1;
1163
+
1164
+ if (git_path_isfile(loose_path.ptr)) {
1165
+ error = p_unlink(loose_path.ptr);
1166
+ loose_deleted = 1;
1167
+ }
1168
+
1169
+ git_buf_free(&loose_path);
1170
+
1171
+ if (error != 0)
1172
+ goto cleanup;
1173
+
1174
+ if ((error = packed_reload(backend)) < 0)
1175
+ goto cleanup;
1176
+
1177
+ /* If a packed reference exists, remove it from the packfile and repack */
1178
+ if ((error = git_sortedcache_wlock(backend->refcache)) < 0)
1179
+ goto cleanup;
1180
+
1181
+ if (!(error = git_sortedcache_lookup_index(
1182
+ &pack_pos, backend->refcache, ref_name)))
1183
+ error = git_sortedcache_remove(backend->refcache, pack_pos);
1184
+
1185
+ git_sortedcache_wunlock(backend->refcache);
1186
+
1187
+ if (error == GIT_ENOTFOUND) {
1188
+ error = loose_deleted ? 0 : ref_error_notfound(ref_name);
1189
+ goto cleanup;
1190
+ }
1191
+
1192
+ error = packed_write(backend);
1193
+
1194
+ cleanup:
1195
+ git_filebuf_cleanup(&file);
1196
+
1197
+ return error;
1198
+ }
1199
+
1200
+ static int refdb_reflog_fs__rename(git_refdb_backend *_backend, const char *old_name, const char *new_name);
1201
+
1118
1202
  static int refdb_fs_backend__rename(
1119
1203
  git_reference **out,
1120
1204
  git_refdb_backend *_backend,
1121
1205
  const char *old_name,
1122
1206
  const char *new_name,
1123
- int force)
1207
+ int force,
1208
+ const git_signature *who,
1209
+ const char *message)
1124
1210
  {
1125
- refdb_fs_backend *backend;
1211
+ refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
1126
1212
  git_reference *old, *new;
1213
+ git_filebuf file = GIT_FILEBUF_INIT;
1127
1214
  int error;
1128
1215
 
1129
- assert(_backend);
1130
- backend = (refdb_fs_backend *)_backend;
1216
+ assert(backend);
1131
1217
 
1132
- error = reference_path_available(backend, new_name, old_name, force);
1133
- if (error < 0)
1218
+ if ((error = reference_path_available(
1219
+ backend, new_name, old_name, force)) < 0 ||
1220
+ (error = refdb_fs_backend__lookup(&old, _backend, old_name)) < 0)
1134
1221
  return error;
1135
1222
 
1136
- error = refdb_fs_backend__lookup(&old, _backend, old_name);
1137
- if (error < 0)
1223
+ if ((error = refdb_fs_backend__delete(_backend, old_name, NULL, NULL)) < 0) {
1224
+ git_reference_free(old);
1138
1225
  return error;
1226
+ }
1139
1227
 
1140
- error = refdb_fs_backend__delete(_backend, old_name);
1141
- if (error < 0) {
1228
+ new = git_reference__set_name(old, new_name);
1229
+ if (!new) {
1142
1230
  git_reference_free(old);
1231
+ return -1;
1232
+ }
1233
+
1234
+ if ((error = loose_lock(&file, backend, new->name)) < 0) {
1235
+ git_reference_free(new);
1143
1236
  return error;
1144
1237
  }
1145
1238
 
1146
- new = realloc(old, sizeof(git_reference) + strlen(new_name) + 1);
1147
- memcpy(new->name, new_name, strlen(new_name) + 1);
1239
+ /* Try to rename the refog; it's ok if the old doesn't exist */
1240
+ error = refdb_reflog_fs__rename(_backend, old_name, new_name);
1241
+ if (((error == 0) || (error == GIT_ENOTFOUND)) &&
1242
+ ((error = reflog_append(backend, new, NULL, NULL, who, message)) < 0)) {
1243
+ git_reference_free(new);
1244
+ git_filebuf_cleanup(&file);
1245
+ return error;
1246
+ }
1148
1247
 
1149
- error = loose_write(backend, new);
1150
1248
  if (error < 0) {
1151
1249
  git_reference_free(new);
1250
+ git_filebuf_cleanup(&file);
1152
1251
  return error;
1153
1252
  }
1154
1253
 
1155
- if (out) {
1156
- *out = new;
1157
- } else {
1254
+
1255
+ if ((error = loose_commit(&file, new)) < 0 || out == NULL) {
1158
1256
  git_reference_free(new);
1257
+ return error;
1159
1258
  }
1160
1259
 
1260
+ *out = new;
1161
1261
  return 0;
1162
1262
  }
1163
1263
 
1164
1264
  static int refdb_fs_backend__compress(git_refdb_backend *_backend)
1165
1265
  {
1166
- refdb_fs_backend *backend;
1266
+ refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
1167
1267
 
1168
- assert(_backend);
1169
- backend = (refdb_fs_backend *)_backend;
1268
+ assert(backend);
1170
1269
 
1171
- if (packed_load(backend) < 0 || /* load the existing packfile */
1270
+ if (packed_reload(backend) < 0 || /* load the existing packfile */
1172
1271
  packed_loadloose(backend) < 0 || /* add all the loose refs */
1173
1272
  packed_write(backend) < 0) /* write back to disk */
1174
1273
  return -1;
@@ -1176,29 +1275,13 @@ static int refdb_fs_backend__compress(git_refdb_backend *_backend)
1176
1275
  return 0;
1177
1276
  }
1178
1277
 
1179
- static void refcache_free(git_refcache *refs)
1180
- {
1181
- assert(refs);
1182
-
1183
- if (refs->packfile) {
1184
- struct packref *reference;
1185
-
1186
- git_strmap_foreach_value(refs->packfile, reference, {
1187
- git__free(reference);
1188
- });
1189
-
1190
- git_strmap_free(refs->packfile);
1191
- }
1192
- }
1193
-
1194
1278
  static void refdb_fs_backend__free(git_refdb_backend *_backend)
1195
1279
  {
1196
- refdb_fs_backend *backend;
1280
+ refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
1197
1281
 
1198
- assert(_backend);
1199
- backend = (refdb_fs_backend *)_backend;
1282
+ assert(backend);
1200
1283
 
1201
- refcache_free(&backend->refcache);
1284
+ git_sortedcache_free(backend->refcache);
1202
1285
  git__free(backend->path);
1203
1286
  git__free(backend);
1204
1287
  }
@@ -1222,7 +1305,7 @@ static int setup_namespace(git_buf *path, git_repository *repo)
1222
1305
  if (parts == NULL)
1223
1306
  return -1;
1224
1307
 
1225
- /**
1308
+ /*
1226
1309
  * From `man gitnamespaces`:
1227
1310
  * namespaces which include a / will expand to a hierarchy
1228
1311
  * of namespaces; for example, GIT_NAMESPACE=foo/bar will store
@@ -1239,15 +1322,484 @@ static int setup_namespace(git_buf *path, git_repository *repo)
1239
1322
  if (git_futils_mkdir_r(git_buf_cstr(path), repo->path_repository, 0777) < 0)
1240
1323
  return -1;
1241
1324
 
1242
- /* Return the root of the namespaced path, i.e. without the trailing '/refs' */
1325
+ /* Return root of the namespaced path, i.e. without the trailing '/refs' */
1243
1326
  git_buf_rtruncate_at_char(path, '/');
1244
1327
  return 0;
1245
1328
  }
1246
1329
 
1330
+ static int reflog_alloc(git_reflog **reflog, const char *name)
1331
+ {
1332
+ git_reflog *log;
1333
+
1334
+ *reflog = NULL;
1335
+
1336
+ log = git__calloc(1, sizeof(git_reflog));
1337
+ GITERR_CHECK_ALLOC(log);
1338
+
1339
+ log->ref_name = git__strdup(name);
1340
+ GITERR_CHECK_ALLOC(log->ref_name);
1341
+
1342
+ if (git_vector_init(&log->entries, 0, NULL) < 0) {
1343
+ git__free(log->ref_name);
1344
+ git__free(log);
1345
+ return -1;
1346
+ }
1347
+
1348
+ *reflog = log;
1349
+
1350
+ return 0;
1351
+ }
1352
+
1353
+ static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size)
1354
+ {
1355
+ const char *ptr;
1356
+ git_reflog_entry *entry;
1357
+
1358
+ #define seek_forward(_increase) do { \
1359
+ if (_increase >= buf_size) { \
1360
+ giterr_set(GITERR_INVALID, "Ran out of data while parsing reflog"); \
1361
+ goto fail; \
1362
+ } \
1363
+ buf += _increase; \
1364
+ buf_size -= _increase; \
1365
+ } while (0)
1366
+
1367
+ while (buf_size > GIT_REFLOG_SIZE_MIN) {
1368
+ entry = git__calloc(1, sizeof(git_reflog_entry));
1369
+ GITERR_CHECK_ALLOC(entry);
1370
+
1371
+ entry->committer = git__malloc(sizeof(git_signature));
1372
+ GITERR_CHECK_ALLOC(entry->committer);
1373
+
1374
+ if (git_oid_fromstrn(&entry->oid_old, buf, GIT_OID_HEXSZ) < 0)
1375
+ goto fail;
1376
+ seek_forward(GIT_OID_HEXSZ + 1);
1377
+
1378
+ if (git_oid_fromstrn(&entry->oid_cur, buf, GIT_OID_HEXSZ) < 0)
1379
+ goto fail;
1380
+ seek_forward(GIT_OID_HEXSZ + 1);
1381
+
1382
+ ptr = buf;
1383
+
1384
+ /* Seek forward to the end of the signature. */
1385
+ while (*buf && *buf != '\t' && *buf != '\n')
1386
+ seek_forward(1);
1387
+
1388
+ if (git_signature__parse(entry->committer, &ptr, buf + 1, NULL, *buf) < 0)
1389
+ goto fail;
1390
+
1391
+ if (*buf == '\t') {
1392
+ /* We got a message. Read everything till we reach LF. */
1393
+ seek_forward(1);
1394
+ ptr = buf;
1395
+
1396
+ while (*buf && *buf != '\n')
1397
+ seek_forward(1);
1398
+
1399
+ entry->msg = git__strndup(ptr, buf - ptr);
1400
+ GITERR_CHECK_ALLOC(entry->msg);
1401
+ } else
1402
+ entry->msg = NULL;
1403
+
1404
+ while (*buf && *buf == '\n' && buf_size > 1)
1405
+ seek_forward(1);
1406
+
1407
+ if (git_vector_insert(&log->entries, entry) < 0)
1408
+ goto fail;
1409
+ }
1410
+
1411
+ return 0;
1412
+
1413
+ #undef seek_forward
1414
+
1415
+ fail:
1416
+ if (entry)
1417
+ git_reflog_entry__free(entry);
1418
+
1419
+ return -1;
1420
+ }
1421
+
1422
+ static int create_new_reflog_file(const char *filepath)
1423
+ {
1424
+ int fd, error;
1425
+
1426
+ if ((error = git_futils_mkpath2file(filepath, GIT_REFLOG_DIR_MODE)) < 0)
1427
+ return error;
1428
+
1429
+ if ((fd = p_open(filepath,
1430
+ O_WRONLY | O_CREAT,
1431
+ GIT_REFLOG_FILE_MODE)) < 0)
1432
+ return -1;
1433
+
1434
+ return p_close(fd);
1435
+ }
1436
+
1437
+ GIT_INLINE(int) retrieve_reflog_path(git_buf *path, git_repository *repo, const char *name)
1438
+ {
1439
+ return git_buf_join3(path, '/', repo->path_repository, GIT_REFLOG_DIR, name);
1440
+ }
1441
+
1442
+ static int refdb_reflog_fs__ensure_log(git_refdb_backend *_backend, const char *name)
1443
+ {
1444
+ refdb_fs_backend *backend;
1445
+ git_repository *repo;
1446
+ git_buf path = GIT_BUF_INIT;
1447
+ int error;
1448
+
1449
+ assert(_backend && name);
1450
+
1451
+ backend = (refdb_fs_backend *) _backend;
1452
+ repo = backend->repo;
1453
+
1454
+ if ((error = retrieve_reflog_path(&path, repo, name)) < 0)
1455
+ return error;
1456
+
1457
+ error = create_new_reflog_file(git_buf_cstr(&path));
1458
+ git_buf_free(&path);
1459
+
1460
+ return error;
1461
+ }
1462
+
1463
+ static int has_reflog(git_repository *repo, const char *name)
1464
+ {
1465
+ int ret = 0;
1466
+ git_buf path = GIT_BUF_INIT;
1467
+
1468
+ if (retrieve_reflog_path(&path, repo, name) < 0)
1469
+ goto cleanup;
1470
+
1471
+ ret = git_path_isfile(git_buf_cstr(&path));
1472
+
1473
+ cleanup:
1474
+ git_buf_free(&path);
1475
+ return ret;
1476
+ }
1477
+
1478
+ static int refdb_reflog_fs__has_log(git_refdb_backend *_backend, const char *name)
1479
+ {
1480
+ refdb_fs_backend *backend;
1481
+
1482
+ assert(_backend && name);
1483
+
1484
+ backend = (refdb_fs_backend *) _backend;
1485
+
1486
+ return has_reflog(backend->repo, name);
1487
+ }
1488
+
1489
+ static int refdb_reflog_fs__read(git_reflog **out, git_refdb_backend *_backend, const char *name)
1490
+ {
1491
+ int error = -1;
1492
+ git_buf log_path = GIT_BUF_INIT;
1493
+ git_buf log_file = GIT_BUF_INIT;
1494
+ git_reflog *log = NULL;
1495
+ git_repository *repo;
1496
+ refdb_fs_backend *backend;
1497
+
1498
+ assert(out && _backend && name);
1499
+
1500
+ backend = (refdb_fs_backend *) _backend;
1501
+ repo = backend->repo;
1502
+
1503
+ if (reflog_alloc(&log, name) < 0)
1504
+ return -1;
1505
+
1506
+ if (retrieve_reflog_path(&log_path, repo, name) < 0)
1507
+ goto cleanup;
1508
+
1509
+ error = git_futils_readbuffer(&log_file, git_buf_cstr(&log_path));
1510
+ if (error < 0 && error != GIT_ENOTFOUND)
1511
+ goto cleanup;
1512
+
1513
+ if ((error == GIT_ENOTFOUND) &&
1514
+ ((error = create_new_reflog_file(git_buf_cstr(&log_path))) < 0))
1515
+ goto cleanup;
1516
+
1517
+ if ((error = reflog_parse(log,
1518
+ git_buf_cstr(&log_file), git_buf_len(&log_file))) < 0)
1519
+ goto cleanup;
1520
+
1521
+ *out = log;
1522
+ goto success;
1523
+
1524
+ cleanup:
1525
+ git_reflog_free(log);
1526
+
1527
+ success:
1528
+ git_buf_free(&log_file);
1529
+ git_buf_free(&log_path);
1530
+
1531
+ return error;
1532
+ }
1533
+
1534
+ static int serialize_reflog_entry(
1535
+ git_buf *buf,
1536
+ const git_oid *oid_old,
1537
+ const git_oid *oid_new,
1538
+ const git_signature *committer,
1539
+ const char *msg)
1540
+ {
1541
+ char raw_old[GIT_OID_HEXSZ+1];
1542
+ char raw_new[GIT_OID_HEXSZ+1];
1543
+
1544
+ git_oid_tostr(raw_old, GIT_OID_HEXSZ+1, oid_old);
1545
+ git_oid_tostr(raw_new, GIT_OID_HEXSZ+1, oid_new);
1546
+
1547
+ git_buf_clear(buf);
1548
+
1549
+ git_buf_puts(buf, raw_old);
1550
+ git_buf_putc(buf, ' ');
1551
+ git_buf_puts(buf, raw_new);
1552
+
1553
+ git_signature__writebuf(buf, " ", committer);
1554
+
1555
+ /* drop trailing LF */
1556
+ git_buf_rtrim(buf);
1557
+
1558
+ if (msg) {
1559
+ git_buf_putc(buf, '\t');
1560
+ git_buf_puts(buf, msg);
1561
+ }
1562
+
1563
+ git_buf_putc(buf, '\n');
1564
+
1565
+ return git_buf_oom(buf);
1566
+ }
1567
+
1568
+ static int lock_reflog(git_filebuf *file, refdb_fs_backend *backend, const char *refname)
1569
+ {
1570
+ git_repository *repo;
1571
+ git_buf log_path = GIT_BUF_INIT;
1572
+ int error;
1573
+
1574
+ repo = backend->repo;
1575
+
1576
+ if (retrieve_reflog_path(&log_path, repo, refname) < 0)
1577
+ return -1;
1578
+
1579
+ if (!git_path_isfile(git_buf_cstr(&log_path))) {
1580
+ giterr_set(GITERR_INVALID,
1581
+ "Log file for reference '%s' doesn't exist.", refname);
1582
+ error = -1;
1583
+ goto cleanup;
1584
+ }
1585
+
1586
+ error = git_filebuf_open(file, git_buf_cstr(&log_path), 0, GIT_REFLOG_FILE_MODE);
1587
+
1588
+ cleanup:
1589
+ git_buf_free(&log_path);
1590
+
1591
+ return error;
1592
+ }
1593
+
1594
+ static int refdb_reflog_fs__write(git_refdb_backend *_backend, git_reflog *reflog)
1595
+ {
1596
+ int error = -1;
1597
+ unsigned int i;
1598
+ git_reflog_entry *entry;
1599
+ refdb_fs_backend *backend;
1600
+ git_buf log = GIT_BUF_INIT;
1601
+ git_filebuf fbuf = GIT_FILEBUF_INIT;
1602
+
1603
+ assert(_backend && reflog);
1604
+
1605
+ backend = (refdb_fs_backend *) _backend;
1606
+
1607
+ if ((error = lock_reflog(&fbuf, backend, reflog->ref_name)) < 0)
1608
+ return -1;
1609
+
1610
+ git_vector_foreach(&reflog->entries, i, entry) {
1611
+ if (serialize_reflog_entry(&log, &(entry->oid_old), &(entry->oid_cur), entry->committer, entry->msg) < 0)
1612
+ goto cleanup;
1613
+
1614
+ if ((error = git_filebuf_write(&fbuf, log.ptr, log.size)) < 0)
1615
+ goto cleanup;
1616
+ }
1617
+
1618
+ error = git_filebuf_commit(&fbuf);
1619
+ goto success;
1620
+
1621
+ cleanup:
1622
+ git_filebuf_cleanup(&fbuf);
1623
+
1624
+ success:
1625
+ git_buf_free(&log);
1626
+
1627
+ return error;
1628
+ }
1629
+
1630
+ /* Append to the reflog, must be called under reference lock */
1631
+ static int reflog_append(refdb_fs_backend *backend, const git_reference *ref, const git_oid *old, const git_oid *new, const git_signature *who, const char *message)
1632
+ {
1633
+ int error, is_symbolic;
1634
+ git_oid old_id = {{0}}, new_id = {{0}};
1635
+ git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT;
1636
+ git_repository *repo = backend->repo;
1637
+
1638
+ is_symbolic = ref->type == GIT_REF_SYMBOLIC;
1639
+
1640
+ /* "normal" symbolic updates do not write */
1641
+ if (is_symbolic &&
1642
+ strcmp(ref->name, GIT_HEAD_FILE) &&
1643
+ !(old && new))
1644
+ return 0;
1645
+
1646
+ /* From here on is_symoblic also means that it's HEAD */
1647
+
1648
+ if (old) {
1649
+ git_oid_cpy(&old_id, old);
1650
+ } else {
1651
+ error = git_reference_name_to_id(&old_id, repo, ref->name);
1652
+ if (error < 0 && error != GIT_ENOTFOUND)
1653
+ return error;
1654
+ }
1655
+
1656
+ if (new) {
1657
+ git_oid_cpy(&new_id, new);
1658
+ } else {
1659
+ if (!is_symbolic) {
1660
+ git_oid_cpy(&new_id, git_reference_target(ref));
1661
+ } else {
1662
+ error = git_reference_name_to_id(&new_id, repo, git_reference_symbolic_target(ref));
1663
+ if (error < 0 && error != GIT_ENOTFOUND)
1664
+ return error;
1665
+ /* detaching HEAD does not create an entry */
1666
+ if (error == GIT_ENOTFOUND)
1667
+ return 0;
1668
+
1669
+ giterr_clear();
1670
+ }
1671
+ }
1672
+
1673
+ if ((error = serialize_reflog_entry(&buf, &old_id, &new_id, who, message)) < 0)
1674
+ goto cleanup;
1675
+
1676
+ if ((error = retrieve_reflog_path(&path, repo, ref->name)) < 0)
1677
+ goto cleanup;
1678
+
1679
+ if (((error = git_futils_mkpath2file(git_buf_cstr(&path), 0777)) < 0) &&
1680
+ (error != GIT_EEXISTS)) {
1681
+ goto cleanup;
1682
+ }
1683
+
1684
+ error = git_futils_writebuffer(&buf, git_buf_cstr(&path), O_WRONLY|O_CREAT|O_APPEND, GIT_REFLOG_FILE_MODE);
1685
+
1686
+ cleanup:
1687
+ git_buf_free(&buf);
1688
+ git_buf_free(&path);
1689
+
1690
+ return error;
1691
+ }
1692
+
1693
+ static int refdb_reflog_fs__rename(git_refdb_backend *_backend, const char *old_name, const char *new_name)
1694
+ {
1695
+ int error = 0, fd;
1696
+ git_buf old_path = GIT_BUF_INIT;
1697
+ git_buf new_path = GIT_BUF_INIT;
1698
+ git_buf temp_path = GIT_BUF_INIT;
1699
+ git_buf normalized = GIT_BUF_INIT;
1700
+ git_repository *repo;
1701
+ refdb_fs_backend *backend;
1702
+
1703
+ assert(_backend && old_name && new_name);
1704
+
1705
+ backend = (refdb_fs_backend *) _backend;
1706
+ repo = backend->repo;
1707
+
1708
+ if ((error = git_reference__normalize_name(
1709
+ &normalized, new_name, GIT_REF_FORMAT_ALLOW_ONELEVEL)) < 0)
1710
+ return error;
1711
+
1712
+ if (git_buf_joinpath(&temp_path, repo->path_repository, GIT_REFLOG_DIR) < 0)
1713
+ return -1;
1714
+
1715
+ if (git_buf_joinpath(&old_path, git_buf_cstr(&temp_path), old_name) < 0)
1716
+ return -1;
1717
+
1718
+ if (git_buf_joinpath(&new_path, git_buf_cstr(&temp_path), git_buf_cstr(&normalized)) < 0)
1719
+ return -1;
1720
+
1721
+ if (!git_path_exists(git_buf_cstr(&old_path))) {
1722
+ error = GIT_ENOTFOUND;
1723
+ goto cleanup;
1724
+ }
1725
+
1726
+ /*
1727
+ * Move the reflog to a temporary place. This two-phase renaming is required
1728
+ * in order to cope with funny renaming use cases when one tries to move a reference
1729
+ * to a partially colliding namespace:
1730
+ * - a/b -> a/b/c
1731
+ * - a/b/c/d -> a/b/c
1732
+ */
1733
+ if (git_buf_joinpath(&temp_path, git_buf_cstr(&temp_path), "temp_reflog") < 0)
1734
+ return -1;
1735
+
1736
+ if ((fd = git_futils_mktmp(&temp_path, git_buf_cstr(&temp_path), GIT_REFLOG_FILE_MODE)) < 0) {
1737
+ error = -1;
1738
+ goto cleanup;
1739
+ }
1740
+
1741
+ p_close(fd);
1742
+
1743
+ if (p_rename(git_buf_cstr(&old_path), git_buf_cstr(&temp_path)) < 0) {
1744
+ giterr_set(GITERR_OS, "Failed to rename reflog for %s", new_name);
1745
+ error = -1;
1746
+ goto cleanup;
1747
+ }
1748
+
1749
+ if (git_path_isdir(git_buf_cstr(&new_path)) &&
1750
+ (git_futils_rmdir_r(git_buf_cstr(&new_path), NULL, GIT_RMDIR_SKIP_NONEMPTY) < 0)) {
1751
+ error = -1;
1752
+ goto cleanup;
1753
+ }
1754
+
1755
+ if (git_futils_mkpath2file(git_buf_cstr(&new_path), GIT_REFLOG_DIR_MODE) < 0) {
1756
+ error = -1;
1757
+ goto cleanup;
1758
+ }
1759
+
1760
+ if (p_rename(git_buf_cstr(&temp_path), git_buf_cstr(&new_path)) < 0) {
1761
+ giterr_set(GITERR_OS, "Failed to rename reflog for %s", new_name);
1762
+ error = -1;
1763
+ }
1764
+
1765
+ cleanup:
1766
+ git_buf_free(&temp_path);
1767
+ git_buf_free(&old_path);
1768
+ git_buf_free(&new_path);
1769
+ git_buf_free(&normalized);
1770
+
1771
+ return error;
1772
+ }
1773
+
1774
+ static int refdb_reflog_fs__delete(git_refdb_backend *_backend, const char *name)
1775
+ {
1776
+ int error;
1777
+ git_buf path = GIT_BUF_INIT;
1778
+
1779
+ git_repository *repo;
1780
+ refdb_fs_backend *backend;
1781
+
1782
+ assert(_backend && name);
1783
+
1784
+ backend = (refdb_fs_backend *) _backend;
1785
+ repo = backend->repo;
1786
+
1787
+ error = retrieve_reflog_path(&path, repo, name);
1788
+
1789
+ if (!error && git_path_exists(path.ptr))
1790
+ error = p_unlink(path.ptr);
1791
+
1792
+ git_buf_free(&path);
1793
+
1794
+ return error;
1795
+
1796
+ }
1797
+
1247
1798
  int git_refdb_backend_fs(
1248
1799
  git_refdb_backend **backend_out,
1249
1800
  git_repository *repository)
1250
1801
  {
1802
+ int t = 0;
1251
1803
  git_buf path = GIT_BUF_INIT;
1252
1804
  refdb_fs_backend *backend;
1253
1805
 
@@ -1256,22 +1808,49 @@ int git_refdb_backend_fs(
1256
1808
 
1257
1809
  backend->repo = repository;
1258
1810
 
1259
- if (setup_namespace(&path, repository) < 0) {
1260
- git__free(backend);
1261
- return -1;
1262
- }
1811
+ if (setup_namespace(&path, repository) < 0)
1812
+ goto fail;
1263
1813
 
1264
1814
  backend->path = git_buf_detach(&path);
1265
1815
 
1816
+ if (git_buf_joinpath(&path, backend->path, GIT_PACKEDREFS_FILE) < 0 ||
1817
+ git_sortedcache_new(
1818
+ &backend->refcache, offsetof(struct packref, name),
1819
+ NULL, NULL, packref_cmp, git_buf_cstr(&path)) < 0)
1820
+ goto fail;
1821
+
1822
+ git_buf_free(&path);
1823
+
1824
+ if (!git_repository__cvar(&t, backend->repo, GIT_CVAR_IGNORECASE) && t) {
1825
+ backend->iterator_flags |= GIT_ITERATOR_IGNORE_CASE;
1826
+ backend->direach_flags |= GIT_PATH_DIR_IGNORE_CASE;
1827
+ }
1828
+ if (!git_repository__cvar(&t, backend->repo, GIT_CVAR_PRECOMPOSE) && t) {
1829
+ backend->iterator_flags |= GIT_ITERATOR_PRECOMPOSE_UNICODE;
1830
+ backend->direach_flags |= GIT_PATH_DIR_PRECOMPOSE_UNICODE;
1831
+ }
1832
+
1266
1833
  backend->parent.exists = &refdb_fs_backend__exists;
1267
1834
  backend->parent.lookup = &refdb_fs_backend__lookup;
1268
1835
  backend->parent.iterator = &refdb_fs_backend__iterator;
1269
1836
  backend->parent.write = &refdb_fs_backend__write;
1270
- backend->parent.delete = &refdb_fs_backend__delete;
1837
+ backend->parent.del = &refdb_fs_backend__delete;
1271
1838
  backend->parent.rename = &refdb_fs_backend__rename;
1272
1839
  backend->parent.compress = &refdb_fs_backend__compress;
1840
+ backend->parent.has_log = &refdb_reflog_fs__has_log;
1841
+ backend->parent.ensure_log = &refdb_reflog_fs__ensure_log;
1273
1842
  backend->parent.free = &refdb_fs_backend__free;
1843
+ backend->parent.reflog_read = &refdb_reflog_fs__read;
1844
+ backend->parent.reflog_write = &refdb_reflog_fs__write;
1845
+ backend->parent.reflog_rename = &refdb_reflog_fs__rename;
1846
+ backend->parent.reflog_delete = &refdb_reflog_fs__delete;
1274
1847
 
1275
1848
  *backend_out = (git_refdb_backend *)backend;
1276
1849
  return 0;
1850
+
1851
+ fail:
1852
+ git_buf_free(&path);
1853
+ git__free(backend->path);
1854
+ git__free(backend);
1855
+ return -1;
1277
1856
  }