rugged 0.25.0b5 → 0.25.0b6

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