Fossil SCM

Add the new subcommand `fossil branch lsh' to list the most recently modified branches.

florian 2022-08-10 06:00 trunk merge
Commit fc8e5750d7b05e78565b6d9a1f5d7c94c52e86a6a7d60cca9e49be78e611c964
1 file changed +55 -10
+55 -10
--- src/branch.c
+++ src/branch.c
@@ -289,31 +289,51 @@
289289
** Prepare a query that will list branches.
290290
**
291291
** If (which<0) then the query pulls only closed branches. If
292292
** (which>0) then the query pulls all (closed and opened)
293293
** branches. Else the query pulls currently-opened 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.
294303
*/
295
-void branch_prepare_list_query(Stmt *pQuery, int brFlags, const char *zBrNameGlob){
304
+void branch_prepare_list_query(
305
+ Stmt *pQuery,
306
+ int brFlags,
307
+ const char *zBrNameGlob,
308
+ int nLimitMRU
309
+){
296310
Blob sql;
297311
blob_init(&sql, 0, 0);
298312
brlist_create_temp_table();
313
+ /* Ignore nLimitMRU if no chronological sort requested. */
314
+ if( (brFlags & BRL_ORDERBY_MTIME)==0 ) nLimitMRU = 0;
315
+ /* Undocumented: invert negative values for nLimitMRU, so that command-line
316
+ ** arguments similar to `head -5' with "option numbers" are possible. */
317
+ if( nLimitMRU<0 ) nLimitMRU = -nLimitMRU;
318
+ blob_append_sql(&sql,"SELECT name, isprivate FROM ("); /* OUTER QUERY */
299319
switch( brFlags & BRL_OPEN_CLOSED_MASK ){
300320
case BRL_CLOSED_ONLY: {
301321
blob_append_sql(&sql,
302
- "SELECT name, isprivate FROM tmp_brlist WHERE isclosed"
322
+ "SELECT name, isprivate, mtime FROM tmp_brlist WHERE isclosed"
303323
);
304324
break;
305325
}
306326
case BRL_BOTH: {
307327
blob_append_sql(&sql,
308
- "SELECT name, isprivate FROM tmp_brlist WHERE 1"
328
+ "SELECT name, isprivate, mtime FROM tmp_brlist WHERE 1"
309329
);
310330
break;
311331
}
312332
case BRL_OPEN_ONLY: {
313333
blob_append_sql(&sql,
314
- "SELECT name, isprivate FROM tmp_brlist WHERE NOT isclosed"
334
+ "SELECT name, isprivate, mtime FROM tmp_brlist WHERE NOT isclosed"
315335
);
316336
break;
317337
}
318338
}
319339
if( brFlags & BRL_PRIVATE ) blob_append_sql(&sql, " AND isprivate");
@@ -321,13 +341,20 @@
321341
if( brFlags & BRL_ORDERBY_MTIME ){
322342
blob_append_sql(&sql, " ORDER BY -mtime");
323343
}else{
324344
blob_append_sql(&sql, " ORDER BY name COLLATE nocase");
325345
}
326
- if( brFlags & BRL_REVERSE ){
346
+ if( brFlags & BRL_REVERSE && !nLimitMRU ){
327347
blob_append_sql(&sql," DESC");
328348
}
349
+ if( nLimitMRU ){
350
+ blob_append_sql(&sql," LIMIT %d",nLimitMRU);
351
+ }
352
+ blob_append_sql(&sql,")"); /* OUTER QUERY */
353
+ if( brFlags & BRL_REVERSE && nLimitMRU ){
354
+ blob_append_sql(&sql," ORDER BY mtime"); /* OUTER QUERY */
355
+ }
329356
db_prepare_blob(pQuery, &sql);
330357
blob_reset(&sql);
331358
}
332359
333360
/*
@@ -585,10 +612,11 @@
585612
** > fossil branch info BRANCH-NAME
586613
**
587614
** Print information about a branch
588615
**
589616
** > fossil branch list|ls ?OPTIONS? ?GLOB?
617
+** > fossil branch lsh ?OPTIONS? ?LIMIT?
590618
**
591619
** List all branches. Options:
592620
** -a|--all List all branches. Default show only open branches
593621
** -c|--closed List closed branches.
594622
** -p List only private branches.
@@ -597,10 +625,15 @@
597625
**
598626
** The current branch is marked with an asterisk. Private branches are
599627
** marked with a hash sign.
600628
**
601629
** If GLOB is given, show only branches matching the pattern.
630
+**
631
+** The "lsh" variant of this subcommand shows recently changed branches,
632
+** and accepts an optional LIMIT argument (defaults to 5) to cap output,
633
+** but no GLOB argument. All other options are supported, with -t being
634
+** an implied no-op.
602635
**
603636
** > fossil branch new BRANCH-NAME BASIS ?OPTIONS?
604637
**
605638
** Create a new branch BRANCH-NAME off of check-in BASIS.
606639
** Supported options for this subcommand include:
@@ -651,29 +684,41 @@
651684
"SELECT datetime(mtime,toLocal()) FROM event"
652685
" WHERE objid=%d", rid);
653686
fossil_print("%s: open as of %s on %.16s\n", zBrName, zDate, zUuid);
654687
}
655688
}
656
- }else if( (strncmp(zCmd,"list",n)==0)||(strncmp(zCmd, "ls", n)==0) ){
689
+ }else if( strncmp(zCmd,"list",n)==0 ||
690
+ strncmp(zCmd, "ls", n)==0 ||
691
+ strcmp(zCmd, "lsh")==0 ){
657692
Stmt q;
658693
int vid;
659694
char *zCurrent = 0;
660695
const char *zBrNameGlob = 0;
696
+ int nLimit = 0;
661697
int brFlags = BRL_OPEN_ONLY;
662698
if( find_option("all","a",0)!=0 ) brFlags = BRL_BOTH;
663699
if( find_option("closed","c",0)!=0 ) brFlags = BRL_CLOSED_ONLY;
664700
if( find_option("t",0,0)!=0 ) brFlags |= BRL_ORDERBY_MTIME;
665701
if( find_option("r",0,0)!=0 ) brFlags |= BRL_REVERSE;
666702
if( find_option("p",0,0)!=0 ) brFlags |= BRL_PRIVATE;
667
- if( g.argc >= 4 ) zBrNameGlob = g.argv[3];
703
+
704
+ if( strcmp(zCmd, "lsh")==0 ){
705
+ nLimit = 5;
706
+ if( g.argc>4 || (g.argc==4 && (nLimit = atoi(g.argv[3]))==0) ){
707
+ fossil_fatal("the lsh subcommand allows one optional numeric argument");
708
+ }
709
+ brFlags |= BRL_ORDERBY_MTIME;
710
+ }else{
711
+ if( g.argc >= 4 ) zBrNameGlob = g.argv[3];
712
+ }
668713
669714
if( g.localOpen ){
670715
vid = db_lget_int("checkout", 0);
671716
zCurrent = db_text(0, "SELECT value FROM tagxref"
672717
" WHERE rid=%d AND tagid=%d", vid, TAG_BRANCH);
673718
}
674
- branch_prepare_list_query(&q, brFlags, zBrNameGlob);
719
+ branch_prepare_list_query(&q, brFlags, zBrNameGlob, nLimit);
675720
while( db_step(&q)==SQLITE_ROW ){
676721
const char *zBr = db_column_text(&q, 0);
677722
int isPriv = zCurrent!=0 && db_column_int(&q, 1)==1;
678723
int isCur = zCurrent!=0 && fossil_strcmp(zCurrent,zBr)==0;
679724
fossil_print("%s%s%s\n",
@@ -703,11 +748,11 @@
703748
usage("branch unhide branch-name(s)...");
704749
}
705750
branch_cmd_hide(3,0);
706751
}else{
707752
fossil_fatal("branch subcommand should be one of: "
708
- "close current hide info list ls new reopen unhide");
753
+ "close current hide info list ls lsh new reopen unhide");
709754
}
710755
}
711756
712757
/*
713758
** This is the new-style branch-list page that shows the branch names
@@ -862,11 +907,11 @@
862907
@ reopened).</li>
863908
@ </ol>
864909
style_sidebox_end();
865910
#endif
866911
867
- branch_prepare_list_query(&q, brFlags, 0);
912
+ branch_prepare_list_query(&q, brFlags, 0, 0);
868913
cnt = 0;
869914
while( db_step(&q)==SQLITE_ROW ){
870915
const char *zBr = db_column_text(&q, 0);
871916
if( cnt==0 ){
872917
if( colorTest ){
873918
--- src/branch.c
+++ src/branch.c
@@ -289,31 +289,51 @@
289 ** Prepare a query that will list branches.
290 **
291 ** If (which<0) then the query pulls only closed branches. If
292 ** (which>0) then the query pulls all (closed and opened)
293 ** branches. Else the query pulls currently-opened branches.
 
 
 
 
 
 
 
 
 
294 */
295 void branch_prepare_list_query(Stmt *pQuery, int brFlags, const char *zBrNameGlob){
 
 
 
 
 
296 Blob sql;
297 blob_init(&sql, 0, 0);
298 brlist_create_temp_table();
 
 
 
 
 
 
299 switch( brFlags & BRL_OPEN_CLOSED_MASK ){
300 case BRL_CLOSED_ONLY: {
301 blob_append_sql(&sql,
302 "SELECT name, isprivate FROM tmp_brlist WHERE isclosed"
303 );
304 break;
305 }
306 case BRL_BOTH: {
307 blob_append_sql(&sql,
308 "SELECT name, isprivate FROM tmp_brlist WHERE 1"
309 );
310 break;
311 }
312 case BRL_OPEN_ONLY: {
313 blob_append_sql(&sql,
314 "SELECT name, isprivate FROM tmp_brlist WHERE NOT isclosed"
315 );
316 break;
317 }
318 }
319 if( brFlags & BRL_PRIVATE ) blob_append_sql(&sql, " AND isprivate");
@@ -321,13 +341,20 @@
321 if( brFlags & BRL_ORDERBY_MTIME ){
322 blob_append_sql(&sql, " ORDER BY -mtime");
323 }else{
324 blob_append_sql(&sql, " ORDER BY name COLLATE nocase");
325 }
326 if( brFlags & BRL_REVERSE ){
327 blob_append_sql(&sql," DESC");
328 }
 
 
 
 
 
 
 
329 db_prepare_blob(pQuery, &sql);
330 blob_reset(&sql);
331 }
332
333 /*
@@ -585,10 +612,11 @@
585 ** > fossil branch info BRANCH-NAME
586 **
587 ** Print information about a branch
588 **
589 ** > fossil branch list|ls ?OPTIONS? ?GLOB?
 
590 **
591 ** List all branches. Options:
592 ** -a|--all List all branches. Default show only open branches
593 ** -c|--closed List closed branches.
594 ** -p List only private branches.
@@ -597,10 +625,15 @@
597 **
598 ** The current branch is marked with an asterisk. Private branches are
599 ** marked with a hash sign.
600 **
601 ** If GLOB is given, show only branches matching the pattern.
 
 
 
 
 
602 **
603 ** > fossil branch new BRANCH-NAME BASIS ?OPTIONS?
604 **
605 ** Create a new branch BRANCH-NAME off of check-in BASIS.
606 ** Supported options for this subcommand include:
@@ -651,29 +684,41 @@
651 "SELECT datetime(mtime,toLocal()) FROM event"
652 " WHERE objid=%d", rid);
653 fossil_print("%s: open as of %s on %.16s\n", zBrName, zDate, zUuid);
654 }
655 }
656 }else if( (strncmp(zCmd,"list",n)==0)||(strncmp(zCmd, "ls", n)==0) ){
 
 
657 Stmt q;
658 int vid;
659 char *zCurrent = 0;
660 const char *zBrNameGlob = 0;
 
661 int brFlags = BRL_OPEN_ONLY;
662 if( find_option("all","a",0)!=0 ) brFlags = BRL_BOTH;
663 if( find_option("closed","c",0)!=0 ) brFlags = BRL_CLOSED_ONLY;
664 if( find_option("t",0,0)!=0 ) brFlags |= BRL_ORDERBY_MTIME;
665 if( find_option("r",0,0)!=0 ) brFlags |= BRL_REVERSE;
666 if( find_option("p",0,0)!=0 ) brFlags |= BRL_PRIVATE;
667 if( g.argc >= 4 ) zBrNameGlob = g.argv[3];
 
 
 
 
 
 
 
 
 
668
669 if( g.localOpen ){
670 vid = db_lget_int("checkout", 0);
671 zCurrent = db_text(0, "SELECT value FROM tagxref"
672 " WHERE rid=%d AND tagid=%d", vid, TAG_BRANCH);
673 }
674 branch_prepare_list_query(&q, brFlags, zBrNameGlob);
675 while( db_step(&q)==SQLITE_ROW ){
676 const char *zBr = db_column_text(&q, 0);
677 int isPriv = zCurrent!=0 && db_column_int(&q, 1)==1;
678 int isCur = zCurrent!=0 && fossil_strcmp(zCurrent,zBr)==0;
679 fossil_print("%s%s%s\n",
@@ -703,11 +748,11 @@
703 usage("branch unhide branch-name(s)...");
704 }
705 branch_cmd_hide(3,0);
706 }else{
707 fossil_fatal("branch subcommand should be one of: "
708 "close current hide info list ls new reopen unhide");
709 }
710 }
711
712 /*
713 ** This is the new-style branch-list page that shows the branch names
@@ -862,11 +907,11 @@
862 @ reopened).</li>
863 @ </ol>
864 style_sidebox_end();
865 #endif
866
867 branch_prepare_list_query(&q, brFlags, 0);
868 cnt = 0;
869 while( db_step(&q)==SQLITE_ROW ){
870 const char *zBr = db_column_text(&q, 0);
871 if( cnt==0 ){
872 if( colorTest ){
873
--- src/branch.c
+++ src/branch.c
@@ -289,31 +289,51 @@
289 ** Prepare a query that will list branches.
290 **
291 ** If (which<0) then the query pulls only closed branches. If
292 ** (which>0) then the query pulls all (closed and opened)
293 ** branches. Else the query pulls currently-opened 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 ){
310 Blob sql;
311 blob_init(&sql, 0, 0);
312 brlist_create_temp_table();
313 /* Ignore nLimitMRU if no chronological sort requested. */
314 if( (brFlags & BRL_ORDERBY_MTIME)==0 ) nLimitMRU = 0;
315 /* Undocumented: invert negative values for nLimitMRU, so that command-line
316 ** arguments similar to `head -5' with "option numbers" are possible. */
317 if( nLimitMRU<0 ) nLimitMRU = -nLimitMRU;
318 blob_append_sql(&sql,"SELECT name, isprivate FROM ("); /* OUTER QUERY */
319 switch( brFlags & BRL_OPEN_CLOSED_MASK ){
320 case BRL_CLOSED_ONLY: {
321 blob_append_sql(&sql,
322 "SELECT name, isprivate, mtime FROM tmp_brlist WHERE isclosed"
323 );
324 break;
325 }
326 case BRL_BOTH: {
327 blob_append_sql(&sql,
328 "SELECT name, isprivate, mtime FROM tmp_brlist WHERE 1"
329 );
330 break;
331 }
332 case BRL_OPEN_ONLY: {
333 blob_append_sql(&sql,
334 "SELECT name, isprivate, mtime FROM tmp_brlist WHERE NOT isclosed"
335 );
336 break;
337 }
338 }
339 if( brFlags & BRL_PRIVATE ) blob_append_sql(&sql, " AND isprivate");
@@ -321,13 +341,20 @@
341 if( brFlags & BRL_ORDERBY_MTIME ){
342 blob_append_sql(&sql, " ORDER BY -mtime");
343 }else{
344 blob_append_sql(&sql, " ORDER BY name COLLATE nocase");
345 }
346 if( brFlags & BRL_REVERSE && !nLimitMRU ){
347 blob_append_sql(&sql," DESC");
348 }
349 if( nLimitMRU ){
350 blob_append_sql(&sql," LIMIT %d",nLimitMRU);
351 }
352 blob_append_sql(&sql,")"); /* OUTER QUERY */
353 if( brFlags & BRL_REVERSE && nLimitMRU ){
354 blob_append_sql(&sql," ORDER BY mtime"); /* OUTER QUERY */
355 }
356 db_prepare_blob(pQuery, &sql);
357 blob_reset(&sql);
358 }
359
360 /*
@@ -585,10 +612,11 @@
612 ** > fossil branch info BRANCH-NAME
613 **
614 ** Print information about a branch
615 **
616 ** > fossil branch list|ls ?OPTIONS? ?GLOB?
617 ** > fossil branch lsh ?OPTIONS? ?LIMIT?
618 **
619 ** List all branches. Options:
620 ** -a|--all List all branches. Default show only open branches
621 ** -c|--closed List closed branches.
622 ** -p List only private branches.
@@ -597,10 +625,15 @@
625 **
626 ** The current branch is marked with an asterisk. Private branches are
627 ** marked with a hash sign.
628 **
629 ** If GLOB is given, show only branches matching the pattern.
630 **
631 ** The "lsh" variant of this subcommand shows recently changed branches,
632 ** and accepts an optional LIMIT argument (defaults to 5) to cap output,
633 ** but no GLOB argument. All other options are supported, with -t being
634 ** an implied no-op.
635 **
636 ** > fossil branch new BRANCH-NAME BASIS ?OPTIONS?
637 **
638 ** Create a new branch BRANCH-NAME off of check-in BASIS.
639 ** Supported options for this subcommand include:
@@ -651,29 +684,41 @@
684 "SELECT datetime(mtime,toLocal()) FROM event"
685 " WHERE objid=%d", rid);
686 fossil_print("%s: open as of %s on %.16s\n", zBrName, zDate, zUuid);
687 }
688 }
689 }else if( strncmp(zCmd,"list",n)==0 ||
690 strncmp(zCmd, "ls", n)==0 ||
691 strcmp(zCmd, "lsh")==0 ){
692 Stmt q;
693 int vid;
694 char *zCurrent = 0;
695 const char *zBrNameGlob = 0;
696 int nLimit = 0;
697 int brFlags = BRL_OPEN_ONLY;
698 if( find_option("all","a",0)!=0 ) brFlags = BRL_BOTH;
699 if( find_option("closed","c",0)!=0 ) brFlags = BRL_CLOSED_ONLY;
700 if( find_option("t",0,0)!=0 ) brFlags |= BRL_ORDERBY_MTIME;
701 if( find_option("r",0,0)!=0 ) brFlags |= BRL_REVERSE;
702 if( find_option("p",0,0)!=0 ) brFlags |= BRL_PRIVATE;
703
704 if( strcmp(zCmd, "lsh")==0 ){
705 nLimit = 5;
706 if( g.argc>4 || (g.argc==4 && (nLimit = atoi(g.argv[3]))==0) ){
707 fossil_fatal("the lsh subcommand allows one optional numeric argument");
708 }
709 brFlags |= BRL_ORDERBY_MTIME;
710 }else{
711 if( g.argc >= 4 ) zBrNameGlob = g.argv[3];
712 }
713
714 if( g.localOpen ){
715 vid = db_lget_int("checkout", 0);
716 zCurrent = db_text(0, "SELECT value FROM tagxref"
717 " WHERE rid=%d AND tagid=%d", vid, TAG_BRANCH);
718 }
719 branch_prepare_list_query(&q, brFlags, zBrNameGlob, nLimit);
720 while( db_step(&q)==SQLITE_ROW ){
721 const char *zBr = db_column_text(&q, 0);
722 int isPriv = zCurrent!=0 && db_column_int(&q, 1)==1;
723 int isCur = zCurrent!=0 && fossil_strcmp(zCurrent,zBr)==0;
724 fossil_print("%s%s%s\n",
@@ -703,11 +748,11 @@
748 usage("branch unhide branch-name(s)...");
749 }
750 branch_cmd_hide(3,0);
751 }else{
752 fossil_fatal("branch subcommand should be one of: "
753 "close current hide info list ls lsh new reopen unhide");
754 }
755 }
756
757 /*
758 ** This is the new-style branch-list page that shows the branch names
@@ -862,11 +907,11 @@
907 @ reopened).</li>
908 @ </ol>
909 style_sidebox_end();
910 #endif
911
912 branch_prepare_list_query(&q, brFlags, 0, 0);
913 cnt = 0;
914 while( db_step(&q)==SQLITE_ROW ){
915 const char *zBr = db_column_text(&q, 0);
916 if( cnt==0 ){
917 if( colorTest ){
918

Keyboard Shortcuts

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