aboutsummaryrefslogblamecommitdiffstats
path: root/libpatch_format_unified_patch.c
blob: 1caab9c074220ad92fa531b30bb010f6e27e6ffc (plain) (tree)





















































































                                                                                                                  
/* 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, const struct libpatch_file *file, size_t lineno)
{
	printer->put_context_prefix(printer, " ", 1, 1, lineno);
	print_line(printer, file, lineno);
}


int
libpatch_format_unified_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;

	printer->put_label_prefix(printer, "---", 1);
	printer->put_whitespace(printer, " ");
	if (file1->label)
		printer->put_label(printer, file1->label, 1);
	printer->put_newline(printer);

	printer->put_label_prefix(printer, "+++", 2);
	printer->put_whitespace(printer, " ");
	if (file2->label)
		printer->put_label(printer, file2->label, 2);
	printer->put_newline(printer);

	ai = 0;
	bi = 0;
	for (i = 0; (n = libpatch_next_hunk(diff, difflen, &i, &ai, &bi, &an, &bn, 0));) {
		printer->put_hunk_head_prefix(printer, "@@", 0);
		printer->put_whitespace(printer, " ");
		printer->put_hunk_start(printer, "-", ai + (size_t)(an > 0), 1);
		if (an)
			printer->put_hunk_length(printer, ",", an, 1);
		printer->put_whitespace(printer, " ");
		printer->put_hunk_start(printer, "+", bi + (size_t)(bn > 0), 2);
		if (bn)
			printer->put_hunk_length(printer, ",", bn, 2);
		printer->put_whitespace(printer, " ");
		printer->put_hunk_head_suffix(printer, "@@", 0);
		printer->put_newline(printer);

		for (n += i; i < n; i++) {
			for (j = 0; j < diff[i].repetition; j++) {
				if (diff[i].change == LIBPATCH_DIFF2_FILE1_ONLY) {
					print_change(printer, 1, file1, ai++);
				} else if (diff[i].change == LIBPATCH_DIFF2_FILE2_ONLY) {
					print_change(printer, 2, file2, bi++);
				} else if (diff[i].change == LIBPATCH_DIFF2_TWO_FILE_CHANGE) {
					print_change(printer, 1, file1, ai++);
					print_change(printer, 2, file2, bi++);
				} else if (printer->use_file2_when_common) {
					print_context(printer, file2, bi++), ai++;
				} else {
					print_context(printer, file1, ai++), bi++;
				}
			}
		}
	}

	return 0;
}