Fossil SCM

If the test-markdown-render or test-wiki-render commands are invoked without a repository in which to check for Wiki page names and artifact hashes, then substitute a temporary, empty, in-memory repository so that the commands will still work and won't give SQL errors.

drh 2019-08-23 12:23 trunk
Commit 0ac64dad806cabeb436439db59380bb3ddf6464d61cef96d11eaec46be93d62a
3 files changed +19 -5 +1 -1 +1 -1
+19 -5
--- src/db.c
+++ src/db.c
@@ -117,10 +117,11 @@
117117
/*
118118
** Arrange for the given file to be deleted on a failure.
119119
*/
120120
void db_delete_on_failure(const char *zFilename){
121121
assert( db.nDeleteOnFail<count(db.azDeleteOnFail) );
122
+ if( zFilename==0 ) return;
122123
db.azDeleteOnFail[db.nDeleteOnFail++] = fossil_strdup(zFilename);
123124
}
124125
125126
/*
126127
** Return the transaction nesting depth. 0 means we are currently
@@ -799,10 +800,13 @@
799800
}
800801
801802
/*
802803
** Initialize a new database file with the given schema. If anything
803804
** goes wrong, call db_err() to exit.
805
+**
806
+** If zFilename is NULL, then create an empty repository in an in-memory
807
+** database.
804808
*/
805809
void db_init_database(
806810
const char *zFileName, /* Name of database file to create */
807811
const char *zSchema, /* First part of schema */
808812
... /* Additional SQL to run. Terminate with NULL. */
@@ -810,11 +814,11 @@
810814
sqlite3 *db;
811815
int rc;
812816
const char *zSql;
813817
va_list ap;
814818
815
- db = db_open(zFileName);
819
+ db = db_open(zFileName ? zFileName : ":memory:");
816820
sqlite3_exec(db, "BEGIN EXCLUSIVE", 0, 0, 0);
817821
rc = sqlite3_exec(db, zSchema, 0, 0, 0);
818822
if( rc!=SQLITE_OK ){
819823
db_err("%s", sqlite3_errmsg(db));
820824
}
@@ -825,11 +829,15 @@
825829
db_err("%s", sqlite3_errmsg(db));
826830
}
827831
}
828832
va_end(ap);
829833
sqlite3_exec(db, "COMMIT", 0, 0, 0);
830
- sqlite3_close(db);
834
+ if( zFileName || g.db!=0 ){
835
+ sqlite3_close(db);
836
+ }else{
837
+ g.db = db;
838
+ }
831839
}
832840
833841
/*
834842
** Function to return the number of seconds since 1970. This is
835843
** the same as strftime('%s','now') but is more compact.
@@ -1768,12 +1776,13 @@
17681776
17691777
/*
17701778
** Flags for the db_find_and_open_repository() function.
17711779
*/
17721780
#if INTERFACE
1773
-#define OPEN_OK_NOT_FOUND 0x001 /* Do not error out if not found */
1774
-#define OPEN_ANY_SCHEMA 0x002 /* Do not error if schema is wrong */
1781
+#define OPEN_OK_NOT_FOUND 0x001 /* Do not error out if not found */
1782
+#define OPEN_ANY_SCHEMA 0x002 /* Do not error if schema is wrong */
1783
+#define OPEN_SUBSTITUTE 0x004 /* Fake in-memory repo if not found */
17751784
#endif
17761785
17771786
/*
17781787
** Try to find the repository and open it. Use the -R or --repository
17791788
** option to locate the repository. If no such option is available, then
@@ -1802,11 +1811,16 @@
18021811
if( g.repositoryOpen ){
18031812
if( (bFlags & OPEN_ANY_SCHEMA)==0 ) db_verify_schema();
18041813
return;
18051814
}
18061815
rep_not_found:
1807
- if( (bFlags & OPEN_OK_NOT_FOUND)==0 ){
1816
+ if( bFlags & OPEN_OK_NOT_FOUND ){
1817
+ /* No errors if the database is not found */
1818
+ if( bFlags & OPEN_SUBSTITUTE ){
1819
+ db_create_repository(0);
1820
+ }
1821
+ }else{
18081822
#ifdef FOSSIL_ENABLE_JSON
18091823
g.json.resultCode = FSL_JSON_E_DB_NOT_FOUND;
18101824
#endif
18111825
if( nArgUsed==0 ){
18121826
fossil_fatal("use --repository or -R to specify the repository database");
18131827
--- src/db.c
+++ src/db.c
@@ -117,10 +117,11 @@
117 /*
118 ** Arrange for the given file to be deleted on a failure.
119 */
120 void db_delete_on_failure(const char *zFilename){
121 assert( db.nDeleteOnFail<count(db.azDeleteOnFail) );
 
122 db.azDeleteOnFail[db.nDeleteOnFail++] = fossil_strdup(zFilename);
123 }
124
125 /*
126 ** Return the transaction nesting depth. 0 means we are currently
@@ -799,10 +800,13 @@
799 }
800
801 /*
802 ** Initialize a new database file with the given schema. If anything
803 ** goes wrong, call db_err() to exit.
 
 
 
804 */
805 void db_init_database(
806 const char *zFileName, /* Name of database file to create */
807 const char *zSchema, /* First part of schema */
808 ... /* Additional SQL to run. Terminate with NULL. */
@@ -810,11 +814,11 @@
810 sqlite3 *db;
811 int rc;
812 const char *zSql;
813 va_list ap;
814
815 db = db_open(zFileName);
816 sqlite3_exec(db, "BEGIN EXCLUSIVE", 0, 0, 0);
817 rc = sqlite3_exec(db, zSchema, 0, 0, 0);
818 if( rc!=SQLITE_OK ){
819 db_err("%s", sqlite3_errmsg(db));
820 }
@@ -825,11 +829,15 @@
825 db_err("%s", sqlite3_errmsg(db));
826 }
827 }
828 va_end(ap);
829 sqlite3_exec(db, "COMMIT", 0, 0, 0);
830 sqlite3_close(db);
 
 
 
 
831 }
832
833 /*
834 ** Function to return the number of seconds since 1970. This is
835 ** the same as strftime('%s','now') but is more compact.
@@ -1768,12 +1776,13 @@
1768
1769 /*
1770 ** Flags for the db_find_and_open_repository() function.
1771 */
1772 #if INTERFACE
1773 #define OPEN_OK_NOT_FOUND 0x001 /* Do not error out if not found */
1774 #define OPEN_ANY_SCHEMA 0x002 /* Do not error if schema is wrong */
 
1775 #endif
1776
1777 /*
1778 ** Try to find the repository and open it. Use the -R or --repository
1779 ** option to locate the repository. If no such option is available, then
@@ -1802,11 +1811,16 @@
1802 if( g.repositoryOpen ){
1803 if( (bFlags & OPEN_ANY_SCHEMA)==0 ) db_verify_schema();
1804 return;
1805 }
1806 rep_not_found:
1807 if( (bFlags & OPEN_OK_NOT_FOUND)==0 ){
 
 
 
 
 
1808 #ifdef FOSSIL_ENABLE_JSON
1809 g.json.resultCode = FSL_JSON_E_DB_NOT_FOUND;
1810 #endif
1811 if( nArgUsed==0 ){
1812 fossil_fatal("use --repository or -R to specify the repository database");
1813
--- src/db.c
+++ src/db.c
@@ -117,10 +117,11 @@
117 /*
118 ** Arrange for the given file to be deleted on a failure.
119 */
120 void db_delete_on_failure(const char *zFilename){
121 assert( db.nDeleteOnFail<count(db.azDeleteOnFail) );
122 if( zFilename==0 ) return;
123 db.azDeleteOnFail[db.nDeleteOnFail++] = fossil_strdup(zFilename);
124 }
125
126 /*
127 ** Return the transaction nesting depth. 0 means we are currently
@@ -799,10 +800,13 @@
800 }
801
802 /*
803 ** Initialize a new database file with the given schema. If anything
804 ** goes wrong, call db_err() to exit.
805 **
806 ** If zFilename is NULL, then create an empty repository in an in-memory
807 ** database.
808 */
809 void db_init_database(
810 const char *zFileName, /* Name of database file to create */
811 const char *zSchema, /* First part of schema */
812 ... /* Additional SQL to run. Terminate with NULL. */
@@ -810,11 +814,11 @@
814 sqlite3 *db;
815 int rc;
816 const char *zSql;
817 va_list ap;
818
819 db = db_open(zFileName ? zFileName : ":memory:");
820 sqlite3_exec(db, "BEGIN EXCLUSIVE", 0, 0, 0);
821 rc = sqlite3_exec(db, zSchema, 0, 0, 0);
822 if( rc!=SQLITE_OK ){
823 db_err("%s", sqlite3_errmsg(db));
824 }
@@ -825,11 +829,15 @@
829 db_err("%s", sqlite3_errmsg(db));
830 }
831 }
832 va_end(ap);
833 sqlite3_exec(db, "COMMIT", 0, 0, 0);
834 if( zFileName || g.db!=0 ){
835 sqlite3_close(db);
836 }else{
837 g.db = db;
838 }
839 }
840
841 /*
842 ** Function to return the number of seconds since 1970. This is
843 ** the same as strftime('%s','now') but is more compact.
@@ -1768,12 +1776,13 @@
1776
1777 /*
1778 ** Flags for the db_find_and_open_repository() function.
1779 */
1780 #if INTERFACE
1781 #define OPEN_OK_NOT_FOUND 0x001 /* Do not error out if not found */
1782 #define OPEN_ANY_SCHEMA 0x002 /* Do not error if schema is wrong */
1783 #define OPEN_SUBSTITUTE 0x004 /* Fake in-memory repo if not found */
1784 #endif
1785
1786 /*
1787 ** Try to find the repository and open it. Use the -R or --repository
1788 ** option to locate the repository. If no such option is available, then
@@ -1802,11 +1811,16 @@
1811 if( g.repositoryOpen ){
1812 if( (bFlags & OPEN_ANY_SCHEMA)==0 ) db_verify_schema();
1813 return;
1814 }
1815 rep_not_found:
1816 if( bFlags & OPEN_OK_NOT_FOUND ){
1817 /* No errors if the database is not found */
1818 if( bFlags & OPEN_SUBSTITUTE ){
1819 db_create_repository(0);
1820 }
1821 }else{
1822 #ifdef FOSSIL_ENABLE_JSON
1823 g.json.resultCode = FSL_JSON_E_DB_NOT_FOUND;
1824 #endif
1825 if( nArgUsed==0 ){
1826 fossil_fatal("use --repository or -R to specify the repository database");
1827
+1 -1
--- src/wiki.c
+++ src/wiki.c
@@ -1629,11 +1629,11 @@
16291629
** Render markdown wiki from FILE to stdout.
16301630
**
16311631
*/
16321632
void test_markdown_render(void){
16331633
Blob in, out;
1634
- db_find_and_open_repository(0,0);
1634
+ db_find_and_open_repository(OPEN_OK_NOT_FOUND|OPEN_SUBSTITUTE,0);
16351635
verify_all_options();
16361636
if( g.argc!=3 ) usage("FILE");
16371637
blob_zero(&out);
16381638
blob_read_from_file(&in, g.argv[2], ExtFILE);
16391639
markdown_to_html(&in, 0, &out);
16401640
--- src/wiki.c
+++ src/wiki.c
@@ -1629,11 +1629,11 @@
1629 ** Render markdown wiki from FILE to stdout.
1630 **
1631 */
1632 void test_markdown_render(void){
1633 Blob in, out;
1634 db_find_and_open_repository(0,0);
1635 verify_all_options();
1636 if( g.argc!=3 ) usage("FILE");
1637 blob_zero(&out);
1638 blob_read_from_file(&in, g.argv[2], ExtFILE);
1639 markdown_to_html(&in, 0, &out);
1640
--- src/wiki.c
+++ src/wiki.c
@@ -1629,11 +1629,11 @@
1629 ** Render markdown wiki from FILE to stdout.
1630 **
1631 */
1632 void test_markdown_render(void){
1633 Blob in, out;
1634 db_find_and_open_repository(OPEN_OK_NOT_FOUND|OPEN_SUBSTITUTE,0);
1635 verify_all_options();
1636 if( g.argc!=3 ) usage("FILE");
1637 blob_zero(&out);
1638 blob_read_from_file(&in, g.argv[2], ExtFILE);
1639 markdown_to_html(&in, 0, &out);
1640
--- src/wikiformat.c
+++ src/wikiformat.c
@@ -1794,11 +1794,11 @@
17941794
if( find_option("htmlonly",0,0)!=0 ) flags |= WIKI_HTMLONLY;
17951795
if( find_option("linksonly",0,0)!=0 ) flags |= WIKI_LINKSONLY;
17961796
if( find_option("nobadlinks",0,0)!=0 ) flags |= WIKI_NOBADLINKS;
17971797
if( find_option("inline",0,0)!=0 ) flags |= WIKI_INLINE;
17981798
if( find_option("noblock",0,0)!=0 ) flags |= WIKI_NOBLOCK;
1799
- db_find_and_open_repository(0,0);
1799
+ db_find_and_open_repository(OPEN_OK_NOT_FOUND|OPEN_SUBSTITUTE,0);
18001800
verify_all_options();
18011801
if( g.argc!=3 ) usage("FILE");
18021802
blob_zero(&out);
18031803
blob_read_from_file(&in, g.argv[2], ExtFILE);
18041804
wiki_convert(&in, &out, flags);
18051805
--- src/wikiformat.c
+++ src/wikiformat.c
@@ -1794,11 +1794,11 @@
1794 if( find_option("htmlonly",0,0)!=0 ) flags |= WIKI_HTMLONLY;
1795 if( find_option("linksonly",0,0)!=0 ) flags |= WIKI_LINKSONLY;
1796 if( find_option("nobadlinks",0,0)!=0 ) flags |= WIKI_NOBADLINKS;
1797 if( find_option("inline",0,0)!=0 ) flags |= WIKI_INLINE;
1798 if( find_option("noblock",0,0)!=0 ) flags |= WIKI_NOBLOCK;
1799 db_find_and_open_repository(0,0);
1800 verify_all_options();
1801 if( g.argc!=3 ) usage("FILE");
1802 blob_zero(&out);
1803 blob_read_from_file(&in, g.argv[2], ExtFILE);
1804 wiki_convert(&in, &out, flags);
1805
--- src/wikiformat.c
+++ src/wikiformat.c
@@ -1794,11 +1794,11 @@
1794 if( find_option("htmlonly",0,0)!=0 ) flags |= WIKI_HTMLONLY;
1795 if( find_option("linksonly",0,0)!=0 ) flags |= WIKI_LINKSONLY;
1796 if( find_option("nobadlinks",0,0)!=0 ) flags |= WIKI_NOBADLINKS;
1797 if( find_option("inline",0,0)!=0 ) flags |= WIKI_INLINE;
1798 if( find_option("noblock",0,0)!=0 ) flags |= WIKI_NOBLOCK;
1799 db_find_and_open_repository(OPEN_OK_NOT_FOUND|OPEN_SUBSTITUTE,0);
1800 verify_all_options();
1801 if( g.argc!=3 ) usage("FILE");
1802 blob_zero(&out);
1803 blob_read_from_file(&in, g.argv[2], ExtFILE);
1804 wiki_convert(&in, &out, flags);
1805

Keyboard Shortcuts

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