Fossil SCM

Take advantage of the new pBlob==NULL capabilities in blob_appendf() to simplify some of the diff logic.

drh 2021-09-07 13:29 trunk
Commit 590e01dbddd07fa8280e0acdb5930927d2a60d0639f3063345d953ce5751cdd5
1 file changed +25 -60
+25 -60
--- src/diffcmd.c
+++ src/diffcmd.c
@@ -113,22 +113,16 @@
113113
}
114114
115115
/*
116116
** Print the "Index:" message that patches wants to see at the top of a diff.
117117
*/
118
-void diff_print_index(const char *zFile, DiffConfig *pCfg, Blob *diffBlob){
118
+void diff_print_index(const char *zFile, DiffConfig *pCfg, Blob *pOut){
119119
if( (pCfg->diffFlags &
120120
(DIFF_SIDEBYSIDE|DIFF_BRIEF|DIFF_NUMSTAT|DIFF_JSON|
121121
DIFF_WEBPAGE|DIFF_TCL))==0
122122
){
123
- char *z = mprintf("Index: %s\n%.66c\n", zFile, '=');
124
- if( !diffBlob ){
125
- fossil_print("%s", z);
126
- }else{
127
- blob_appendf(diffBlob, "%s", z);
128
- }
129
- fossil_free(z);
123
+ blob_appendf(pOut, "Index: %s\n%.66c\n", zFile, '=');
130124
}
131125
}
132126
133127
/*
134128
** Print the +++/--- filename lines or whatever filename information
@@ -136,82 +130,61 @@
136130
*/
137131
void diff_print_filenames(
138132
const char *zLeft, /* Name of the left file */
139133
const char *zRight, /* Name of the right file */
140134
DiffConfig *pCfg, /* Diff configuration */
141
- Blob *diffBlob /* Write to this blob, or stdout of this is NULL */
135
+ Blob *pOut /* Write to this blob, or stdout of this is NULL */
142136
){
143
- char *z = 0;
144137
u64 diffFlags = pCfg->diffFlags;
145138
if( diffFlags & (DIFF_BRIEF|DIFF_RAW) ){
146139
/* no-op */
147140
}else if( diffFlags & DIFF_DEBUG ){
148
- fossil_print("FILE-LEFT %s\nFILE-RIGHT %s\n",
149
- zLeft, zRight);
141
+ blob_appendf(pOut, "FILE-LEFT %s\nFILE-RIGHT %s\n", zLeft, zRight);
150142
}else if( diffFlags & DIFF_WEBPAGE ){
151143
if( fossil_strcmp(zLeft,zRight)==0 ){
152
- z = mprintf("<h1>%h</h1>\n", zLeft);
153
- }else{
154
- z = mprintf("<h1>%h &lrarr; %h</h1>\n", zLeft, zRight);
155
- }
156
- }else if( diffFlags & (DIFF_TCL|DIFF_JSON) ){
157
- Blob *pOut;
158
- Blob x;
159
- if( diffBlob ){
160
- pOut = diffBlob;
161
- }else{
162
- blob_init(&x, 0, 0);
163
- pOut = &x;
164
- }
144
+ blob_appendf(pOut,"<h1>%h</h1>\n", zLeft);
145
+ }else{
146
+ blob_appendf(pOut,"<h1>%h &lrarr; %h</h1>\n", zLeft, zRight);
147
+ }
148
+ }else if( diffFlags & (DIFF_TCL|DIFF_JSON) ){
165149
if( diffFlags & DIFF_TCL ){
166150
blob_append(pOut, "FILE ", 5);
167151
blob_append_tcl_literal(pOut, zLeft, (int)strlen(zLeft));
168152
blob_append_char(pOut, ' ');
169153
blob_append_tcl_literal(pOut, zRight, (int)strlen(zRight));
170154
blob_append_char(pOut, '\n');
171155
}else{
172
- blob_trim(pOut);
156
+ if( pOut ) blob_trim(pOut);
173157
blob_append(pOut, (pCfg->nFile==0 ? "[{" : ",\n{"), -1);
174158
pCfg->nFile++;
175159
blob_append(pOut, "\n \"leftname\":", -1);
176160
blob_append_json_literal(pOut, zLeft, (int)strlen(zLeft));
177161
blob_append(pOut, ",\n \"rightname\":", -1);
178162
blob_append_json_literal(pOut, zRight, (int)strlen(zRight));
179163
blob_append(pOut, ",\n \"diff\":\n", -1);
180164
}
181
- if( !diffBlob ){
182
- fossil_print("%s", blob_str(pOut));
183
- blob_reset(&x);
184
- }
185
- return;
186165
}else if( diffFlags & DIFF_SIDEBYSIDE ){
187166
int w = diff_width(pCfg);
188167
int n1 = strlen(zLeft);
189168
int n2 = strlen(zRight);
190169
int x;
191170
if( n1==n2 && fossil_strcmp(zLeft,zRight)==0 ){
192171
if( n1>w*2 ) n1 = w*2;
193172
x = w*2+17 - (n1+2);
194
- z = mprintf("%.*c %.*s %.*c\n",
195
- x/2, '=', n1, zLeft, (x+1)/2, '=');
173
+ blob_appendf(pOut, "%.*c %.*s %.*c\n",
174
+ x/2, '=', n1, zLeft, (x+1)/2, '=');
196175
}else{
197176
if( w<20 ) w = 20;
198177
if( n1>w-10 ) n1 = w - 10;
199178
if( n2>w-10 ) n2 = w - 10;
200
- z = mprintf("%.*c %.*s %.*c versus %.*c %.*s %.*c\n",
201
- (w-n1+10)/2, '=', n1, zLeft, (w-n1+1)/2, '=',
202
- (w-n2)/2, '=', n2, zRight, (w-n2+1)/2, '=');
179
+ blob_appendf(pOut, "%.*c %.*s %.*c versus %.*c %.*s %.*c\n",
180
+ (w-n1+10)/2, '=', n1, zLeft, (w-n1+1)/2, '=',
181
+ (w-n2)/2, '=', n2, zRight, (w-n2+1)/2, '=');
203182
}
204183
}else{
205
- z = mprintf("--- %s\n+++ %s\n", zLeft, zRight);
206
- }
207
- if( !diffBlob ){
208
- fossil_print("%s", z);
209
- }else{
210
- blob_appendf(diffBlob, "%s", z);
211
- }
212
- fossil_free(z);
184
+ blob_appendf(pOut, "--- %s\n+++ %s\n", zLeft, zRight);
185
+ }
213186
}
214187
215188
216189
/*
217190
** Default header text for diff with --webpage
@@ -405,11 +378,11 @@
405378
void diff_file(
406379
Blob *pFile1, /* In memory content to compare from */
407380
const char *zFile2, /* On disk content to compare to */
408381
const char *zName, /* Display name of the file */
409382
DiffConfig *pCfg, /* Flags to control the diff */
410
- Blob *diffBlob /* Blob to store diff output */
383
+ Blob *pOut /* Blob to store diff output */
411384
){
412385
if( pCfg->zDiffCmd==0 ){
413386
Blob out; /* Diff output text */
414387
Blob file2; /* Content of zFile2 */
415388
const char *zName2; /* Name of zFile2 for display */
@@ -431,22 +404,14 @@
431404
}else{
432405
blob_zero(&out);
433406
text_diff(pFile1, &file2, &out, pCfg);
434407
if( blob_size(&out) ){
435408
if( pCfg->diffFlags & DIFF_NUMSTAT ){
436
- if( !diffBlob ){
437
- fossil_print("%s %s\n", blob_str(&out), zName);
438
- }else{
439
- blob_appendf(diffBlob, "%s %s\n", blob_str(&out), zName);
440
- }
441
- }else{
442
- diff_print_filenames(zName, zName2, pCfg, diffBlob);
443
- if( !diffBlob ){
444
- fossil_print("%s\n", blob_str(&out));
445
- }else{
446
- blob_appendf(diffBlob, "%s\n", blob_str(&out));
447
- }
409
+ blob_appendf(pOut, "%s %s\n", blob_str(&out), zName);
410
+ }else{
411
+ diff_print_filenames(zName, zName2, pCfg, pOut);
412
+ blob_appendf(pOut, "%s\n", blob_str(&out));
448413
}
449414
}
450415
blob_reset(&out);
451416
}
452417
@@ -603,11 +568,11 @@
603568
*/
604569
void diff_against_disk(
605570
const char *zFrom, /* Version to difference from */
606571
DiffConfig *pCfg, /* Flags controlling diff output */
607572
FileDirList *pFileDir, /* Which files to diff */
608
- Blob *diffBlob /* Blob to output diff instead of stdout */
573
+ Blob *pOut /* Blob to output diff instead of stdout */
609574
){
610575
int vid;
611576
Blob sql;
612577
Stmt q;
613578
int asNewFile; /* Treat non-existant files as empty files */
@@ -707,12 +672,12 @@
707672
if( srcid>0 ){
708673
content_get(srcid, &content);
709674
}else{
710675
blob_zero(&content);
711676
}
712
- diff_print_index(zPathname, pCfg, diffBlob);
713
- diff_file(&content, zFullName, zPathname, pCfg, diffBlob);
677
+ diff_print_index(zPathname, pCfg, pOut);
678
+ diff_file(&content, zFullName, zPathname, pCfg, pOut);
714679
blob_reset(&content);
715680
}
716681
blob_reset(&fname);
717682
}
718683
db_finalize(&q);
719684
--- src/diffcmd.c
+++ src/diffcmd.c
@@ -113,22 +113,16 @@
113 }
114
115 /*
116 ** Print the "Index:" message that patches wants to see at the top of a diff.
117 */
118 void diff_print_index(const char *zFile, DiffConfig *pCfg, Blob *diffBlob){
119 if( (pCfg->diffFlags &
120 (DIFF_SIDEBYSIDE|DIFF_BRIEF|DIFF_NUMSTAT|DIFF_JSON|
121 DIFF_WEBPAGE|DIFF_TCL))==0
122 ){
123 char *z = mprintf("Index: %s\n%.66c\n", zFile, '=');
124 if( !diffBlob ){
125 fossil_print("%s", z);
126 }else{
127 blob_appendf(diffBlob, "%s", z);
128 }
129 fossil_free(z);
130 }
131 }
132
133 /*
134 ** Print the +++/--- filename lines or whatever filename information
@@ -136,82 +130,61 @@
136 */
137 void diff_print_filenames(
138 const char *zLeft, /* Name of the left file */
139 const char *zRight, /* Name of the right file */
140 DiffConfig *pCfg, /* Diff configuration */
141 Blob *diffBlob /* Write to this blob, or stdout of this is NULL */
142 ){
143 char *z = 0;
144 u64 diffFlags = pCfg->diffFlags;
145 if( diffFlags & (DIFF_BRIEF|DIFF_RAW) ){
146 /* no-op */
147 }else if( diffFlags & DIFF_DEBUG ){
148 fossil_print("FILE-LEFT %s\nFILE-RIGHT %s\n",
149 zLeft, zRight);
150 }else if( diffFlags & DIFF_WEBPAGE ){
151 if( fossil_strcmp(zLeft,zRight)==0 ){
152 z = mprintf("<h1>%h</h1>\n", zLeft);
153 }else{
154 z = mprintf("<h1>%h &lrarr; %h</h1>\n", zLeft, zRight);
155 }
156 }else if( diffFlags & (DIFF_TCL|DIFF_JSON) ){
157 Blob *pOut;
158 Blob x;
159 if( diffBlob ){
160 pOut = diffBlob;
161 }else{
162 blob_init(&x, 0, 0);
163 pOut = &x;
164 }
165 if( diffFlags & DIFF_TCL ){
166 blob_append(pOut, "FILE ", 5);
167 blob_append_tcl_literal(pOut, zLeft, (int)strlen(zLeft));
168 blob_append_char(pOut, ' ');
169 blob_append_tcl_literal(pOut, zRight, (int)strlen(zRight));
170 blob_append_char(pOut, '\n');
171 }else{
172 blob_trim(pOut);
173 blob_append(pOut, (pCfg->nFile==0 ? "[{" : ",\n{"), -1);
174 pCfg->nFile++;
175 blob_append(pOut, "\n \"leftname\":", -1);
176 blob_append_json_literal(pOut, zLeft, (int)strlen(zLeft));
177 blob_append(pOut, ",\n \"rightname\":", -1);
178 blob_append_json_literal(pOut, zRight, (int)strlen(zRight));
179 blob_append(pOut, ",\n \"diff\":\n", -1);
180 }
181 if( !diffBlob ){
182 fossil_print("%s", blob_str(pOut));
183 blob_reset(&x);
184 }
185 return;
186 }else if( diffFlags & DIFF_SIDEBYSIDE ){
187 int w = diff_width(pCfg);
188 int n1 = strlen(zLeft);
189 int n2 = strlen(zRight);
190 int x;
191 if( n1==n2 && fossil_strcmp(zLeft,zRight)==0 ){
192 if( n1>w*2 ) n1 = w*2;
193 x = w*2+17 - (n1+2);
194 z = mprintf("%.*c %.*s %.*c\n",
195 x/2, '=', n1, zLeft, (x+1)/2, '=');
196 }else{
197 if( w<20 ) w = 20;
198 if( n1>w-10 ) n1 = w - 10;
199 if( n2>w-10 ) n2 = w - 10;
200 z = mprintf("%.*c %.*s %.*c versus %.*c %.*s %.*c\n",
201 (w-n1+10)/2, '=', n1, zLeft, (w-n1+1)/2, '=',
202 (w-n2)/2, '=', n2, zRight, (w-n2+1)/2, '=');
203 }
204 }else{
205 z = mprintf("--- %s\n+++ %s\n", zLeft, zRight);
206 }
207 if( !diffBlob ){
208 fossil_print("%s", z);
209 }else{
210 blob_appendf(diffBlob, "%s", z);
211 }
212 fossil_free(z);
213 }
214
215
216 /*
217 ** Default header text for diff with --webpage
@@ -405,11 +378,11 @@
405 void diff_file(
406 Blob *pFile1, /* In memory content to compare from */
407 const char *zFile2, /* On disk content to compare to */
408 const char *zName, /* Display name of the file */
409 DiffConfig *pCfg, /* Flags to control the diff */
410 Blob *diffBlob /* Blob to store diff output */
411 ){
412 if( pCfg->zDiffCmd==0 ){
413 Blob out; /* Diff output text */
414 Blob file2; /* Content of zFile2 */
415 const char *zName2; /* Name of zFile2 for display */
@@ -431,22 +404,14 @@
431 }else{
432 blob_zero(&out);
433 text_diff(pFile1, &file2, &out, pCfg);
434 if( blob_size(&out) ){
435 if( pCfg->diffFlags & DIFF_NUMSTAT ){
436 if( !diffBlob ){
437 fossil_print("%s %s\n", blob_str(&out), zName);
438 }else{
439 blob_appendf(diffBlob, "%s %s\n", blob_str(&out), zName);
440 }
441 }else{
442 diff_print_filenames(zName, zName2, pCfg, diffBlob);
443 if( !diffBlob ){
444 fossil_print("%s\n", blob_str(&out));
445 }else{
446 blob_appendf(diffBlob, "%s\n", blob_str(&out));
447 }
448 }
449 }
450 blob_reset(&out);
451 }
452
@@ -603,11 +568,11 @@
603 */
604 void diff_against_disk(
605 const char *zFrom, /* Version to difference from */
606 DiffConfig *pCfg, /* Flags controlling diff output */
607 FileDirList *pFileDir, /* Which files to diff */
608 Blob *diffBlob /* Blob to output diff instead of stdout */
609 ){
610 int vid;
611 Blob sql;
612 Stmt q;
613 int asNewFile; /* Treat non-existant files as empty files */
@@ -707,12 +672,12 @@
707 if( srcid>0 ){
708 content_get(srcid, &content);
709 }else{
710 blob_zero(&content);
711 }
712 diff_print_index(zPathname, pCfg, diffBlob);
713 diff_file(&content, zFullName, zPathname, pCfg, diffBlob);
714 blob_reset(&content);
715 }
716 blob_reset(&fname);
717 }
718 db_finalize(&q);
719
--- src/diffcmd.c
+++ src/diffcmd.c
@@ -113,22 +113,16 @@
113 }
114
115 /*
116 ** Print the "Index:" message that patches wants to see at the top of a diff.
117 */
118 void diff_print_index(const char *zFile, DiffConfig *pCfg, Blob *pOut){
119 if( (pCfg->diffFlags &
120 (DIFF_SIDEBYSIDE|DIFF_BRIEF|DIFF_NUMSTAT|DIFF_JSON|
121 DIFF_WEBPAGE|DIFF_TCL))==0
122 ){
123 blob_appendf(pOut, "Index: %s\n%.66c\n", zFile, '=');
 
 
 
 
 
 
124 }
125 }
126
127 /*
128 ** Print the +++/--- filename lines or whatever filename information
@@ -136,82 +130,61 @@
130 */
131 void diff_print_filenames(
132 const char *zLeft, /* Name of the left file */
133 const char *zRight, /* Name of the right file */
134 DiffConfig *pCfg, /* Diff configuration */
135 Blob *pOut /* Write to this blob, or stdout of this is NULL */
136 ){
 
137 u64 diffFlags = pCfg->diffFlags;
138 if( diffFlags & (DIFF_BRIEF|DIFF_RAW) ){
139 /* no-op */
140 }else if( diffFlags & DIFF_DEBUG ){
141 blob_appendf(pOut, "FILE-LEFT %s\nFILE-RIGHT %s\n", zLeft, zRight);
 
142 }else if( diffFlags & DIFF_WEBPAGE ){
143 if( fossil_strcmp(zLeft,zRight)==0 ){
144 blob_appendf(pOut,"<h1>%h</h1>\n", zLeft);
145 }else{
146 blob_appendf(pOut,"<h1>%h &lrarr; %h</h1>\n", zLeft, zRight);
147 }
148 }else if( diffFlags & (DIFF_TCL|DIFF_JSON) ){
 
 
 
 
 
 
 
 
149 if( diffFlags & DIFF_TCL ){
150 blob_append(pOut, "FILE ", 5);
151 blob_append_tcl_literal(pOut, zLeft, (int)strlen(zLeft));
152 blob_append_char(pOut, ' ');
153 blob_append_tcl_literal(pOut, zRight, (int)strlen(zRight));
154 blob_append_char(pOut, '\n');
155 }else{
156 if( pOut ) blob_trim(pOut);
157 blob_append(pOut, (pCfg->nFile==0 ? "[{" : ",\n{"), -1);
158 pCfg->nFile++;
159 blob_append(pOut, "\n \"leftname\":", -1);
160 blob_append_json_literal(pOut, zLeft, (int)strlen(zLeft));
161 blob_append(pOut, ",\n \"rightname\":", -1);
162 blob_append_json_literal(pOut, zRight, (int)strlen(zRight));
163 blob_append(pOut, ",\n \"diff\":\n", -1);
164 }
 
 
 
 
 
165 }else if( diffFlags & DIFF_SIDEBYSIDE ){
166 int w = diff_width(pCfg);
167 int n1 = strlen(zLeft);
168 int n2 = strlen(zRight);
169 int x;
170 if( n1==n2 && fossil_strcmp(zLeft,zRight)==0 ){
171 if( n1>w*2 ) n1 = w*2;
172 x = w*2+17 - (n1+2);
173 blob_appendf(pOut, "%.*c %.*s %.*c\n",
174 x/2, '=', n1, zLeft, (x+1)/2, '=');
175 }else{
176 if( w<20 ) w = 20;
177 if( n1>w-10 ) n1 = w - 10;
178 if( n2>w-10 ) n2 = w - 10;
179 blob_appendf(pOut, "%.*c %.*s %.*c versus %.*c %.*s %.*c\n",
180 (w-n1+10)/2, '=', n1, zLeft, (w-n1+1)/2, '=',
181 (w-n2)/2, '=', n2, zRight, (w-n2+1)/2, '=');
182 }
183 }else{
184 blob_appendf(pOut, "--- %s\n+++ %s\n", zLeft, zRight);
185 }
 
 
 
 
 
 
186 }
187
188
189 /*
190 ** Default header text for diff with --webpage
@@ -405,11 +378,11 @@
378 void diff_file(
379 Blob *pFile1, /* In memory content to compare from */
380 const char *zFile2, /* On disk content to compare to */
381 const char *zName, /* Display name of the file */
382 DiffConfig *pCfg, /* Flags to control the diff */
383 Blob *pOut /* Blob to store diff output */
384 ){
385 if( pCfg->zDiffCmd==0 ){
386 Blob out; /* Diff output text */
387 Blob file2; /* Content of zFile2 */
388 const char *zName2; /* Name of zFile2 for display */
@@ -431,22 +404,14 @@
404 }else{
405 blob_zero(&out);
406 text_diff(pFile1, &file2, &out, pCfg);
407 if( blob_size(&out) ){
408 if( pCfg->diffFlags & DIFF_NUMSTAT ){
409 blob_appendf(pOut, "%s %s\n", blob_str(&out), zName);
410 }else{
411 diff_print_filenames(zName, zName2, pCfg, pOut);
412 blob_appendf(pOut, "%s\n", blob_str(&out));
 
 
 
 
 
 
 
 
413 }
414 }
415 blob_reset(&out);
416 }
417
@@ -603,11 +568,11 @@
568 */
569 void diff_against_disk(
570 const char *zFrom, /* Version to difference from */
571 DiffConfig *pCfg, /* Flags controlling diff output */
572 FileDirList *pFileDir, /* Which files to diff */
573 Blob *pOut /* Blob to output diff instead of stdout */
574 ){
575 int vid;
576 Blob sql;
577 Stmt q;
578 int asNewFile; /* Treat non-existant files as empty files */
@@ -707,12 +672,12 @@
672 if( srcid>0 ){
673 content_get(srcid, &content);
674 }else{
675 blob_zero(&content);
676 }
677 diff_print_index(zPathname, pCfg, pOut);
678 diff_file(&content, zFullName, zPathname, pCfg, pOut);
679 blob_reset(&content);
680 }
681 blob_reset(&fname);
682 }
683 db_finalize(&q);
684

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button