Fossil SCM
Moved the core logic of both "rebuild_database" and "create_repository_cmd" into their own functions, for sharing with "reconstruct_cmd".
Commit
e00384d26d5e24bc41daf1788352e38dd85968c9
Parent
033ad72c712b7b9…
2 files changed
+43
-27
+30
-18
M
src/db.c
+43
-27
| --- src/db.c | ||
| +++ src/db.c | ||
| @@ -642,32 +642,26 @@ | ||
| 642 | 642 | zRepositorySchema2, |
| 643 | 643 | (char*)0 |
| 644 | 644 | ); |
| 645 | 645 | } |
| 646 | 646 | |
| 647 | - | |
| 648 | 647 | /* |
| 649 | -** COMMAND: new | |
| 648 | +** Fill an empty repository database with the basic information for a | |
| 649 | +** repository. This function is shared between 'create_repository_cmd' | |
| 650 | +** ('new') and 'reconstruct_cmd' ('reconstruct'), both of which create | |
| 651 | +** new repositories. | |
| 650 | 652 | ** |
| 651 | -** Usage: %fossil new FILENAME | |
| 652 | -** Create a repository for a new project in the file named FILENAME. | |
| 653 | -** This command is distinct from "clone". The "clone" command makes | |
| 654 | -** a copy of an existing project. This command starts a new project. | |
| 653 | +** The caller determines wheter the function inserts an empty root | |
| 654 | +** manifest (zRoot == TRUE), or not (zRoot == FALSE). | |
| 655 | 655 | */ |
| 656 | -void create_repository_cmd(void){ | |
| 656 | + | |
| 657 | +void db_initial_setup (int zRoot){ | |
| 657 | 658 | char *zDate; |
| 658 | 659 | char *zUser; |
| 659 | 660 | Blob hash; |
| 660 | 661 | Blob manifest; |
| 661 | 662 | |
| 662 | - if( g.argc!=3 ){ | |
| 663 | - usage("REPOSITORY-NAME"); | |
| 664 | - } | |
| 665 | - db_create_repository(g.argv[2]); | |
| 666 | - db_open_repository(g.argv[2]); | |
| 667 | - db_open_config(); | |
| 668 | - db_begin_transaction(); | |
| 669 | 663 | db_set("content-schema", CONTENT_SCHEMA); |
| 670 | 664 | db_set("aux-schema", AUX_SCHEMA); |
| 671 | 665 | db_set_int("authenticate-localhost", 0); |
| 672 | 666 | db_multi_exec( |
| 673 | 667 | "INSERT INTO config(name,value) VALUES('server-code', hex(randomblob(20)));" |
| @@ -689,23 +683,45 @@ | ||
| 689 | 683 | " VALUES('anonymous','anonymous','hjkorw','Anon');" |
| 690 | 684 | "INSERT INTO user(login,pw,cap,info)" |
| 691 | 685 | " VALUES('nobody','','jor','Nobody');" |
| 692 | 686 | ); |
| 693 | 687 | user_select(); |
| 694 | - blob_zero(&manifest); | |
| 695 | - blob_appendf(&manifest, "C initial\\sempty\\sbaseline\n"); | |
| 696 | - zDate = db_text(0, "SELECT datetime('now')"); | |
| 697 | - zDate[10]='T'; | |
| 698 | - blob_appendf(&manifest, "D %s\n", zDate); | |
| 699 | - blob_appendf(&manifest, "P\n"); | |
| 700 | - md5sum_init(); | |
| 701 | - blob_appendf(&manifest, "R %s\n", md5sum_finish(0)); | |
| 702 | - blob_appendf(&manifest, "U %F\n", g.zLogin); | |
| 703 | - md5sum_blob(&manifest, &hash); | |
| 704 | - blob_appendf(&manifest, "Z %b\n", &hash); | |
| 705 | - blob_reset(&hash); | |
| 706 | - content_put(&manifest, 0, 0); | |
| 688 | + | |
| 689 | + if (zRoot){ | |
| 690 | + blob_zero(&manifest); | |
| 691 | + blob_appendf(&manifest, "C initial\\sempty\\sbaseline\n"); | |
| 692 | + zDate = db_text(0, "SELECT datetime('now')"); | |
| 693 | + zDate[10]='T'; | |
| 694 | + blob_appendf(&manifest, "D %s\n", zDate); | |
| 695 | + blob_appendf(&manifest, "P\n"); | |
| 696 | + md5sum_init(); | |
| 697 | + blob_appendf(&manifest, "R %s\n", md5sum_finish(0)); | |
| 698 | + blob_appendf(&manifest, "U %F\n", g.zLogin); | |
| 699 | + md5sum_blob(&manifest, &hash); | |
| 700 | + blob_appendf(&manifest, "Z %b\n", &hash); | |
| 701 | + blob_reset(&hash); | |
| 702 | + content_put(&manifest, 0, 0); | |
| 703 | + } | |
| 704 | +} | |
| 705 | + | |
| 706 | +/* | |
| 707 | +** COMMAND: new | |
| 708 | +** | |
| 709 | +** Usage: %fossil new FILENAME | |
| 710 | +** Create a repository for a new project in the file named FILENAME. | |
| 711 | +** This command is distinct from "clone". The "clone" command makes | |
| 712 | +** a copy of an existing project. This command starts a new project. | |
| 713 | +*/ | |
| 714 | +void create_repository_cmd(void){ | |
| 715 | + if( g.argc!=3 ){ | |
| 716 | + usage("REPOSITORY-NAME"); | |
| 717 | + } | |
| 718 | + db_create_repository(g.argv[2]); | |
| 719 | + db_open_repository(g.argv[2]); | |
| 720 | + db_open_config(); | |
| 721 | + db_begin_transaction(); | |
| 722 | + db_initial_setup (1); | |
| 707 | 723 | db_end_transaction(0); |
| 708 | 724 | printf("project-id: %s\n", db_get("project-code", 0)); |
| 709 | 725 | printf("server-id: %s\n", db_get("server-code", 0)); |
| 710 | 726 | printf("admin-user: %s (no password set yet!)\n", g.zLogin); |
| 711 | 727 | printf("baseline: %s\n", db_text(0, "SELECT uuid FROM blob")); |
| 712 | 728 |
| --- src/db.c | |
| +++ src/db.c | |
| @@ -642,32 +642,26 @@ | |
| 642 | zRepositorySchema2, |
| 643 | (char*)0 |
| 644 | ); |
| 645 | } |
| 646 | |
| 647 | |
| 648 | /* |
| 649 | ** COMMAND: new |
| 650 | ** |
| 651 | ** Usage: %fossil new FILENAME |
| 652 | ** Create a repository for a new project in the file named FILENAME. |
| 653 | ** This command is distinct from "clone". The "clone" command makes |
| 654 | ** a copy of an existing project. This command starts a new project. |
| 655 | */ |
| 656 | void create_repository_cmd(void){ |
| 657 | char *zDate; |
| 658 | char *zUser; |
| 659 | Blob hash; |
| 660 | Blob manifest; |
| 661 | |
| 662 | if( g.argc!=3 ){ |
| 663 | usage("REPOSITORY-NAME"); |
| 664 | } |
| 665 | db_create_repository(g.argv[2]); |
| 666 | db_open_repository(g.argv[2]); |
| 667 | db_open_config(); |
| 668 | db_begin_transaction(); |
| 669 | db_set("content-schema", CONTENT_SCHEMA); |
| 670 | db_set("aux-schema", AUX_SCHEMA); |
| 671 | db_set_int("authenticate-localhost", 0); |
| 672 | db_multi_exec( |
| 673 | "INSERT INTO config(name,value) VALUES('server-code', hex(randomblob(20)));" |
| @@ -689,23 +683,45 @@ | |
| 689 | " VALUES('anonymous','anonymous','hjkorw','Anon');" |
| 690 | "INSERT INTO user(login,pw,cap,info)" |
| 691 | " VALUES('nobody','','jor','Nobody');" |
| 692 | ); |
| 693 | user_select(); |
| 694 | blob_zero(&manifest); |
| 695 | blob_appendf(&manifest, "C initial\\sempty\\sbaseline\n"); |
| 696 | zDate = db_text(0, "SELECT datetime('now')"); |
| 697 | zDate[10]='T'; |
| 698 | blob_appendf(&manifest, "D %s\n", zDate); |
| 699 | blob_appendf(&manifest, "P\n"); |
| 700 | md5sum_init(); |
| 701 | blob_appendf(&manifest, "R %s\n", md5sum_finish(0)); |
| 702 | blob_appendf(&manifest, "U %F\n", g.zLogin); |
| 703 | md5sum_blob(&manifest, &hash); |
| 704 | blob_appendf(&manifest, "Z %b\n", &hash); |
| 705 | blob_reset(&hash); |
| 706 | content_put(&manifest, 0, 0); |
| 707 | db_end_transaction(0); |
| 708 | printf("project-id: %s\n", db_get("project-code", 0)); |
| 709 | printf("server-id: %s\n", db_get("server-code", 0)); |
| 710 | printf("admin-user: %s (no password set yet!)\n", g.zLogin); |
| 711 | printf("baseline: %s\n", db_text(0, "SELECT uuid FROM blob")); |
| 712 |
| --- src/db.c | |
| +++ src/db.c | |
| @@ -642,32 +642,26 @@ | |
| 642 | zRepositorySchema2, |
| 643 | (char*)0 |
| 644 | ); |
| 645 | } |
| 646 | |
| 647 | /* |
| 648 | ** Fill an empty repository database with the basic information for a |
| 649 | ** repository. This function is shared between 'create_repository_cmd' |
| 650 | ** ('new') and 'reconstruct_cmd' ('reconstruct'), both of which create |
| 651 | ** new repositories. |
| 652 | ** |
| 653 | ** The caller determines wheter the function inserts an empty root |
| 654 | ** manifest (zRoot == TRUE), or not (zRoot == FALSE). |
| 655 | */ |
| 656 | |
| 657 | void db_initial_setup (int zRoot){ |
| 658 | char *zDate; |
| 659 | char *zUser; |
| 660 | Blob hash; |
| 661 | Blob manifest; |
| 662 | |
| 663 | db_set("content-schema", CONTENT_SCHEMA); |
| 664 | db_set("aux-schema", AUX_SCHEMA); |
| 665 | db_set_int("authenticate-localhost", 0); |
| 666 | db_multi_exec( |
| 667 | "INSERT INTO config(name,value) VALUES('server-code', hex(randomblob(20)));" |
| @@ -689,23 +683,45 @@ | |
| 683 | " VALUES('anonymous','anonymous','hjkorw','Anon');" |
| 684 | "INSERT INTO user(login,pw,cap,info)" |
| 685 | " VALUES('nobody','','jor','Nobody');" |
| 686 | ); |
| 687 | user_select(); |
| 688 | |
| 689 | if (zRoot){ |
| 690 | blob_zero(&manifest); |
| 691 | blob_appendf(&manifest, "C initial\\sempty\\sbaseline\n"); |
| 692 | zDate = db_text(0, "SELECT datetime('now')"); |
| 693 | zDate[10]='T'; |
| 694 | blob_appendf(&manifest, "D %s\n", zDate); |
| 695 | blob_appendf(&manifest, "P\n"); |
| 696 | md5sum_init(); |
| 697 | blob_appendf(&manifest, "R %s\n", md5sum_finish(0)); |
| 698 | blob_appendf(&manifest, "U %F\n", g.zLogin); |
| 699 | md5sum_blob(&manifest, &hash); |
| 700 | blob_appendf(&manifest, "Z %b\n", &hash); |
| 701 | blob_reset(&hash); |
| 702 | content_put(&manifest, 0, 0); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | /* |
| 707 | ** COMMAND: new |
| 708 | ** |
| 709 | ** Usage: %fossil new FILENAME |
| 710 | ** Create a repository for a new project in the file named FILENAME. |
| 711 | ** This command is distinct from "clone". The "clone" command makes |
| 712 | ** a copy of an existing project. This command starts a new project. |
| 713 | */ |
| 714 | void create_repository_cmd(void){ |
| 715 | if( g.argc!=3 ){ |
| 716 | usage("REPOSITORY-NAME"); |
| 717 | } |
| 718 | db_create_repository(g.argv[2]); |
| 719 | db_open_repository(g.argv[2]); |
| 720 | db_open_config(); |
| 721 | db_begin_transaction(); |
| 722 | db_initial_setup (1); |
| 723 | db_end_transaction(0); |
| 724 | printf("project-id: %s\n", db_get("project-code", 0)); |
| 725 | printf("server-id: %s\n", db_get("server-code", 0)); |
| 726 | printf("admin-user: %s (no password set yet!)\n", g.zLogin); |
| 727 | printf("baseline: %s\n", db_text(0, "SELECT uuid FROM blob")); |
| 728 |
+30
-18
| --- src/rebuild.c | ||
| +++ src/rebuild.c | ||
| @@ -25,33 +25,23 @@ | ||
| 25 | 25 | */ |
| 26 | 26 | #include "config.h" |
| 27 | 27 | #include "rebuild.h" |
| 28 | 28 | #include <assert.h> |
| 29 | 29 | |
| 30 | - | |
| 31 | 30 | /* |
| 32 | -** COMMAND: rebuild | |
| 33 | -** | |
| 34 | -** Usage: %fossil rebuild REPOSITORY | |
| 35 | -** | |
| 36 | -** Reconstruct the named repository database from the core | |
| 37 | -** records. Run this command after updating the fossil | |
| 38 | -** executable in a way that changes the database schema. | |
| 31 | +** Core function to rebuild the infomration in the derived tables of a | |
| 32 | +** fossil repository from the blobs. This function is shared between | |
| 33 | +** 'rebuild_database' ('rebuild') and 'reconstruct_cmd' | |
| 34 | +** ('reconstruct'), both of which have to regenerate this information | |
| 35 | +** from scratch. | |
| 39 | 36 | */ |
| 40 | -void rebuild_database(void){ | |
| 37 | + | |
| 38 | +int rebuild_db(void){ | |
| 41 | 39 | Stmt s; |
| 42 | - int errCnt; | |
| 43 | - int forceFlag; | |
| 40 | + int errCnt = 0; | |
| 44 | 41 | char *zTable; |
| 45 | 42 | |
| 46 | - forceFlag = find_option("force","f",0)!=0; | |
| 47 | - if( g.argc!=3 ){ | |
| 48 | - usage("REPOSITORY-FILENAME"); | |
| 49 | - } | |
| 50 | - errCnt = 0; | |
| 51 | - db_open_repository(g.argv[2]); | |
| 52 | - db_begin_transaction(); | |
| 53 | 43 | db_multi_exec( |
| 54 | 44 | "CREATE INDEX IF NOT EXISTS delta_i1 ON delta(srcid);" |
| 55 | 45 | ); |
| 56 | 46 | for(;;){ |
| 57 | 47 | zTable = db_text(0, |
| @@ -75,14 +65,36 @@ | ||
| 75 | 65 | blob_reset(&content); |
| 76 | 66 | }else{ |
| 77 | 67 | db_multi_exec("INSERT INTO phantom VALUES(%d)", rid); |
| 78 | 68 | } |
| 79 | 69 | } |
| 70 | + return errCnt; | |
| 71 | +} | |
| 72 | + | |
| 73 | +/* | |
| 74 | +** COMMAND: rebuild | |
| 75 | +** | |
| 76 | +** Usage: %fossil rebuild REPOSITORY | |
| 77 | +** | |
| 78 | +** Reconstruct the named repository database from the core | |
| 79 | +** records. Run this command after updating the fossil | |
| 80 | +** executable in a way that changes the database schema. | |
| 81 | +*/ | |
| 82 | +void rebuild_database(void){ | |
| 83 | + int forceFlag; | |
| 84 | + int errCnt; | |
| 80 | 85 | |
| 86 | + forceFlag = find_option("force","f",0)!=0; | |
| 87 | + if( g.argc!=3 ){ | |
| 88 | + usage("REPOSITORY-FILENAME"); | |
| 89 | + } | |
| 90 | + db_open_repository(g.argv[2]); | |
| 91 | + db_begin_transaction(); | |
| 92 | + errCnt = rebuild_db(); | |
| 81 | 93 | if( errCnt && !forceFlag ){ |
| 82 | 94 | printf("%d errors. Rolling back changes. Use --force to force a commit.\n", |
| 83 | 95 | errCnt); |
| 84 | 96 | db_end_transaction(1); |
| 85 | 97 | }else{ |
| 86 | 98 | db_end_transaction(0); |
| 87 | 99 | } |
| 88 | 100 | } |
| 89 | 101 |
| --- src/rebuild.c | |
| +++ src/rebuild.c | |
| @@ -25,33 +25,23 @@ | |
| 25 | */ |
| 26 | #include "config.h" |
| 27 | #include "rebuild.h" |
| 28 | #include <assert.h> |
| 29 | |
| 30 | |
| 31 | /* |
| 32 | ** COMMAND: rebuild |
| 33 | ** |
| 34 | ** Usage: %fossil rebuild REPOSITORY |
| 35 | ** |
| 36 | ** Reconstruct the named repository database from the core |
| 37 | ** records. Run this command after updating the fossil |
| 38 | ** executable in a way that changes the database schema. |
| 39 | */ |
| 40 | void rebuild_database(void){ |
| 41 | Stmt s; |
| 42 | int errCnt; |
| 43 | int forceFlag; |
| 44 | char *zTable; |
| 45 | |
| 46 | forceFlag = find_option("force","f",0)!=0; |
| 47 | if( g.argc!=3 ){ |
| 48 | usage("REPOSITORY-FILENAME"); |
| 49 | } |
| 50 | errCnt = 0; |
| 51 | db_open_repository(g.argv[2]); |
| 52 | db_begin_transaction(); |
| 53 | db_multi_exec( |
| 54 | "CREATE INDEX IF NOT EXISTS delta_i1 ON delta(srcid);" |
| 55 | ); |
| 56 | for(;;){ |
| 57 | zTable = db_text(0, |
| @@ -75,14 +65,36 @@ | |
| 75 | blob_reset(&content); |
| 76 | }else{ |
| 77 | db_multi_exec("INSERT INTO phantom VALUES(%d)", rid); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if( errCnt && !forceFlag ){ |
| 82 | printf("%d errors. Rolling back changes. Use --force to force a commit.\n", |
| 83 | errCnt); |
| 84 | db_end_transaction(1); |
| 85 | }else{ |
| 86 | db_end_transaction(0); |
| 87 | } |
| 88 | } |
| 89 |
| --- src/rebuild.c | |
| +++ src/rebuild.c | |
| @@ -25,33 +25,23 @@ | |
| 25 | */ |
| 26 | #include "config.h" |
| 27 | #include "rebuild.h" |
| 28 | #include <assert.h> |
| 29 | |
| 30 | /* |
| 31 | ** Core function to rebuild the infomration in the derived tables of a |
| 32 | ** fossil repository from the blobs. This function is shared between |
| 33 | ** 'rebuild_database' ('rebuild') and 'reconstruct_cmd' |
| 34 | ** ('reconstruct'), both of which have to regenerate this information |
| 35 | ** from scratch. |
| 36 | */ |
| 37 | |
| 38 | int rebuild_db(void){ |
| 39 | Stmt s; |
| 40 | int errCnt = 0; |
| 41 | char *zTable; |
| 42 | |
| 43 | db_multi_exec( |
| 44 | "CREATE INDEX IF NOT EXISTS delta_i1 ON delta(srcid);" |
| 45 | ); |
| 46 | for(;;){ |
| 47 | zTable = db_text(0, |
| @@ -75,14 +65,36 @@ | |
| 65 | blob_reset(&content); |
| 66 | }else{ |
| 67 | db_multi_exec("INSERT INTO phantom VALUES(%d)", rid); |
| 68 | } |
| 69 | } |
| 70 | return errCnt; |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | ** COMMAND: rebuild |
| 75 | ** |
| 76 | ** Usage: %fossil rebuild REPOSITORY |
| 77 | ** |
| 78 | ** Reconstruct the named repository database from the core |
| 79 | ** records. Run this command after updating the fossil |
| 80 | ** executable in a way that changes the database schema. |
| 81 | */ |
| 82 | void rebuild_database(void){ |
| 83 | int forceFlag; |
| 84 | int errCnt; |
| 85 | |
| 86 | forceFlag = find_option("force","f",0)!=0; |
| 87 | if( g.argc!=3 ){ |
| 88 | usage("REPOSITORY-FILENAME"); |
| 89 | } |
| 90 | db_open_repository(g.argv[2]); |
| 91 | db_begin_transaction(); |
| 92 | errCnt = rebuild_db(); |
| 93 | if( errCnt && !forceFlag ){ |
| 94 | printf("%d errors. Rolling back changes. Use --force to force a commit.\n", |
| 95 | errCnt); |
| 96 | db_end_transaction(1); |
| 97 | }else{ |
| 98 | db_end_transaction(0); |
| 99 | } |
| 100 | } |
| 101 |