Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
| Comment: | modify looks_like_binary() to looks_like_text() such that it is usable in cr_warning() as well |
|---|---|
| Downloads: | Tarball | ZIP archive |
| Timelines: | family | ancestors | descendants | both | trunk |
| Files: | files | file ages | folders |
| SHA1: |
1bffce5230e5d9f5d5142fa479adcc5d |
| User & Date: | jan.nijtmans 2012-10-28 09:34:36.828 |
Context
|
2012-10-28
| ||
| 17:28 | Performance enhancement for branch name look-ups. ... (check-in: 186405ce3a user: drh tags: trunk) | |
| 10:03 | merge trunk ... (check-in: 0b6b0dfd0b user: jan.nijtmans tags: use-blob_strip_bom) | |
| 09:34 | modify looks_like_binary() to looks_like_text() such that it is usable in cr_warning() as well ... (check-in: 1bffce5230 user: jan.nijtmans tags: trunk) | |
| 09:05 | glad that I double-checked.... ... (check-in: 6feba0dba5 user: jan.nijtmans tags: trunk) | |
Changes
Changes to src/checkin.c.
| ︙ | ︙ | |||
883 884 885 886 887 888 889 |
}
/*
** Issue a warning and give the user an opportunity to abandon out
** if a \r\n line ending is seen in a text file.
*/
static void cr_warning(const Blob *p, const char *zFilename){
| < < < < < < < < < < < < < < < < < < | < < < < | 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 |
}
/*
** Issue a warning and give the user an opportunity to abandon out
** if a \r\n line ending is seen in a text file.
*/
static void cr_warning(const Blob *p, const char *zFilename){
char *zMsg; /* Warning message */
Blob fname; /* Relative pathname of the file */
static int allOk = 0; /* Set to true to disable this routine */
if( allOk ) return;
if( looks_like_text(p)<0 ){
Blob ans;
char cReply;
file_relative_name(zFilename, &fname, 0);
blob_zero(&ans);
zMsg = mprintf(
"%s contains CR/NL line endings; commit anyhow (yes/no/all)?",
|
| ︙ | ︙ |
Changes to src/diff.c.
| ︙ | ︙ | |||
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
*/
#define DIFF_CANNOT_COMPUTE_BINARY \
"cannot compute difference between binary files\n"
#define DIFF_CANNOT_COMPUTE_SYMLINK \
"cannot compute difference between symlink and regular file\n"
#endif /* INTERFACE */
/*
** Maximum length of a line in a text file. (8192)
*/
#define LENGTH_MASK_SZ 13
#define LENGTH_MASK ((1<<LENGTH_MASK_SZ)-1)
| > | 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
*/
#define DIFF_CANNOT_COMPUTE_BINARY \
"cannot compute difference between binary files\n"
#define DIFF_CANNOT_COMPUTE_SYMLINK \
"cannot compute difference between symlink and regular file\n"
#define looks_like_binary(blob) (!(looks_like_text(blob)&1))
#endif /* INTERFACE */
/*
** Maximum length of a line in a text file. (8192)
*/
#define LENGTH_MASK_SZ 13
#define LENGTH_MASK ((1<<LENGTH_MASK_SZ)-1)
|
| ︙ | ︙ | |||
104 105 106 107 108 109 110 | int nFrom; /* Number of lines in aFrom[] */ DLine *aTo; /* File on right side of the diff */ int nTo; /* Number of lines in aTo[] */ }; /* ** Return an array of DLine objects containing a pointer to the | | | 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | int nFrom; /* Number of lines in aFrom[] */ DLine *aTo; /* File on right side of the diff */ int nTo; /* Number of lines in aTo[] */ }; /* ** Return an array of DLine objects containing a pointer to the ** start of each line and a hash of that line. The lower ** bits of the hash store the length of each line. ** ** Trailing whitespace is removed from each line. 2010-08-20: Not any ** more. If trailing whitespace is ignored, the "patch" command gets ** confused by the diff output. Ticket [a9f7b23c2e376af5b0e5b] ** ** Return 0 if the file is binary or contains a line that is |
| ︙ | ︙ | |||
167 168 169 170 171 172 173 | /* Return results */ *pnLine = nLine; return a; } /* | > | | > | > | < > > > | | | | 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
/* Return results */
*pnLine = nLine;
return a;
}
/*
** Returns 1, if the file appears text, and does not contain CrLf
** Returns 0 if the specified content appears to be binary or
** contains a line that is too long
** Returns -1, if the file appears text, but it contains CrLf
*/
int looks_like_text(const Blob *pContent){
const char *z = blob_buffer(pContent);
int n = blob_size(pContent);
int i, j;
int result = 1;
/* Check individual lines.
*/
for(i=j=0; i<n; i++, j++){
int c = z[i];
if( c==0 ) return 1; /* \000 byte in a file -> binary */
if( c=='\n' ){
if( i>0 && z[i-1]=='\r' ){
result = -1; /* Contains CrLf, continue */
}
if( j>LENGTH_MASK ){
return 0; /* Very long line -> binary */
}
j = 0;
}
}
if( j>LENGTH_MASK ){
return 0; /* Very long line -> binary */
}
return result; /* No problems seen -> not binary */
}
/*
** Return true if two DLine elements are identical.
*/
static int same_dline(DLine *pA, DLine *pB){
return pA->h==pB->h && memcmp(pA->z,pB->z,pA->h & LENGTH_MASK)==0;
|
| ︙ | ︙ | |||
239 240 241 242 243 244 245 | blob_append(pOut, "\n", 1); } /* ** Add two line numbers to the beginning of an output line for a context ** diff. One or of the other of the two numbers might be zero, which means ** to leave that number field blank. The "html" parameter means to format | | | 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
blob_append(pOut, "\n", 1);
}
/*
** Add two line numbers to the beginning of an output line for a context
** diff. One or of the other of the two numbers might be zero, which means
** to leave that number field blank. The "html" parameter means to format
** the output for HTML.
*/
static void appendDiffLineno(Blob *pOut, int lnA, int lnB, int html){
if( html ) blob_append(pOut, "<span class=\"diffln\">", -1);
if( lnA>0 ){
blob_appendf(pOut, "%6d ", lnA);
}else{
blob_append(pOut, " ", 7);
|
| ︙ | ︙ | |||
269 270 271 272 273 274 275 |
DContext *p, /* The difference */
Blob *pOut, /* Output a context diff to here */
int nContext, /* Number of lines of context */
int showLn, /* Show line numbers */
int html /* Render as HTML */
){
DLine *A; /* Left side of the diff */
| | | 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
DContext *p, /* The difference */
Blob *pOut, /* Output a context diff to here */
int nContext, /* Number of lines of context */
int showLn, /* Show line numbers */
int html /* Render as HTML */
){
DLine *A; /* Left side of the diff */
DLine *B; /* Right side of the diff */
int a = 0; /* Index of next line in A[] */
int b = 0; /* Index of next line in B[] */
int *R; /* Array of COPY/DELETE/INSERT triples */
int r; /* Index into R[] */
int nr; /* Number of COPY/DELETE/INSERT triples to process */
int mxr; /* Maximum value for r */
int na, nb; /* Number of lines shown from A and B */
|
| ︙ | ︙ | |||
525 526 527 528 529 530 531 | sqlite3_snprintf(7, &p->zLine[p->n], "%5d ", ln+1); p->n += 6; sbsWriteHtml(p, "</span>"); p->zLine[p->n++] = ' '; } /* | | | 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 | sqlite3_snprintf(7, &p->zLine[p->n], "%5d ", ln+1); p->n += 6; sbsWriteHtml(p, "</span>"); p->zLine[p->n++] = ' '; } /* ** The two text segments zLeft and zRight are known to be different on ** both ends, but they might have a common segment in the middle. If ** they do not have a common segment, return 0. If they do have a large ** common segment, return 1 and before doing so set: ** ** aLCS[0] = start of the common segment in zLeft ** aLCS[1] = end of the common segment in zLeft ** aLCS[2] = start of the common segment in zLeft |
| ︙ | ︙ | |||
710 711 712 713 714 715 716 |
if( p->iStart==p->iEnd ){
p->iStart = p->iStart2;
p->iEnd = p->iEnd2;
p->zStart = p->zStart2;
p->iStart2 = 0;
p->iEnd2 = 0;
}
| | | 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 |
if( p->iStart==p->iEnd ){
p->iStart = p->iStart2;
p->iEnd = p->iEnd2;
p->zStart = p->zStart2;
p->iStart2 = 0;
p->iEnd2 = 0;
}
if( p->iStart==p->iEnd ) p->iStart = p->iEnd = -1;
sbsWriteText(p, pRight, SBS_NEWLINE);
return;
}
/* If all else fails, show a single big change between left and right */
sbsWriteLineno(p, lnLeft);
p->iStart2 = p->iEnd2 = 0;
|
| ︙ | ︙ | |||
926 927 928 929 930 931 932 |
DContext *p, /* The computed diff */
Blob *pOut, /* Write the results here */
int nContext, /* Number of lines of context around each change */
int width, /* Width of each column of output */
int escHtml /* True to generate HTML output */
){
DLine *A; /* Left side of the diff */
| | | 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 |
DContext *p, /* The computed diff */
Blob *pOut, /* Write the results here */
int nContext, /* Number of lines of context around each change */
int width, /* Width of each column of output */
int escHtml /* True to generate HTML output */
){
DLine *A; /* Left side of the diff */
DLine *B; /* Right side of the diff */
int a = 0; /* Index of next line in A[] */
int b = 0; /* Index of next line in B[] */
int *R; /* Array of COPY/DELETE/INSERT triples */
int r; /* Index into R[] */
int nr; /* Number of COPY/DELETE/INSERT triples to process */
int mxr; /* Maximum value for r */
int na, nb; /* Number of lines shown from A and B */
|
| ︙ | ︙ | |||
1022 1023 1024 1025 1026 1027 1028 |
/* Show the differences */
for(i=0; i<nr; i++){
unsigned char *alignment;
ma = R[r+i*3+1]; /* Lines on left but not on right */
mb = R[r+i*3+2]; /* Lines on right but not on left */
/* If the gap between the current diff and then next diff within the
| | | 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 |
/* Show the differences */
for(i=0; i<nr; i++){
unsigned char *alignment;
ma = R[r+i*3+1]; /* Lines on left but not on right */
mb = R[r+i*3+2]; /* Lines on right but not on left */
/* If the gap between the current diff and then next diff within the
** same block is not too great, then render them as if they are a
** single diff. */
while( i<nr-1 && smallGap(&R[r+i*3]) ){
i++;
m = R[r+i*3];
ma += R[r+i*3+1] + m;
mb += R[r+i*3+2] + m;
}
|
| ︙ | ︙ | |||
1168 1169 1170 1171 1172 1173 1174 | ** If there are two or more possible answers of the same length, the ** returned sequence should be the one closest to the center of the ** input range. ** ** Ideally, the common sequence should be the longest possible common ** sequence. However, an exact computation of LCS is O(N*N) which is ** way too slow for larger files. So this routine uses an O(N) | | | 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 | ** If there are two or more possible answers of the same length, the ** returned sequence should be the one closest to the center of the ** input range. ** ** Ideally, the common sequence should be the longest possible common ** sequence. However, an exact computation of LCS is O(N*N) which is ** way too slow for larger files. So this routine uses an O(N) ** heuristic approximation based on hashing that usually works about ** as well. But if the O(N) algorithm doesn't get a good solution ** and N is not too large, we fall back to an exact solution by ** calling optimalLCS(). */ static void longestCommonSequence( DContext *p, /* Two files being compared */ int iS1, int iE1, /* Range of lines in p->aFrom[] */ |
| ︙ | ︙ | |||
1201 1202 1203 1204 1205 1206 1207 |
iEXb = iEXp = iS1;
iSYb = iSYp = iS2;
iEYb = iEYp = iS2;
mid = (iE1 + iS1)/2;
for(i=iS1; i<iE1; i++){
int limit = 0;
j = p->aTo[p->aFrom[i].h % p->nTo].iHash;
| | | 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 |
iEXb = iEXp = iS1;
iSYb = iSYp = iS2;
iEYb = iEYp = iS2;
mid = (iE1 + iS1)/2;
for(i=iS1; i<iE1; i++){
int limit = 0;
j = p->aTo[p->aFrom[i].h % p->nTo].iHash;
while( j>0
&& (j-1<iS2 || j>=iE2 || !same_dline(&p->aFrom[i], &p->aTo[j-1]))
){
if( limit++ > 10 ){
j = 0;
break;
}
j = p->aTo[j-1].iNext;
|
| ︙ | ︙ | |||
1258 1259 1260 1261 1262 1263 1264 |
optimalLCS(p, iS1, iE1, iS2, iE2, piSX, piEX, piSY, piEY);
}else{
*piSX = iSXb;
*piSY = iSYb;
*piEX = iEXb;
*piEY = iEYb;
}
| | | 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 |
optimalLCS(p, iS1, iE1, iS2, iE2, piSX, piEX, piSY, piEY);
}else{
*piSX = iSXb;
*piSY = iSYb;
*piEX = iEXb;
*piEY = iEYb;
}
/* printf("LCS(%d..%d/%d..%d) = %d..%d/%d..%d\n",
iS1, iE1, iS2, iE2, *piSX, *piEX, *piSY, *piEY); */
}
/*
** Expand the size of aEdit[] array to hold at least nEdit elements.
*/
static void expandEdit(DContext *p, int nEdit){
|
| ︙ | ︙ | |||
1293 1294 1295 1296 1297 1298 1299 |
return;
}
}
if( nCopy==0 && nDel==0 ){
p->aEdit[p->nEdit-1] += nIns;
return;
}
| | | 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 |
return;
}
}
if( nCopy==0 && nDel==0 ){
p->aEdit[p->nEdit-1] += nIns;
return;
}
}
if( p->nEdit+3>p->nEditAlloc ){
expandEdit(p, p->nEdit*2 + 15);
if( p->aEdit==0 ) return;
}
p->aEdit[p->nEdit++] = nCopy;
p->aEdit[p->nEdit++] = nDel;
p->aEdit[p->nEdit++] = nIns;
|
| ︙ | ︙ | |||
1521 1522 1523 1524 1525 1526 1527 | return w; } /* ** Generate a report of the differences between files pA and pB. ** If pOut is not NULL then a unified diff is appended there. It ** is assumed that pOut has already been initialized. If pOut is | | | | 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 |
return w;
}
/*
** Generate a report of the differences between files pA and pB.
** If pOut is not NULL then a unified diff is appended there. It
** is assumed that pOut has already been initialized. If pOut is
** NULL, then a pointer to an array of integers is returned.
** The integers come in triples. For each triple,
** the elements are the number of lines copied, the number of
** lines deleted, and the number of lines inserted. The vector
** is terminated by a triple of all zeros.
**
** This diff utility does not work on binary files. If a binary
** file is encountered, 0 is returned and pOut is written with
** text "cannot compute difference between binary files".
*/
int *text_diff(
Blob *pA_Blob, /* FROM file */
Blob *pB_Blob, /* TO file */
Blob *pOut, /* Write diff here if not NULL */
u64 diffFlags /* DIFF_* flags defined above */
){
int ignoreEolWs; /* Ignore whitespace at the end of lines */
int nContext; /* Amount of context to display */
DContext c;
if( diffFlags & DIFF_INVERT ){
Blob *pTemp = pA_Blob;
pA_Blob = pB_Blob;
pB_Blob = pTemp;
}
|
| ︙ | ︙ | |||
1594 1595 1596 1597 1598 1599 1600 |
free(c.aTo);
return c.aEdit;
}
}
/*
** Process diff-related command-line options and return an appropriate
| | | 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 |
free(c.aTo);
return c.aEdit;
}
}
/*
** Process diff-related command-line options and return an appropriate
** "diffFlags" integer.
**
** --brief Show filenames only DIFF_BRIEF
** --context|-c N N lines of context. DIFF_CONTEXT_MASK
** --html Format for HTML DIFF_HTML
** --invert Invert the diff DIFF_INVERT
** --linenum|-n Show line numbers DIFF_LINENO
** --noopt Disable optimization DIFF_NOOPT
|
| ︙ | ︙ | |||
1765 1766 1767 1768 1769 1770 1771 | /* Clear out the diff results */ fossil_free(p->c.aEdit); p->c.aEdit = 0; p->c.nEdit = 0; p->c.nEditAlloc = 0; /* Clear out the from file */ | | | 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 | /* Clear out the diff results */ fossil_free(p->c.aEdit); p->c.aEdit = 0; p->c.nEdit = 0; p->c.nEditAlloc = 0; /* Clear out the from file */ free(p->c.aFrom); /* Return no errors */ return 0; } /* |
| ︙ | ︙ | |||
1836 1837 1838 1839 1840 1841 1842 |
fossil_panic("unable to retrieve content of artifact #%d", rid);
}
db_multi_exec("CREATE TEMP TABLE ok(rid INTEGER PRIMARY KEY)");
if( iLimit<=0 ) iLimit = 1000000000;
compute_direct_ancestors(mid, iLimit);
annotation_start(p, &toAnnotate);
| | | 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 |
fossil_panic("unable to retrieve content of artifact #%d", rid);
}
db_multi_exec("CREATE TEMP TABLE ok(rid INTEGER PRIMARY KEY)");
if( iLimit<=0 ) iLimit = 1000000000;
compute_direct_ancestors(mid, iLimit);
annotation_start(p, &toAnnotate);
db_prepare(&q,
"SELECT mlink.fid,"
" (SELECT uuid FROM blob WHERE rid=mlink.%s),"
" date(event.mtime), "
" coalesce(event.euser,event.user) "
" FROM ancestor, mlink, event"
" WHERE mlink.fnid=%d"
" AND mlink.mid=ancestor.rid"
|
| ︙ | ︙ | |||
1858 1859 1860 1861 1862 1863 1864 |
while( db_step(&q)==SQLITE_ROW ){
int pid = db_column_int(&q, 0);
const char *zUuid = db_column_text(&q, 1);
const char *zDate = db_column_text(&q, 2);
const char *zUser = db_column_text(&q, 3);
if( webLabel ){
zLabel = mprintf(
| | | 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 |
while( db_step(&q)==SQLITE_ROW ){
int pid = db_column_int(&q, 0);
const char *zUuid = db_column_text(&q, 1);
const char *zDate = db_column_text(&q, 2);
const char *zUser = db_column_text(&q, 3);
if( webLabel ){
zLabel = mprintf(
"<a href='%R/info/%s' target='infowindow'>%.10s</a> %s %13.13s",
zUuid, zUuid, zDate, zUser
);
}else{
zLabel = mprintf("%.10s %s %13.13s", zUuid, zDate, zUser);
}
p->nVers++;
p->azVers = fossil_realloc(p->azVers, p->nVers*sizeof(p->azVers[0]) );
|
| ︙ | ︙ | |||
1993 1994 1995 1996 1997 1998 1999 |
if( showLog ){
for(i=0; i<ann.nVers; i++){
printf("version %3d: %s\n", i+1, ann.azVers[i]);
}
printf("---------------------------------------------------\n");
}
for(i=0; i<ann.nOrig; i++){
| | | 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 |
if( showLog ){
for(i=0; i<ann.nVers; i++){
printf("version %3d: %s\n", i+1, ann.azVers[i]);
}
printf("---------------------------------------------------\n");
}
for(i=0; i<ann.nOrig; i++){
fossil_print("%s: %.*s\n",
ann.aOrig[i].zSrc, ann.aOrig[i].n, ann.aOrig[i].z);
}
}
|