aboutsummaryrefslogtreecommitdiffstats
path: root/libpatch_format_normal_patch.c
blob: daf8e8bb4db4a4e208526425a62f6934a22f1105 (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "common.h"


static void
print_line(struct libpatch_diff2_printer *printer, const struct libpatch_file *file, size_t lineno)
{
	printer->put_line(printer, file->lines[lineno]);
	printer->put_newline(printer);
	if (lineno + 1 == file->nlines && !file->lf_terminated) {
		printer->put_no_newline(printer, "\\ No newline at end of file");
		printer->put_newline(printer);
	}
}


static void
print_change(struct libpatch_diff2_printer *printer, int fileno, const struct libpatch_file *file, size_t lineno)
{
	printer->put_change_prefix(printer, fileno == 1 ? "< " : "> ", fileno, 0, lineno);
	print_line(printer, file, lineno);
}


static void
print_context(struct libpatch_diff2_printer *printer, int fileno, const struct libpatch_file *file, size_t lineno)
{
	printer->put_context_prefix(printer, "  ", fileno == 1, fileno == 2, lineno);
	print_line(printer, file, lineno);
}


static void
print_change_context(struct libpatch_diff2_printer *printer, int fileno, const struct libpatch_file *file, size_t lineno)
{
	printer->put_context_prefix(printer, fileno == 1 ? "< " : "> ", fileno == 1, fileno == 2, lineno);
	print_line(printer, file, lineno);
}


int
libpatch_format_normal_patch(struct libpatch_diff2_printer *printer, struct libpatch_diff2 *diff, size_t difflen,
                             const struct libpatch_file *file1, const struct libpatch_file *file2)
{
	size_t i, i0, j, n, ai, bi, an, bn;
	int have, have_a, have_b;

#define HUNK(FNO, FI, FN)\
	do {\
		for (; i < n; i++) {\
			for (j = 0; j < diff[i].repetition; j++) {\
				if (diff[i].change == LIBPATCH_DIFF2_FILE##FNO##_ONLY ||\
				    diff[i].change == LIBPATCH_DIFF2_TWO_FILE_CHANGE)\
					print_change(printer, FNO, file##FNO, FI++);\
				else if (diff[i].change != LIBPATCH_DIFF2_CONTEXT)\
					continue;\
				else if (printer->use_nonstandard_syntax)\
					print_context(printer, FNO, file##FNO, FI++);\
				else\
					print_change_context(printer, FNO, file##FNO, FI++);\
			}\
		}\
	} while (0)

	ai = 0;
	bi = 0;
	for (i = 0; (n = libpatch_next_hunk(diff, difflen, &i, &ai, &bi, &an, &bn, 0));) {
		have = 0;
		for (n += j = i; j < n; j++)
			have |= diff[j].change;
		if (!printer->use_nonstandard_syntax && (have & LIBPATCH_DIFF2_CONTEXT)) {
			have_a = have_b = 1;
		} else {
			have_a = (have & 1);
			have_b = (have & 2);
			if (!have_a && !have_b)
				have_a = have_b = 1;
		}

		printer->put_hunk_head_prefix(printer, "", 0);
		printer->put_hunk_start(printer, "", ai + (have_a ? 1U : 0U), 1);
		if (an > 1)
			printer->put_hunk_end(printer, ",", ai + an, 1);
		printer->put_hunk_operation(printer, !have_b ? "d" : !have_a ? "a" : "c", have_a, have_b);
		printer->put_hunk_start(printer, "", bi + (have_b ? 1U : 0U), 2);
		if (bn > 1)
			printer->put_hunk_end(printer, ",", bi + bn, 2);
		printer->put_hunk_head_suffix(printer, "", 0);
		printer->put_newline(printer);

		if (have_a) {
			i0 = i;
			HUNK(1, ai, an);
			if (have_b) {
				printer->put_syntactical_garbage(printer, "---");
				printer->put_newline(printer);
			}
			i = i0;
		}
		HUNK(2, bi, bn);
	}

	return 0;
}