rugged 0.18.0.gh.de28323 → 0.19.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (283) hide show
  1. data/README.md +9 -4
  2. data/Rakefile +1 -1
  3. data/ext/rugged/extconf.rb +10 -0
  4. data/ext/rugged/rugged.c +153 -86
  5. data/ext/rugged/rugged.h +44 -33
  6. data/ext/rugged/rugged_blob.c +288 -60
  7. data/ext/rugged/rugged_branch.c +82 -57
  8. data/ext/rugged/rugged_commit.c +83 -86
  9. data/ext/rugged/rugged_config.c +68 -68
  10. data/ext/rugged/rugged_diff.c +509 -0
  11. data/ext/rugged/rugged_diff_delta.c +94 -0
  12. data/ext/rugged/rugged_diff_hunk.c +100 -0
  13. data/ext/rugged/rugged_diff_line.c +79 -0
  14. data/ext/rugged/rugged_diff_patch.c +169 -0
  15. data/ext/rugged/rugged_index.c +539 -8
  16. data/ext/rugged/rugged_note.c +74 -80
  17. data/ext/rugged/rugged_object.c +63 -8
  18. data/ext/rugged/rugged_reference.c +231 -145
  19. data/ext/rugged/rugged_remote.c +509 -53
  20. data/ext/rugged/rugged_repo.c +572 -236
  21. data/ext/rugged/rugged_revwalk.c +59 -36
  22. data/ext/rugged/rugged_settings.c +7 -9
  23. data/ext/rugged/rugged_signature.c +7 -11
  24. data/ext/rugged/rugged_tag.c +93 -39
  25. data/ext/rugged/rugged_tree.c +321 -58
  26. data/lib/rugged.rb +1 -0
  27. data/lib/rugged/commit.rb +16 -1
  28. data/lib/rugged/console.rb +9 -0
  29. data/lib/rugged/diff.rb +19 -0
  30. data/lib/rugged/diff/delta.rb +54 -0
  31. data/lib/rugged/diff/hunk.rb +23 -0
  32. data/lib/rugged/diff/line.rb +29 -0
  33. data/lib/rugged/diff/patch.rb +28 -0
  34. data/lib/rugged/repository.rb +36 -39
  35. data/lib/rugged/version.rb +1 -1
  36. data/test/blob_test.rb +308 -1
  37. data/test/branch_test.rb +7 -0
  38. data/test/commit_test.rb +7 -10
  39. data/test/coverage/cover.rb +9 -1
  40. data/test/diff_test.rb +777 -0
  41. data/test/fixtures/archive.tar.gz +0 -0
  42. data/test/fixtures/attr/attr0 +1 -0
  43. data/test/fixtures/attr/attr1 +29 -0
  44. data/test/fixtures/attr/attr2 +21 -0
  45. data/test/fixtures/attr/attr3 +4 -0
  46. data/test/fixtures/attr/binfile +1 -0
  47. data/test/fixtures/attr/dir/file +0 -0
  48. data/test/fixtures/attr/file +1 -0
  49. data/test/fixtures/attr/gitattributes +29 -0
  50. data/test/fixtures/attr/gitignore +2 -0
  51. data/test/fixtures/attr/ign +1 -0
  52. data/test/fixtures/attr/macro_bad +1 -0
  53. data/test/fixtures/attr/macro_test +1 -0
  54. data/test/fixtures/attr/root_test1 +1 -0
  55. data/test/fixtures/attr/root_test2 +6 -0
  56. data/test/fixtures/attr/root_test3 +19 -0
  57. data/test/fixtures/attr/root_test4.txt +14 -0
  58. data/test/fixtures/attr/sub/abc +37 -0
  59. data/test/fixtures/attr/sub/dir/file +0 -0
  60. data/test/fixtures/attr/sub/file +1 -0
  61. data/test/fixtures/attr/sub/ign/file +1 -0
  62. data/test/fixtures/attr/sub/ign/sub/file +1 -0
  63. data/test/fixtures/attr/sub/sub/dir +0 -0
  64. data/test/fixtures/attr/sub/sub/file +1 -0
  65. data/test/fixtures/attr/sub/sub/subsub.txt +1 -0
  66. data/test/fixtures/attr/sub/subdir_test1 +2 -0
  67. data/test/fixtures/attr/sub/subdir_test2.txt +1 -0
  68. data/test/fixtures/diff/another.txt +38 -0
  69. data/test/fixtures/diff/readme.txt +36 -0
  70. data/test/fixtures/mergedrepo/conflicts-one.txt +5 -0
  71. data/test/fixtures/mergedrepo/conflicts-two.txt +5 -0
  72. data/test/fixtures/mergedrepo/one.txt +10 -0
  73. data/test/fixtures/mergedrepo/two.txt +12 -0
  74. data/test/fixtures/status/current_file +1 -0
  75. data/test/fixtures/status/ignored_file +1 -0
  76. data/test/fixtures/status/modified_file +2 -0
  77. data/test/fixtures/status/new_file +1 -0
  78. data/test/fixtures/status/staged_changes +2 -0
  79. data/test/fixtures/status/staged_changes_modified_file +3 -0
  80. data/test/fixtures/status/staged_delete_modified_file +1 -0
  81. data/test/fixtures/status/staged_new_file +1 -0
  82. data/test/fixtures/status/staged_new_file_modified_file +2 -0
  83. data/test/fixtures/status/subdir.txt +2 -0
  84. data/test/fixtures/status/subdir/current_file +1 -0
  85. data/test/fixtures/status/subdir/modified_file +2 -0
  86. data/test/fixtures/status/subdir/new_file +1 -0
  87. data/test/fixtures/status//350/277/231 +1 -0
  88. data/test/fixtures/testrepo.git/config +5 -0
  89. data/test/fixtures/testrepo.git/objects/77/71329dfa3002caf8c61a0ceb62a31d09023f37 +0 -0
  90. data/test/fixtures/text_file.md +464 -0
  91. data/test/fixtures/unsymlinked.git/HEAD +1 -0
  92. data/test/fixtures/unsymlinked.git/config +6 -0
  93. data/test/fixtures/unsymlinked.git/description +1 -0
  94. data/test/fixtures/unsymlinked.git/info/exclude +2 -0
  95. data/test/fixtures/unsymlinked.git/objects/08/8b64704e0d6b8bd061dea879418cb5442a3fbf +0 -0
  96. data/test/fixtures/unsymlinked.git/objects/13/a5e939bca25940c069fd2169d993dba328e30b +0 -0
  97. data/test/fixtures/unsymlinked.git/objects/19/bf568e59e3a0b363cafb4106226e62d4a4c41c +0 -0
  98. data/test/fixtures/unsymlinked.git/objects/58/1fadd35b4cf320d102a152f918729011604773 +0 -0
  99. data/test/fixtures/unsymlinked.git/objects/5c/87b6791e8b13da658a14d1ef7e09b5dc3bac8c +0 -0
  100. data/test/fixtures/unsymlinked.git/objects/6f/e5f5398af85fb3de8a6aba0339b6d3bfa26a27 +0 -0
  101. data/test/fixtures/unsymlinked.git/objects/7f/ccd75616ec188b8f1b23d67506a334cc34a49d +0 -0
  102. data/test/fixtures/unsymlinked.git/objects/80/6999882bf91d24241e4077906b9017605eb1f3 +0 -0
  103. data/test/fixtures/unsymlinked.git/objects/83/7d176303c5005505ec1e4a30231c40930c0230 +0 -0
  104. data/test/fixtures/unsymlinked.git/objects/a8/595ccca04f40818ae0155c8f9c77a230e597b6 +2 -0
  105. data/test/fixtures/unsymlinked.git/objects/cf/8f1cf5cce859c438d6cc067284cb5e161206e7 +0 -0
  106. data/test/fixtures/unsymlinked.git/objects/d5/278d05c8607ec420bfee4cf219fbc0eeebfd6a +0 -0
  107. data/test/fixtures/unsymlinked.git/objects/f4/e16fb76536591a41454194058d048d8e4dd2e9 +0 -0
  108. data/test/fixtures/unsymlinked.git/objects/f9/e65619d93fdf2673882e0a261c5e93b1a84006 +0 -0
  109. data/test/fixtures/unsymlinked.git/refs/heads/exe-file +1 -0
  110. data/test/fixtures/unsymlinked.git/refs/heads/master +1 -0
  111. data/test/fixtures/unsymlinked.git/refs/heads/reg-file +1 -0
  112. data/test/index_test.rb +120 -0
  113. data/test/reference_test.rb +38 -3
  114. data/test/remote_test.rb +224 -3
  115. data/test/repo_reset_test.rb +2 -0
  116. data/test/repo_test.rb +147 -10
  117. data/test/test_helper.rb +5 -2
  118. data/vendor/libgit2/include/git2/attr.h +3 -3
  119. data/vendor/libgit2/include/git2/blob.h +11 -17
  120. data/vendor/libgit2/include/git2/branch.h +3 -2
  121. data/vendor/libgit2/include/git2/checkout.h +7 -0
  122. data/vendor/libgit2/include/git2/clone.h +3 -0
  123. data/vendor/libgit2/include/git2/commit.h +61 -66
  124. data/vendor/libgit2/include/git2/common.h +73 -42
  125. data/vendor/libgit2/include/git2/config.h +57 -71
  126. data/vendor/libgit2/include/git2/cred_helpers.h +2 -2
  127. data/vendor/libgit2/include/git2/diff.h +179 -30
  128. data/vendor/libgit2/include/git2/errors.h +3 -3
  129. data/vendor/libgit2/include/git2/index.h +225 -146
  130. data/vendor/libgit2/include/git2/indexer.h +2 -22
  131. data/vendor/libgit2/include/git2/inttypes.h +9 -9
  132. data/vendor/libgit2/include/git2/merge.h +123 -5
  133. data/vendor/libgit2/include/git2/odb.h +59 -38
  134. data/vendor/libgit2/include/git2/odb_backend.h +45 -104
  135. data/vendor/libgit2/include/git2/oid.h +30 -19
  136. data/vendor/libgit2/include/git2/pack.h +21 -3
  137. data/vendor/libgit2/include/git2/refdb.h +0 -35
  138. data/vendor/libgit2/include/git2/refs.h +93 -31
  139. data/vendor/libgit2/include/git2/refspec.h +17 -0
  140. data/vendor/libgit2/include/git2/remote.h +60 -20
  141. data/vendor/libgit2/include/git2/repository.h +48 -70
  142. data/vendor/libgit2/include/git2/reset.h +3 -3
  143. data/vendor/libgit2/include/git2/revparse.h +22 -0
  144. data/vendor/libgit2/include/git2/stash.h +1 -1
  145. data/vendor/libgit2/include/git2/status.h +131 -56
  146. data/vendor/libgit2/include/git2/strarray.h +2 -2
  147. data/vendor/libgit2/include/git2/submodule.h +16 -16
  148. data/vendor/libgit2/include/git2/sys/commit.h +46 -0
  149. data/vendor/libgit2/include/git2/sys/config.h +71 -0
  150. data/vendor/libgit2/include/git2/sys/index.h +179 -0
  151. data/vendor/libgit2/include/git2/sys/odb_backend.h +86 -0
  152. data/vendor/libgit2/include/git2/sys/refdb_backend.h +158 -0
  153. data/vendor/libgit2/include/git2/sys/refs.h +38 -0
  154. data/vendor/libgit2/include/git2/sys/repository.h +106 -0
  155. data/vendor/libgit2/include/git2/tag.h +44 -18
  156. data/vendor/libgit2/include/git2/trace.h +1 -2
  157. data/vendor/libgit2/include/git2/transport.h +74 -0
  158. data/vendor/libgit2/include/git2/tree.h +12 -22
  159. data/vendor/libgit2/include/git2/types.h +33 -0
  160. data/vendor/libgit2/include/git2/version.h +2 -2
  161. data/vendor/libgit2/src/array.h +66 -0
  162. data/vendor/libgit2/src/attr.c +26 -13
  163. data/vendor/libgit2/src/attr_file.c +3 -2
  164. data/vendor/libgit2/src/attr_file.h +3 -3
  165. data/vendor/libgit2/src/attrcache.h +4 -4
  166. data/vendor/libgit2/src/blob.c +13 -9
  167. data/vendor/libgit2/src/blob.h +2 -2
  168. data/vendor/libgit2/src/branch.c +67 -49
  169. data/vendor/libgit2/src/cache.c +224 -54
  170. data/vendor/libgit2/src/cache.h +33 -20
  171. data/vendor/libgit2/src/checkout.c +145 -85
  172. data/vendor/libgit2/src/clone.c +62 -50
  173. data/vendor/libgit2/src/commit.c +74 -40
  174. data/vendor/libgit2/src/commit.h +2 -3
  175. data/vendor/libgit2/src/commit_list.c +14 -8
  176. data/vendor/libgit2/src/config.c +119 -36
  177. data/vendor/libgit2/src/config.h +3 -0
  178. data/vendor/libgit2/src/config_cache.c +24 -7
  179. data/vendor/libgit2/src/config_file.c +9 -6
  180. data/vendor/libgit2/src/crlf.c +4 -2
  181. data/vendor/libgit2/src/date.c +3 -3
  182. data/vendor/libgit2/src/delta.c +1 -1
  183. data/vendor/libgit2/src/diff.c +681 -303
  184. data/vendor/libgit2/src/diff.h +34 -2
  185. data/vendor/libgit2/src/diff_driver.c +405 -0
  186. data/vendor/libgit2/src/diff_driver.h +49 -0
  187. data/vendor/libgit2/src/diff_file.c +447 -0
  188. data/vendor/libgit2/src/diff_file.h +58 -0
  189. data/vendor/libgit2/src/diff_patch.c +995 -0
  190. data/vendor/libgit2/src/diff_patch.h +46 -0
  191. data/vendor/libgit2/src/diff_print.c +430 -0
  192. data/vendor/libgit2/src/diff_tform.c +464 -203
  193. data/vendor/libgit2/src/diff_xdiff.c +166 -0
  194. data/vendor/libgit2/src/diff_xdiff.h +28 -0
  195. data/vendor/libgit2/src/fetch.c +11 -4
  196. data/vendor/libgit2/src/fileops.c +85 -61
  197. data/vendor/libgit2/src/fileops.h +4 -0
  198. data/vendor/libgit2/src/global.c +10 -2
  199. data/vendor/libgit2/src/global.h +0 -8
  200. data/vendor/libgit2/src/hash/hash_generic.h +3 -3
  201. data/vendor/libgit2/src/hash/hash_win32.h +4 -4
  202. data/vendor/libgit2/src/hashsig.c +0 -1
  203. data/vendor/libgit2/src/ignore.c +68 -28
  204. data/vendor/libgit2/src/ignore.h +10 -1
  205. data/vendor/libgit2/src/index.c +666 -84
  206. data/vendor/libgit2/src/index.h +6 -0
  207. data/vendor/libgit2/src/indexer.c +10 -28
  208. data/vendor/libgit2/src/iterator.c +427 -283
  209. data/vendor/libgit2/src/iterator.h +58 -4
  210. data/vendor/libgit2/src/merge.c +1892 -32
  211. data/vendor/libgit2/src/merge.h +132 -5
  212. data/vendor/libgit2/src/merge_file.c +174 -0
  213. data/vendor/libgit2/src/merge_file.h +71 -0
  214. data/vendor/libgit2/src/mwindow.c +1 -1
  215. data/vendor/libgit2/src/notes.c +45 -48
  216. data/vendor/libgit2/src/object.c +89 -127
  217. data/vendor/libgit2/src/object.h +0 -1
  218. data/vendor/libgit2/src/object_api.c +129 -0
  219. data/vendor/libgit2/src/odb.c +156 -59
  220. data/vendor/libgit2/src/odb.h +5 -2
  221. data/vendor/libgit2/src/odb_loose.c +31 -17
  222. data/vendor/libgit2/src/odb_pack.c +39 -43
  223. data/vendor/libgit2/src/oid.c +62 -27
  224. data/vendor/libgit2/src/oid.h +33 -0
  225. data/vendor/libgit2/src/oidmap.h +4 -6
  226. data/vendor/libgit2/src/pack-objects.c +54 -22
  227. data/vendor/libgit2/src/pack.c +98 -56
  228. data/vendor/libgit2/src/pack.h +3 -1
  229. data/vendor/libgit2/src/pathspec.c +26 -1
  230. data/vendor/libgit2/src/pathspec.h +14 -0
  231. data/vendor/libgit2/src/pool.c +5 -0
  232. data/vendor/libgit2/src/posix.c +2 -2
  233. data/vendor/libgit2/src/posix.h +3 -0
  234. data/vendor/libgit2/src/push.c +13 -10
  235. data/vendor/libgit2/src/refdb.c +82 -62
  236. data/vendor/libgit2/src/refdb.h +16 -16
  237. data/vendor/libgit2/src/refdb_fs.c +386 -133
  238. data/vendor/libgit2/src/reflog.c +3 -1
  239. data/vendor/libgit2/src/refs.c +247 -221
  240. data/vendor/libgit2/src/refs.h +2 -1
  241. data/vendor/libgit2/src/refspec.c +18 -1
  242. data/vendor/libgit2/src/refspec.h +3 -1
  243. data/vendor/libgit2/src/remote.c +434 -253
  244. data/vendor/libgit2/src/remote.h +5 -3
  245. data/vendor/libgit2/src/repository.c +197 -111
  246. data/vendor/libgit2/src/repository.h +26 -5
  247. data/vendor/libgit2/src/reset.c +1 -1
  248. data/vendor/libgit2/src/revparse.c +84 -79
  249. data/vendor/libgit2/src/revwalk.c +1 -1
  250. data/vendor/libgit2/src/signature.c +22 -10
  251. data/vendor/libgit2/src/stash.c +5 -2
  252. data/vendor/libgit2/src/status.c +311 -107
  253. data/vendor/libgit2/src/status.h +23 -0
  254. data/vendor/libgit2/src/submodule.c +21 -13
  255. data/vendor/libgit2/src/tag.c +42 -31
  256. data/vendor/libgit2/src/tag.h +2 -3
  257. data/vendor/libgit2/src/thread-utils.h +105 -3
  258. data/vendor/libgit2/src/trace.c +1 -2
  259. data/vendor/libgit2/src/trace.h +3 -3
  260. data/vendor/libgit2/src/transport.c +18 -6
  261. data/vendor/libgit2/src/transports/cred.c +103 -1
  262. data/vendor/libgit2/src/transports/local.c +19 -9
  263. data/vendor/libgit2/src/transports/smart_protocol.c +32 -12
  264. data/vendor/libgit2/src/transports/ssh.c +519 -0
  265. data/vendor/libgit2/src/transports/winhttp.c +3 -1
  266. data/vendor/libgit2/src/tree.c +26 -28
  267. data/vendor/libgit2/src/tree.h +3 -3
  268. data/vendor/libgit2/src/unix/posix.h +2 -0
  269. data/vendor/libgit2/src/util.c +43 -6
  270. data/vendor/libgit2/src/util.h +40 -12
  271. data/vendor/libgit2/src/vector.c +3 -5
  272. data/vendor/libgit2/src/vector.h +9 -0
  273. data/vendor/libgit2/src/win32/dir.c +1 -1
  274. data/vendor/libgit2/src/win32/error.c +2 -0
  275. data/vendor/libgit2/src/win32/findfile.c +3 -6
  276. data/vendor/libgit2/src/win32/posix_w32.c +85 -59
  277. data/vendor/libgit2/src/win32/pthread.c +16 -8
  278. data/vendor/libgit2/src/win32/pthread.h +7 -4
  279. metadata +407 -306
  280. data/test/coverage/HEAD.json +0 -1
  281. data/vendor/libgit2/include/git2/refdb_backend.h +0 -109
  282. data/vendor/libgit2/src/diff_output.c +0 -1819
  283. data/vendor/libgit2/src/diff_output.h +0 -93
@@ -21,6 +21,7 @@
21
21
  #include "fileops.h"
22
22
  #include "refs.h"
23
23
  #include "path.h"
24
+ #include "repository.h"
24
25
 
25
26
  static int create_branch(
26
27
  git_reference **branch,
@@ -132,14 +133,14 @@ static int reference_matches_remote_head(
132
133
  return 0;
133
134
  }
134
135
 
135
- if (git_oid_cmp(&head_info->remote_head_oid, &oid) == 0) {
136
+ if (git_oid__cmp(&head_info->remote_head_oid, &oid) == 0) {
136
137
  /* Determine the local reference name from the remote tracking one */
137
138
  if (git_refspec_transform_l(
138
- &head_info->branchname,
139
+ &head_info->branchname,
139
140
  head_info->refspec,
140
141
  reference_name) < 0)
141
142
  return -1;
142
-
143
+
143
144
  if (git_buf_len(&head_info->branchname) > 0) {
144
145
  if (git_buf_sets(
145
146
  &head_info->branchname,
@@ -187,6 +188,7 @@ static int get_head_callback(git_remote_head *head, void *payload)
187
188
  static int update_head_to_remote(git_repository *repo, git_remote *remote)
188
189
  {
189
190
  int retcode = -1;
191
+ git_refspec dummy_spec;
190
192
  git_remote_head *remote_head;
191
193
  struct head_info head_info;
192
194
  git_buf remote_master_name = GIT_BUF_INIT;
@@ -211,8 +213,13 @@ static int update_head_to_remote(git_repository *repo, git_remote *remote)
211
213
  git_oid_cpy(&head_info.remote_head_oid, &remote_head->oid);
212
214
  git_buf_init(&head_info.branchname, 16);
213
215
  head_info.repo = repo;
214
- head_info.refspec = git_remote_fetchspec(remote);
216
+ head_info.refspec = git_remote__matching_refspec(remote, GIT_REFS_HEADS_MASTER_FILE);
215
217
  head_info.found = 0;
218
+
219
+ if (head_info.refspec == NULL) {
220
+ memset(&dummy_spec, 0, sizeof(git_refspec));
221
+ head_info.refspec = &dummy_spec;
222
+ }
216
223
 
217
224
  /* Determine the remote tracking reference name from the local master */
218
225
  if (git_refspec_transform_r(
@@ -235,9 +242,8 @@ static int update_head_to_remote(git_repository *repo, git_remote *remote)
235
242
  }
236
243
 
237
244
  /* Not master. Check all the other refs. */
238
- if (git_reference_foreach(
245
+ if (git_reference_foreach_name(
239
246
  repo,
240
- GIT_REF_LISTALL,
241
247
  reference_matches_remote_head,
242
248
  &head_info) < 0)
243
249
  goto cleanup;
@@ -269,7 +275,7 @@ static int update_head_to_branch(
269
275
  int retcode;
270
276
  git_buf remote_branch_name = GIT_BUF_INIT;
271
277
  git_reference* remote_ref = NULL;
272
-
278
+
273
279
  assert(options->checkout_branch);
274
280
 
275
281
  if ((retcode = git_buf_printf(&remote_branch_name, GIT_REFS_REMOTES_DIR "%s/%s",
@@ -317,18 +323,24 @@ static int create_and_configure_origin(
317
323
  (error = git_remote_set_callbacks(origin, options->remote_callbacks)) < 0)
318
324
  goto on_error;
319
325
 
320
- if (options->fetch_spec &&
321
- (error = git_remote_set_fetchspec(origin, options->fetch_spec)) < 0)
322
- goto on_error;
326
+ if (options->fetch_spec) {
327
+ git_remote_clear_refspecs(origin);
328
+ if ((error = git_remote_add_fetch(origin, options->fetch_spec)) < 0)
329
+ goto on_error;
330
+ }
323
331
 
324
332
  if (options->push_spec &&
325
- (error = git_remote_set_pushspec(origin, options->push_spec)) < 0)
333
+ (error = git_remote_add_push(origin, options->push_spec)) < 0)
326
334
  goto on_error;
327
335
 
328
336
  if (options->pushurl &&
329
337
  (error = git_remote_set_pushurl(origin, options->pushurl)) < 0)
330
338
  goto on_error;
331
339
 
340
+ if (options->transport_flags == GIT_TRANSPORTFLAGS_NO_CHECK_CERT) {
341
+ git_remote_check_cert(origin, 0);
342
+ }
343
+
332
344
  if ((error = git_remote_save(origin)) < 0)
333
345
  goto on_error;
334
346
 
@@ -347,50 +359,48 @@ static int setup_remotes_and_fetch(
347
359
  const git_clone_options *options)
348
360
  {
349
361
  int retcode = GIT_ERROR;
350
- git_remote *origin;
362
+ git_remote *origin = NULL;
351
363
 
352
364
  /* Construct an origin remote */
353
- if (!create_and_configure_origin(&origin, repo, url, options)) {
354
- git_remote_set_update_fetchhead(origin, 0);
355
-
356
- /* Connect and download everything */
357
- if (!git_remote_connect(origin, GIT_DIRECTION_FETCH)) {
358
- if (!(retcode = git_remote_download(origin, options->fetch_progress_cb,
359
- options->fetch_progress_payload))) {
360
- /* Create "origin/foo" branches for all remote branches */
361
- if (!git_remote_update_tips(origin)) {
362
- /* Point HEAD to the requested branch */
363
- if (options->checkout_branch) {
364
- if (!update_head_to_branch(repo, options))
365
- retcode = 0;
366
- }
367
- /* Point HEAD to the same ref as the remote's head */
368
- else if (!update_head_to_remote(repo, origin)) {
369
- retcode = 0;
370
- }
371
- }
372
- }
373
- git_remote_disconnect(origin);
374
- }
375
- git_remote_free(origin);
376
- }
365
+ if ((retcode = create_and_configure_origin(&origin, repo, url, options)) < 0)
366
+ goto on_error;
377
367
 
378
- return retcode;
379
- }
368
+ git_remote_set_update_fetchhead(origin, 0);
380
369
 
370
+ /* If the download_tags value has not been specified, then make sure to
371
+ * download tags as well. It is set here because we want to download tags
372
+ * on the initial clone, but do not want to persist the value in the
373
+ * configuration file.
374
+ */
375
+ if (origin->download_tags == GIT_REMOTE_DOWNLOAD_TAGS_AUTO &&
376
+ ((retcode = git_remote_add_fetch(origin, "refs/tags/*:refs/tags/*")) < 0))
377
+ goto on_error;
381
378
 
382
- static bool path_is_okay(const char *path)
383
- {
384
- /* The path must either not exist, or be an empty directory */
385
- if (!git_path_exists(path)) return true;
386
- if (!git_path_is_empty_dir(path)) {
387
- giterr_set(GITERR_INVALID,
388
- "'%s' exists and is not an empty directory", path);
389
- return false;
390
- }
391
- return true;
379
+ /* Connect and download everything */
380
+ if ((retcode = git_remote_connect(origin, GIT_DIRECTION_FETCH)) < 0)
381
+ goto on_error;
382
+
383
+ if ((retcode = git_remote_download(origin, options->fetch_progress_cb,
384
+ options->fetch_progress_payload)) < 0)
385
+ goto on_error;
386
+
387
+ /* Create "origin/foo" branches for all remote branches */
388
+ if ((retcode = git_remote_update_tips(origin)) < 0)
389
+ goto on_error;
390
+
391
+ /* Point HEAD to the requested branch */
392
+ if (options->checkout_branch)
393
+ retcode = update_head_to_branch(repo, options);
394
+ /* Point HEAD to the same ref as the remote's head */
395
+ else
396
+ retcode = update_head_to_remote(repo, origin);
397
+
398
+ on_error:
399
+ git_remote_free(origin);
400
+ return retcode;
392
401
  }
393
402
 
403
+
394
404
  static bool should_checkout(
395
405
  git_repository *repo,
396
406
  bool is_bare,
@@ -417,7 +427,6 @@ static void normalize_options(git_clone_options *dst, const git_clone_options *s
417
427
 
418
428
  /* Provide defaults for null pointers */
419
429
  if (!dst->remote_name) dst->remote_name = "origin";
420
- if (!dst->remote_autotag) dst->remote_autotag = GIT_REMOTE_DOWNLOAD_TAGS_ALL;
421
430
  }
422
431
 
423
432
  int git_clone(
@@ -436,7 +445,10 @@ int git_clone(
436
445
  normalize_options(&normOptions, options);
437
446
  GITERR_CHECK_VERSION(&normOptions, GIT_CLONE_OPTIONS_VERSION, "git_clone_options");
438
447
 
439
- if (!path_is_okay(local_path)) {
448
+ /* Only clone to a new directory or an empty directory */
449
+ if (git_path_exists(local_path) && !git_path_is_empty_dir(local_path)) {
450
+ giterr_set(GITERR_INVALID,
451
+ "'%s' exists and is not an empty directory", local_path);
440
452
  return GIT_ERROR;
441
453
  }
442
454
 
@@ -9,6 +9,7 @@
9
9
  #include "git2/object.h"
10
10
  #include "git2/repository.h"
11
11
  #include "git2/signature.h"
12
+ #include "git2/sys/commit.h"
12
13
 
13
14
  #include "common.h"
14
15
  #include "odb.h"
@@ -30,8 +31,10 @@ static void clear_parents(git_commit *commit)
30
31
  git_vector_clear(&commit->parent_ids);
31
32
  }
32
33
 
33
- void git_commit__free(git_commit *commit)
34
+ void git_commit__free(void *_commit)
34
35
  {
36
+ git_commit *commit = _commit;
37
+
35
38
  clear_parents(commit);
36
39
  git_vector_free(&commit->parent_ids);
37
40
 
@@ -44,16 +47,16 @@ void git_commit__free(git_commit *commit)
44
47
  }
45
48
 
46
49
  int git_commit_create_v(
47
- git_oid *oid,
48
- git_repository *repo,
49
- const char *update_ref,
50
- const git_signature *author,
51
- const git_signature *committer,
52
- const char *message_encoding,
53
- const char *message,
54
- const git_tree *tree,
55
- int parent_count,
56
- ...)
50
+ git_oid *oid,
51
+ git_repository *repo,
52
+ const char *update_ref,
53
+ const git_signature *author,
54
+ const git_signature *committer,
55
+ const char *message_encoding,
56
+ const char *message,
57
+ const git_tree *tree,
58
+ int parent_count,
59
+ ...)
57
60
  {
58
61
  va_list ap;
59
62
  int i, res;
@@ -76,30 +79,28 @@ int git_commit_create_v(
76
79
  return res;
77
80
  }
78
81
 
79
- int git_commit_create(
80
- git_oid *oid,
81
- git_repository *repo,
82
- const char *update_ref,
83
- const git_signature *author,
84
- const git_signature *committer,
85
- const char *message_encoding,
86
- const char *message,
87
- const git_tree *tree,
88
- int parent_count,
89
- const git_commit *parents[])
82
+ int git_commit_create_from_oids(
83
+ git_oid *oid,
84
+ git_repository *repo,
85
+ const char *update_ref,
86
+ const git_signature *author,
87
+ const git_signature *committer,
88
+ const char *message_encoding,
89
+ const char *message,
90
+ const git_oid *tree,
91
+ int parent_count,
92
+ const git_oid *parents[])
90
93
  {
91
94
  git_buf commit = GIT_BUF_INIT;
92
95
  int i;
93
96
  git_odb *odb;
94
97
 
95
- assert(git_object_owner((const git_object *)tree) == repo);
98
+ assert(oid && repo && tree && parent_count >= 0);
96
99
 
97
- git_oid__writebuf(&commit, "tree ", git_object_id((const git_object *)tree));
100
+ git_oid__writebuf(&commit, "tree ", tree);
98
101
 
99
- for (i = 0; i < parent_count; ++i) {
100
- assert(git_object_owner((const git_object *)parents[i]) == repo);
101
- git_oid__writebuf(&commit, "parent ", git_object_id((const git_object *)parents[i]));
102
- }
102
+ for (i = 0; i < parent_count; ++i)
103
+ git_oid__writebuf(&commit, "parent ", parents[i]);
103
104
 
104
105
  git_signature__writebuf(&commit, "author ", author);
105
106
  git_signature__writebuf(&commit, "committer ", committer);
@@ -131,10 +132,47 @@ on_error:
131
132
  return -1;
132
133
  }
133
134
 
134
- int git_commit__parse_buffer(git_commit *commit, const void *data, size_t len)
135
+ int git_commit_create(
136
+ git_oid *oid,
137
+ git_repository *repo,
138
+ const char *update_ref,
139
+ const git_signature *author,
140
+ const git_signature *committer,
141
+ const char *message_encoding,
142
+ const char *message,
143
+ const git_tree *tree,
144
+ int parent_count,
145
+ const git_commit *parents[])
135
146
  {
136
- const char *buffer = data;
137
- const char *buffer_end = (const char *)data + len;
147
+ int retval, i;
148
+ const git_oid **parent_oids;
149
+
150
+ assert(parent_count >= 0);
151
+ assert(git_object_owner((const git_object *)tree) == repo);
152
+
153
+ parent_oids = git__malloc(parent_count * sizeof(git_oid *));
154
+ GITERR_CHECK_ALLOC(parent_oids);
155
+
156
+ for (i = 0; i < parent_count; ++i) {
157
+ assert(git_object_owner((const git_object *)parents[i]) == repo);
158
+ parent_oids[i] = git_object_id((const git_object *)parents[i]);
159
+ }
160
+
161
+ retval = git_commit_create_from_oids(
162
+ oid, repo, update_ref, author, committer,
163
+ message_encoding, message,
164
+ git_object_id((const git_object *)tree), parent_count, parent_oids);
165
+
166
+ git__free((void *)parent_oids);
167
+
168
+ return retval;
169
+ }
170
+
171
+ int git_commit__parse(void *_commit, git_odb_object *odb_obj)
172
+ {
173
+ git_commit *commit = _commit;
174
+ const char *buffer = git_odb_object_data(odb_obj);
175
+ const char *buffer_end = buffer + git_odb_object_size(odb_obj);
138
176
  git_oid parent_id;
139
177
 
140
178
  if (git_vector_init(&commit->parent_ids, 4, NULL) < 0)
@@ -206,12 +244,6 @@ bad_buffer:
206
244
  return -1;
207
245
  }
208
246
 
209
- int git_commit__parse(git_commit *commit, git_odb_object *obj)
210
- {
211
- assert(commit);
212
- return git_commit__parse_buffer(commit, obj->raw.data, obj->raw.len);
213
- }
214
-
215
247
  #define GIT_COMMIT_GETTER(_rvalue, _name, _return) \
216
248
  _rvalue git_commit_##_name(const git_commit *commit) \
217
249
  {\
@@ -234,14 +266,16 @@ int git_commit_tree(git_tree **tree_out, const git_commit *commit)
234
266
  return git_tree_lookup(tree_out, commit->object.repo, &commit->tree_id);
235
267
  }
236
268
 
237
- const git_oid *git_commit_parent_id(git_commit *commit, unsigned int n)
269
+ const git_oid *git_commit_parent_id(
270
+ const git_commit *commit, unsigned int n)
238
271
  {
239
272
  assert(commit);
240
273
 
241
274
  return git_vector_get(&commit->parent_ids, n);
242
275
  }
243
276
 
244
- int git_commit_parent(git_commit **parent, git_commit *commit, unsigned int n)
277
+ int git_commit_parent(
278
+ git_commit **parent, const git_commit *commit, unsigned int n)
245
279
  {
246
280
  const git_oid *parent_id;
247
281
  assert(commit);
@@ -260,7 +294,7 @@ int git_commit_nth_gen_ancestor(
260
294
  const git_commit *commit,
261
295
  unsigned int n)
262
296
  {
263
- git_commit *current, *parent;
297
+ git_commit *current, *parent = NULL;
264
298
  int error;
265
299
 
266
300
  assert(ancestor && commit);
@@ -27,8 +27,7 @@ struct git_commit {
27
27
  char *message;
28
28
  };
29
29
 
30
- void git_commit__free(git_commit *c);
31
- int git_commit__parse(git_commit *commit, git_odb_object *obj);
30
+ void git_commit__free(void *commit);
31
+ int git_commit__parse(void *commit, git_odb_object *obj);
32
32
 
33
- int git_commit__parse_buffer(git_commit *commit, const void *data, size_t len);
34
33
  #endif
@@ -36,7 +36,7 @@ git_commit_list *git_commit_list_insert_by_date(git_commit_list_node *item, git_
36
36
  git_commit_list *p;
37
37
 
38
38
  while ((p = *pp) != NULL) {
39
- if (git_commit_list_time_cmp(p->item, item) < 0)
39
+ if (git_commit_list_time_cmp(p->item, item) > 0)
40
40
  break;
41
41
 
42
42
  pp = &p->next;
@@ -100,12 +100,15 @@ git_commit_list_node *git_commit_list_pop(git_commit_list **stack)
100
100
  return item;
101
101
  }
102
102
 
103
- static int commit_quick_parse(git_revwalk *walk, git_commit_list_node *commit, git_rawobj *raw)
103
+ static int commit_quick_parse(
104
+ git_revwalk *walk,
105
+ git_commit_list_node *commit,
106
+ const uint8_t *buffer,
107
+ size_t buffer_len)
104
108
  {
105
109
  const size_t parent_len = strlen("parent ") + GIT_OID_HEXSZ + 1;
106
- unsigned char *buffer = raw->data;
107
- unsigned char *buffer_end = buffer + raw->len;
108
- unsigned char *parents_start, *committer_start;
110
+ const uint8_t *buffer_end = buffer + buffer_len;
111
+ const uint8_t *parents_start, *committer_start;
109
112
  int i, parents = 0;
110
113
  int commit_time;
111
114
 
@@ -124,7 +127,7 @@ static int commit_quick_parse(git_revwalk *walk, git_commit_list_node *commit, g
124
127
  for (i = 0; i < parents; ++i) {
125
128
  git_oid oid;
126
129
 
127
- if (git_oid_fromstr(&oid, (char *)buffer + strlen("parent ")) < 0)
130
+ if (git_oid_fromstr(&oid, (const char *)buffer + strlen("parent ")) < 0)
128
131
  return -1;
129
132
 
130
133
  commit->parents[i] = git_revwalk__commit_lookup(walk, &oid);
@@ -182,11 +185,14 @@ int git_commit_list_parse(git_revwalk *walk, git_commit_list_node *commit)
182
185
  if ((error = git_odb_read(&obj, walk->odb, &commit->oid)) < 0)
183
186
  return error;
184
187
 
185
- if (obj->raw.type != GIT_OBJ_COMMIT) {
188
+ if (obj->cached.type != GIT_OBJ_COMMIT) {
186
189
  giterr_set(GITERR_INVALID, "Object is no commit object");
187
190
  error = -1;
188
191
  } else
189
- error = commit_quick_parse(walk, commit, &obj->raw);
192
+ error = commit_quick_parse(
193
+ walk, commit,
194
+ (const uint8_t *)git_odb_object_data(obj),
195
+ git_odb_object_size(obj));
190
196
 
191
197
  git_odb_object_free(obj);
192
198
  return error;
@@ -9,6 +9,7 @@
9
9
  #include "fileops.h"
10
10
  #include "config.h"
11
11
  #include "git2/config.h"
12
+ #include "git2/sys/config.h"
12
13
  #include "vector.h"
13
14
  #include "buf_text.h"
14
15
  #include "config_file.h"
@@ -22,7 +23,7 @@ typedef struct {
22
23
  git_refcount rc;
23
24
 
24
25
  git_config_backend *file;
25
- unsigned int level;
26
+ git_config_level_t level;
26
27
  } file_internal;
27
28
 
28
29
  static void file_internal_free(file_internal *internal)
@@ -39,12 +40,14 @@ static void config_free(git_config *cfg)
39
40
  size_t i;
40
41
  file_internal *internal;
41
42
 
42
- for(i = 0; i < cfg->files.length; ++i){
43
+ for (i = 0; i < cfg->files.length; ++i) {
43
44
  internal = git_vector_get(&cfg->files, i);
44
45
  GIT_REFCOUNT_DEC(internal, file_internal_free);
45
46
  }
46
47
 
47
48
  git_vector_free(&cfg->files);
49
+
50
+ git__memzero(cfg, sizeof(*cfg));
48
51
  git__free(cfg);
49
52
  }
50
53
 
@@ -86,17 +89,19 @@ int git_config_new(git_config **out)
86
89
  int git_config_add_file_ondisk(
87
90
  git_config *cfg,
88
91
  const char *path,
89
- unsigned int level,
92
+ git_config_level_t level,
90
93
  int force)
91
94
  {
92
95
  git_config_backend *file = NULL;
96
+ struct stat st;
93
97
  int res;
94
98
 
95
99
  assert(cfg && path);
96
100
 
97
- if (!git_path_isfile(path)) {
98
- giterr_set(GITERR_CONFIG, "Cannot find config file '%s'", path);
99
- return GIT_ENOTFOUND;
101
+ res = p_stat(path, &st);
102
+ if (res < 0 && errno != ENOENT) {
103
+ giterr_set(GITERR_CONFIG, "Error stat'ing config file '%s'", path);
104
+ return -1;
100
105
  }
101
106
 
102
107
  if (git_config_file__ondisk(&file, path) < 0)
@@ -135,11 +140,11 @@ int git_config_open_ondisk(git_config **out, const char *path)
135
140
  static int find_internal_file_by_level(
136
141
  file_internal **internal_out,
137
142
  const git_config *cfg,
138
- int level)
143
+ git_config_level_t level)
139
144
  {
140
145
  int pos = -1;
141
146
  file_internal *internal;
142
- unsigned int i;
147
+ size_t i;
143
148
 
144
149
  /* when passing GIT_CONFIG_HIGHEST_LEVEL, the idea is to get the config file
145
150
  * which has the highest level. As config files are stored in a vector
@@ -150,14 +155,14 @@ static int find_internal_file_by_level(
150
155
  pos = 0;
151
156
  } else {
152
157
  git_vector_foreach(&cfg->files, i, internal) {
153
- if (internal->level == (unsigned int)level)
154
- pos = i;
158
+ if (internal->level == level)
159
+ pos = (int)i;
155
160
  }
156
161
  }
157
162
 
158
163
  if (pos == -1) {
159
164
  giterr_set(GITERR_CONFIG,
160
- "No config file exists for the given level '%i'", level);
165
+ "No config file exists for the given level '%i'", (int)level);
161
166
  return GIT_ENOTFOUND;
162
167
  }
163
168
 
@@ -172,21 +177,21 @@ static int duplicate_level(void **old_raw, void *new_raw)
172
177
 
173
178
  GIT_UNUSED(new_raw);
174
179
 
175
- giterr_set(GITERR_CONFIG, "A file with the same level (%i) has already been added to the config", (*old)->level);
180
+ giterr_set(GITERR_CONFIG, "A file with the same level (%i) has already been added to the config", (int)(*old)->level);
176
181
  return GIT_EEXISTS;
177
182
  }
178
183
 
179
184
  static void try_remove_existing_file_internal(
180
185
  git_config *cfg,
181
- unsigned int level)
186
+ git_config_level_t level)
182
187
  {
183
188
  int pos = -1;
184
189
  file_internal *internal;
185
- unsigned int i;
190
+ size_t i;
186
191
 
187
192
  git_vector_foreach(&cfg->files, i, internal) {
188
193
  if (internal->level == level)
189
- pos = i;
194
+ pos = (int)i;
190
195
  }
191
196
 
192
197
  if (pos == -1)
@@ -203,7 +208,7 @@ static void try_remove_existing_file_internal(
203
208
  static int git_config__add_internal(
204
209
  git_config *cfg,
205
210
  file_internal *internal,
206
- unsigned int level,
211
+ git_config_level_t level,
207
212
  int force)
208
213
  {
209
214
  int result;
@@ -224,10 +229,18 @@ static int git_config__add_internal(
224
229
  return 0;
225
230
  }
226
231
 
232
+ int git_config_open_global(git_config **cfg_out, git_config *cfg)
233
+ {
234
+ if (!git_config_open_level(cfg_out, cfg, GIT_CONFIG_LEVEL_XDG))
235
+ return 0;
236
+
237
+ return git_config_open_level(cfg_out, cfg, GIT_CONFIG_LEVEL_GLOBAL);
238
+ }
239
+
227
240
  int git_config_open_level(
228
- git_config **cfg_out,
229
- const git_config *cfg_parent,
230
- unsigned int level)
241
+ git_config **cfg_out,
242
+ const git_config *cfg_parent,
243
+ git_config_level_t level)
231
244
  {
232
245
  git_config *cfg;
233
246
  file_internal *internal;
@@ -252,7 +265,7 @@ int git_config_open_level(
252
265
  int git_config_add_backend(
253
266
  git_config *cfg,
254
267
  git_config_backend *file,
255
- unsigned int level,
268
+ git_config_level_t level,
256
269
  int force)
257
270
  {
258
271
  file_internal *internal;
@@ -292,6 +305,9 @@ int git_config_refresh(git_config *cfg)
292
305
  error = file->refresh(file);
293
306
  }
294
307
 
308
+ if (!error && GIT_REFCOUNT_OWNER(cfg) != NULL)
309
+ git_repository__cvar_cache_clear(GIT_REFCOUNT_OWNER(cfg));
310
+
295
311
  return error;
296
312
  }
297
313
 
@@ -325,21 +341,30 @@ int git_config_foreach_match(
325
341
  return ret;
326
342
  }
327
343
 
344
+ /**************
345
+ * Setters
346
+ **************/
347
+
348
+ static int config_error_nofiles(const char *name)
349
+ {
350
+ giterr_set(GITERR_CONFIG,
351
+ "Cannot set value for '%s' when no config files exist", name);
352
+ return GIT_ENOTFOUND;
353
+ }
354
+
328
355
  int git_config_delete_entry(git_config *cfg, const char *name)
329
356
  {
330
357
  git_config_backend *file;
331
358
  file_internal *internal;
332
359
 
333
360
  internal = git_vector_get(&cfg->files, 0);
361
+ if (!internal || !internal->file)
362
+ return config_error_nofiles(name);
334
363
  file = internal->file;
335
364
 
336
365
  return file->del(file, name);
337
366
  }
338
367
 
339
- /**************
340
- * Setters
341
- **************/
342
-
343
368
  int git_config_set_int64(git_config *cfg, const char *name, int64_t value)
344
369
  {
345
370
  char str_value[32]; /* All numbers should fit in here */
@@ -359,6 +384,7 @@ int git_config_set_bool(git_config *cfg, const char *name, int value)
359
384
 
360
385
  int git_config_set_string(git_config *cfg, const char *name, const char *value)
361
386
  {
387
+ int error;
362
388
  git_config_backend *file;
363
389
  file_internal *internal;
364
390
 
@@ -368,9 +394,16 @@ int git_config_set_string(git_config *cfg, const char *name, const char *value)
368
394
  }
369
395
 
370
396
  internal = git_vector_get(&cfg->files, 0);
397
+ if (!internal || !internal->file)
398
+ return config_error_nofiles(name);
371
399
  file = internal->file;
372
400
 
373
- return file->set(file, name, value);
401
+ error = file->set(file, name, value);
402
+
403
+ if (!error && GIT_REFCOUNT_OWNER(cfg) != NULL)
404
+ git_repository__cvar_cache_clear(GIT_REFCOUNT_OWNER(cfg));
405
+
406
+ return error;
374
407
  }
375
408
 
376
409
  /***********
@@ -426,19 +459,28 @@ static int get_string_at_file(const char **out, const git_config_backend *file,
426
459
  return res;
427
460
  }
428
461
 
462
+ static int config_error_notfound(const char *name)
463
+ {
464
+ giterr_set(GITERR_CONFIG, "Config value '%s' was not found", name);
465
+ return GIT_ENOTFOUND;
466
+ }
467
+
429
468
  static int get_string(const char **out, const git_config *cfg, const char *name)
430
469
  {
431
470
  file_internal *internal;
432
471
  unsigned int i;
472
+ int res;
433
473
 
434
474
  git_vector_foreach(&cfg->files, i, internal) {
435
- int res = get_string_at_file(out, internal->file, name);
475
+ if (!internal || !internal->file)
476
+ continue;
436
477
 
478
+ res = get_string_at_file(out, internal->file, name);
437
479
  if (res != GIT_ENOTFOUND)
438
480
  return res;
439
481
  }
440
482
 
441
- return GIT_ENOTFOUND;
483
+ return config_error_notfound(name);
442
484
  }
443
485
 
444
486
  int git_config_get_bool(int *out, const git_config *cfg, const char *name)
@@ -468,21 +510,27 @@ int git_config_get_entry(const git_config_entry **out, const git_config *cfg, co
468
510
  {
469
511
  file_internal *internal;
470
512
  unsigned int i;
513
+ git_config_backend *file;
514
+ int ret;
471
515
 
472
516
  *out = NULL;
473
517
 
474
518
  git_vector_foreach(&cfg->files, i, internal) {
475
- git_config_backend *file = internal->file;
476
- int ret = file->get(file, name, out);
519
+ if (!internal || !internal->file)
520
+ continue;
521
+ file = internal->file;
522
+
523
+ ret = file->get(file, name, out);
477
524
  if (ret != GIT_ENOTFOUND)
478
525
  return ret;
479
526
  }
480
527
 
481
- return GIT_ENOTFOUND;
528
+ return config_error_notfound(name);
482
529
  }
483
530
 
484
- int git_config_get_multivar(const git_config *cfg, const char *name, const char *regexp,
485
- git_config_foreach_cb cb, void *payload)
531
+ int git_config_get_multivar(
532
+ const git_config *cfg, const char *name, const char *regexp,
533
+ git_config_foreach_cb cb, void *payload)
486
534
  {
487
535
  file_internal *internal;
488
536
  git_config_backend *file;
@@ -495,13 +543,16 @@ int git_config_get_multivar(const git_config *cfg, const char *name, const char
495
543
  */
496
544
  for (i = cfg->files.length; i > 0; --i) {
497
545
  internal = git_vector_get(&cfg->files, i - 1);
546
+ if (!internal || !internal->file)
547
+ continue;
498
548
  file = internal->file;
549
+
499
550
  ret = file->get_multivar(file, name, regexp, cb, payload);
500
551
  if (ret < 0 && ret != GIT_ENOTFOUND)
501
552
  return ret;
502
553
  }
503
554
 
504
- return 0;
555
+ return (ret == GIT_ENOTFOUND) ? config_error_notfound(name) : 0;
505
556
  }
506
557
 
507
558
  int git_config_set_multivar(git_config *cfg, const char *name, const char *regexp, const char *value)
@@ -510,6 +561,8 @@ int git_config_set_multivar(git_config *cfg, const char *name, const char *regex
510
561
  file_internal *internal;
511
562
 
512
563
  internal = git_vector_get(&cfg->files, 0);
564
+ if (!internal || !internal->file)
565
+ return config_error_nofiles(name);
513
566
  file = internal->file;
514
567
 
515
568
  return file->set_multivar(file, name, regexp, value);
@@ -570,17 +623,46 @@ int git_config_find_system(char *system_config_path, size_t length)
570
623
  system_config_path, length, git_config_find_system_r);
571
624
  }
572
625
 
626
+ int git_config__global_location(git_buf *buf)
627
+ {
628
+ const git_buf *paths;
629
+ const char *sep, *start;
630
+ size_t len;
631
+
632
+ if (git_futils_dirs_get(&paths, GIT_FUTILS_DIR_GLOBAL) < 0)
633
+ return -1;
634
+
635
+ /* no paths, so give up */
636
+ if (git_buf_len(paths) == 0)
637
+ return -1;
638
+
639
+ start = git_buf_cstr(paths);
640
+ sep = strchr(start, GIT_PATH_LIST_SEPARATOR);
641
+
642
+ if (sep)
643
+ len = sep - start;
644
+ else
645
+ len = paths->size;
646
+
647
+ if (git_buf_set(buf, start, len) < 0)
648
+ return -1;
649
+
650
+ return git_buf_joinpath(buf, buf->ptr, GIT_CONFIG_FILENAME_GLOBAL);
651
+ }
652
+
573
653
  int git_config_open_default(git_config **out)
574
654
  {
575
655
  int error;
576
656
  git_config *cfg = NULL;
577
657
  git_buf buf = GIT_BUF_INIT;
578
658
 
579
- error = git_config_new(&cfg);
659
+ if ((error = git_config_new(&cfg)) < 0)
660
+ return error;
580
661
 
581
- if (!error && !git_config_find_global_r(&buf))
662
+ if (!git_config_find_global_r(&buf) || !git_config__global_location(&buf)) {
582
663
  error = git_config_add_file_ondisk(cfg, buf.ptr,
583
664
  GIT_CONFIG_LEVEL_GLOBAL, 0);
665
+ }
584
666
 
585
667
  if (!error && !git_config_find_xdg_r(&buf))
586
668
  error = git_config_add_file_ondisk(cfg, buf.ptr,
@@ -592,7 +674,7 @@ int git_config_open_default(git_config **out)
592
674
 
593
675
  git_buf_free(&buf);
594
676
 
595
- if (error && cfg) {
677
+ if (error) {
596
678
  git_config_free(cfg);
597
679
  cfg = NULL;
598
680
  }
@@ -605,6 +687,7 @@ int git_config_open_default(git_config **out)
605
687
  /***********
606
688
  * Parsers
607
689
  ***********/
690
+
608
691
  int git_config_lookup_map_value(
609
692
  int *out,
610
693
  const git_cvar_map *maps,