aboutsummaryrefslogtreecommitdiffstats
path: root/libpatch_format_ed_alternative_patch.c
blob: 68cd05ee2251f3308ad8cd042f2b6b75668f2bd2 (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
/* See LICENSE file for copyright and license details. */
#include "common.h"


int
libpatch_format_ed_alternative_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, j, n, ai, bi, an, bn;

	ai = 0;
	bi = 0;
	for (i = 0; (n = libpatch_next_hunk(diff, difflen, &i, &ai, &bi, &an, &bn, 1));) {
		printer->put_hunk_head_prefix(printer, "", 0);
		printer->put_hunk_operation(printer, !bn ? "d" : !an ? "a" : "c", an > 0, bn > 0);
		printer->put_hunk_start(printer, "", ai + (an ? 1U : 0U), 1);
		if (an > 1)
			printer->put_hunk_end(printer, " ", ai + an, 1);
		printer->put_hunk_head_suffix(printer, "", 0);
		printer->put_newline(printer);

		ai += an;
		if (bn) {
			for (; i < n; i++) {
				for (j = 0; j < diff[i].repetition; j++) {
					if (diff[i].change != LIBPATCH_DIFF2_FILE1_ONLY) {
						printer->put_change_prefix(printer, "", 2, an > 0, bi);
						printer->put_line(printer, file2->lines[bi++]);
						printer->put_newline(printer);
					}
				}
			}
			printer->put_end_of_change(printer, ".");
			printer->put_newline(printer);
		}
	}

	(void) file1;
	return 0;
}