diff: avoid empty hunks when there are no changes

Currently, diff outputs a file header, even if there are
no changes to the file. This is wonky.

It means that the header chunks are ambiguous, since
not all header chunks are followed by '@@ hunk', and
'--- file', '+++ file' lines can be generated from
file content.

This changes the way that we decide to print the file
header, so we only print it when outputting the first
hunk on flushchanges.

Flushchanges is called once per regular file, at the
end of `diffreg`, so we output a hunk header once per
file.
This commit is contained in:
Ori Bernstein 2022-06-04 02:21:19 +00:00
parent 9eb9c9e560
commit 5d37407e3c
3 changed files with 8 additions and 13 deletions

View file

@ -28,6 +28,5 @@ Biobuf *prepare(int, char *, char *);
void panic(int, char *, ...);
void check(Biobuf *, Biobuf *);
void change(int, int, int, int);
void fileheader(void);
void flushchanges(void);

View file

@ -329,24 +329,16 @@ 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)
{
int a, b, c, d, at;
int a, b, c, d, at, hdr;
int i, j;
if(nchanges == 0)
return;
hdr = 0;
for(i=0; i<nchanges; ){
j = changeset(i);
a = changes[i].a-Lines;
@ -369,6 +361,11 @@ flushchanges(void)
j = nchanges;
}
if(mode == 'u'){
if(!hdr){
Bprint(&stdout, "--- %s\n", file1);
Bprint(&stdout, "+++ %s\n", file2);
hdr = 1;
}
Bprint(&stdout, "@@ -%d,%d +%d,%d @@\n", a, b-a+1, c, d-c+1);
}else{
Bprint(&stdout, "%s:", file1);

View file

@ -284,7 +284,6 @@ output(void)
m = len[0];
J[0] = 0;
J[m+1] = len[1]+1;
fileheader();
if (mode != 'e') {
for (i0 = 1; i0 <= m; i0 = i1+1) {
while (i0 <= m && J[i0] == J[i0-1]+1)