rugged 0.24.6.1 → 0.25.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (213) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +1 -1
  3. data/ext/rugged/extconf.rb +9 -2
  4. data/ext/rugged/rugged.c +85 -21
  5. data/ext/rugged/rugged.h +7 -21
  6. data/ext/rugged/rugged_backend.c +3 -20
  7. data/ext/rugged/rugged_blame.c +7 -24
  8. data/ext/rugged/rugged_blob.c +136 -59
  9. data/ext/rugged/rugged_branch.c +3 -20
  10. data/ext/rugged/rugged_branch_collection.c +3 -20
  11. data/ext/rugged/rugged_commit.c +251 -101
  12. data/ext/rugged/rugged_config.c +3 -20
  13. data/ext/rugged/rugged_cred.c +3 -20
  14. data/ext/rugged/rugged_diff.c +3 -20
  15. data/ext/rugged/rugged_diff_delta.c +3 -20
  16. data/ext/rugged/rugged_diff_hunk.c +3 -20
  17. data/ext/rugged/rugged_diff_line.c +3 -20
  18. data/ext/rugged/rugged_index.c +46 -229
  19. data/ext/rugged/rugged_note.c +3 -20
  20. data/ext/rugged/rugged_object.c +3 -20
  21. data/ext/rugged/rugged_patch.c +192 -34
  22. data/ext/rugged/rugged_rebase.c +90 -48
  23. data/ext/rugged/rugged_reference.c +4 -21
  24. data/ext/rugged/rugged_reference_collection.c +3 -20
  25. data/ext/rugged/rugged_remote.c +70 -42
  26. data/ext/rugged/rugged_remote_collection.c +3 -20
  27. data/ext/rugged/rugged_repo.c +50 -59
  28. data/ext/rugged/rugged_revwalk.c +4 -21
  29. data/ext/rugged/rugged_settings.c +3 -20
  30. data/ext/rugged/rugged_signature.c +3 -20
  31. data/ext/rugged/rugged_submodule.c +4 -21
  32. data/ext/rugged/rugged_submodule_collection.c +3 -20
  33. data/ext/rugged/rugged_tag.c +3 -20
  34. data/ext/rugged/rugged_tag_collection.c +3 -20
  35. data/ext/rugged/rugged_tree.c +189 -184
  36. data/lib/rugged/attributes.rb +5 -0
  37. data/lib/rugged/blob.rb +5 -0
  38. data/lib/rugged/branch.rb +6 -1
  39. data/lib/rugged/commit.rb +5 -0
  40. data/lib/rugged/console.rb +5 -0
  41. data/lib/rugged/credentials.rb +5 -0
  42. data/lib/rugged/diff/delta.rb +5 -0
  43. data/lib/rugged/diff/hunk.rb +5 -0
  44. data/lib/rugged/diff/line.rb +5 -0
  45. data/lib/rugged/diff.rb +5 -0
  46. data/lib/rugged/index.rb +120 -0
  47. data/lib/rugged/object.rb +5 -0
  48. data/lib/rugged/patch.rb +5 -0
  49. data/lib/rugged/reference.rb +5 -0
  50. data/lib/rugged/remote.rb +5 -0
  51. data/lib/rugged/repository.rb +9 -4
  52. data/lib/rugged/submodule_collection.rb +5 -0
  53. data/lib/rugged/tag.rb +5 -0
  54. data/lib/rugged/tree.rb +156 -1
  55. data/lib/rugged/version.rb +6 -1
  56. data/lib/rugged/walker.rb +5 -0
  57. data/lib/rugged.rb +5 -0
  58. data/vendor/libgit2/CMakeLists.txt +12 -2
  59. data/vendor/libgit2/include/git2/blob.h +39 -28
  60. data/vendor/libgit2/include/git2/commit.h +76 -0
  61. data/vendor/libgit2/include/git2/common.h +21 -1
  62. data/vendor/libgit2/include/git2/describe.h +5 -2
  63. data/vendor/libgit2/include/git2/diff.h +62 -7
  64. data/vendor/libgit2/include/git2/errors.h +2 -1
  65. data/vendor/libgit2/include/git2/index.h +25 -0
  66. data/vendor/libgit2/include/git2/merge.h +10 -1
  67. data/vendor/libgit2/include/git2/odb.h +47 -1
  68. data/vendor/libgit2/include/git2/pack.h +4 -4
  69. data/vendor/libgit2/include/git2/patch.h +1 -1
  70. data/vendor/libgit2/include/git2/proxy.h +92 -0
  71. data/vendor/libgit2/include/git2/refs.h +11 -0
  72. data/vendor/libgit2/include/git2/remote.h +21 -8
  73. data/vendor/libgit2/include/git2/repository.h +20 -1
  74. data/vendor/libgit2/include/git2/revwalk.h +4 -6
  75. data/vendor/libgit2/include/git2/signature.h +13 -0
  76. data/vendor/libgit2/include/git2/submodule.h +11 -3
  77. data/vendor/libgit2/include/git2/sys/merge.h +177 -0
  78. data/vendor/libgit2/include/git2/sys/odb_backend.h +11 -0
  79. data/vendor/libgit2/include/git2/sys/remote.h +16 -0
  80. data/vendor/libgit2/include/git2/sys/stream.h +2 -1
  81. data/vendor/libgit2/include/git2/sys/time.h +31 -0
  82. data/vendor/libgit2/include/git2/sys/transport.h +3 -1
  83. data/vendor/libgit2/include/git2/tag.h +9 -0
  84. data/vendor/libgit2/include/git2/transaction.h +9 -0
  85. data/vendor/libgit2/include/git2/tree.h +55 -0
  86. data/vendor/libgit2/include/git2/version.h +4 -4
  87. data/vendor/libgit2/include/git2.h +1 -0
  88. data/vendor/libgit2/src/annotated_commit.c +99 -80
  89. data/vendor/libgit2/src/annotated_commit.h +5 -2
  90. data/vendor/libgit2/src/apply.c +377 -0
  91. data/vendor/libgit2/src/apply.h +21 -0
  92. data/vendor/libgit2/src/array.h +0 -1
  93. data/vendor/libgit2/src/blob.c +71 -39
  94. data/vendor/libgit2/src/branch.c +7 -5
  95. data/vendor/libgit2/src/buffer.c +252 -20
  96. data/vendor/libgit2/src/buffer.h +8 -0
  97. data/vendor/libgit2/src/checkout.c +69 -42
  98. data/vendor/libgit2/src/clone.c +0 -8
  99. data/vendor/libgit2/src/commit.c +193 -49
  100. data/vendor/libgit2/src/commit_list.c +8 -3
  101. data/vendor/libgit2/src/commit_list.h +1 -0
  102. data/vendor/libgit2/src/common.h +2 -1
  103. data/vendor/libgit2/src/config.c +3 -3
  104. data/vendor/libgit2/src/config_file.c +20 -10
  105. data/vendor/libgit2/src/crlf.c +1 -0
  106. data/vendor/libgit2/src/curl_stream.c +106 -6
  107. data/vendor/libgit2/src/delta.c +238 -62
  108. data/vendor/libgit2/src/delta.h +79 -58
  109. data/vendor/libgit2/src/describe.c +1 -1
  110. data/vendor/libgit2/src/diff.c +32 -1554
  111. data/vendor/libgit2/src/diff.h +14 -122
  112. data/vendor/libgit2/src/diff_driver.c +4 -6
  113. data/vendor/libgit2/src/diff_file.c +3 -0
  114. data/vendor/libgit2/src/diff_generate.c +1613 -0
  115. data/vendor/libgit2/src/diff_generate.h +123 -0
  116. data/vendor/libgit2/src/diff_parse.c +101 -0
  117. data/vendor/libgit2/src/diff_parse.h +18 -0
  118. data/vendor/libgit2/src/diff_print.c +263 -144
  119. data/vendor/libgit2/src/diff_stats.c +21 -12
  120. data/vendor/libgit2/src/diff_tform.c +1 -0
  121. data/vendor/libgit2/src/diff_tform.h +22 -0
  122. data/vendor/libgit2/src/diff_xdiff.c +9 -9
  123. data/vendor/libgit2/src/diff_xdiff.h +5 -5
  124. data/vendor/libgit2/src/fetchhead.c +8 -8
  125. data/vendor/libgit2/src/filebuf.c +6 -1
  126. data/vendor/libgit2/src/filebuf.h +1 -0
  127. data/vendor/libgit2/src/fileops.c +22 -1
  128. data/vendor/libgit2/src/fileops.h +8 -2
  129. data/vendor/libgit2/src/fnmatch.c +18 -5
  130. data/vendor/libgit2/src/global.c +21 -4
  131. data/vendor/libgit2/src/global.h +6 -0
  132. data/vendor/libgit2/src/graph.c +1 -1
  133. data/vendor/libgit2/src/index.c +159 -46
  134. data/vendor/libgit2/src/index.h +2 -0
  135. data/vendor/libgit2/src/iterator.c +1573 -1468
  136. data/vendor/libgit2/src/iterator.h +52 -69
  137. data/vendor/libgit2/src/merge.c +163 -64
  138. data/vendor/libgit2/src/merge.h +61 -2
  139. data/vendor/libgit2/src/merge_driver.c +397 -0
  140. data/vendor/libgit2/src/merge_driver.h +60 -0
  141. data/vendor/libgit2/src/merge_file.c +11 -49
  142. data/vendor/libgit2/src/netops.c +12 -10
  143. data/vendor/libgit2/src/object_api.c +19 -1
  144. data/vendor/libgit2/src/odb.c +228 -52
  145. data/vendor/libgit2/src/odb_loose.c +19 -1
  146. data/vendor/libgit2/src/odb_mempack.c +1 -1
  147. data/vendor/libgit2/src/odb_pack.c +27 -1
  148. data/vendor/libgit2/src/openssl_stream.c +4 -5
  149. data/vendor/libgit2/src/pack-objects.c +105 -76
  150. data/vendor/libgit2/src/pack-objects.h +13 -12
  151. data/vendor/libgit2/src/pack.c +16 -10
  152. data/vendor/libgit2/src/pack.h +2 -0
  153. data/vendor/libgit2/src/patch.c +216 -0
  154. data/vendor/libgit2/src/patch.h +66 -0
  155. data/vendor/libgit2/src/{diff_patch.c → patch_generate.c} +203 -376
  156. data/vendor/libgit2/src/patch_generate.h +68 -0
  157. data/vendor/libgit2/src/patch_parse.c +1159 -0
  158. data/vendor/libgit2/src/patch_parse.h +56 -0
  159. data/vendor/libgit2/src/path.c +38 -2
  160. data/vendor/libgit2/src/path.h +18 -0
  161. data/vendor/libgit2/src/pathspec.c +1 -1
  162. data/vendor/libgit2/src/pool.h +5 -0
  163. data/vendor/libgit2/src/pqueue.c +12 -5
  164. data/vendor/libgit2/src/pqueue.h +1 -0
  165. data/vendor/libgit2/src/proxy.c +32 -0
  166. data/vendor/libgit2/src/proxy.h +14 -0
  167. data/vendor/libgit2/src/push.c +1 -1
  168. data/vendor/libgit2/src/rebase.c +63 -36
  169. data/vendor/libgit2/src/refdb.c +4 -2
  170. data/vendor/libgit2/src/refdb_fs.c +82 -54
  171. data/vendor/libgit2/src/refs.c +13 -1
  172. data/vendor/libgit2/src/remote.c +20 -81
  173. data/vendor/libgit2/src/repository.c +212 -29
  174. data/vendor/libgit2/src/reset.c +1 -1
  175. data/vendor/libgit2/src/revparse.c +1 -1
  176. data/vendor/libgit2/src/revwalk.c +260 -184
  177. data/vendor/libgit2/src/settings.c +11 -3
  178. data/vendor/libgit2/src/signature.c +27 -2
  179. data/vendor/libgit2/src/sortedcache.c +14 -5
  180. data/vendor/libgit2/src/stash.c +1 -0
  181. data/vendor/libgit2/src/status.c +1 -0
  182. data/vendor/libgit2/src/stransport_stream.c +4 -2
  183. data/vendor/libgit2/src/stream.h +2 -2
  184. data/vendor/libgit2/src/submodule.c +16 -4
  185. data/vendor/libgit2/src/sysdir.c +1 -1
  186. data/vendor/libgit2/src/transport.c +3 -5
  187. data/vendor/libgit2/src/transports/http.c +38 -13
  188. data/vendor/libgit2/src/transports/local.c +4 -1
  189. data/vendor/libgit2/src/transports/smart.c +6 -0
  190. data/vendor/libgit2/src/transports/smart.h +1 -0
  191. data/vendor/libgit2/src/transports/smart_pkt.c +5 -13
  192. data/vendor/libgit2/src/transports/smart_protocol.c +22 -7
  193. data/vendor/libgit2/src/transports/winhttp.c +144 -11
  194. data/vendor/libgit2/src/tree.c +267 -2
  195. data/vendor/libgit2/src/unix/posix.h +10 -0
  196. data/vendor/libgit2/src/unix/pthread.h +2 -0
  197. data/vendor/libgit2/src/util.c +25 -2
  198. data/vendor/libgit2/src/util.h +10 -0
  199. data/vendor/libgit2/src/varint.c +44 -0
  200. data/vendor/libgit2/src/varint.h +15 -0
  201. data/vendor/libgit2/src/vector.c +58 -0
  202. data/vendor/libgit2/src/vector.h +8 -0
  203. data/vendor/libgit2/src/win32/posix.h +3 -0
  204. data/vendor/libgit2/src/win32/thread.c +18 -0
  205. data/vendor/libgit2/src/win32/thread.h +2 -0
  206. data/vendor/libgit2/src/win32/w32_util.h +1 -1
  207. data/vendor/libgit2/src/zstream.c +37 -8
  208. data/vendor/libgit2/src/zstream.h +8 -1
  209. metadata +100 -82
  210. data/vendor/libgit2/Makefile.embed +0 -60
  211. data/vendor/libgit2/src/delta-apply.c +0 -166
  212. data/vendor/libgit2/src/delta-apply.h +0 -62
  213. data/vendor/libgit2/src/diff_patch.h +0 -83
@@ -0,0 +1,1613 @@
1
+ /*
2
+ * Copyright (C) the libgit2 contributors. All rights reserved.
3
+ *
4
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
5
+ * a Linking Exception. For full terms see the included COPYING file.
6
+ */
7
+ #include "common.h"
8
+ #include "diff.h"
9
+ #include "diff_generate.h"
10
+ #include "patch_generate.h"
11
+ #include "fileops.h"
12
+ #include "config.h"
13
+ #include "attr_file.h"
14
+ #include "filter.h"
15
+ #include "pathspec.h"
16
+ #include "index.h"
17
+ #include "odb.h"
18
+ #include "submodule.h"
19
+
20
+ #define DIFF_FLAG_IS_SET(DIFF,FLAG) \
21
+ (((DIFF)->base.opts.flags & (FLAG)) != 0)
22
+ #define DIFF_FLAG_ISNT_SET(DIFF,FLAG) \
23
+ (((DIFF)->base.opts.flags & (FLAG)) == 0)
24
+ #define DIFF_FLAG_SET(DIFF,FLAG,VAL) (DIFF)->base.opts.flags = \
25
+ (VAL) ? ((DIFF)->base.opts.flags | (FLAG)) : \
26
+ ((DIFF)->base.opts.flags & ~(VAL))
27
+
28
+ typedef struct {
29
+ struct git_diff base;
30
+
31
+ git_vector pathspec;
32
+
33
+ uint32_t diffcaps;
34
+ bool index_updated;
35
+ } git_diff_generated;
36
+
37
+ static git_diff_delta *diff_delta__alloc(
38
+ git_diff_generated *diff,
39
+ git_delta_t status,
40
+ const char *path)
41
+ {
42
+ git_diff_delta *delta = git__calloc(1, sizeof(git_diff_delta));
43
+ if (!delta)
44
+ return NULL;
45
+
46
+ delta->old_file.path = git_pool_strdup(&diff->base.pool, path);
47
+ if (delta->old_file.path == NULL) {
48
+ git__free(delta);
49
+ return NULL;
50
+ }
51
+
52
+ delta->new_file.path = delta->old_file.path;
53
+
54
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
55
+ switch (status) {
56
+ case GIT_DELTA_ADDED: status = GIT_DELTA_DELETED; break;
57
+ case GIT_DELTA_DELETED: status = GIT_DELTA_ADDED; break;
58
+ default: break; /* leave other status values alone */
59
+ }
60
+ }
61
+ delta->status = status;
62
+
63
+ return delta;
64
+ }
65
+
66
+ static int diff_insert_delta(
67
+ git_diff_generated *diff,
68
+ git_diff_delta *delta,
69
+ const char *matched_pathspec)
70
+ {
71
+ int error = 0;
72
+
73
+ if (diff->base.opts.notify_cb) {
74
+ error = diff->base.opts.notify_cb(
75
+ &diff->base, delta, matched_pathspec, diff->base.opts.payload);
76
+
77
+ if (error) {
78
+ git__free(delta);
79
+
80
+ if (error > 0) /* positive value means to skip this delta */
81
+ return 0;
82
+ else /* negative value means to cancel diff */
83
+ return giterr_set_after_callback_function(error, "git_diff");
84
+ }
85
+ }
86
+
87
+ if ((error = git_vector_insert(&diff->base.deltas, delta)) < 0)
88
+ git__free(delta);
89
+
90
+ return error;
91
+ }
92
+
93
+ static bool diff_pathspec_match(
94
+ const char **matched_pathspec,
95
+ git_diff_generated *diff,
96
+ const git_index_entry *entry)
97
+ {
98
+ bool disable_pathspec_match =
99
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_DISABLE_PATHSPEC_MATCH);
100
+
101
+ /* If we're disabling fnmatch, then the iterator has already applied
102
+ * the filters to the files for us and we don't have to do anything.
103
+ * However, this only applies to *files* - the iterator will include
104
+ * directories that we need to recurse into when not autoexpanding,
105
+ * so we still need to apply the pathspec match to directories.
106
+ */
107
+ if ((S_ISLNK(entry->mode) || S_ISREG(entry->mode)) &&
108
+ disable_pathspec_match) {
109
+ *matched_pathspec = entry->path;
110
+ return true;
111
+ }
112
+
113
+ return git_pathspec__match(
114
+ &diff->pathspec, entry->path, disable_pathspec_match,
115
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE),
116
+ matched_pathspec, NULL);
117
+ }
118
+
119
+ static int diff_delta__from_one(
120
+ git_diff_generated *diff,
121
+ git_delta_t status,
122
+ const git_index_entry *oitem,
123
+ const git_index_entry *nitem)
124
+ {
125
+ const git_index_entry *entry = nitem;
126
+ bool has_old = false;
127
+ git_diff_delta *delta;
128
+ const char *matched_pathspec;
129
+
130
+ assert((oitem != NULL) ^ (nitem != NULL));
131
+
132
+ if (oitem) {
133
+ entry = oitem;
134
+ has_old = true;
135
+ }
136
+
137
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE))
138
+ has_old = !has_old;
139
+
140
+ if ((entry->flags & GIT_IDXENTRY_VALID) != 0)
141
+ return 0;
142
+
143
+ if (status == GIT_DELTA_IGNORED &&
144
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_IGNORED))
145
+ return 0;
146
+
147
+ if (status == GIT_DELTA_UNTRACKED &&
148
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNTRACKED))
149
+ return 0;
150
+
151
+ if (status == GIT_DELTA_UNREADABLE &&
152
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNREADABLE))
153
+ return 0;
154
+
155
+ if (!diff_pathspec_match(&matched_pathspec, diff, entry))
156
+ return 0;
157
+
158
+ delta = diff_delta__alloc(diff, status, entry->path);
159
+ GITERR_CHECK_ALLOC(delta);
160
+
161
+ /* This fn is just for single-sided diffs */
162
+ assert(status != GIT_DELTA_MODIFIED);
163
+ delta->nfiles = 1;
164
+
165
+ if (has_old) {
166
+ delta->old_file.mode = entry->mode;
167
+ delta->old_file.size = entry->file_size;
168
+ delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS;
169
+ git_oid_cpy(&delta->old_file.id, &entry->id);
170
+ delta->old_file.id_abbrev = GIT_OID_HEXSZ;
171
+ } else /* ADDED, IGNORED, UNTRACKED */ {
172
+ delta->new_file.mode = entry->mode;
173
+ delta->new_file.size = entry->file_size;
174
+ delta->new_file.flags |= GIT_DIFF_FLAG_EXISTS;
175
+ git_oid_cpy(&delta->new_file.id, &entry->id);
176
+ delta->new_file.id_abbrev = GIT_OID_HEXSZ;
177
+ }
178
+
179
+ delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID;
180
+
181
+ if (has_old || !git_oid_iszero(&delta->new_file.id))
182
+ delta->new_file.flags |= GIT_DIFF_FLAG_VALID_ID;
183
+
184
+ return diff_insert_delta(diff, delta, matched_pathspec);
185
+ }
186
+
187
+ static int diff_delta__from_two(
188
+ git_diff_generated *diff,
189
+ git_delta_t status,
190
+ const git_index_entry *old_entry,
191
+ uint32_t old_mode,
192
+ const git_index_entry *new_entry,
193
+ uint32_t new_mode,
194
+ const git_oid *new_id,
195
+ const char *matched_pathspec)
196
+ {
197
+ const git_oid *old_id = &old_entry->id;
198
+ git_diff_delta *delta;
199
+ const char *canonical_path = old_entry->path;
200
+
201
+ if (status == GIT_DELTA_UNMODIFIED &&
202
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNMODIFIED))
203
+ return 0;
204
+
205
+ if (!new_id)
206
+ new_id = &new_entry->id;
207
+
208
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
209
+ uint32_t temp_mode = old_mode;
210
+ const git_index_entry *temp_entry = old_entry;
211
+ const git_oid *temp_id = old_id;
212
+
213
+ old_entry = new_entry;
214
+ new_entry = temp_entry;
215
+ old_mode = new_mode;
216
+ new_mode = temp_mode;
217
+ old_id = new_id;
218
+ new_id = temp_id;
219
+ }
220
+
221
+ delta = diff_delta__alloc(diff, status, canonical_path);
222
+ GITERR_CHECK_ALLOC(delta);
223
+ delta->nfiles = 2;
224
+
225
+ if (!git_index_entry_is_conflict(old_entry)) {
226
+ delta->old_file.size = old_entry->file_size;
227
+ delta->old_file.mode = old_mode;
228
+ git_oid_cpy(&delta->old_file.id, old_id);
229
+ delta->old_file.id_abbrev = GIT_OID_HEXSZ;
230
+ delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID |
231
+ GIT_DIFF_FLAG_EXISTS;
232
+ }
233
+
234
+ if (!git_index_entry_is_conflict(new_entry)) {
235
+ git_oid_cpy(&delta->new_file.id, new_id);
236
+ delta->new_file.id_abbrev = GIT_OID_HEXSZ;
237
+ delta->new_file.size = new_entry->file_size;
238
+ delta->new_file.mode = new_mode;
239
+ delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS;
240
+ delta->new_file.flags |= GIT_DIFF_FLAG_EXISTS;
241
+
242
+ if (!git_oid_iszero(&new_entry->id))
243
+ delta->new_file.flags |= GIT_DIFF_FLAG_VALID_ID;
244
+ }
245
+
246
+ return diff_insert_delta(diff, delta, matched_pathspec);
247
+ }
248
+
249
+ static git_diff_delta *diff_delta__last_for_item(
250
+ git_diff_generated *diff,
251
+ const git_index_entry *item)
252
+ {
253
+ git_diff_delta *delta = git_vector_last(&diff->base.deltas);
254
+ if (!delta)
255
+ return NULL;
256
+
257
+ switch (delta->status) {
258
+ case GIT_DELTA_UNMODIFIED:
259
+ case GIT_DELTA_DELETED:
260
+ if (git_oid__cmp(&delta->old_file.id, &item->id) == 0)
261
+ return delta;
262
+ break;
263
+ case GIT_DELTA_ADDED:
264
+ if (git_oid__cmp(&delta->new_file.id, &item->id) == 0)
265
+ return delta;
266
+ break;
267
+ case GIT_DELTA_UNREADABLE:
268
+ case GIT_DELTA_UNTRACKED:
269
+ if (diff->base.strcomp(delta->new_file.path, item->path) == 0 &&
270
+ git_oid__cmp(&delta->new_file.id, &item->id) == 0)
271
+ return delta;
272
+ break;
273
+ case GIT_DELTA_MODIFIED:
274
+ if (git_oid__cmp(&delta->old_file.id, &item->id) == 0 ||
275
+ git_oid__cmp(&delta->new_file.id, &item->id) == 0)
276
+ return delta;
277
+ break;
278
+ default:
279
+ break;
280
+ }
281
+
282
+ return NULL;
283
+ }
284
+
285
+ static char *diff_strdup_prefix(git_pool *pool, const char *prefix)
286
+ {
287
+ size_t len = strlen(prefix);
288
+
289
+ /* append '/' at end if needed */
290
+ if (len > 0 && prefix[len - 1] != '/')
291
+ return git_pool_strcat(pool, prefix, "/");
292
+ else
293
+ return git_pool_strndup(pool, prefix, len + 1);
294
+ }
295
+
296
+ GIT_INLINE(const char *) diff_delta__i2w_path(const git_diff_delta *delta)
297
+ {
298
+ return delta->old_file.path ?
299
+ delta->old_file.path : delta->new_file.path;
300
+ }
301
+
302
+ int git_diff_delta__i2w_cmp(const void *a, const void *b)
303
+ {
304
+ const git_diff_delta *da = a, *db = b;
305
+ int val = strcmp(diff_delta__i2w_path(da), diff_delta__i2w_path(db));
306
+ return val ? val : ((int)da->status - (int)db->status);
307
+ }
308
+
309
+ int git_diff_delta__i2w_casecmp(const void *a, const void *b)
310
+ {
311
+ const git_diff_delta *da = a, *db = b;
312
+ int val = strcasecmp(diff_delta__i2w_path(da), diff_delta__i2w_path(db));
313
+ return val ? val : ((int)da->status - (int)db->status);
314
+ }
315
+
316
+ bool git_diff_delta__should_skip(
317
+ const git_diff_options *opts, const git_diff_delta *delta)
318
+ {
319
+ uint32_t flags = opts ? opts->flags : 0;
320
+
321
+ if (delta->status == GIT_DELTA_UNMODIFIED &&
322
+ (flags & GIT_DIFF_INCLUDE_UNMODIFIED) == 0)
323
+ return true;
324
+
325
+ if (delta->status == GIT_DELTA_IGNORED &&
326
+ (flags & GIT_DIFF_INCLUDE_IGNORED) == 0)
327
+ return true;
328
+
329
+ if (delta->status == GIT_DELTA_UNTRACKED &&
330
+ (flags & GIT_DIFF_INCLUDE_UNTRACKED) == 0)
331
+ return true;
332
+
333
+ if (delta->status == GIT_DELTA_UNREADABLE &&
334
+ (flags & GIT_DIFF_INCLUDE_UNREADABLE) == 0)
335
+ return true;
336
+
337
+ return false;
338
+ }
339
+
340
+
341
+ static const char *diff_mnemonic_prefix(
342
+ git_iterator_type_t type, bool left_side)
343
+ {
344
+ const char *pfx = "";
345
+
346
+ switch (type) {
347
+ case GIT_ITERATOR_TYPE_EMPTY: pfx = "c"; break;
348
+ case GIT_ITERATOR_TYPE_TREE: pfx = "c"; break;
349
+ case GIT_ITERATOR_TYPE_INDEX: pfx = "i"; break;
350
+ case GIT_ITERATOR_TYPE_WORKDIR: pfx = "w"; break;
351
+ case GIT_ITERATOR_TYPE_FS: pfx = left_side ? "1" : "2"; break;
352
+ default: break;
353
+ }
354
+
355
+ /* note: without a deeper look at pathspecs, there is no easy way
356
+ * to get the (o)bject / (w)ork tree mnemonics working...
357
+ */
358
+
359
+ return pfx;
360
+ }
361
+
362
+ void git_diff__set_ignore_case(git_diff *diff, bool ignore_case)
363
+ {
364
+ if (!ignore_case) {
365
+ diff->opts.flags &= ~GIT_DIFF_IGNORE_CASE;
366
+
367
+ diff->strcomp = git__strcmp;
368
+ diff->strncomp = git__strncmp;
369
+ diff->pfxcomp = git__prefixcmp;
370
+ diff->entrycomp = git_diff__entry_cmp;
371
+
372
+ git_vector_set_cmp(&diff->deltas, git_diff_delta__cmp);
373
+ } else {
374
+ diff->opts.flags |= GIT_DIFF_IGNORE_CASE;
375
+
376
+ diff->strcomp = git__strcasecmp;
377
+ diff->strncomp = git__strncasecmp;
378
+ diff->pfxcomp = git__prefixcmp_icase;
379
+ diff->entrycomp = git_diff__entry_icmp;
380
+
381
+ git_vector_set_cmp(&diff->deltas, git_diff_delta__casecmp);
382
+ }
383
+
384
+ git_vector_sort(&diff->deltas);
385
+ }
386
+
387
+ static void diff_generated_free(git_diff *d)
388
+ {
389
+ git_diff_generated *diff = (git_diff_generated *)d;
390
+
391
+ git_vector_free_deep(&diff->base.deltas);
392
+
393
+ git_pathspec__vfree(&diff->pathspec);
394
+ git_pool_clear(&diff->base.pool);
395
+
396
+ git__memzero(diff, sizeof(*diff));
397
+ git__free(diff);
398
+ }
399
+
400
+ static git_diff_generated *diff_generated_alloc(
401
+ git_repository *repo,
402
+ git_iterator *old_iter,
403
+ git_iterator *new_iter)
404
+ {
405
+ git_diff_generated *diff;
406
+ git_diff_options dflt = GIT_DIFF_OPTIONS_INIT;
407
+
408
+ assert(repo && old_iter && new_iter);
409
+
410
+ if ((diff = git__calloc(1, sizeof(git_diff_generated))) == NULL)
411
+ return NULL;
412
+
413
+ GIT_REFCOUNT_INC(diff);
414
+ diff->base.type = GIT_DIFF_TYPE_GENERATED;
415
+ diff->base.repo = repo;
416
+ diff->base.old_src = old_iter->type;
417
+ diff->base.new_src = new_iter->type;
418
+ diff->base.patch_fn = git_patch_generated_from_diff;
419
+ diff->base.free_fn = diff_generated_free;
420
+ memcpy(&diff->base.opts, &dflt, sizeof(git_diff_options));
421
+
422
+ git_pool_init(&diff->base.pool, 1);
423
+
424
+ if (git_vector_init(&diff->base.deltas, 0, git_diff_delta__cmp) < 0) {
425
+ git_diff_free(&diff->base);
426
+ return NULL;
427
+ }
428
+
429
+ /* Use case-insensitive compare if either iterator has
430
+ * the ignore_case bit set */
431
+ git_diff__set_ignore_case(
432
+ &diff->base,
433
+ git_iterator_ignore_case(old_iter) ||
434
+ git_iterator_ignore_case(new_iter));
435
+
436
+ return diff;
437
+ }
438
+
439
+ static int diff_generated_apply_options(
440
+ git_diff_generated *diff,
441
+ const git_diff_options *opts)
442
+ {
443
+ git_config *cfg = NULL;
444
+ git_repository *repo = diff->base.repo;
445
+ git_pool *pool = &diff->base.pool;
446
+ int val;
447
+
448
+ if (opts) {
449
+ /* copy user options (except case sensitivity info from iterators) */
450
+ bool icase = DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE);
451
+ memcpy(&diff->base.opts, opts, sizeof(diff->base.opts));
452
+ DIFF_FLAG_SET(diff, GIT_DIFF_IGNORE_CASE, icase);
453
+
454
+ /* initialize pathspec from options */
455
+ if (git_pathspec__vinit(&diff->pathspec, &opts->pathspec, pool) < 0)
456
+ return -1;
457
+ }
458
+
459
+ /* flag INCLUDE_TYPECHANGE_TREES implies INCLUDE_TYPECHANGE */
460
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES))
461
+ diff->base.opts.flags |= GIT_DIFF_INCLUDE_TYPECHANGE;
462
+
463
+ /* flag INCLUDE_UNTRACKED_CONTENT implies INCLUDE_UNTRACKED */
464
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_SHOW_UNTRACKED_CONTENT))
465
+ diff->base.opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED;
466
+
467
+ /* load config values that affect diff behavior */
468
+ if ((val = git_repository_config_snapshot(&cfg, repo)) < 0)
469
+ return val;
470
+
471
+ if (!git_config__cvar(&val, cfg, GIT_CVAR_SYMLINKS) && val)
472
+ diff->diffcaps |= GIT_DIFFCAPS_HAS_SYMLINKS;
473
+
474
+ if (!git_config__cvar(&val, cfg, GIT_CVAR_IGNORESTAT) && val)
475
+ diff->diffcaps |= GIT_DIFFCAPS_IGNORE_STAT;
476
+
477
+ if ((diff->base.opts.flags & GIT_DIFF_IGNORE_FILEMODE) == 0 &&
478
+ !git_config__cvar(&val, cfg, GIT_CVAR_FILEMODE) && val)
479
+ diff->diffcaps |= GIT_DIFFCAPS_TRUST_MODE_BITS;
480
+
481
+ if (!git_config__cvar(&val, cfg, GIT_CVAR_TRUSTCTIME) && val)
482
+ diff->diffcaps |= GIT_DIFFCAPS_TRUST_CTIME;
483
+
484
+ /* Don't set GIT_DIFFCAPS_USE_DEV - compile time option in core git */
485
+
486
+ /* If not given explicit `opts`, check `diff.xyz` configs */
487
+ if (!opts) {
488
+ int context = git_config__get_int_force(cfg, "diff.context", 3);
489
+ diff->base.opts.context_lines = context >= 0 ? (uint32_t)context : 3;
490
+
491
+ /* add other defaults here */
492
+ }
493
+
494
+ /* Reverse src info if diff is reversed */
495
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
496
+ git_iterator_type_t tmp_src = diff->base.old_src;
497
+ diff->base.old_src = diff->base.new_src;
498
+ diff->base.new_src = tmp_src;
499
+ }
500
+
501
+ /* Unset UPDATE_INDEX unless diffing workdir and index */
502
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_UPDATE_INDEX) &&
503
+ (!(diff->base.old_src == GIT_ITERATOR_TYPE_WORKDIR ||
504
+ diff->base.new_src == GIT_ITERATOR_TYPE_WORKDIR) ||
505
+ !(diff->base.old_src == GIT_ITERATOR_TYPE_INDEX ||
506
+ diff->base.new_src == GIT_ITERATOR_TYPE_INDEX)))
507
+ diff->base.opts.flags &= ~GIT_DIFF_UPDATE_INDEX;
508
+
509
+ /* if ignore_submodules not explicitly set, check diff config */
510
+ if (diff->base.opts.ignore_submodules <= 0) {
511
+ git_config_entry *entry;
512
+ git_config__lookup_entry(&entry, cfg, "diff.ignoresubmodules", true);
513
+
514
+ if (entry && git_submodule_parse_ignore(
515
+ &diff->base.opts.ignore_submodules, entry->value) < 0)
516
+ giterr_clear();
517
+ git_config_entry_free(entry);
518
+ }
519
+
520
+ /* if either prefix is not set, figure out appropriate value */
521
+ if (!diff->base.opts.old_prefix || !diff->base.opts.new_prefix) {
522
+ const char *use_old = DIFF_OLD_PREFIX_DEFAULT;
523
+ const char *use_new = DIFF_NEW_PREFIX_DEFAULT;
524
+
525
+ if (git_config__get_bool_force(cfg, "diff.noprefix", 0))
526
+ use_old = use_new = "";
527
+ else if (git_config__get_bool_force(cfg, "diff.mnemonicprefix", 0)) {
528
+ use_old = diff_mnemonic_prefix(diff->base.old_src, true);
529
+ use_new = diff_mnemonic_prefix(diff->base.new_src, false);
530
+ }
531
+
532
+ if (!diff->base.opts.old_prefix)
533
+ diff->base.opts.old_prefix = use_old;
534
+ if (!diff->base.opts.new_prefix)
535
+ diff->base.opts.new_prefix = use_new;
536
+ }
537
+
538
+ /* strdup prefix from pool so we're not dependent on external data */
539
+ diff->base.opts.old_prefix = diff_strdup_prefix(pool, diff->base.opts.old_prefix);
540
+ diff->base.opts.new_prefix = diff_strdup_prefix(pool, diff->base.opts.new_prefix);
541
+
542
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
543
+ const char *tmp_prefix = diff->base.opts.old_prefix;
544
+ diff->base.opts.old_prefix = diff->base.opts.new_prefix;
545
+ diff->base.opts.new_prefix = tmp_prefix;
546
+ }
547
+
548
+ git_config_free(cfg);
549
+
550
+ /* check strdup results for error */
551
+ return (!diff->base.opts.old_prefix || !diff->base.opts.new_prefix) ? -1 : 0;
552
+ }
553
+
554
+ int git_diff__oid_for_file(
555
+ git_oid *out,
556
+ git_diff *diff,
557
+ const char *path,
558
+ uint16_t mode,
559
+ git_off_t size)
560
+ {
561
+ git_index_entry entry;
562
+
563
+ memset(&entry, 0, sizeof(entry));
564
+ entry.mode = mode;
565
+ entry.file_size = size;
566
+ entry.path = (char *)path;
567
+
568
+ return git_diff__oid_for_entry(out, diff, &entry, mode, NULL);
569
+ }
570
+
571
+ int git_diff__oid_for_entry(
572
+ git_oid *out,
573
+ git_diff *d,
574
+ const git_index_entry *src,
575
+ uint16_t mode,
576
+ const git_oid *update_match)
577
+ {
578
+ git_diff_generated *diff;
579
+ git_buf full_path = GIT_BUF_INIT;
580
+ git_index_entry entry = *src;
581
+ git_filter_list *fl = NULL;
582
+ int error = 0;
583
+
584
+ assert(d->type == GIT_DIFF_TYPE_GENERATED);
585
+ diff = (git_diff_generated *)d;
586
+
587
+ memset(out, 0, sizeof(*out));
588
+
589
+ if (git_buf_joinpath(&full_path,
590
+ git_repository_workdir(diff->base.repo), entry.path) < 0)
591
+ return -1;
592
+
593
+ if (!mode) {
594
+ struct stat st;
595
+
596
+ diff->base.perf.stat_calls++;
597
+
598
+ if (p_stat(full_path.ptr, &st) < 0) {
599
+ error = git_path_set_error(errno, entry.path, "stat");
600
+ git_buf_free(&full_path);
601
+ return error;
602
+ }
603
+
604
+ git_index_entry__init_from_stat(&entry,
605
+ &st, (diff->diffcaps & GIT_DIFFCAPS_TRUST_MODE_BITS) != 0);
606
+ }
607
+
608
+ /* calculate OID for file if possible */
609
+ if (S_ISGITLINK(mode)) {
610
+ git_submodule *sm;
611
+
612
+ if (!git_submodule_lookup(&sm, diff->base.repo, entry.path)) {
613
+ const git_oid *sm_oid = git_submodule_wd_id(sm);
614
+ if (sm_oid)
615
+ git_oid_cpy(out, sm_oid);
616
+ git_submodule_free(sm);
617
+ } else {
618
+ /* if submodule lookup failed probably just in an intermediate
619
+ * state where some init hasn't happened, so ignore the error
620
+ */
621
+ giterr_clear();
622
+ }
623
+ } else if (S_ISLNK(mode)) {
624
+ error = git_odb__hashlink(out, full_path.ptr);
625
+ diff->base.perf.oid_calculations++;
626
+ } else if (!git__is_sizet(entry.file_size)) {
627
+ giterr_set(GITERR_OS, "File size overflow (for 32-bits) on '%s'",
628
+ entry.path);
629
+ error = -1;
630
+ } else if (!(error = git_filter_list_load(&fl,
631
+ diff->base.repo, NULL, entry.path,
632
+ GIT_FILTER_TO_ODB, GIT_FILTER_ALLOW_UNSAFE)))
633
+ {
634
+ int fd = git_futils_open_ro(full_path.ptr);
635
+ if (fd < 0)
636
+ error = fd;
637
+ else {
638
+ error = git_odb__hashfd_filtered(
639
+ out, fd, (size_t)entry.file_size, GIT_OBJ_BLOB, fl);
640
+ p_close(fd);
641
+ diff->base.perf.oid_calculations++;
642
+ }
643
+
644
+ git_filter_list_free(fl);
645
+ }
646
+
647
+ /* update index for entry if requested */
648
+ if (!error && update_match && git_oid_equal(out, update_match)) {
649
+ git_index *idx;
650
+ git_index_entry updated_entry;
651
+
652
+ memcpy(&updated_entry, &entry, sizeof(git_index_entry));
653
+ updated_entry.mode = mode;
654
+ git_oid_cpy(&updated_entry.id, out);
655
+
656
+ if (!(error = git_repository_index__weakptr(&idx,
657
+ diff->base.repo))) {
658
+ error = git_index_add(idx, &updated_entry);
659
+ diff->index_updated = true;
660
+ }
661
+ }
662
+
663
+ git_buf_free(&full_path);
664
+ return error;
665
+ }
666
+
667
+ typedef struct {
668
+ git_repository *repo;
669
+ git_iterator *old_iter;
670
+ git_iterator *new_iter;
671
+ const git_index_entry *oitem;
672
+ const git_index_entry *nitem;
673
+ } diff_in_progress;
674
+
675
+ #define MODE_BITS_MASK 0000777
676
+
677
+ static int maybe_modified_submodule(
678
+ git_delta_t *status,
679
+ git_oid *found_oid,
680
+ git_diff_generated *diff,
681
+ diff_in_progress *info)
682
+ {
683
+ int error = 0;
684
+ git_submodule *sub;
685
+ unsigned int sm_status = 0;
686
+ git_submodule_ignore_t ign = diff->base.opts.ignore_submodules;
687
+
688
+ *status = GIT_DELTA_UNMODIFIED;
689
+
690
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES) ||
691
+ ign == GIT_SUBMODULE_IGNORE_ALL)
692
+ return 0;
693
+
694
+ if ((error = git_submodule_lookup(
695
+ &sub, diff->base.repo, info->nitem->path)) < 0) {
696
+
697
+ /* GIT_EEXISTS means dir with .git in it was found - ignore it */
698
+ if (error == GIT_EEXISTS) {
699
+ giterr_clear();
700
+ error = 0;
701
+ }
702
+ return error;
703
+ }
704
+
705
+ if (ign <= 0 && git_submodule_ignore(sub) == GIT_SUBMODULE_IGNORE_ALL)
706
+ /* ignore it */;
707
+ else if ((error = git_submodule__status(
708
+ &sm_status, NULL, NULL, found_oid, sub, ign)) < 0)
709
+ /* return error below */;
710
+
711
+ /* check IS_WD_UNMODIFIED because this case is only used
712
+ * when the new side of the diff is the working directory
713
+ */
714
+ else if (!GIT_SUBMODULE_STATUS_IS_WD_UNMODIFIED(sm_status))
715
+ *status = GIT_DELTA_MODIFIED;
716
+
717
+ /* now that we have a HEAD OID, check if HEAD moved */
718
+ else if ((sm_status & GIT_SUBMODULE_STATUS_IN_WD) != 0 &&
719
+ !git_oid_equal(&info->oitem->id, found_oid))
720
+ *status = GIT_DELTA_MODIFIED;
721
+
722
+ git_submodule_free(sub);
723
+ return error;
724
+ }
725
+
726
+ static int maybe_modified(
727
+ git_diff_generated *diff,
728
+ diff_in_progress *info)
729
+ {
730
+ git_oid noid;
731
+ git_delta_t status = GIT_DELTA_MODIFIED;
732
+ const git_index_entry *oitem = info->oitem;
733
+ const git_index_entry *nitem = info->nitem;
734
+ unsigned int omode = oitem->mode;
735
+ unsigned int nmode = nitem->mode;
736
+ bool new_is_workdir = (info->new_iter->type == GIT_ITERATOR_TYPE_WORKDIR);
737
+ bool modified_uncertain = false;
738
+ const char *matched_pathspec;
739
+ int error = 0;
740
+
741
+ if (!diff_pathspec_match(&matched_pathspec, diff, oitem))
742
+ return 0;
743
+
744
+ memset(&noid, 0, sizeof(noid));
745
+
746
+ /* on platforms with no symlinks, preserve mode of existing symlinks */
747
+ if (S_ISLNK(omode) && S_ISREG(nmode) && new_is_workdir &&
748
+ !(diff->diffcaps & GIT_DIFFCAPS_HAS_SYMLINKS))
749
+ nmode = omode;
750
+
751
+ /* on platforms with no execmode, just preserve old mode */
752
+ if (!(diff->diffcaps & GIT_DIFFCAPS_TRUST_MODE_BITS) &&
753
+ (nmode & MODE_BITS_MASK) != (omode & MODE_BITS_MASK) &&
754
+ new_is_workdir)
755
+ nmode = (nmode & ~MODE_BITS_MASK) | (omode & MODE_BITS_MASK);
756
+
757
+ /* if one side is a conflict, mark the whole delta as conflicted */
758
+ if (git_index_entry_is_conflict(oitem) ||
759
+ git_index_entry_is_conflict(nitem)) {
760
+ status = GIT_DELTA_CONFLICTED;
761
+
762
+ /* support "assume unchanged" (poorly, b/c we still stat everything) */
763
+ } else if ((oitem->flags & GIT_IDXENTRY_VALID) != 0) {
764
+ status = GIT_DELTA_UNMODIFIED;
765
+
766
+ /* support "skip worktree" index bit */
767
+ } else if ((oitem->flags_extended & GIT_IDXENTRY_SKIP_WORKTREE) != 0) {
768
+ status = GIT_DELTA_UNMODIFIED;
769
+
770
+ /* if basic type of file changed, then split into delete and add */
771
+ } else if (GIT_MODE_TYPE(omode) != GIT_MODE_TYPE(nmode)) {
772
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE)) {
773
+ status = GIT_DELTA_TYPECHANGE;
774
+ }
775
+
776
+ else if (nmode == GIT_FILEMODE_UNREADABLE) {
777
+ if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL)))
778
+ error = diff_delta__from_one(diff, GIT_DELTA_UNREADABLE, NULL, nitem);
779
+ return error;
780
+ }
781
+
782
+ else {
783
+ if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL)))
784
+ error = diff_delta__from_one(diff, GIT_DELTA_ADDED, NULL, nitem);
785
+ return error;
786
+ }
787
+
788
+ /* if oids and modes match (and are valid), then file is unmodified */
789
+ } else if (git_oid_equal(&oitem->id, &nitem->id) &&
790
+ omode == nmode &&
791
+ !git_oid_iszero(&oitem->id)) {
792
+ status = GIT_DELTA_UNMODIFIED;
793
+
794
+ /* if we have an unknown OID and a workdir iterator, then check some
795
+ * circumstances that can accelerate things or need special handling
796
+ */
797
+ } else if (git_oid_iszero(&nitem->id) && new_is_workdir) {
798
+ bool use_ctime =
799
+ ((diff->diffcaps & GIT_DIFFCAPS_TRUST_CTIME) != 0);
800
+ git_index *index = git_iterator_index(info->new_iter);
801
+
802
+ status = GIT_DELTA_UNMODIFIED;
803
+
804
+ if (S_ISGITLINK(nmode)) {
805
+ if ((error = maybe_modified_submodule(&status, &noid, diff, info)) < 0)
806
+ return error;
807
+ }
808
+
809
+ /* if the stat data looks different, then mark modified - this just
810
+ * means that the OID will be recalculated below to confirm change
811
+ */
812
+ else if (omode != nmode || oitem->file_size != nitem->file_size) {
813
+ status = GIT_DELTA_MODIFIED;
814
+ modified_uncertain =
815
+ (oitem->file_size <= 0 && nitem->file_size > 0);
816
+ }
817
+ else if (!git_index_time_eq(&oitem->mtime, &nitem->mtime) ||
818
+ (use_ctime && !git_index_time_eq(&oitem->ctime, &nitem->ctime)) ||
819
+ oitem->ino != nitem->ino ||
820
+ oitem->uid != nitem->uid ||
821
+ oitem->gid != nitem->gid ||
822
+ git_index_entry_newer_than_index(nitem, index))
823
+ {
824
+ status = GIT_DELTA_MODIFIED;
825
+ modified_uncertain = true;
826
+ }
827
+
828
+ /* if mode is GITLINK and submodules are ignored, then skip */
829
+ } else if (S_ISGITLINK(nmode) &&
830
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES)) {
831
+ status = GIT_DELTA_UNMODIFIED;
832
+ }
833
+
834
+ /* if we got here and decided that the files are modified, but we
835
+ * haven't calculated the OID of the new item, then calculate it now
836
+ */
837
+ if (modified_uncertain && git_oid_iszero(&nitem->id)) {
838
+ const git_oid *update_check =
839
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_UPDATE_INDEX) && omode == nmode ?
840
+ &oitem->id : NULL;
841
+
842
+ if ((error = git_diff__oid_for_entry(
843
+ &noid, &diff->base, nitem, nmode, update_check)) < 0)
844
+ return error;
845
+
846
+ /* if oid matches, then mark unmodified (except submodules, where
847
+ * the filesystem content may be modified even if the oid still
848
+ * matches between the index and the workdir HEAD)
849
+ */
850
+ if (omode == nmode && !S_ISGITLINK(omode) &&
851
+ git_oid_equal(&oitem->id, &noid))
852
+ status = GIT_DELTA_UNMODIFIED;
853
+ }
854
+
855
+ /* If we want case changes, then break this into a delete of the old
856
+ * and an add of the new so that consumers can act accordingly (eg,
857
+ * checkout will update the case on disk.)
858
+ */
859
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE) &&
860
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_CASECHANGE) &&
861
+ strcmp(oitem->path, nitem->path) != 0) {
862
+
863
+ if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL)))
864
+ error = diff_delta__from_one(diff, GIT_DELTA_ADDED, NULL, nitem);
865
+
866
+ return error;
867
+ }
868
+
869
+ return diff_delta__from_two(
870
+ diff, status, oitem, omode, nitem, nmode,
871
+ git_oid_iszero(&noid) ? NULL : &noid, matched_pathspec);
872
+ }
873
+
874
+ static bool entry_is_prefixed(
875
+ git_diff_generated *diff,
876
+ const git_index_entry *item,
877
+ const git_index_entry *prefix_item)
878
+ {
879
+ size_t pathlen;
880
+
881
+ if (!item || diff->base.pfxcomp(item->path, prefix_item->path) != 0)
882
+ return false;
883
+
884
+ pathlen = strlen(prefix_item->path);
885
+
886
+ return (prefix_item->path[pathlen - 1] == '/' ||
887
+ item->path[pathlen] == '\0' ||
888
+ item->path[pathlen] == '/');
889
+ }
890
+
891
+ static int iterator_current(
892
+ const git_index_entry **entry,
893
+ git_iterator *iterator)
894
+ {
895
+ int error;
896
+
897
+ if ((error = git_iterator_current(entry, iterator)) == GIT_ITEROVER) {
898
+ *entry = NULL;
899
+ error = 0;
900
+ }
901
+
902
+ return error;
903
+ }
904
+
905
+ static int iterator_advance(
906
+ const git_index_entry **entry,
907
+ git_iterator *iterator)
908
+ {
909
+ const git_index_entry *prev_entry = *entry;
910
+ int cmp, error;
911
+
912
+ /* if we're looking for conflicts, we only want to report
913
+ * one conflict for each file, instead of all three sides.
914
+ * so if this entry is a conflict for this file, and the
915
+ * previous one was a conflict for the same file, skip it.
916
+ */
917
+ while ((error = git_iterator_advance(entry, iterator)) == 0) {
918
+ if (!(iterator->flags & GIT_ITERATOR_INCLUDE_CONFLICTS) ||
919
+ !git_index_entry_is_conflict(prev_entry) ||
920
+ !git_index_entry_is_conflict(*entry))
921
+ break;
922
+
923
+ cmp = (iterator->flags & GIT_ITERATOR_IGNORE_CASE) ?
924
+ strcasecmp(prev_entry->path, (*entry)->path) :
925
+ strcmp(prev_entry->path, (*entry)->path);
926
+
927
+ if (cmp)
928
+ break;
929
+ }
930
+
931
+ if (error == GIT_ITEROVER) {
932
+ *entry = NULL;
933
+ error = 0;
934
+ }
935
+
936
+ return error;
937
+ }
938
+
939
+ static int iterator_advance_into(
940
+ const git_index_entry **entry,
941
+ git_iterator *iterator)
942
+ {
943
+ int error;
944
+
945
+ if ((error = git_iterator_advance_into(entry, iterator)) == GIT_ITEROVER) {
946
+ *entry = NULL;
947
+ error = 0;
948
+ }
949
+
950
+ return error;
951
+ }
952
+
953
+ static int iterator_advance_over(
954
+ const git_index_entry **entry,
955
+ git_iterator_status_t *status,
956
+ git_iterator *iterator)
957
+ {
958
+ int error = git_iterator_advance_over(entry, status, iterator);
959
+
960
+ if (error == GIT_ITEROVER) {
961
+ *entry = NULL;
962
+ error = 0;
963
+ }
964
+
965
+ return error;
966
+ }
967
+
968
+ static int handle_unmatched_new_item(
969
+ git_diff_generated *diff, diff_in_progress *info)
970
+ {
971
+ int error = 0;
972
+ const git_index_entry *nitem = info->nitem;
973
+ git_delta_t delta_type = GIT_DELTA_UNTRACKED;
974
+ bool contains_oitem;
975
+
976
+ /* check if this is a prefix of the other side */
977
+ contains_oitem = entry_is_prefixed(diff, info->oitem, nitem);
978
+
979
+ /* update delta_type if this item is conflicted */
980
+ if (git_index_entry_is_conflict(nitem))
981
+ delta_type = GIT_DELTA_CONFLICTED;
982
+
983
+ /* update delta_type if this item is ignored */
984
+ else if (git_iterator_current_is_ignored(info->new_iter))
985
+ delta_type = GIT_DELTA_IGNORED;
986
+
987
+ if (nitem->mode == GIT_FILEMODE_TREE) {
988
+ bool recurse_into_dir = contains_oitem;
989
+
990
+ /* check if user requests recursion into this type of dir */
991
+ recurse_into_dir = contains_oitem ||
992
+ (delta_type == GIT_DELTA_UNTRACKED &&
993
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_RECURSE_UNTRACKED_DIRS)) ||
994
+ (delta_type == GIT_DELTA_IGNORED &&
995
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_RECURSE_IGNORED_DIRS));
996
+
997
+ /* do not advance into directories that contain a .git file */
998
+ if (recurse_into_dir && !contains_oitem) {
999
+ git_buf *full = NULL;
1000
+ if (git_iterator_current_workdir_path(&full, info->new_iter) < 0)
1001
+ return -1;
1002
+ if (full && git_path_contains(full, DOT_GIT)) {
1003
+ /* TODO: warning if not a valid git repository */
1004
+ recurse_into_dir = false;
1005
+ }
1006
+ }
1007
+
1008
+ /* still have to look into untracked directories to match core git -
1009
+ * with no untracked files, directory is treated as ignored
1010
+ */
1011
+ if (!recurse_into_dir &&
1012
+ delta_type == GIT_DELTA_UNTRACKED &&
1013
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_ENABLE_FAST_UNTRACKED_DIRS))
1014
+ {
1015
+ git_diff_delta *last;
1016
+ git_iterator_status_t untracked_state;
1017
+
1018
+ /* attempt to insert record for this directory */
1019
+ if ((error = diff_delta__from_one(diff, delta_type, NULL, nitem)) != 0)
1020
+ return error;
1021
+
1022
+ /* if delta wasn't created (because of rules), just skip ahead */
1023
+ last = diff_delta__last_for_item(diff, nitem);
1024
+ if (!last)
1025
+ return iterator_advance(&info->nitem, info->new_iter);
1026
+
1027
+ /* iterate into dir looking for an actual untracked file */
1028
+ if ((error = iterator_advance_over(
1029
+ &info->nitem, &untracked_state, info->new_iter)) < 0)
1030
+ return error;
1031
+
1032
+ /* if we found nothing that matched our pathlist filter, exclude */
1033
+ if (untracked_state == GIT_ITERATOR_STATUS_FILTERED) {
1034
+ git_vector_pop(&diff->base.deltas);
1035
+ git__free(last);
1036
+ }
1037
+
1038
+ /* if we found nothing or just ignored items, update the record */
1039
+ if (untracked_state == GIT_ITERATOR_STATUS_IGNORED ||
1040
+ untracked_state == GIT_ITERATOR_STATUS_EMPTY) {
1041
+ last->status = GIT_DELTA_IGNORED;
1042
+
1043
+ /* remove the record if we don't want ignored records */
1044
+ if (DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_IGNORED)) {
1045
+ git_vector_pop(&diff->base.deltas);
1046
+ git__free(last);
1047
+ }
1048
+ }
1049
+
1050
+ return 0;
1051
+ }
1052
+
1053
+ /* try to advance into directory if necessary */
1054
+ if (recurse_into_dir) {
1055
+ error = iterator_advance_into(&info->nitem, info->new_iter);
1056
+
1057
+ /* if directory is empty, can't advance into it, so skip it */
1058
+ if (error == GIT_ENOTFOUND) {
1059
+ giterr_clear();
1060
+ error = iterator_advance(&info->nitem, info->new_iter);
1061
+ }
1062
+
1063
+ return error;
1064
+ }
1065
+ }
1066
+
1067
+ else if (delta_type == GIT_DELTA_IGNORED &&
1068
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_RECURSE_IGNORED_DIRS) &&
1069
+ git_iterator_current_tree_is_ignored(info->new_iter))
1070
+ /* item contained in ignored directory, so skip over it */
1071
+ return iterator_advance(&info->nitem, info->new_iter);
1072
+
1073
+ else if (info->new_iter->type != GIT_ITERATOR_TYPE_WORKDIR) {
1074
+ if (delta_type != GIT_DELTA_CONFLICTED)
1075
+ delta_type = GIT_DELTA_ADDED;
1076
+ }
1077
+
1078
+ else if (nitem->mode == GIT_FILEMODE_COMMIT) {
1079
+ /* ignore things that are not actual submodules */
1080
+ if (git_submodule_lookup(NULL, info->repo, nitem->path) != 0) {
1081
+ giterr_clear();
1082
+ delta_type = GIT_DELTA_IGNORED;
1083
+
1084
+ /* if this contains a tracked item, treat as normal TREE */
1085
+ if (contains_oitem) {
1086
+ error = iterator_advance_into(&info->nitem, info->new_iter);
1087
+ if (error != GIT_ENOTFOUND)
1088
+ return error;
1089
+
1090
+ giterr_clear();
1091
+ return iterator_advance(&info->nitem, info->new_iter);
1092
+ }
1093
+ }
1094
+ }
1095
+
1096
+ else if (nitem->mode == GIT_FILEMODE_UNREADABLE) {
1097
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_UNREADABLE_AS_UNTRACKED))
1098
+ delta_type = GIT_DELTA_UNTRACKED;
1099
+ else
1100
+ delta_type = GIT_DELTA_UNREADABLE;
1101
+ }
1102
+
1103
+ /* Actually create the record for this item if necessary */
1104
+ if ((error = diff_delta__from_one(diff, delta_type, NULL, nitem)) != 0)
1105
+ return error;
1106
+
1107
+ /* If user requested TYPECHANGE records, then check for that instead of
1108
+ * just generating an ADDED/UNTRACKED record
1109
+ */
1110
+ if (delta_type != GIT_DELTA_IGNORED &&
1111
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES) &&
1112
+ contains_oitem)
1113
+ {
1114
+ /* this entry was prefixed with a tree - make TYPECHANGE */
1115
+ git_diff_delta *last = diff_delta__last_for_item(diff, nitem);
1116
+ if (last) {
1117
+ last->status = GIT_DELTA_TYPECHANGE;
1118
+ last->old_file.mode = GIT_FILEMODE_TREE;
1119
+ }
1120
+ }
1121
+
1122
+ return iterator_advance(&info->nitem, info->new_iter);
1123
+ }
1124
+
1125
+ static int handle_unmatched_old_item(
1126
+ git_diff_generated *diff, diff_in_progress *info)
1127
+ {
1128
+ git_delta_t delta_type = GIT_DELTA_DELETED;
1129
+ int error;
1130
+
1131
+ /* update delta_type if this item is conflicted */
1132
+ if (git_index_entry_is_conflict(info->oitem))
1133
+ delta_type = GIT_DELTA_CONFLICTED;
1134
+
1135
+ if ((error = diff_delta__from_one(diff, delta_type, info->oitem, NULL)) < 0)
1136
+ return error;
1137
+
1138
+ /* if we are generating TYPECHANGE records then check for that
1139
+ * instead of just generating a DELETE record
1140
+ */
1141
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES) &&
1142
+ entry_is_prefixed(diff, info->nitem, info->oitem))
1143
+ {
1144
+ /* this entry has become a tree! convert to TYPECHANGE */
1145
+ git_diff_delta *last = diff_delta__last_for_item(diff, info->oitem);
1146
+ if (last) {
1147
+ last->status = GIT_DELTA_TYPECHANGE;
1148
+ last->new_file.mode = GIT_FILEMODE_TREE;
1149
+ }
1150
+
1151
+ /* If new_iter is a workdir iterator, then this situation
1152
+ * will certainly be followed by a series of untracked items.
1153
+ * Unless RECURSE_UNTRACKED_DIRS is set, skip over them...
1154
+ */
1155
+ if (S_ISDIR(info->nitem->mode) &&
1156
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_RECURSE_UNTRACKED_DIRS))
1157
+ return iterator_advance(&info->nitem, info->new_iter);
1158
+ }
1159
+
1160
+ return iterator_advance(&info->oitem, info->old_iter);
1161
+ }
1162
+
1163
+ static int handle_matched_item(
1164
+ git_diff_generated *diff, diff_in_progress *info)
1165
+ {
1166
+ int error = 0;
1167
+
1168
+ if ((error = maybe_modified(diff, info)) < 0)
1169
+ return error;
1170
+
1171
+ if (!(error = iterator_advance(&info->oitem, info->old_iter)))
1172
+ error = iterator_advance(&info->nitem, info->new_iter);
1173
+
1174
+ return error;
1175
+ }
1176
+
1177
+ int git_diff__from_iterators(
1178
+ git_diff **out,
1179
+ git_repository *repo,
1180
+ git_iterator *old_iter,
1181
+ git_iterator *new_iter,
1182
+ const git_diff_options *opts)
1183
+ {
1184
+ git_diff_generated *diff;
1185
+ diff_in_progress info;
1186
+ int error = 0;
1187
+
1188
+ *out = NULL;
1189
+
1190
+ diff = diff_generated_alloc(repo, old_iter, new_iter);
1191
+ GITERR_CHECK_ALLOC(diff);
1192
+
1193
+ info.repo = repo;
1194
+ info.old_iter = old_iter;
1195
+ info.new_iter = new_iter;
1196
+
1197
+ /* make iterators have matching icase behavior */
1198
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE)) {
1199
+ git_iterator_set_ignore_case(old_iter, true);
1200
+ git_iterator_set_ignore_case(new_iter, true);
1201
+ }
1202
+
1203
+ /* finish initialization */
1204
+ if ((error = diff_generated_apply_options(diff, opts)) < 0)
1205
+ goto cleanup;
1206
+
1207
+ if ((error = iterator_current(&info.oitem, old_iter)) < 0 ||
1208
+ (error = iterator_current(&info.nitem, new_iter)) < 0)
1209
+ goto cleanup;
1210
+
1211
+ /* run iterators building diffs */
1212
+ while (!error && (info.oitem || info.nitem)) {
1213
+ int cmp;
1214
+
1215
+ /* report progress */
1216
+ if (opts && opts->progress_cb) {
1217
+ if ((error = opts->progress_cb(&diff->base,
1218
+ info.oitem ? info.oitem->path : NULL,
1219
+ info.nitem ? info.nitem->path : NULL,
1220
+ opts->payload)))
1221
+ break;
1222
+ }
1223
+
1224
+ cmp = info.oitem ?
1225
+ (info.nitem ? diff->base.entrycomp(info.oitem, info.nitem) : -1) : 1;
1226
+
1227
+ /* create DELETED records for old items not matched in new */
1228
+ if (cmp < 0)
1229
+ error = handle_unmatched_old_item(diff, &info);
1230
+
1231
+ /* create ADDED, TRACKED, or IGNORED records for new items not
1232
+ * matched in old (and/or descend into directories as needed)
1233
+ */
1234
+ else if (cmp > 0)
1235
+ error = handle_unmatched_new_item(diff, &info);
1236
+
1237
+ /* otherwise item paths match, so create MODIFIED record
1238
+ * (or ADDED and DELETED pair if type changed)
1239
+ */
1240
+ else
1241
+ error = handle_matched_item(diff, &info);
1242
+ }
1243
+
1244
+ diff->base.perf.stat_calls +=
1245
+ old_iter->stat_calls + new_iter->stat_calls;
1246
+
1247
+ cleanup:
1248
+ if (!error)
1249
+ *out = &diff->base;
1250
+ else
1251
+ git_diff_free(&diff->base);
1252
+
1253
+ return error;
1254
+ }
1255
+
1256
+ #define DIFF_FROM_ITERATORS(MAKE_FIRST, FLAGS_FIRST, MAKE_SECOND, FLAGS_SECOND) do { \
1257
+ git_iterator *a = NULL, *b = NULL; \
1258
+ char *pfx = (opts && !(opts->flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH)) ? \
1259
+ git_pathspec_prefix(&opts->pathspec) : NULL; \
1260
+ git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT, \
1261
+ b_opts = GIT_ITERATOR_OPTIONS_INIT; \
1262
+ a_opts.flags = FLAGS_FIRST; \
1263
+ a_opts.start = pfx; \
1264
+ a_opts.end = pfx; \
1265
+ b_opts.flags = FLAGS_SECOND; \
1266
+ b_opts.start = pfx; \
1267
+ b_opts.end = pfx; \
1268
+ GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options"); \
1269
+ if (opts && (opts->flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH)) { \
1270
+ a_opts.pathlist.strings = opts->pathspec.strings; \
1271
+ a_opts.pathlist.count = opts->pathspec.count; \
1272
+ b_opts.pathlist.strings = opts->pathspec.strings; \
1273
+ b_opts.pathlist.count = opts->pathspec.count; \
1274
+ } \
1275
+ if (!error && !(error = MAKE_FIRST) && !(error = MAKE_SECOND)) \
1276
+ error = git_diff__from_iterators(&diff, repo, a, b, opts); \
1277
+ git__free(pfx); git_iterator_free(a); git_iterator_free(b); \
1278
+ } while (0)
1279
+
1280
+ int git_diff_tree_to_tree(
1281
+ git_diff **out,
1282
+ git_repository *repo,
1283
+ git_tree *old_tree,
1284
+ git_tree *new_tree,
1285
+ const git_diff_options *opts)
1286
+ {
1287
+ git_diff *diff = NULL;
1288
+ git_iterator_flag_t iflag = GIT_ITERATOR_DONT_IGNORE_CASE;
1289
+ int error = 0;
1290
+
1291
+ assert(out && repo);
1292
+
1293
+ *out = NULL;
1294
+
1295
+ /* for tree to tree diff, be case sensitive even if the index is
1296
+ * currently case insensitive, unless the user explicitly asked
1297
+ * for case insensitivity
1298
+ */
1299
+ if (opts && (opts->flags & GIT_DIFF_IGNORE_CASE) != 0)
1300
+ iflag = GIT_ITERATOR_IGNORE_CASE;
1301
+
1302
+ DIFF_FROM_ITERATORS(
1303
+ git_iterator_for_tree(&a, old_tree, &a_opts), iflag,
1304
+ git_iterator_for_tree(&b, new_tree, &b_opts), iflag
1305
+ );
1306
+
1307
+ if (!error)
1308
+ *out = diff;
1309
+
1310
+ return error;
1311
+ }
1312
+
1313
+ static int diff_load_index(git_index **index, git_repository *repo)
1314
+ {
1315
+ int error = git_repository_index__weakptr(index, repo);
1316
+
1317
+ /* reload the repository index when user did not pass one in */
1318
+ if (!error && git_index_read(*index, false) < 0)
1319
+ giterr_clear();
1320
+
1321
+ return error;
1322
+ }
1323
+
1324
+ int git_diff_tree_to_index(
1325
+ git_diff **out,
1326
+ git_repository *repo,
1327
+ git_tree *old_tree,
1328
+ git_index *index,
1329
+ const git_diff_options *opts)
1330
+ {
1331
+ git_diff *diff = NULL;
1332
+ git_iterator_flag_t iflag = GIT_ITERATOR_DONT_IGNORE_CASE |
1333
+ GIT_ITERATOR_INCLUDE_CONFLICTS;
1334
+ bool index_ignore_case = false;
1335
+ int error = 0;
1336
+
1337
+ assert(out && repo);
1338
+
1339
+ *out = NULL;
1340
+
1341
+ if (!index && (error = diff_load_index(&index, repo)) < 0)
1342
+ return error;
1343
+
1344
+ index_ignore_case = index->ignore_case;
1345
+
1346
+ DIFF_FROM_ITERATORS(
1347
+ git_iterator_for_tree(&a, old_tree, &a_opts), iflag,
1348
+ git_iterator_for_index(&b, repo, index, &b_opts), iflag
1349
+ );
1350
+
1351
+ /* if index is in case-insensitive order, re-sort deltas to match */
1352
+ if (!error && index_ignore_case)
1353
+ git_diff__set_ignore_case(diff, true);
1354
+
1355
+ if (!error)
1356
+ *out = diff;
1357
+
1358
+ return error;
1359
+ }
1360
+
1361
+ int git_diff_index_to_workdir(
1362
+ git_diff **out,
1363
+ git_repository *repo,
1364
+ git_index *index,
1365
+ const git_diff_options *opts)
1366
+ {
1367
+ git_diff *diff = NULL;
1368
+ int error = 0;
1369
+
1370
+ assert(out && repo);
1371
+
1372
+ *out = NULL;
1373
+
1374
+ if (!index && (error = diff_load_index(&index, repo)) < 0)
1375
+ return error;
1376
+
1377
+ DIFF_FROM_ITERATORS(
1378
+ git_iterator_for_index(&a, repo, index, &a_opts),
1379
+ GIT_ITERATOR_INCLUDE_CONFLICTS,
1380
+
1381
+ git_iterator_for_workdir(&b, repo, index, NULL, &b_opts),
1382
+ GIT_ITERATOR_DONT_AUTOEXPAND
1383
+ );
1384
+
1385
+ if (!error && (diff->opts.flags & GIT_DIFF_UPDATE_INDEX) != 0 &&
1386
+ ((git_diff_generated *)diff)->index_updated)
1387
+ error = git_index_write(index);
1388
+
1389
+ if (!error)
1390
+ *out = diff;
1391
+
1392
+ return error;
1393
+ }
1394
+
1395
+ int git_diff_tree_to_workdir(
1396
+ git_diff **out,
1397
+ git_repository *repo,
1398
+ git_tree *old_tree,
1399
+ const git_diff_options *opts)
1400
+ {
1401
+ git_diff *diff = NULL;
1402
+ git_index *index;
1403
+ int error = 0;
1404
+
1405
+ assert(out && repo);
1406
+
1407
+ *out = NULL;
1408
+
1409
+ if ((error = git_repository_index__weakptr(&index, repo)))
1410
+ return error;
1411
+
1412
+ DIFF_FROM_ITERATORS(
1413
+ git_iterator_for_tree(&a, old_tree, &a_opts), 0,
1414
+ git_iterator_for_workdir(&b, repo, index, old_tree, &b_opts), GIT_ITERATOR_DONT_AUTOEXPAND
1415
+ );
1416
+
1417
+ if (!error)
1418
+ *out = diff;
1419
+
1420
+ return error;
1421
+ }
1422
+
1423
+ int git_diff_tree_to_workdir_with_index(
1424
+ git_diff **out,
1425
+ git_repository *repo,
1426
+ git_tree *tree,
1427
+ const git_diff_options *opts)
1428
+ {
1429
+ git_diff *d1 = NULL, *d2 = NULL;
1430
+ git_index *index = NULL;
1431
+ int error = 0;
1432
+
1433
+ assert(out && repo);
1434
+
1435
+ *out = NULL;
1436
+
1437
+ if ((error = diff_load_index(&index, repo)) < 0)
1438
+ return error;
1439
+
1440
+ if (!(error = git_diff_tree_to_index(&d1, repo, tree, index, opts)) &&
1441
+ !(error = git_diff_index_to_workdir(&d2, repo, index, opts)))
1442
+ error = git_diff_merge(d1, d2);
1443
+
1444
+ git_diff_free(d2);
1445
+
1446
+ if (error) {
1447
+ git_diff_free(d1);
1448
+ d1 = NULL;
1449
+ }
1450
+
1451
+ *out = d1;
1452
+ return error;
1453
+ }
1454
+
1455
+ int git_diff_index_to_index(
1456
+ git_diff **out,
1457
+ git_repository *repo,
1458
+ git_index *old_index,
1459
+ git_index *new_index,
1460
+ const git_diff_options *opts)
1461
+ {
1462
+ git_diff *diff;
1463
+ int error = 0;
1464
+
1465
+ assert(out && old_index && new_index);
1466
+
1467
+ *out = NULL;
1468
+
1469
+ DIFF_FROM_ITERATORS(
1470
+ git_iterator_for_index(&a, repo, old_index, &a_opts), GIT_ITERATOR_DONT_IGNORE_CASE,
1471
+ git_iterator_for_index(&b, repo, new_index, &b_opts), GIT_ITERATOR_DONT_IGNORE_CASE
1472
+ );
1473
+
1474
+ /* if index is in case-insensitive order, re-sort deltas to match */
1475
+ if (!error && (old_index->ignore_case || new_index->ignore_case))
1476
+ git_diff__set_ignore_case(diff, true);
1477
+
1478
+ if (!error)
1479
+ *out = diff;
1480
+
1481
+ return error;
1482
+ }
1483
+
1484
+ int git_diff__paired_foreach(
1485
+ git_diff *head2idx,
1486
+ git_diff *idx2wd,
1487
+ int (*cb)(git_diff_delta *h2i, git_diff_delta *i2w, void *payload),
1488
+ void *payload)
1489
+ {
1490
+ int cmp, error = 0;
1491
+ git_diff_delta *h2i, *i2w;
1492
+ size_t i, j, i_max, j_max;
1493
+ int (*strcomp)(const char *, const char *) = git__strcmp;
1494
+ bool h2i_icase, i2w_icase, icase_mismatch;
1495
+
1496
+ i_max = head2idx ? head2idx->deltas.length : 0;
1497
+ j_max = idx2wd ? idx2wd->deltas.length : 0;
1498
+ if (!i_max && !j_max)
1499
+ return 0;
1500
+
1501
+ /* At some point, tree-to-index diffs will probably never ignore case,
1502
+ * even if that isn't true now. Index-to-workdir diffs may or may not
1503
+ * ignore case, but the index filename for the idx2wd diff should
1504
+ * still be using the canonical case-preserving name.
1505
+ *
1506
+ * Therefore the main thing we need to do here is make sure the diffs
1507
+ * are traversed in a compatible order. To do this, we temporarily
1508
+ * resort a mismatched diff to get the order correct.
1509
+ *
1510
+ * In order to traverse renames in the index->workdir, we need to
1511
+ * ensure that we compare the index name on both sides, so we
1512
+ * always sort by the old name in the i2w list.
1513
+ */
1514
+ h2i_icase = head2idx != NULL && git_diff_is_sorted_icase(head2idx);
1515
+ i2w_icase = idx2wd != NULL && git_diff_is_sorted_icase(idx2wd);
1516
+
1517
+ icase_mismatch =
1518
+ (head2idx != NULL && idx2wd != NULL && h2i_icase != i2w_icase);
1519
+
1520
+ if (icase_mismatch && h2i_icase) {
1521
+ git_vector_set_cmp(&head2idx->deltas, git_diff_delta__cmp);
1522
+ git_vector_sort(&head2idx->deltas);
1523
+ }
1524
+
1525
+ if (i2w_icase && !icase_mismatch) {
1526
+ strcomp = git__strcasecmp;
1527
+
1528
+ git_vector_set_cmp(&idx2wd->deltas, git_diff_delta__i2w_casecmp);
1529
+ git_vector_sort(&idx2wd->deltas);
1530
+ } else if (idx2wd != NULL) {
1531
+ git_vector_set_cmp(&idx2wd->deltas, git_diff_delta__i2w_cmp);
1532
+ git_vector_sort(&idx2wd->deltas);
1533
+ }
1534
+
1535
+ for (i = 0, j = 0; i < i_max || j < j_max; ) {
1536
+ h2i = head2idx ? GIT_VECTOR_GET(&head2idx->deltas, i) : NULL;
1537
+ i2w = idx2wd ? GIT_VECTOR_GET(&idx2wd->deltas, j) : NULL;
1538
+
1539
+ cmp = !i2w ? -1 : !h2i ? 1 :
1540
+ strcomp(h2i->new_file.path, i2w->old_file.path);
1541
+
1542
+ if (cmp < 0) {
1543
+ i++; i2w = NULL;
1544
+ } else if (cmp > 0) {
1545
+ j++; h2i = NULL;
1546
+ } else {
1547
+ i++; j++;
1548
+ }
1549
+
1550
+ if ((error = cb(h2i, i2w, payload)) != 0) {
1551
+ giterr_set_after_callback(error);
1552
+ break;
1553
+ }
1554
+ }
1555
+
1556
+ /* restore case-insensitive delta sort */
1557
+ if (icase_mismatch && h2i_icase) {
1558
+ git_vector_set_cmp(&head2idx->deltas, git_diff_delta__casecmp);
1559
+ git_vector_sort(&head2idx->deltas);
1560
+ }
1561
+
1562
+ /* restore idx2wd sort by new path */
1563
+ if (idx2wd != NULL) {
1564
+ git_vector_set_cmp(&idx2wd->deltas,
1565
+ i2w_icase ? git_diff_delta__casecmp : git_diff_delta__cmp);
1566
+ git_vector_sort(&idx2wd->deltas);
1567
+ }
1568
+
1569
+ return error;
1570
+ }
1571
+
1572
+ int git_diff__commit(
1573
+ git_diff **out,
1574
+ git_repository *repo,
1575
+ const git_commit *commit,
1576
+ const git_diff_options *opts)
1577
+ {
1578
+ git_commit *parent = NULL;
1579
+ git_diff *commit_diff = NULL;
1580
+ git_tree *old_tree = NULL, *new_tree = NULL;
1581
+ size_t parents;
1582
+ int error = 0;
1583
+
1584
+ *out = NULL;
1585
+
1586
+ if ((parents = git_commit_parentcount(commit)) > 1) {
1587
+ char commit_oidstr[GIT_OID_HEXSZ + 1];
1588
+
1589
+ error = -1;
1590
+ giterr_set(GITERR_INVALID, "Commit %s is a merge commit",
1591
+ git_oid_tostr(commit_oidstr, GIT_OID_HEXSZ + 1, git_commit_id(commit)));
1592
+ goto on_error;
1593
+ }
1594
+
1595
+ if (parents > 0)
1596
+ if ((error = git_commit_parent(&parent, commit, 0)) < 0 ||
1597
+ (error = git_commit_tree(&old_tree, parent)) < 0)
1598
+ goto on_error;
1599
+
1600
+ if ((error = git_commit_tree(&new_tree, commit)) < 0 ||
1601
+ (error = git_diff_tree_to_tree(&commit_diff, repo, old_tree, new_tree, opts)) < 0)
1602
+ goto on_error;
1603
+
1604
+ *out = commit_diff;
1605
+
1606
+ on_error:
1607
+ git_tree_free(new_tree);
1608
+ git_tree_free(old_tree);
1609
+ git_commit_free(parent);
1610
+
1611
+ return error;
1612
+ }
1613
+