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
@@ -12,43 +12,56 @@
12
12
  #include "git2/odb.h"
13
13
 
14
14
  #include "thread-utils.h"
15
+ #include "oidmap.h"
15
16
 
16
- #define GIT_DEFAULT_CACHE_SIZE 128
17
-
18
- typedef void (*git_cached_obj_freeptr)(void *);
17
+ enum {
18
+ GIT_CACHE_STORE_ANY = 0,
19
+ GIT_CACHE_STORE_RAW = 1,
20
+ GIT_CACHE_STORE_PARSED = 2
21
+ };
19
22
 
20
23
  typedef struct {
21
- git_oid oid;
24
+ git_oid oid;
25
+ int16_t type; /* git_otype value */
26
+ uint16_t flags; /* GIT_CACHE_STORE value */
27
+ size_t size;
22
28
  git_atomic refcount;
23
29
  } git_cached_obj;
24
30
 
25
31
  typedef struct {
26
- git_cached_obj **nodes;
27
- git_mutex lock;
28
-
29
- unsigned int lru_count;
30
- size_t size_mask;
31
- git_cached_obj_freeptr free_obj;
32
+ git_oidmap *map;
33
+ git_mutex lock;
34
+ ssize_t used_memory;
32
35
  } git_cache;
33
36
 
34
- int git_cache_init(git_cache *cache, size_t size, git_cached_obj_freeptr free_ptr);
37
+ extern bool git_cache__enabled;
38
+ extern ssize_t git_cache__max_storage;
39
+ extern git_atomic_ssize git_cache__current_storage;
40
+
41
+ int git_cache_set_max_object_size(git_otype type, size_t size);
42
+
43
+ int git_cache_init(git_cache *cache);
35
44
  void git_cache_free(git_cache *cache);
45
+ void git_cache_clear(git_cache *cache);
36
46
 
37
- void *git_cache_try_store(git_cache *cache, void *entry);
38
- void *git_cache_get(git_cache *cache, const git_oid *oid);
47
+ void *git_cache_store_raw(git_cache *cache, git_odb_object *entry);
48
+ void *git_cache_store_parsed(git_cache *cache, git_object *entry);
39
49
 
40
- GIT_INLINE(void) git_cached_obj_incref(void *_obj)
50
+ git_odb_object *git_cache_get_raw(git_cache *cache, const git_oid *oid);
51
+ git_object *git_cache_get_parsed(git_cache *cache, const git_oid *oid);
52
+ void *git_cache_get_any(git_cache *cache, const git_oid *oid);
53
+
54
+ GIT_INLINE(size_t) git_cache_size(git_cache *cache)
41
55
  {
42
- git_cached_obj *obj = _obj;
43
- git_atomic_inc(&obj->refcount);
56
+ return (size_t)kh_size(cache->map);
44
57
  }
45
58
 
46
- GIT_INLINE(void) git_cached_obj_decref(void *_obj, git_cached_obj_freeptr free_obj)
59
+ GIT_INLINE(void) git_cached_obj_incref(void *_obj)
47
60
  {
48
61
  git_cached_obj *obj = _obj;
49
-
50
- if (git_atomic_dec(&obj->refcount) == 0)
51
- free_obj(obj);
62
+ git_atomic_inc(&obj->refcount);
52
63
  }
53
64
 
65
+ void git_cached_obj_decref(void *_obj);
66
+
54
67
  #endif
@@ -16,9 +16,11 @@
16
16
  #include "git2/config.h"
17
17
  #include "git2/diff.h"
18
18
  #include "git2/submodule.h"
19
+ #include "git2/sys/index.h"
19
20
 
20
21
  #include "refs.h"
21
22
  #include "repository.h"
23
+ #include "index.h"
22
24
  #include "filter.h"
23
25
  #include "blob.h"
24
26
  #include "diff.h"
@@ -138,7 +140,7 @@ static bool checkout_is_workdir_modified(
138
140
  if (!sm_oid)
139
141
  return false;
140
142
 
141
- return (git_oid_cmp(&baseitem->oid, sm_oid) != 0);
143
+ return (git_oid__cmp(&baseitem->oid, sm_oid) != 0);
142
144
  }
143
145
 
144
146
  /* Look at the cache to decide if the workdir is modified. If not,
@@ -149,7 +151,7 @@ static bool checkout_is_workdir_modified(
149
151
  if (wditem->mtime.seconds == ie->mtime.seconds &&
150
152
  wditem->mtime.nanoseconds == ie->mtime.nanoseconds &&
151
153
  wditem->file_size == ie->file_size)
152
- return (git_oid_cmp(&baseitem->oid, &ie->oid) != 0);
154
+ return (git_oid__cmp(&baseitem->oid, &ie->oid) != 0);
153
155
  }
154
156
 
155
157
  /* depending on where base is coming from, we may or may not know
@@ -163,7 +165,7 @@ static bool checkout_is_workdir_modified(
163
165
  wditem->file_size, &oid) < 0)
164
166
  return false;
165
167
 
166
- return (git_oid_cmp(&baseitem->oid, &oid) != 0);
168
+ return (git_oid__cmp(&baseitem->oid, &oid) != 0);
167
169
  }
168
170
 
169
171
  #define CHECKOUT_ACTION_IF(FLAG,YES,NO) \
@@ -188,6 +190,10 @@ static int checkout_action_common(
188
190
  action = (action & ~CHECKOUT_ACTION__UPDATE_BLOB) |
189
191
  CHECKOUT_ACTION__UPDATE_SUBMODULE;
190
192
 
193
+ /* to "update" a symlink, we must remove the old one first */
194
+ if (delta->new_file.mode == GIT_FILEMODE_LINK && wd != NULL)
195
+ action |= CHECKOUT_ACTION__REMOVE;
196
+
191
197
  notify = GIT_CHECKOUT_NOTIFY_UPDATED;
192
198
  }
193
199
 
@@ -214,9 +220,11 @@ static int checkout_action_no_wd(
214
220
  action = CHECKOUT_ACTION_IF(SAFE_CREATE, UPDATE_BLOB, NONE);
215
221
  break;
216
222
  case GIT_DELTA_ADDED: /* case 2 or 28 (and 5 but not really) */
217
- case GIT_DELTA_MODIFIED: /* case 13 (and 35 but not really) */
218
223
  action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
219
224
  break;
225
+ case GIT_DELTA_MODIFIED: /* case 13 (and 35 but not really) */
226
+ action = CHECKOUT_ACTION_IF(SAFE_CREATE, UPDATE_BLOB, CONFLICT);
227
+ break;
220
228
  case GIT_DELTA_TYPECHANGE: /* case 21 (B->T) and 28 (T->B)*/
221
229
  if (delta->new_file.mode == GIT_FILEMODE_TREE)
222
230
  action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
@@ -466,6 +474,7 @@ static int checkout_action(
466
474
  int cmp = -1, act;
467
475
  int (*strcomp)(const char *, const char *) = data->diff->strcomp;
468
476
  int (*pfxcomp)(const char *str, const char *pfx) = data->diff->pfxcomp;
477
+ int error;
469
478
 
470
479
  /* move workdir iterator to follow along with deltas */
471
480
 
@@ -489,9 +498,9 @@ static int checkout_action(
489
498
  if (cmp == 0) {
490
499
  if (wd->mode == GIT_FILEMODE_TREE) {
491
500
  /* case 2 - entry prefixed by workdir tree */
492
- if (git_iterator_advance_into(&wd, workdir) < 0)
501
+ error = git_iterator_advance_into_or_over(&wd, workdir);
502
+ if (error && error != GIT_ITEROVER)
493
503
  goto fail;
494
-
495
504
  *wditem_ptr = wd;
496
505
  continue;
497
506
  }
@@ -506,8 +515,10 @@ static int checkout_action(
506
515
  }
507
516
 
508
517
  /* case 1 - handle wd item (if it matches pathspec) */
509
- if (checkout_action_wd_only(data, workdir, wd, pathspec) < 0 ||
510
- git_iterator_advance(&wd, workdir) < 0)
518
+ if (checkout_action_wd_only(data, workdir, wd, pathspec) < 0)
519
+ goto fail;
520
+ if ((error = git_iterator_advance(&wd, workdir)) < 0 &&
521
+ error != GIT_ITEROVER)
511
522
  goto fail;
512
523
 
513
524
  *wditem_ptr = wd;
@@ -530,8 +541,9 @@ static int checkout_action(
530
541
  if (delta->status == GIT_DELTA_TYPECHANGE) {
531
542
  if (delta->old_file.mode == GIT_FILEMODE_TREE) {
532
543
  act = checkout_action_with_wd(data, delta, wd);
533
- if (git_iterator_advance_into(&wd, workdir) < 0)
534
- wd = NULL;
544
+ if ((error = git_iterator_advance_into(&wd, workdir)) < 0 &&
545
+ error != GIT_ENOTFOUND)
546
+ goto fail;
535
547
  *wditem_ptr = wd;
536
548
  return act;
537
549
  }
@@ -541,8 +553,9 @@ static int checkout_action(
541
553
  delta->old_file.mode == GIT_FILEMODE_COMMIT)
542
554
  {
543
555
  act = checkout_action_with_wd(data, delta, wd);
544
- if (git_iterator_advance(&wd, workdir) < 0)
545
- wd = NULL;
556
+ if ((error = git_iterator_advance(&wd, workdir)) < 0 &&
557
+ error != GIT_ITEROVER)
558
+ goto fail;
546
559
  *wditem_ptr = wd;
547
560
  return act;
548
561
  }
@@ -573,6 +586,9 @@ static int checkout_remaining_wd_items(
573
586
  error = git_iterator_advance(&wd, workdir);
574
587
  }
575
588
 
589
+ if (error == GIT_ITEROVER)
590
+ error = 0;
591
+
576
592
  return error;
577
593
  }
578
594
 
@@ -594,7 +610,8 @@ static int checkout_get_actions(
594
610
  git_pathspec_init(&pathspec, &data->opts.paths, &pathpool) < 0)
595
611
  return -1;
596
612
 
597
- if ((error = git_iterator_current(&wditem, workdir)) < 0)
613
+ if ((error = git_iterator_current(&wditem, workdir)) < 0 &&
614
+ error != GIT_ITEROVER)
598
615
  goto fail;
599
616
 
600
617
  deltas = &data->diff->deltas;
@@ -667,33 +684,26 @@ static int buffer_to_file(
667
684
  int file_open_flags,
668
685
  mode_t file_mode)
669
686
  {
670
- int fd, error;
687
+ int error;
671
688
 
672
689
  if ((error = git_futils_mkpath2file(path, dir_mode)) < 0)
673
690
  return error;
674
691
 
675
- if ((fd = p_open(path, file_open_flags, file_mode)) < 0) {
676
- giterr_set(GITERR_OS, "Could not open '%s' for writing", path);
677
- return fd;
678
- }
679
-
680
- if ((error = p_write(fd, git_buf_cstr(buffer), git_buf_len(buffer))) < 0) {
681
- giterr_set(GITERR_OS, "Could not write to '%s'", path);
682
- (void)p_close(fd);
683
- } else {
684
- if ((error = p_close(fd)) < 0)
685
- giterr_set(GITERR_OS, "Error while closing '%s'", path);
692
+ if ((error = git_futils_writebuffer(
693
+ buffer, path, file_open_flags, file_mode)) < 0)
694
+ return error;
686
695
 
687
- if ((error = p_stat(path, st)) < 0)
688
- giterr_set(GITERR_OS, "Error while statting '%s'", path);
696
+ if (st != NULL && (error = p_stat(path, st)) < 0) {
697
+ giterr_set(GITERR_OS, "Error while statting '%s'", path);
698
+ return error;
689
699
  }
690
700
 
691
- if (!error &&
692
- (file_mode & 0100) != 0 &&
693
- (error = p_chmod(path, file_mode)) < 0)
701
+ if ((file_mode & 0100) != 0 && (error = p_chmod(path, file_mode)) < 0) {
694
702
  giterr_set(GITERR_OS, "Failed to set permissions on '%s'", path);
703
+ return error;
704
+ }
695
705
 
696
- return error;
706
+ return 0;
697
707
  }
698
708
 
699
709
  static int blob_content_to_file(
@@ -710,8 +720,8 @@ static int blob_content_to_file(
710
720
  git_vector filters = GIT_VECTOR_INIT;
711
721
 
712
722
  /* Create a fake git_buf from the blob raw data... */
713
- filtered.ptr = blob->odb_object->raw.data;
714
- filtered.size = blob->odb_object->raw.len;
723
+ filtered.ptr = (void *)git_blob_rawcontent(blob);
724
+ filtered.size = (size_t)git_blob_rawsize(blob);
715
725
  /* ... and make sure it doesn't get unexpectedly freed */
716
726
  dont_free_filtered = true;
717
727
 
@@ -759,17 +769,24 @@ cleanup:
759
769
  }
760
770
 
761
771
  static int blob_content_to_link(
762
- struct stat *st, git_blob *blob, const char *path, int can_symlink)
772
+ struct stat *st,
773
+ git_blob *blob,
774
+ const char *path,
775
+ mode_t dir_mode,
776
+ int can_symlink)
763
777
  {
764
778
  git_buf linktarget = GIT_BUF_INIT;
765
779
  int error;
766
780
 
781
+ if ((error = git_futils_mkpath2file(path, dir_mode)) < 0)
782
+ return error;
783
+
767
784
  if ((error = git_blob__getbuf(&linktarget, blob)) < 0)
768
785
  return error;
769
786
 
770
787
  if (can_symlink) {
771
788
  if ((error = p_symlink(git_buf_cstr(&linktarget), path)) < 0)
772
- giterr_set(GITERR_CHECKOUT, "Could not create symlink %s\n", path);
789
+ giterr_set(GITERR_OS, "Could not create symlink %s\n", path);
773
790
  } else {
774
791
  error = git_futils_fake_symlink(git_buf_cstr(&linktarget), path);
775
792
  }
@@ -804,6 +821,31 @@ static int checkout_update_index(
804
821
  return git_index_add(data->index, &entry);
805
822
  }
806
823
 
824
+ static int checkout_submodule_update_index(
825
+ checkout_data *data,
826
+ const git_diff_file *file)
827
+ {
828
+ struct stat st;
829
+
830
+ /* update the index unless prevented */
831
+ if ((data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) != 0)
832
+ return 0;
833
+
834
+ git_buf_truncate(&data->path, data->workdir_len);
835
+ if (git_buf_puts(&data->path, file->path) < 0)
836
+ return -1;
837
+
838
+ if (p_stat(git_buf_cstr(&data->path), &st) < 0) {
839
+ giterr_set(
840
+ GITERR_CHECKOUT, "Could not stat submodule %s\n", file->path);
841
+ return GIT_ENOTFOUND;
842
+ }
843
+
844
+ st.st_mode = GIT_FILEMODE_COMMIT;
845
+
846
+ return checkout_update_index(data, file, &st);
847
+ }
848
+
807
849
  static int checkout_submodule(
808
850
  checkout_data *data,
809
851
  const git_diff_file *file)
@@ -816,12 +858,21 @@ static int checkout_submodule(
816
858
  return 0;
817
859
 
818
860
  if ((error = git_futils_mkdir(
819
- file->path, git_repository_workdir(data->repo),
861
+ file->path, data->opts.target_directory,
820
862
  data->opts.dir_mode, GIT_MKDIR_PATH)) < 0)
821
863
  return error;
822
864
 
823
- if ((error = git_submodule_lookup(&sm, data->repo, file->path)) < 0)
865
+ if ((error = git_submodule_lookup(&sm, data->repo, file->path)) < 0) {
866
+ /* I've observed repos with submodules in the tree that do not
867
+ * have a .gitmodules - core Git just makes an empty directory
868
+ */
869
+ if (error == GIT_ENOTFOUND) {
870
+ giterr_clear();
871
+ return checkout_submodule_update_index(data, file);
872
+ }
873
+
824
874
  return error;
875
+ }
825
876
 
826
877
  /* TODO: Support checkout_strategy options. Two circumstances:
827
878
  * 1 - submodule already checked out, but we need to move the HEAD
@@ -832,26 +883,7 @@ static int checkout_submodule(
832
883
  * command should probably be able to. Do we need a submodule callback?
833
884
  */
834
885
 
835
- /* update the index unless prevented */
836
- if ((data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0) {
837
- struct stat st;
838
-
839
- git_buf_truncate(&data->path, data->workdir_len);
840
- if (git_buf_puts(&data->path, file->path) < 0)
841
- return -1;
842
-
843
- if ((error = p_stat(git_buf_cstr(&data->path), &st)) < 0) {
844
- giterr_set(
845
- GITERR_CHECKOUT, "Could not stat submodule %s\n", file->path);
846
- return error;
847
- }
848
-
849
- st.st_mode = GIT_FILEMODE_COMMIT;
850
-
851
- error = checkout_update_index(data, file, &st);
852
- }
853
-
854
- return error;
886
+ return checkout_submodule_update_index(data, file);
855
887
  }
856
888
 
857
889
  static void report_progress(
@@ -909,7 +941,7 @@ static int checkout_blob(
909
941
 
910
942
  if (S_ISLNK(file->mode))
911
943
  error = blob_content_to_link(
912
- &st, blob, git_buf_cstr(&data->path), data->can_symlink);
944
+ &st, blob, git_buf_cstr(&data->path), data->opts.dir_mode, data->can_symlink);
913
945
  else
914
946
  error = blob_content_to_file(
915
947
  &st, blob, git_buf_cstr(&data->path), file->mode, &data->opts);
@@ -950,6 +982,9 @@ static int checkout_remove_the_old(
950
982
  uint32_t flg = GIT_RMDIR_EMPTY_PARENTS |
951
983
  GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_REMOVE_BLOCKERS;
952
984
 
985
+ if (data->opts.checkout_strategy & GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES)
986
+ flg |= GIT_RMDIR_SKIP_NONEMPTY;
987
+
953
988
  git_buf_truncate(&data->path, data->workdir_len);
954
989
 
955
990
  git_vector_foreach(&data->diff->deltas, i, delta) {
@@ -995,7 +1030,7 @@ static int checkout_deferred_remove(git_repository *repo, const char *path)
995
1030
  {
996
1031
  #if 0
997
1032
  int error = git_futils_rmdir_r(
998
- path, git_repository_workdir(repo), GIT_RMDIR_EMPTY_PARENTS);
1033
+ path, data->opts.target_directory, GIT_RMDIR_EMPTY_PARENTS);
999
1034
 
1000
1035
  if (error == GIT_ENOTFOUND) {
1001
1036
  error = 0;
@@ -1119,7 +1154,6 @@ static int checkout_data_init(
1119
1154
  git_checkout_opts *proposed)
1120
1155
  {
1121
1156
  int error = 0;
1122
- git_config *cfg;
1123
1157
  git_repository *repo = git_iterator_owner(target);
1124
1158
 
1125
1159
  memset(data, 0, sizeof(*data));
@@ -1129,10 +1163,8 @@ static int checkout_data_init(
1129
1163
  return -1;
1130
1164
  }
1131
1165
 
1132
- if ((error = git_repository__ensure_not_bare(repo, "checkout")) < 0)
1133
- return error;
1134
-
1135
- if ((error = git_repository_config__weakptr(&cfg, repo)) < 0)
1166
+ if ((!proposed || !proposed->target_directory) &&
1167
+ (error = git_repository__ensure_not_bare(repo, "checkout")) < 0)
1136
1168
  return error;
1137
1169
 
1138
1170
  data->repo = repo;
@@ -1145,9 +1177,19 @@ static int checkout_data_init(
1145
1177
  else
1146
1178
  memmove(&data->opts, proposed, sizeof(git_checkout_opts));
1147
1179
 
1180
+ if (!data->opts.target_directory)
1181
+ data->opts.target_directory = git_repository_workdir(repo);
1182
+ else if (!git_path_isdir(data->opts.target_directory) &&
1183
+ (error = git_futils_mkdir(data->opts.target_directory, NULL,
1184
+ GIT_DIR_MODE, GIT_MKDIR_VERIFY_DIR)) < 0)
1185
+ goto cleanup;
1186
+
1148
1187
  /* refresh config and index content unless NO_REFRESH is given */
1149
1188
  if ((data->opts.checkout_strategy & GIT_CHECKOUT_NO_REFRESH) == 0) {
1150
- if ((error = git_config_refresh(cfg)) < 0)
1189
+ git_config *cfg;
1190
+
1191
+ if ((error = git_repository_config__weakptr(&cfg, repo)) < 0 ||
1192
+ (error = git_config_refresh(cfg)) < 0)
1151
1193
  goto cleanup;
1152
1194
 
1153
1195
  /* if we are checking out the index, don't reload,
@@ -1184,19 +1226,13 @@ static int checkout_data_init(
1184
1226
 
1185
1227
  data->pfx = git_pathspec_prefix(&data->opts.paths);
1186
1228
 
1187
- error = git_config_get_bool(&data->can_symlink, cfg, "core.symlinks");
1188
- if (error < 0) {
1189
- if (error != GIT_ENOTFOUND)
1190
- goto cleanup;
1191
-
1192
- /* If "core.symlinks" is not found anywhere, default to true. */
1193
- data->can_symlink = true;
1194
- giterr_clear();
1195
- error = 0;
1196
- }
1229
+ if ((error = git_repository__cvar(
1230
+ &data->can_symlink, repo, GIT_CVAR_SYMLINKS)) < 0)
1231
+ goto cleanup;
1197
1232
 
1198
1233
  if (!data->opts.baseline) {
1199
1234
  data->opts_free_baseline = true;
1235
+
1200
1236
  error = checkout_lookup_head_tree(&data->opts.baseline, repo);
1201
1237
 
1202
1238
  if (error == GIT_EORPHANEDHEAD) {
@@ -1210,7 +1246,8 @@ static int checkout_data_init(
1210
1246
 
1211
1247
  if ((error = git_vector_init(&data->removes, 0, git__strcmp_cb)) < 0 ||
1212
1248
  (error = git_pool_init(&data->pool, 1, 0)) < 0 ||
1213
- (error = git_buf_puts(&data->path, git_repository_workdir(repo))) < 0)
1249
+ (error = git_buf_puts(&data->path, data->opts.target_directory)) < 0 ||
1250
+ (error = git_path_to_dir(&data->path)) < 0)
1214
1251
  goto cleanup;
1215
1252
 
1216
1253
  data->workdir_len = git_buf_len(&data->path);
@@ -1258,11 +1295,13 @@ int git_checkout_iterator(
1258
1295
  GIT_ITERATOR_IGNORE_CASE : GIT_ITERATOR_DONT_IGNORE_CASE;
1259
1296
 
1260
1297
  if ((error = git_iterator_reset(target, data.pfx, data.pfx)) < 0 ||
1261
- (error = git_iterator_for_workdir(
1262
- &workdir, data.repo, iterflags | GIT_ITERATOR_DONT_AUTOEXPAND,
1298
+ (error = git_iterator_for_workdir_ext(
1299
+ &workdir, data.repo, data.opts.target_directory,
1300
+ iterflags | GIT_ITERATOR_DONT_AUTOEXPAND,
1263
1301
  data.pfx, data.pfx)) < 0 ||
1264
1302
  (error = git_iterator_for_tree(
1265
- &baseline, data.opts.baseline, iterflags, data.pfx, data.pfx)) < 0)
1303
+ &baseline, data.opts.baseline,
1304
+ iterflags, data.pfx, data.pfx)) < 0)
1266
1305
  goto cleanup;
1267
1306
 
1268
1307
  /* Should not have case insensitivity mismatch */
@@ -1330,8 +1369,19 @@ int git_checkout_index(
1330
1369
  int error;
1331
1370
  git_iterator *index_i;
1332
1371
 
1333
- if ((error = git_repository__ensure_not_bare(repo, "checkout index")) < 0)
1334
- return error;
1372
+ if (!index && !repo) {
1373
+ giterr_set(GITERR_CHECKOUT,
1374
+ "Must provide either repository or index to checkout");
1375
+ return -1;
1376
+ }
1377
+ if (index && repo && git_index_owner(index) != repo) {
1378
+ giterr_set(GITERR_CHECKOUT,
1379
+ "Index to checkout does not match repository");
1380
+ return -1;
1381
+ }
1382
+
1383
+ if (!repo)
1384
+ repo = git_index_owner(index);
1335
1385
 
1336
1386
  if (!index && (error = git_repository_index__weakptr(&index, repo)) < 0)
1337
1387
  return error;
@@ -1355,8 +1405,19 @@ int git_checkout_tree(
1355
1405
  git_tree *tree = NULL;
1356
1406
  git_iterator *tree_i = NULL;
1357
1407
 
1358
- if ((error = git_repository__ensure_not_bare(repo, "checkout tree")) < 0)
1359
- return error;
1408
+ if (!treeish && !repo) {
1409
+ giterr_set(GITERR_CHECKOUT,
1410
+ "Must provide either repository or tree to checkout");
1411
+ return -1;
1412
+ }
1413
+ if (treeish && repo && git_object_owner(treeish) != repo) {
1414
+ giterr_set(GITERR_CHECKOUT,
1415
+ "Object to checkout does not match repository");
1416
+ return -1;
1417
+ }
1418
+
1419
+ if (!repo)
1420
+ repo = git_object_owner(treeish);
1360
1421
 
1361
1422
  if (git_object_peel((git_object **)&tree, treeish, GIT_OBJ_TREE) < 0) {
1362
1423
  giterr_set(
@@ -1381,8 +1442,7 @@ int git_checkout_head(
1381
1442
  git_tree *head = NULL;
1382
1443
  git_iterator *head_i = NULL;
1383
1444
 
1384
- if ((error = git_repository__ensure_not_bare(repo, "checkout head")) < 0)
1385
- return error;
1445
+ assert(repo);
1386
1446
 
1387
1447
  if (!(error = checkout_lookup_head_tree(&head, repo)) &&
1388
1448
  !(error = git_iterator_for_tree(&head_i, head, 0, NULL, NULL)))