| | @@ -24,100 +24,61 @@ |
| 24 | 24 | ** This file contains code used to push, pull, and sync a repository |
| 25 | 25 | */ |
| 26 | 26 | #include "config.h" |
| 27 | 27 | #include "sync.h" |
| 28 | 28 | #include <assert.h> |
| 29 | + |
| 30 | +/* |
| 31 | +** This routine processes the command-line argument for push, pull, |
| 32 | +** and sync. If a command-line argument is given, that is the URL |
| 33 | +** of a server to sync against. If no argument is given, use the |
| 34 | +** most recently synced URL. Remember the current URL for next time. |
| 35 | +*/ |
| 36 | +static void process_sync_args(void){ |
| 37 | + const char *zUrl = 0; |
| 38 | + db_find_and_open_repository(); |
| 39 | + if( g.argc==2 ){ |
| 40 | + zUrl = db_get("last-sync-url", 0); |
| 41 | + }else if( g.argc==3 ){ |
| 42 | + zUrl = g.argv[2]; |
| 43 | + } |
| 44 | + if( zUrl==0 ){ |
| 45 | + usage("URL"); |
| 46 | + } |
| 47 | + url_parse(zUrl); |
| 48 | + if( g.urlIsFile ){ |
| 49 | + fossil_fatal("network sync only"); |
| 50 | + } |
| 51 | + db_set("last-sync-url", zUrl); |
| 52 | + user_select(); |
| 53 | +} |
| 29 | 54 | |
| 30 | 55 | /* |
| 31 | 56 | ** COMMAND: pull |
| 32 | 57 | ** |
| 33 | 58 | ** Pull changes in a remote repository into the local repository |
| 34 | 59 | */ |
| 35 | 60 | void pull_cmd(void){ |
| 36 | | - if( g.argc!=3 ){ |
| 37 | | - usage("FILE-OR-URL"); |
| 38 | | - } |
| 39 | | - url_parse(g.argv[2]); |
| 40 | | - db_must_be_within_tree(); |
| 41 | | - user_select(); |
| 42 | | - if( g.urlIsFile ){ |
| 43 | | - Stmt q; |
| 44 | | - char *zRemote = g.urlName; |
| 45 | | - if( !file_isfile(zRemote) ){ |
| 46 | | - zRemote = mprintf("%s/_FOSSIL_"); |
| 47 | | - } |
| 48 | | - if( !file_isfile(zRemote) ){ |
| 49 | | - fossil_panic("no such repository: %s", zRemote); |
| 50 | | - } |
| 51 | | - db_multi_exec("ATTACH DATABASE %Q AS other", zRemote); |
| 52 | | - db_begin_transaction(); |
| 53 | | - db_prepare(&q, |
| 54 | | - "SELECT rid FROM other.blob WHERE NOT EXISTS" |
| 55 | | - " (SELECT 1 FROM blob WHERE uuid=other.blob.uuid)" |
| 56 | | - ); |
| 57 | | - while( db_step(&q)==SQLITE_ROW ){ |
| 58 | | - int nrid; |
| 59 | | - int rid = db_column_int(&q, 0); |
| 60 | | - Blob rec; |
| 61 | | - content_get_from_db(rid, &rec, "other"); |
| 62 | | - nrid = content_put(&rec, 0); |
| 63 | | - manifest_crosslink(nrid, &rec); |
| 64 | | - } |
| 65 | | - db_finalize(&q); |
| 66 | | - db_end_transaction(0); |
| 67 | | - }else{ |
| 68 | | - client_sync(0,1,0); |
| 69 | | - } |
| 61 | + process_sync_args(); |
| 62 | + client_sync(0,1,0); |
| 70 | 63 | } |
| 71 | 64 | |
| 72 | 65 | /* |
| 73 | 66 | ** COMMAND: push |
| 74 | 67 | ** |
| 75 | 68 | ** Push changes in the local repository over into a remote repository |
| 76 | 69 | */ |
| 77 | 70 | void push_cmd(void){ |
| 78 | | - if( g.argc!=3 ){ |
| 79 | | - usage("FILE-OR-URL"); |
| 80 | | - } |
| 81 | | - url_parse(g.argv[2]); |
| 82 | | - db_must_be_within_tree(); |
| 83 | | - if( g.urlIsFile ){ |
| 84 | | - Blob remote; |
| 85 | | - char *zRemote; |
| 86 | | - file_canonical_name(g.urlName, &remote); |
| 87 | | - zRemote = blob_str(&remote); |
| 88 | | - if( file_isdir(zRemote)!=1 ){ |
| 89 | | - int i = strlen(zRemote); |
| 90 | | - while( i>0 && zRemote[i]!='/' ){ i--; } |
| 91 | | - zRemote[i] = 0; |
| 92 | | - } |
| 93 | | - if( chdir(zRemote) ){ |
| 94 | | - fossil_panic("unable to change the working directory to %s", zRemote); |
| 95 | | - } |
| 96 | | - db_close(); |
| 97 | | - g.argv[2] = g.zLocalRoot; |
| 98 | | - pull_cmd(); |
| 99 | | - }else{ |
| 100 | | - client_sync(1,0,0); |
| 101 | | - } |
| 71 | + process_sync_args(); |
| 72 | + client_sync(1,0,0); |
| 102 | 73 | } |
| 103 | 74 | |
| 104 | 75 | |
| 105 | 76 | /* |
| 106 | 77 | ** COMMAND: sync |
| 107 | 78 | ** |
| 108 | 79 | ** Synchronize the local repository with a remote repository |
| 109 | 80 | */ |
| 110 | 81 | void sync_cmd(void){ |
| 111 | | - if( g.argc!=3 ){ |
| 112 | | - usage("FILE-OR-URL"); |
| 113 | | - } |
| 114 | | - url_parse(g.argv[2]); |
| 115 | | - if( g.urlIsFile ){ |
| 116 | | - pull_cmd(); |
| 117 | | - db_close(); |
| 118 | | - push_cmd(); |
| 119 | | - }else{ |
| 120 | | - db_must_be_within_tree(); |
| 121 | | - client_sync(1,1,0); |
| 122 | | - } |
| 82 | + process_sync_args(); |
| 83 | + client_sync(1,1,0); |
| 123 | 84 | } |
| 124 | 85 | |