Fossil SCM

fossil-scm / src / branch.c
Blame History Raw 1162 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 create new branches within a repository.
19
*/
20
#include "config.h"
21
#include "branch.h"
22
#include <assert.h>
23
24
/*
25
** Return the name of the main branch. Cache the result.
26
**
27
** This is the current value of the "main-branch" setting, or its default
28
** value (historically, and as of 2025-10-28: "trunk") if not set.
29
*/
30
const char *db_main_branch(void){
31
static char *zMainBranch = 0;
32
if( zMainBranch==0 ) zMainBranch = db_get("main-branch", 0);
33
return zMainBranch;
34
}
35
36
/*
37
** Return true if zBr is the branch name associated with check-in with
38
** blob.uuid value of zUuid
39
*/
40
int branch_includes_uuid(const char *zBr, const char *zUuid){
41
return db_exists(
42
"SELECT 1 FROM tagxref, blob"
43
" WHERE blob.uuid=%Q AND tagxref.rid=blob.rid"
44
" AND tagxref.value=%Q AND tagxref.tagtype>0"
45
" AND tagxref.tagid=%d",
46
zUuid, zBr, TAG_BRANCH
47
);
48
}
49
50
/*
51
** If RID refers to a check-in, return the name of the branch for that
52
** check-in.
53
**
54
** Space to hold the returned value is obtained from fossil_malloc()
55
** and should be freed by the caller.
56
*/
57
char *branch_of_rid(int rid){
58
char *zBr = 0;
59
static Stmt q;
60
db_static_prepare(&q,
61
"SELECT value FROM tagxref"
62
" WHERE rid=$rid AND tagid=%d"
63
" AND tagtype>0", TAG_BRANCH);
64
db_bind_int(&q, "$rid", rid);
65
if( db_step(&q)==SQLITE_ROW ){
66
zBr = fossil_strdup(db_column_text(&q,0));
67
}
68
db_reset(&q);
69
if( zBr==0 ){
70
zBr = fossil_strdup(db_main_branch());
71
}
72
return zBr;
73
}
74
75
/*
76
** fossil branch new NAME BASIS ?OPTIONS?
77
** argv0 argv1 argv2 argv3 argv4
78
*/
79
void branch_new(void){
80
int rootid; /* RID of the root check-in - what we branch off of */
81
int brid; /* RID of the branch check-in */
82
int noSign; /* True if the branch is unsigned */
83
int i; /* Loop counter */
84
char *zUuid; /* Artifact ID of origin */
85
Stmt q; /* Generic query */
86
const char *zBranch; /* Name of the new branch */
87
char *zDate; /* Date that branch was created */
88
char *zComment; /* Check-in comment for the new branch */
89
const char *zColor; /* Color of the new branch */
90
Blob branch; /* manifest for the new branch */
91
Manifest *pParent; /* Parsed parent manifest */
92
Blob mcksum; /* Self-checksum on the manifest */
93
const char *zDateOvrd; /* Override date string */
94
const char *zUserOvrd; /* Override user name */
95
int isPrivate = 0; /* True if the branch should be private */
96
97
noSign = find_option("nosign","",0)!=0;
98
if( find_option("nosync",0,0) ) g.fNoSync = 1;
99
zColor = find_option("bgcolor","c",1);
100
isPrivate = find_option("private",0,0)!=0;
101
zDateOvrd = find_option("date-override",0,1);
102
zUserOvrd = find_option("user-override",0,1);
103
verify_all_options();
104
if( g.argc<5 ){
105
usage("new BRANCH-NAME BASIS ?OPTIONS?");
106
}
107
db_find_and_open_repository(0, 0);
108
noSign = db_get_boolean("omitsign", 0)|noSign;
109
if( db_get_boolean("clearsign", 0)==0 ){ noSign = 1; }
110
111
/* fossil branch new name */
112
zBranch = g.argv[3];
113
if( zBranch==0 || zBranch[0]==0 ){
114
fossil_fatal("branch name cannot be empty");
115
}
116
if( branch_is_open(zBranch) ){
117
fossil_fatal("an open branch named \"%s\" already exists", zBranch);
118
}
119
120
user_select();
121
db_begin_transaction();
122
rootid = name_to_typed_rid(g.argv[4], "ci");
123
if( rootid==0 ){
124
fossil_fatal("unable to locate check-in off of which to branch");
125
}
126
127
pParent = manifest_get(rootid, CFTYPE_MANIFEST, 0);
128
if( pParent==0 ){
129
fossil_fatal("%s is not a valid check-in", g.argv[4]);
130
}
131
132
/* Create a manifest for the new branch */
133
blob_zero(&branch);
134
if( pParent->zBaseline ){
135
blob_appendf(&branch, "B %s\n", pParent->zBaseline);
136
}
137
zComment = mprintf("Create new branch named \"%h\"", zBranch);
138
blob_appendf(&branch, "C %F\n", zComment);
139
zDate = date_in_standard_format(zDateOvrd ? zDateOvrd : "now");
140
blob_appendf(&branch, "D %s\n", zDate);
141
142
/* Copy all of the content from the parent into the branch */
143
for(i=0; i<pParent->nFile; ++i){
144
blob_appendf(&branch, "F %F", pParent->aFile[i].zName);
145
if( pParent->aFile[i].zUuid ){
146
blob_appendf(&branch, " %s", pParent->aFile[i].zUuid);
147
if( pParent->aFile[i].zPerm && pParent->aFile[i].zPerm[0] ){
148
blob_appendf(&branch, " %s", pParent->aFile[i].zPerm);
149
}
150
}
151
blob_append(&branch, "\n", 1);
152
}
153
zUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", rootid);
154
blob_appendf(&branch, "P %s\n", zUuid);
155
if( pParent->zRepoCksum ){
156
blob_appendf(&branch, "R %s\n", pParent->zRepoCksum);
157
}
158
manifest_destroy(pParent);
159
160
/* Add the symbolic branch name and the "branch" tag to identify
161
** this as a new branch */
162
if( content_is_private(rootid) ) isPrivate = 1;
163
if( zColor!=0 ){
164
blob_appendf(&branch, "T *bgcolor * %F\n", zColor);
165
}
166
blob_appendf(&branch, "T *branch * %F\n", zBranch);
167
blob_appendf(&branch, "T *sym-%F *\n", zBranch);
168
if( isPrivate ){
169
noSign = 1;
170
}
171
172
/* Cancel all other symbolic tags */
173
db_prepare(&q,
174
"SELECT tagname FROM tagxref, tag"
175
" WHERE tagxref.rid=%d AND tagxref.tagid=tag.tagid"
176
" AND tagtype>0 AND tagname GLOB 'sym-*'"
177
" ORDER BY tagname",
178
rootid);
179
while( db_step(&q)==SQLITE_ROW ){
180
const char *zTag = db_column_text(&q, 0);
181
blob_appendf(&branch, "T -%F *\n", zTag);
182
}
183
db_finalize(&q);
184
185
blob_appendf(&branch, "U %F\n", zUserOvrd ? zUserOvrd : login_name());
186
md5sum_blob(&branch, &mcksum);
187
blob_appendf(&branch, "Z %b\n", &mcksum);
188
if( !noSign && clearsign(&branch, &branch) ){
189
Blob ans;
190
char cReply;
191
prompt_user("unable to sign manifest. continue (y/N)? ", &ans);
192
cReply = blob_str(&ans)[0];
193
if( cReply!='y' && cReply!='Y'){
194
db_end_transaction(1);
195
fossil_exit(1);
196
}
197
}
198
199
brid = content_put_ex(&branch, 0, 0, 0, isPrivate);
200
if( brid==0 ){
201
fossil_fatal("trouble committing manifest: %s", g.zErrMsg);
202
}
203
db_add_unsent(brid);
204
if( manifest_crosslink(brid, &branch, MC_PERMIT_HOOKS)==0 ){
205
fossil_fatal("%s", g.zErrMsg);
206
}
207
assert( blob_is_reset(&branch) );
208
content_deltify(rootid, &brid, 1, 0);
209
zUuid = rid_to_uuid(brid);
210
fossil_print("New branch: %s\n", zUuid);
211
if( g.argc==3 ){
212
fossil_print(
213
"\n"
214
"Note: the local check-out has not been updated to the new\n"
215
" branch. To begin working on the new branch, do this:\n"
216
"\n"
217
" %s update %s\n",
218
g.argv[0], zBranch
219
);
220
}
221
222
223
/* Commit */
224
db_end_transaction(0);
225
226
/* Do an autosync push, if requested */
227
if( !isPrivate ) autosync_loop(SYNC_PUSH, 0, "branch");
228
}
229
230
/*
231
** Create a TEMP table named "tmp_brlist" with 7 columns:
232
**
233
** name Name of the branch
234
** mtime Time of last check-in on this branch
235
** isclosed True if the branch is closed
236
** mergeto Another branch this branch was merged into
237
** nckin Number of check-ins on this branch
238
** ckin Hash of the last check-in on this branch
239
** isprivate True if the branch is private
240
** bgclr Background color for this branch
241
*/
242
static const char createBrlistQuery[] =
243
@ CREATE TEMP TABLE IF NOT EXISTS tmp_brlist AS
244
@ SELECT
245
@ tagxref.value AS name,
246
@ max(event.mtime) AS mtime,
247
@ EXISTS(SELECT 1 FROM tagxref AS tx
248
@ WHERE tx.rid=tagxref.rid
249
@ AND tx.tagid=(SELECT tagid FROM tag WHERE tagname='closed')
250
@ AND tx.tagtype>0) AS isclosed,
251
@ (SELECT tagxref.value
252
@ FROM plink CROSS JOIN tagxref
253
@ WHERE plink.pid=event.objid
254
@ AND tagxref.rid=plink.cid
255
@ AND tagxref.tagid=(SELECT tagid FROM tag WHERE tagname='branch')
256
@ AND tagtype>0) AS mergeto,
257
@ count(*) AS nckin,
258
@ (SELECT uuid FROM blob WHERE rid=tagxref.rid) AS ckin,
259
@ event.bgcolor AS bgclr,
260
@ EXISTS(SELECT 1 FROM private WHERE rid=tagxref.rid) AS isprivate
261
@ FROM tagxref, tag, event
262
@ WHERE tagxref.tagid=tag.tagid
263
@ AND tagxref.tagtype>0
264
@ AND tag.tagname='branch'
265
@ AND event.objid=tagxref.rid
266
@ GROUP BY 1;
267
;
268
269
/* Call this routine to create the TEMP table */
270
static void brlist_create_temp_table(void){
271
db_exec_sql(createBrlistQuery);
272
}
273
274
275
#if INTERFACE
276
/*
277
** Allows bits in the mBplqFlags parameter to branch_prepare_list_query().
278
*/
279
#define BRL_CLOSED_ONLY 0x001 /* Show only closed branches */
280
#define BRL_OPEN_ONLY 0x002 /* Show only open branches */
281
#define BRL_BOTH 0x003 /* Show both open and closed branches */
282
#define BRL_OPEN_CLOSED_MASK 0x003
283
#define BRL_ORDERBY_MTIME 0x004 /* Sort by MTIME. (otherwise sort by name)*/
284
#define BRL_REVERSE 0x008 /* Reverse the sort order */
285
#define BRL_PRIVATE 0x010 /* Show only private branches */
286
#define BRL_MERGED 0x020 /* Show only merged branches */
287
#define BRL_UNMERGED 0x040 /* Show only unmerged branches */
288
#define BRL_LIST_USERS 0x080 /* Populate list of users participating */
289
290
#endif /* INTERFACE */
291
292
/*
293
** Prepare a query that will list branches.
294
**
295
** If the BRL_ORDERBY_MTIME flag is set and nLimitMRU ("Limit Most Recently Used
296
** style") is a non-zero number, the result is limited to nLimitMRU entries, and
297
** the BRL_REVERSE flag is applied in an outer query after processing the limit,
298
** so that it's possible to generate short lists with the most recently modified
299
** branches sorted chronologically in either direction, as does the "branch lsh"
300
** command.
301
** For other cases, the outer query is also generated, but works as a no-op. The
302
** code to build the outer query is marked with *//* OUTER QUERY *//* comments.
303
*/
304
void branch_prepare_list_query(
305
Stmt *pQuery,
306
int brFlags,
307
const char *zBrNameGlob,
308
int nLimitMRU,
309
const char *zUser
310
){
311
Blob sql;
312
blob_init(&sql, 0, 0);
313
brlist_create_temp_table();
314
/* Ignore nLimitMRU if no chronological sort requested. */
315
if( (brFlags & BRL_ORDERBY_MTIME)==0 ) nLimitMRU = 0;
316
/* Negative values for nLimitMRU also mean "no limit". */
317
if( nLimitMRU<0 ) nLimitMRU = 0;
318
/* OUTER QUERY */
319
blob_append_sql(&sql,"SELECT name, isprivate, mergeto,");
320
if( brFlags & BRL_LIST_USERS ){
321
blob_append_sql(&sql,
322
" (SELECT group_concat(user) FROM ("
323
" SELECT DISTINCT * FROM ("
324
" SELECT coalesce(euser,user) AS user"
325
" FROM event"
326
" WHERE type='ci' AND objid IN ("
327
" SELECT rid FROM tagxref WHERE value=name)"
328
" ORDER BY 1)))"
329
);
330
}else{
331
blob_append_sql(&sql, " NULL");
332
}
333
blob_append_sql(&sql," FROM (");
334
/* INNER QUERY */
335
switch( brFlags & BRL_OPEN_CLOSED_MASK ){
336
case BRL_CLOSED_ONLY: {
337
blob_append_sql(&sql,
338
"SELECT name, isprivate, mtime, mergeto FROM tmp_brlist WHERE isclosed"
339
);
340
break;
341
}
342
case BRL_BOTH: {
343
blob_append_sql(&sql,
344
"SELECT name, isprivate, mtime, mergeto FROM tmp_brlist WHERE 1"
345
);
346
break;
347
}
348
case BRL_OPEN_ONLY: {
349
blob_append_sql(&sql,
350
"SELECT name, isprivate, mtime, mergeto FROM tmp_brlist "
351
" WHERE NOT isclosed"
352
);
353
break;
354
}
355
}
356
if( brFlags & BRL_PRIVATE ) blob_append_sql(&sql, " AND isprivate");
357
if( brFlags & BRL_MERGED ) blob_append_sql(&sql, " AND mergeto IS NOT NULL");
358
if( zBrNameGlob ) blob_append_sql(&sql, " AND (name GLOB %Q)", zBrNameGlob);
359
if( zUser && zUser[0] ) blob_append_sql(&sql,
360
" AND EXISTS (SELECT 1 FROM event WHERE type='ci' AND (user=%Q OR euser=%Q)"
361
" AND objid in (SELECT rid FROM tagxref WHERE value=tmp_brlist.name))",
362
zUser, zUser
363
);
364
if( brFlags & BRL_ORDERBY_MTIME ){
365
blob_append_sql(&sql, " ORDER BY -mtime");
366
}else{
367
blob_append_sql(&sql, " ORDER BY name COLLATE nocase");
368
}
369
if( brFlags & BRL_REVERSE && !nLimitMRU ){
370
blob_append_sql(&sql," DESC");
371
}
372
if( nLimitMRU ){
373
blob_append_sql(&sql," LIMIT %d",nLimitMRU);
374
}
375
blob_append_sql(&sql,")"); /* OUTER QUERY */
376
if( brFlags & BRL_REVERSE && nLimitMRU ){
377
blob_append_sql(&sql," ORDER BY mtime"); /* OUTER QUERY */
378
}
379
db_prepare_blob(pQuery, &sql);
380
blob_reset(&sql);
381
}
382
383
/*
384
** If the branch named in the argument is open, return a RID for one of
385
** the open leaves of that branch. If the branch does not exists or is
386
** closed, return 0.
387
*/
388
int branch_is_open(const char *zBrName){
389
return db_int(0,
390
"SELECT rid FROM tagxref AS ox"
391
" WHERE tagid=%d"
392
" AND tagtype=2"
393
" AND value=%Q"
394
" AND rid IN leaf"
395
" AND NOT EXISTS(SELECT 1 FROM tagxref AS ix"
396
" WHERE tagid=%d"
397
" AND tagtype=1"
398
" AND ox.rid=ix.rid)",
399
TAG_BRANCH, zBrName, TAG_CLOSED
400
);
401
}
402
403
/*
404
** Internal helper for branch_cmd_close() and friends. Adds a row to
405
** the to the brcmdtag TEMP table, initializing that table if needed,
406
** holding a pending tag for the given blob.rid (which is assumed to
407
** be valid). zTag must be a fully-formed tag name, including the
408
** (+,-,*) prefix character.
409
**
410
*/
411
static void branch_cmd_tag_add(int rid, const char *zTag){
412
static int once = 0;
413
assert(zTag && ('+'==zTag[0] || '-'==zTag[0] || '*'==zTag[0]));
414
if(0==once++){
415
db_multi_exec("CREATE TEMP TABLE brcmdtag("
416
"rid INTEGER UNIQUE ON CONFLICT IGNORE,"
417
"tag TEXT NOT NULL"
418
")");
419
}
420
db_multi_exec("INSERT INTO brcmdtag(rid,tag) VALUES(%d,%Q)",
421
rid, zTag);
422
}
423
424
/*
425
** Internal helper for branch_cmd_close() and friends. Creates and
426
** saves a control artifact of tag changes stored via
427
** branch_cmd_tag_add(). Fails fatally on error, returns 0 if it saves
428
** an artifact, and a negative value if it does not save anything
429
** because no tags were queued up. A positive return value is reserved
430
** for potential future semantics.
431
**
432
** This function asserts that a transaction is underway and it ends
433
** the transaction, committing or rolling back, as appropriate.
434
*/
435
static int branch_cmd_tag_finalize(int fDryRun /* roll back if true */,
436
int fVerbose /* output extra info */,
437
const char *zDateOvrd /* --date-override */,
438
const char *zUserOvrd /* --user-override */){
439
int nTags = 0;
440
Stmt q = empty_Stmt;
441
Blob manifest = empty_blob;
442
int doRollback = fDryRun!=0;
443
444
assert(db_transaction_nesting_depth() > 0);
445
if(!db_table_exists("temp","brcmdtag")){
446
fossil_warning("No tags added - nothing to do.");
447
db_end_transaction(1);
448
return -1;
449
}
450
db_prepare(&q, "SELECT b.uuid, t.tag "
451
"FROM blob b, brcmdtag t "
452
"WHERE b.rid=t.rid "
453
"ORDER BY t.tag, b.uuid");
454
blob_appendf(&manifest, "D %z\n",
455
date_in_standard_format( zDateOvrd ? zDateOvrd : "now"));
456
while(SQLITE_ROW==db_step(&q)){
457
const char * zHash = db_column_text(&q, 0);
458
const char * zTag = db_column_text(&q, 1);
459
blob_appendf(&manifest, "T %s %s\n", zTag, zHash);
460
++nTags;
461
}
462
if(!nTags){
463
fossil_warning("No tags added - nothing to do.");
464
db_end_transaction(1);
465
blob_reset(&manifest);
466
return -1;
467
}
468
user_select();
469
blob_appendf(&manifest, "U %F\n", zUserOvrd ? zUserOvrd : login_name());
470
{ /* Z-card and save artifact */
471
int newRid;
472
Blob cksum = empty_blob;
473
md5sum_blob(&manifest, &cksum);
474
blob_appendf(&manifest, "Z %b\n", &cksum);
475
blob_reset(&cksum);
476
if(fDryRun && fVerbose){
477
fossil_print("Dry-run mode: will roll back new artifact:\n%b",
478
&manifest);
479
/* Run through the saving steps, though, noting that doing so
480
** will clear out &manifest, which is why we output it here
481
** instead of after saving. */
482
}
483
newRid = content_put(&manifest);
484
if(0==newRid){
485
fossil_fatal("Problem saving new artifact: %s\n%b",
486
g.zErrMsg, &manifest);
487
}else if(manifest_crosslink(newRid, &manifest, 0)==0){
488
fossil_fatal("Crosslinking error: %s", g.zErrMsg);
489
}
490
fossil_print("Saved new control artifact %z (RID %d).\n",
491
rid_to_uuid(newRid), newRid);
492
db_add_unsent(newRid);
493
if(fDryRun){
494
fossil_print("Dry-run mode: rolling back new artifact.\n");
495
assert(0!=doRollback);
496
}
497
}
498
db_multi_exec("DROP TABLE brcmdtag");
499
blob_reset(&manifest);
500
db_end_transaction(doRollback);
501
return 0;
502
}
503
504
/*
505
** Internal helper for branch_cmd_close() and friends. zName is a
506
** symbolic check-in name. Returns the blob.rid of the check-in or fails
507
** fatally if the name does not resolve unambiguously. If zUuid is
508
** not NULL, *zUuid is set to the resolved blob.uuid and must be freed
509
** by the caller via fossil_free().
510
*/
511
static int branch_resolve_name(char const *zName, char **zUuid){
512
const int rid = name_to_uuid2(zName, "ci", zUuid);
513
if(0==rid){
514
fossil_fatal("Cannot resolve name: %s", zName);
515
}else if(rid<0){
516
fossil_fatal("Ambiguous name: %s", zName);
517
}
518
return rid;
519
}
520
521
/*
522
** Implementation of (branch hide/unhide) subcommands. nStartAtArg is
523
** the g.argv index to start reading branch/check-in names. fHide is
524
** true for hiding, false for unhiding. Fails fatally on error.
525
*/
526
static void branch_cmd_hide(int nStartAtArg, int fHide){
527
int argPos = nStartAtArg; /* g.argv pos with first branch/ci name */
528
char * zUuid = 0; /* Resolved branch UUID. */
529
const int fVerbose = find_option("verbose","v",0)!=0;
530
const int fDryRun = find_option("dry-run","n",0)!=0;
531
const char *zDateOvrd = find_option("date-override",0,1);
532
const char *zUserOvrd = find_option("user-override",0,1);
533
534
verify_all_options();
535
db_begin_transaction();
536
for( ; argPos < g.argc; fossil_free(zUuid), ++argPos ){
537
const char * zName = g.argv[argPos];
538
const int rid = branch_resolve_name(zName, &zUuid);
539
const int isHidden = rid_has_tag(rid, TAG_HIDDEN);
540
/* Potential TODO: check for existing 'hidden' flag and skip this
541
** entry if it already has (if fHide) or does not have (if !fHide)
542
** that tag. FWIW, /ci_edit does not do so. */
543
if(fHide && isHidden){
544
fossil_warning("Skipping hidden check-in %s: %s.", zName, zUuid);
545
continue;
546
}else if(!fHide && !isHidden){
547
fossil_warning("Skipping non-hidden check-in %s: %s.", zName, zUuid);
548
continue;
549
}
550
branch_cmd_tag_add(rid, fHide ? "*hidden" : "-hidden");
551
if(fVerbose!=0){
552
fossil_print("%s check-in [%s] %s\n",
553
fHide ? "Hiding" : "Unhiding",
554
zName, zUuid);
555
}
556
}
557
branch_cmd_tag_finalize(fDryRun, fVerbose, zDateOvrd, zUserOvrd);
558
}
559
560
/*
561
** Implementation of (branch close|reopen) subcommands. nStartAtArg is
562
** the g.argv index to start reading branch/check-in names. The given
563
** check-ins are closed if fClose is true, else their "closed" tag (if
564
** any) is cancelled. Fails fatally on error.
565
*/
566
static void branch_cmd_close(int nStartAtArg, int fClose){
567
int argPos = nStartAtArg; /* g.argv pos with first branch name */
568
char * zUuid = 0; /* Resolved branch UUID. */
569
const int fVerbose = find_option("verbose","v",0)!=0;
570
const int fDryRun = find_option("dry-run","n",0)!=0;
571
const char *zDateOvrd = find_option("date-override",0,1);
572
const char *zUserOvrd = find_option("user-override",0,1);
573
574
verify_all_options();
575
db_begin_transaction();
576
for( ; argPos < g.argc; fossil_free(zUuid), ++argPos ){
577
const char * zName = g.argv[argPos];
578
const int rid = branch_resolve_name(zName, &zUuid);
579
const int isClosed = leaf_is_closed(rid);
580
if(!is_a_leaf(rid)){
581
/* This behaviour is different from /ci_edit closing, where
582
** is_a_leaf() adds a "+" tag and !is_a_leaf() adds a "*"
583
** tag. We might want to change this to match for consistency's
584
** sake, but it currently seems unnecessary to close/re-open a
585
** non-leaf. */
586
fossil_warning("Skipping non-leaf [%s] %s", zName, zUuid);
587
continue;
588
}else if(fClose && isClosed){
589
fossil_warning("Skipping closed leaf [%s] %s", zName, zUuid);
590
continue;
591
}else if(!fClose && !isClosed){
592
fossil_warning("Skipping non-closed leaf [%s] %s", zName, zUuid);
593
continue;
594
}
595
branch_cmd_tag_add(rid, fClose ? "+closed" : "-closed");
596
if(fVerbose!=0){
597
fossil_print("%s branch [%s] %s\n",
598
fClose ? "Closing" : "Re-opening",
599
zName, zUuid);
600
}
601
}
602
branch_cmd_tag_finalize(fDryRun, fVerbose, zDateOvrd, zUserOvrd);
603
}
604
605
/*
606
** COMMAND: branch
607
**
608
** Usage: %fossil branch SUBCOMMAND ... ?OPTIONS?
609
**
610
** Run various subcommands to manage branches of the open repository or
611
** of the repository identified by the -R or --repository option.
612
**
613
** > fossil branch close|reopen ?OPTIONS? BRANCH-NAME ?...BRANCH-NAMES?
614
**
615
** Adds or cancels the "closed" tag to one or more branches.
616
** It accepts arbitrary unambiguous symbolic names but
617
** will only resolve check-in names and skips any which resolve
618
** to non-leaf check-ins.
619
**
620
** Options:
621
** -n|--dry-run Do not commit changes, but dump artifact
622
** to stdout
623
** -v|--verbose Output more information
624
** --date-override DATE DATE to use instead of 'now'
625
** --user-override USER USER to use instead of the current default
626
**
627
** > fossil branch current
628
**
629
** Print the name of the branch for the current check-out
630
**
631
** > fossil branch hide|unhide ?OPTIONS? BRANCH-NAME ?...BRANCH-NAMES?
632
**
633
** Adds or cancels the "hidden" tag for the specified branches or
634
** or check-in IDs. Accepts the same options as the close
635
** subcommand.
636
**
637
** > fossil branch info BRANCH-NAME
638
**
639
** Print information about a branch
640
**
641
** > fossil branch list|ls ?OPTIONS? ?GLOB?
642
** > fossil branch lsh ?OPTIONS? ?LIMIT?
643
**
644
** List all branches.
645
**
646
** Options:
647
** -a|--all List all branches. Default show only open branches
648
** -c|--closed List closed branches
649
** -m|--merged List branches merged into the current branch
650
** -M|--unmerged List branches not merged into the current branch
651
** -p List only private branches
652
** -r Reverse the sort order
653
** -t Show recently changed branches first
654
** --self List only branches where you participate
655
** --username USER List only branches where USER participates
656
** --users N List up to N users participating
657
**
658
** The current branch is marked with an asterisk. Private branches are
659
** marked with a hash sign.
660
**
661
** If GLOB is given, show only branches matching the pattern.
662
**
663
** The "lsh" variant of this subcommand shows recently changed branches,
664
** and accepts an optional LIMIT argument (defaults to 5) to cap output,
665
** but no GLOB argument. All other options are supported, with -t being
666
** an implied no-op.
667
**
668
** > fossil branch new BRANCH-NAME BASIS ?OPTIONS?
669
**
670
** Create a new branch BRANCH-NAME off of check-in BASIS.
671
**
672
** This command is available for people who want to create a branch
673
** in advance. But the use of this command is discouraged. The
674
** preferred idiom in Fossil is to create new branches at the point
675
** of need, using the "--branch NAME" option to the "fossil commit"
676
** command.
677
**
678
** Options:
679
** --private Branch is private (i.e., remains local)
680
** --bgcolor COLOR Use COLOR instead of automatic background
681
** --nosign Do not sign the manifest for the check-in
682
** that creates this branch
683
** --nosync Do not auto-sync prior to creating the branch
684
** --date-override DATE DATE to use instead of 'now'
685
** --user-override USER USER to use instead of the current default
686
**
687
** Options:
688
** -R|--repository REPO Run commands on repository REPO
689
*/
690
void branch_cmd(void){
691
int n;
692
const char *zCmd = "list";
693
db_find_and_open_repository(0, 0);
694
if( g.argc>=3 ) zCmd = g.argv[2];
695
n = strlen(zCmd);
696
if( strncmp(zCmd,"current",n)==0 ){
697
if( !g.localOpen ){
698
fossil_fatal("not within an open check-out");
699
}else{
700
int vid = db_lget_int("checkout", 0);
701
char *zCurrent = db_text(0, "SELECT value FROM tagxref"
702
" WHERE rid=%d AND tagid=%d", vid, TAG_BRANCH);
703
fossil_print("%s\n", zCurrent);
704
fossil_free(zCurrent);
705
}
706
}else if( strncmp(zCmd,"info",n)==0 ){
707
int i;
708
for(i=3; i<g.argc; i++){
709
const char *zBrName = g.argv[i];
710
int rid = branch_is_open(zBrName);
711
if( rid==0 ){
712
fossil_print("%s: not an open branch\n", zBrName);
713
}else{
714
const char *zUuid = db_text(0,"SELECT uuid FROM blob WHERE rid=%d",rid);
715
const char *zDate = db_text(0,
716
"SELECT datetime(mtime,toLocal()) FROM event"
717
" WHERE objid=%d", rid);
718
fossil_print("%s: open as of %s on %.16s\n", zBrName, zDate, zUuid);
719
}
720
}
721
}else if( strncmp(zCmd,"list",n)==0 ||
722
strncmp(zCmd, "ls", n)==0 ||
723
strcmp(zCmd, "lsh")==0 ){
724
Stmt q;
725
Blob txt = empty_blob;
726
int vid;
727
char *zCurrent = 0;
728
const char *zBrNameGlob = 0;
729
const char *zUser = find_option("username",0,1);
730
const char *zUsersOpt = find_option("users",0,1);
731
int nUsers = zUsersOpt ? atoi(zUsersOpt) : 0;
732
int nLimit = 0;
733
int brFlags = BRL_OPEN_ONLY;
734
if( find_option("all","a",0)!=0 ) brFlags = BRL_BOTH;
735
if( find_option("closed","c",0)!=0 ) brFlags = BRL_CLOSED_ONLY;
736
if( find_option("t",0,0)!=0 ) brFlags |= BRL_ORDERBY_MTIME;
737
if( find_option("r",0,0)!=0 ) brFlags |= BRL_REVERSE;
738
if( find_option("p",0,0)!=0 ) brFlags |= BRL_PRIVATE;
739
if( find_option("merged","m",0)!=0 ) brFlags |= BRL_MERGED;
740
if( find_option("unmerged","M",0)!=0 ) brFlags |= BRL_UNMERGED;
741
if( find_option("self",0,0)!=0 ){
742
if( zUser ){
743
fossil_fatal("flags --username and --self are mutually exclusive");
744
}
745
user_select();
746
zUser = login_name();
747
}
748
verify_all_options();
749
750
if ( (brFlags & BRL_MERGED) && (brFlags & BRL_UNMERGED) ){
751
fossil_fatal("flags --merged and --unmerged are mutually exclusive");
752
}
753
if( zUsersOpt ){
754
if( nUsers <= 0) fossil_fatal("With --users, N must be positive");
755
brFlags |= BRL_LIST_USERS;
756
}
757
if( strcmp(zCmd, "lsh")==0 ){
758
nLimit = 5;
759
if( g.argc>4 || (g.argc==4 && (nLimit = atoi(g.argv[3]))==0) ){
760
fossil_fatal("the lsh subcommand allows one optional numeric argument");
761
}
762
brFlags |= BRL_ORDERBY_MTIME;
763
}else{
764
if( g.argc >= 4 ) zBrNameGlob = g.argv[3];
765
}
766
767
if( g.localOpen ){
768
vid = db_lget_int("checkout", 0);
769
zCurrent = db_text(0, "SELECT value FROM tagxref"
770
" WHERE rid=%d AND tagid=%d", vid, TAG_BRANCH);
771
}
772
branch_prepare_list_query(&q, brFlags, zBrNameGlob, nLimit, zUser);
773
blob_init(&txt, 0, 0);
774
while( db_step(&q)==SQLITE_ROW ){
775
const char *zBr = db_column_text(&q, 0);
776
int isPriv = db_column_int(&q, 1)==1;
777
const char *zMergeTo = db_column_text(&q, 2);
778
int isCur = zCurrent!=0 && fossil_strcmp(zCurrent,zBr)==0;
779
const char *zUsers = db_column_text(&q, 3);
780
if( (brFlags & BRL_MERGED) && fossil_strcmp(zCurrent,zMergeTo)!=0 ){
781
continue;
782
}
783
if( (brFlags & BRL_UNMERGED) && (fossil_strcmp(zCurrent,zMergeTo)==0
784
|| isCur) ){
785
continue;
786
}
787
blob_appendf(&txt, "%s%s%s",
788
( (brFlags & BRL_PRIVATE) ? " " : ( isPriv ? "#" : " ") ),
789
(isCur ? "* " : " "), zBr);
790
if( nUsers ){
791
char c;
792
const char *cp;
793
const char *pComma = 0;
794
int commas = 0;
795
for( cp = zUsers; ( c = *cp ) != 0; cp++ ){
796
if( c == ',' ){
797
commas++;
798
if( commas == nUsers ) pComma = cp;
799
}
800
}
801
if( pComma ){
802
blob_appendf(&txt, " (%.*s,... %i more)",
803
pComma - zUsers, zUsers, commas + 1 - nUsers);
804
}else{
805
blob_appendf(&txt, " (%s)", zUsers);
806
}
807
}
808
fossil_print("%s\n", blob_str(&txt));
809
blob_reset(&txt);
810
}
811
db_finalize(&q);
812
}else if( strncmp(zCmd,"new",n)==0 ){
813
branch_new();
814
}else if( strncmp(zCmd,"close",5)==0 ){
815
if(g.argc<4){
816
usage("close branch-name(s)...");
817
}
818
branch_cmd_close(3, 1);
819
}else if( strncmp(zCmd,"reopen",6)==0 ){
820
if(g.argc<4){
821
usage("reopen branch-name(s)...");
822
}
823
branch_cmd_close(3, 0);
824
}else if( strncmp(zCmd,"hide",4)==0 ){
825
if(g.argc<4){
826
usage("hide branch-name(s)...");
827
}
828
branch_cmd_hide(3,1);
829
}else if( strncmp(zCmd,"unhide",6)==0 ){
830
if(g.argc<4){
831
usage("unhide branch-name(s)...");
832
}
833
branch_cmd_hide(3,0);
834
}else{
835
fossil_fatal("branch subcommand should be one of: "
836
"close current hide info list ls lsh new reopen unhide");
837
}
838
}
839
840
/*
841
** This is the new-style branch-list page that shows the branch names
842
** together with their ages (time of last check-in) and whether or not
843
** they are closed or merged to another branch.
844
**
845
** Control jumps to this routine from brlist_page() (the /brlist handler)
846
** if there are no query parameters.
847
*/
848
static void new_brlist_page(void){
849
Stmt q;
850
double rNow;
851
int show_colors = PB("colors");
852
const char *zMainBranch;
853
login_check_credentials();
854
if( !g.perm.Read ){ login_needed(g.anon.Read); return; }
855
style_set_current_feature("branch");
856
style_header("Branches");
857
style_adunit_config(ADUNIT_RIGHT_OK);
858
style_submenu_checkbox("colors", "Use Branch Colors", 0, 0);
859
860
login_anonymous_available();
861
zMainBranch = db_main_branch();
862
863
brlist_create_temp_table();
864
db_prepare(&q, "SELECT * FROM tmp_brlist ORDER BY mtime DESC");
865
rNow = db_double(0.0, "SELECT julianday('now')");
866
@ <script id="brlist-data" type="application/json">\
867
@ {"timelineUrl":"%R/timeline"}</script>
868
@ <div class="brlist">
869
@ <table class='sortable' data-column-types='tkNtt' data-init-sort='2'>
870
@ <thead><tr>
871
@ <th>Branch Name</th>
872
@ <th>Last Change</th>
873
@ <th>Check-ins</th>
874
@ <th>Status</th>
875
@ <th>Resolution</th>
876
@ </tr></thead><tbody>
877
while( db_step(&q)==SQLITE_ROW ){
878
const char *zBranch = db_column_text(&q, 0);
879
double rMtime = db_column_double(&q, 1);
880
int isClosed = db_column_int(&q, 2);
881
const char *zMergeTo = db_column_text(&q, 3);
882
int nCkin = db_column_int(&q, 4);
883
const char *zLastCkin = db_column_text(&q, 5);
884
const char *zBgClr = db_column_text(&q, 6);
885
char *zAge = human_readable_age(rNow - rMtime);
886
sqlite3_int64 iMtime = (sqlite3_int64)(rMtime*86400.0);
887
if( zMergeTo && zMergeTo[0]==0 ) zMergeTo = 0;
888
if( zBgClr ) zBgClr = reasonable_bg_color(zBgClr, 0);
889
if( zBgClr==0 ){
890
if( zBranch==0 || strcmp(zBranch, zMainBranch)==0 ){
891
zBgClr = 0;
892
}else{
893
zBgClr = hash_color(zBranch);
894
}
895
}
896
if( zBgClr && zBgClr[0] && show_colors ){
897
@ <tr style="background-color:%s(zBgClr)">
898
}else{
899
@ <tr>
900
}
901
@ <td>%z(href("%R/timeline?r=%T",zBranch))%h(zBranch)</a><input
902
@ type="checkbox" disabled="disabled"/></td>
903
@ <td data-sortkey="%016llx(iMtime)">%s(zAge)</td>
904
@ <td>%d(nCkin)</td>
905
fossil_free(zAge);
906
@ <td>%s(isClosed?"closed":"")</td>
907
if( zMergeTo ){
908
@ <td>merged into
909
@ %z(href("%R/timeline?f=%!S",zLastCkin))%h(zMergeTo)</a></td>
910
}else{
911
@ <td></td>
912
}
913
@ </tr>
914
}
915
@ </tbody></table></div>
916
db_finalize(&q);
917
builtin_request_js("fossil.page.brlist.js");
918
style_table_sorter();
919
style_finish_page();
920
}
921
922
/*
923
** WEBPAGE: brlist
924
** Show a list of branches. With no query parameters, a sortable table
925
** is used to show all branches. If query parameters are present a
926
** fixed bullet list is shown.
927
**
928
** Query parameters:
929
**
930
** all Show all branches
931
** closed Show only closed branches
932
** open Show only open branches
933
** colortest Show all branches with automatic color
934
**
935
** When there are no query parameters, a new-style /brlist page shows
936
** all branches in a sortable table. The new-style /brlist page is
937
** preferred and is the default.
938
*/
939
void brlist_page(void){
940
Stmt q;
941
int cnt;
942
int showClosed = P("closed")!=0;
943
int showAll = P("all")!=0;
944
int showOpen = P("open")!=0;
945
int colorTest = P("colortest")!=0;
946
int brFlags = BRL_OPEN_ONLY;
947
948
if( showClosed==0 && showAll==0 && showOpen==0 && colorTest==0 ){
949
new_brlist_page();
950
return;
951
}
952
login_check_credentials();
953
if( !g.perm.Read ){ login_needed(g.anon.Read); return; }
954
cgi_check_for_malice();
955
if( colorTest ){
956
showClosed = 0;
957
showAll = 1;
958
}
959
if( showAll ) brFlags = BRL_BOTH;
960
if( showClosed ) brFlags = BRL_CLOSED_ONLY;
961
962
style_set_current_feature("branch");
963
style_header("%s", showClosed ? "Closed Branches" :
964
showAll ? "All Branches" : "Open Branches");
965
style_submenu_element("Timeline", "brtimeline");
966
if( showClosed ){
967
style_submenu_element("All", "brlist?all");
968
style_submenu_element("Open", "brlist?open");
969
}else if( showAll ){
970
style_submenu_element("Closed", "brlist?closed");
971
style_submenu_element("Open", "brlist");
972
}else{
973
style_submenu_element("All", "brlist?all");
974
style_submenu_element("Closed", "brlist?closed");
975
}
976
if( !colorTest ){
977
style_submenu_element("Color-Test", "brlist?colortest");
978
}else{
979
style_submenu_element("All", "brlist?all");
980
}
981
login_anonymous_available();
982
#if 0
983
style_sidebox_begin("Nomenclature:", "33%");
984
@ <ol>
985
@ <li> An <div class="sideboxDescribed">%z(href("brlist"))
986
@ open branch</a></div> is a branch that has one or more
987
@ <div class="sideboxDescribed">%z(href("leaves"))open leaves.</a></div>
988
@ The presence of open leaves presumably means
989
@ that the branch is still being extended with new check-ins.</li>
990
@ <li> A <div class="sideboxDescribed">%z(href("brlist?closed"))
991
@ closed branch</a></div> is a branch with only
992
@ <div class="sideboxDescribed">%z(href("leaves?closed"))
993
@ closed leaves</a></div>.
994
@ Closed branches are fixed and do not change (unless they are first
995
@ reopened).</li>
996
@ </ol>
997
style_sidebox_end();
998
#endif
999
1000
branch_prepare_list_query(&q, brFlags, 0, 0, 0);
1001
cnt = 0;
1002
while( db_step(&q)==SQLITE_ROW ){
1003
const char *zBr = db_column_text(&q, 0);
1004
if( cnt==0 ){
1005
if( colorTest ){
1006
@ <h2>Default background colors for all branches:</h2>
1007
}else if( showClosed ){
1008
@ <h2>Closed Branches:</h2>
1009
}else if( showAll ){
1010
@ <h2>All Branches:</h2>
1011
}else{
1012
@ <h2>Open Branches:</h2>
1013
}
1014
@ <ul>
1015
cnt++;
1016
}
1017
if( colorTest ){
1018
const char *zColor = hash_color(zBr);
1019
@ <li><span style="background-color: %s(zColor)">
1020
@ %h(zBr) &rarr; %s(zColor)</span></li>
1021
}else{
1022
@ <li>%z(href("%R/timeline?r=%T",zBr))%h(zBr)</a></li>
1023
}
1024
}
1025
if( cnt ){
1026
@ </ul>
1027
}
1028
db_finalize(&q);
1029
style_finish_page();
1030
}
1031
1032
/*
1033
** This routine is called while for each check-in that is rendered by
1034
** the timeline of a "brlist" page. Add some additional hyperlinks
1035
** to the end of the line.
1036
*/
1037
static void brtimeline_extra(
1038
Stmt *pQuery, /* Current row of the timeline query */
1039
int tmFlags, /* Flags to www_print_timeline() */
1040
const char *zThisUser, /* Suppress links to this user */
1041
const char *zThisTag /* Suppress links to this tag */
1042
){
1043
int rid;
1044
int tmFlagsNew;
1045
char *zBrName;
1046
1047
if( (tmFlags & TIMELINE_INLINE)!=0 ){
1048
tmFlagsNew = (tmFlags & ~TIMELINE_VIEWS) | TIMELINE_MODERN;
1049
cgi_printf("(");
1050
}else{
1051
tmFlagsNew = tmFlags;
1052
}
1053
timeline_extra(pQuery,tmFlagsNew,zThisUser,zThisTag);
1054
1055
if( !g.perm.Hyperlink ) return;
1056
rid = db_column_int(pQuery,0);
1057
zBrName = branch_of_rid(rid);
1058
@ branch:&nbsp;<span class='timelineHash'>\
1059
@ %z(href("%R/timeline?r=%T",zBrName))%h(zBrName)</a></span>
1060
if( (tmFlags & TIMELINE_INLINE)!=0 ){
1061
cgi_printf(")");
1062
}
1063
}
1064
1065
/*
1066
** WEBPAGE: brtimeline
1067
**
1068
** List the first check of every branch, starting with the most recent
1069
** and going backwards in time.
1070
**
1071
** Query parameters:
1072
**
1073
** ubg Color the graph by user, not by branch.
1074
*/
1075
void brtimeline_page(void){
1076
Blob sql = empty_blob;
1077
Stmt q;
1078
int tmFlags; /* Timeline display flags */
1079
int fNoHidden = PB("nohidden")!=0; /* The "nohidden" query parameter */
1080
int fOnlyHidden = PB("onlyhidden")!=0; /* The "onlyhidden" query parameter */
1081
1082
login_check_credentials();
1083
if( !g.perm.Read ){ login_needed(g.anon.Read); return; }
1084
if( robot_restrict("timelineX") ) return;
1085
1086
style_set_current_feature("branch");
1087
style_header("Branches");
1088
style_submenu_element("Branch List", "brlist");
1089
login_anonymous_available();
1090
timeline_ss_submenu();
1091
cgi_check_for_malice();
1092
@ <h2>The initial check-in for each branch:</h2>
1093
blob_append(&sql, timeline_query_for_www(), -1);
1094
blob_append_sql(&sql,
1095
"AND blob.rid IN (SELECT rid FROM tagxref"
1096
" WHERE tagtype>0 AND tagid=%d AND srcid!=0)", TAG_BRANCH);
1097
if( fNoHidden || fOnlyHidden ){
1098
const char* zUnaryOp = fNoHidden ? "NOT" : "";
1099
blob_append_sql(&sql,
1100
" AND %s EXISTS(SELECT 1 FROM tagxref"
1101
" WHERE tagid=%d AND tagtype>0 AND rid=blob.rid)\n",
1102
zUnaryOp/*safe-for-%s*/, TAG_HIDDEN);
1103
}
1104
db_prepare(&q, "%s ORDER BY event.mtime DESC", blob_sql_text(&sql));
1105
blob_reset(&sql);
1106
/* Always specify TIMELINE_DISJOINT, or graph_finish() may fail because of too
1107
** many descenders to (off-screen) parents. */
1108
tmFlags = TIMELINE_DISJOINT | TIMELINE_NOSCROLL;
1109
if( PB("ubg")!=0 ){
1110
tmFlags |= TIMELINE_UCOLOR;
1111
}else{
1112
tmFlags |= TIMELINE_BRCOLOR;
1113
}
1114
www_print_timeline(&q, tmFlags, 0, 0, 0, 0, 0, brtimeline_extra);
1115
db_finalize(&q);
1116
style_finish_page();
1117
}
1118
1119
/*
1120
** Generate a multichoice submenu for the few recent active branches. zName is
1121
** the query parameter used to select the current check-in. zCI is optional and
1122
** represent the currently selected check-in, so if it is a check-in hash
1123
** instead of a branch, it can be part of the multichoice menu.
1124
*/
1125
void generate_branch_submenu_multichoice(
1126
const char* zName, /* Query parameter name */
1127
const char* zCI /* Current check-in */
1128
){
1129
Stmt q;
1130
const int brFlags = BRL_ORDERBY_MTIME | BRL_OPEN_ONLY;
1131
static const char *zBranchMenuList[32*2]; /* 2 per entries */
1132
const int nLimit = count(zBranchMenuList)/2;
1133
int i = 0;
1134
1135
if( zName == 0 ) zName = "ci";
1136
1137
branch_prepare_list_query(&q, brFlags, 0, nLimit, 0);
1138
zBranchMenuList[i++] = "";
1139
zBranchMenuList[i++] = "All Check-ins";
1140
1141
if( zCI ){
1142
zCI = fossil_strdup(zCI);
1143
zBranchMenuList[i++] = zCI;
1144
zBranchMenuList[i++] = zCI;
1145
}
1146
/* If current check-in is not "tip", add it to the list */
1147
if( zCI==0 || strcmp(zCI, "tip") ){
1148
zBranchMenuList[i++] = "tip";
1149
zBranchMenuList[i++] = "tip";
1150
}
1151
while( i/2 < nLimit && db_step(&q)==SQLITE_ROW ){
1152
const char* zBr = fossil_strdup(db_column_text(&q, 0));
1153
/* zCI is already in the list, don't add it twice */
1154
if( zCI==0 || strcmp(zBr, zCI) ){
1155
zBranchMenuList[i++] = zBr;
1156
zBranchMenuList[i++] = zBr;
1157
}
1158
}
1159
db_finalize(&q);
1160
style_submenu_multichoice(zName, i/2, zBranchMenuList, 0);
1161
}
1162

Keyboard Shortcuts

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