summaryrefslogtreecommitdiff
path: root/diff.h
blob: 9443dc1b0039026ba6c32efd3e445fa61a0860f5 (plain)
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/*
 * Copyright (C) 2005 Junio C Hamano
 */
#ifndef DIFF_H
#define DIFF_H

#include "tree-walk.h"
#include "pathspec.h"
#include "object.h"
#include "oidset.h"

/**
 * The diff API is for programs that compare two sets of files (e.g. two trees,
 * one tree and the index) and present the found difference in various ways.
 * The calling program is responsible for feeding the API pairs of files, one
 * from the "old" set and the corresponding one from "new" set, that are
 * different.
 * The library called through this API is called diffcore, and is responsible
 * for two things.
 *
 * - finding total rewrites (`-B`), renames (`-M`) and copies (`-C`), and
 * changes that touch a string (`-S`), as specified by the caller.
 *
 * - outputting the differences in various formats, as specified by the caller.
 *
 * Calling sequence
 * ----------------
 *
 * - Prepare `struct diff_options` to record the set of diff options, and then
 * call `repo_diff_setup()` to initialize this structure.  This sets up the
 * vanilla default.
 *
 * - Fill in the options structure to specify desired output format, rename
 * detection, etc.  `diff_opt_parse()` can be used to parse options given
 * from the command line in a way consistent with existing git-diff family
 * of programs.
 *
 * - Call `diff_setup_done()`; this inspects the options set up so far for
 * internal consistency and make necessary tweaking to it (e.g. if textual
 * patch output was asked, recursive behaviour is turned on); the callback
 * set_default in diff_options can be used to tweak this more.
 *
 * - As you find different pairs of files, call `diff_change()` to feed
 * modified files, `diff_addremove()` to feed created or deleted files, or
 * `diff_unmerge()` to feed a file whose state is 'unmerged' to the API.
 * These are thin wrappers to a lower-level `diff_queue()` function that is
 * flexible enough to record any of these kinds of changes.
 *
 * - Once you finish feeding the pairs of files, call `diffcore_std()`.
 * This will tell the diffcore library to go ahead and do its work.
 *
 * - Calling `diff_flush()` will produce the output.
 */

struct combine_diff_path;
struct commit;
struct diff_filespec;
struct diff_options;
struct diff_queue_struct;
struct oid_array;
struct option;
struct repository;
struct rev_info;
struct strbuf;
struct userdiff_driver;

typedef int (*pathchange_fn_t)(struct diff_options *options,
		 struct combine_diff_path *path);

typedef void (*change_fn_t)(struct diff_options *options,
		 unsigned old_mode, unsigned new_mode,
		 const struct object_id *old_oid,
		 const struct object_id *new_oid,
		 int old_oid_valid, int new_oid_valid,
		 const char *fullpath,
		 unsigned old_dirty_submodule, unsigned new_dirty_submodule);

typedef void (*add_remove_fn_t)(struct diff_options *options,
		    int addremove, unsigned mode,
		    const struct object_id *oid,
		    int oid_valid,
		    const char *fullpath, unsigned dirty_submodule);

typedef void (*diff_format_fn_t)(struct diff_queue_struct *q,
		struct diff_options *options, void *data);

typedef struct strbuf *(*diff_prefix_fn_t)(struct diff_options *opt, void *data);

#define DIFF_FORMAT_RAW		0x0001
#define DIFF_FORMAT_DIFFSTAT	0x0002
#define DIFF_FORMAT_NUMSTAT	0x0004
#define DIFF_FORMAT_SUMMARY	0x0008
#define DIFF_FORMAT_PATCH	0x0010
#define DIFF_FORMAT_SHORTSTAT	0x0020
#define DIFF_FORMAT_DIRSTAT	0x0040

/* These override all above */
#define DIFF_FORMAT_NAME	0x0100
#define DIFF_FORMAT_NAME_STATUS	0x0200
#define DIFF_FORMAT_CHECKDIFF	0x0400

/* Same as output_format = 0 but we know that -s flag was given
 * and we should not give default value to output_format.
 */
#define DIFF_FORMAT_NO_OUTPUT	0x0800

#define DIFF_FORMAT_CALLBACK	0x1000

#define DIFF_FLAGS_INIT { 0 }
struct diff_flags {

	/**
	 * Tells if tree traversal done by tree-diff should recursively descend
	 * into a tree object pair that are different in preimage and postimage set.
	 */
	unsigned recursive;
	unsigned tree_in_recursive;

	/* Affects the way how a file that is seemingly binary is treated. */
	unsigned binary;
	unsigned text;

	/**
	 * Tells the patch output format not to use abbreviated object names on the
	 * "index" lines.
	 */
	unsigned full_index;

	/* Affects if diff-files shows removed files. */
	unsigned silent_on_remove;

	/**
	 * Tells the diffcore library that the caller is feeding unchanged
	 * filepairs to allow copies from unmodified files be detected.
	 */
	unsigned find_copies_harder;

	unsigned follow_renames;
	unsigned rename_empty;

	/* Internal; used for optimization to see if there is any change. */
	unsigned has_changes;

	unsigned quick;

	/**
	 * Tells diff-files that the input is not tracked files but files in random
	 * locations on the filesystem.
	 */
	unsigned no_index;

	/**
	 * Tells output routine that it is Ok to call user specified patch output
	 * routine.  Plumbing disables this to ensure stable output.
	 */
	unsigned allow_external;

	/**
	 * For communication between the calling program and the options parser;
	 * tell the calling program to signal the presence of difference using
	 * program exit code.
	 */
	unsigned exit_with_status;

	/**
	 * Tells the library that the calling program is feeding the filepairs
	 * reversed; `one` is two, and `two` is one.
	 */
	unsigned reverse_diff;

	unsigned check_failed;
	unsigned relative_name;
	unsigned ignore_submodules;
	unsigned dirstat_cumulative;
	unsigned dirstat_by_file;
	unsigned allow_textconv;
	unsigned textconv_set_via_cmdline;
	unsigned diff_from_contents;
	unsigned dirty_submodules;
	unsigned ignore_untracked_in_submodules;
	unsigned ignore_dirty_submodules;
	unsigned override_submodule_config;
	unsigned dirstat_by_line;
	unsigned funccontext;
	unsigned default_follow_renames;
	unsigned stat_with_summary;
	unsigned suppress_diff_headers;
	unsigned dual_color_diffed_diffs;
	unsigned suppress_hunk_header_line_count;
};

static inline void diff_flags_or(struct diff_flags *a,
				 const struct diff_flags *b)
{
	char *tmp_a = (char *)a;
	const char *tmp_b = (const char *)b;
	int i;

	for (i = 0; i < sizeof(struct diff_flags); i++)
		tmp_a[i] |= tmp_b[i];
}

#define DIFF_XDL_TST(opts, flag)    ((opts)->xdl_opts & XDF_##flag)
#define DIFF_XDL_SET(opts, flag)    ((opts)->xdl_opts |= XDF_##flag)
#define DIFF_XDL_CLR(opts, flag)    ((opts)->xdl_opts &= ~XDF_##flag)

#define DIFF_WITH_ALG(opts, flag)   (((opts)->xdl_opts & ~XDF_DIFF_ALGORITHM_MASK) | XDF_##flag)

enum diff_words_type {
	DIFF_WORDS_NONE = 0,
	DIFF_WORDS_PORCELAIN,
	DIFF_WORDS_PLAIN,
	DIFF_WORDS_COLOR
};

enum diff_submodule_format {
	DIFF_SUBMODULE_SHORT = 0,
	DIFF_SUBMODULE_LOG,
	DIFF_SUBMODULE_INLINE_DIFF
};

/**
 * the set of options the calling program wants to affect the operation of
 * diffcore library with.
 */
struct diff_options {
	const char *orderfile;

	/**
	 * A constant string (can and typically does contain newlines to look for
	 * a block of text, not just a single line) to filter out the filepairs
	 * that do not change the number of strings contained in its preimage and
	 * postimage of the diff_queue.
	 */
	const char *pickaxe;

	const char *single_follow;
	const char *a_prefix, *b_prefix;
	const char *line_prefix;
	size_t line_prefix_length;

	/**
	 * collection of boolean options that affects the operation, but some do
	 * not have anything to do with the diffcore library.
	 */
	struct diff_flags flags;

	/* diff-filter bits */
	unsigned int filter;

	int use_color;

	/* Number of context lines to generate in patch output. */
	int context;

	int interhunkcontext;

	/* Affects the way detection logic for complete rewrites, renames and
	 * copies.
	 */
	int break_opt;
	int detect_rename;

	int irreversible_delete;
	int skip_stat_unmatch;
	int line_termination;

	/* The output format used when `diff_flush()` is run. */
	int output_format;

	unsigned pickaxe_opts;

	/* Affects the way detection logic for complete rewrites, renames and
	 * copies.
	 */
	int rename_score;
	int rename_limit;

	int needed_rename_limit;
	int degraded_cc_to_c;
	int show_rename_progress;
	int dirstat_permille;
	int setup;

	/* Number of hexdigits to abbreviate raw format output to. */
	int abbrev;

	/* If non-zero, then stop computing after this many changes. */
	int max_changes;
	/* For internal use only. */
	int num_changes;

	int ita_invisible_in_index;
/* white-space error highlighting */
#define WSEH_NEW (1<<12)
#define WSEH_CONTEXT (1<<13)
#define WSEH_OLD (1<<14)
	unsigned ws_error_highlight;
	const char *prefix;
	int prefix_length;
	const char *stat_sep;
	int xdl_opts;

	/* see Documentation/diff-options.txt */
	char **anchors;
	size_t anchors_nr, anchors_alloc;

	int stat_width;
	int stat_name_width;
	int stat_graph_width;
	int stat_count;
	const char *word_regex;
	enum diff_words_type word_diff;
	enum diff_submodule_format submodule_format;

	struct oidset *objfind;

	/* this is set by diffcore for DIFF_FORMAT_PATCH */
	int found_changes;

	/* to support internal diff recursion by --follow hack*/
	int found_follow;

	/* Callback which allows tweaking the options in diff_setup_done(). */
	void (*set_default)(struct diff_options *);

	FILE *file;
	int close_file;

#define OUTPUT_INDICATOR_NEW 0
#define OUTPUT_INDICATOR_OLD 1
#define OUTPUT_INDICATOR_CONTEXT 2
	char output_indicators[3];

	struct pathspec pathspec;
	pathchange_fn_t pathchange;
	change_fn_t change;
	add_remove_fn_t add_remove;
	void *change_fn_data;
	diff_format_fn_t format_callback;
	void *format_callback_data;
	diff_prefix_fn_t output_prefix;