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
@@ -42,8 +42,8 @@ typedef struct git_pobject {
42
42
  * me */
43
43
 
44
44
  void *delta_data;
45
- unsigned long delta_size;
46
- unsigned long z_delta_size;
45
+ size_t delta_size;
46
+ size_t z_delta_size;
47
47
 
48
48
  int written:1,
49
49
  recursing:1,
@@ -65,10 +65,11 @@ struct git_packbuilder {
65
65
  git_zstream zstream;
66
66
 
67
67
  uint32_t nr_objects,
68
- nr_deltified,
69
- nr_alloc,
70
- nr_written,
71
- nr_remaining;
68
+ nr_deltified,
69
+ nr_written,
70
+ nr_remaining;
71
+
72
+ size_t nr_alloc;
72
73
 
73
74
  git_pobject *object_list;
74
75
 
@@ -85,13 +86,13 @@ struct git_packbuilder {
85
86
  git_cond progress_cond;
86
87
 
87
88
  /* configs */
88
- uint64_t delta_cache_size;
89
- uint64_t max_delta_cache_size;
90
- uint64_t cache_max_small_delta_size;
91
- uint64_t big_file_threshold;
92
- uint64_t window_memory_limit;
89
+ size_t delta_cache_size;
90
+ size_t max_delta_cache_size;
91
+ size_t cache_max_small_delta_size;
92
+ size_t big_file_threshold;
93
+ size_t window_memory_limit;
93
94
 
94
- int nr_threads; /* nr of threads to use */
95
+ unsigned int nr_threads; /* nr of threads to use */
95
96
 
96
97
  git_packbuilder_progress progress_cb;
97
98
  void *progress_cb_payload;
@@ -8,7 +8,7 @@
8
8
  #include "common.h"
9
9
  #include "odb.h"
10
10
  #include "pack.h"
11
- #include "delta-apply.h"
11
+ #include "delta.h"
12
12
  #include "sha1_lookup.h"
13
13
  #include "mwindow.h"
14
14
  #include "fileops.h"
@@ -505,7 +505,7 @@ int git_packfile_resolve_header(
505
505
  git_mwindow_close(&w_curs);
506
506
  if ((error = git_packfile_stream_open(&stream, p, curpos)) < 0)
507
507
  return error;
508
- error = git__delta_read_header_fromstream(&base_size, size_p, &stream);
508
+ error = git_delta_read_header_fromstream(&base_size, size_p, &stream);
509
509
  git_packfile_stream_free(&stream);
510
510
  if (error < 0)
511
511
  return error;
@@ -730,8 +730,9 @@ int git_packfile_unpack(
730
730
  obj->len = 0;
731
731
  obj->type = GIT_OBJ_BAD;
732
732
 
733
- error = git__delta_apply(obj, base.data, base.len, delta.data, delta.len);
733
+ error = git_delta_apply(&obj->data, &obj->len, base.data, base.len, delta.data, delta.len);
734
734
  obj->type = base_type;
735
+
735
736
  /*
736
737
  * We usually don't want to free the base at this
737
738
  * point, as we put it into the cache in the previous
@@ -102,6 +102,8 @@ struct git_pack_file {
102
102
 
103
103
  git_pack_cache bases; /* delta base cache */
104
104
 
105
+ time_t last_freshen; /* last time the packfile was freshened */
106
+
105
107
  /* something like ".git/objects/pack/xxxxx.pack" */
106
108
  char pack_name[GIT_FLEX_ARRAY]; /* more */
107
109
  };
@@ -0,0 +1,207 @@
1
+ #include "git2/patch.h"
2
+ #include "diff.h"
3
+ #include "patch.h"
4
+
5
+
6
+ int git_patch__invoke_callbacks(
7
+ git_patch *patch,
8
+ git_diff_file_cb file_cb,
9
+ git_diff_binary_cb binary_cb,
10
+ git_diff_hunk_cb hunk_cb,
11
+ git_diff_line_cb line_cb,
12
+ void *payload)
13
+ {
14
+ int error = 0;
15
+ uint32_t i, j;
16
+
17
+ if (file_cb)
18
+ error = file_cb(patch->delta, 0, payload);
19
+
20
+ if ((patch->delta->flags & GIT_DIFF_FLAG_BINARY) != 0) {
21
+ if (binary_cb)
22
+ error = binary_cb(patch->delta, &patch->binary, payload);
23
+
24
+ return error;
25
+ }
26
+
27
+ if (!hunk_cb && !line_cb)
28
+ return error;
29
+
30
+ for (i = 0; !error && i < git_array_size(patch->hunks); ++i) {
31
+ git_patch_hunk *h = git_array_get(patch->hunks, i);
32
+
33
+ if (hunk_cb)
34
+ error = hunk_cb(patch->delta, &h->hunk, payload);
35
+
36
+ if (!line_cb)
37
+ continue;
38
+
39
+ for (j = 0; !error && j < h->line_count; ++j) {
40
+ git_diff_line *l =
41
+ git_array_get(patch->lines, h->line_start + j);
42
+
43
+ error = line_cb(patch->delta, &h->hunk, l, payload);
44
+ }
45
+ }
46
+
47
+ return error;
48
+ }
49
+
50
+ size_t git_patch_size(
51
+ git_patch *patch,
52
+ int include_context,
53
+ int include_hunk_headers,
54
+ int include_file_headers)
55
+ {
56
+ size_t out;
57
+
58
+ assert(patch);
59
+
60
+ out = patch->content_size;
61
+
62
+ if (!include_context)
63
+ out -= patch->context_size;
64
+
65
+ if (include_hunk_headers)
66
+ out += patch->header_size;
67
+
68
+ if (include_file_headers) {
69
+ git_buf file_header = GIT_BUF_INIT;
70
+
71
+ if (git_diff_delta__format_file_header(
72
+ &file_header, patch->delta, NULL, NULL, 0) < 0)
73
+ giterr_clear();
74
+ else
75
+ out += git_buf_len(&file_header);
76
+
77
+ git_buf_free(&file_header);
78
+ }
79
+
80
+ return out;
81
+ }
82
+
83
+ int git_patch_line_stats(
84
+ size_t *total_ctxt,
85
+ size_t *total_adds,
86
+ size_t *total_dels,
87
+ const git_patch *patch)
88
+ {
89
+ size_t totals[3], idx;
90
+
91
+ memset(totals, 0, sizeof(totals));
92
+
93
+ for (idx = 0; idx < git_array_size(patch->lines); ++idx) {
94
+ git_diff_line *line = git_array_get(patch->lines, idx);
95
+ if (!line)
96
+ continue;
97
+
98
+ switch (line->origin) {
99
+ case GIT_DIFF_LINE_CONTEXT: totals[0]++; break;
100
+ case GIT_DIFF_LINE_ADDITION: totals[1]++; break;
101
+ case GIT_DIFF_LINE_DELETION: totals[2]++; break;
102
+ default:
103
+ /* diff --stat and --numstat don't count EOFNL marks because
104
+ * they will always be paired with a ADDITION or DELETION line.
105
+ */
106
+ break;
107
+ }
108
+ }
109
+
110
+ if (total_ctxt)
111
+ *total_ctxt = totals[0];
112
+ if (total_adds)
113
+ *total_adds = totals[1];
114
+ if (total_dels)
115
+ *total_dels = totals[2];
116
+
117
+ return 0;
118
+ }
119
+
120
+ const git_diff_delta *git_patch_get_delta(const git_patch *patch)
121
+ {
122
+ assert(patch);
123
+ return patch->delta;
124
+ }
125
+
126
+ size_t git_patch_num_hunks(const git_patch *patch)
127
+ {
128
+ assert(patch);
129
+ return git_array_size(patch->hunks);
130
+ }
131
+
132
+ static int patch_error_outofrange(const char *thing)
133
+ {
134
+ giterr_set(GITERR_INVALID, "patch %s index out of range", thing);
135
+ return GIT_ENOTFOUND;
136
+ }
137
+
138
+ int git_patch_get_hunk(
139
+ const git_diff_hunk **out,
140
+ size_t *lines_in_hunk,
141
+ git_patch *patch,
142
+ size_t hunk_idx)
143
+ {
144
+ git_patch_hunk *hunk;
145
+ assert(patch);
146
+
147
+ hunk = git_array_get(patch->hunks, hunk_idx);
148
+
149
+ if (!hunk) {
150
+ if (out) *out = NULL;
151
+ if (lines_in_hunk) *lines_in_hunk = 0;
152
+ return patch_error_outofrange("hunk");
153
+ }
154
+
155
+ if (out) *out = &hunk->hunk;
156
+ if (lines_in_hunk) *lines_in_hunk = hunk->line_count;
157
+ return 0;
158
+ }
159
+
160
+ int git_patch_num_lines_in_hunk(const git_patch *patch, size_t hunk_idx)
161
+ {
162
+ git_patch_hunk *hunk;
163
+ assert(patch);
164
+
165
+ if (!(hunk = git_array_get(patch->hunks, hunk_idx)))
166
+ return patch_error_outofrange("hunk");
167
+ return (int)hunk->line_count;
168
+ }
169
+
170
+ int git_patch_get_line_in_hunk(
171
+ const git_diff_line **out,
172
+ git_patch *patch,
173
+ size_t hunk_idx,
174
+ size_t line_of_hunk)
175
+ {
176
+ git_patch_hunk *hunk;
177
+ git_diff_line *line;
178
+
179
+ assert(patch);
180
+
181
+ if (!(hunk = git_array_get(patch->hunks, hunk_idx))) {
182
+ if (out) *out = NULL;
183
+ return patch_error_outofrange("hunk");
184
+ }
185
+
186
+ if (line_of_hunk >= hunk->line_count ||
187
+ !(line = git_array_get(
188
+ patch->lines, hunk->line_start + line_of_hunk))) {
189
+ if (out) *out = NULL;
190
+ return patch_error_outofrange("line");
191
+ }
192
+
193
+ if (out) *out = line;
194
+ return 0;
195
+ }
196
+
197
+ static void git_patch__free(git_patch *patch)
198
+ {
199
+ if (patch->free_fn)
200
+ patch->free_fn(patch);
201
+ }
202
+
203
+ void git_patch_free(git_patch *patch)
204
+ {
205
+ if (patch)
206
+ GIT_REFCOUNT_DEC(patch, git_patch__free);
207
+ }
@@ -0,0 +1,66 @@
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
+ #ifndef INCLUDE_patch_h__
8
+ #define INCLUDE_patch_h__
9
+
10
+ #include "git2/patch.h"
11
+ #include "array.h"
12
+
13
+ /* cached information about a hunk in a patch */
14
+ typedef struct git_patch_hunk {
15
+ git_diff_hunk hunk;
16
+ size_t line_start;
17
+ size_t line_count;
18
+ } git_patch_hunk;
19
+
20
+ struct git_patch {
21
+ git_refcount rc;
22
+
23
+ git_repository *repo; /* may be null */
24
+
25
+ git_diff_options diff_opts;
26
+
27
+ git_diff_delta *delta;
28
+ git_diff_binary binary;
29
+ git_array_t(git_patch_hunk) hunks;
30
+ git_array_t(git_diff_line) lines;
31
+
32
+ size_t header_size;
33
+ size_t content_size;
34
+ size_t context_size;
35
+
36
+ void (*free_fn)(git_patch *patch);
37
+ };
38
+
39
+ extern int git_patch__invoke_callbacks(
40
+ git_patch *patch,
41
+ git_diff_file_cb file_cb,
42
+ git_diff_binary_cb binary_cb,
43
+ git_diff_hunk_cb hunk_cb,
44
+ git_diff_line_cb line_cb,
45
+ void *payload);
46
+
47
+ extern int git_patch_line_stats(
48
+ size_t *total_ctxt,
49
+ size_t *total_adds,
50
+ size_t *total_dels,
51
+ const git_patch *patch);
52
+
53
+ /** Options for parsing patch files. */
54
+ typedef struct {
55
+ /**
56
+ * The length of the prefix (in path segments) for the filenames.
57
+ * This prefix will be removed when looking for files. The default is 1.
58
+ */
59
+ uint32_t prefix_len;
60
+ } git_patch_options;
61
+
62
+ #define GIT_PATCH_OPTIONS_INIT { 1 }
63
+
64
+ extern void git_patch_free(git_patch *patch);
65
+
66
+ #endif
@@ -7,49 +7,78 @@
7
7
  #include "common.h"
8
8
  #include "git2/blob.h"
9
9
  #include "diff.h"
10
+ #include "diff_generate.h"
10
11
  #include "diff_file.h"
11
12
  #include "diff_driver.h"
12
- #include "diff_patch.h"
13
+ #include "patch_generate.h"
13
14
  #include "diff_xdiff.h"
14
15
  #include "delta.h"
15
16
  #include "zstream.h"
16
17
  #include "fileops.h"
17
18
 
18
19
  static void diff_output_init(
19
- git_diff_output*, const git_diff_options*, git_diff_file_cb,
20
+ git_patch_generated_output *, const git_diff_options *, git_diff_file_cb,
20
21
  git_diff_binary_cb, git_diff_hunk_cb, git_diff_line_cb, void*);
21
22
 
22
- static void diff_output_to_patch(git_diff_output *, git_patch *);
23
+ static void diff_output_to_patch(
24
+ git_patch_generated_output *, git_patch_generated *);
23
25
 
24
- static void diff_patch_update_binary(git_patch *patch)
26
+ static void patch_generated_free(git_patch *p)
25
27
  {
26
- if ((patch->delta->flags & DIFF_FLAGS_KNOWN_BINARY) != 0)
28
+ git_patch_generated *patch = (git_patch_generated *)p;
29
+
30
+ git_array_clear(patch->base.lines);
31
+ git_array_clear(patch->base.hunks);
32
+
33
+ git__free((char *)patch->base.binary.old_file.data);
34
+ git__free((char *)patch->base.binary.new_file.data);
35
+
36
+ git_diff_file_content__clear(&patch->ofile);
37
+ git_diff_file_content__clear(&patch->nfile);
38
+
39
+ git_diff_free(patch->diff); /* decrements refcount */
40
+ patch->diff = NULL;
41
+
42
+ git_pool_clear(&patch->flattened);
43
+
44
+ git__free((char *)patch->base.diff_opts.old_prefix);
45
+ git__free((char *)patch->base.diff_opts.new_prefix);
46
+
47
+ if (patch->flags & GIT_PATCH_GENERATED_ALLOCATED)
48
+ git__free(patch);
49
+ }
50
+
51
+ static void patch_generated_update_binary(git_patch_generated *patch)
52
+ {
53
+ if ((patch->base.delta->flags & DIFF_FLAGS_KNOWN_BINARY) != 0)
27
54
  return;
28
55
 
29
56
  if ((patch->ofile.file->flags & GIT_DIFF_FLAG_BINARY) != 0 ||
30
57
  (patch->nfile.file->flags & GIT_DIFF_FLAG_BINARY) != 0)
31
- patch->delta->flags |= GIT_DIFF_FLAG_BINARY;
58
+ patch->base.delta->flags |= GIT_DIFF_FLAG_BINARY;
32
59
 
33
60
  else if (patch->ofile.file->size > GIT_XDIFF_MAX_SIZE ||
34
- patch->nfile.file->size > GIT_XDIFF_MAX_SIZE)
35
- patch->delta->flags |= GIT_DIFF_FLAG_BINARY;
61
+ patch->nfile.file->size > GIT_XDIFF_MAX_SIZE)
62
+ patch->base.delta->flags |= GIT_DIFF_FLAG_BINARY;
36
63
 
37
64
  else if ((patch->ofile.file->flags & DIFF_FLAGS_NOT_BINARY) != 0 &&
38
- (patch->nfile.file->flags & DIFF_FLAGS_NOT_BINARY) != 0)
39
- patch->delta->flags |= GIT_DIFF_FLAG_NOT_BINARY;
65
+ (patch->nfile.file->flags & DIFF_FLAGS_NOT_BINARY) != 0)
66
+ patch->base.delta->flags |= GIT_DIFF_FLAG_NOT_BINARY;
40
67
  }
41
68
 
42
- static void diff_patch_init_common(git_patch *patch)
69
+ static void patch_generated_init_common(git_patch_generated *patch)
43
70
  {
44
- diff_patch_update_binary(patch);
71
+ patch->base.free_fn = patch_generated_free;
45
72
 
46
- patch->flags |= GIT_DIFF_PATCH_INITIALIZED;
73
+ patch_generated_update_binary(patch);
74
+
75
+ patch->flags |= GIT_PATCH_GENERATED_INITIALIZED;
47
76
 
48
77
  if (patch->diff)
49
78
  git_diff_addref(patch->diff);
50
79
  }
51
80
 
52
- static int diff_patch_normalize_options(
81
+ static int patch_generated_normalize_options(
53
82
  git_diff_options *out,
54
83
  const git_diff_options *opts)
55
84
  {
@@ -75,38 +104,40 @@ static int diff_patch_normalize_options(
75
104
  return 0;
76
105
  }
77
106
 
78
- static int diff_patch_init_from_diff(
79
- git_patch *patch, git_diff *diff, size_t delta_index)
107
+ static int patch_generated_init(
108
+ git_patch_generated *patch, git_diff *diff, size_t delta_index)
80
109
  {
81
110
  int error = 0;
82
111
 
83
112
  memset(patch, 0, sizeof(*patch));
84
- patch->diff = diff;
85
- patch->delta = git_vector_get(&diff->deltas, delta_index);
113
+
114
+ patch->diff = diff;
115
+ patch->base.repo = diff->repo;
116
+ patch->base.delta = git_vector_get(&diff->deltas, delta_index);
86
117
  patch->delta_index = delta_index;
87
118
 
88
- if ((error = diff_patch_normalize_options(
89
- &patch->diff_opts, &diff->opts)) < 0 ||
119
+ if ((error = patch_generated_normalize_options(
120
+ &patch->base.diff_opts, &diff->opts)) < 0 ||
90
121
  (error = git_diff_file_content__init_from_diff(
91
- &patch->ofile, diff, patch->delta, true)) < 0 ||
122
+ &patch->ofile, diff, patch->base.delta, true)) < 0 ||
92
123
  (error = git_diff_file_content__init_from_diff(
93
- &patch->nfile, diff, patch->delta, false)) < 0)
124
+ &patch->nfile, diff, patch->base.delta, false)) < 0)
94
125
  return error;
95
126
 
96
- diff_patch_init_common(patch);
127
+ patch_generated_init_common(patch);
97
128
 
98
129
  return 0;
99
130
  }
100
131
 
101
- static int diff_patch_alloc_from_diff(
102
- git_patch **out, git_diff *diff, size_t delta_index)
132
+ static int patch_generated_alloc_from_diff(
133
+ git_patch_generated **out, git_diff *diff, size_t delta_index)
103
134
  {
104
135
  int error;
105
- git_patch *patch = git__calloc(1, sizeof(git_patch));
136
+ git_patch_generated *patch = git__calloc(1, sizeof(git_patch_generated));
106
137
  GITERR_CHECK_ALLOC(patch);
107
138
 
108
- if (!(error = diff_patch_init_from_diff(patch, diff, delta_index))) {
109
- patch->flags |= GIT_DIFF_PATCH_ALLOCATED;
139
+ if (!(error = patch_generated_init(patch, diff, delta_index))) {
140
+ patch->flags |= GIT_PATCH_GENERATED_ALLOCATED;
110
141
  GIT_REFCOUNT_INC(patch);
111
142
  } else {
112
143
  git__free(patch);
@@ -117,27 +148,27 @@ static int diff_patch_alloc_from_diff(
117
148
  return error;
118
149
  }
119
150
 
120
- GIT_INLINE(bool) should_skip_binary(git_patch *patch, git_diff_file *file)
151
+ GIT_INLINE(bool) should_skip_binary(git_patch_generated *patch, git_diff_file *file)
121
152
  {
122
- if ((patch->diff_opts.flags & GIT_DIFF_SHOW_BINARY) != 0)
153
+ if ((patch->base.diff_opts.flags & GIT_DIFF_SHOW_BINARY) != 0)
123
154
  return false;
124
155
 
125
156
  return (file->flags & GIT_DIFF_FLAG_BINARY) != 0;
126
157
  }
127
158
 
128
- static bool diff_patch_diffable(git_patch *patch)
159
+ static bool patch_generated_diffable(git_patch_generated *patch)
129
160
  {
130
161
  size_t olen, nlen;
131
162
 
132
- if (patch->delta->status == GIT_DELTA_UNMODIFIED)
163
+ if (patch->base.delta->status == GIT_DELTA_UNMODIFIED)
133
164
  return false;
134
165
 
135
166
  /* if we've determined this to be binary (and we are not showing binary
136
167
  * data) then we have skipped loading the map data. instead, query the
137
168
  * file data itself.
138
169
  */
139
- if ((patch->delta->flags & GIT_DIFF_FLAG_BINARY) != 0 &&
140
- (patch->diff_opts.flags & GIT_DIFF_SHOW_BINARY) == 0) {
170
+ if ((patch->base.delta->flags & GIT_DIFF_FLAG_BINARY) != 0 &&
171
+ (patch->base.diff_opts.flags & GIT_DIFF_SHOW_BINARY) == 0) {
141
172
  olen = (size_t)patch->ofile.file->size;
142
173
  nlen = (size_t)patch->nfile.file->size;
143
174
  } else {
@@ -154,12 +185,12 @@ static bool diff_patch_diffable(git_patch *patch)
154
185
  !git_oid_equal(&patch->ofile.file->id, &patch->nfile.file->id));
155
186
  }
156
187
 
157
- static int diff_patch_load(git_patch *patch, git_diff_output *output)
188
+ static int patch_generated_load(git_patch_generated *patch, git_patch_generated_output *output)
158
189
  {
159
190
  int error = 0;
160
191
  bool incomplete_data;
161
192
 
162
- if ((patch->flags & GIT_DIFF_PATCH_LOADED) != 0)
193
+ if ((patch->flags & GIT_PATCH_GENERATED_LOADED) != 0)
163
194
  return 0;
164
195
 
165
196
  /* if no hunk and data callbacks and user doesn't care if data looks
@@ -180,13 +211,13 @@ static int diff_patch_load(git_patch *patch, git_diff_output *output)
180
211
  */
181
212
  if (patch->ofile.src == GIT_ITERATOR_TYPE_WORKDIR) {
182
213
  if ((error = git_diff_file_content__load(
183
- &patch->ofile, &patch->diff_opts)) < 0 ||
214
+ &patch->ofile, &patch->base.diff_opts)) < 0 ||
184
215
  should_skip_binary(patch, patch->ofile.file))
185
216
  goto cleanup;
186
217
  }
187
218
  if (patch->nfile.src == GIT_ITERATOR_TYPE_WORKDIR) {
188
219
  if ((error = git_diff_file_content__load(
189
- &patch->nfile, &patch->diff_opts)) < 0 ||
220
+ &patch->nfile, &patch->base.diff_opts)) < 0 ||
190
221
  should_skip_binary(patch, patch->nfile.file))
191
222
  goto cleanup;
192
223
  }
@@ -194,13 +225,13 @@ static int diff_patch_load(git_patch *patch, git_diff_output *output)
194
225
  /* once workdir has been tried, load other data as needed */
195
226
  if (patch->ofile.src != GIT_ITERATOR_TYPE_WORKDIR) {
196
227
  if ((error = git_diff_file_content__load(
197
- &patch->ofile, &patch->diff_opts)) < 0 ||
228
+ &patch->ofile, &patch->base.diff_opts)) < 0 ||
198
229
  should_skip_binary(patch, patch->ofile.file))
199
230
  goto cleanup;
200
231
  }
201
232
  if (patch->nfile.src != GIT_ITERATOR_TYPE_WORKDIR) {
202
233
  if ((error = git_diff_file_content__load(
203
- &patch->nfile, &patch->diff_opts)) < 0 ||
234
+ &patch->nfile, &patch->base.diff_opts)) < 0 ||
204
235
  should_skip_binary(patch, patch->nfile.file))
205
236
  goto cleanup;
206
237
  }
@@ -212,24 +243,24 @@ static int diff_patch_load(git_patch *patch, git_diff_output *output)
212
243
  patch->ofile.file->mode == patch->nfile.file->mode &&
213
244
  patch->ofile.file->mode != GIT_FILEMODE_COMMIT &&
214
245
  git_oid_equal(&patch->ofile.file->id, &patch->nfile.file->id) &&
215
- patch->delta->status == GIT_DELTA_MODIFIED) /* not RENAMED/COPIED! */
216
- patch->delta->status = GIT_DELTA_UNMODIFIED;
246
+ patch->base.delta->status == GIT_DELTA_MODIFIED) /* not RENAMED/COPIED! */
247
+ patch->base.delta->status = GIT_DELTA_UNMODIFIED;
217
248
 
218
249
  cleanup:
219
- diff_patch_update_binary(patch);
250
+ patch_generated_update_binary(patch);
220
251
 
221
252
  if (!error) {
222
- if (diff_patch_diffable(patch))
223
- patch->flags |= GIT_DIFF_PATCH_DIFFABLE;
253
+ if (patch_generated_diffable(patch))
254
+ patch->flags |= GIT_PATCH_GENERATED_DIFFABLE;
224
255
 
225
- patch->flags |= GIT_DIFF_PATCH_LOADED;
256
+ patch->flags |= GIT_PATCH_GENERATED_LOADED;
226
257
  }
227
258
 
228
259
  return error;
229
260
  }
230
261
 
231
- static int diff_patch_invoke_file_callback(
232
- git_patch *patch, git_diff_output *output)
262
+ static int patch_generated_invoke_file_callback(
263
+ git_patch_generated *patch, git_patch_generated_output *output)
233
264
  {
234
265
  float progress = patch->diff ?
235
266
  ((float)patch->delta_index / patch->diff->deltas.length) : 1.0f;
@@ -238,7 +269,7 @@ static int diff_patch_invoke_file_callback(
238
269
  return 0;
239
270
 
240
271
  return giterr_set_after_callback_function(
241
- output->file_cb(patch->delta, progress, output->payload),
272
+ output->file_cb(patch->base.delta, progress, output->payload),
242
273
  "git_patch");
243
274
  }
244
275
 
@@ -253,7 +284,7 @@ static int create_binary(
253
284
  size_t b_datalen)
254
285
  {
255
286
  git_buf deflate = GIT_BUF_INIT, delta = GIT_BUF_INIT;
256
- unsigned long delta_data_len;
287
+ size_t delta_data_len;
257
288
  int error;
258
289
 
259
290
  /* The git_delta function accepts unsigned long only */
@@ -270,20 +301,24 @@ static int create_binary(
270
301
  }
271
302
 
272
303
  if (a_datalen && b_datalen) {
273
- void *delta_data = git_delta(
274
- a_data, (unsigned long)a_datalen,
275
- b_data, (unsigned long)b_datalen,
276
- &delta_data_len, (unsigned long)deflate.size);
304
+ void *delta_data;
277
305
 
278
- if (delta_data) {
306
+ error = git_delta(&delta_data, &delta_data_len,
307
+ a_data, a_datalen,
308
+ b_data, b_datalen,
309
+ deflate.size);
310
+
311
+ if (error == 0) {
279
312
  error = git_zstream_deflatebuf(
280
- &delta, delta_data, (size_t)delta_data_len);
313
+ &delta, delta_data, delta_data_len);
281
314
 
282
315
  git__free(delta_data);
283
-
284
- if (error < 0)
285
- goto done;
316
+ } else if (error == GIT_EBUFS) {
317
+ error = 0;
286
318
  }
319
+
320
+ if (error < 0)
321
+ goto done;
287
322
  }
288
323
 
289
324
  if (delta.size && delta.size < deflate.size) {
@@ -305,7 +340,7 @@ done:
305
340
  return error;
306
341
  }
307
342
 
308
- static int diff_binary(git_diff_output *output, git_patch *patch)
343
+ static int diff_binary(git_patch_generated_output *output, git_patch_generated *patch)
309
344
  {
310
345
  git_diff_binary binary = {{0}};
311
346
  const char *old_data = patch->ofile.map.data;
@@ -330,7 +365,7 @@ static int diff_binary(git_diff_output *output, git_patch *patch)
330
365
  return error;
331
366
 
332
367
  error = giterr_set_after_callback_function(
333
- output->binary_cb(patch->delta, &binary, output->payload),
368
+ output->binary_cb(patch->base.delta, &binary, output->payload),
334
369
  "git_patch");
335
370
 
336
371
  git__free((char *) binary.old_file.data);
@@ -339,25 +374,27 @@ static int diff_binary(git_diff_output *output, git_patch *patch)
339
374
  return error;
340
375
  }
341
376
 
342
- static int diff_patch_generate(git_patch *patch, git_diff_output *output)
377
+ static int patch_generated_create(
378
+ git_patch_generated *patch,
379
+ git_patch_generated_output *output)
343
380
  {
344
381
  int error = 0;
345
382
 
346
- if ((patch->flags & GIT_DIFF_PATCH_DIFFED) != 0)
383
+ if ((patch->flags & GIT_PATCH_GENERATED_DIFFED) != 0)
347
384
  return 0;
348
385
 
349
386
  /* if we are not looking at the binary or text data, don't do the diff */
350
387
  if (!output->binary_cb && !output->hunk_cb && !output->data_cb)
351
388
  return 0;
352
389
 
353
- if ((patch->flags & GIT_DIFF_PATCH_LOADED) == 0 &&
354
- (error = diff_patch_load(patch, output)) < 0)
390
+ if ((patch->flags & GIT_PATCH_GENERATED_LOADED) == 0 &&
391
+ (error = patch_generated_load(patch, output)) < 0)
355
392
  return error;
356
393
 
357
- if ((patch->flags & GIT_DIFF_PATCH_DIFFABLE) == 0)
394
+ if ((patch->flags & GIT_PATCH_GENERATED_DIFFABLE) == 0)
358
395
  return 0;
359
396
 
360
- if ((patch->delta->flags & GIT_DIFF_FLAG_BINARY) != 0) {
397
+ if ((patch->base.delta->flags & GIT_DIFF_FLAG_BINARY) != 0) {
361
398
  if (output->binary_cb)
362
399
  error = diff_binary(output, patch);
363
400
  }
@@ -366,33 +403,10 @@ static int diff_patch_generate(git_patch *patch, git_diff_output *output)
366
403
  error = output->diff_cb(output, patch);
367
404
  }
368
405
 
369
- patch->flags |= GIT_DIFF_PATCH_DIFFED;
406
+ patch->flags |= GIT_PATCH_GENERATED_DIFFED;
370
407
  return error;
371
408
  }
372
409
 
373
- static void diff_patch_free(git_patch *patch)
374
- {
375
- git_diff_file_content__clear(&patch->ofile);
376
- git_diff_file_content__clear(&patch->nfile);
377
-
378
- git_array_clear(patch->lines);
379
- git_array_clear(patch->hunks);
380
-
381
- git_diff_free(patch->diff); /* decrements refcount */
382
- patch->diff = NULL;
383
-
384
- git_pool_clear(&patch->flattened);
385
-
386
- git__free((char *)patch->diff_opts.old_prefix);
387
- git__free((char *)patch->diff_opts.new_prefix);
388
-
389
- git__free((char *)patch->binary.old_file.data);
390
- git__free((char *)patch->binary.new_file.data);
391
-
392
- if (patch->flags & GIT_DIFF_PATCH_ALLOCATED)
393
- git__free(patch);
394
- }
395
-
396
410
  static int diff_required(git_diff *diff, const char *action)
397
411
  {
398
412
  if (diff)
@@ -412,7 +426,7 @@ int git_diff_foreach(
412
426
  int error = 0;
413
427
  git_xdiff_output xo;
414
428
  size_t idx;
415
- git_patch patch;
429
+ git_patch_generated patch;
416
430
 
417
431
  if ((error = diff_required(diff, "git_diff_foreach")) < 0)
418
432
  return error;
@@ -423,24 +437,24 @@ int git_diff_foreach(
423
437
  &xo.output, &diff->opts, file_cb, binary_cb, hunk_cb, data_cb, payload);
424
438
  git_xdiff_init(&xo, &diff->opts);
425
439
 
426
- git_vector_foreach(&diff->deltas, idx, patch.delta) {
440
+ git_vector_foreach(&diff->deltas, idx, patch.base.delta) {
427
441
 
428
442
  /* check flags against patch status */
429
- if (git_diff_delta__should_skip(&diff->opts, patch.delta))
443
+ if (git_diff_delta__should_skip(&diff->opts, patch.base.delta))
430
444
  continue;
431
445
 
432
446
  if (binary_cb || hunk_cb || data_cb) {
433
- if ((error = diff_patch_init_from_diff(&patch, diff, idx)) != 0 ||
434
- (error = diff_patch_load(&patch, &xo.output)) != 0)
447
+ if ((error = patch_generated_init(&patch, diff, idx)) != 0 ||
448
+ (error = patch_generated_load(&patch, &xo.output)) != 0)
435
449
  return error;
436
450
  }
437
451
 
438
- if ((error = diff_patch_invoke_file_callback(&patch, &xo.output)) == 0) {
452
+ if ((error = patch_generated_invoke_file_callback(&patch, &xo.output)) == 0) {
439
453
  if (binary_cb || hunk_cb || data_cb)
440
- error = diff_patch_generate(&patch, &xo.output);
454
+ error = patch_generated_create(&patch, &xo.output);
441
455
  }
442
456
 
443
- git_patch_free(&patch);
457
+ git_patch_free(&patch.base);
444
458
 
445
459
  if (error)
446
460
  break;
@@ -450,15 +464,15 @@ int git_diff_foreach(
450
464
  }
451
465
 
452
466
  typedef struct {
453
- git_patch patch;
467
+ git_patch_generated patch;
454
468
  git_diff_delta delta;
455
469
  char paths[GIT_FLEX_ARRAY];
456
- } diff_patch_with_delta;
470
+ } patch_generated_with_delta;
457
471
 
458
- static int diff_single_generate(diff_patch_with_delta *pd, git_xdiff_output *xo)
472
+ static int diff_single_generate(patch_generated_with_delta *pd, git_xdiff_output *xo)
459
473
  {
460
474
  int error = 0;
461
- git_patch *patch = &pd->patch;
475
+ git_patch_generated *patch = &pd->patch;
462
476
  bool has_old = ((patch->ofile.flags & GIT_DIFF_FLAG__NO_DATA) == 0);
463
477
  bool has_new = ((patch->nfile.flags & GIT_DIFF_FLAG__NO_DATA) == 0);
464
478
 
@@ -469,24 +483,24 @@ static int diff_single_generate(diff_patch_with_delta *pd, git_xdiff_output *xo)
469
483
  if (git_oid_equal(&patch->nfile.file->id, &patch->ofile.file->id))
470
484
  pd->delta.status = GIT_DELTA_UNMODIFIED;
471
485
 
472
- patch->delta = &pd->delta;
486
+ patch->base.delta = &pd->delta;
473
487
 
474
- diff_patch_init_common(patch);
488
+ patch_generated_init_common(patch);
475
489
 
476
490
  if (pd->delta.status == GIT_DELTA_UNMODIFIED &&
477
491
  !(patch->ofile.opts_flags & GIT_DIFF_INCLUDE_UNMODIFIED))
478
492
  return error;
479
493
 
480
- error = diff_patch_invoke_file_callback(patch, (git_diff_output *)xo);
494
+ error = patch_generated_invoke_file_callback(patch, (git_patch_generated_output *)xo);
481
495
 
482
496
  if (!error)
483
- error = diff_patch_generate(patch, (git_diff_output *)xo);
497
+ error = patch_generated_create(patch, (git_patch_generated_output *)xo);
484
498
 
485
499
  return error;
486
500
  }
487
501
 
488
- static int diff_patch_from_sources(
489
- diff_patch_with_delta *pd,
502
+ static int patch_generated_from_sources(
503
+ patch_generated_with_delta *pd,
490
504
  git_xdiff_output *xo,
491
505
  git_diff_file_content_src *oldsrc,
492
506
  git_diff_file_content_src *newsrc,
@@ -499,7 +513,7 @@ static int diff_patch_from_sources(
499
513
  git_diff_file *lfile = &pd->delta.old_file, *rfile = &pd->delta.new_file;
500
514
  git_diff_file_content *ldata = &pd->patch.ofile, *rdata = &pd->patch.nfile;
501
515
 
502
- if ((error = diff_patch_normalize_options(&pd->patch.diff_opts, opts)) < 0)
516
+ if ((error = patch_generated_normalize_options(&pd->patch.base.diff_opts, opts)) < 0)
503
517
  return error;
504
518
 
505
519
  if (opts && (opts->flags & GIT_DIFF_REVERSE) != 0) {
@@ -507,7 +521,7 @@ static int diff_patch_from_sources(
507
521
  tmp = ldata; ldata = rdata; rdata = tmp;
508
522
  }
509
523
 
510
- pd->patch.delta = &pd->delta;
524
+ pd->patch.base.delta = &pd->delta;
511
525
 
512
526
  if (!oldsrc->as_path) {
513
527
  if (newsrc->as_path)
@@ -530,12 +544,12 @@ static int diff_patch_from_sources(
530
544
  return diff_single_generate(pd, xo);
531
545
  }
532
546
 
533
- static int diff_patch_with_delta_alloc(
534
- diff_patch_with_delta **out,
547
+ static int patch_generated_with_delta_alloc(
548
+ patch_generated_with_delta **out,
535
549
  const char **old_path,
536
550
  const char **new_path)
537
551
  {
538
- diff_patch_with_delta *pd;
552
+ patch_generated_with_delta *pd;
539
553
  size_t old_len = *old_path ? strlen(*old_path) : 0;
540
554
  size_t new_len = *new_path ? strlen(*new_path) : 0;
541
555
  size_t alloc_len;
@@ -547,7 +561,7 @@ static int diff_patch_with_delta_alloc(
547
561
  *out = pd = git__calloc(1, alloc_len);
548
562
  GITERR_CHECK_ALLOC(pd);
549
563
 
550
- pd->patch.flags = GIT_DIFF_PATCH_ALLOCATED;
564
+ pd->patch.flags = GIT_PATCH_GENERATED_ALLOCATED;
551
565
 
552
566
  if (*old_path) {
553
567
  memcpy(&pd->paths[0], *old_path, old_len);
@@ -575,7 +589,7 @@ static int diff_from_sources(
575
589
  void *payload)
576
590
  {
577
591
  int error = 0;
578
- diff_patch_with_delta pd;
592
+ patch_generated_with_delta pd;
579
593
  git_xdiff_output xo;
580
594
 
581
595
  memset(&xo, 0, sizeof(xo));
@@ -585,9 +599,9 @@ static int diff_from_sources(
585
599
 
586
600
  memset(&pd, 0, sizeof(pd));
587
601
 
588
- error = diff_patch_from_sources(&pd, &xo, oldsrc, newsrc, opts);
602
+ error = patch_generated_from_sources(&pd, &xo, oldsrc, newsrc, opts);
589
603
 
590
- git_patch_free(&pd.patch);
604
+ git_patch_free(&pd.patch.base);
591
605
 
592
606
  return error;
593
607
  }
@@ -599,13 +613,13 @@ static int patch_from_sources(
599
613
  const git_diff_options *opts)
600
614
  {
601
615
  int error = 0;
602
- diff_patch_with_delta *pd;
616
+ patch_generated_with_delta *pd;
603
617
  git_xdiff_output xo;
604
618
 
605
619
  assert(out);
606
620
  *out = NULL;
607
621
 
608
- if ((error = diff_patch_with_delta_alloc(
622
+ if ((error = patch_generated_with_delta_alloc(
609
623
  &pd, &oldsrc->as_path, &newsrc->as_path)) < 0)
610
624
  return error;
611
625
 
@@ -613,7 +627,7 @@ static int patch_from_sources(
613
627
  diff_output_to_patch(&xo.output, &pd->patch);
614
628
  git_xdiff_init(&xo, opts);
615
629
 
616
- if (!(error = diff_patch_from_sources(pd, &xo, oldsrc, newsrc, opts)))
630
+ if (!(error = patch_generated_from_sources(pd, &xo, oldsrc, newsrc, opts)))
617
631
  *out = (git_patch *)pd;
618
632
  else
619
633
  git_patch_free((git_patch *)pd);
@@ -738,7 +752,7 @@ int git_patch_from_diff(
738
752
  int error = 0;
739
753
  git_xdiff_output xo;
740
754
  git_diff_delta *delta = NULL;
741
- git_patch *patch = NULL;
755
+ git_patch_generated *patch = NULL;
742
756
 
743
757
  if (patch_ptr) *patch_ptr = NULL;
744
758
 
@@ -760,17 +774,17 @@ int git_patch_from_diff(
760
774
  (diff->opts.flags & GIT_DIFF_SKIP_BINARY_CHECK) != 0))
761
775
  return 0;
762
776
 
763
- if ((error = diff_patch_alloc_from_diff(&patch, diff, idx)) < 0)
777
+ if ((error = patch_generated_alloc_from_diff(&patch, diff, idx)) < 0)
764
778
  return error;
765
779
 
766
780
  memset(&xo, 0, sizeof(xo));
767
781
  diff_output_to_patch(&xo.output, patch);
768
782
  git_xdiff_init(&xo, &diff->opts);
769
783
 
770
- error = diff_patch_invoke_file_callback(patch, &xo.output);
784
+ error = patch_generated_invoke_file_callback(patch, &xo.output);
771
785
 
772
786
  if (!error)
773
- error = diff_patch_generate(patch, &xo.output);
787
+ error = patch_generated_create(patch, &xo.output);
774
788
 
775
789
  if (!error) {
776
790
  /* TODO: if cumulative diff size is < 0.5 total size, flatten patch */
@@ -778,237 +792,34 @@ int git_patch_from_diff(
778
792
  }
779
793
 
780
794
  if (error || !patch_ptr)
781
- git_patch_free(patch);
795
+ git_patch_free(&patch->base);
782
796
  else
783
- *patch_ptr = patch;
797
+ *patch_ptr = &patch->base;
784
798
 
785
799
  return error;
786
800
  }
787
801
 
788
- void git_patch_free(git_patch *patch)
789
- {
790
- if (patch)
791
- GIT_REFCOUNT_DEC(patch, diff_patch_free);
792
- }
793
-
794
- const git_diff_delta *git_patch_get_delta(const git_patch *patch)
795
- {
796
- assert(patch);
797
- return patch->delta;
798
- }
799
-
800
- size_t git_patch_num_hunks(const git_patch *patch)
801
- {
802
- assert(patch);
803
- return git_array_size(patch->hunks);
804
- }
805
-
806
- int git_patch_line_stats(
807
- size_t *total_ctxt,
808
- size_t *total_adds,
809
- size_t *total_dels,
810
- const git_patch *patch)
811
- {
812
- size_t totals[3], idx;
813
-
814
- memset(totals, 0, sizeof(totals));
815
-
816
- for (idx = 0; idx < git_array_size(patch->lines); ++idx) {
817
- git_diff_line *line = git_array_get(patch->lines, idx);
818
- if (!line)
819
- continue;
820
-
821
- switch (line->origin) {
822
- case GIT_DIFF_LINE_CONTEXT: totals[0]++; break;
823
- case GIT_DIFF_LINE_ADDITION: totals[1]++; break;
824
- case GIT_DIFF_LINE_DELETION: totals[2]++; break;
825
- default:
826
- /* diff --stat and --numstat don't count EOFNL marks because
827
- * they will always be paired with a ADDITION or DELETION line.
828
- */
829
- break;
830
- }
831
- }
832
-
833
- if (total_ctxt)
834
- *total_ctxt = totals[0];
835
- if (total_adds)
836
- *total_adds = totals[1];
837
- if (total_dels)
838
- *total_dels = totals[2];
839
-
840
- return 0;
841
- }
842
-
843
- static int diff_error_outofrange(const char *thing)
844
- {
845
- giterr_set(GITERR_INVALID, "Diff patch %s index out of range", thing);
846
- return GIT_ENOTFOUND;
847
- }
848
-
849
- int git_patch_get_hunk(
850
- const git_diff_hunk **out,
851
- size_t *lines_in_hunk,
852
- git_patch *patch,
853
- size_t hunk_idx)
854
- {
855
- diff_patch_hunk *hunk;
856
- assert(patch);
857
-
858
- hunk = git_array_get(patch->hunks, hunk_idx);
859
-
860
- if (!hunk) {
861
- if (out) *out = NULL;
862
- if (lines_in_hunk) *lines_in_hunk = 0;
863
- return diff_error_outofrange("hunk");
864
- }
865
-
866
- if (out) *out = &hunk->hunk;
867
- if (lines_in_hunk) *lines_in_hunk = hunk->line_count;
868
- return 0;
869
- }
870
-
871
- int git_patch_num_lines_in_hunk(const git_patch *patch, size_t hunk_idx)
872
- {
873
- diff_patch_hunk *hunk;
874
- assert(patch);
875
-
876
- if (!(hunk = git_array_get(patch->hunks, hunk_idx)))
877
- return diff_error_outofrange("hunk");
878
- return (int)hunk->line_count;
879
- }
880
-
881
- int git_patch_get_line_in_hunk(
882
- const git_diff_line **out,
883
- git_patch *patch,
884
- size_t hunk_idx,
885
- size_t line_of_hunk)
886
- {
887
- diff_patch_hunk *hunk;
888
- git_diff_line *line;
889
-
890
- assert(patch);
891
-
892
- if (!(hunk = git_array_get(patch->hunks, hunk_idx))) {
893
- if (out) *out = NULL;
894
- return diff_error_outofrange("hunk");
895
- }
896
-
897
- if (line_of_hunk >= hunk->line_count ||
898
- !(line = git_array_get(
899
- patch->lines, hunk->line_start + line_of_hunk))) {
900
- if (out) *out = NULL;
901
- return diff_error_outofrange("line");
902
- }
903
-
904
- if (out) *out = line;
905
- return 0;
906
- }
907
-
908
- size_t git_patch_size(
909
- git_patch *patch,
910
- int include_context,
911
- int include_hunk_headers,
912
- int include_file_headers)
913
- {
914
- size_t out;
915
-
916
- assert(patch);
917
-
918
- out = patch->content_size;
919
-
920
- if (!include_context)
921
- out -= patch->context_size;
922
-
923
- if (include_hunk_headers)
924
- out += patch->header_size;
925
-
926
- if (include_file_headers) {
927
- git_buf file_header = GIT_BUF_INIT;
928
-
929
- if (git_diff_delta__format_file_header(
930
- &file_header, patch->delta, NULL, NULL, 0) < 0)
931
- giterr_clear();
932
- else
933
- out += git_buf_len(&file_header);
934
-
935
- git_buf_free(&file_header);
936
- }
937
-
938
- return out;
939
- }
940
-
941
- git_diff *git_patch__diff(git_patch *patch)
942
- {
943
- return patch->diff;
944
- }
945
-
946
- git_diff_driver *git_patch__driver(git_patch *patch)
802
+ git_diff_driver *git_patch_generated_driver(git_patch_generated *patch)
947
803
  {
948
804
  /* ofile driver is representative for whole patch */
949
805
  return patch->ofile.driver;
950
806
  }
951
807
 
952
- void git_patch__old_data(
953
- char **ptr, size_t *len, git_patch *patch)
808
+ void git_patch_generated_old_data(
809
+ char **ptr, size_t *len, git_patch_generated *patch)
954
810
  {
955
811
  *ptr = patch->ofile.map.data;
956
812
  *len = patch->ofile.map.len;
957
813
  }
958
814
 
959
- void git_patch__new_data(
960
- char **ptr, size_t *len, git_patch *patch)
815
+ void git_patch_generated_new_data(
816
+ char **ptr, size_t *len, git_patch_generated *patch)
961
817
  {
962
818
  *ptr = patch->nfile.map.data;
963
819
  *len = patch->nfile.map.len;
964
820
  }
965
821
 
966
- int git_patch__invoke_callbacks(
967
- git_patch *patch,
968
- git_diff_file_cb file_cb,
969
- git_diff_binary_cb binary_cb,
970
- git_diff_hunk_cb hunk_cb,
971
- git_diff_line_cb line_cb,
972
- void *payload)
973
- {
974
- int error = 0;
975
- uint32_t i, j;
976
-
977
- if (file_cb)
978
- error = file_cb(patch->delta, 0, payload);
979
-
980
- if ((patch->delta->flags & GIT_DIFF_FLAG_BINARY) != 0) {
981
- if (binary_cb)
982
- error = binary_cb(patch->delta, &patch->binary, payload);
983
-
984
- return error;
985
- }
986
-
987
- if (!hunk_cb && !line_cb)
988
- return error;
989
-
990
- for (i = 0; !error && i < git_array_size(patch->hunks); ++i) {
991
- diff_patch_hunk *h = git_array_get(patch->hunks, i);
992
-
993
- if (hunk_cb)
994
- error = hunk_cb(patch->delta, &h->hunk, payload);
995
-
996
- if (!line_cb)
997
- continue;
998
-
999
- for (j = 0; !error && j < h->line_count; ++j) {
1000
- git_diff_line *l =
1001
- git_array_get(patch->lines, h->line_start + j);
1002
-
1003
- error = line_cb(patch->delta, &h->hunk, l, payload);
1004
- }
1005
- }
1006
-
1007
- return error;
1008
- }
1009
-
1010
-
1011
- static int diff_patch_file_cb(
822
+ static int patch_generated_file_cb(
1012
823
  const git_diff_delta *delta,
1013
824
  float progress,
1014
825
  void *payload)
@@ -1017,7 +828,7 @@ static int diff_patch_file_cb(
1017
828
  return 0;
1018
829
  }
1019
830
 
1020
- static int diff_patch_binary_cb(
831
+ static int patch_generated_binary_cb(
1021
832
  const git_diff_delta *delta,
1022
833
  const git_diff_binary *binary,
1023
834
  void *payload)
@@ -1047,62 +858,62 @@ static int diff_patch_binary_cb(
1047
858
  return 0;
1048
859
  }
1049
860
 
1050
- static int diff_patch_hunk_cb(
861
+ static int git_patch_hunk_cb(
1051
862
  const git_diff_delta *delta,
1052
863
  const git_diff_hunk *hunk_,
1053
864
  void *payload)
1054
865
  {
1055
- git_patch *patch = payload;
1056
- diff_patch_hunk *hunk;
866
+ git_patch_generated *patch = payload;
867
+ git_patch_hunk *hunk;
1057
868
 
1058
869
  GIT_UNUSED(delta);
1059
870
 
1060
- hunk = git_array_alloc(patch->hunks);
871
+ hunk = git_array_alloc(patch->base.hunks);
1061
872
  GITERR_CHECK_ALLOC(hunk);
1062
873
 
1063
874
  memcpy(&hunk->hunk, hunk_, sizeof(hunk->hunk));
1064
875
 
1065
- patch->header_size += hunk_->header_len;
876
+ patch->base.header_size += hunk_->header_len;
1066
877
 
1067
- hunk->line_start = git_array_size(patch->lines);
878
+ hunk->line_start = git_array_size(patch->base.lines);
1068
879
  hunk->line_count = 0;
1069
880
 
1070
881
  return 0;
1071
882
  }
1072
883
 
1073
- static int diff_patch_line_cb(
884
+ static int patch_generated_line_cb(
1074
885
  const git_diff_delta *delta,
1075
886
  const git_diff_hunk *hunk_,
1076
887
  const git_diff_line *line_,
1077
888
  void *payload)
1078
889
  {
1079
- git_patch *patch = payload;
1080
- diff_patch_hunk *hunk;
890
+ git_patch_generated *patch = payload;
891
+ git_patch_hunk *hunk;
1081
892
  git_diff_line *line;
1082
893
 
1083
894
  GIT_UNUSED(delta);
1084
895
  GIT_UNUSED(hunk_);
1085
896
 
1086
- hunk = git_array_last(patch->hunks);
897
+ hunk = git_array_last(patch->base.hunks);
1087
898
  assert(hunk); /* programmer error if no hunk is available */
1088
899
 
1089
- line = git_array_alloc(patch->lines);
900
+ line = git_array_alloc(patch->base.lines);
1090
901
  GITERR_CHECK_ALLOC(line);
1091
902
 
1092
903
  memcpy(line, line_, sizeof(*line));
1093
904
 
1094
905
  /* do some bookkeeping so we can provide old/new line numbers */
1095
906
 
1096
- patch->content_size += line->content_len;
907
+ patch->base.content_size += line->content_len;
1097
908
 
1098
909
  if (line->origin == GIT_DIFF_LINE_ADDITION ||
1099
910
  line->origin == GIT_DIFF_LINE_DELETION)
1100
- patch->content_size += 1;
911
+ patch->base.content_size += 1;
1101
912
  else if (line->origin == GIT_DIFF_LINE_CONTEXT) {
1102
- patch->content_size += 1;
1103
- patch->context_size += line->content_len + 1;
913
+ patch->base.content_size += 1;
914
+ patch->base.context_size += line->content_len + 1;
1104
915
  } else if (line->origin == GIT_DIFF_LINE_CONTEXT_EOFNL)
1105
- patch->context_size += line->content_len;
916
+ patch->base.context_size += line->content_len;
1106
917
 
1107
918
  hunk->line_count++;
1108
919
 
@@ -1110,7 +921,7 @@ static int diff_patch_line_cb(
1110
921
  }
1111
922
 
1112
923
  static void diff_output_init(
1113
- git_diff_output *out,
924
+ git_patch_generated_output *out,
1114
925
  const git_diff_options *opts,
1115
926
  git_diff_file_cb file_cb,
1116
927
  git_diff_binary_cb binary_cb,
@@ -1129,14 +940,15 @@ static void diff_output_init(
1129
940
  out->payload = payload;
1130
941
  }
1131
942
 
1132
- static void diff_output_to_patch(git_diff_output *out, git_patch *patch)
943
+ static void diff_output_to_patch(
944
+ git_patch_generated_output *out, git_patch_generated *patch)
1133
945
  {
1134
946
  diff_output_init(
1135
947
  out,
1136
948
  NULL,
1137
- diff_patch_file_cb,
1138
- diff_patch_binary_cb,
1139
- diff_patch_hunk_cb,
1140
- diff_patch_line_cb,
949
+ patch_generated_file_cb,
950
+ patch_generated_binary_cb,
951
+ git_patch_hunk_cb,
952
+ patch_generated_line_cb,
1141
953
  patch);
1142
954
  }