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
@@ -19,17 +19,15 @@
19
19
  __KHASH_TYPE(oid, const git_oid *, void *);
20
20
  typedef khash_t(oid) git_oidmap;
21
21
 
22
- GIT_INLINE(khint_t) hash_git_oid(const git_oid *oid)
22
+ GIT_INLINE(khint_t) git_oidmap_hash(const git_oid *oid)
23
23
  {
24
- int i;
25
- khint_t h = 0;
26
- for (i = 0; i < 20; ++i)
27
- h = (h << 5) - h + oid->id[i];
24
+ khint_t h;
25
+ memcpy(&h, oid, sizeof(khint_t));
28
26
  return h;
29
27
  }
30
28
 
31
29
  #define GIT__USE_OIDMAP \
32
- __KHASH_IMPL(oid, static kh_inline, const git_oid *, void *, 1, hash_git_oid, git_oid_equal)
30
+ __KHASH_IMPL(oid, static kh_inline, const git_oid *, void *, 1, git_oidmap_hash, git_oid_equal)
33
31
 
34
32
  #define git_oidmap_alloc() kh_init(oid)
35
33
  #define git_oidmap_free(h) kh_destroy(oid,h), h = NULL
@@ -33,6 +33,11 @@ struct tree_walk_context {
33
33
  git_buf buf;
34
34
  };
35
35
 
36
+ struct pack_write_context {
37
+ git_indexer_stream *indexer;
38
+ git_transfer_progress *stats;
39
+ };
40
+
36
41
  #ifdef GIT_THREADS
37
42
 
38
43
  #define GIT_PACKBUILDER__MUTEX_OP(pb, mtx, op) do { \
@@ -127,7 +132,10 @@ int git_packbuilder_new(git_packbuilder **out, git_repository *repo)
127
132
  if (git_mutex_init(&pb->cache_mutex) ||
128
133
  git_mutex_init(&pb->progress_mutex) ||
129
134
  git_cond_init(&pb->progress_cond))
135
+ {
136
+ giterr_set(GITERR_OS, "Failed to initialize packbuilder mutex");
130
137
  goto on_error;
138
+ }
131
139
 
132
140
  #endif
133
141
 
@@ -620,26 +628,6 @@ static int write_pack_buf(void *buf, size_t size, void *data)
620
628
  return git_buf_put(b, buf, size);
621
629
  }
622
630
 
623
- static int write_pack_to_file(void *buf, size_t size, void *data)
624
- {
625
- git_filebuf *file = (git_filebuf *)data;
626
- return git_filebuf_write(file, buf, size);
627
- }
628
-
629
- static int write_pack_file(git_packbuilder *pb, const char *path)
630
- {
631
- git_filebuf file = GIT_FILEBUF_INIT;
632
-
633
- if (git_filebuf_open(&file, path, 0) < 0 ||
634
- write_pack(pb, &write_pack_to_file, &file) < 0 ||
635
- git_filebuf_commit(&file, GIT_PACK_FILE_MODE) < 0) {
636
- git_filebuf_cleanup(&file);
637
- return -1;
638
- }
639
-
640
- return 0;
641
- }
642
-
643
631
  static int type_size_sort(const void *_a, const void *_b)
644
632
  {
645
633
  const git_pobject *a = (git_pobject *)_a;
@@ -1259,10 +1247,39 @@ int git_packbuilder_write_buf(git_buf *buf, git_packbuilder *pb)
1259
1247
  return write_pack(pb, &write_pack_buf, buf);
1260
1248
  }
1261
1249
 
1262
- int git_packbuilder_write(git_packbuilder *pb, const char *path)
1250
+ static int write_cb(void *buf, size_t len, void *payload)
1263
1251
  {
1252
+ struct pack_write_context *ctx = payload;
1253
+ return git_indexer_stream_add(ctx->indexer, buf, len, ctx->stats);
1254
+ }
1255
+
1256
+ int git_packbuilder_write(
1257
+ git_packbuilder *pb,
1258
+ const char *path,
1259
+ git_transfer_progress_callback progress_cb,
1260
+ void *progress_cb_payload)
1261
+ {
1262
+ git_indexer_stream *indexer;
1263
+ git_transfer_progress stats;
1264
+ struct pack_write_context ctx;
1265
+
1264
1266
  PREPARE_PACK;
1265
- return write_pack_file(pb, path);
1267
+
1268
+ if (git_indexer_stream_new(
1269
+ &indexer, path, progress_cb, progress_cb_payload) < 0)
1270
+ return -1;
1271
+
1272
+ ctx.indexer = indexer;
1273
+ ctx.stats = &stats;
1274
+
1275
+ if (git_packbuilder_foreach(pb, write_cb, &ctx) < 0 ||
1276
+ git_indexer_stream_finalize(indexer, &stats) < 0) {
1277
+ git_indexer_stream_free(indexer);
1278
+ return -1;
1279
+ }
1280
+
1281
+ git_indexer_stream_free(indexer);
1282
+ return 0;
1266
1283
  }
1267
1284
 
1268
1285
  #undef PREPARE_PACK
@@ -1284,6 +1301,21 @@ static int cb_tree_walk(const char *root, const git_tree_entry *entry, void *pay
1284
1301
  git_buf_cstr(&ctx->buf));
1285
1302
  }
1286
1303
 
1304
+ int git_packbuilder_insert_commit(git_packbuilder *pb, const git_oid *oid)
1305
+ {
1306
+ git_commit *commit;
1307
+
1308
+ if (git_commit_lookup(&commit, pb->repo, oid) < 0 ||
1309
+ git_packbuilder_insert(pb, oid, NULL) < 0)
1310
+ return -1;
1311
+
1312
+ if (git_packbuilder_insert_tree(pb, git_commit_tree_id(commit)) < 0)
1313
+ return -1;
1314
+
1315
+ git_commit_free(commit);
1316
+ return 0;
1317
+ }
1318
+
1287
1319
  int git_packbuilder_insert_tree(git_packbuilder *pb, const git_oid *oid)
1288
1320
  {
1289
1321
  git_tree *tree;
@@ -12,8 +12,8 @@
12
12
  #include "sha1_lookup.h"
13
13
  #include "mwindow.h"
14
14
  #include "fileops.h"
15
+ #include "oid.h"
15
16
 
16
- #include "git2/oid.h"
17
17
  #include <zlib.h>
18
18
 
19
19
  static int packfile_open(struct git_pack_file *p);
@@ -85,15 +85,27 @@ static void cache_free(git_pack_cache *cache)
85
85
  git_offmap_free(cache->entries);
86
86
  git_mutex_free(&cache->lock);
87
87
  }
88
+
89
+ memset(cache, 0, sizeof(*cache));
88
90
  }
89
91
 
90
92
  static int cache_init(git_pack_cache *cache)
91
93
  {
92
- memset(cache, 0, sizeof(git_pack_cache));
94
+ memset(cache, 0, sizeof(*cache));
95
+
93
96
  cache->entries = git_offmap_alloc();
94
97
  GITERR_CHECK_ALLOC(cache->entries);
98
+
95
99
  cache->memory_limit = GIT_PACK_CACHE_MEMORY_LIMIT;
96
- git_mutex_init(&cache->lock);
100
+
101
+ if (git_mutex_init(&cache->lock)) {
102
+ giterr_set(GITERR_OS, "Failed to initialize pack cache mutex");
103
+
104
+ git__free(cache->entries);
105
+ cache->entries = NULL;
106
+
107
+ return -1;
108
+ }
97
109
 
98
110
  return 0;
99
111
  }
@@ -205,13 +217,18 @@ static int pack_index_check(const char *path, struct git_pack_file *p)
205
217
  if (fd < 0)
206
218
  return fd;
207
219
 
208
- if (p_fstat(fd, &st) < 0 ||
209
- !S_ISREG(st.st_mode) ||
220
+ if (p_fstat(fd, &st) < 0) {
221
+ p_close(fd);
222
+ giterr_set(GITERR_OS, "Unable to stat pack index '%s'", path);
223
+ return -1;
224
+ }
225
+
226
+ if (!S_ISREG(st.st_mode) ||
210
227
  !git__is_sizet(st.st_size) ||
211
228
  (idx_size = (size_t)st.st_size) < 4 * 256 + 20 + 20)
212
229
  {
213
230
  p_close(fd);
214
- giterr_set(GITERR_OS, "Failed to check pack index.");
231
+ giterr_set(GITERR_ODB, "Invalid pack index '%s'", path);
215
232
  return -1;
216
233
  }
217
234
 
@@ -288,32 +305,40 @@ static int pack_index_check(const char *path, struct git_pack_file *p)
288
305
  }
289
306
  }
290
307
 
291
- p->index_version = version;
292
308
  p->num_objects = nr;
309
+ p->index_version = version;
293
310
  return 0;
294
311
  }
295
312
 
296
313
  static int pack_index_open(struct git_pack_file *p)
297
314
  {
298
315
  char *idx_name;
299
- int error;
300
- size_t name_len, offset;
316
+ int error = 0;
317
+ size_t name_len, base_len;
301
318
 
302
- if (p->index_map.data)
319
+ if (p->index_version > -1)
303
320
  return 0;
304
321
 
305
- idx_name = git__strdup(p->pack_name);
306
- GITERR_CHECK_ALLOC(idx_name);
322
+ name_len = strlen(p->pack_name);
323
+ assert(name_len > strlen(".pack")); /* checked by git_pack_file alloc */
307
324
 
308
- name_len = strlen(idx_name);
309
- offset = name_len - strlen(".pack");
310
- assert(offset < name_len); /* make sure no underflow */
325
+ if ((idx_name = git__malloc(name_len)) == NULL)
326
+ return -1;
327
+
328
+ base_len = name_len - strlen(".pack");
329
+ memcpy(idx_name, p->pack_name, base_len);
330
+ memcpy(idx_name + base_len, ".idx", sizeof(".idx"));
331
+
332
+ if ((error = git_mutex_lock(&p->lock)) < 0)
333
+ return error;
311
334
 
312
- strncpy(idx_name + offset, ".idx", name_len - offset);
335
+ if (p->index_version == -1)
336
+ error = pack_index_check(idx_name, p);
313
337
 
314
- error = pack_index_check(idx_name, p);
315
338
  git__free(idx_name);
316
339
 
340
+ git_mutex_unlock(&p->lock);
341
+
317
342
  return error;
318
343
  }
319
344
 
@@ -389,12 +414,12 @@ int git_packfile_unpack_header(
389
414
  * the maximum deflated object size is 2^137, which is just
390
415
  * insane, so we know won't exceed what we have been given.
391
416
  */
392
- // base = pack_window_open(p, w_curs, *curpos, &left);
417
+ /* base = pack_window_open(p, w_curs, *curpos, &left); */
393
418
  base = git_mwindow_open(mwf, w_curs, *curpos, 20, &left);
394
419
  if (base == NULL)
395
420
  return GIT_EBUFS;
396
421
 
397
- ret = packfile_unpack_header1(&used, size_p, type_p, base, left);
422
+ ret = packfile_unpack_header1(&used, size_p, type_p, base, left);
398
423
  git_mwindow_close(w_curs);
399
424
  if (ret == GIT_EBUFS)
400
425
  return ret;
@@ -786,23 +811,14 @@ git_off_t get_delta_base(
786
811
  *
787
812
  ***********************************************************/
788
813
 
789
- static struct git_pack_file *packfile_alloc(size_t extra)
790
- {
791
- struct git_pack_file *p = git__calloc(1, sizeof(*p) + extra);
792
- if (p != NULL)
793
- p->mwf.fd = -1;
794
- return p;
795
- }
796
-
797
-
798
814
  void git_packfile_free(struct git_pack_file *p)
799
815
  {
800
- assert(p);
816
+ if (!p)
817
+ return;
801
818
 
802
819
  cache_free(&p->bases);
803
820
 
804
821
  git_mwindow_free_all(&p->mwf);
805
- git_mwindow_file_deregister(&p->mwf);
806
822
 
807
823
  if (p->mwf.fd != -1)
808
824
  p_close(p->mwf.fd);
@@ -810,6 +826,8 @@ void git_packfile_free(struct git_pack_file *p)
810
826
  pack_index_free(p);
811
827
 
812
828
  git__free(p->bad_object_sha1);
829
+
830
+ git_mutex_free(&p->lock);
813
831
  git__free(p);
814
832
  }
815
833
 
@@ -820,17 +838,22 @@ static int packfile_open(struct git_pack_file *p)
820
838
  git_oid sha1;
821
839
  unsigned char *idx_sha1;
822
840
 
823
- assert(p->index_map.data);
824
-
825
- if (!p->index_map.data && pack_index_open(p) < 0)
841
+ if (p->index_version == -1 && pack_index_open(p) < 0)
826
842
  return git_odb__error_notfound("failed to open packfile", NULL);
827
843
 
844
+ /* if mwf opened by another thread, return now */
845
+ if (git_mutex_lock(&p->lock) < 0)
846
+ return packfile_error("failed to get lock for open");
847
+
848
+ if (p->mwf.fd >= 0) {
849
+ git_mutex_unlock(&p->lock);
850
+ return 0;
851
+ }
852
+
828
853
  /* TODO: open with noatime */
829
854
  p->mwf.fd = git_futils_open_ro(p->pack_name);
830
- if (p->mwf.fd < 0) {
831
- p->mwf.fd = -1;
832
- return -1;
833
- }
855
+ if (p->mwf.fd < 0)
856
+ goto cleanup;
834
857
 
835
858
  if (p_fstat(p->mwf.fd, &st) < 0 ||
836
859
  git_mwindow_file_register(&p->mwf) < 0)
@@ -871,44 +894,54 @@ static int packfile_open(struct git_pack_file *p)
871
894
 
872
895
  idx_sha1 = ((unsigned char *)p->index_map.data) + p->index_map.len - 40;
873
896
 
874
- if (git_oid_cmp(&sha1, (git_oid *)idx_sha1) == 0)
875
- return 0;
897
+ if (git_oid__cmp(&sha1, (git_oid *)idx_sha1) != 0)
898
+ goto cleanup;
899
+
900
+ git_mutex_unlock(&p->lock);
901
+ return 0;
876
902
 
877
903
  cleanup:
878
904
  giterr_set(GITERR_OS, "Invalid packfile '%s'", p->pack_name);
905
+
879
906
  p_close(p->mwf.fd);
880
907
  p->mwf.fd = -1;
908
+
909
+ git_mutex_unlock(&p->lock);
910
+
881
911
  return -1;
882
912
  }
883
913
 
884
- int git_packfile_check(struct git_pack_file **pack_out, const char *path)
914
+ int git_packfile_alloc(struct git_pack_file **pack_out, const char *path)
885
915
  {
886
916
  struct stat st;
887
917
  struct git_pack_file *p;
888
- size_t path_len;
918
+ size_t path_len = path ? strlen(path) : 0;
889
919
 
890
920
  *pack_out = NULL;
891
- path_len = strlen(path);
892
- p = packfile_alloc(path_len + 2);
921
+
922
+ if (path_len < strlen(".idx"))
923
+ return git_odb__error_notfound("invalid packfile path", NULL);
924
+
925
+ p = git__calloc(1, sizeof(*p) + path_len + 2);
893
926
  GITERR_CHECK_ALLOC(p);
894
927
 
928
+ memcpy(p->pack_name, path, path_len + 1);
929
+
895
930
  /*
896
931
  * Make sure a corresponding .pack file exists and that
897
932
  * the index looks sane.
898
933
  */
899
- path_len -= strlen(".idx");
900
- if (path_len < 1) {
901
- git__free(p);
902
- return git_odb__error_notfound("invalid packfile path", NULL);
903
- }
934
+ if (git__suffixcmp(path, ".idx") == 0) {
935
+ size_t root_len = path_len - strlen(".idx");
904
936
 
905
- memcpy(p->pack_name, path, path_len);
937
+ memcpy(p->pack_name + root_len, ".keep", sizeof(".keep"));
938
+ if (git_path_exists(p->pack_name) == true)
939
+ p->pack_keep = 1;
906
940
 
907
- strcpy(p->pack_name + path_len, ".keep");
908
- if (git_path_exists(p->pack_name) == true)
909
- p->pack_keep = 1;
941
+ memcpy(p->pack_name + root_len, ".pack", sizeof(".pack"));
942
+ path_len = path_len - strlen(".idx") + strlen(".pack");
943
+ }
910
944
 
911
- strcpy(p->pack_name + path_len, ".pack");
912
945
  if (p_stat(p->pack_name, &st) < 0 || !S_ISREG(st.st_mode)) {
913
946
  git__free(p);
914
947
  return git_odb__error_notfound("packfile not found", NULL);
@@ -917,9 +950,17 @@ int git_packfile_check(struct git_pack_file **pack_out, const char *path)
917
950
  /* ok, it looks sane as far as we can check without
918
951
  * actually mapping the pack file.
919
952
  */
953
+ p->mwf.fd = -1;
920
954
  p->mwf.size = st.st_size;
921
955
  p->pack_local = 1;
922
956
  p->mtime = (git_time_t)st.st_mtime;
957
+ p->index_version = -1;
958
+
959
+ if (git_mutex_init(&p->lock)) {
960
+ giterr_set(GITERR_OS, "Failed to initialize packfile mutex");
961
+ git__free(p);
962
+ return -1;
963
+ }
923
964
 
924
965
  /* see if we can parse the sha1 oid in the packfile name */
925
966
  if (path_len < 40 ||
@@ -1034,12 +1075,11 @@ static int pack_entry_find_offset(
1034
1075
 
1035
1076
  *offset_out = 0;
1036
1077
 
1037
- if (index == NULL) {
1078
+ if (p->index_version == -1) {
1038
1079
  int error;
1039
1080
 
1040
1081
  if ((error = pack_index_open(p)) < 0)
1041
1082
  return error;
1042
-
1043
1083
  assert(p->index_map.data);
1044
1084
 
1045
1085
  index = p->index_map.data;
@@ -1099,6 +1139,7 @@ static int pack_entry_find_offset(
1099
1139
  return git_odb__error_notfound("failed to find offset for pack entry", short_oid);
1100
1140
  if (found > 1)
1101
1141
  return git_odb__error_ambiguous("found multiple offsets for pack entry");
1142
+
1102
1143
  *offset_out = nth_packed_object_offset(p, pos);
1103
1144
  git_oid_fromraw(found_oid, current);
1104
1145
 
@@ -1110,6 +1151,7 @@ static int pack_entry_find_offset(
1110
1151
  printf("found lo=%d %s\n", lo, hex_sha1);
1111
1152
  }
1112
1153
  #endif
1154
+
1113
1155
  return 0;
1114
1156
  }
1115
1157
 
@@ -1128,7 +1170,7 @@ int git_pack_entry_find(
1128
1170
  if (len == GIT_OID_HEXSZ && p->num_bad_objects) {
1129
1171
  unsigned i;
1130
1172
  for (i = 0; i < p->num_bad_objects; i++)
1131
- if (git_oid_cmp(short_oid, &p->bad_object_sha1[i]) == 0)
1173
+ if (git_oid__cmp(short_oid, &p->bad_object_sha1[i]) == 0)
1132
1174
  return packfile_error("bad object found in packfile");
1133
1175
  }
1134
1176