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
@@ -7,14 +7,15 @@
7
7
 
8
8
  #include "common.h"
9
9
  #include "config.h"
10
- #include "fileops.h"
11
10
  #include "filebuf.h"
11
+ #include "sysdir.h"
12
12
  #include "buffer.h"
13
13
  #include "buf_text.h"
14
14
  #include "git2/config.h"
15
15
  #include "git2/sys/config.h"
16
16
  #include "git2/types.h"
17
17
  #include "strmap.h"
18
+ #include "array.h"
18
19
 
19
20
  #include <ctype.h>
20
21
  #include <sys/types.h>
@@ -25,8 +26,18 @@ GIT__USE_STRMAP;
25
26
  typedef struct cvar_t {
26
27
  struct cvar_t *next;
27
28
  git_config_entry *entry;
29
+ bool included; /* whether this is part of [include] */
28
30
  } cvar_t;
29
31
 
32
+ typedef struct git_config_file_iter {
33
+ git_config_iterator parent;
34
+ git_strmap_iter iter;
35
+ cvar_t* next_var;
36
+ } git_config_file_iter;
37
+
38
+ /* Max depth for [include] directives */
39
+ #define MAX_INCLUDE_DEPTH 10
40
+
30
41
  #define CVAR_LIST_HEAD(list) ((list)->head)
31
42
 
32
43
  #define CVAR_LIST_TAIL(list) ((list)->tail)
@@ -65,34 +76,63 @@ typedef struct cvar_t {
65
76
  (iter) && (((tmp) = CVAR_LIST_NEXT(iter) || 1));\
66
77
  (iter) = (tmp))
67
78
 
68
- typedef struct {
69
- git_config_backend parent;
79
+ struct reader {
80
+ time_t file_mtime;
81
+ size_t file_size;
82
+ char *file_path;
83
+ git_buf buffer;
84
+ char *read_ptr;
85
+ int line_number;
86
+ int eof;
87
+ };
70
88
 
89
+ typedef struct {
90
+ git_atomic refcount;
71
91
  git_strmap *values;
92
+ } refcounted_strmap;
72
93
 
73
- struct {
74
- git_buf buffer;
75
- char *read_ptr;
76
- int line_number;
77
- int eof;
78
- } reader;
94
+ typedef struct {
95
+ git_config_backend parent;
96
+ /* mutex to coordinate accessing the values */
97
+ git_mutex values_mutex;
98
+ refcounted_strmap *values;
99
+ int readonly;
100
+ } diskfile_header;
79
101
 
80
- char *file_path;
81
- time_t file_mtime;
82
- size_t file_size;
102
+ typedef struct {
103
+ diskfile_header header;
83
104
 
84
105
  git_config_level_t level;
106
+
107
+ git_array_t(struct reader) readers;
108
+
109
+ char *file_path;
85
110
  } diskfile_backend;
86
111
 
87
- static int config_parse(diskfile_backend *cfg_file, git_config_level_t level);
88
- static int parse_variable(diskfile_backend *cfg, char **var_name, char **var_value);
112
+ typedef struct {
113
+ diskfile_header header;
114
+
115
+ diskfile_backend *snapshot_from;
116
+ } diskfile_readonly_backend;
117
+
118
+ static int config_parse(git_strmap *values, diskfile_backend *cfg_file, struct reader *reader, git_config_level_t level, int depth);
119
+ static int parse_variable(struct reader *reader, char **var_name, char **var_value);
89
120
  static int config_write(diskfile_backend *cfg, const char *key, const regex_t *preg, const char *value);
90
121
  static char *escape_value(const char *ptr);
91
122
 
92
- static void set_parse_error(diskfile_backend *backend, int col, const char *error_str)
123
+ int git_config_file__snapshot(git_config_backend **out, diskfile_backend *in);
124
+ static int config_snapshot(git_config_backend **out, git_config_backend *in);
125
+
126
+ static void set_parse_error(struct reader *reader, int col, const char *error_str)
93
127
  {
94
128
  giterr_set(GITERR_CONFIG, "Failed to parse config file: %s (in %s:%d, column %d)",
95
- error_str, backend->file_path, backend->reader.line_number, col);
129
+ error_str, reader->file_path, reader->line_number, col);
130
+ }
131
+
132
+ static int config_error_readonly(void)
133
+ {
134
+ giterr_set(GITERR_CONFIG, "this backend is read-only");
135
+ return -1;
96
136
  }
97
137
 
98
138
  static void cvar_free(cvar_t *var)
@@ -118,7 +158,7 @@ int git_config_file_normalize_section(char *start, char *end)
118
158
  if (end && scan >= end)
119
159
  break;
120
160
  if (isalnum(*scan))
121
- *scan = tolower(*scan);
161
+ *scan = (char)tolower(*scan);
122
162
  else if (*scan != '-' || scan == start)
123
163
  return GIT_EINVALIDSPEC;
124
164
  }
@@ -129,39 +169,28 @@ int git_config_file_normalize_section(char *start, char *end)
129
169
  return 0;
130
170
  }
131
171
 
132
- /* Take something the user gave us and make it nice for our hash function */
133
- static int normalize_name(const char *in, char **out)
172
+ /* Add or append the new config option */
173
+ static int append_entry(git_strmap *values, cvar_t *var)
134
174
  {
135
- char *name, *fdot, *ldot;
136
-
137
- assert(in && out);
138
-
139
- name = git__strdup(in);
140
- GITERR_CHECK_ALLOC(name);
141
-
142
- fdot = strchr(name, '.');
143
- ldot = strrchr(name, '.');
144
-
145
- if (fdot == NULL || fdot == name || ldot == NULL || !ldot[1])
146
- goto invalid;
175
+ git_strmap_iter pos;
176
+ cvar_t *existing;
177
+ int error = 0;
147
178
 
148
- /* Validate and downcase up to first dot and after last dot */
149
- if (git_config_file_normalize_section(name, fdot) < 0 ||
150
- git_config_file_normalize_section(ldot + 1, NULL) < 0)
151
- goto invalid;
152
-
153
- /* If there is a middle range, make sure it doesn't have newlines */
154
- while (fdot < ldot)
155
- if (*fdot++ == '\n')
156
- goto invalid;
179
+ pos = git_strmap_lookup_index(values, var->entry->name);
180
+ if (!git_strmap_valid_index(values, pos)) {
181
+ git_strmap_insert(values, var->entry->name, var, error);
182
+ } else {
183
+ existing = git_strmap_value_at(values, pos);
184
+ while (existing->next != NULL) {
185
+ existing = existing->next;
186
+ }
187
+ existing->next = var;
188
+ }
157
189
 
158
- *out = name;
159
- return 0;
190
+ if (error > 0)
191
+ error = 0;
160
192
 
161
- invalid:
162
- git__free(name);
163
- giterr_set(GITERR_CONFIG, "Invalid config item name '%s'", in);
164
- return GIT_EINVALIDSPEC;
193
+ return error;
165
194
  }
166
195
 
167
196
  static void free_vars(git_strmap *values)
@@ -181,295 +210,341 @@ static void free_vars(git_strmap *values)
181
210
  git_strmap_free(values);
182
211
  }
183
212
 
213
+ static void refcounted_strmap_free(refcounted_strmap *map)
214
+ {
215
+ if (!map)
216
+ return;
217
+
218
+ if (git_atomic_dec(&map->refcount) != 0)
219
+ return;
220
+
221
+ free_vars(map->values);
222
+ git__free(map);
223
+ }
224
+
225
+ /**
226
+ * Take the current values map from the backend and increase its
227
+ * refcount. This is its own function to make sure we use the mutex to
228
+ * avoid the map pointer from changing under us.
229
+ */
230
+ static refcounted_strmap *refcounted_strmap_take(diskfile_header *h)
231
+ {
232
+ refcounted_strmap *map;
233
+
234
+ git_mutex_lock(&h->values_mutex);
235
+
236
+ map = h->values;
237
+ git_atomic_inc(&map->refcount);
238
+
239
+ git_mutex_unlock(&h->values_mutex);
240
+
241
+ return map;
242
+ }
243
+
244
+ static int refcounted_strmap_alloc(refcounted_strmap **out)
245
+ {
246
+ refcounted_strmap *map;
247
+ int error;
248
+
249
+ map = git__calloc(1, sizeof(refcounted_strmap));
250
+ GITERR_CHECK_ALLOC(map);
251
+
252
+ git_atomic_set(&map->refcount, 1);
253
+
254
+ if ((error = git_strmap_alloc(&map->values)) < 0)
255
+ git__free(map);
256
+ else
257
+ *out = map;
258
+
259
+ return error;
260
+ }
261
+
184
262
  static int config_open(git_config_backend *cfg, git_config_level_t level)
185
263
  {
186
264
  int res;
265
+ struct reader *reader;
187
266
  diskfile_backend *b = (diskfile_backend *)cfg;
188
267
 
189
268
  b->level = level;
190
269
 
191
- b->values = git_strmap_alloc();
192
- GITERR_CHECK_ALLOC(b->values);
270
+ if ((res = refcounted_strmap_alloc(&b->header.values)) < 0)
271
+ return res;
272
+
273
+ git_array_init(b->readers);
274
+ reader = git_array_alloc(b->readers);
275
+ if (!reader) {
276
+ refcounted_strmap_free(b->header.values);
277
+ return -1;
278
+ }
279
+ memset(reader, 0, sizeof(struct reader));
280
+
281
+ reader->file_path = git__strdup(b->file_path);
282
+ GITERR_CHECK_ALLOC(reader->file_path);
193
283
 
194
- git_buf_init(&b->reader.buffer, 0);
284
+ git_buf_init(&reader->buffer, 0);
195
285
  res = git_futils_readbuffer_updated(
196
- &b->reader.buffer, b->file_path, &b->file_mtime, &b->file_size, NULL);
286
+ &reader->buffer, b->file_path, &reader->file_mtime, &reader->file_size, NULL);
197
287
 
198
288
  /* It's fine if the file doesn't exist */
199
289
  if (res == GIT_ENOTFOUND)
200
290
  return 0;
201
291
 
202
- if (res < 0 || (res = config_parse(b, level)) < 0) {
203
- free_vars(b->values);
204
- b->values = NULL;
292
+ if (res < 0 || (res = config_parse(b->header.values->values, b, reader, level, 0)) < 0) {
293
+ refcounted_strmap_free(b->header.values);
294
+ b->header.values = NULL;
205
295
  }
206
296
 
207
- git_buf_free(&b->reader.buffer);
297
+ reader = git_array_get(b->readers, 0);
298
+ git_buf_free(&reader->buffer);
299
+
208
300
  return res;
209
301
  }
210
302
 
303
+ /* The meat of the refresh, as we want to use it in different places */
304
+ static int config__refresh(git_config_backend *cfg)
305
+ {
306
+ refcounted_strmap *values = NULL, *tmp;
307
+ diskfile_backend *b = (diskfile_backend *)cfg;
308
+ struct reader *reader = NULL;
309
+ int error = 0;
310
+
311
+ if ((error = refcounted_strmap_alloc(&values)) < 0)
312
+ goto out;
313
+
314
+ reader = git_array_get(b->readers, git_array_size(b->readers) - 1);
315
+ GITERR_CHECK_ALLOC(reader);
316
+
317
+ if ((error = config_parse(values->values, b, reader, b->level, 0)) < 0)
318
+ goto out;
319
+
320
+ git_mutex_lock(&b->header.values_mutex);
321
+
322
+ tmp = b->header.values;
323
+ b->header.values = values;
324
+ values = tmp;
325
+
326
+ git_mutex_unlock(&b->header.values_mutex);
327
+
328
+ out:
329
+ refcounted_strmap_free(values);
330
+ if (reader)
331
+ git_buf_free(&reader->buffer);
332
+ return error;
333
+ }
334
+
211
335
  static int config_refresh(git_config_backend *cfg)
212
336
  {
213
- int res, updated = 0;
337
+ int error = 0, updated = 0, any_updated = 0;
214
338
  diskfile_backend *b = (diskfile_backend *)cfg;
215
- git_strmap *old_values;
339
+ struct reader *reader = NULL;
340
+ uint32_t i;
216
341
 
217
- res = git_futils_readbuffer_updated(
218
- &b->reader.buffer, b->file_path, &b->file_mtime, &b->file_size, &updated);
219
- if (res < 0 || !updated)
220
- return (res == GIT_ENOTFOUND) ? 0 : res;
221
-
222
- /* need to reload - store old values and prep for reload */
223
- old_values = b->values;
224
- b->values = git_strmap_alloc();
225
- GITERR_CHECK_ALLOC(b->values);
226
-
227
- if ((res = config_parse(b, b->level)) < 0) {
228
- free_vars(b->values);
229
- b->values = old_values;
230
- } else {
231
- free_vars(old_values);
342
+ for (i = 0; i < git_array_size(b->readers); i++) {
343
+ reader = git_array_get(b->readers, i);
344
+ error = git_futils_readbuffer_updated(
345
+ &reader->buffer, reader->file_path,
346
+ &reader->file_mtime, &reader->file_size, &updated);
347
+
348
+ if (error < 0 && error != GIT_ENOTFOUND)
349
+ return error;
350
+
351
+ if (updated)
352
+ any_updated = 1;
232
353
  }
233
354
 
234
- git_buf_free(&b->reader.buffer);
235
- return res;
355
+ if (!any_updated)
356
+ return (error == GIT_ENOTFOUND) ? 0 : error;
357
+
358
+ return config__refresh(cfg);
236
359
  }
237
360
 
238
361
  static void backend_free(git_config_backend *_backend)
239
362
  {
240
363
  diskfile_backend *backend = (diskfile_backend *)_backend;
364
+ uint32_t i;
241
365
 
242
366
  if (backend == NULL)
243
367
  return;
244
368
 
369
+ for (i = 0; i < git_array_size(backend->readers); i++) {
370
+ struct reader *r = git_array_get(backend->readers, i);
371
+ git__free(r->file_path);
372
+ }
373
+ git_array_clear(backend->readers);
374
+
245
375
  git__free(backend->file_path);
246
- free_vars(backend->values);
376
+ refcounted_strmap_free(backend->header.values);
377
+ git_mutex_free(&backend->header.values_mutex);
247
378
  git__free(backend);
248
379
  }
249
380
 
250
- static int file_foreach(
251
- git_config_backend *backend,
252
- const char *regexp,
253
- int (*fn)(const git_config_entry *, void *),
254
- void *data)
381
+ static void config_iterator_free(
382
+ git_config_iterator* iter)
255
383
  {
256
- diskfile_backend *b = (diskfile_backend *)backend;
257
- cvar_t *var, *next_var;
258
- const char *key;
259
- regex_t regex;
260
- int result = 0;
384
+ iter->backend->free(iter->backend);
385
+ git__free(iter);
386
+ }
261
387
 
262
- if (!b->values)
263
- return 0;
388
+ static int config_iterator_next(
389
+ git_config_entry **entry,
390
+ git_config_iterator *iter)
391
+ {
392
+ git_config_file_iter *it = (git_config_file_iter *) iter;
393
+ diskfile_header *h = (diskfile_header *) it->parent.backend;
394
+ git_strmap *values = h->values->values;
395
+ int err = 0;
396
+ cvar_t * var;
397
+
398
+ if (it->next_var == NULL) {
399
+ err = git_strmap_next((void**) &var, &(it->iter), values);
400
+ } else {
401
+ var = it->next_var;
402
+ }
264
403
 
265
- if (regexp != NULL) {
266
- if ((result = regcomp(&regex, regexp, REG_EXTENDED)) < 0) {
267
- giterr_set_regex(&regex, result);
268
- regfree(&regex);
269
- return -1;
270
- }
404
+ if (err < 0) {
405
+ it->next_var = NULL;
406
+ return err;
271
407
  }
272
408
 
273
- git_strmap_foreach(b->values, key, var,
274
- for (; var != NULL; var = next_var) {
275
- next_var = CVAR_LIST_NEXT(var);
409
+ *entry = var->entry;
410
+ it->next_var = CVAR_LIST_NEXT(var);
276
411
 
277
- /* skip non-matching keys if regexp was provided */
278
- if (regexp && regexec(&regex, key, 0, NULL, 0) != 0)
279
- continue;
412
+ return 0;
413
+ }
280
414
 
281
- /* abort iterator on non-zero return value */
282
- if (fn(var->entry, data)) {
283
- giterr_clear();
284
- result = GIT_EUSER;
285
- goto cleanup;
286
- }
287
- }
288
- );
415
+ static int config_iterator_new(
416
+ git_config_iterator **iter,
417
+ struct git_config_backend* backend)
418
+ {
419
+ diskfile_header *h;
420
+ git_config_file_iter *it;
421
+ git_config_backend *snapshot;
422
+ diskfile_backend *b = (diskfile_backend *) backend;
423
+ int error;
289
424
 
290
- cleanup:
291
- if (regexp != NULL)
292
- regfree(&regex);
425
+ if ((error = config_snapshot(&snapshot, backend)) < 0)
426
+ return error;
293
427
 
294
- return result;
428
+ if ((error = snapshot->open(snapshot, b->level)) < 0)
429
+ return error;
430
+
431
+ it = git__calloc(1, sizeof(git_config_file_iter));
432
+ GITERR_CHECK_ALLOC(it);
433
+
434
+ h = (diskfile_header *)snapshot;
435
+
436
+ /* strmap_begin() is currently a macro returning 0 */
437
+ GIT_UNUSED(h);
438
+
439
+ it->parent.backend = snapshot;
440
+ it->iter = git_strmap_begin(h->values);
441
+ it->next_var = NULL;
442
+
443
+ it->parent.next = config_iterator_next;
444
+ it->parent.free = config_iterator_free;
445
+ *iter = (git_config_iterator *) it;
446
+
447
+ return 0;
295
448
  }
296
449
 
297
450
  static int config_set(git_config_backend *cfg, const char *name, const char *value)
298
451
  {
299
- cvar_t *var = NULL, *old_var = NULL;
300
452
  diskfile_backend *b = (diskfile_backend *)cfg;
453
+ refcounted_strmap *map;
454
+ git_strmap *values;
301
455
  char *key, *esc_value = NULL;
302
456
  khiter_t pos;
303
457
  int rval, ret;
304
458
 
305
- if ((rval = normalize_name(name, &key)) < 0)
459
+ if ((rval = git_config__normalize_name(name, &key)) < 0)
306
460
  return rval;
307
461
 
462
+ map = refcounted_strmap_take(&b->header);
463
+ values = map->values;
464
+
308
465
  /*
309
466
  * Try to find it in the existing values and update it if it
310
467
  * only has one value.
311
468
  */
312
- pos = git_strmap_lookup_index(b->values, key);
313
- if (git_strmap_valid_index(b->values, pos)) {
314
- cvar_t *existing = git_strmap_value_at(b->values, pos);
315
- char *tmp = NULL;
316
-
317
- git__free(key);
469
+ pos = git_strmap_lookup_index(values, key);
470
+ if (git_strmap_valid_index(values, pos)) {
471
+ cvar_t *existing = git_strmap_value_at(values, pos);
318
472
 
319
473
  if (existing->next != NULL) {
320
474
  giterr_set(GITERR_CONFIG, "Multivar incompatible with simple set");
321
- return -1;
475
+ ret = -1;
476
+ goto out;
322
477
  }
323
478
 
324
479
  /* don't update if old and new values already match */
325
480
  if ((!existing->entry->value && !value) ||
326
- (existing->entry->value && value && !strcmp(existing->entry->value, value)))
327
- return 0;
328
-
329
- if (value) {
330
- tmp = git__strdup(value);
331
- GITERR_CHECK_ALLOC(tmp);
332
- esc_value = escape_value(value);
333
- GITERR_CHECK_ALLOC(esc_value);
481
+ (existing->entry->value && value &&
482
+ !strcmp(existing->entry->value, value))) {
483
+ ret = 0;
484
+ goto out;
334
485
  }
335
-
336
- git__free((void *)existing->entry->value);
337
- existing->entry->value = tmp;
338
-
339
- ret = config_write(b, existing->entry->name, NULL, esc_value);
340
-
341
- git__free(esc_value);
342
- return ret;
343
486
  }
344
487
 
345
- var = git__malloc(sizeof(cvar_t));
346
- GITERR_CHECK_ALLOC(var);
347
- memset(var, 0x0, sizeof(cvar_t));
348
- var->entry = git__malloc(sizeof(git_config_entry));
349
- GITERR_CHECK_ALLOC(var->entry);
350
- memset(var->entry, 0x0, sizeof(git_config_entry));
351
-
352
- var->entry->name = key;
353
- var->entry->value = NULL;
488
+ /* No early returns due to sanity checks, let's write it out and refresh */
354
489
 
355
490
  if (value) {
356
- var->entry->value = git__strdup(value);
357
- GITERR_CHECK_ALLOC(var->entry->value);
358
491
  esc_value = escape_value(value);
359
492
  GITERR_CHECK_ALLOC(esc_value);
360
493
  }
361
494
 
362
- if (config_write(b, key, NULL, esc_value) < 0) {
363
- git__free(esc_value);
364
- cvar_free(var);
365
- return -1;
366
- }
495
+ if ((ret = config_write(b, key, NULL, esc_value)) < 0)
496
+ goto out;
367
497
 
368
- git__free(esc_value);
369
- git_strmap_insert2(b->values, key, var, old_var, rval);
370
- if (rval < 0)
371
- return -1;
372
- if (old_var != NULL)
373
- cvar_free(old_var);
498
+ ret = config_refresh(cfg);
374
499
 
375
- return 0;
500
+ out:
501
+ refcounted_strmap_free(map);
502
+ git__free(esc_value);
503
+ git__free(key);
504
+ return ret;
376
505
  }
377
506
 
378
507
  /*
379
508
  * Internal function that actually gets the value in string form
380
509
  */
381
- static int config_get(const git_config_backend *cfg, const char *name, const git_config_entry **out)
510
+ static int config_get(git_config_backend *cfg, const char *key, const git_config_entry **out)
382
511
  {
383
- diskfile_backend *b = (diskfile_backend *)cfg;
384
- char *key;
512
+ diskfile_header *h = (diskfile_header *)cfg;
513
+ refcounted_strmap *map;
514
+ git_strmap *values;
385
515
  khiter_t pos;
386
- int error;
387
-
388
- if ((error = normalize_name(name, &key)) < 0)
389
- return error;
390
-
391
- pos = git_strmap_lookup_index(b->values, key);
392
- git__free(key);
393
-
394
- /* no error message; the config system will write one */
395
- if (!git_strmap_valid_index(b->values, pos))
396
- return GIT_ENOTFOUND;
397
-
398
- *out = ((cvar_t *)git_strmap_value_at(b->values, pos))->entry;
399
-
400
- return 0;
401
- }
402
-
403
- static int config_get_multivar(
404
- git_config_backend *cfg,
405
- const char *name,
406
- const char *regex_str,
407
- int (*fn)(const git_config_entry *, void *),
408
- void *data)
409
- {
410
516
  cvar_t *var;
411
- diskfile_backend *b = (diskfile_backend *)cfg;
412
- char *key;
413
- khiter_t pos;
414
517
  int error;
415
518
 
416
- if ((error = normalize_name(name, &key)) < 0)
519
+ if (!h->readonly && ((error = config_refresh(cfg)) < 0))
417
520
  return error;
418
521
 
419
- pos = git_strmap_lookup_index(b->values, key);
420
- git__free(key);
421
-
422
- if (!git_strmap_valid_index(b->values, pos))
423
- return GIT_ENOTFOUND;
424
-
425
- var = git_strmap_value_at(b->values, pos);
426
-
427
- if (regex_str != NULL) {
428
- regex_t regex;
429
- int result;
522
+ map = refcounted_strmap_take(h);
523
+ values = map->values;
430
524
 
431
- /* regex matching; build the regex */
432
- result = regcomp(&regex, regex_str, REG_EXTENDED);
433
- if (result < 0) {
434
- giterr_set_regex(&regex, result);
435
- regfree(&regex);
436
- return -1;
437
- }
438
-
439
- /* and throw the callback only on the variables that
440
- * match the regex */
441
- do {
442
- if (regexec(&regex, var->entry->value, 0, NULL, 0) == 0) {
443
- /* early termination by the user is not an error;
444
- * just break and return successfully */
445
- if (fn(var->entry, data) < 0)
446
- break;
447
- }
448
-
449
- var = var->next;
450
- } while (var != NULL);
451
- regfree(&regex);
452
- } else {
453
- /* no regex; go through all the variables */
454
- do {
455
- /* early termination by the user is not an error;
456
- * just break and return successfully */
457
- if (fn(var->entry, data) < 0)
458
- break;
525
+ pos = git_strmap_lookup_index(values, key);
459
526
 
460
- var = var->next;
461
- } while (var != NULL);
527
+ /* no error message; the config system will write one */
528
+ if (!git_strmap_valid_index(values, pos)) {
529
+ refcounted_strmap_free(map);
530
+ return GIT_ENOTFOUND;
462
531
  }
463
532
 
533
+ var = git_strmap_value_at(values, pos);
534
+ while (var->next)
535
+ var = var->next;
536
+
537
+ refcounted_strmap_free(map);
538
+ *out = var->entry;
464
539
  return 0;
465
540
  }
466
541
 
467
542
  static int config_set_multivar(
468
543
  git_config_backend *cfg, const char *name, const char *regexp, const char *value)
469
544
  {
470
- int replaced = 0;
471
- cvar_t *var, *newvar;
472
545
  diskfile_backend *b = (diskfile_backend *)cfg;
546
+ refcounted_strmap *map;
547
+ git_strmap *values;
473
548
  char *key;
474
549
  regex_t preg;
475
550
  int result;
@@ -477,65 +552,36 @@ static int config_set_multivar(
477
552
 
478
553
  assert(regexp);
479
554
 
480
- if ((result = normalize_name(name, &key)) < 0)
555
+ if ((result = git_config__normalize_name(name, &key)) < 0)
481
556
  return result;
482
557
 
483
- pos = git_strmap_lookup_index(b->values, key);
484
- if (!git_strmap_valid_index(b->values, pos)) {
558
+ map = refcounted_strmap_take(&b->header);
559
+ values = b->header.values->values;
560
+
561
+ pos = git_strmap_lookup_index(values, key);
562
+ if (!git_strmap_valid_index(values, pos)) {
485
563
  /* If we don't have it, behave like a normal set */
486
564
  result = config_set(cfg, name, value);
565
+ refcounted_strmap_free(map);
487
566
  git__free(key);
488
567
  return result;
489
568
  }
490
569
 
491
- var = git_strmap_value_at(b->values, pos);
492
-
493
570
  result = regcomp(&preg, regexp, REG_EXTENDED);
494
571
  if (result < 0) {
495
- git__free(key);
496
572
  giterr_set_regex(&preg, result);
497
- regfree(&preg);
498
- return -1;
573
+ result = -1;
574
+ goto out;
499
575
  }
500
576
 
501
- for (;;) {
502
- if (regexec(&preg, var->entry->value, 0, NULL, 0) == 0) {
503
- char *tmp = git__strdup(value);
504
- GITERR_CHECK_ALLOC(tmp);
505
-
506
- git__free((void *)var->entry->value);
507
- var->entry->value = tmp;
508
- replaced = 1;
509
- }
577
+ /* If we do have it, set call config_write() and reload */
578
+ if ((result = config_write(b, key, &preg, value)) < 0)
579
+ goto out;
510
580
 
511
- if (var->next == NULL)
512
- break;
513
-
514
- var = var->next;
515
- }
516
-
517
- /* If we've reached the end of the variables and we haven't found it yet, we need to append it */
518
- if (!replaced) {
519
- newvar = git__malloc(sizeof(cvar_t));
520
- GITERR_CHECK_ALLOC(newvar);
521
- memset(newvar, 0x0, sizeof(cvar_t));
522
- newvar->entry = git__malloc(sizeof(git_config_entry));
523
- GITERR_CHECK_ALLOC(newvar->entry);
524
- memset(newvar->entry, 0x0, sizeof(git_config_entry));
525
-
526
- newvar->entry->name = git__strdup(var->entry->name);
527
- GITERR_CHECK_ALLOC(newvar->entry->name);
528
-
529
- newvar->entry->value = git__strdup(value);
530
- GITERR_CHECK_ALLOC(newvar->entry->value);
531
-
532
- newvar->entry->level = var->entry->level;
533
-
534
- var->next = newvar;
535
- }
536
-
537
- result = config_write(b, key, &preg, value);
581
+ result = config_refresh(cfg);
538
582
 
583
+ out:
584
+ refcounted_strmap_free(map);
539
585
  git__free(key);
540
586
  regfree(&preg);
541
587
 
@@ -546,36 +592,92 @@ static int config_delete(git_config_backend *cfg, const char *name)
546
592
  {
547
593
  cvar_t *var;
548
594
  diskfile_backend *b = (diskfile_backend *)cfg;
595
+ refcounted_strmap *map; git_strmap *values;
549
596
  char *key;
550
597
  int result;
551
598
  khiter_t pos;
552
599
 
553
- if ((result = normalize_name(name, &key)) < 0)
600
+ if ((result = git_config__normalize_name(name, &key)) < 0)
554
601
  return result;
555
602
 
556
- pos = git_strmap_lookup_index(b->values, key);
603
+ map = refcounted_strmap_take(&b->header);
604
+ values = b->header.values->values;
605
+
606
+ pos = git_strmap_lookup_index(values, key);
557
607
  git__free(key);
558
608
 
559
- if (!git_strmap_valid_index(b->values, pos)) {
609
+ if (!git_strmap_valid_index(values, pos)) {
610
+ refcounted_strmap_free(map);
560
611
  giterr_set(GITERR_CONFIG, "Could not find key '%s' to delete", name);
561
612
  return GIT_ENOTFOUND;
562
613
  }
563
614
 
564
- var = git_strmap_value_at(b->values, pos);
615
+ var = git_strmap_value_at(values, pos);
616
+ refcounted_strmap_free(map);
565
617
 
566
618
  if (var->next != NULL) {
567
619
  giterr_set(GITERR_CONFIG, "Cannot delete multivar with a single delete");
568
620
  return -1;
569
621
  }
570
622
 
571
- git_strmap_delete_at(b->values, pos);
623
+ if ((result = config_write(b, var->entry->name, NULL, NULL)) < 0)
624
+ return result;
625
+
626
+ return config_refresh(cfg);
627
+ }
628
+
629
+ static int config_delete_multivar(git_config_backend *cfg, const char *name, const char *regexp)
630
+ {
631
+ diskfile_backend *b = (diskfile_backend *)cfg;
632
+ refcounted_strmap *map;
633
+ git_strmap *values;
634
+ char *key;
635
+ regex_t preg;
636
+ int result;
637
+ khiter_t pos;
638
+
639
+ if ((result = git_config__normalize_name(name, &key)) < 0)
640
+ return result;
641
+
642
+ map = refcounted_strmap_take(&b->header);
643
+ values = b->header.values->values;
572
644
 
573
- result = config_write(b, var->entry->name, NULL, NULL);
645
+ pos = git_strmap_lookup_index(values, key);
574
646
 
575
- cvar_free(var);
647
+ if (!git_strmap_valid_index(values, pos)) {
648
+ refcounted_strmap_free(map);
649
+ git__free(key);
650
+ giterr_set(GITERR_CONFIG, "Could not find key '%s' to delete", name);
651
+ return GIT_ENOTFOUND;
652
+ }
653
+
654
+ refcounted_strmap_free(map);
655
+
656
+ result = regcomp(&preg, regexp, REG_EXTENDED);
657
+ if (result < 0) {
658
+ giterr_set_regex(&preg, result);
659
+ result = -1;
660
+ goto out;
661
+ }
662
+
663
+ if ((result = config_write(b, key, &preg, NULL)) < 0)
664
+ goto out;
665
+
666
+ result = config_refresh(cfg);
667
+
668
+ out:
669
+ git__free(key);
670
+ regfree(&preg);
576
671
  return result;
577
672
  }
578
673
 
674
+ static int config_snapshot(git_config_backend **out, git_config_backend *in)
675
+ {
676
+ diskfile_backend *b = (diskfile_backend *) in;
677
+
678
+ return git_config_file__snapshot(out, b);
679
+ }
680
+
579
681
  int git_config_file__ondisk(git_config_backend **out, const char *path)
580
682
  {
581
683
  diskfile_backend *backend;
@@ -583,46 +685,148 @@ int git_config_file__ondisk(git_config_backend **out, const char *path)
583
685
  backend = git__calloc(1, sizeof(diskfile_backend));
584
686
  GITERR_CHECK_ALLOC(backend);
585
687
 
586
- backend->parent.version = GIT_CONFIG_BACKEND_VERSION;
688
+ backend->header.parent.version = GIT_CONFIG_BACKEND_VERSION;
689
+ git_mutex_init(&backend->header.values_mutex);
587
690
 
588
691
  backend->file_path = git__strdup(path);
589
692
  GITERR_CHECK_ALLOC(backend->file_path);
590
693
 
591
- backend->parent.open = config_open;
592
- backend->parent.get = config_get;
593
- backend->parent.get_multivar = config_get_multivar;
594
- backend->parent.set = config_set;
595
- backend->parent.set_multivar = config_set_multivar;
596
- backend->parent.del = config_delete;
597
- backend->parent.foreach = file_foreach;
598
- backend->parent.refresh = config_refresh;
599
- backend->parent.free = backend_free;
694
+ backend->header.parent.open = config_open;
695
+ backend->header.parent.get = config_get;
696
+ backend->header.parent.set = config_set;
697
+ backend->header.parent.set_multivar = config_set_multivar;
698
+ backend->header.parent.del = config_delete;
699
+ backend->header.parent.del_multivar = config_delete_multivar;
700
+ backend->header.parent.iterator = config_iterator_new;
701
+ backend->header.parent.refresh = config_refresh;
702
+ backend->header.parent.snapshot = config_snapshot;
703
+ backend->header.parent.free = backend_free;
704
+
705
+ *out = (git_config_backend *)backend;
706
+
707
+ return 0;
708
+ }
709
+
710
+ static int config_set_readonly(git_config_backend *cfg, const char *name, const char *value)
711
+ {
712
+ GIT_UNUSED(cfg);
713
+ GIT_UNUSED(name);
714
+ GIT_UNUSED(value);
715
+
716
+ return config_error_readonly();
717
+ }
718
+
719
+ static int config_set_multivar_readonly(
720
+ git_config_backend *cfg, const char *name, const char *regexp, const char *value)
721
+ {
722
+ GIT_UNUSED(cfg);
723
+ GIT_UNUSED(name);
724
+ GIT_UNUSED(regexp);
725
+ GIT_UNUSED(value);
726
+
727
+ return config_error_readonly();
728
+ }
729
+
730
+ static int config_delete_multivar_readonly(git_config_backend *cfg, const char *name, const char *regexp)
731
+ {
732
+ GIT_UNUSED(cfg);
733
+ GIT_UNUSED(name);
734
+ GIT_UNUSED(regexp);
735
+
736
+ return config_error_readonly();
737
+ }
738
+
739
+ static int config_delete_readonly(git_config_backend *cfg, const char *name)
740
+ {
741
+ GIT_UNUSED(cfg);
742
+ GIT_UNUSED(name);
743
+
744
+ return config_error_readonly();
745
+ }
746
+
747
+ static int config_refresh_readonly(git_config_backend *cfg)
748
+ {
749
+ GIT_UNUSED(cfg);
750
+
751
+ return config_error_readonly();
752
+ }
753
+
754
+ static void backend_readonly_free(git_config_backend *_backend)
755
+ {
756
+ diskfile_backend *backend = (diskfile_backend *)_backend;
757
+
758
+ if (backend == NULL)
759
+ return;
760
+
761
+ refcounted_strmap_free(backend->header.values);
762
+ git_mutex_free(&backend->header.values_mutex);
763
+ git__free(backend);
764
+ }
765
+
766
+ static int config_readonly_open(git_config_backend *cfg, git_config_level_t level)
767
+ {
768
+ diskfile_readonly_backend *b = (diskfile_readonly_backend *) cfg;
769
+ diskfile_backend *src = b->snapshot_from;
770
+ refcounted_strmap *src_map;
771
+
772
+ /* We're just copying data, don't care about the level */
773
+ GIT_UNUSED(level);
774
+
775
+ src_map = refcounted_strmap_take(&src->header);
776
+ b->header.values = src_map;
777
+
778
+ return 0;
779
+ }
780
+
781
+ int git_config_file__snapshot(git_config_backend **out, diskfile_backend *in)
782
+ {
783
+ diskfile_readonly_backend *backend;
784
+
785
+ backend = git__calloc(1, sizeof(diskfile_readonly_backend));
786
+ GITERR_CHECK_ALLOC(backend);
787
+
788
+ backend->header.parent.version = GIT_CONFIG_BACKEND_VERSION;
789
+ git_mutex_init(&backend->header.values_mutex);
790
+
791
+ backend->snapshot_from = in;
792
+
793
+ backend->header.readonly = 1;
794
+ backend->header.parent.version = GIT_CONFIG_BACKEND_VERSION;
795
+ backend->header.parent.open = config_readonly_open;
796
+ backend->header.parent.get = config_get;
797
+ backend->header.parent.set = config_set_readonly;
798
+ backend->header.parent.set_multivar = config_set_multivar_readonly;
799
+ backend->header.parent.del = config_delete_readonly;
800
+ backend->header.parent.del_multivar = config_delete_multivar_readonly;
801
+ backend->header.parent.iterator = config_iterator_new;
802
+ backend->header.parent.refresh = config_refresh_readonly;
803
+ backend->header.parent.free = backend_readonly_free;
600
804
 
601
805
  *out = (git_config_backend *)backend;
602
806
 
603
807
  return 0;
604
808
  }
605
809
 
606
- static int cfg_getchar_raw(diskfile_backend *cfg)
810
+ static int reader_getchar_raw(struct reader *reader)
607
811
  {
608
812
  int c;
609
813
 
610
- c = *cfg->reader.read_ptr++;
814
+ c = *reader->read_ptr++;
611
815
 
612
816
  /*
613
817
  Win 32 line breaks: if we find a \r\n sequence,
614
818
  return only the \n as a newline
615
819
  */
616
- if (c == '\r' && *cfg->reader.read_ptr == '\n') {
617
- cfg->reader.read_ptr++;
820
+ if (c == '\r' && *reader->read_ptr == '\n') {
821
+ reader->read_ptr++;
618
822
  c = '\n';
619
823
  }
620
824
 
621
825
  if (c == '\n')
622
- cfg->reader.line_number++;
826
+ reader->line_number++;
623
827
 
624
828
  if (c == 0) {
625
- cfg->reader.eof = 1;
829
+ reader->eof = 1;
626
830
  c = '\n';
627
831
  }
628
832
 
@@ -632,21 +836,23 @@ static int cfg_getchar_raw(diskfile_backend *cfg)
632
836
  #define SKIP_WHITESPACE (1 << 1)
633
837
  #define SKIP_COMMENTS (1 << 2)
634
838
 
635
- static int cfg_getchar(diskfile_backend *cfg_file, int flags)
839
+ static int reader_getchar(struct reader *reader, int flags)
636
840
  {
637
841
  const int skip_whitespace = (flags & SKIP_WHITESPACE);
638
842
  const int skip_comments = (flags & SKIP_COMMENTS);
639
843
  int c;
640
844
 
641
- assert(cfg_file->reader.read_ptr);
845
+ assert(reader->read_ptr);
642
846
 
643
- do c = cfg_getchar_raw(cfg_file);
644
- while (skip_whitespace && git__isspace(c) &&
645
- !cfg_file->reader.eof);
847
+ do {
848
+ c = reader_getchar_raw(reader);
849
+ } while (skip_whitespace && git__isspace(c) &&
850
+ !reader->eof);
646
851
 
647
852
  if (skip_comments && (c == '#' || c == ';')) {
648
- do c = cfg_getchar_raw(cfg_file);
649
- while (c != '\n');
853
+ do {
854
+ c = reader_getchar_raw(reader);
855
+ } while (c != '\n');
650
856
  }
651
857
 
652
858
  return c;
@@ -655,23 +861,23 @@ static int cfg_getchar(diskfile_backend *cfg_file, int flags)
655
861
  /*
656
862
  * Read the next char, but don't move the reading pointer.
657
863
  */
658
- static int cfg_peek(diskfile_backend *cfg, int flags)
864
+ static int reader_peek(struct reader *reader, int flags)
659
865
  {
660
866
  void *old_read_ptr;
661
867
  int old_lineno, old_eof;
662
868
  int ret;
663
869
 
664
- assert(cfg->reader.read_ptr);
870
+ assert(reader->read_ptr);
665
871
 
666
- old_read_ptr = cfg->reader.read_ptr;
667
- old_lineno = cfg->reader.line_number;
668
- old_eof = cfg->reader.eof;
872
+ old_read_ptr = reader->read_ptr;
873
+ old_lineno = reader->line_number;
874
+ old_eof = reader->eof;
669
875
 
670
- ret = cfg_getchar(cfg, flags);
876
+ ret = reader_getchar(reader, flags);
671
877
 
672
- cfg->reader.read_ptr = old_read_ptr;
673
- cfg->reader.line_number = old_lineno;
674
- cfg->reader.eof = old_eof;
878
+ reader->read_ptr = old_read_ptr;
879
+ reader->line_number = old_lineno;
880
+ reader->eof = old_eof;
675
881
 
676
882
  return ret;
677
883
  }
@@ -679,13 +885,13 @@ static int cfg_peek(diskfile_backend *cfg, int flags)
679
885
  /*
680
886
  * Read and consume a line, returning it in newly-allocated memory.
681
887
  */
682
- static char *cfg_readline(diskfile_backend *cfg, bool skip_whitespace)
888
+ static char *reader_readline(struct reader *reader, bool skip_whitespace)
683
889
  {
684
890
  char *line = NULL;
685
891
  char *line_src, *line_end;
686
892
  size_t line_len;
687
893
 
688
- line_src = cfg->reader.read_ptr;
894
+ line_src = reader->read_ptr;
689
895
 
690
896
  if (skip_whitespace) {
691
897
  /* Skip empty empty lines */
@@ -714,10 +920,10 @@ static char *cfg_readline(diskfile_backend *cfg, bool skip_whitespace)
714
920
  line_end++;
715
921
 
716
922
  if (*line_end == '\0')
717
- cfg->reader.eof = 1;
923
+ reader->eof = 1;
718
924
 
719
- cfg->reader.line_number++;
720
- cfg->reader.read_ptr = line_end;
925
+ reader->line_number++;
926
+ reader->read_ptr = line_end;
721
927
 
722
928
  return line;
723
929
  }
@@ -725,11 +931,11 @@ static char *cfg_readline(diskfile_backend *cfg, bool skip_whitespace)
725
931
  /*
726
932
  * Consume a line, without storing it anywhere
727
933
  */
728
- static void cfg_consume_line(diskfile_backend *cfg)
934
+ static void reader_consume_line(struct reader *reader)
729
935
  {
730
936
  char *line_start, *line_end;
731
937
 
732
- line_start = cfg->reader.read_ptr;
938
+ line_start = reader->read_ptr;
733
939
  line_end = strchr(line_start, '\n');
734
940
  /* No newline at EOF */
735
941
  if(line_end == NULL){
@@ -740,10 +946,10 @@ static void cfg_consume_line(diskfile_backend *cfg)
740
946
  line_end++;
741
947
 
742
948
  if (*line_end == '\0')
743
- cfg->reader.eof = 1;
949
+ reader->eof = 1;
744
950
 
745
- cfg->reader.line_number++;
746
- cfg->reader.read_ptr = line_end;
951
+ reader->line_number++;
952
+ reader->read_ptr = line_end;
747
953
  }
748
954
 
749
955
  GIT_INLINE(int) config_keychar(int c)
@@ -751,12 +957,11 @@ GIT_INLINE(int) config_keychar(int c)
751
957
  return isalnum(c) || c == '-';
752
958
  }
753
959
 
754
- static int parse_section_header_ext(diskfile_backend *cfg, const char *line, const char *base_name, char **section_name)
960
+ static int parse_section_header_ext(struct reader *reader, const char *line, const char *base_name, char **section_name)
755
961
  {
756
962
  int c, rpos;
757
963
  char *first_quote, *last_quote;
758
964
  git_buf buf = GIT_BUF_INIT;
759
- int quote_marks;
760
965
  /*
761
966
  * base_name is what came before the space. We should be at the
762
967
  * first quotation mark, except for now, line isn't being kept in
@@ -767,7 +972,7 @@ static int parse_section_header_ext(diskfile_backend *cfg, const char *line, con
767
972
  last_quote = strrchr(line, '"');
768
973
 
769
974
  if (last_quote - first_quote == 0) {
770
- set_parse_error(cfg, 0, "Missing closing quotation mark in section header");
975
+ set_parse_error(reader, 0, "Missing closing quotation mark in section header");
771
976
  return -1;
772
977
  }
773
978
 
@@ -775,37 +980,30 @@ static int parse_section_header_ext(diskfile_backend *cfg, const char *line, con
775
980
  git_buf_printf(&buf, "%s.", base_name);
776
981
 
777
982
  rpos = 0;
778
- quote_marks = 0;
779
983
 
780
984
  line = first_quote;
781
- c = line[rpos++];
985
+ c = line[++rpos];
782
986
 
783
987
  /*
784
988
  * At the end of each iteration, whatever is stored in c will be
785
989
  * added to the string. In case of error, jump to out
786
990
  */
787
991
  do {
788
- if (quote_marks == 2) {
789
- set_parse_error(cfg, rpos, "Unexpected text after closing quotes");
992
+
993
+ switch (c) {
994
+ case 0:
995
+ set_parse_error(reader, 0, "Unexpected end-of-line in section header");
790
996
  git_buf_free(&buf);
791
997
  return -1;
792
- }
793
998
 
794
- switch (c) {
795
999
  case '"':
796
- ++quote_marks;
797
- continue;
1000
+ goto end_parse;
798
1001
 
799
1002
  case '\\':
800
- c = line[rpos++];
801
-
802
- switch (c) {
803
- case '"':
804
- case '\\':
805
- break;
1003
+ c = line[++rpos];
806
1004
 
807
- default:
808
- set_parse_error(cfg, rpos, "Unsupported escape sequence");
1005
+ if (c == 0) {
1006
+ set_parse_error(reader, rpos, "Unexpected end-of-line in section header");
809
1007
  git_buf_free(&buf);
810
1008
  return -1;
811
1009
  }
@@ -814,29 +1012,37 @@ static int parse_section_header_ext(diskfile_backend *cfg, const char *line, con
814
1012
  break;
815
1013
  }
816
1014
 
817
- git_buf_putc(&buf, c);
818
- } while ((c = line[rpos++]) != ']');
1015
+ git_buf_putc(&buf, (char)c);
1016
+ c = line[++rpos];
1017
+ } while (line + rpos < last_quote);
1018
+
1019
+ end_parse:
1020
+ if (line[rpos] != '"' || line[rpos + 1] != ']') {
1021
+ set_parse_error(reader, rpos, "Unexpected text after closing quotes");
1022
+ git_buf_free(&buf);
1023
+ return -1;
1024
+ }
819
1025
 
820
1026
  *section_name = git_buf_detach(&buf);
821
1027
  return 0;
822
1028
  }
823
1029
 
824
- static int parse_section_header(diskfile_backend *cfg, char **section_out)
1030
+ static int parse_section_header(struct reader *reader, char **section_out)
825
1031
  {
826
1032
  char *name, *name_end;
827
1033
  int name_length, c, pos;
828
1034
  int result;
829
1035
  char *line;
830
1036
 
831
- line = cfg_readline(cfg, true);
1037
+ line = reader_readline(reader, true);
832
1038
  if (line == NULL)
833
1039
  return -1;
834
1040
 
835
1041
  /* find the end of the variable's name */
836
- name_end = strchr(line, ']');
1042
+ name_end = strrchr(line, ']');
837
1043
  if (name_end == NULL) {
838
1044
  git__free(line);
839
- set_parse_error(cfg, 0, "Missing ']' in section header");
1045
+ set_parse_error(reader, 0, "Missing ']' in section header");
840
1046
  return -1;
841
1047
  }
842
1048
 
@@ -855,14 +1061,14 @@ static int parse_section_header(diskfile_backend *cfg, char **section_out)
855
1061
  do {
856
1062
  if (git__isspace(c)){
857
1063
  name[name_length] = '\0';
858
- result = parse_section_header_ext(cfg, line, name, section_out);
1064
+ result = parse_section_header_ext(reader, line, name, section_out);
859
1065
  git__free(line);
860
1066
  git__free(name);
861
1067
  return result;
862
1068
  }
863
1069
 
864
1070
  if (!config_keychar(c) && c != '.') {
865
- set_parse_error(cfg, pos, "Unexpected character in header");
1071
+ set_parse_error(reader, pos, "Unexpected character in header");
866
1072
  goto fail_parse;
867
1073
  }
868
1074
 
@@ -871,7 +1077,7 @@ static int parse_section_header(diskfile_backend *cfg, char **section_out)
871
1077
  } while ((c = line[pos++]) != ']');
872
1078
 
873
1079
  if (line[pos - 1] != ']') {
874
- set_parse_error(cfg, pos, "Unexpected end of file");
1080
+ set_parse_error(reader, pos, "Unexpected end of file");
875
1081
  goto fail_parse;
876
1082
  }
877
1083
 
@@ -888,14 +1094,14 @@ fail_parse:
888
1094
  return -1;
889
1095
  }
890
1096
 
891
- static int skip_bom(diskfile_backend *cfg)
1097
+ static int skip_bom(struct reader *reader)
892
1098
  {
893
1099
  git_bom_t bom;
894
1100
  int bom_offset = git_buf_text_detect_bom(&bom,
895
- &cfg->reader.buffer, cfg->reader.read_ptr - cfg->reader.buffer.ptr);
1101
+ &reader->buffer, reader->read_ptr - reader->buffer.ptr);
896
1102
 
897
1103
  if (bom == GIT_BOM_UTF8)
898
- cfg->reader.read_ptr += bom_offset;
1104
+ reader->read_ptr += bom_offset;
899
1105
 
900
1106
  /* TODO: reference implementation is pretty stupid with BoM */
901
1107
 
@@ -965,49 +1171,64 @@ static int strip_comments(char *line, int in_quotes)
965
1171
  return quote_count;
966
1172
  }
967
1173
 
968
- static int config_parse(diskfile_backend *cfg_file, git_config_level_t level)
1174
+ static int included_path(git_buf *out, const char *dir, const char *path)
1175
+ {
1176
+ /* From the user's home */
1177
+ if (path[0] == '~' && path[1] == '/')
1178
+ return git_sysdir_find_global_file(out, &path[1]);
1179
+
1180
+ return git_path_join_unrooted(out, path, dir, NULL);
1181
+ }
1182
+
1183
+ static int config_parse(git_strmap *values, diskfile_backend *cfg_file, struct reader *reader, git_config_level_t level, int depth)
969
1184
  {
970
1185
  int c;
971
1186
  char *current_section = NULL;
972
1187
  char *var_name;
973
1188
  char *var_value;
974
- cvar_t *var, *existing;
1189
+ cvar_t *var;
975
1190
  git_buf buf = GIT_BUF_INIT;
976
1191
  int result = 0;
977
- khiter_t pos;
1192
+ uint32_t reader_idx;
1193
+
1194
+ if (depth >= MAX_INCLUDE_DEPTH) {
1195
+ giterr_set(GITERR_CONFIG, "Maximum config include depth reached");
1196
+ return -1;
1197
+ }
978
1198
 
1199
+ reader_idx = git_array_size(cfg_file->readers) - 1;
979
1200
  /* Initialize the reading position */
980
- cfg_file->reader.read_ptr = cfg_file->reader.buffer.ptr;
981
- cfg_file->reader.eof = 0;
1201
+ reader->read_ptr = reader->buffer.ptr;
1202
+ reader->eof = 0;
982
1203
 
983
1204
  /* If the file is empty, there's nothing for us to do */
984
- if (*cfg_file->reader.read_ptr == '\0')
1205
+ if (*reader->read_ptr == '\0')
985
1206
  return 0;
986
1207
 
987
- skip_bom(cfg_file);
1208
+ skip_bom(reader);
988
1209
 
989
- while (result == 0 && !cfg_file->reader.eof) {
1210
+ while (result == 0 && !reader->eof) {
990
1211
 
991
- c = cfg_peek(cfg_file, SKIP_WHITESPACE);
1212
+ c = reader_peek(reader, SKIP_WHITESPACE);
992
1213
 
993
1214
  switch (c) {
994
1215
  case '\n': /* EOF when peeking, set EOF in the reader to exit the loop */
995
- cfg_file->reader.eof = 1;
1216
+ reader->eof = 1;
996
1217
  break;
997
1218
 
998
1219
  case '[': /* section header, new section begins */
999
1220
  git__free(current_section);
1000
1221
  current_section = NULL;
1001
- result = parse_section_header(cfg_file, &current_section);
1222
+ result = parse_section_header(reader, &current_section);
1002
1223
  break;
1003
1224
 
1004
1225
  case ';':
1005
1226
  case '#':
1006
- cfg_consume_line(cfg_file);
1227
+ reader_consume_line(reader);
1007
1228
  break;
1008
1229
 
1009
1230
  default: /* assume variable declaration */
1010
- result = parse_variable(cfg_file, &var_name, &var_value);
1231
+ result = parse_variable(reader, &var_name, &var_value);
1011
1232
  if (result < 0)
1012
1233
  break;
1013
1234
 
@@ -1022,26 +1243,57 @@ static int config_parse(diskfile_backend *cfg_file, git_config_level_t level)
1022
1243
  git_buf_printf(&buf, "%s.%s", current_section, var_name);
1023
1244
  git__free(var_name);
1024
1245
 
1025
- if (git_buf_oom(&buf))
1246
+ if (git_buf_oom(&buf)) {
1247
+ git__free(var_value);
1026
1248
  return -1;
1249
+ }
1027
1250
 
1028
1251
  var->entry->name = git_buf_detach(&buf);
1029
1252
  var->entry->value = var_value;
1030
1253
  var->entry->level = level;
1254
+ var->included = !!depth;
1255
+
1256
+
1257
+ if ((result = append_entry(values, var)) < 0)
1258
+ break;
1259
+ else
1260
+ result = 0;
1031
1261
 
1032
1262
  /* Add or append the new config option */
1033
- pos = git_strmap_lookup_index(cfg_file->values, var->entry->name);
1034
- if (!git_strmap_valid_index(cfg_file->values, pos)) {
1035
- git_strmap_insert(cfg_file->values, var->entry->name, var, result);
1263
+ if (!git__strcmp(var->entry->name, "include.path")) {
1264
+ struct reader *r;
1265
+ git_buf path = GIT_BUF_INIT;
1266
+ char *dir;
1267
+ uint32_t index;
1268
+
1269
+ r = git_array_alloc(cfg_file->readers);
1270
+ /* The reader may have been reallocated */
1271
+ reader = git_array_get(cfg_file->readers, reader_idx);
1272
+ memset(r, 0, sizeof(struct reader));
1273
+ if ((result = git_path_dirname_r(&path, reader->file_path)) < 0)
1274
+ break;
1275
+
1276
+ /* We need to know out index in the array, as the next config_parse call may realloc */
1277
+ index = git_array_size(cfg_file->readers) - 1;
1278
+ dir = git_buf_detach(&path);
1279
+ result = included_path(&path, dir, var->entry->value);
1280
+ git__free(dir);
1281
+
1282
+ if (result < 0)
1283
+ break;
1284
+
1285
+ r->file_path = git_buf_detach(&path);
1286
+ git_buf_init(&r->buffer, 0);
1287
+ if ((result = git_futils_readbuffer_updated(&r->buffer, r->file_path, &r->file_mtime,
1288
+ &r->file_size, NULL)) < 0)
1289
+ break;
1290
+
1291
+ result = config_parse(values, cfg_file, r, level, depth+1);
1292
+ r = git_array_get(cfg_file->readers, index);
1293
+ git_buf_free(&r->buffer);
1294
+
1036
1295
  if (result < 0)
1037
1296
  break;
1038
- result = 0;
1039
- } else {
1040
- existing = git_strmap_value_at(cfg_file->values, pos);
1041
- while (existing->next != NULL) {
1042
- existing = existing->next;
1043
- }
1044
- existing->next = var;
1045
1297
  }
1046
1298
 
1047
1299
  break;
@@ -1082,6 +1334,24 @@ static int write_section(git_filebuf *file, const char *key)
1082
1334
  return result;
1083
1335
  }
1084
1336
 
1337
+ static const char *quotes_for_value(const char *value)
1338
+ {
1339
+ const char *ptr;
1340
+
1341
+ if (value[0] == ' ' || value[0] == '\0')
1342
+ return "\"";
1343
+
1344
+ for (ptr = value; *ptr; ++ptr) {
1345
+ if (*ptr == ';' || *ptr == '#')
1346
+ return "\"";
1347
+ }
1348
+
1349
+ if (ptr[-1] == ' ')
1350
+ return "\"";
1351
+
1352
+ return "";
1353
+ }
1354
+
1085
1355
  /*
1086
1356
  * This is pretty much the parsing, except we write out anything we don't have
1087
1357
  */
@@ -1089,38 +1359,44 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
1089
1359
  {
1090
1360
  int result, c;
1091
1361
  int section_matches = 0, last_section_matched = 0, preg_replaced = 0, write_trailer = 0;
1092
- const char *pre_end = NULL, *post_start = NULL, *data_start;
1362
+ const char *pre_end = NULL, *post_start = NULL, *data_start, *write_start;
1093
1363
  char *current_section = NULL, *section, *name, *ldot;
1094
1364
  git_filebuf file = GIT_FILEBUF_INIT;
1365
+ struct reader *reader = git_array_get(cfg->readers, 0);
1095
1366
 
1096
1367
  /* We need to read in our own config file */
1097
- result = git_futils_readbuffer(&cfg->reader.buffer, cfg->file_path);
1368
+ result = git_futils_readbuffer(&reader->buffer, cfg->file_path);
1098
1369
 
1099
1370
  /* Initialise the reading position */
1100
1371
  if (result == GIT_ENOTFOUND) {
1101
- cfg->reader.read_ptr = NULL;
1102
- cfg->reader.eof = 1;
1372
+ reader->read_ptr = NULL;
1373
+ reader->eof = 1;
1103
1374
  data_start = NULL;
1104
- git_buf_clear(&cfg->reader.buffer);
1375
+ git_buf_clear(&reader->buffer);
1105
1376
  } else if (result == 0) {
1106
- cfg->reader.read_ptr = cfg->reader.buffer.ptr;
1107
- cfg->reader.eof = 0;
1108
- data_start = cfg->reader.read_ptr;
1377
+ reader->read_ptr = reader->buffer.ptr;
1378
+ reader->eof = 0;
1379
+ data_start = reader->read_ptr;
1109
1380
  } else {
1110
1381
  return -1; /* OS error when reading the file */
1111
1382
  }
1112
1383
 
1384
+ write_start = data_start;
1385
+
1113
1386
  /* Lock the file */
1114
- if (git_filebuf_open(&file, cfg->file_path, 0) < 0)
1115
- return -1;
1387
+ if ((result = git_filebuf_open(
1388
+ &file, cfg->file_path, 0, GIT_CONFIG_FILE_MODE)) < 0) {
1389
+ git_buf_free(&reader->buffer);
1390
+ return result;
1391
+ }
1116
1392
 
1117
- skip_bom(cfg);
1393
+ skip_bom(reader);
1118
1394
  ldot = strrchr(key, '.');
1119
1395
  name = ldot + 1;
1120
1396
  section = git__strndup(key, ldot - key);
1121
1397
 
1122
- while (!cfg->reader.eof) {
1123
- c = cfg_peek(cfg, SKIP_WHITESPACE);
1398
+ while (!reader->eof) {
1399
+ c = reader_peek(reader, SKIP_WHITESPACE);
1124
1400
 
1125
1401
  if (c == '\0') { /* We've arrived at the end of the file */
1126
1402
  break;
@@ -1133,11 +1409,11 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
1133
1409
  * new section. If we actually want to replace it, the
1134
1410
  * default case will take care of updating them.
1135
1411
  */
1136
- pre_end = post_start = cfg->reader.read_ptr;
1412
+ pre_end = post_start = reader->read_ptr;
1137
1413
 
1138
1414
  git__free(current_section);
1139
1415
  current_section = NULL;
1140
- if (parse_section_header(cfg, &current_section) < 0)
1416
+ if (parse_section_header(reader, &current_section) < 0)
1141
1417
  goto rewrite_fail;
1142
1418
 
1143
1419
  /* Keep track of when it stops matching */
@@ -1146,7 +1422,7 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
1146
1422
  }
1147
1423
 
1148
1424
  else if (c == ';' || c == '#') {
1149
- cfg_consume_line(cfg);
1425
+ reader_consume_line(reader);
1150
1426
  }
1151
1427
 
1152
1428
  else {
@@ -1162,15 +1438,15 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
1162
1438
  */
1163
1439
  if (!section_matches) {
1164
1440
  if (!last_section_matched) {
1165
- cfg_consume_line(cfg);
1441
+ reader_consume_line(reader);
1166
1442
  continue;
1167
1443
  }
1168
1444
  } else {
1169
1445
  int has_matched = 0;
1170
1446
  char *var_name, *var_value;
1171
1447
 
1172
- pre_end = cfg->reader.read_ptr;
1173
- if (parse_variable(cfg, &var_name, &var_value) < 0)
1448
+ pre_end = reader->read_ptr;
1449
+ if (parse_variable(reader, &var_name, &var_value) < 0)
1174
1450
  goto rewrite_fail;
1175
1451
 
1176
1452
  /* First try to match the name of the variable */
@@ -1189,23 +1465,28 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
1189
1465
  if (!has_matched)
1190
1466
  continue;
1191
1467
 
1192
- post_start = cfg->reader.read_ptr;
1468
+ post_start = reader->read_ptr;
1193
1469
  }
1194
1470
 
1195
1471
  /* We've found the variable we wanted to change, so
1196
1472
  * write anything up to it */
1197
- git_filebuf_write(&file, data_start, pre_end - data_start);
1473
+ git_filebuf_write(&file, write_start, pre_end - write_start);
1198
1474
  preg_replaced = 1;
1199
1475
 
1200
1476
  /* Then replace the variable. If the value is NULL, it
1201
1477
  * means we want to delete it, so don't write anything. */
1202
1478
  if (value != NULL) {
1203
- git_filebuf_printf(&file, "\t%s = %s\n", name, value);
1479
+ const char *q = quotes_for_value(value);
1480
+ git_filebuf_printf(&file, "\t%s = %s%s%s\n", name, q, value, q);
1204
1481
  }
1205
1482
 
1206
- /* multiline variable? we need to keep reading lines to match */
1207
- if (preg != NULL) {
1208
- data_start = post_start;
1483
+ /*
1484
+ * If we have a multivar, we should keep looking for entries,
1485
+ * but only if we're in the right section. Otherwise we'll end up
1486
+ * looping on the edge of a matching and a non-matching section.
1487
+ */
1488
+ if (section_matches && preg != NULL) {
1489
+ write_start = post_start;
1209
1490
  continue;
1210
1491
  }
1211
1492
 
@@ -1232,12 +1513,14 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
1232
1513
  */
1233
1514
  if (write_trailer) {
1234
1515
  /* Write out rest of the file */
1235
- git_filebuf_write(&file, post_start, cfg->reader.buffer.size - (post_start - data_start));
1516
+ git_filebuf_write(&file, post_start, reader->buffer.size - (post_start - data_start));
1236
1517
  } else {
1237
1518
  if (preg_replaced) {
1238
- git_filebuf_printf(&file, "\n%s", data_start);
1519
+ git_filebuf_printf(&file, "\n%s", write_start);
1239
1520
  } else {
1240
- git_filebuf_write(&file, cfg->reader.buffer.ptr, cfg->reader.buffer.size);
1521
+ const char *q;
1522
+
1523
+ git_filebuf_write(&file, reader->buffer.ptr, reader->buffer.size);
1241
1524
 
1242
1525
  /* And now if we just need to add a variable */
1243
1526
  if (!section_matches && write_section(&file, section) < 0)
@@ -1248,15 +1531,16 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
1248
1531
  * this, but instead we'll handle it gracefully with an error. */
1249
1532
  if (value == NULL) {
1250
1533
  giterr_set(GITERR_CONFIG,
1251
- "Race condition when writing a config file (a cvar has been removed)");
1534
+ "race condition when writing a config file (a cvar has been removed)");
1252
1535
  goto rewrite_fail;
1253
1536
  }
1254
1537
 
1255
1538
  /* If we are here, there is at least a section line */
1256
- if (cfg->reader.buffer.size > 0 && *(cfg->reader.buffer.ptr + cfg->reader.buffer.size - 1) != '\n')
1539
+ if (reader->buffer.size > 0 && *(reader->buffer.ptr + reader->buffer.size - 1) != '\n')
1257
1540
  git_filebuf_write(&file, "\n", 1);
1258
1541
 
1259
- git_filebuf_printf(&file, "\t%s = %s\n", name, value);
1542
+ q = quotes_for_value(value);
1543
+ git_filebuf_printf(&file, "\t%s = %s%s%s\n", name, q, value, q);
1260
1544
  }
1261
1545
  }
1262
1546
 
@@ -1264,10 +1548,10 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
1264
1548
  git__free(current_section);
1265
1549
 
1266
1550
  /* refresh stats - if this errors, then commit will error too */
1267
- (void)git_filebuf_stats(&cfg->file_mtime, &cfg->file_size, &file);
1551
+ (void)git_filebuf_stats(&reader->file_mtime, &reader->file_size, &file);
1268
1552
 
1269
- result = git_filebuf_commit(&file, GIT_CONFIG_FILE_MODE);
1270
- git_buf_free(&cfg->reader.buffer);
1553
+ result = git_filebuf_commit(&file);
1554
+ git_buf_free(&reader->buffer);
1271
1555
 
1272
1556
  return result;
1273
1557
 
@@ -1276,7 +1560,7 @@ rewrite_fail:
1276
1560
  git__free(current_section);
1277
1561
 
1278
1562
  git_filebuf_cleanup(&file);
1279
- git_buf_free(&cfg->reader.buffer);
1563
+ git_buf_free(&reader->buffer);
1280
1564
  return -1;
1281
1565
  }
1282
1566
 
@@ -1293,6 +1577,9 @@ static char *escape_value(const char *ptr)
1293
1577
  assert(ptr);
1294
1578
 
1295
1579
  len = strlen(ptr);
1580
+ if (!len)
1581
+ return git__calloc(1, sizeof(char));
1582
+
1296
1583
  git_buf_grow(&buf, len);
1297
1584
 
1298
1585
  while (*ptr != '\0') {
@@ -1365,19 +1652,19 @@ static int is_multiline_var(const char *str)
1365
1652
  return (end > str) && (count & 1);
1366
1653
  }
1367
1654
 
1368
- static int parse_multiline_variable(diskfile_backend *cfg, git_buf *value, int in_quotes)
1655
+ static int parse_multiline_variable(struct reader *reader, git_buf *value, int in_quotes)
1369
1656
  {
1370
1657
  char *line = NULL, *proc_line = NULL;
1371
1658
  int quote_count;
1372
1659
 
1373
1660
  /* Check that the next line exists */
1374
- line = cfg_readline(cfg, false);
1661
+ line = reader_readline(reader, false);
1375
1662
  if (line == NULL)
1376
1663
  return -1;
1377
1664
 
1378
1665
  /* We've reached the end of the file, there is input missing */
1379
1666
  if (line[0] == '\0') {
1380
- set_parse_error(cfg, 0, "Unexpected end of file while parsing multine var");
1667
+ set_parse_error(reader, 0, "Unexpected end of file while parsing multine var");
1381
1668
  git__free(line);
1382
1669
  return -1;
1383
1670
  }
@@ -1387,7 +1674,7 @@ static int parse_multiline_variable(diskfile_backend *cfg, git_buf *value, int i
1387
1674
  /* If it was just a comment, pretend it didn't exist */
1388
1675
  if (line[0] == '\0') {
1389
1676
  git__free(line);
1390
- return parse_multiline_variable(cfg, value, quote_count);
1677
+ return parse_multiline_variable(reader, value, quote_count);
1391
1678
  /* TODO: unbounded recursion. This **could** be exploitable */
1392
1679
  }
1393
1680
 
@@ -1395,7 +1682,7 @@ static int parse_multiline_variable(diskfile_backend *cfg, git_buf *value, int i
1395
1682
  * standard, this character **has** to be last one in the buf, with
1396
1683
  * no whitespace after it */
1397
1684
  assert(is_multiline_var(value->ptr));
1398
- git_buf_truncate(value, git_buf_len(value) - 1);
1685
+ git_buf_shorten(value, 1);
1399
1686
 
1400
1687
  proc_line = fixup_line(line, in_quotes);
1401
1688
  if (proc_line == NULL) {
@@ -1412,19 +1699,19 @@ static int parse_multiline_variable(diskfile_backend *cfg, git_buf *value, int i
1412
1699
  * keep putting stuff in the buffer
1413
1700
  */
1414
1701
  if (is_multiline_var(value->ptr))
1415
- return parse_multiline_variable(cfg, value, quote_count);
1702
+ return parse_multiline_variable(reader, value, quote_count);
1416
1703
 
1417
1704
  return 0;
1418
1705
  }
1419
1706
 
1420
- static int parse_variable(diskfile_backend *cfg, char **var_name, char **var_value)
1707
+ static int parse_variable(struct reader *reader, char **var_name, char **var_value)
1421
1708
  {
1422
1709
  const char *var_end = NULL;
1423
1710
  const char *value_start = NULL;
1424
1711
  char *line;
1425
1712
  int quote_count;
1426
1713
 
1427
- line = cfg_readline(cfg, true);
1714
+ line = reader_readline(reader, true);
1428
1715
  if (line == NULL)
1429
1716
  return -1;
1430
1717
 
@@ -1459,7 +1746,7 @@ static int parse_variable(diskfile_backend *cfg, char **var_name, char **var_val
1459
1746
  GITERR_CHECK_ALLOC(proc_line);
1460
1747
  git_buf_puts(&multi_value, proc_line);
1461
1748
  git__free(proc_line);
1462
- if (parse_multiline_variable(cfg, &multi_value, quote_count) < 0 || git_buf_oom(&multi_value)) {
1749
+ if (parse_multiline_variable(reader, &multi_value, quote_count) < 0 || git_buf_oom(&multi_value)) {
1463
1750
  git__free(*var_name);
1464
1751
  git__free(line);
1465
1752
  git_buf_free(&multi_value);