aboutsummaryrefslogtreecommitdiffstats
path: root/libpatch_format_normal_patch.c
diff options
context:
space:
mode:
Diffstat (limited to 'libpatch_format_normal_patch.c')
-rw-r--r--libpatch_format_normal_patch.c104
1 files changed, 104 insertions, 0 deletions
diff --git a/libpatch_format_normal_patch.c b/libpatch_format_normal_patch.c
new file mode 100644
index 0000000..daf8e8b
--- /dev/null
+++ b/libpatch_format_normal_patch.c
@@ -0,0 +1,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;
+}