Commit 360f42f4b3f5de31270416220bd799b951202b2d

Russell Belfer 2013-06-12T14:18:09

Fix diff header naming issues This makes the git_diff_patch definition private to diff_patch.c and fixes a number of other header file naming inconsistencies to use `git_` prefixes on functions and structures that are shared between files.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
diff --git a/src/diff_file.c b/src/diff_file.c
index 5bdb9e4..4fd1177 100644
--- a/src/diff_file.c
+++ b/src/diff_file.c
@@ -85,7 +85,7 @@ static int diff_file_content_init_common(
 	return 0;
 }
 
-int diff_file_content_init_from_diff(
+int git_diff_file_content__init_from_diff(
 	git_diff_file_content *fc,
 	git_diff_list *diff,
 	size_t delta_index,
@@ -127,7 +127,7 @@ int diff_file_content_init_from_diff(
 	return diff_file_content_init_common(fc, &diff->opts);
 }
 
-int diff_file_content_init_from_blob(
+int git_diff_file_content__init_from_blob(
 	git_diff_file_content *fc,
 	git_repository *repo,
 	const git_diff_options *opts,
@@ -152,7 +152,7 @@ int diff_file_content_init_from_blob(
 	return diff_file_content_init_common(fc, opts);
 }
 
-int diff_file_content_init_from_raw(
+int git_diff_file_content__init_from_raw(
 	git_diff_file_content *fc,
 	git_repository *repo,
 	const git_diff_options *opts,
@@ -385,7 +385,7 @@ static int diff_file_content_load_workdir(git_diff_file_content *fc)
 	return error;
 }
 
-int diff_file_content_load(git_diff_file_content *fc)
+int git_diff_file_content__load(git_diff_file_content *fc)
 {
 	int error = 0;
 
@@ -409,7 +409,7 @@ int diff_file_content_load(git_diff_file_content *fc)
 	return 0;
 }
 
-void diff_file_content_unload(git_diff_file_content *fc)
+void git_diff_file_content__unload(git_diff_file_content *fc)
 {
 	if (fc->file.flags & GIT_DIFF_FLAG__FREE_DATA) {
 		git__free(fc->map.data);
@@ -433,9 +433,9 @@ void diff_file_content_unload(git_diff_file_content *fc)
 	fc->file.flags &= ~GIT_DIFF_FLAG__LOADED;
 }
 
-void diff_file_content_clear(git_diff_file_content *fc)
+void git_diff_file_content__clear(git_diff_file_content *fc)
 {
-	diff_file_content_unload(fc);
+	git_diff_file_content__unload(fc);
 
 	/* for now, nothing else to do */
 }
diff --git a/src/diff_file.h b/src/diff_file.h
index ab7b1dc..afad851 100644
--- a/src/diff_file.h
+++ b/src/diff_file.h
@@ -24,19 +24,19 @@ typedef struct {
 	git_map map;
 } git_diff_file_content;
 
-extern int diff_file_content_init_from_diff(
+extern int git_diff_file_content__init_from_diff(
 	git_diff_file_content *fc,
 	git_diff_list *diff,
 	size_t delta_index,
 	bool use_old);
 
-extern int diff_file_content_init_from_blob(
+extern int git_diff_file_content__init_from_blob(
 	git_diff_file_content *fc,
 	git_repository *repo,
 	const git_diff_options *opts,
 	const git_blob *blob);
 
-extern int diff_file_content_init_from_raw(
+extern int git_diff_file_content__init_from_raw(
 	git_diff_file_content *fc,
 	git_repository *repo,
 	const git_diff_options *opts,
@@ -44,12 +44,12 @@ extern int diff_file_content_init_from_raw(
 	size_t buflen);
 
 /* this loads the blob/file-on-disk as needed */
-extern int diff_file_content_load(git_diff_file_content *fc);
+extern int git_diff_file_content__load(git_diff_file_content *fc);
 
 /* this releases the blob/file-in-memory */
-extern void diff_file_content_unload(git_diff_file_content *fc);
+extern void git_diff_file_content__unload(git_diff_file_content *fc);
 
 /* this unloads and also releases any other resources */
-extern void diff_file_content_clear(git_diff_file_content *fc);
+extern void git_diff_file_content__clear(git_diff_file_content *fc);
 
 #endif
diff --git a/src/diff_patch.c b/src/diff_patch.c
index 05dee5e..a1e1fe8 100644
--- a/src/diff_patch.c
+++ b/src/diff_patch.c
@@ -11,6 +11,49 @@
 #include "diff_patch.h"
 #include "diff_xdiff.h"
 
+/* cached information about a single span in a diff */
+typedef struct diff_patch_line diff_patch_line;
+struct diff_patch_line {
+	const char *ptr;
+	size_t len;
+	size_t lines, oldno, newno;
+	char origin;
+};
+
+/* cached information about a hunk in a diff */
+typedef struct diff_patch_hunk diff_patch_hunk;
+struct diff_patch_hunk {
+	git_diff_range range;
+	char   header[128];
+	size_t header_len;
+	size_t line_start;
+	size_t line_count;
+};
+
+struct git_diff_patch {
+	git_refcount rc;
+	git_diff_list *diff; /* for refcount purposes, maybe NULL for blob diffs */
+	git_diff_delta *delta;
+	size_t delta_index;
+	git_diff_file_content ofile;
+	git_diff_file_content nfile;
+	uint32_t flags;
+	git_array_t(diff_patch_hunk) hunks;
+	git_array_t(diff_patch_line) lines;
+	size_t oldno, newno;
+	size_t content_size;
+	git_pool flattened;
+};
+
+enum {
+	GIT_DIFF_PATCH_ALLOCATED   = (1 << 0),
+	GIT_DIFF_PATCH_INITIALIZED = (1 << 1),
+	GIT_DIFF_PATCH_LOADED      = (1 << 2),
+	GIT_DIFF_PATCH_DIFFABLE    = (1 << 3),
+	GIT_DIFF_PATCH_DIFFED      = (1 << 4),
+	GIT_DIFF_PATCH_FLATTENED   = (1 << 5),
+};
+
 static void diff_output_init(git_diff_output*, const git_diff_options*,
 	git_diff_file_cb, git_diff_hunk_cb, git_diff_data_cb, void*);
 
@@ -53,9 +96,9 @@ static int diff_patch_init_from_diff(
 	patch->delta = git_vector_get(&diff->deltas, delta_index);
 	patch->delta_index = delta_index;
 
-	if ((error = diff_file_content_init_from_diff(
+	if ((error = git_diff_file_content__init_from_diff(
 			&patch->ofile, diff, delta_index, true)) < 0 ||
-		(error = diff_file_content_init_from_diff(
+		(error = git_diff_file_content__init_from_diff(
 			&patch->nfile, diff, delta_index, false)) < 0)
 		return error;
 
@@ -110,24 +153,24 @@ static int diff_patch_load(git_diff_patch *patch, git_diff_output *output)
 	 * need 2x data size and this minimizes peak memory footprint
 	 */
 	if (patch->ofile.src == GIT_ITERATOR_TYPE_WORKDIR) {
-		if ((error = diff_file_content_load(&patch->ofile)) < 0 ||
+		if ((error = git_diff_file_content__load(&patch->ofile)) < 0 ||
 			(patch->ofile.file.flags & GIT_DIFF_FLAG_BINARY) != 0)
 			goto cleanup;
 	}
 	if (patch->nfile.src == GIT_ITERATOR_TYPE_WORKDIR) {
-		if ((error = diff_file_content_load(&patch->nfile)) < 0 ||
+		if ((error = git_diff_file_content__load(&patch->nfile)) < 0 ||
 			(patch->nfile.file.flags & GIT_DIFF_FLAG_BINARY) != 0)
 			goto cleanup;
 	}
 
 	/* once workdir has been tried, load other data as needed */
 	if (patch->ofile.src != GIT_ITERATOR_TYPE_WORKDIR) {
-		if ((error = diff_file_content_load(&patch->ofile)) < 0 ||
+		if ((error = git_diff_file_content__load(&patch->ofile)) < 0 ||
 			(patch->ofile.file.flags & GIT_DIFF_FLAG_BINARY) != 0)
 			goto cleanup;
 	}
 	if (patch->nfile.src != GIT_ITERATOR_TYPE_WORKDIR) {
-		if ((error = diff_file_content_load(&patch->nfile)) < 0 ||
+		if ((error = git_diff_file_content__load(&patch->nfile)) < 0 ||
 			(patch->nfile.file.flags & GIT_DIFF_FLAG_BINARY) != 0)
 			goto cleanup;
 	}
@@ -198,8 +241,8 @@ static int diff_patch_generate(git_diff_patch *patch, git_diff_output *output)
 
 static void diff_patch_free(git_diff_patch *patch)
 {
-	diff_file_content_clear(&patch->ofile);
-	diff_file_content_clear(&patch->nfile);
+	git_diff_file_content__clear(&patch->ofile);
+	git_diff_file_content__clear(&patch->nfile);
 
 	git_array_clear(patch->lines);
 	git_array_clear(patch->hunks);
@@ -323,9 +366,9 @@ static int diff_patch_from_blobs(
 		new_blob = swap;
 	}
 
-	if ((error = diff_file_content_init_from_blob(
+	if ((error = git_diff_file_content__init_from_blob(
 			&pd->patch.ofile, repo, opts, old_blob)) < 0 ||
-		(error = diff_file_content_init_from_blob(
+		(error = git_diff_file_content__init_from_blob(
 			&pd->patch.nfile, repo, opts, new_blob)) < 0)
 		return error;
 
@@ -409,14 +452,14 @@ static int diff_patch_from_blob_and_buffer(
 		return 0;
 
 	if (opts && (opts->flags & GIT_DIFF_REVERSE) != 0) {
-		if (!(error = diff_file_content_init_from_raw(
+		if (!(error = git_diff_file_content__init_from_raw(
 				&pd->patch.ofile, repo, opts, buf, buflen)))
-			error = diff_file_content_init_from_blob(
+			error = git_diff_file_content__init_from_blob(
 				&pd->patch.nfile, repo, opts, old_blob);
 	} else {
-		if (!(error = diff_file_content_init_from_blob(
+		if (!(error = git_diff_file_content__init_from_blob(
 				&pd->patch.ofile, repo, opts, old_blob)))
-			error = diff_file_content_init_from_raw(
+			error = git_diff_file_content__init_from_raw(
 				&pd->patch.nfile, repo, opts, buf, buflen);
 	}
 
@@ -690,6 +733,68 @@ notfound:
 	return diff_error_outofrange(thing);
 }
 
+git_diff_list *git_diff_patch__diff(git_diff_patch *patch)
+{
+	return patch->diff;
+}
+
+git_diff_driver *git_diff_patch__driver(git_diff_patch *patch)
+{
+	/* ofile driver is representative for whole patch */
+	return patch->ofile.driver;
+}
+
+void git_diff_patch__old_data(
+	char **ptr, size_t *len, git_diff_patch *patch)
+{
+	*ptr = patch->ofile.map.data;
+	*len = patch->ofile.map.len;
+}
+
+void git_diff_patch__new_data(
+	char **ptr, size_t *len, git_diff_patch *patch)
+{
+	*ptr = patch->nfile.map.data;
+	*len = patch->nfile.map.len;
+}
+
+int git_diff_patch__invoke_callbacks(
+	git_diff_patch *patch,
+	git_diff_file_cb file_cb,
+	git_diff_hunk_cb hunk_cb,
+	git_diff_data_cb line_cb,
+	void *payload)
+{
+	int error = 0;
+	uint32_t i, j;
+
+	if (file_cb)
+		error = file_cb(patch->delta, 0, payload);
+
+	if (!hunk_cb && !line_cb)
+		return error;
+
+	for (i = 0; !error && i < git_array_size(patch->hunks); ++i) {
+		diff_patch_hunk *h = git_array_get(patch->hunks, i);
+
+		error = hunk_cb(
+			patch->delta, &h->range, h->header, h->header_len, payload);
+
+		if (!line_cb)
+			continue;
+
+		for (j = 0; !error && j < h->line_count; ++j) {
+			diff_patch_line *l =
+				git_array_get(patch->lines, h->line_start + j);
+
+			error = line_cb(
+				patch->delta, &h->range, l->origin, l->ptr, l->len, payload);
+		}
+	}
+
+	return error;
+}
+
 
 static int diff_patch_file_cb(
 	const git_diff_delta *delta,
diff --git a/src/diff_patch.h b/src/diff_patch.h
index 7de6e1e..56af146 100644
--- a/src/diff_patch.h
+++ b/src/diff_patch.h
@@ -12,48 +12,19 @@
 #include "diff_file.h"
 #include "array.h"
 
-/* cached information about a single span in a diff */
-typedef struct diff_patch_line diff_patch_line;
-struct diff_patch_line {
-	const char *ptr;
-	size_t len;
-	size_t lines, oldno, newno;
-	char origin;
-};
+extern git_diff_list *git_diff_patch__diff(git_diff_patch *);
 
-/* cached information about a hunk in a diff */
-typedef struct diff_patch_hunk diff_patch_hunk;
-struct diff_patch_hunk {
-	git_diff_range range;
-	char   header[128];
-	size_t header_len;
-	size_t line_start;
-	size_t line_count;
-};
+extern git_diff_driver *git_diff_patch__driver(git_diff_patch *);
 
-struct git_diff_patch {
-	git_refcount rc;
-	git_diff_list *diff; /* for refcount purposes, maybe NULL for blob diffs */
-	git_diff_delta *delta;
-	size_t delta_index;
-	git_diff_file_content ofile;
-	git_diff_file_content nfile;
-	uint32_t flags;
-	git_array_t(diff_patch_hunk) hunks;
-	git_array_t(diff_patch_line) lines;
-	size_t oldno, newno;
-	size_t content_size;
-	git_pool flattened;
-};
+extern void git_diff_patch__old_data(char **, size_t *, git_diff_patch *);
+extern void git_diff_patch__new_data(char **, size_t *, git_diff_patch *);
 
-enum {
-	GIT_DIFF_PATCH_ALLOCATED   = (1 << 0),
-	GIT_DIFF_PATCH_INITIALIZED = (1 << 1),
-	GIT_DIFF_PATCH_LOADED      = (1 << 2),
-	GIT_DIFF_PATCH_DIFFABLE    = (1 << 3),
-	GIT_DIFF_PATCH_DIFFED      = (1 << 4),
-	GIT_DIFF_PATCH_FLATTENED   = (1 << 5),
-};
+extern int git_diff_patch__invoke_callbacks(
+	git_diff_patch *patch,
+	git_diff_file_cb file_cb,
+	git_diff_hunk_cb hunk_cb,
+	git_diff_data_cb line_cb,
+	void *payload);
 
 typedef struct git_diff_output git_diff_output;
 struct git_diff_output {
diff --git a/src/diff_print.c b/src/diff_print.c
index 8608765..244aa6e 100644
--- a/src/diff_print.c
+++ b/src/diff_print.c
@@ -383,29 +383,13 @@ int git_diff_patch_print(
 	int error;
 	git_buf temp = GIT_BUF_INIT;
 	diff_print_info pi;
-	size_t h, l;
 
 	assert(patch && print_cb);
 
 	if (!(error = diff_print_info_init(
-			&pi, &temp, patch->diff, print_cb, payload)))
-		error = print_patch_file(patch->delta, 0, &pi);
-
-	for (h = 0; h < git_array_size(patch->hunks) && !error; ++h) {
-		diff_patch_hunk *hunk = git_array_get(patch->hunks, h);
-
-		error = print_patch_hunk(
-			patch->delta, &hunk->range, hunk->header, hunk->header_len, &pi);
-
-		for (l = 0; l < hunk->line_count && !error; ++l) {
-			diff_patch_line *line =
-				git_array_get(patch->lines, hunk->line_start + l);
-
-			error = print_patch_line(
-				patch->delta, &hunk->range,
-				line->origin, line->ptr, line->len, &pi);
-		}
-	}
+			&pi, &temp, git_diff_patch__diff(patch), print_cb, payload)))
+		error = git_diff_patch__invoke_callbacks(
+			patch, print_patch_file, print_patch_hunk, print_patch_line, &pi);
 
 	git_buf_free(&temp);
 
diff --git a/src/diff_xdiff.c b/src/diff_xdiff.c
index 91c56f7..7694fb9 100644
--- a/src/diff_xdiff.c
+++ b/src/diff_xdiff.c
@@ -59,6 +59,7 @@ static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len)
 {
 	git_xdiff_info *info = priv;
 	git_diff_patch *patch = info->patch;
+	const git_diff_delta *delta = git_diff_patch_delta(patch);
 	git_diff_output *output = &info->xo->output;
 
 	if (len == 1) {
@@ -67,7 +68,7 @@ static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len)
 			return output->error;
 
 		if (output->hunk_cb != NULL &&
-			output->hunk_cb(patch->delta, &info->range,
+			output->hunk_cb(delta, &info->range,
 				bufs[0].ptr, bufs[0].size, output->payload))
 			output->error = GIT_EUSER;
 	}
@@ -80,7 +81,7 @@ static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len)
 			GIT_DIFF_LINE_CONTEXT;
 
 		if (output->data_cb != NULL &&
-			output->data_cb(patch->delta, &info->range,
+			output->data_cb(delta, &info->range,
 				origin, bufs[1].ptr, bufs[1].size, output->payload))
 			output->error = GIT_EUSER;
 	}
@@ -97,7 +98,7 @@ static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len)
 			GIT_DIFF_LINE_CONTEXT_EOFNL;
 
 		if (output->data_cb != NULL &&
-			output->data_cb(patch->delta, &info->range,
+			output->data_cb(delta, &info->range,
 				origin, bufs[2].ptr, bufs[2].size, output->payload))
 			output->error = GIT_EUSER;
 	}
@@ -110,7 +111,7 @@ static int git_xdiff(git_diff_output *output, git_diff_patch *patch)
 	git_xdiff_output *xo = (git_xdiff_output *)output;
 	git_xdiff_info info;
 	git_diff_find_context_payload findctxt;
-	mmfile_t old_xdiff_data, new_xdiff_data;
+	mmfile_t xd_old_data, xd_new_data;
 
 	memset(&info, 0, sizeof(info));
 	info.patch = patch;
@@ -119,7 +120,7 @@ static int git_xdiff(git_diff_output *output, git_diff_patch *patch)
 	xo->callback.priv = &info;
 
 	git_diff_find_context_init(
-		&xo->config.find_func, &findctxt, patch->ofile.driver);
+		&xo->config.find_func, &findctxt, git_diff_patch__driver(patch));
 	xo->config.find_func_priv = &findctxt;
 
 	if (xo->config.find_func != NULL)
@@ -131,12 +132,10 @@ static int git_xdiff(git_diff_output *output, git_diff_patch *patch)
 	 * updates are needed to xo->params.flags
 	 */
 
-	old_xdiff_data.ptr  = patch->ofile.map.data;
-	old_xdiff_data.size = patch->ofile.map.len;
-	new_xdiff_data.ptr  = patch->nfile.map.data;
-	new_xdiff_data.size = patch->nfile.map.len;
+	git_diff_patch__old_data(&xd_old_data.ptr, &xd_old_data.size, patch);
+	git_diff_patch__new_data(&xd_new_data.ptr, &xd_new_data.size, patch);
 
-	xdl_diff(&old_xdiff_data, &new_xdiff_data,
+	xdl_diff(&xd_old_data, &xd_new_data,
 		&xo->params, &xo->config, &xo->callback);
 
 	git_diff_find_context_clear(&findctxt);