Fossil SCM

fossil-scm / src / sync.c
Blame History Raw 942 lines
1
/*
2
** Copyright (c) 2007 D. Richard Hipp
3
**
4
** This program is free software; you can redistribute it and/or
5
** modify it under the terms of the Simplified BSD License (also
6
** known as the "2-Clause License" or "FreeBSD License".)
7
8
** This program is distributed in the hope that it will be useful,
9
** but without any warranty; without even the implied warranty of
10
** merchantability or fitness for a particular purpose.
11
**
12
** Author contact information:
13
** [email protected]
14
** http://www.hwaci.com/drh/
15
**
16
*******************************************************************************
17
**
18
** This file contains code used to push, pull, and sync a repository
19
*/
20
#include "config.h"
21
#include "sync.h"
22
#include <assert.h>
23
24
/*
25
** Explain what type of sync operation is about to occur
26
*/
27
static void sync_explain(unsigned syncFlags){
28
if( (g.url.isAlias || g.url.useProxy) && (syncFlags & SYNC_QUIET)==0 ){
29
const char *url;
30
if( g.url.useProxy ){
31
url = g.url.proxyUrlCanonical;
32
}else{
33
url = g.url.canonical;
34
}
35
if( (syncFlags & (SYNC_PUSH|SYNC_PULL))==(SYNC_PUSH|SYNC_PULL) ){
36
fossil_print("Sync with %s\n", url);
37
}else if( syncFlags & SYNC_PUSH ){
38
fossil_print("Push to %s\n", url);
39
}else if( syncFlags & SYNC_PULL ){
40
fossil_print("Pull from %s\n", url);
41
}else if( syncFlags & SYNC_PING ){
42
fossil_print("Ping %s\n", url);
43
}
44
}
45
}
46
47
48
/*
49
** Call client_sync() one or more times in order to complete a
50
** sync operation. Usually, client_sync() is called only once, though
51
** is can be called multiple times if the SYNC_ALLURL flags is set.
52
*/
53
static int client_sync_all_urls(
54
unsigned syncFlags, /* Mask of SYNC_* flags */
55
unsigned configRcvMask, /* Receive these configuration items */
56
unsigned configSendMask, /* Send these configuration items */
57
const char *zAltPCode /* Alternative project code (usually NULL) */
58
){
59
int nErr = 0; /* Number of errors seen */
60
int nOther; /* Number of extra remote URLs */
61
char **azOther; /* Text of extra remote URLs */
62
int i; /* Loop counter */
63
int iEnd; /* Loop termination point */
64
int nextIEnd; /* Loop termination point for next pass */
65
int iPass; /* Which pass through the remotes. 0 or 1 */
66
int nPass; /* Number of passes to make. 1 or 2 */
67
Stmt q; /* An SQL statement */
68
UrlData baseUrl; /* Saved parse of the default remote */
69
70
sync_explain(syncFlags);
71
if( (syncFlags & SYNC_ALLURL)==0 ){
72
/* Common-case: Only sync with the remote identified by g.url */
73
nErr = client_sync(syncFlags, configRcvMask, configSendMask, zAltPCode, 0);
74
if( nErr==0 ) url_remember();
75
return nErr;
76
}
77
78
/* If we reach this point, it means we want to sync with all remotes */
79
memset(&baseUrl, 0, sizeof(baseUrl));
80
url_move_parse(&baseUrl, &g.url);
81
nOther = 0;
82
azOther = 0;
83
db_prepare(&q,
84
"SELECT substr(name,10) FROM config"
85
" WHERE name glob 'sync-url:*'"
86
" AND value<>(SELECT value FROM config WHERE name='last-sync-url')"
87
);
88
while( db_step(&q)==SQLITE_ROW ){
89
const char *zUrl = db_column_text(&q, 0);
90
azOther = fossil_realloc(azOther, sizeof(*azOther)*(nOther+1));
91
azOther[nOther++] = fossil_strdup(zUrl);
92
}
93
db_finalize(&q);
94
iEnd = nOther+1;
95
nextIEnd = 0;
96
nPass = 1 + ((syncFlags & (SYNC_PUSH|SYNC_PULL))==(SYNC_PUSH|SYNC_PULL));
97
for(iPass=0; iPass<nPass; iPass++){
98
for(i=0; i<iEnd; i++){
99
int rc;
100
int nRcvd;
101
if( i==0 ){
102
url_move_parse(&g.url, &baseUrl); /* Load canonical URL */
103
}else{
104
/* Load an auxiliary remote URL */
105
url_parse(azOther[i-1],
106
URL_PROMPT_PW|URL_ASK_REMEMBER_PW|URL_USE_CONFIG);
107
}
108
if( i>0 || iPass>0 ) sync_explain(syncFlags);
109
rc = client_sync(syncFlags, configRcvMask, configSendMask,
110
zAltPCode, &nRcvd);
111
if( nRcvd>0 ){
112
/* If new artifacts were received, we want to repeat all prior
113
** remotes on the second pass */
114
nextIEnd = i;
115
}
116
nErr += rc;
117
if( rc==0 && iPass==0 ){
118
if( i==0 ){
119
url_remember();
120
}else if( (g.url.flags & URL_REMEMBER_PW)!=0 ){
121
char *zKey = mprintf("sync-pw:%s", azOther[i-1]);
122
char *zPw = obscure(g.url.passwd);
123
if( zPw && zPw[0] ){
124
db_set(zKey/*works-like:""*/, zPw, 0);
125
}
126
fossil_free(zPw);
127
fossil_free(zKey);
128
}
129
}
130
if( i==0 ){
131
url_move_parse(&baseUrl, &g.url); /* Don't forget canonical URL */
132
}else{
133
url_unparse(&g.url); /* Delete auxiliary URL parses */
134
}
135
}
136
iEnd = nextIEnd;
137
}
138
for(i=0; i<nOther; i++){
139
fossil_free(azOther[i]);
140
azOther[i] = 0;
141
}
142
fossil_free(azOther);
143
url_move_parse(&g.url, &baseUrl); /* Restore the canonical URL parse */
144
return nErr;
145
}
146
147
148
/*
149
** If the repository is configured for autosyncing, then do an
150
** autosync. Bits of the "flags" parameter determine details of behavior:
151
**
152
** SYNC_PULL Pull content from the server to the local repo
153
** SYNC_PUSH Push content from local up to the server
154
** SYNC_CKIN_LOCK Take a check-in lock on the current check-out.
155
** SYNC_VERBOSE Extra output
156
**
157
** Return the number of errors.
158
**
159
** The autosync setting can be a boolean or "pullonly". No autosync
160
** is attempted if the autosync setting is off, and only auto-pull is
161
** attempted if autosync is set to "pullonly". The check-in lock is
162
** not acquired unless autosync is set to "on".
163
**
164
** If dont-push setting is true, that is the same as having autosync
165
** set to pullonly.
166
*/
167
static int autosync(int flags, const char *zSubsys){
168
const char *zAutosync;
169
int rc;
170
int configSync = 0; /* configuration changes transferred */
171
if( g.fNoSync ){
172
return 0;
173
}
174
zAutosync = db_get_for_subsystem("autosync", zSubsys);
175
if( zAutosync==0 ) zAutosync = "on"; /* defend against misconfig */
176
if( is_false(zAutosync) ) return 0;
177
if( db_get_boolean("dont-push",0)
178
|| sqlite3_strglob("*pull*", zAutosync)==0
179
){
180
flags &= ~SYNC_CKIN_LOCK;
181
if( flags & SYNC_PUSH ) return 0;
182
}
183
if( find_option("verbose","v",0)!=0 ) flags |= SYNC_VERBOSE;
184
url_parse(0, URL_REMEMBER|URL_USE_CONFIG);
185
if( g.url.protocol==0 ) return 0;
186
if( g.url.user!=0 && g.url.passwd==0 ){
187
g.url.passwd = unobscure(db_get("last-sync-pw", 0));
188
g.url.flags |= URL_PROMPT_PW;
189
url_prompt_for_password();
190
}
191
g.zHttpAuth = get_httpauth();
192
if( sqlite3_strglob("*all*", zAutosync)==0 ){
193
rc = client_sync_all_urls(flags|SYNC_ALLURL, configSync, 0, 0);
194
}else{
195
url_remember();
196
sync_explain(flags);
197
url_enable_proxy("via proxy: ");
198
rc = client_sync(flags, configSync, 0, 0, 0);
199
}
200
return rc;
201
}
202
203
/*
204
** This routine will try a number of times to perform autosync with a
205
** 0.5 second sleep between attempts. The number of attempts is determined
206
** by the "autosync-tries" setting, which defaults to 1.
207
**
208
** Return zero on success and non-zero on a failure. If failure occurs
209
** and doPrompt flag is true, ask the user if they want to continue, and
210
** if they answer "yes" then return zero in spite of the failure.
211
*/
212
int autosync_loop(int flags, int doPrompt, const char *zSubsystem){
213
int n = 0;
214
int rc = 0;
215
int nTries = db_get_int("autosync-tries", 1);
216
if( (flags & (SYNC_PUSH|SYNC_PULL))==(SYNC_PUSH|SYNC_PULL)
217
&& db_get_boolean("uv-sync",0)
218
){
219
flags |= SYNC_UNVERSIONED;
220
}
221
if( nTries<1 ) nTries = 1;
222
while( (n==0 || n<nTries) && (rc=autosync(flags, zSubsystem)) ){
223
if( rc ){
224
if( ++n<nTries ){
225
fossil_warning("Autosync failed, making another attempt.");
226
sqlite3_sleep(500);
227
}else{
228
fossil_warning("Autosync failed.");
229
}
230
}
231
}
232
if( rc && doPrompt ){
233
Blob ans;
234
char cReply;
235
prompt_user("continue in spite of sync failure (y/N)? ", &ans);
236
cReply = blob_str(&ans)[0];
237
if( cReply=='y' || cReply=='Y' ) rc = 0;
238
blob_reset(&ans);
239
}
240
return rc;
241
}
242
243
/*
244
** This routine processes the command-line argument for push, pull,
245
** and sync. If a command-line argument is given, that is the URL
246
** of a server to sync against. If no argument is given, use the
247
** most recently synced URL. Remember the current URL for next time.
248
*/
249
static void process_sync_args(
250
unsigned *pConfigFlags, /* Write configuration flags here */
251
unsigned *pSyncFlags, /* Write sync flags here */
252
int uvOnly, /* Special handling flags for UV sync */
253
unsigned urlOmitFlags /* Omit these URL flags */
254
){
255
const char *zUrl = 0;
256
const char *zHttpAuth = 0;
257
unsigned configSync = 0;
258
unsigned urlFlags = URL_REMEMBER | URL_PROMPT_PW;
259
int urlOptional = 0;
260
if( find_option("autourl",0,0)!=0 ){
261
urlOptional = 1;
262
urlFlags = 0;
263
}
264
zHttpAuth = find_option("httpauth","B",1);
265
if( find_option("once",0,0)!=0 ) urlFlags &= ~URL_REMEMBER;
266
if( (*pSyncFlags) & SYNC_FROMPARENT ) urlFlags |= URL_USE_PARENT;
267
if( !uvOnly ){
268
if( find_option("private",0,0)!=0 ){
269
*pSyncFlags |= SYNC_PRIVATE;
270
}
271
/* The --verily option to sync, push, and pull forces extra igot cards
272
** to be exchanged. This can overcome malfunctions in the sync protocol.
273
*/
274
if( find_option("verily",0,0)!=0 ){
275
*pSyncFlags |= SYNC_RESYNC;
276
}
277
}
278
if( find_option("private",0,0)!=0 ){
279
*pSyncFlags |= SYNC_PRIVATE;
280
}
281
if( find_option("verbose","v",0)!=0 ){
282
*pSyncFlags |= SYNC_VERBOSE;
283
if( find_option("verbose","v",0)!=0 ){
284
*pSyncFlags |= SYNC_XVERBOSE;
285
}
286
}
287
if( find_option("no-http-compression",0,0)!=0 ){
288
*pSyncFlags |= SYNC_NOHTTPCOMPRESS;
289
}
290
if( find_option("all",0,0)!=0 ){
291
*pSyncFlags |= SYNC_ALLURL;
292
}
293
294
/* Undocumented option to cause links transitive links to other
295
** repositories to be shared */
296
if( ((*pSyncFlags) & SYNC_PULL)!=0
297
&& find_option("share-links",0,0)!=0
298
){
299
*pSyncFlags |= SYNC_SHARE_LINKS;
300
}
301
302
/* Option: --transport-command COMMAND
303
**
304
** Causes COMMAND to be run with three arguments in order to talk
305
** to the server.
306
**
307
** COMMAND URL PAYLOAD REPLY
308
**
309
** URL is the server name. PAYLOAD is the name of a temporary file
310
** that will contain the xfer-protocol payload to send to the server.
311
** REPLY is a temporary filename in which COMMAND should write the
312
** content of the reply from the server.
313
**
314
** CMD is responsible for HTTP redirects. The following Fossil command
315
** can be used for CMD to achieve a working sync:
316
**
317
** fossil test-httpmsg --xfer
318
*/
319
g.zHttpCmd = find_option("transport-command",0,1);
320
321
url_proxy_options();
322
clone_ssh_find_options();
323
if( !uvOnly ) db_find_and_open_repository(0, 0);
324
db_open_config(0, 1);
325
if( g.argc==2 ){
326
if( db_get_boolean("auto-shun",0) ) configSync = CONFIGSET_SHUN;
327
}else if( g.argc==3 ){
328
zUrl = g.argv[2];
329
if( (*pSyncFlags) & SYNC_ALLURL ){
330
fossil_fatal("cannot use both the --all option and specific URL \"%s\"",
331
zUrl);
332
}
333
}
334
if( ((*pSyncFlags) & (SYNC_PUSH|SYNC_PULL))==(SYNC_PUSH|SYNC_PULL)
335
&& db_get_boolean("uv-sync",0)
336
){
337
*pSyncFlags |= SYNC_UNVERSIONED;
338
}
339
urlFlags &= ~urlOmitFlags;
340
if( urlFlags & URL_REMEMBER ){
341
clone_ssh_db_set_options();
342
}
343
url_parse(zUrl, urlFlags|URL_USE_CONFIG);
344
remember_or_get_http_auth(zHttpAuth, urlFlags & URL_REMEMBER, zUrl);
345
if( g.url.protocol==0 ){
346
if( urlOptional ) fossil_exit(0);
347
usage("URL");
348
}
349
user_select();
350
url_enable_proxy("via proxy: ");
351
*pConfigFlags |= configSync;
352
if( (*pSyncFlags & SYNC_ALLURL)==0 && zUrl==0 ){
353
const char *zAutosync = db_get_for_subsystem("autosync", "sync");
354
if( sqlite3_strglob("*all*", zAutosync)==0 ){
355
*pSyncFlags |= SYNC_ALLURL;
356
}
357
}
358
}
359
360
/*
361
* Wrapper method around fossil_warning for sync errors takes single
362
* argument for the operation (e.g. sync, pull, push).
363
*/
364
static void sync_errors(const char *zOp){
365
fossil_warning("Warning: see above for errors encountered during %s.", zOp);
366
}
367
368
/*
369
** COMMAND: pull
370
**
371
** Usage: %fossil pull ?URL? ?options?
372
**
373
** Pull all sharable changes from a remote repository into the local
374
** repository. Sharable changes include public check-ins, edits to
375
** wiki pages, tickets, tech-notes, and forum posts. Add
376
** the --private option to pull private branches. Use the
377
** "configuration pull" command to pull website configuration details.
378
**
379
** If URL is not specified, then the URL from the most recent clone, push,
380
** pull, remote, or sync command is used. See "fossil help clone" for
381
** details on the URL formats.
382
**
383
** Options:
384
** --all Pull from all remotes, not just the default
385
** -B|--httpauth USER:PASS Credentials for the simple HTTP auth protocol,
386
** if required by the remote website
387
** --from-parent-project Pull content from the parent project
388
** --ipv4 Use only IPv4, not IPv6
389
** --no-http-compression Do not compress HTTP traffic
390
** --once Do not remember URL for subsequent syncs
391
** --private Pull private branches too
392
** --project-code CODE Use CODE as the project code
393
** --proxy PROXY Use the specified HTTP proxy
394
** -R|--repository REPO Local repository to pull into
395
** --ssl-identity FILE Local SSL credentials, if requested by remote
396
** --ssh-command SSH Use SSH as the "ssh" command
397
** --transport-command CMD Use external command CMD to move messages
398
** between client and server
399
** -v|--verbose Additional (debugging) output - use twice to
400
** also trace network traffic.
401
** --verily Exchange extra information with the remote
402
** to ensure no content is overlooked
403
**
404
** See also: [[clone]], [[config]], [[push]], [[remote]], [[sync]]
405
*/
406
void pull_cmd(void){
407
unsigned configFlags = 0;
408
unsigned syncFlags = SYNC_PULL;
409
unsigned urlOmitFlags = 0;
410
const char *zAltPCode = find_option("project-code",0,1);
411
int nErr;
412
if( find_option("from-parent-project",0,0)!=0 ){
413
syncFlags |= SYNC_FROMPARENT;
414
}
415
if( zAltPCode ) urlOmitFlags = URL_REMEMBER;
416
process_sync_args(&configFlags, &syncFlags, 0, urlOmitFlags);
417
418
/* We should be done with options.. */
419
verify_all_options();
420
421
nErr = client_sync_all_urls(syncFlags, configFlags, 0, zAltPCode);
422
if( nErr ){ sync_errors("pull"); }
423
}
424
425
/*
426
** COMMAND: push
427
**
428
** Usage: %fossil push ?URL? ?options?
429
**
430
** Push all sharable changes from the local repository to a remote
431
** repository. Sharable changes include public check-ins, edits to
432
** wiki pages, tickets, tech-notes, and forum posts. Use
433
** --private to also push private branches. Use the "configuration
434
** push" command to push website configuration details.
435
**
436
** If URL is not specified, then the URL from the most recent clone, push,
437
** pull, remote, or sync command is used. See "fossil help clone" for
438
** details on the URL formats.
439
**
440
** Options:
441
** --all Push to all remotes, not just the default
442
** -B|--httpauth USER:PASS Credentials for the simple HTTP auth protocol,
443
** if required by the remote website
444
** --ipv4 Use only IPv4, not IPv6
445
** --no-http-compression Do not compress HTTP traffic
446
** --once Do not remember URL for subsequent syncs
447
** --proxy PROXY Use the specified HTTP proxy
448
** --private Push private branches too
449
** -R|--repository REPO Local repository to push from
450
** --ssl-identity FILE Local SSL credentials, if requested by remote
451
** --ssh-command SSH Use SSH as the "ssh" command
452
** --transport-command CMD Use external command CMD to communicate with
453
** the server
454
** -v|--verbose Additional (debugging) output - use twice for
455
** network debugging
456
** --verily Exchange extra information with the remote
457
** to ensure no content is overlooked
458
**
459
** See also: [[clone]], [[config]], [[pull]], [[remote]], [[sync]]
460
*/
461
void push_cmd(void){
462
unsigned configFlags = 0;
463
unsigned syncFlags = SYNC_PUSH;
464
int nErr;
465
process_sync_args(&configFlags, &syncFlags, 0, 0);
466
467
/* We should be done with options.. */
468
verify_all_options();
469
470
if( db_get_boolean("dont-push",0) ){
471
fossil_fatal("pushing is prohibited: the 'dont-push' option is set");
472
}
473
nErr = client_sync_all_urls(syncFlags, 0, 0, 0);
474
if( nErr ){ sync_errors("push"); }
475
}
476
477
478
/*
479
** COMMAND: sync
480
**
481
** Usage: %fossil sync ?REMOTE? ?options?
482
**
483
** Synchronize all sharable changes between the local repository and a
484
** remote repository, with the remote provided as a URL or a
485
** configured remote name (see the [[remote]] command). Sharable
486
** changes include public check-ins and edits to wiki pages, tickets,
487
** forum posts, and technical notes.
488
**
489
** If REMOTE is not specified, then the URL from the most recent clone, push,
490
** pull, remote, or sync command is used. See "fossil help clone" for
491
** details on the URL formats.
492
**
493
** Options:
494
** --all Sync with all remotes, not just the default
495
** -B|--httpauth USER:PASS Credentials for the simple HTTP auth protocol,
496
** if required by the remote website
497
** --ipv4 Use only IPv4, not IPv6
498
** --no-http-compression Do not compress HTTP traffic
499
** --once Do not remember URL for subsequent syncs
500
** --ping Just verify that the server is alive
501
** --proxy PROXY Use the specified HTTP proxy
502
** --private Sync private branches too
503
** -q|--quiet Omit all output
504
** -R|--repository REPO Local repository to sync with
505
** --ssl-identity FILE Local SSL credentials, if requested by remote
506
** --ssh-command SSH Use SSH as the "ssh" command
507
** --transport-command CMD Use external command CMD to move message
508
** between the client and the server
509
** -u|--unversioned Also sync unversioned content
510
** -v|--verbose Additional (debugging) output - use twice to
511
** get network debug info
512
** --verily Exchange extra information with the remote
513
** to ensure no content is overlooked
514
**
515
** See also: [[clone]], [[pull]], [[push]], [[remote]]
516
*/
517
void sync_cmd(void){
518
unsigned configFlags = 0;
519
unsigned syncFlags = SYNC_PUSH|SYNC_PULL;
520
int nErr;
521
if( find_option("unversioned","u",0)!=0 ){
522
syncFlags |= SYNC_UNVERSIONED;
523
}
524
if( find_option("ping",0,0)!=0 ){
525
syncFlags = SYNC_PING;
526
}
527
if( g.fQuiet ){
528
syncFlags |= SYNC_QUIET;
529
}
530
process_sync_args(&configFlags, &syncFlags, 0, 0);
531
532
/* We should be done with options.. */
533
verify_all_options();
534
535
if( (syncFlags & SYNC_PING)==0 ){
536
if( db_get_boolean("dont-push",0) ) syncFlags &= ~SYNC_PUSH;
537
if( (syncFlags & SYNC_PUSH)==0 ){
538
fossil_warning("pull only: the 'dont-push' option is set");
539
}
540
}
541
nErr = client_sync_all_urls(syncFlags, configFlags, 0, 0);
542
if( nErr ){ sync_errors("sync"); }
543
}
544
545
/*
546
** Handle the "fossil unversioned sync" and "fossil unversioned revert"
547
** commands.
548
*/
549
void sync_unversioned(unsigned syncFlags){
550
unsigned configFlags = 0;
551
(void)find_option("uv-noop",0,0);
552
process_sync_args(&configFlags, &syncFlags, 1, 0);
553
verify_all_options();
554
client_sync(syncFlags, 0, 0, 0, 0);
555
}
556
557
/*
558
** COMMAND: remote
559
** COMMAND: remote-url*
560
**
561
** Usage: %fossil remote ?SUBCOMMAND ...?
562
**
563
** View or modify the URLs of remote repositories used for syncing.
564
** The "default" remote is specially named by Fossil and corresponds to
565
** the URL used in the most recent "sync", "push", "pull", "clone", or
566
** similar command. As such, the default remote can be updated by
567
** Fossil with each sync command. Other named remotes are persistent.
568
**
569
** > fossil remote
570
**
571
** With no arguments, this command shows the current default remote
572
** URL. If there is no default, it shows "off".
573
**
574
** > fossil remote add NAME URL
575
**
576
** Add a new named URL. Afterwards, NAME can be used as a short
577
** symbolic name for URL in contexts where a URL is required. The
578
** URL argument can be "default" or a prior symbolic name to make
579
** a copy of an existing URL under the new NAME. The "default"
580
** remote cannot be defined with this subcommand; instead,
581
** use 'fossil remote REF' as documented below.
582
**
583
** > fossil remote config-data
584
**
585
** DEBUG USE ONLY - Show the name and value of every CONFIG table
586
** entry in the repository that is associated with the remote URL store.
587
** Passwords are obscured in the output.
588
**
589
** > fossil remote delete NAME
590
**
591
** Delete a named URL previously created by the "add" subcommand.
592
**
593
** > fossil remote hyperlink ?FILENAME? ?LINENUM? ?LINENUM?
594
**
595
** Print a URL that will access the current check-out on the remote
596
** repository. Or if the FILENAME argument is included, print the
597
** URL to access that particular file within the current check-out.
598
** If one or two linenumber arguments are provided after the filename,
599
** then the URL is for the line or range of lines specified.
600
**
601
** > fossil remote list|ls
602
**
603
** Show all remote repository URLs.
604
**
605
** > fossil remote off
606
**
607
** Forget the default URL. This disables autosync.
608
**
609
** This is a convenient way to enter "airplane mode". To enter
610
** airplane mode, first save the current default URL, then turn the
611
** default off. Perhaps like this:
612
**
613
** fossil remote add main default
614
** fossil remote off
615
**
616
** To exit airplane mode and turn autosync back on again:
617
**
618
** fossil remote main
619
**
620
** > fossil remote scrub
621
**
622
** Forget any saved passwords for remote repositories, but continue
623
** to remember the URLs themselves. You will be prompted for the
624
** password the next time it is needed.
625
**
626
** > fossil remote ui ?FILENAME? ?LINENUM? ?LINENUM?
627
**
628
** Bring up a web browser pointing at the remote repository, and
629
** specifically to the page that describes the current check-out
630
** on that remote repository. Or if FILENAME and/or LINENUM arguments
631
** are provided, to the specific file and range of lines. This
632
** command is similar to "fossil remote hyperlink" except that instead
633
** of printing the URL, it passes the URL off to the web browser.
634
**
635
** > fossil remote REF
636
**
637
** Make REF the new default URL, replacing the prior default.
638
** REF may be a URL or a NAME from a prior "add".
639
*/
640
void remote_url_cmd(void){
641
char *zUrl, *zArg;
642
int nArg;
643
int showPw;
644
db_find_and_open_repository(0, 0);
645
showPw = find_option("show-passwords",0,0)!=0;
646
647
/* We should be done with options.. */
648
verify_all_options();
649
650
/* 2021-10-25: A note about data structures.
651
**
652
** The remote URLs are stored in the CONFIG table. The URL is stored
653
** separately from the password. The password is obscured using the
654
** obscure() function.
655
**
656
** Originally, Fossil only preserved a single remote URL. That URL
657
** is stored in "last-sync-url" and the password in "last-sync-pw". The
658
** ability to have multiple remotes was added later so these names
659
** were retained for backwards compatibility. The other remotes are
660
** stored in "sync-url:NAME" and "sync-pw:NAME" where NAME is the name
661
** of the remote.
662
**
663
** The last-sync-url is called "default" for the display list.
664
**
665
** The last-sync-url might be duplicated into one of the sync-url:NAME
666
** entries. Thus, when doing a "fossil sync --all" or an autosync with
667
** autosync=all, each sync-url:NAME entry is checked to see if it is the
668
** same as last-sync-url and if it is then that entry is skipped.
669
*/
670
671
if( g.argc==2 ){
672
/* "fossil remote" with no arguments: Show the last sync URL. */
673
zUrl = db_get("last-sync-url", 0);
674
if( zUrl==0 ){
675
fossil_print("off\n");
676
}else{
677
url_parse(zUrl, 0);
678
fossil_print("%s\n", g.url.canonical);
679
}
680
return;
681
}
682
zArg = g.argv[2];
683
nArg = (int)strlen(zArg);
684
if( strcmp(zArg,"off")==0 ){
685
/* fossil remote off
686
** Forget the last-sync-URL and its password
687
*/
688
if( g.argc!=3 ) usage("off");
689
remote_delete_default:
690
db_unprotect(PROTECT_CONFIG);
691
db_multi_exec(
692
"DELETE FROM config WHERE name GLOB 'last-sync-*';"
693
);
694
db_protect_pop();
695
return;
696
}
697
if( strncmp(zArg, "list", nArg)==0 || strcmp(zArg,"ls")==0 ){
698
Stmt q;
699
if( g.argc!=3 ) usage("list");
700
db_prepare(&q,
701
"SELECT 'default', value FROM config WHERE name='last-sync-url'"
702
" UNION ALL "
703
"SELECT substr(name,10), value FROM config"
704
" WHERE name GLOB 'sync-url:*'"
705
" ORDER BY 1"
706
);
707
while( db_step(&q)==SQLITE_ROW ){
708
fossil_print("%-18s %s\n", db_column_text(&q,0), db_column_text(&q,1));
709
}
710
db_finalize(&q);
711
return;
712
}
713
if( strcmp(zArg, "add")==0 ){
714
char *zName;
715
char *zUrl;
716
UrlData x;
717
if( g.argc!=5 ) usage("add NAME URL");
718
memset(&x, 0, sizeof(x));
719
zName = g.argv[3];
720
zUrl = g.argv[4];
721
if( strcmp(zName,"default")==0 ){
722
fossil_fatal("update the \"default\" remote-url with 'fossil remote REF'"
723
"\nsee 'fossil help remote' for complete usage information");
724
}
725
db_begin_write();
726
if( fossil_strcmp(zUrl,"default")==0 ){
727
x.canonical = db_get("last-sync-url",0);
728
x.passwd = unobscure(db_get("last-sync-pw",0));
729
}else{
730
url_parse_local(zUrl, URL_PROMPT_PW|URL_USE_CONFIG, &x);
731
}
732
db_unprotect(PROTECT_CONFIG);
733
db_multi_exec(
734
"REPLACE INTO config(name, value, mtime)"
735
" VALUES('sync-url:%q',%Q,now())",
736
zName, x.canonical
737
);
738
db_multi_exec(
739
"REPLACE INTO config(name, value, mtime)"
740
" VALUES('sync-pw:%q',obscure(%Q),now())",
741
zName, x.passwd
742
);
743
db_protect_pop();
744
db_commit_transaction();
745
return;
746
}
747
if( strncmp(zArg, "delete", nArg)==0 ){
748
char *zName;
749
if( g.argc!=4 ) usage("delete NAME");
750
zName = g.argv[3];
751
if( strcmp(zName,"default")==0 ) goto remote_delete_default;
752
db_begin_write();
753
db_unprotect(PROTECT_CONFIG);
754
db_multi_exec("DELETE FROM config WHERE name glob 'sync-url:%q'", zName);
755
db_multi_exec("DELETE FROM config WHERE name glob 'sync-pw:%q'", zName);
756
db_protect_pop();
757
db_commit_transaction();
758
return;
759
}
760
if( strncmp(zArg, "hyperlink", nArg)==0
761
|| (nArg==2 && strcmp(zArg, "ui")==0)
762
){
763
char *zBase;
764
char *zUuid;
765
Blob fname;
766
Blob url;
767
char *zSubCmd = g.argv[2][0]=='u' ? "ui" : "hyperlink";
768
if( !db_table_exists("localdb","vvar") ){
769
fossil_fatal("the \"remote %s\" command only works from "
770
"within an open check-out", zSubCmd);
771
}
772
zUrl = db_get("last-sync-url", 0);
773
if( zUrl==0 ){
774
zUrl = "http://localhost:8080/";
775
}
776
url_parse(zUrl, 0);
777
if( g.url.isFile ){
778
url_parse("http://localhost:8080/", 0);
779
}
780
zBase = url_nouser(&g.url);
781
blob_init(&url, 0, 0);
782
if( g.argc==3 ){
783
blob_appendf(&url, "%s/info/%!S",
784
zBase,
785
db_text("???",
786
"SELECT uuid FROM blob, vvar"
787
" WHERE blob.rid=0+vvar.value"
788
" AND vvar.name='checkout';"
789
));
790
}else{
791
blob_init(&fname, 0, 0);
792
file_tree_name(g.argv[3], &fname, 0, 1);
793
zUuid = db_text(0,
794
"SELECT uuid FROM files_of_checkin"
795
" WHERE checkinID=(SELECT value FROM vvar WHERE name='checkout')"
796
" AND filename=%Q",
797
blob_str(&fname)
798
);
799
if( zUuid==0 ){
800
fossil_fatal("not a managed file: \"%s\"", g.argv[3]);
801
}
802
blob_appendf(&url, "%s/info/%S",zBase,zUuid);
803
if( g.argc>4 ){
804
int ln1 = atoi(g.argv[4]);
805
if( ln1<=0 || sqlite3_strglob("*[^0-9]*",g.argv[4])==0 ){
806
fossil_fatal("\"%s\" is not a valid line number", g.argv[4]);
807
}
808
if( g.argc>5 ){
809
int ln2 = atoi(g.argv[5]);
810
if( ln2==0 || sqlite3_strglob("*[^0-9]*",g.argv[5])==0 ){
811
fossil_fatal("\"%s\" is not a valid line number", g.argv[5]);
812
}
813
if( ln2<=ln1 ){
814
fossil_fatal("second line number should be greater than the first");
815
}
816
blob_appendf(&url,"?ln=%d,%d", ln1, ln2);
817
}else{
818
blob_appendf(&url,"?ln=%d", ln1);
819
}
820
}
821
if( g.argc>6 ){
822
usage(mprintf("%s ?FILENAME? ?LINENUMBER? ?LINENUMBER?", zSubCmd));
823
}
824
}
825
if( g.argv[2][0]=='u' ){
826
char *zCmd;
827
zCmd = mprintf("%s %!$ &", fossil_web_browser(), blob_str(&url));
828
fossil_system(zCmd);
829
}else{
830
fossil_print("%s\n", blob_str(&url));
831
}
832
return;
833
}
834
if( strncmp(zArg, "scrub", nArg)==0 ){
835
if( g.argc!=3 ) usage("scrub");
836
db_begin_write();
837
db_unprotect(PROTECT_CONFIG);
838
db_multi_exec("DELETE FROM config WHERE name glob 'sync-pw:*'");
839
db_multi_exec("DELETE FROM config WHERE name = 'last-sync-pw'");
840
db_protect_pop();
841
db_commit_transaction();
842
return;
843
}
844
if( strncmp(zArg, "config-data", nArg)==0 ){
845
/* Undocumented command: "fossil remote config-data [-show-passwords]"
846
**
847
** Show the CONFIG table entries that relate to remembering remote URLs
848
*/
849
Stmt q;
850
int n;
851
sqlite3_create_function(g.db, "unobscure", 1, SQLITE_UTF8, &g.db,
852
db_obscure, 0, 0);
853
n = db_int(13,
854
"SELECT max(length(name))"
855
" FROM config"
856
" WHERE name GLOB 'sync-*:*'"
857
" OR name GLOB 'last-sync-*'"
858
" OR name GLOB 'parent-project-*'"
859
);
860
db_prepare(&q,
861
"SELECT name,"
862
" CASE WHEN name NOT LIKE '%%sync-pw%%' AND name<>'parent-project-pw'"
863
" THEN value"
864
" WHEN %d THEN unobscure(value)"
865
" ELSE printf('%%.*c',length(value)/2-1,'*') END"
866
" FROM config"
867
" WHERE name GLOB 'sync-*:*'"
868
" OR name GLOB 'last-sync-*'"
869
" OR name GLOB 'parent-project-*'"
870
" ORDER BY name LIKE '%%sync-pw%%' OR name='parent-project-pw', name",
871
showPw
872
);
873
while( db_step(&q)==SQLITE_ROW ){
874
fossil_print("%-*s %s\n",
875
n, db_column_text(&q,0),
876
db_column_text(&q,1)
877
);
878
}
879
db_finalize(&q);
880
return;
881
}
882
if( sqlite3_strlike("http://%",zArg,0)==0
883
|| sqlite3_strlike("https://%",zArg,0)==0
884
|| sqlite3_strlike("ssh:%",zArg,0)==0
885
|| sqlite3_strlike("file:%",zArg,0)==0
886
|| db_exists("SELECT 1 FROM config WHERE name='sync-url:%q'",zArg)
887
){
888
db_unset("last-sync-url", 0);
889
db_unset("last-sync-pw", 0);
890
url_parse(g.argv[2], URL_REMEMBER|URL_PROMPT_PW|
891
URL_USE_CONFIG|URL_ASK_REMEMBER_PW);
892
url_remember();
893
return;
894
}
895
fossil_fatal("unknown command \"%s\" - should be a URL or one of: "
896
"add delete hyperlink list off scrub", zArg);
897
}
898
899
/*
900
** COMMAND: backup*
901
**
902
** Usage: %fossil backup ?OPTIONS? FILE|DIRECTORY
903
**
904
** Make a backup of the repository into the named file or into the named
905
** directory. This backup is guaranteed to be consistent even if there are
906
** concurrent changes taking place on the repository. In other words, it
907
** is safe to run "fossil backup" on a repository that is in active use.
908
**
909
** Only the main repository database is backed up by this command. The
910
** open check-out file (if any) is not saved. Nor is the global configuration
911
** database.
912
**
913
** Options:
914
** --overwrite OK to overwrite an existing file
915
** -R NAME Filename of the repository to backup
916
*/
917
void backup_cmd(void){
918
char *zDest;
919
int bOverwrite = 0;
920
db_find_and_open_repository(OPEN_ANY_SCHEMA, 0);
921
bOverwrite = find_option("overwrite",0,0)!=0;
922
verify_all_options();
923
if( g.argc!=3 ){
924
usage("FILE|DIRECTORY");
925
}
926
zDest = g.argv[2];
927
if( file_isdir(zDest, ExtFILE)==1 ){
928
zDest = mprintf("%s/%s", zDest, file_tail(g.zRepositoryName));
929
}
930
if( file_isfile(zDest, ExtFILE) ){
931
if( bOverwrite ){
932
if( file_delete(zDest) ){
933
fossil_fatal("unable to delete old copy of \"%s\"", zDest);
934
}
935
}else{
936
fossil_fatal("backup \"%s\" already exists", zDest);
937
}
938
}
939
db_unprotect(PROTECT_ALL);
940
db_multi_exec("VACUUM repository INTO %Q", zDest);
941
}
942

Keyboard Shortcuts

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