From 8fe1d622b5b46dee51db6581282ec1fba76782b1 Mon Sep 17 00:00:00 2001 From: Ori Bernstein Date: Mon, 21 Sep 2020 09:40:42 -0700 Subject: [PATCH] diff: support unified diff via -u The format produced by `diff -u` is inferior to that produced by `diff -c`, but it's what ape/patch and unix patch expect, so it's useful to generate it. This patch adds `diff -u`. --- sys/man/1/diff | 20 +++++++++++++++++++- sys/src/cmd/diff/diff.h | 1 + sys/src/cmd/diff/diffio.c | 34 ++++++++++++++++++++++++---------- sys/src/cmd/diff/diffreg.c | 1 + sys/src/cmd/diff/main.c | 1 + 5 files changed, 46 insertions(+), 11 deletions(-) diff --git a/sys/man/1/diff b/sys/man/1/diff index 91d3d491a..771ccc83f 100644 --- a/sys/man/1/diff +++ b/sys/man/1/diff @@ -4,7 +4,7 @@ diff \- differential file comparator .SH SYNOPSIS .B diff [ -.B -abcefmnrw +.B -abcefmnruw ] .I file1 ... file2 .SH DESCRIPTION @@ -143,6 +143,24 @@ The .B -a flag displays the entire file as context. .PP +The +.B -u +option provides a unix-compatible unified diff. +This format is similar to that provided by +.BR -c . +However, the +.L + +and +.L - +prefixes are not separated from the rest of the line by spaces, +and the file header is in the following format: +.IP +.EX +--- filename.old ++++ filename.new +@@ -line,len +line,len @@ +.EE +.PP Except in rare circumstances, .I diff finds a smallest sufficient set of file diff --git a/sys/src/cmd/diff/diff.h b/sys/src/cmd/diff/diff.h index 76fe050df..34a5dc010 100644 --- a/sys/src/cmd/diff/diff.h +++ b/sys/src/cmd/diff/diff.h @@ -22,5 +22,6 @@ Biobuf *prepare(int, char *); void panic(int, char *, ...); void check(Biobuf *, Biobuf *); void change(int, int, int, int); +void fileheader(void); void flushchanges(void); diff --git a/sys/src/cmd/diff/diffio.c b/sys/src/cmd/diff/diffio.c index 640f2d2ba..d937b1fef 100644 --- a/sys/src/cmd/diff/diffio.c +++ b/sys/src/cmd/diff/diffio.c @@ -303,6 +303,7 @@ change(int a, int b, int c, int d) break; case 'c': case 'a': + case 'u': if(nchanges%1024 == 0) changes = erealloc(changes, (nchanges+1024)*sizeof(changes[0])); ch = &changes[nchanges++]; @@ -338,6 +339,15 @@ changeset(int i) return nchanges; } +void +fileheader(void) +{ + if(mode != 'u') + return; + Bprint(&stdout, "--- %s\n", file1); + Bprint(&stdout, "+++ %s\n", file2); +} + void flushchanges(void) { @@ -368,20 +378,24 @@ flushchanges(void) d = len[1]; j = nchanges; } - Bprint(&stdout, "%s:", file1); - range(a, b, ","); - Bprint(&stdout, " - "); - Bprint(&stdout, "%s:", file2); - range(c, d, ","); - Bputc(&stdout, '\n'); + if(mode == 'u'){ + Bprint(&stdout, "@@ -%d,%d +%d,%d @@\n", a, b-a+1, c, d-c+1); + }else{ + Bprint(&stdout, "%s:", file1); + range(a, b, ","); + Bprint(&stdout, " - "); + Bprint(&stdout, "%s:", file2); + range(c, d, ","); + Bputc(&stdout, '\n'); + } at = a; for(; i