rugged 0.17.0.b7 → 0.18.0.b1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (310) hide show
  1. data/LICENSE +1 -1
  2. data/README.md +88 -32
  3. data/ext/rugged/extconf.rb +4 -2
  4. data/ext/rugged/rugged.c +72 -10
  5. data/ext/rugged/rugged.h +14 -10
  6. data/ext/rugged/rugged_blob.c +8 -10
  7. data/ext/rugged/rugged_branch.c +11 -14
  8. data/ext/rugged/rugged_commit.c +31 -24
  9. data/ext/rugged/rugged_config.c +2 -2
  10. data/ext/rugged/rugged_index.c +133 -198
  11. data/ext/rugged/rugged_note.c +372 -0
  12. data/ext/rugged/rugged_object.c +50 -22
  13. data/ext/rugged/rugged_reference.c +122 -130
  14. data/ext/rugged/rugged_remote.c +72 -29
  15. data/ext/rugged/rugged_repo.c +402 -20
  16. data/ext/rugged/rugged_revwalk.c +7 -3
  17. data/ext/rugged/rugged_settings.c +110 -0
  18. data/ext/rugged/rugged_signature.c +23 -7
  19. data/ext/rugged/rugged_tag.c +32 -16
  20. data/ext/rugged/rugged_tree.c +44 -15
  21. data/lib/rugged.rb +1 -0
  22. data/lib/rugged/index.rb +8 -0
  23. data/lib/rugged/remote.rb +13 -0
  24. data/lib/rugged/repository.rb +3 -3
  25. data/lib/rugged/version.rb +1 -1
  26. data/test/blob_test.rb +13 -15
  27. data/test/branch_test.rb +32 -67
  28. data/test/commit_test.rb +50 -12
  29. data/test/config_test.rb +12 -11
  30. data/test/coverage/HEAD.json +1 -1
  31. data/test/coverage/cover.rb +40 -21
  32. data/test/errors_test.rb +34 -0
  33. data/test/fixtures/alternate/objects/14/6ae76773c91e3b1d00cf7a338ec55ae58297e2 +0 -0
  34. data/test/fixtures/alternate/objects/14/9c32d47e99d0a3572ff1e70a2e0051bbf347a9 +0 -0
  35. data/test/fixtures/alternate/objects/14/fb3108588f9421bf764041e5e3ac305eb6277f +0 -0
  36. data/test/fixtures/testrepo.git/logs/refs/notes/commits +1 -0
  37. data/test/fixtures/testrepo.git/objects/44/1034f860c1d5d90e4188d11ae0d325176869a8 +1 -0
  38. data/test/fixtures/testrepo.git/objects/60/d415052a33de2150bf68757f6461df4f563ae4 +0 -0
  39. data/test/fixtures/testrepo.git/objects/68/8a8f4ef7496901d15322972f96e212a9e466cc +1 -0
  40. data/test/fixtures/testrepo.git/objects/94/eca2de348d5f672faf56b0decafa5937e3235e +0 -0
  41. data/test/fixtures/testrepo.git/objects/9b/7384fe1676186192842f5d3e129457b62db9e3 +0 -0
  42. data/test/fixtures/testrepo.git/objects/b7/4713326bc972cc15751ed504dca6f6f3b91f7a +3 -0
  43. data/test/fixtures/testrepo.git/refs/notes/commits +1 -0
  44. data/test/index_test.rb +65 -69
  45. data/test/lib_test.rb +76 -11
  46. data/test/note_test.rb +158 -0
  47. data/test/object_test.rb +8 -11
  48. data/test/reference_test.rb +77 -85
  49. data/test/remote_test.rb +86 -8
  50. data/test/repo_pack_test.rb +9 -7
  51. data/test/repo_reset_test.rb +80 -0
  52. data/test/repo_test.rb +176 -53
  53. data/test/tag_test.rb +44 -7
  54. data/test/test_helper.rb +63 -35
  55. data/test/tree_test.rb +34 -13
  56. data/test/walker_test.rb +14 -14
  57. data/vendor/libgit2/Makefile.embed +1 -1
  58. data/vendor/libgit2/deps/http-parser/http_parser.c +974 -578
  59. data/vendor/libgit2/deps/http-parser/http_parser.h +106 -70
  60. data/vendor/libgit2/deps/regex/regcomp.c +7 -6
  61. data/vendor/libgit2/deps/regex/regex_internal.c +1 -1
  62. data/vendor/libgit2/deps/regex/regex_internal.h +12 -3
  63. data/vendor/libgit2/deps/regex/regexec.c +5 -5
  64. data/vendor/libgit2/include/git2.h +5 -1
  65. data/vendor/libgit2/include/git2/attr.h +4 -2
  66. data/vendor/libgit2/include/git2/blob.h +39 -12
  67. data/vendor/libgit2/include/git2/branch.h +123 -35
  68. data/vendor/libgit2/include/git2/checkout.h +206 -48
  69. data/vendor/libgit2/include/git2/clone.h +72 -27
  70. data/vendor/libgit2/include/git2/commit.h +20 -17
  71. data/vendor/libgit2/include/git2/common.h +67 -1
  72. data/vendor/libgit2/include/git2/config.h +81 -60
  73. data/vendor/libgit2/include/git2/cred_helpers.h +53 -0
  74. data/vendor/libgit2/include/git2/diff.h +459 -150
  75. data/vendor/libgit2/include/git2/errors.h +9 -1
  76. data/vendor/libgit2/include/git2/graph.h +41 -0
  77. data/vendor/libgit2/include/git2/ignore.h +7 -6
  78. data/vendor/libgit2/include/git2/index.h +323 -97
  79. data/vendor/libgit2/include/git2/indexer.h +27 -59
  80. data/vendor/libgit2/include/git2/inttypes.h +4 -0
  81. data/vendor/libgit2/include/git2/merge.h +13 -3
  82. data/vendor/libgit2/include/git2/message.h +14 -8
  83. data/vendor/libgit2/include/git2/net.h +9 -7
  84. data/vendor/libgit2/include/git2/notes.h +88 -29
  85. data/vendor/libgit2/include/git2/object.h +16 -6
  86. data/vendor/libgit2/include/git2/odb.h +80 -17
  87. data/vendor/libgit2/include/git2/odb_backend.h +47 -11
  88. data/vendor/libgit2/include/git2/oid.h +26 -17
  89. data/vendor/libgit2/include/git2/pack.h +62 -8
  90. data/vendor/libgit2/include/git2/push.h +131 -0
  91. data/vendor/libgit2/include/git2/refdb.h +103 -0
  92. data/vendor/libgit2/include/git2/refdb_backend.h +109 -0
  93. data/vendor/libgit2/include/git2/reflog.h +30 -21
  94. data/vendor/libgit2/include/git2/refs.h +215 -193
  95. data/vendor/libgit2/include/git2/refspec.h +22 -2
  96. data/vendor/libgit2/include/git2/remote.h +158 -37
  97. data/vendor/libgit2/include/git2/repository.h +150 -31
  98. data/vendor/libgit2/include/git2/reset.h +43 -9
  99. data/vendor/libgit2/include/git2/revparse.h +48 -4
  100. data/vendor/libgit2/include/git2/revwalk.h +25 -10
  101. data/vendor/libgit2/include/git2/signature.h +20 -12
  102. data/vendor/libgit2/include/git2/stash.h +121 -0
  103. data/vendor/libgit2/include/git2/status.h +122 -53
  104. data/vendor/libgit2/include/git2/strarray.h +17 -11
  105. data/vendor/libgit2/include/git2/submodule.h +42 -7
  106. data/vendor/libgit2/include/git2/tag.h +72 -59
  107. data/vendor/libgit2/include/git2/threads.h +4 -2
  108. data/vendor/libgit2/include/git2/trace.h +68 -0
  109. data/vendor/libgit2/include/git2/transport.h +328 -0
  110. data/vendor/libgit2/include/git2/tree.h +149 -120
  111. data/vendor/libgit2/include/git2/types.h +13 -12
  112. data/vendor/libgit2/include/git2/version.h +3 -3
  113. data/vendor/libgit2/src/amiga/map.c +2 -2
  114. data/vendor/libgit2/src/attr.c +58 -48
  115. data/vendor/libgit2/src/attr.h +4 -18
  116. data/vendor/libgit2/src/attr_file.c +30 -6
  117. data/vendor/libgit2/src/attr_file.h +6 -8
  118. data/vendor/libgit2/src/attrcache.h +24 -0
  119. data/vendor/libgit2/src/blob.c +30 -7
  120. data/vendor/libgit2/src/blob.h +1 -1
  121. data/vendor/libgit2/src/branch.c +361 -68
  122. data/vendor/libgit2/src/branch.h +17 -0
  123. data/vendor/libgit2/src/bswap.h +1 -1
  124. data/vendor/libgit2/src/buf_text.c +291 -0
  125. data/vendor/libgit2/src/buf_text.h +122 -0
  126. data/vendor/libgit2/src/buffer.c +27 -101
  127. data/vendor/libgit2/src/buffer.h +54 -39
  128. data/vendor/libgit2/src/cache.c +15 -6
  129. data/vendor/libgit2/src/cache.h +1 -1
  130. data/vendor/libgit2/src/cc-compat.h +3 -1
  131. data/vendor/libgit2/src/checkout.c +1165 -222
  132. data/vendor/libgit2/src/checkout.h +24 -0
  133. data/vendor/libgit2/src/clone.c +171 -86
  134. data/vendor/libgit2/src/commit.c +44 -45
  135. data/vendor/libgit2/src/commit.h +3 -3
  136. data/vendor/libgit2/src/commit_list.c +194 -0
  137. data/vendor/libgit2/src/commit_list.h +49 -0
  138. data/vendor/libgit2/src/common.h +44 -10
  139. data/vendor/libgit2/src/compress.c +1 -1
  140. data/vendor/libgit2/src/compress.h +1 -1
  141. data/vendor/libgit2/src/config.c +211 -124
  142. data/vendor/libgit2/src/config.h +23 -4
  143. data/vendor/libgit2/src/config_cache.c +2 -2
  144. data/vendor/libgit2/src/config_file.c +129 -53
  145. data/vendor/libgit2/src/config_file.h +10 -8
  146. data/vendor/libgit2/src/crlf.c +66 -67
  147. data/vendor/libgit2/src/date.c +12 -12
  148. data/vendor/libgit2/src/delta-apply.c +14 -1
  149. data/vendor/libgit2/src/delta-apply.h +18 -1
  150. data/vendor/libgit2/src/delta.c +40 -107
  151. data/vendor/libgit2/src/delta.h +19 -17
  152. data/vendor/libgit2/src/diff.c +347 -496
  153. data/vendor/libgit2/src/diff.h +27 -1
  154. data/vendor/libgit2/src/diff_output.c +564 -249
  155. data/vendor/libgit2/src/diff_output.h +15 -8
  156. data/vendor/libgit2/src/diff_tform.c +687 -0
  157. data/vendor/libgit2/src/errors.c +27 -36
  158. data/vendor/libgit2/src/fetch.c +13 -351
  159. data/vendor/libgit2/src/fetch.h +13 -3
  160. data/vendor/libgit2/src/fetchhead.c +295 -0
  161. data/vendor/libgit2/src/fetchhead.h +34 -0
  162. data/vendor/libgit2/src/filebuf.c +42 -15
  163. data/vendor/libgit2/src/filebuf.h +4 -2
  164. data/vendor/libgit2/src/fileops.c +466 -113
  165. data/vendor/libgit2/src/fileops.h +154 -28
  166. data/vendor/libgit2/src/filter.c +3 -75
  167. data/vendor/libgit2/src/filter.h +1 -29
  168. data/vendor/libgit2/src/fnmatch.c +1 -1
  169. data/vendor/libgit2/src/fnmatch.h +1 -1
  170. data/vendor/libgit2/src/global.c +54 -10
  171. data/vendor/libgit2/src/global.h +10 -1
  172. data/vendor/libgit2/src/graph.c +178 -0
  173. data/vendor/libgit2/src/hash.c +25 -52
  174. data/vendor/libgit2/src/hash.h +21 -9
  175. data/vendor/libgit2/src/{sha1/sha1.c → hash/hash_generic.c} +20 -12
  176. data/vendor/libgit2/src/hash/hash_generic.h +24 -0
  177. data/vendor/libgit2/src/hash/hash_openssl.h +45 -0
  178. data/vendor/libgit2/src/hash/hash_win32.c +291 -0
  179. data/vendor/libgit2/src/hash/hash_win32.h +140 -0
  180. data/vendor/libgit2/src/hashsig.c +368 -0
  181. data/vendor/libgit2/src/hashsig.h +72 -0
  182. data/vendor/libgit2/src/ignore.c +22 -15
  183. data/vendor/libgit2/src/ignore.h +6 -1
  184. data/vendor/libgit2/src/index.c +770 -171
  185. data/vendor/libgit2/src/index.h +13 -5
  186. data/vendor/libgit2/src/indexer.c +286 -431
  187. data/vendor/libgit2/src/iterator.c +854 -466
  188. data/vendor/libgit2/src/iterator.h +134 -109
  189. data/vendor/libgit2/src/map.h +1 -1
  190. data/vendor/libgit2/src/merge.c +296 -0
  191. data/vendor/libgit2/src/merge.h +22 -0
  192. data/vendor/libgit2/src/message.c +1 -1
  193. data/vendor/libgit2/src/message.h +1 -1
  194. data/vendor/libgit2/src/mwindow.c +35 -30
  195. data/vendor/libgit2/src/mwindow.h +2 -2
  196. data/vendor/libgit2/src/netops.c +162 -98
  197. data/vendor/libgit2/src/netops.h +50 -15
  198. data/vendor/libgit2/src/notes.c +109 -58
  199. data/vendor/libgit2/src/notes.h +2 -1
  200. data/vendor/libgit2/src/object.c +46 -57
  201. data/vendor/libgit2/src/object.h +1 -8
  202. data/vendor/libgit2/src/odb.c +151 -40
  203. data/vendor/libgit2/src/odb.h +5 -1
  204. data/vendor/libgit2/src/odb_loose.c +4 -5
  205. data/vendor/libgit2/src/odb_pack.c +122 -80
  206. data/vendor/libgit2/src/offmap.h +65 -0
  207. data/vendor/libgit2/src/oid.c +12 -4
  208. data/vendor/libgit2/src/oidmap.h +1 -1
  209. data/vendor/libgit2/src/pack-objects.c +88 -61
  210. data/vendor/libgit2/src/pack-objects.h +8 -8
  211. data/vendor/libgit2/src/pack.c +293 -28
  212. data/vendor/libgit2/src/pack.h +49 -4
  213. data/vendor/libgit2/src/path.c +103 -14
  214. data/vendor/libgit2/src/path.h +23 -7
  215. data/vendor/libgit2/src/pathspec.c +168 -0
  216. data/vendor/libgit2/src/pathspec.h +40 -0
  217. data/vendor/libgit2/src/pool.c +29 -4
  218. data/vendor/libgit2/src/pool.h +8 -1
  219. data/vendor/libgit2/src/posix.c +26 -27
  220. data/vendor/libgit2/src/posix.h +2 -3
  221. data/vendor/libgit2/src/pqueue.c +23 -1
  222. data/vendor/libgit2/src/pqueue.h +23 -1
  223. data/vendor/libgit2/src/push.c +653 -0
  224. data/vendor/libgit2/src/push.h +51 -0
  225. data/vendor/libgit2/src/refdb.c +185 -0
  226. data/vendor/libgit2/src/refdb.h +46 -0
  227. data/vendor/libgit2/src/refdb_fs.c +1024 -0
  228. data/vendor/libgit2/src/refdb_fs.h +15 -0
  229. data/vendor/libgit2/src/reflog.c +77 -45
  230. data/vendor/libgit2/src/reflog.h +1 -3
  231. data/vendor/libgit2/src/refs.c +366 -1326
  232. data/vendor/libgit2/src/refs.h +22 -13
  233. data/vendor/libgit2/src/refspec.c +46 -7
  234. data/vendor/libgit2/src/refspec.h +11 -1
  235. data/vendor/libgit2/src/remote.c +758 -120
  236. data/vendor/libgit2/src/remote.h +10 -5
  237. data/vendor/libgit2/src/repo_template.h +6 -6
  238. data/vendor/libgit2/src/repository.c +315 -96
  239. data/vendor/libgit2/src/repository.h +5 -3
  240. data/vendor/libgit2/src/reset.c +99 -81
  241. data/vendor/libgit2/src/revparse.c +157 -84
  242. data/vendor/libgit2/src/revwalk.c +68 -470
  243. data/vendor/libgit2/src/revwalk.h +44 -0
  244. data/vendor/libgit2/src/sha1_lookup.c +1 -1
  245. data/vendor/libgit2/src/sha1_lookup.h +1 -1
  246. data/vendor/libgit2/src/signature.c +68 -200
  247. data/vendor/libgit2/src/signature.h +1 -1
  248. data/vendor/libgit2/src/stash.c +663 -0
  249. data/vendor/libgit2/src/status.c +101 -79
  250. data/vendor/libgit2/src/strmap.h +1 -1
  251. data/vendor/libgit2/src/submodule.c +67 -51
  252. data/vendor/libgit2/src/submodule.h +1 -1
  253. data/vendor/libgit2/src/tag.c +35 -29
  254. data/vendor/libgit2/src/tag.h +1 -1
  255. data/vendor/libgit2/src/thread-utils.c +1 -1
  256. data/vendor/libgit2/src/thread-utils.h +2 -2
  257. data/vendor/libgit2/src/trace.c +39 -0
  258. data/vendor/libgit2/src/trace.h +56 -0
  259. data/vendor/libgit2/src/transport.c +81 -34
  260. data/vendor/libgit2/src/transports/cred.c +60 -0
  261. data/vendor/libgit2/src/transports/cred_helpers.c +49 -0
  262. data/vendor/libgit2/src/transports/git.c +234 -127
  263. data/vendor/libgit2/src/transports/http.c +761 -433
  264. data/vendor/libgit2/src/transports/local.c +460 -64
  265. data/vendor/libgit2/src/transports/smart.c +345 -0
  266. data/vendor/libgit2/src/transports/smart.h +179 -0
  267. data/vendor/libgit2/src/{pkt.c → transports/smart_pkt.c} +131 -12
  268. data/vendor/libgit2/src/transports/smart_protocol.c +856 -0
  269. data/vendor/libgit2/src/transports/winhttp.c +1136 -0
  270. data/vendor/libgit2/src/tree-cache.c +2 -2
  271. data/vendor/libgit2/src/tree-cache.h +1 -1
  272. data/vendor/libgit2/src/tree.c +239 -166
  273. data/vendor/libgit2/src/tree.h +11 -2
  274. data/vendor/libgit2/src/tsort.c +39 -23
  275. data/vendor/libgit2/src/unix/map.c +1 -1
  276. data/vendor/libgit2/src/unix/posix.h +12 -2
  277. data/vendor/libgit2/src/unix/realpath.c +30 -0
  278. data/vendor/libgit2/src/util.c +250 -13
  279. data/vendor/libgit2/src/util.h +71 -14
  280. data/vendor/libgit2/src/vector.c +123 -60
  281. data/vendor/libgit2/src/vector.h +24 -22
  282. data/vendor/libgit2/src/win32/dir.c +1 -1
  283. data/vendor/libgit2/src/win32/dir.h +1 -1
  284. data/vendor/libgit2/src/win32/error.c +77 -0
  285. data/vendor/libgit2/src/win32/error.h +13 -0
  286. data/vendor/libgit2/src/win32/findfile.c +143 -54
  287. data/vendor/libgit2/src/win32/findfile.h +10 -6
  288. data/vendor/libgit2/src/win32/map.c +1 -1
  289. data/vendor/libgit2/src/win32/mingw-compat.h +1 -1
  290. data/vendor/libgit2/src/win32/msvc-compat.h +10 -1
  291. data/vendor/libgit2/src/win32/posix.h +10 -1
  292. data/vendor/libgit2/src/win32/posix_w32.c +132 -63
  293. data/vendor/libgit2/src/win32/precompiled.c +1 -1
  294. data/vendor/libgit2/src/win32/pthread.c +1 -1
  295. data/vendor/libgit2/src/win32/pthread.h +1 -1
  296. data/vendor/libgit2/src/win32/utf-conv.c +5 -5
  297. data/vendor/libgit2/src/win32/utf-conv.h +3 -3
  298. data/vendor/libgit2/src/win32/version.h +20 -0
  299. metadata +308 -252
  300. data/test/fixtures/testrepo.git/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 +0 -0
  301. data/test/fixtures/testrepo.git/objects/7f/043268ea43ce18e3540acaabf9e090c91965b0 +0 -0
  302. data/test/fixtures/testrepo.git/objects/a3/e05719b428a2d0ed7a55c4ce53dcc5768c6d5e +0 -0
  303. data/test/index_test.rb~ +0 -218
  304. data/vendor/libgit2/src/pkt.h +0 -91
  305. data/vendor/libgit2/src/ppc/sha1.c +0 -70
  306. data/vendor/libgit2/src/ppc/sha1.h +0 -26
  307. data/vendor/libgit2/src/protocol.c +0 -110
  308. data/vendor/libgit2/src/protocol.h +0 -21
  309. data/vendor/libgit2/src/sha1.h +0 -33
  310. data/vendor/libgit2/src/transport.h +0 -148
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (C) 2009-2012 the libgit2 contributors
2
+ * Copyright (C) the libgit2 contributors. All rights reserved.
3
3
  *
4
4
  * This file is part of libgit2, distributed under the GNU GPL v2 with
5
5
  * a Linking Exception. For full terms see the included COPYING file.
@@ -28,6 +28,15 @@ enum {
28
28
  GIT_DIFFCAPS_USE_DEV = (1 << 4), /* use st_dev? */
29
29
  };
30
30
 
31
+ enum {
32
+ GIT_DIFF_FLAG__FREE_PATH = (1 << 7), /* `path` is allocated memory */
33
+ GIT_DIFF_FLAG__FREE_DATA = (1 << 8), /* internal file data is allocated */
34
+ GIT_DIFF_FLAG__UNMAP_DATA = (1 << 9), /* internal file data is mmap'ed */
35
+ GIT_DIFF_FLAG__NO_DATA = (1 << 10), /* file data should not be loaded */
36
+ GIT_DIFF_FLAG__TO_DELETE = (1 << 11), /* delete entry during rename det. */
37
+ GIT_DIFF_FLAG__TO_SPLIT = (1 << 12), /* split entry during rename det. */
38
+ };
39
+
31
40
  struct git_diff_list {
32
41
  git_refcount rc;
33
42
  git_repository *repo;
@@ -38,6 +47,11 @@ struct git_diff_list {
38
47
  git_iterator_type_t old_src;
39
48
  git_iterator_type_t new_src;
40
49
  uint32_t diffcaps;
50
+
51
+ int (*strcomp)(const char *, const char *);
52
+ int (*strncomp)(const char *, const char *, size_t);
53
+ int (*pfxcomp)(const char *str, const char *pfx);
54
+ int (*entrycomp)(const void *a, const void *b);
41
55
  };
42
56
 
43
57
  extern void git_diff__cleanup_modes(
@@ -45,8 +59,20 @@ extern void git_diff__cleanup_modes(
45
59
 
46
60
  extern void git_diff_list_addref(git_diff_list *diff);
47
61
 
62
+ extern int git_diff_delta__cmp(const void *a, const void *b);
63
+
48
64
  extern bool git_diff_delta__should_skip(
49
65
  const git_diff_options *opts, const git_diff_delta *delta);
50
66
 
67
+ extern int git_diff__oid_for_file(
68
+ git_repository *, const char *, uint16_t, git_off_t, git_oid *);
69
+
70
+ extern int git_diff__from_iterators(
71
+ git_diff_list **diff_ptr,
72
+ git_repository *repo,
73
+ git_iterator *old_iter,
74
+ git_iterator *new_iter,
75
+ const git_diff_options *opts);
76
+
51
77
  #endif
52
78
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (C) 2012 the libgit2 contributors
2
+ * Copyright (C) the libgit2 contributors. All rights reserved.
3
3
  *
4
4
  * This file is part of libgit2, distributed under the GNU GPL v2 with
5
5
  * a Linking Exception. For full terms see the included COPYING file.
@@ -12,6 +12,7 @@
12
12
  #include <ctype.h>
13
13
  #include "fileops.h"
14
14
  #include "filter.h"
15
+ #include "buf_text.h"
15
16
 
16
17
  static int read_next_int(const char **str, int *value)
17
18
  {
@@ -52,8 +53,8 @@ static int parse_hunk_header(git_diff_range *range, const char *header)
52
53
  return 0;
53
54
  }
54
55
 
55
- #define KNOWN_BINARY_FLAGS (GIT_DIFF_FILE_BINARY|GIT_DIFF_FILE_NOT_BINARY)
56
- #define NOT_BINARY_FLAGS (GIT_DIFF_FILE_NOT_BINARY|GIT_DIFF_FILE_NO_DATA)
56
+ #define KNOWN_BINARY_FLAGS (GIT_DIFF_FLAG_BINARY|GIT_DIFF_FLAG_NOT_BINARY)
57
+ #define NOT_BINARY_FLAGS (GIT_DIFF_FLAG_NOT_BINARY|GIT_DIFF_FLAG__NO_DATA)
57
58
 
58
59
  static int update_file_is_binary_by_attr(
59
60
  git_repository *repo, git_diff_file *file)
@@ -68,9 +69,9 @@ static int update_file_is_binary_by_attr(
68
69
  return -1;
69
70
 
70
71
  if (GIT_ATTR_FALSE(value))
71
- file->flags |= GIT_DIFF_FILE_BINARY;
72
+ file->flags |= GIT_DIFF_FLAG_BINARY;
72
73
  else if (GIT_ATTR_TRUE(value))
73
- file->flags |= GIT_DIFF_FILE_NOT_BINARY;
74
+ file->flags |= GIT_DIFF_FLAG_NOT_BINARY;
74
75
  /* otherwise leave file->flags alone */
75
76
 
76
77
  return 0;
@@ -78,49 +79,62 @@ static int update_file_is_binary_by_attr(
78
79
 
79
80
  static void update_delta_is_binary(git_diff_delta *delta)
80
81
  {
81
- if ((delta->old_file.flags & GIT_DIFF_FILE_BINARY) != 0 ||
82
- (delta->new_file.flags & GIT_DIFF_FILE_BINARY) != 0)
83
- delta->binary = 1;
82
+ if ((delta->old_file.flags & GIT_DIFF_FLAG_BINARY) != 0 ||
83
+ (delta->new_file.flags & GIT_DIFF_FLAG_BINARY) != 0)
84
+ delta->flags |= GIT_DIFF_FLAG_BINARY;
84
85
 
85
86
  else if ((delta->old_file.flags & NOT_BINARY_FLAGS) != 0 &&
86
87
  (delta->new_file.flags & NOT_BINARY_FLAGS) != 0)
87
- delta->binary = 0;
88
+ delta->flags |= GIT_DIFF_FLAG_NOT_BINARY;
88
89
 
89
- /* otherwise leave delta->binary value untouched */
90
+ /* otherwise leave delta->flags binary value untouched */
90
91
  }
91
92
 
92
- static int diff_delta_is_binary_by_attr(
93
- diff_context *ctxt, git_diff_patch *patch)
93
+ /* returns if we forced binary setting (and no further checks needed) */
94
+ static bool diff_delta_is_binary_forced(
95
+ diff_context *ctxt,
96
+ git_diff_delta *delta)
94
97
  {
95
- int error = 0, mirror_new;
96
- git_diff_delta *delta = patch->delta;
97
-
98
- delta->binary = -1;
98
+ /* return true if binary-ness has already been settled */
99
+ if ((delta->flags & KNOWN_BINARY_FLAGS) != 0)
100
+ return true;
99
101
 
100
102
  /* make sure files are conceivably mmap-able */
101
103
  if ((git_off_t)((size_t)delta->old_file.size) != delta->old_file.size ||
102
104
  (git_off_t)((size_t)delta->new_file.size) != delta->new_file.size)
103
105
  {
104
- delta->old_file.flags |= GIT_DIFF_FILE_BINARY;
105
- delta->new_file.flags |= GIT_DIFF_FILE_BINARY;
106
- delta->binary = 1;
107
- return 0;
106
+ delta->old_file.flags |= GIT_DIFF_FLAG_BINARY;
107
+ delta->new_file.flags |= GIT_DIFF_FLAG_BINARY;
108
+ delta->flags |= GIT_DIFF_FLAG_BINARY;
109
+ return true;
108
110
  }
109
111
 
110
112
  /* check if user is forcing us to text diff these files */
111
113
  if (ctxt->opts && (ctxt->opts->flags & GIT_DIFF_FORCE_TEXT) != 0) {
112
- delta->old_file.flags |= GIT_DIFF_FILE_NOT_BINARY;
113
- delta->new_file.flags |= GIT_DIFF_FILE_NOT_BINARY;
114
- delta->binary = 0;
115
- return 0;
114
+ delta->old_file.flags |= GIT_DIFF_FLAG_NOT_BINARY;
115
+ delta->new_file.flags |= GIT_DIFF_FLAG_NOT_BINARY;
116
+ delta->flags |= GIT_DIFF_FLAG_NOT_BINARY;
117
+ return true;
116
118
  }
117
119
 
120
+ return false;
121
+ }
122
+
123
+ static int diff_delta_is_binary_by_attr(
124
+ diff_context *ctxt, git_diff_patch *patch)
125
+ {
126
+ int error = 0, mirror_new;
127
+ git_diff_delta *delta = patch->delta;
128
+
129
+ if (diff_delta_is_binary_forced(ctxt, delta))
130
+ return 0;
131
+
118
132
  /* check diff attribute +, -, or 0 */
119
133
  if (update_file_is_binary_by_attr(ctxt->repo, &delta->old_file) < 0)
120
134
  return -1;
121
135
 
122
136
  mirror_new = (delta->new_file.path == delta->old_file.path ||
123
- strcmp(delta->new_file.path, delta->old_file.path) == 0);
137
+ ctxt->diff->strcomp(delta->new_file.path, delta->old_file.path) == 0);
124
138
  if (mirror_new)
125
139
  delta->new_file.flags |= (delta->old_file.flags & KNOWN_BINARY_FLAGS);
126
140
  else
@@ -132,21 +146,26 @@ static int diff_delta_is_binary_by_attr(
132
146
  }
133
147
 
134
148
  static int diff_delta_is_binary_by_content(
135
- diff_context *ctxt, git_diff_delta *delta, git_diff_file *file, git_map *map)
149
+ diff_context *ctxt,
150
+ git_diff_delta *delta,
151
+ git_diff_file *file,
152
+ const git_map *map)
136
153
  {
137
- git_buf search;
154
+ const git_buf search = { map->data, 0, min(map->len, 4000) };
138
155
 
139
- GIT_UNUSED(ctxt);
156
+ if (diff_delta_is_binary_forced(ctxt, delta))
157
+ return 0;
140
158
 
141
- if ((file->flags & KNOWN_BINARY_FLAGS) == 0) {
142
- search.ptr = map->data;
143
- search.size = min(map->len, 4000);
159
+ /* TODO: provide encoding / binary detection callbacks that can
160
+ * be UTF-8 aware, etc. For now, instead of trying to be smart,
161
+ * let's just use the simple NUL-byte detection that core git uses.
162
+ */
144
163
 
145
- if (git_buf_is_binary(&search))
146
- file->flags |= GIT_DIFF_FILE_BINARY;
147
- else
148
- file->flags |= GIT_DIFF_FILE_NOT_BINARY;
149
- }
164
+ /* previously was: if (git_buf_text_is_binary(&search)) */
165
+ if (git_buf_text_contains_nul(&search))
166
+ file->flags |= GIT_DIFF_FLAG_BINARY;
167
+ else
168
+ file->flags |= GIT_DIFF_FLAG_NOT_BINARY;
150
169
 
151
170
  update_delta_is_binary(delta);
152
171
 
@@ -170,7 +189,7 @@ static int diff_delta_is_binary_by_size(
170
189
  }
171
190
 
172
191
  if (file->size > threshold)
173
- file->flags |= GIT_DIFF_FILE_BINARY;
192
+ file->flags |= GIT_DIFF_FLAG_BINARY;
174
193
 
175
194
  update_delta_is_binary(delta);
176
195
 
@@ -184,7 +203,7 @@ static void setup_xdiff_options(
184
203
  memset(param, 0, sizeof(xpparam_t));
185
204
 
186
205
  cfg->ctxlen =
187
- (!opts || !opts->context_lines) ? 3 : opts->context_lines;
206
+ (!opts) ? 3 : opts->context_lines;
188
207
  cfg->interhunkctxlen =
189
208
  (!opts) ? 0 : opts->interhunk_lines;
190
209
 
@@ -225,7 +244,7 @@ static int get_blob_content(
225
244
  map->data = git_buf_detach(&content);
226
245
  map->len = strlen(map->data);
227
246
 
228
- file->flags |= GIT_DIFF_FILE_FREE_DATA;
247
+ file->flags |= GIT_DIFF_FLAG__FREE_DATA;
229
248
  return 0;
230
249
  }
231
250
 
@@ -248,7 +267,7 @@ static int get_blob_content(
248
267
  /* if blob is too large to diff, mark as binary */
249
268
  if ((error = diff_delta_is_binary_by_size(ctxt, delta, file)) < 0)
250
269
  return error;
251
- if (delta->binary == 1)
270
+ if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
252
271
  return 0;
253
272
 
254
273
  if (odb_obj != NULL) {
@@ -262,7 +281,7 @@ static int get_blob_content(
262
281
  return error;
263
282
 
264
283
  map->data = (void *)git_blob_rawcontent(*blob);
265
- map->len = git_blob_rawsize(*blob);
284
+ map->len = (size_t)git_blob_rawsize(*blob);
266
285
 
267
286
  return diff_delta_is_binary_by_content(ctxt, delta, file, map);
268
287
  }
@@ -281,17 +300,22 @@ static int get_workdir_sm_content(
281
300
 
282
301
  if ((error = git_submodule_lookup(&sm, ctxt->repo, file->path)) < 0 ||
283
302
  (error = git_submodule_status(&sm_status, sm)) < 0)
303
+ {
304
+ /* GIT_EEXISTS means a "submodule" that has not been git added */
305
+ if (error == GIT_EEXISTS)
306
+ error = 0;
284
307
  return error;
308
+ }
285
309
 
286
310
  /* update OID if we didn't have it previously */
287
- if ((file->flags & GIT_DIFF_FILE_VALID_OID) == 0) {
311
+ if ((file->flags & GIT_DIFF_FLAG_VALID_OID) == 0) {
288
312
  const git_oid* sm_head;
289
313
 
290
- if ((sm_head = git_submodule_wd_oid(sm)) != NULL ||
291
- (sm_head = git_submodule_head_oid(sm)) != NULL)
314
+ if ((sm_head = git_submodule_wd_id(sm)) != NULL ||
315
+ (sm_head = git_submodule_head_id(sm)) != NULL)
292
316
  {
293
317
  git_oid_cpy(&file->oid, sm_head);
294
- file->flags |= GIT_DIFF_FILE_VALID_OID;
318
+ file->flags |= GIT_DIFF_FLAG_VALID_OID;
295
319
  }
296
320
  }
297
321
 
@@ -307,11 +331,38 @@ static int get_workdir_sm_content(
307
331
  map->data = git_buf_detach(&content);
308
332
  map->len = strlen(map->data);
309
333
 
310
- file->flags |= GIT_DIFF_FILE_FREE_DATA;
334
+ file->flags |= GIT_DIFF_FLAG__FREE_DATA;
311
335
 
312
336
  return 0;
313
337
  }
314
338
 
339
+ static int get_filtered(
340
+ git_map *map, git_file fd, git_diff_file *file, git_vector *filters)
341
+ {
342
+ int error;
343
+ git_buf raw = GIT_BUF_INIT, filtered = GIT_BUF_INIT;
344
+
345
+ if ((error = git_futils_readbuffer_fd(&raw, fd, (size_t)file->size)) < 0)
346
+ return error;
347
+
348
+ if (!filters->length)
349
+ git_buf_swap(&filtered, &raw);
350
+ else
351
+ error = git_filters_apply(&filtered, &raw, filters);
352
+
353
+ if (!error) {
354
+ map->len = git_buf_len(&filtered);
355
+ map->data = git_buf_detach(&filtered);
356
+
357
+ file->flags |= GIT_DIFF_FLAG__FREE_DATA;
358
+ }
359
+
360
+ git_buf_free(&raw);
361
+ git_buf_free(&filtered);
362
+
363
+ return error;
364
+ }
365
+
315
366
  static int get_workdir_content(
316
367
  diff_context *ctxt,
317
368
  git_diff_delta *delta,
@@ -334,8 +385,8 @@ static int get_workdir_content(
334
385
  if (S_ISLNK(file->mode)) {
335
386
  ssize_t alloc_len, read_len;
336
387
 
337
- file->flags |= GIT_DIFF_FILE_FREE_DATA;
338
- file->flags |= GIT_DIFF_FILE_BINARY;
388
+ file->flags |= GIT_DIFF_FLAG__FREE_DATA;
389
+ file->flags |= GIT_DIFF_FLAG_BINARY;
339
390
 
340
391
  /* link path on disk could be UTF-16, so prepare a buffer that is
341
392
  * big enough to handle some UTF-8 data expansion
@@ -345,7 +396,7 @@ static int get_workdir_content(
345
396
  map->data = git__malloc(alloc_len);
346
397
  GITERR_CHECK_ALLOC(map->data);
347
398
 
348
- read_len = p_readlink(path.ptr, map->data, (int)alloc_len);
399
+ read_len = p_readlink(path.ptr, map->data, alloc_len);
349
400
  if (read_len < 0) {
350
401
  giterr_set(GITERR_OS, "Failed to read symlink '%s'", file->path);
351
402
  error = -1;
@@ -363,11 +414,11 @@ static int get_workdir_content(
363
414
  goto cleanup;
364
415
  }
365
416
 
366
- if (!file->size)
367
- file->size = git_futils_filesize(fd);
417
+ if (!file->size && !(file->size = git_futils_filesize(fd)))
418
+ goto close_and_cleanup;
368
419
 
369
420
  if ((error = diff_delta_is_binary_by_size(ctxt, delta, file)) < 0 ||
370
- delta->binary == 1)
421
+ (delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
371
422
  goto close_and_cleanup;
372
423
 
373
424
  error = git_filters_load(
@@ -377,22 +428,11 @@ static int get_workdir_content(
377
428
 
378
429
  if (error == 0) { /* note: git_filters_load returns filter count */
379
430
  error = git_futils_mmap_ro(map, fd, 0, (size_t)file->size);
380
- file->flags |= GIT_DIFF_FILE_UNMAP_DATA;
381
- } else {
382
- git_buf raw = GIT_BUF_INIT, filtered = GIT_BUF_INIT;
383
-
384
- if (!(error = git_futils_readbuffer_fd(&raw, fd, (size_t)file->size)) &&
385
- !(error = git_filters_apply(&filtered, &raw, &filters)))
386
- {
387
- map->len = git_buf_len(&filtered);
388
- map->data = git_buf_detach(&filtered);
389
-
390
- file->flags |= GIT_DIFF_FILE_FREE_DATA;
391
- }
392
-
393
- git_buf_free(&raw);
394
- git_buf_free(&filtered);
431
+ if (!error)
432
+ file->flags |= GIT_DIFF_FLAG__UNMAP_DATA;
395
433
  }
434
+ if (error != 0)
435
+ error = get_filtered(map, fd, file, &filters);
396
436
 
397
437
  close_and_cleanup:
398
438
  git_filters_free(&filters);
@@ -400,11 +440,11 @@ close_and_cleanup:
400
440
  }
401
441
 
402
442
  /* once data is loaded, update OID if we didn't have it previously */
403
- if (!error && (file->flags & GIT_DIFF_FILE_VALID_OID) == 0) {
443
+ if (!error && (file->flags & GIT_DIFF_FLAG_VALID_OID) == 0) {
404
444
  error = git_odb_hash(
405
445
  &file->oid, map->data, map->len, GIT_OBJ_BLOB);
406
446
  if (!error)
407
- file->flags |= GIT_DIFF_FILE_VALID_OID;
447
+ file->flags |= GIT_DIFF_FLAG_VALID_OID;
408
448
  }
409
449
 
410
450
  if (!error)
@@ -420,43 +460,51 @@ static void release_content(git_diff_file *file, git_map *map, git_blob *blob)
420
460
  if (blob != NULL)
421
461
  git_blob_free(blob);
422
462
 
423
- if (file->flags & GIT_DIFF_FILE_FREE_DATA) {
463
+ if (file->flags & GIT_DIFF_FLAG__FREE_DATA) {
424
464
  git__free(map->data);
425
465
  map->data = "";
426
466
  map->len = 0;
427
- file->flags &= ~GIT_DIFF_FILE_FREE_DATA;
467
+ file->flags &= ~GIT_DIFF_FLAG__FREE_DATA;
428
468
  }
429
- else if (file->flags & GIT_DIFF_FILE_UNMAP_DATA) {
469
+ else if (file->flags & GIT_DIFF_FLAG__UNMAP_DATA) {
430
470
  git_futils_mmap_free(map);
431
471
  map->data = "";
432
472
  map->len = 0;
433
- file->flags &= ~GIT_DIFF_FILE_UNMAP_DATA;
473
+ file->flags &= ~GIT_DIFF_FLAG__UNMAP_DATA;
434
474
  }
435
475
  }
436
476
 
437
477
 
438
- static void diff_context_init(
478
+ static int diff_context_init(
439
479
  diff_context *ctxt,
440
480
  git_diff_list *diff,
441
481
  git_repository *repo,
442
482
  const git_diff_options *opts,
443
- void *data,
444
- git_diff_file_fn file_cb,
445
- git_diff_hunk_fn hunk_cb,
446
- git_diff_data_fn data_cb)
483
+ git_diff_file_cb file_cb,
484
+ git_diff_hunk_cb hunk_cb,
485
+ git_diff_data_cb data_cb,
486
+ void *payload)
447
487
  {
448
488
  memset(ctxt, 0, sizeof(diff_context));
449
489
 
490
+ if (!repo && diff)
491
+ repo = diff->repo;
492
+
493
+ if (!opts && diff)
494
+ opts = &diff->opts;
495
+
450
496
  ctxt->repo = repo;
451
497
  ctxt->diff = diff;
452
498
  ctxt->opts = opts;
453
499
  ctxt->file_cb = file_cb;
454
500
  ctxt->hunk_cb = hunk_cb;
455
501
  ctxt->data_cb = data_cb;
456
- ctxt->cb_data = data;
457
- ctxt->cb_error = 0;
502
+ ctxt->payload = payload;
503
+ ctxt->error = 0;
458
504
 
459
505
  setup_xdiff_options(ctxt->opts, &ctxt->xdiff_config, &ctxt->xdiff_params);
506
+
507
+ return 0;
460
508
  }
461
509
 
462
510
  static int diff_delta_file_callback(
@@ -469,10 +517,10 @@ static int diff_delta_file_callback(
469
517
 
470
518
  progress = ctxt->diff ? ((float)idx / ctxt->diff->deltas.length) : 1.0f;
471
519
 
472
- if (ctxt->file_cb(ctxt->cb_data, delta, progress) != 0)
473
- ctxt->cb_error = GIT_EUSER;
520
+ if (ctxt->file_cb(delta, progress, ctxt->payload) != 0)
521
+ ctxt->error = GIT_EUSER;
474
522
 
475
- return ctxt->cb_error;
523
+ return ctxt->error;
476
524
  }
477
525
 
478
526
  static void diff_patch_init(
@@ -487,7 +535,7 @@ static void diff_patch_init(
487
535
  patch->old_src = patch->diff->old_src;
488
536
  patch->new_src = patch->diff->new_src;
489
537
  } else {
490
- patch->old_src = patch->new_src = GIT_ITERATOR_TREE;
538
+ patch->old_src = patch->new_src = GIT_ITERATOR_TYPE_TREE;
491
539
  }
492
540
  }
493
541
 
@@ -530,7 +578,7 @@ static int diff_patch_load(
530
578
  patch->new_data.len = 0;
531
579
  patch->new_blob = NULL;
532
580
 
533
- if (delta->binary == 1)
581
+ if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
534
582
  goto cleanup;
535
583
 
536
584
  if (!ctxt->hunk_cb &&
@@ -540,25 +588,25 @@ static int diff_patch_load(
540
588
 
541
589
  switch (delta->status) {
542
590
  case GIT_DELTA_ADDED:
543
- delta->old_file.flags |= GIT_DIFF_FILE_NO_DATA;
591
+ delta->old_file.flags |= GIT_DIFF_FLAG__NO_DATA;
544
592
  break;
545
593
  case GIT_DELTA_DELETED:
546
- delta->new_file.flags |= GIT_DIFF_FILE_NO_DATA;
594
+ delta->new_file.flags |= GIT_DIFF_FLAG__NO_DATA;
547
595
  break;
548
596
  case GIT_DELTA_MODIFIED:
549
597
  break;
550
598
  case GIT_DELTA_UNTRACKED:
551
- delta->old_file.flags |= GIT_DIFF_FILE_NO_DATA;
599
+ delta->old_file.flags |= GIT_DIFF_FLAG__NO_DATA;
552
600
  if ((ctxt->opts->flags & GIT_DIFF_INCLUDE_UNTRACKED_CONTENT) == 0)
553
- delta->new_file.flags |= GIT_DIFF_FILE_NO_DATA;
601
+ delta->new_file.flags |= GIT_DIFF_FLAG__NO_DATA;
554
602
  break;
555
603
  default:
556
- delta->new_file.flags |= GIT_DIFF_FILE_NO_DATA;
557
- delta->old_file.flags |= GIT_DIFF_FILE_NO_DATA;
604
+ delta->new_file.flags |= GIT_DIFF_FLAG__NO_DATA;
605
+ delta->old_file.flags |= GIT_DIFF_FLAG__NO_DATA;
558
606
  break;
559
607
  }
560
608
 
561
- #define CHECK_UNMODIFIED (GIT_DIFF_FILE_NO_DATA | GIT_DIFF_FILE_VALID_OID)
609
+ #define CHECK_UNMODIFIED (GIT_DIFF_FLAG__NO_DATA | GIT_DIFF_FLAG_VALID_OID)
562
610
 
563
611
  check_if_unmodified =
564
612
  (delta->old_file.flags & CHECK_UNMODIFIED) == 0 &&
@@ -569,41 +617,41 @@ static int diff_patch_load(
569
617
  * memory footprint during diff.
570
618
  */
571
619
 
572
- if ((delta->old_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 &&
573
- patch->old_src == GIT_ITERATOR_WORKDIR) {
620
+ if ((delta->old_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0 &&
621
+ patch->old_src == GIT_ITERATOR_TYPE_WORKDIR) {
574
622
  if ((error = get_workdir_content(
575
623
  ctxt, delta, &delta->old_file, &patch->old_data)) < 0)
576
624
  goto cleanup;
577
- if (delta->binary == 1)
625
+ if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
578
626
  goto cleanup;
579
627
  }
580
628
 
581
- if ((delta->new_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 &&
582
- patch->new_src == GIT_ITERATOR_WORKDIR) {
629
+ if ((delta->new_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0 &&
630
+ patch->new_src == GIT_ITERATOR_TYPE_WORKDIR) {
583
631
  if ((error = get_workdir_content(
584
632
  ctxt, delta, &delta->new_file, &patch->new_data)) < 0)
585
633
  goto cleanup;
586
- if (delta->binary == 1)
634
+ if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
587
635
  goto cleanup;
588
636
  }
589
637
 
590
- if ((delta->old_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 &&
591
- patch->old_src != GIT_ITERATOR_WORKDIR) {
638
+ if ((delta->old_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0 &&
639
+ patch->old_src != GIT_ITERATOR_TYPE_WORKDIR) {
592
640
  if ((error = get_blob_content(
593
641
  ctxt, delta, &delta->old_file,
594
642
  &patch->old_data, &patch->old_blob)) < 0)
595
643
  goto cleanup;
596
- if (delta->binary == 1)
644
+ if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
597
645
  goto cleanup;
598
646
  }
599
647
 
600
- if ((delta->new_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 &&
601
- patch->new_src != GIT_ITERATOR_WORKDIR) {
648
+ if ((delta->new_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0 &&
649
+ patch->new_src != GIT_ITERATOR_TYPE_WORKDIR) {
602
650
  if ((error = get_blob_content(
603
651
  ctxt, delta, &delta->new_file,
604
652
  &patch->new_data, &patch->new_blob)) < 0)
605
653
  goto cleanup;
606
- if (delta->binary == 1)
654
+ if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
607
655
  goto cleanup;
608
656
  }
609
657
 
@@ -621,16 +669,20 @@ static int diff_patch_load(
621
669
  }
622
670
 
623
671
  cleanup:
624
- if (delta->binary == -1)
672
+ if ((delta->flags & KNOWN_BINARY_FLAGS) == 0)
625
673
  update_delta_is_binary(delta);
626
674
 
627
675
  if (!error) {
628
676
  patch->flags |= GIT_DIFF_PATCH_LOADED;
629
677
 
630
- if (delta->binary != 1 &&
678
+ /* patch is diffable only for non-binary, modified files where at
679
+ * least one side has data and there is actual change in the data
680
+ */
681
+ if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0 &&
631
682
  delta->status != GIT_DELTA_UNMODIFIED &&
632
683
  (patch->old_data.len || patch->new_data.len) &&
633
- !git_oid_equal(&delta->old_file.oid, &delta->new_file.oid))
684
+ (patch->old_data.len != patch->new_data.len ||
685
+ !git_oid_equal(&delta->old_file.oid, &delta->new_file.oid)))
634
686
  patch->flags |= GIT_DIFF_PATCH_DIFFABLE;
635
687
  }
636
688
 
@@ -643,14 +695,14 @@ static int diff_patch_cb(void *priv, mmbuffer_t *bufs, int len)
643
695
  diff_context *ctxt = patch->ctxt;
644
696
 
645
697
  if (len == 1) {
646
- ctxt->cb_error = parse_hunk_header(&ctxt->cb_range, bufs[0].ptr);
647
- if (ctxt->cb_error < 0)
648
- return ctxt->cb_error;
698
+ ctxt->error = parse_hunk_header(&ctxt->range, bufs[0].ptr);
699
+ if (ctxt->error < 0)
700
+ return ctxt->error;
649
701
 
650
702
  if (ctxt->hunk_cb != NULL &&
651
- ctxt->hunk_cb(ctxt->cb_data, patch->delta, &ctxt->cb_range,
652
- bufs[0].ptr, bufs[0].size))
653
- ctxt->cb_error = GIT_EUSER;
703
+ ctxt->hunk_cb(patch->delta, &ctxt->range,
704
+ bufs[0].ptr, bufs[0].size, ctxt->payload))
705
+ ctxt->error = GIT_EUSER;
654
706
  }
655
707
 
656
708
  if (len == 2 || len == 3) {
@@ -661,12 +713,12 @@ static int diff_patch_cb(void *priv, mmbuffer_t *bufs, int len)
661
713
  GIT_DIFF_LINE_CONTEXT;
662
714
 
663
715
  if (ctxt->data_cb != NULL &&
664
- ctxt->data_cb(ctxt->cb_data, patch->delta, &ctxt->cb_range,
665
- origin, bufs[1].ptr, bufs[1].size))
666
- ctxt->cb_error = GIT_EUSER;
716
+ ctxt->data_cb(patch->delta, &ctxt->range,
717
+ origin, bufs[1].ptr, bufs[1].size, ctxt->payload))
718
+ ctxt->error = GIT_EUSER;
667
719
  }
668
720
 
669
- if (len == 3 && !ctxt->cb_error) {
721
+ if (len == 3 && !ctxt->error) {
670
722
  /* If we have a '+' and a third buf, then we have added a line
671
723
  * without a newline and the old code had one, so DEL_EOFNL.
672
724
  * If we have a '-' and a third buf, then we have removed a line
@@ -678,12 +730,12 @@ static int diff_patch_cb(void *priv, mmbuffer_t *bufs, int len)
678
730
  GIT_DIFF_LINE_CONTEXT;
679
731
 
680
732
  if (ctxt->data_cb != NULL &&
681
- ctxt->data_cb(ctxt->cb_data, patch->delta, &ctxt->cb_range,
682
- origin, bufs[2].ptr, bufs[2].size))
683
- ctxt->cb_error = GIT_EUSER;
733
+ ctxt->data_cb(patch->delta, &ctxt->range,
734
+ origin, bufs[2].ptr, bufs[2].size, ctxt->payload))
735
+ ctxt->error = GIT_EUSER;
684
736
  }
685
737
 
686
- return ctxt->cb_error;
738
+ return ctxt->error;
687
739
  }
688
740
 
689
741
  static int diff_patch_generate(
@@ -720,7 +772,7 @@ static int diff_patch_generate(
720
772
  xdl_diff(&old_xdiff_data, &new_xdiff_data,
721
773
  &ctxt->xdiff_params, &ctxt->xdiff_config, &xdiff_callback);
722
774
 
723
- error = ctxt->cb_error;
775
+ error = ctxt->error;
724
776
 
725
777
  if (!error)
726
778
  patch->flags |= GIT_DIFF_PATCH_DIFFED;
@@ -775,13 +827,13 @@ static void diff_patch_free(git_diff_patch *patch)
775
827
  #define MIN_LINE_STEP 8
776
828
 
777
829
  static int diff_patch_hunk_cb(
778
- void *cb_data,
779
830
  const git_diff_delta *delta,
780
831
  const git_diff_range *range,
781
832
  const char *header,
782
- size_t header_len)
833
+ size_t header_len,
834
+ void *payload)
783
835
  {
784
- git_diff_patch *patch = cb_data;
836
+ git_diff_patch *patch = payload;
785
837
  diff_patch_hunk *hunk;
786
838
 
787
839
  GIT_UNUSED(delta);
@@ -818,20 +870,23 @@ static int diff_patch_hunk_cb(
818
870
  hunk->line_start = patch->lines_size;
819
871
  hunk->line_count = 0;
820
872
 
873
+ patch->oldno = range->old_start;
874
+ patch->newno = range->new_start;
875
+
821
876
  return 0;
822
877
  }
823
878
 
824
879
  static int diff_patch_line_cb(
825
- void *cb_data,
826
880
  const git_diff_delta *delta,
827
881
  const git_diff_range *range,
828
882
  char line_origin,
829
883
  const char *content,
830
- size_t content_len)
884
+ size_t content_len,
885
+ void *payload)
831
886
  {
832
- git_diff_patch *patch = cb_data;
887
+ git_diff_patch *patch = payload;
833
888
  diff_patch_hunk *hunk;
834
- diff_patch_line *last, *line;
889
+ diff_patch_line *line;
835
890
 
836
891
  GIT_UNUSED(delta);
837
892
  GIT_UNUSED(range);
@@ -861,8 +916,6 @@ static int diff_patch_line_cb(
861
916
  patch->lines_asize = new_size;
862
917
  }
863
918
 
864
- last = (patch->lines_size > 0) ?
865
- &patch->lines[patch->lines_size - 1] : NULL;
866
919
  line = &patch->lines[patch->lines_size++];
867
920
 
868
921
  line->ptr = content;
@@ -876,24 +929,23 @@ static int diff_patch_line_cb(
876
929
  ++line->lines;
877
930
  }
878
931
 
879
- if (!last) {
880
- line->oldno = hunk->range.old_start;
881
- line->newno = hunk->range.new_start;
882
- } else {
883
- switch (last->origin) {
884
- case GIT_DIFF_LINE_ADDITION:
885
- line->oldno = last->oldno;
886
- line->newno = last->newno + last->lines;
887
- break;
888
- case GIT_DIFF_LINE_DELETION:
889
- line->oldno = last->oldno + last->lines;
890
- line->newno = last->newno;
891
- break;
892
- default:
893
- line->oldno = last->oldno + last->lines;
894
- line->newno = last->newno + last->lines;
895
- break;
896
- }
932
+ switch (line_origin) {
933
+ case GIT_DIFF_LINE_ADDITION:
934
+ line->oldno = -1;
935
+ line->newno = patch->newno;
936
+ patch->newno += line->lines;
937
+ break;
938
+ case GIT_DIFF_LINE_DELETION:
939
+ line->oldno = patch->oldno;
940
+ line->newno = -1;
941
+ patch->oldno += line->lines;
942
+ break;
943
+ default:
944
+ line->oldno = patch->oldno;
945
+ line->newno = patch->newno;
946
+ patch->oldno += line->lines;
947
+ patch->newno += line->lines;
948
+ break;
897
949
  }
898
950
 
899
951
  hunk->line_count++;
@@ -901,22 +953,34 @@ static int diff_patch_line_cb(
901
953
  return 0;
902
954
  }
903
955
 
956
+ static int diff_required(git_diff_list *diff, const char *action)
957
+ {
958
+ if (!diff) {
959
+ giterr_set(GITERR_INVALID, "Must provide valid diff to %s", action);
960
+ return -1;
961
+ }
962
+
963
+ return 0;
964
+ }
904
965
 
905
966
  int git_diff_foreach(
906
967
  git_diff_list *diff,
907
- void *cb_data,
908
- git_diff_file_fn file_cb,
909
- git_diff_hunk_fn hunk_cb,
910
- git_diff_data_fn data_cb)
968
+ git_diff_file_cb file_cb,
969
+ git_diff_hunk_cb hunk_cb,
970
+ git_diff_data_cb data_cb,
971
+ void *payload)
911
972
  {
912
973
  int error = 0;
913
974
  diff_context ctxt;
914
975
  size_t idx;
915
976
  git_diff_patch patch;
916
977
 
917
- diff_context_init(
918
- &ctxt, diff, diff->repo, &diff->opts,
919
- cb_data, file_cb, hunk_cb, data_cb);
978
+ if (diff_required(diff, "git_diff_foreach") < 0)
979
+ return -1;
980
+
981
+ if (diff_context_init(
982
+ &ctxt, diff, NULL, NULL, file_cb, hunk_cb, data_cb, payload) < 0)
983
+ return -1;
920
984
 
921
985
  diff_patch_init(&ctxt, &patch);
922
986
 
@@ -951,8 +1015,8 @@ int git_diff_foreach(
951
1015
 
952
1016
  typedef struct {
953
1017
  git_diff_list *diff;
954
- git_diff_data_fn print_cb;
955
- void *cb_data;
1018
+ git_diff_data_cb print_cb;
1019
+ void *payload;
956
1020
  git_buf *buf;
957
1021
  } diff_print_info;
958
1022
 
@@ -986,7 +1050,7 @@ char git_diff_status_char(git_delta_t status)
986
1050
  }
987
1051
 
988
1052
  static int print_compact(
989
- void *data, const git_diff_delta *delta, float progress)
1053
+ const git_diff_delta *delta, float progress, void *data)
990
1054
  {
991
1055
  diff_print_info *pi = data;
992
1056
  char old_suffix, new_suffix, code = git_diff_status_char(delta->status);
@@ -1002,7 +1066,7 @@ static int print_compact(
1002
1066
  git_buf_clear(pi->buf);
1003
1067
 
1004
1068
  if (delta->old_file.path != delta->new_file.path &&
1005
- strcmp(delta->old_file.path,delta->new_file.path) != 0)
1069
+ pi->diff->strcomp(delta->old_file.path,delta->new_file.path) != 0)
1006
1070
  git_buf_printf(pi->buf, "%c\t%s%c -> %s%c\n", code,
1007
1071
  delta->old_file.path, old_suffix, delta->new_file.path, new_suffix);
1008
1072
  else if (delta->old_file.mode != delta->new_file.mode &&
@@ -1017,8 +1081,8 @@ static int print_compact(
1017
1081
  if (git_buf_oom(pi->buf))
1018
1082
  return -1;
1019
1083
 
1020
- if (pi->print_cb(pi->cb_data, delta, NULL, GIT_DIFF_LINE_FILE_HDR,
1021
- git_buf_cstr(pi->buf), git_buf_len(pi->buf)))
1084
+ if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_FILE_HDR,
1085
+ git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload))
1022
1086
  {
1023
1087
  giterr_clear();
1024
1088
  return GIT_EUSER;
@@ -1029,8 +1093,8 @@ static int print_compact(
1029
1093
 
1030
1094
  int git_diff_print_compact(
1031
1095
  git_diff_list *diff,
1032
- void *cb_data,
1033
- git_diff_data_fn print_cb)
1096
+ git_diff_data_cb print_cb,
1097
+ void *payload)
1034
1098
  {
1035
1099
  int error;
1036
1100
  git_buf buf = GIT_BUF_INIT;
@@ -1038,10 +1102,10 @@ int git_diff_print_compact(
1038
1102
 
1039
1103
  pi.diff = diff;
1040
1104
  pi.print_cb = print_cb;
1041
- pi.cb_data = cb_data;
1105
+ pi.payload = payload;
1042
1106
  pi.buf = &buf;
1043
1107
 
1044
- error = git_diff_foreach(diff, &pi, print_compact, NULL, NULL);
1108
+ error = git_diff_foreach(diff, print_compact, NULL, NULL, &pi);
1045
1109
 
1046
1110
  git_buf_free(&buf);
1047
1111
 
@@ -1079,7 +1143,7 @@ static int print_oid_range(diff_print_info *pi, const git_diff_delta *delta)
1079
1143
  }
1080
1144
 
1081
1145
  static int print_patch_file(
1082
- void *data, const git_diff_delta *delta, float progress)
1146
+ const git_diff_delta *delta, float progress, void *data)
1083
1147
  {
1084
1148
  diff_print_info *pi = data;
1085
1149
  const char *oldpfx = pi->diff->opts.old_prefix;
@@ -1089,7 +1153,11 @@ static int print_patch_file(
1089
1153
 
1090
1154
  GIT_UNUSED(progress);
1091
1155
 
1092
- if (S_ISDIR(delta->new_file.mode))
1156
+ if (S_ISDIR(delta->new_file.mode) ||
1157
+ delta->status == GIT_DELTA_UNMODIFIED ||
1158
+ delta->status == GIT_DELTA_IGNORED ||
1159
+ (delta->status == GIT_DELTA_UNTRACKED &&
1160
+ (pi->diff->opts.flags & GIT_DIFF_INCLUDE_UNTRACKED_CONTENT) == 0))
1093
1161
  return 0;
1094
1162
 
1095
1163
  if (!oldpfx)
@@ -1113,7 +1181,7 @@ static int print_patch_file(
1113
1181
  newpath = "/dev/null";
1114
1182
  }
1115
1183
 
1116
- if (delta->binary != 1) {
1184
+ if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0) {
1117
1185
  git_buf_printf(pi->buf, "--- %s%s\n", oldpfx, oldpath);
1118
1186
  git_buf_printf(pi->buf, "+++ %s%s\n", newpfx, newpath);
1119
1187
  }
@@ -1121,13 +1189,14 @@ static int print_patch_file(
1121
1189
  if (git_buf_oom(pi->buf))
1122
1190
  return -1;
1123
1191
 
1124
- if (pi->print_cb(pi->cb_data, delta, NULL, GIT_DIFF_LINE_FILE_HDR, git_buf_cstr(pi->buf), git_buf_len(pi->buf)))
1192
+ if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_FILE_HDR,
1193
+ git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload))
1125
1194
  {
1126
1195
  giterr_clear();
1127
1196
  return GIT_EUSER;
1128
1197
  }
1129
1198
 
1130
- if (delta->binary != 1)
1199
+ if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0)
1131
1200
  return 0;
1132
1201
 
1133
1202
  git_buf_clear(pi->buf);
@@ -1137,8 +1206,8 @@ static int print_patch_file(
1137
1206
  if (git_buf_oom(pi->buf))
1138
1207
  return -1;
1139
1208
 
1140
- if (pi->print_cb(pi->cb_data, delta, NULL, GIT_DIFF_LINE_BINARY,
1141
- git_buf_cstr(pi->buf), git_buf_len(pi->buf)))
1209
+ if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_BINARY,
1210
+ git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload))
1142
1211
  {
1143
1212
  giterr_clear();
1144
1213
  return GIT_EUSER;
@@ -1148,11 +1217,11 @@ static int print_patch_file(
1148
1217
  }
1149
1218
 
1150
1219
  static int print_patch_hunk(
1151
- void *data,
1152
1220
  const git_diff_delta *d,
1153
1221
  const git_diff_range *r,
1154
1222
  const char *header,
1155
- size_t header_len)
1223
+ size_t header_len,
1224
+ void *data)
1156
1225
  {
1157
1226
  diff_print_info *pi = data;
1158
1227
 
@@ -1163,8 +1232,8 @@ static int print_patch_hunk(
1163
1232
  if (git_buf_printf(pi->buf, "%.*s", (int)header_len, header) < 0)
1164
1233
  return -1;
1165
1234
 
1166
- if (pi->print_cb(pi->cb_data, d, r, GIT_DIFF_LINE_HUNK_HDR,
1167
- git_buf_cstr(pi->buf), git_buf_len(pi->buf)))
1235
+ if (pi->print_cb(d, r, GIT_DIFF_LINE_HUNK_HDR,
1236
+ git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload))
1168
1237
  {
1169
1238
  giterr_clear();
1170
1239
  return GIT_EUSER;
@@ -1174,12 +1243,12 @@ static int print_patch_hunk(
1174
1243
  }
1175
1244
 
1176
1245
  static int print_patch_line(
1177
- void *data,
1178
1246
  const git_diff_delta *delta,
1179
1247
  const git_diff_range *range,
1180
1248
  char line_origin, /* GIT_DIFF_LINE value from above */
1181
1249
  const char *content,
1182
- size_t content_len)
1250
+ size_t content_len,
1251
+ void *data)
1183
1252
  {
1184
1253
  diff_print_info *pi = data;
1185
1254
 
@@ -1198,8 +1267,8 @@ static int print_patch_line(
1198
1267
  if (git_buf_oom(pi->buf))
1199
1268
  return -1;
1200
1269
 
1201
- if (pi->print_cb(pi->cb_data, delta, range, line_origin,
1202
- git_buf_cstr(pi->buf), git_buf_len(pi->buf)))
1270
+ if (pi->print_cb(delta, range, line_origin,
1271
+ git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload))
1203
1272
  {
1204
1273
  giterr_clear();
1205
1274
  return GIT_EUSER;
@@ -1210,8 +1279,8 @@ static int print_patch_line(
1210
1279
 
1211
1280
  int git_diff_print_patch(
1212
1281
  git_diff_list *diff,
1213
- void *cb_data,
1214
- git_diff_data_fn print_cb)
1282
+ git_diff_data_cb print_cb,
1283
+ void *payload)
1215
1284
  {
1216
1285
  int error;
1217
1286
  git_buf buf = GIT_BUF_INIT;
@@ -1219,108 +1288,192 @@ int git_diff_print_patch(
1219
1288
 
1220
1289
  pi.diff = diff;
1221
1290
  pi.print_cb = print_cb;
1222
- pi.cb_data = cb_data;
1291
+ pi.payload = payload;
1223
1292
  pi.buf = &buf;
1224
1293
 
1225
1294
  error = git_diff_foreach(
1226
- diff, &pi, print_patch_file, print_patch_hunk, print_patch_line);
1295
+ diff, print_patch_file, print_patch_hunk, print_patch_line, &pi);
1227
1296
 
1228
1297
  git_buf_free(&buf);
1229
1298
 
1230
1299
  return error;
1231
1300
  }
1232
1301
 
1233
-
1234
1302
  static void set_data_from_blob(
1235
- git_blob *blob, git_map *map, git_diff_file *file)
1303
+ const git_blob *blob, git_map *map, git_diff_file *file)
1236
1304
  {
1237
1305
  if (blob) {
1238
- map->data = (char *)git_blob_rawcontent(blob);
1239
- file->size = map->len = git_blob_rawsize(blob);
1306
+ file->size = git_blob_rawsize(blob);
1240
1307
  git_oid_cpy(&file->oid, git_object_id((const git_object *)blob));
1241
1308
  file->mode = 0644;
1309
+
1310
+ map->len = (size_t)file->size;
1311
+ map->data = (char *)git_blob_rawcontent(blob);
1242
1312
  } else {
1243
- map->data = "";
1244
- file->size = map->len = 0;
1245
- file->flags |= GIT_DIFF_FILE_NO_DATA;
1313
+ file->size = 0;
1314
+ file->flags |= GIT_DIFF_FLAG__NO_DATA;
1315
+
1316
+ map->len = 0;
1317
+ map->data = "";
1246
1318
  }
1247
1319
  }
1248
1320
 
1249
- int git_diff_blobs(
1250
- git_blob *old_blob,
1251
- git_blob *new_blob,
1252
- const git_diff_options *options,
1253
- void *cb_data,
1254
- git_diff_file_fn file_cb,
1255
- git_diff_hunk_fn hunk_cb,
1256
- git_diff_data_fn data_cb)
1321
+ static void set_data_from_buffer(
1322
+ const char *buffer, size_t buffer_len, git_map *map, git_diff_file *file)
1257
1323
  {
1258
- int error;
1259
- git_repository *repo;
1260
- diff_context ctxt;
1324
+ file->size = (git_off_t)buffer_len;
1325
+ file->mode = 0644;
1326
+ map->len = buffer_len;
1327
+
1328
+ if (!buffer) {
1329
+ file->flags |= GIT_DIFF_FLAG__NO_DATA;
1330
+ map->data = NULL;
1331
+ } else {
1332
+ map->data = (char *)buffer;
1333
+ git_odb_hash(&file->oid, buffer, buffer_len, GIT_OBJ_BLOB);
1334
+ }
1335
+ }
1336
+
1337
+ typedef struct {
1338
+ diff_context ctxt;
1261
1339
  git_diff_delta delta;
1262
1340
  git_diff_patch patch;
1341
+ } diff_single_data;
1263
1342
 
1264
- if (options && (options->flags & GIT_DIFF_REVERSE)) {
1265
- git_blob *swap = old_blob;
1266
- old_blob = new_blob;
1267
- new_blob = swap;
1268
- }
1343
+ static int diff_single_init(
1344
+ diff_single_data *data,
1345
+ git_repository *repo,
1346
+ const git_diff_options *opts,
1347
+ git_diff_file_cb file_cb,
1348
+ git_diff_hunk_cb hunk_cb,
1349
+ git_diff_data_cb data_cb,
1350
+ void *payload)
1351
+ {
1352
+ GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options");
1269
1353
 
1270
- if (new_blob)
1271
- repo = git_object_owner((git_object *)new_blob);
1272
- else if (old_blob)
1273
- repo = git_object_owner((git_object *)old_blob);
1274
- else
1275
- repo = NULL;
1354
+ memset(data, 0, sizeof(*data));
1276
1355
 
1277
- diff_context_init(
1278
- &ctxt, NULL, repo, options,
1279
- cb_data, file_cb, hunk_cb, data_cb);
1356
+ if (diff_context_init(
1357
+ &data->ctxt, NULL, repo, opts,
1358
+ file_cb, hunk_cb, data_cb, payload) < 0)
1359
+ return -1;
1280
1360
 
1281
- diff_patch_init(&ctxt, &patch);
1361
+ diff_patch_init(&data->ctxt, &data->patch);
1282
1362
 
1283
- /* create a fake delta record and simulate diff_patch_load */
1363
+ return 0;
1364
+ }
1284
1365
 
1285
- memset(&delta, 0, sizeof(delta));
1286
- delta.binary = -1;
1366
+ static int diff_single_apply(diff_single_data *data)
1367
+ {
1368
+ int error;
1369
+ git_diff_delta *delta = &data->delta;
1370
+ bool has_old = ((delta->old_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0);
1371
+ bool has_new = ((delta->new_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0);
1287
1372
 
1288
- set_data_from_blob(old_blob, &patch.old_data, &delta.old_file);
1289
- set_data_from_blob(new_blob, &patch.new_data, &delta.new_file);
1373
+ /* finish setting up fake git_diff_delta record and loaded data */
1290
1374
 
1291
- delta.status = new_blob ?
1292
- (old_blob ? GIT_DELTA_MODIFIED : GIT_DELTA_ADDED) :
1293
- (old_blob ? GIT_DELTA_DELETED : GIT_DELTA_UNTRACKED);
1375
+ data->patch.delta = delta;
1376
+ delta->flags = delta->flags & ~KNOWN_BINARY_FLAGS;
1294
1377
 
1295
- if (git_oid_cmp(&delta.new_file.oid, &delta.old_file.oid) == 0)
1296
- delta.status = GIT_DELTA_UNMODIFIED;
1378
+ delta->status = has_new ?
1379
+ (has_old ? GIT_DELTA_MODIFIED : GIT_DELTA_ADDED) :
1380
+ (has_old ? GIT_DELTA_DELETED : GIT_DELTA_UNTRACKED);
1297
1381
 
1298
- patch.delta = &delta;
1382
+ if (git_oid_cmp(&delta->new_file.oid, &delta->old_file.oid) == 0)
1383
+ delta->status = GIT_DELTA_UNMODIFIED;
1299
1384
 
1300
1385
  if ((error = diff_delta_is_binary_by_content(
1301
- &ctxt, &delta, &delta.old_file, &patch.old_data)) < 0 ||
1386
+ &data->ctxt, delta, &delta->old_file, &data->patch.old_data)) < 0 ||
1302
1387
  (error = diff_delta_is_binary_by_content(
1303
- &ctxt, &delta, &delta.new_file, &patch.new_data)) < 0)
1388
+ &data->ctxt, delta, &delta->new_file, &data->patch.new_data)) < 0)
1304
1389
  goto cleanup;
1305
1390
 
1306
- patch.flags |= GIT_DIFF_PATCH_LOADED;
1307
- if (delta.binary != 1 && delta.status != GIT_DELTA_UNMODIFIED)
1308
- patch.flags |= GIT_DIFF_PATCH_DIFFABLE;
1391
+ data->patch.flags |= GIT_DIFF_PATCH_LOADED;
1392
+
1393
+ if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0 &&
1394
+ delta->status != GIT_DELTA_UNMODIFIED)
1395
+ data->patch.flags |= GIT_DIFF_PATCH_DIFFABLE;
1309
1396
 
1310
1397
  /* do diffs */
1311
1398
 
1312
- if (!(error = diff_delta_file_callback(&ctxt, patch.delta, 1)))
1313
- error = diff_patch_generate(&ctxt, &patch);
1399
+ if (!(error = diff_delta_file_callback(&data->ctxt, delta, 1)))
1400
+ error = diff_patch_generate(&data->ctxt, &data->patch);
1314
1401
 
1315
1402
  cleanup:
1316
- diff_patch_unload(&patch);
1317
-
1318
1403
  if (error == GIT_EUSER)
1319
1404
  giterr_clear();
1320
1405
 
1406
+ diff_patch_unload(&data->patch);
1407
+
1321
1408
  return error;
1322
1409
  }
1323
1410
 
1411
+ int git_diff_blobs(
1412
+ const git_blob *old_blob,
1413
+ const git_blob *new_blob,
1414
+ const git_diff_options *options,
1415
+ git_diff_file_cb file_cb,
1416
+ git_diff_hunk_cb hunk_cb,
1417
+ git_diff_data_cb data_cb,
1418
+ void *payload)
1419
+ {
1420
+ int error;
1421
+ diff_single_data d;
1422
+ git_repository *repo =
1423
+ new_blob ? git_object_owner((const git_object *)new_blob) :
1424
+ old_blob ? git_object_owner((const git_object *)old_blob) : NULL;
1425
+
1426
+ if (!repo) /* Hmm, given two NULL blobs, silently do no callbacks? */
1427
+ return 0;
1428
+
1429
+ if ((error = diff_single_init(
1430
+ &d, repo, options, file_cb, hunk_cb, data_cb, payload)) < 0)
1431
+ return error;
1432
+
1433
+ if (options && (options->flags & GIT_DIFF_REVERSE) != 0) {
1434
+ const git_blob *swap = old_blob;
1435
+ old_blob = new_blob;
1436
+ new_blob = swap;
1437
+ }
1438
+
1439
+ set_data_from_blob(old_blob, &d.patch.old_data, &d.delta.old_file);
1440
+ set_data_from_blob(new_blob, &d.patch.new_data, &d.delta.new_file);
1441
+
1442
+ return diff_single_apply(&d);
1443
+ }
1444
+
1445
+ int git_diff_blob_to_buffer(
1446
+ const git_blob *old_blob,
1447
+ const char *buf,
1448
+ size_t buflen,
1449
+ const git_diff_options *options,
1450
+ git_diff_file_cb file_cb,
1451
+ git_diff_hunk_cb hunk_cb,
1452
+ git_diff_data_cb data_cb,
1453
+ void *payload)
1454
+ {
1455
+ int error;
1456
+ diff_single_data d;
1457
+ git_repository *repo =
1458
+ old_blob ? git_object_owner((const git_object *)old_blob) : NULL;
1459
+
1460
+ if (!repo && !buf) /* Hmm, given NULLs, silently do no callbacks? */
1461
+ return 0;
1462
+
1463
+ if ((error = diff_single_init(
1464
+ &d, repo, options, file_cb, hunk_cb, data_cb, payload)) < 0)
1465
+ return error;
1466
+
1467
+ if (options && (options->flags & GIT_DIFF_REVERSE) != 0) {
1468
+ set_data_from_buffer(buf, buflen, &d.patch.old_data, &d.delta.old_file);
1469
+ set_data_from_blob(old_blob, &d.patch.new_data, &d.delta.new_file);
1470
+ } else {
1471
+ set_data_from_blob(old_blob, &d.patch.old_data, &d.delta.old_file);
1472
+ set_data_from_buffer(buf, buflen, &d.patch.new_data, &d.delta.new_file);
1473
+ }
1474
+
1475
+ return diff_single_apply(&d);
1476
+ }
1324
1477
 
1325
1478
  size_t git_diff_num_deltas(git_diff_list *diff)
1326
1479
  {
@@ -1355,11 +1508,19 @@ int git_diff_get_patch(
1355
1508
 
1356
1509
  if (patch_ptr)
1357
1510
  *patch_ptr = NULL;
1511
+ if (delta_ptr)
1512
+ *delta_ptr = NULL;
1513
+
1514
+ if (diff_required(diff, "git_diff_get_patch") < 0)
1515
+ return -1;
1516
+
1517
+ if (diff_context_init(
1518
+ &ctxt, diff, NULL, NULL,
1519
+ NULL, diff_patch_hunk_cb, diff_patch_line_cb, NULL) < 0)
1520
+ return -1;
1358
1521
 
1359
1522
  delta = git_vector_get(&diff->deltas, idx);
1360
1523
  if (!delta) {
1361
- if (delta_ptr)
1362
- *delta_ptr = NULL;
1363
1524
  giterr_set(GITERR_INVALID, "Index out of range for delta in diff");
1364
1525
  return GIT_ENOTFOUND;
1365
1526
  }
@@ -1368,28 +1529,28 @@ int git_diff_get_patch(
1368
1529
  *delta_ptr = delta;
1369
1530
 
1370
1531
  if (!patch_ptr &&
1371
- (delta->binary != -1 ||
1532
+ ((delta->flags & KNOWN_BINARY_FLAGS) != 0 ||
1372
1533
  (diff->opts.flags & GIT_DIFF_SKIP_BINARY_CHECK) != 0))
1373
1534
  return 0;
1374
1535
 
1375
- diff_context_init(
1376
- &ctxt, diff, diff->repo, &diff->opts,
1377
- NULL, NULL, diff_patch_hunk_cb, diff_patch_line_cb);
1378
-
1379
1536
  if (git_diff_delta__should_skip(ctxt.opts, delta))
1380
1537
  return 0;
1381
1538
 
1539
+ /* Don't load the patch if the user doesn't want it */
1540
+ if (!patch_ptr)
1541
+ return 0;
1542
+
1382
1543
  patch = diff_patch_alloc(&ctxt, delta);
1383
1544
  if (!patch)
1384
1545
  return -1;
1385
1546
 
1386
1547
  if (!(error = diff_patch_load(&ctxt, patch))) {
1387
- ctxt.cb_data = patch;
1548
+ ctxt.payload = patch;
1388
1549
 
1389
1550
  error = diff_patch_generate(&ctxt, patch);
1390
1551
 
1391
1552
  if (error == GIT_EUSER)
1392
- error = ctxt.cb_error;
1553
+ error = ctxt.error;
1393
1554
  }
1394
1555
 
1395
1556
  if (error)
@@ -1418,6 +1579,39 @@ size_t git_diff_patch_num_hunks(git_diff_patch *patch)
1418
1579
  return patch->hunks_size;
1419
1580
  }
1420
1581
 
1582
+ int git_diff_patch_line_stats(
1583
+ size_t *total_ctxt,
1584
+ size_t *total_adds,
1585
+ size_t *total_dels,
1586
+ const git_diff_patch *patch)
1587
+ {
1588
+ size_t totals[3], idx;
1589
+
1590
+ memset(totals, 0, sizeof(totals));
1591
+
1592
+ for (idx = 0; idx < patch->lines_size; ++idx) {
1593
+ switch (patch->lines[idx].origin) {
1594
+ case GIT_DIFF_LINE_CONTEXT: totals[0]++; break;
1595
+ case GIT_DIFF_LINE_ADDITION: totals[1]++; break;
1596
+ case GIT_DIFF_LINE_DELETION: totals[2]++; break;
1597
+ default:
1598
+ /* diff --stat and --numstat don't count EOFNL marks because
1599
+ * they will always be paired with a ADDITION or DELETION line.
1600
+ */
1601
+ break;
1602
+ }
1603
+ }
1604
+
1605
+ if (total_ctxt)
1606
+ *total_ctxt = totals[0];
1607
+ if (total_adds)
1608
+ *total_adds = totals[1];
1609
+ if (total_dels)
1610
+ *total_dels = totals[2];
1611
+
1612
+ return 0;
1613
+ }
1614
+
1421
1615
  int git_diff_patch_get_hunk(
1422
1616
  const git_diff_range **range,
1423
1617
  const char **header,
@@ -1487,8 +1681,8 @@ int git_diff_patch_get_line_in_hunk(
1487
1681
  if (line_origin) *line_origin = line->origin;
1488
1682
  if (content) *content = line->ptr;
1489
1683
  if (content_len) *content_len = line->len;
1490
- if (old_lineno) *old_lineno = line->oldno;
1491
- if (new_lineno) *new_lineno = line->newno;
1684
+ if (old_lineno) *old_lineno = (int)line->oldno;
1685
+ if (new_lineno) *new_lineno = (int)line->newno;
1492
1686
 
1493
1687
  return 0;
1494
1688
 
@@ -1502,3 +1696,124 @@ notfound:
1502
1696
  return GIT_ENOTFOUND;
1503
1697
  }
1504
1698
 
1699
+ static int print_to_buffer_cb(
1700
+ const git_diff_delta *delta,
1701
+ const git_diff_range *range,
1702
+ char line_origin,
1703
+ const char *content,
1704
+ size_t content_len,
1705
+ void *payload)
1706
+ {
1707
+ git_buf *output = payload;
1708
+ GIT_UNUSED(delta); GIT_UNUSED(range); GIT_UNUSED(line_origin);
1709
+ return git_buf_put(output, content, content_len);
1710
+ }
1711
+
1712
+ int git_diff_patch_print(
1713
+ git_diff_patch *patch,
1714
+ git_diff_data_cb print_cb,
1715
+ void *payload)
1716
+ {
1717
+ int error;
1718
+ git_buf temp = GIT_BUF_INIT;
1719
+ diff_print_info pi;
1720
+ size_t h, l;
1721
+
1722
+ assert(patch && print_cb);
1723
+
1724
+ pi.diff = patch->diff;
1725
+ pi.print_cb = print_cb;
1726
+ pi.payload = payload;
1727
+ pi.buf = &temp;
1728
+
1729
+ error = print_patch_file(patch->delta, 0, &pi);
1730
+
1731
+ for (h = 0; h < patch->hunks_size && !error; ++h) {
1732
+ diff_patch_hunk *hunk = &patch->hunks[h];
1733
+
1734
+ error = print_patch_hunk(
1735
+ patch->delta, &hunk->range, hunk->header, hunk->header_len, &pi);
1736
+
1737
+ for (l = 0; l < hunk->line_count && !error; ++l) {
1738
+ diff_patch_line *line = &patch->lines[hunk->line_start + l];
1739
+
1740
+ error = print_patch_line(
1741
+ patch->delta, &hunk->range,
1742
+ line->origin, line->ptr, line->len, &pi);
1743
+ }
1744
+ }
1745
+
1746
+ git_buf_free(&temp);
1747
+
1748
+ return error;
1749
+ }
1750
+
1751
+ int git_diff_patch_to_str(
1752
+ char **string,
1753
+ git_diff_patch *patch)
1754
+ {
1755
+ int error;
1756
+ git_buf output = GIT_BUF_INIT;
1757
+
1758
+ error = git_diff_patch_print(patch, print_to_buffer_cb, &output);
1759
+
1760
+ /* GIT_EUSER means git_buf_put in print_to_buffer_cb returned -1,
1761
+ * meaning a memory allocation failure, so just map to -1...
1762
+ */
1763
+ if (error == GIT_EUSER)
1764
+ error = -1;
1765
+
1766
+ *string = git_buf_detach(&output);
1767
+
1768
+ return error;
1769
+ }
1770
+
1771
+ int git_diff__paired_foreach(
1772
+ git_diff_list *idx2head,
1773
+ git_diff_list *wd2idx,
1774
+ int (*cb)(git_diff_delta *i2h, git_diff_delta *w2i, void *payload),
1775
+ void *payload)
1776
+ {
1777
+ int cmp;
1778
+ git_diff_delta *i2h, *w2i;
1779
+ size_t i, j, i_max, j_max;
1780
+ int (*strcomp)(const char *, const char *);
1781
+
1782
+ i_max = idx2head ? idx2head->deltas.length : 0;
1783
+ j_max = wd2idx ? wd2idx->deltas.length : 0;
1784
+
1785
+ /* Get appropriate strcmp function */
1786
+ strcomp = idx2head ? idx2head->strcomp : wd2idx ? wd2idx->strcomp : NULL;
1787
+
1788
+ /* Assert both iterators use matching ignore-case. If this function ever
1789
+ * supports merging diffs that are not sorted by the same function, then
1790
+ * it will need to spool and sort on one of the results before merging
1791
+ */
1792
+ if (idx2head && wd2idx) {
1793
+ assert(idx2head->strcomp == wd2idx->strcomp);
1794
+ }
1795
+
1796
+ for (i = 0, j = 0; i < i_max || j < j_max; ) {
1797
+ i2h = idx2head ? GIT_VECTOR_GET(&idx2head->deltas,i) : NULL;
1798
+ w2i = wd2idx ? GIT_VECTOR_GET(&wd2idx->deltas,j) : NULL;
1799
+
1800
+ cmp = !w2i ? -1 : !i2h ? 1 :
1801
+ strcomp(i2h->old_file.path, w2i->old_file.path);
1802
+
1803
+ if (cmp < 0) {
1804
+ if (cb(i2h, NULL, payload))
1805
+ return GIT_EUSER;
1806
+ i++;
1807
+ } else if (cmp > 0) {
1808
+ if (cb(NULL, w2i, payload))
1809
+ return GIT_EUSER;
1810
+ j++;
1811
+ } else {
1812
+ if (cb(i2h, w2i, payload))
1813
+ return GIT_EUSER;
1814
+ i++; j++;
1815
+ }
1816
+ }
1817
+
1818
+ return 0;
1819
+ }