From ae850b1ac755f471beac4dbfef4654fe3fbaaae9 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 9 Jan 2024 22:04:24 +0100 Subject: First commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libpatch_format_ed_patch.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 libpatch_format_ed_patch.c (limited to 'libpatch_format_ed_patch.c') diff --git a/libpatch_format_ed_patch.c b/libpatch_format_ed_patch.c new file mode 100644 index 0000000..ba9efcb --- /dev/null +++ b/libpatch_format_ed_patch.c @@ -0,0 +1,58 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +int +libpatch_format_ed_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, bj, an, bn; + int stopped; + + ai = file1->nlines; + bi = file2->nlines; + for (i = difflen; (n = libpatch_previous_hunk(diff, &i, &ai, &bi, &an, &bn, 1));) { + printer->put_hunk_head_prefix(printer, "", 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_operation(printer, !bn ? "d" : !an ? "a" : "c", an > 0, bn > 0); + printer->put_hunk_head_suffix(printer, "", 0); + printer->put_newline(printer); + + if (bn) { + bj = bi; + stopped = 0; + for (; i < n; i++) { + for (j = 0; j < diff[i].repetition; j++) { + if (diff[i].change != LIBPATCH_DIFF2_FILE1_ONLY) { + if (stopped) { + stopped = 0; + printer->put_end_of_change(printer, "a"); + printer->put_newline(printer); + } + printer->put_change_prefix(printer, "", 2, an > 0, bj); + if (file2->lines[bj]->len == 1 && file2->lines[bj]->text[0] == '.') { + printer->put_syntactical_escape(printer, "."); + printer->put_line(printer, file2->lines[bj++]); + printer->put_newline(printer); + printer->put_end_of_change(printer, "."); + printer->put_newline(printer); + printer->put_end_of_change(printer, "s/.//"); + printer->put_newline(printer); + stopped = 1; + } else { + printer->put_line(printer, file2->lines[bj++]); + printer->put_newline(printer); + } + } + } + } + if (!stopped) + printer->put_end_of_change(printer, "."); + printer->put_newline(printer); + } + } + + return 0; +} -- cgit v1.2.3-70-g09d2