Hex Artifact Content
Not logged in

Artifact 9007c3f73bc34768455a8438bcbfd0f0c79e3285:

File src/descendants.c part of check-in [83ac468aae] - Add a "nomenclature" sidebox for the Branches page, explaining the difference between an open and a closed branch. by drh on 2009-01-24 00:11:44.

0000: 2f 2a 0a 2a 2a 20 43 6f 70 79 72 69 67 68 74 20  /*.** Copyright 
0010: 28 63 29 20 32 30 30 37 20 44 2e 20 52 69 63 68  (c) 2007 D. Rich
0020: 61 72 64 20 48 69 70 70 0a 2a 2a 0a 2a 2a 20 54  ard Hipp.**.** T
0030: 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 20 66  his program is f
0040: 72 65 65 20 73 6f 66 74 77 61 72 65 3b 20 79 6f  ree software; yo
0050: 75 20 63 61 6e 20 72 65 64 69 73 74 72 69 62 75  u can redistribu
0060: 74 65 20 69 74 20 61 6e 64 2f 6f 72 0a 2a 2a 20  te it and/or.** 
0070: 6d 6f 64 69 66 79 20 69 74 20 75 6e 64 65 72 20  modify it under 
0080: 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65  the terms of the
0090: 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62   GNU General Pub
00a0: 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 76  lic.** License v
00b0: 65 72 73 69 6f 6e 20 32 20 61 73 20 70 75 62 6c  ersion 2 as publ
00c0: 69 73 68 65 64 20 62 79 20 74 68 65 20 46 72 65  ished by the Fre
00d0: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64  e Software Found
00e0: 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ation..**.** Thi
00f0: 73 20 70 72 6f 67 72 61 6d 20 69 73 20 64 69 73  s program is dis
0100: 74 72 69 62 75 74 65 64 20 69 6e 20 74 68 65 20  tributed in the 
0110: 68 6f 70 65 20 74 68 61 74 20 69 74 20 77 69 6c  hope that it wil
0120: 6c 20 62 65 20 75 73 65 66 75 6c 2c 0a 2a 2a 20  l be useful,.** 
0130: 62 75 74 20 57 49 54 48 4f 55 54 20 41 4e 59 20  but WITHOUT ANY 
0140: 57 41 52 52 41 4e 54 59 3b 20 77 69 74 68 6f 75  WARRANTY; withou
0150: 74 20 65 76 65 6e 20 74 68 65 20 69 6d 70 6c 69  t even the impli
0160: 65 64 20 77 61 72 72 61 6e 74 79 20 6f 66 0a 2a  ed warranty of.*
0170: 2a 20 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54  * MERCHANTABILIT
0180: 59 20 6f 72 20 46 49 54 4e 45 53 53 20 46 4f 52  Y or FITNESS FOR
0190: 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 50 55   A PARTICULAR PU
01a0: 52 50 4f 53 45 2e 20 20 53 65 65 20 74 68 65 20  RPOSE.  See the 
01b0: 47 4e 55 0a 2a 2a 20 47 65 6e 65 72 61 6c 20 50  GNU.** General P
01c0: 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 66 6f  ublic License fo
01d0: 72 20 6d 6f 72 65 20 64 65 74 61 69 6c 73 2e 0a  r more details..
01e0: 2a 2a 20 0a 2a 2a 20 59 6f 75 20 73 68 6f 75 6c  ** .** You shoul
01f0: 64 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20  d have received 
0200: 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 47 4e  a copy of the GN
0210: 55 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63  U General Public
0220: 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 61 6c 6f 6e  .** License alon
0230: 67 20 77 69 74 68 20 74 68 69 73 20 6c 69 62 72  g with this libr
0240: 61 72 79 3b 20 69 66 20 6e 6f 74 2c 20 77 72 69  ary; if not, wri
0250: 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 46 72 65  te to the.** Fre
0260: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64  e Software Found
0270: 61 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 35 39 20  ation, Inc., 59 
0280: 54 65 6d 70 6c 65 20 50 6c 61 63 65 20 2d 20 53  Temple Place - S
0290: 75 69 74 65 20 33 33 30 2c 0a 2a 2a 20 42 6f 73  uite 330,.** Bos
02a0: 74 6f 6e 2c 20 4d 41 20 20 30 32 31 31 31 2d 31  ton, MA  02111-1
02b0: 33 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 2a 2a 20  307, USA..**.** 
02c0: 41 75 74 68 6f 72 20 63 6f 6e 74 61 63 74 20 69  Author contact i
02d0: 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 20 20  nformation:.**  
02e0: 20 64 72 68 40 68 77 61 63 69 2e 63 6f 6d 0a 2a   drh@hwaci.com.*
02f0: 2a 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 68  *   http://www.h
0300: 77 61 63 69 2e 63 6f 6d 2f 64 72 68 2f 0a 2a 2a  waci.com/drh/.**
0310: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
0320: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0330: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0350: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0360: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
0370: 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75   contains code u
0380: 73 65 64 20 74 6f 20 66 69 6e 64 20 64 65 63 65  sed to find dece
0390: 6e 64 61 6e 74 73 20 6f 66 20 61 20 76 65 72 73  ndants of a vers
03a0: 69 6f 6e 0a 2a 2a 20 6f 72 20 6c 65 61 76 65 73  ion.** or leaves
03b0: 20 6f 66 20 61 20 76 65 72 73 69 6f 6e 20 74 72   of a version tr
03c0: 65 65 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20  ee..*/.#include 
03d0: 22 63 6f 6e 66 69 67 2e 68 22 0a 23 69 6e 63 6c  "config.h".#incl
03e0: 75 64 65 20 22 64 65 73 63 65 6e 64 61 6e 74 73  ude "descendants
03f0: 2e 68 22 0a 23 69 6e 63 6c 75 64 65 20 3c 61 73  .h".#include <as
0400: 73 65 72 74 2e 68 3e 0a 0a 0a 2f 2a 0a 2a 2a 20  sert.h>.../*.** 
0410: 43 72 65 61 74 65 20 61 20 74 65 6d 70 6f 72 61  Create a tempora
0420: 72 79 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 22  ry table named "
0430: 6c 65 61 76 65 73 22 20 69 66 20 69 74 20 64 6f  leaves" if it do
0440: 65 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64  es not.** alread
0450: 79 20 65 78 69 73 74 2e 20 20 4c 6f 61 64 20 74  y exist.  Load t
0460: 68 69 73 20 74 61 62 6c 65 20 77 69 74 68 20 74  his table with t
0470: 68 65 20 52 49 44 20 6f 66 20 61 6c 6c 0a 2a 2a  he RID of all.**
0480: 20 63 68 65 63 6b 2d 69 6e 73 20 74 68 61 74 20   check-ins that 
0490: 61 72 65 20 6c 65 61 76 65 73 20 77 68 69 63 68  are leaves which
04a0: 20 61 72 65 20 64 65 63 65 6e 64 65 64 20 66 72   are decended fr
04b0: 6f 6d 0a 2a 2a 20 63 68 65 63 6b 2d 69 6e 20 69  om.** check-in i
04c0: 42 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 22 6c  Base..**.** A "l
04d0: 65 61 66 22 20 69 73 20 61 20 63 68 65 63 6b 2d  eaf" is a check-
04e0: 69 6e 20 74 68 61 74 20 68 61 73 20 6e 6f 20 63  in that has no c
04f0: 68 69 6c 64 72 65 6e 20 69 6e 20 74 68 65 20 73  hildren in the s
0500: 61 6d 65 20 62 72 61 6e 63 68 2e 0a 2a 2a 0a 2a  ame branch..**.*
0510: 2a 20 54 68 65 20 63 6c 6f 73 65 4d 6f 64 65 20  * The closeMode 
0520: 66 6c 61 67 20 64 65 74 65 72 6d 69 6e 65 73 20  flag determines 
0530: 62 65 68 61 76 69 6f 72 20 61 73 73 6f 63 69 61  behavior associa
0540: 74 65 64 20 77 69 74 68 20 74 68 65 20 22 63 6c  ted with the "cl
0550: 6f 73 65 64 22 0a 2a 2a 20 74 61 67 3a 0a 2a 2a  osed".** tag:.**
0560: 0a 2a 2a 20 20 20 20 63 6c 6f 73 65 4d 6f 64 65  .**    closeMode
0570: 3d 3d 30 20 20 20 20 20 20 20 53 68 6f 77 20 61  ==0       Show a
0580: 6c 6c 20 6c 65 61 76 65 73 20 72 65 67 61 72 64  ll leaves regard
0590: 6c 65 73 73 20 6f 66 20 74 68 65 20 22 63 6c 6f  less of the "clo
05a0: 73 65 64 22 20 74 61 67 2e 0a 2a 2a 0a 2a 2a 20  sed" tag..**.** 
05b0: 20 20 20 63 6c 6f 73 65 4d 6f 64 65 3d 3d 31 20     closeMode==1 
05c0: 20 20 20 20 20 20 53 68 6f 77 20 6f 6e 6c 79 20        Show only 
05d0: 6c 65 61 76 65 73 20 77 69 74 68 6f 75 74 20 74  leaves without t
05e0: 68 65 20 22 63 6c 6f 73 65 64 22 20 74 61 67 2e  he "closed" tag.
05f0: 0a 2a 2a 0a 2a 2a 20 20 20 20 63 6c 6f 73 65 4d  .**.**    closeM
0600: 6f 64 65 3d 3d 32 20 20 20 20 20 20 20 53 68 6f  ode==2       Sho
0610: 77 20 6f 6e 6c 79 20 6c 65 61 76 65 73 20 77 69  w only leaves wi
0620: 74 68 20 74 68 65 20 22 63 6c 6f 73 65 64 22 20  th the "closed" 
0630: 74 61 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64  tag..**.** The d
0640: 65 66 61 75 6c 74 20 62 65 68 61 76 69 6f 72 20  efault behavior 
0650: 69 73 20 74 6f 20 69 67 6e 6f 72 65 20 63 6c 6f  is to ignore clo
0660: 73 65 64 20 6c 65 61 76 65 73 20 28 63 6c 6f 73  sed leaves (clos
0670: 65 4d 6f 64 65 3d 3d 30 29 2e 20 20 54 6f 0a 2a  eMode==0).  To.*
0680: 2a 20 53 68 6f 77 20 61 6c 6c 20 6c 65 61 76 65  * Show all leave
0690: 73 2c 20 75 73 65 20 63 6c 6f 73 65 4d 6f 64 65  s, use closeMode
06a0: 3d 3d 31 2e 20 20 54 6f 20 73 68 6f 77 20 6f 6e  ==1.  To show on
06b0: 6c 79 20 63 6c 6f 73 65 64 20 6c 65 61 76 65 73  ly closed leaves
06c0: 2c 20 75 73 65 0a 2a 2a 20 63 6c 6f 73 65 4d 6f  , use.** closeMo
06d0: 64 65 3d 3d 32 2e 0a 2a 2f 0a 76 6f 69 64 20 63  de==2..*/.void c
06e0: 6f 6d 70 75 74 65 5f 6c 65 61 76 65 73 28 69 6e  ompute_leaves(in
06f0: 74 20 69 42 61 73 65 2c 20 69 6e 74 20 63 6c 6f  t iBase, int clo
0700: 73 65 4d 6f 64 65 29 7b 0a 20 20 42 61 67 20 73  seMode){.  Bag s
0710: 65 65 6e 3b 20 20 20 20 20 20 20 2f 2a 20 44 65  een;       /* De
0720: 73 63 65 6e 64 61 6e 74 73 20 73 65 65 6e 20 2a  scendants seen *
0730: 2f 0a 20 20 42 61 67 20 70 65 6e 64 69 6e 67 3b  /.  Bag pending;
0740: 20 20 20 20 2f 2a 20 55 6e 70 72 6f 70 61 67 61      /* Unpropaga
0750: 74 65 64 20 64 65 73 63 65 6e 64 61 6e 74 73 20  ted descendants 
0760: 2a 2f 0a 20 20 53 74 6d 74 20 71 31 3b 20 20 20  */.  Stmt q1;   
0770: 20 20 20 20 20 2f 2a 20 51 75 65 72 79 20 74 6f       /* Query to
0780: 20 66 69 6e 64 20 63 68 69 6c 64 72 65 6e 20 6f   find children o
0790: 66 20 61 20 63 68 65 63 6b 2d 69 6e 20 2a 2f 0a  f a check-in */.
07a0: 20 20 53 74 6d 74 20 69 73 42 72 3b 20 20 20 20    Stmt isBr;    
07b0: 20 20 2f 2a 20 51 75 65 72 79 20 74 6f 20 63 68    /* Query to ch
07c0: 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 61 20  eck to see if a 
07d0: 63 68 65 63 6b 2d 69 6e 20 73 74 61 72 74 73 20  check-in starts 
07e0: 61 20 6e 65 77 20 62 72 61 6e 63 68 20 2a 2f 0a  a new branch */.
07f0: 20 20 53 74 6d 74 20 69 6e 73 3b 20 20 20 20 20    Stmt ins;     
0800: 20 20 2f 2a 20 49 4e 53 45 52 54 20 73 74 61 74    /* INSERT stat
0810: 65 6d 65 6e 74 20 66 6f 72 20 61 20 6e 65 77 20  ement for a new 
0820: 72 65 63 6f 72 64 20 2a 2f 0a 0a 20 20 2f 2a 20  record */..  /* 
0830: 43 72 65 61 74 65 20 74 68 65 20 4c 45 41 56 45  Create the LEAVE
0840: 53 20 74 61 62 6c 65 20 69 66 20 69 74 20 64 6f  S table if it do
0850: 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65  es not already e
0860: 78 69 73 74 2e 20 20 4d 61 6b 65 20 73 75 72 65  xist.  Make sure
0870: 0a 20 20 2a 2a 20 69 74 20 69 73 20 65 6d 70 74  .  ** it is empt
0880: 79 2e 0a 20 20 2a 2f 0a 20 20 64 62 5f 6d 75 6c  y..  */.  db_mul
0890: 74 69 5f 65 78 65 63 28 0a 20 20 20 20 22 43 52  ti_exec(.    "CR
08a0: 45 41 54 45 20 54 45 4d 50 20 54 41 42 4c 45 20  EATE TEMP TABLE 
08b0: 49 46 20 4e 4f 54 20 45 58 49 53 54 53 20 6c 65  IF NOT EXISTS le
08c0: 61 76 65 73 28 22 0a 20 20 20 20 22 20 20 72 69  aves(".    "  ri
08d0: 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  d INTEGER PRIMAR
08e0: 59 20 4b 45 59 22 0a 20 20 20 20 22 29 3b 22 0a  Y KEY".    ");".
08f0: 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d      "DELETE FROM
0900: 20 6c 65 61 76 65 73 3b 22 0a 20 20 29 3b 0a 0a   leaves;".  );..
0910: 20 20 2f 2a 20 57 65 20 61 72 65 20 63 68 65 63    /* We are chec
0920: 6b 69 6e 67 20 61 6c 6c 20 64 65 73 63 65 6e 64  king all descend
0930: 61 6e 74 73 20 6f 66 20 69 42 61 73 65 2e 20 20  ants of iBase.  
0940: 49 66 20 69 42 61 73 65 3d 3d 30 2c 20 74 68 65  If iBase==0, the
0950: 6e 0a 20 20 2a 2a 20 63 68 61 6e 67 65 20 69 42  n.  ** change iB
0960: 61 73 65 20 74 6f 20 62 65 20 74 68 65 20 72 6f  ase to be the ro
0970: 6f 74 20 6f 66 20 74 68 65 20 65 6e 74 69 72 65  ot of the entire
0980: 20 63 68 65 63 6b 2d 69 6e 20 68 69 65 72 61 72   check-in hierar
0990: 63 68 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  chy..  */.  if( 
09a0: 69 42 61 73 65 3c 3d 30 20 29 7b 0a 20 20 20 20  iBase<=0 ){.    
09b0: 69 42 61 73 65 20 3d 20 64 62 5f 69 6e 74 28 30  iBase = db_int(0
09c0: 2c 20 22 53 45 4c 45 43 54 20 6f 62 6a 69 64 20  , "SELECT objid 
09d0: 46 52 4f 4d 20 65 76 65 6e 74 20 57 48 45 52 45  FROM event WHERE
09e0: 20 74 79 70 65 3d 27 63 69 27 22 0a 20 20 20 20   type='ci'".    
09f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
0a00: 20 20 22 20 4f 52 44 45 52 20 42 59 20 6d 74 69    " ORDER BY mti
0a10: 6d 65 20 4c 49 4d 49 54 20 31 22 29 3b 0a 20 20  me LIMIT 1");.  
0a20: 20 20 69 66 28 20 69 42 61 73 65 3d 3d 30 20 29    if( iBase==0 )
0a30: 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
0a40: 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  /* Initialize th
0a50: 65 20 62 61 67 73 2e 20 2a 2f 0a 20 20 62 61 67  e bags. */.  bag
0a60: 5f 69 6e 69 74 28 26 73 65 65 6e 29 3b 0a 20 20  _init(&seen);.  
0a70: 62 61 67 5f 69 6e 69 74 28 26 70 65 6e 64 69 6e  bag_init(&pendin
0a80: 67 29 3b 0a 20 20 62 61 67 5f 69 6e 73 65 72 74  g);.  bag_insert
0a90: 28 26 70 65 6e 64 69 6e 67 2c 20 69 42 61 73 65  (&pending, iBase
0aa0: 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 71 75  );..  /* This qu
0ab0: 65 72 79 20 72 65 74 75 72 6e 73 20 61 6c 6c 20  ery returns all 
0ac0: 6e 6f 6e 2d 6d 65 72 67 65 20 63 68 69 6c 64 72  non-merge childr
0ad0: 65 6e 20 6f 66 20 63 68 65 63 6b 2d 69 6e 20 3a  en of check-in :
0ae0: 72 69 64 20 2a 2f 0a 20 20 64 62 5f 70 72 65 70  rid */.  db_prep
0af0: 61 72 65 28 26 71 31 2c 20 22 53 45 4c 45 43 54  are(&q1, "SELECT
0b00: 20 63 69 64 20 46 52 4f 4d 20 70 6c 69 6e 6b 20   cid FROM plink 
0b10: 57 48 45 52 45 20 70 69 64 3d 3a 72 69 64 20 41  WHERE pid=:rid A
0b20: 4e 44 20 69 73 70 72 69 6d 22 29 3b 0a 0a 20 20  ND isprim");..  
0b30: 2f 2a 20 54 68 69 73 20 71 75 65 72 79 20 72 65  /* This query re
0b40: 74 75 72 6e 73 20 61 20 73 69 6e 67 6c 65 20 72  turns a single r
0b50: 6f 77 20 69 66 20 63 68 65 63 6b 2d 69 6e 20 3a  ow if check-in :
0b60: 72 69 64 20 69 73 20 74 68 65 20 66 69 72 73 74  rid is the first
0b70: 0a 20 20 2a 2a 20 63 68 65 63 6b 2d 69 6e 20 6f  .  ** check-in o
0b80: 66 20 61 20 6e 65 77 20 62 72 61 6e 63 68 2e 0a  f a new branch..
0b90: 20 20 2a 2f 0a 20 20 64 62 5f 70 72 65 70 61 72    */.  db_prepar
0ba0: 65 28 26 69 73 42 72 2c 20 0a 20 20 20 20 20 22  e(&isBr, .     "
0bb0: 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 74 61  SELECT 1 FROM ta
0bc0: 67 78 72 65 66 22 0a 20 20 20 20 20 22 20 57 48  gxref".     " WH
0bd0: 45 52 45 20 72 69 64 3d 3a 72 69 64 20 41 4e 44  ERE rid=:rid AND
0be0: 20 74 61 67 69 64 3d 25 64 20 41 4e 44 20 74 61   tagid=%d AND ta
0bf0: 67 74 79 70 65 3d 32 22 0a 20 20 20 20 20 22 20  gtype=2".     " 
0c00: 20 20 41 4e 44 20 73 72 63 69 64 3e 30 22 2c 0a    AND srcid>0",.
0c10: 20 20 20 20 20 54 41 47 5f 42 52 41 4e 43 48 0a       TAG_BRANCH.
0c20: 20 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20    );..  /* This 
0c30: 73 74 61 74 65 6d 65 6e 74 20 69 6e 73 65 72 74  statement insert
0c40: 73 20 63 68 65 63 6b 2d 69 6e 20 3a 72 69 64 20  s check-in :rid 
0c50: 69 6e 74 6f 20 74 68 65 20 4c 45 41 56 45 53 20  into the LEAVES 
0c60: 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 64 62  table..  */.  db
0c70: 5f 70 72 65 70 61 72 65 28 26 69 6e 73 2c 20 22  _prepare(&ins, "
0c80: 49 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45  INSERT OR IGNORE
0c90: 20 49 4e 54 4f 20 6c 65 61 76 65 73 20 56 41 4c   INTO leaves VAL
0ca0: 55 45 53 28 3a 72 69 64 29 22 29 3b 0a 0a 20 20  UES(:rid)");..  
0cb0: 77 68 69 6c 65 28 20 62 61 67 5f 63 6f 75 6e 74  while( bag_count
0cc0: 28 26 70 65 6e 64 69 6e 67 29 20 29 7b 0a 20 20  (&pending) ){.  
0cd0: 20 20 69 6e 74 20 72 69 64 20 3d 20 62 61 67 5f    int rid = bag_
0ce0: 66 69 72 73 74 28 26 70 65 6e 64 69 6e 67 29 3b  first(&pending);
0cf0: 0a 20 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 30  .    int cnt = 0
0d00: 3b 0a 20 20 20 20 62 61 67 5f 72 65 6d 6f 76 65  ;.    bag_remove
0d10: 28 26 70 65 6e 64 69 6e 67 2c 20 72 69 64 29 3b  (&pending, rid);
0d20: 0a 20 20 20 20 64 62 5f 62 69 6e 64 5f 69 6e 74  .    db_bind_int
0d30: 28 26 71 31 2c 20 22 3a 72 69 64 22 2c 20 72 69  (&q1, ":rid", ri
0d40: 64 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 64  d);.    while( d
0d50: 62 5f 73 74 65 70 28 26 71 31 29 3d 3d 53 51 4c  b_step(&q1)==SQL
0d60: 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20  ITE_ROW ){.     
0d70: 20 69 6e 74 20 63 69 64 20 3d 20 64 62 5f 63 6f   int cid = db_co
0d80: 6c 75 6d 6e 5f 69 6e 74 28 26 71 31 2c 20 30 29  lumn_int(&q1, 0)
0d90: 3b 0a 20 20 20 20 20 20 69 66 28 20 62 61 67 5f  ;.      if( bag_
0da0: 69 6e 73 65 72 74 28 26 73 65 65 6e 2c 20 63 69  insert(&seen, ci
0db0: 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 62 61  d) ){.        ba
0dc0: 67 5f 69 6e 73 65 72 74 28 26 70 65 6e 64 69 6e  g_insert(&pendin
0dd0: 67 2c 20 63 69 64 29 3b 0a 20 20 20 20 20 20 7d  g, cid);.      }
0de0: 0a 20 20 20 20 20 20 64 62 5f 62 69 6e 64 5f 69  .      db_bind_i
0df0: 6e 74 28 26 69 73 42 72 2c 20 22 3a 72 69 64 22  nt(&isBr, ":rid"
0e00: 2c 20 63 69 64 29 3b 0a 20 20 20 20 20 20 69 66  , cid);.      if
0e10: 28 20 64 62 5f 73 74 65 70 28 26 69 73 42 72 29  ( db_step(&isBr)
0e20: 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  ==SQLITE_DONE ){
0e30: 0a 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a  .        cnt++;.
0e40: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 62        }.      db
0e50: 5f 72 65 73 65 74 28 26 69 73 42 72 29 3b 0a 20  _reset(&isBr);. 
0e60: 20 20 20 7d 0a 20 20 20 20 64 62 5f 72 65 73 65     }.    db_rese
0e70: 74 28 26 71 31 29 3b 0a 20 20 20 20 69 66 28 20  t(&q1);.    if( 
0e80: 63 6e 74 3d 3d 30 20 26 26 20 21 69 73 5f 61 5f  cnt==0 && !is_a_
0e90: 6c 65 61 66 28 72 69 64 29 20 29 7b 0a 20 20 20  leaf(rid) ){.   
0ea0: 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 7d 0a     cnt++;.    }.
0eb0: 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 29      if( cnt==0 )
0ec0: 7b 0a 20 20 20 20 20 20 64 62 5f 62 69 6e 64 5f  {.      db_bind_
0ed0: 69 6e 74 28 26 69 6e 73 2c 20 22 3a 72 69 64 22  int(&ins, ":rid"
0ee0: 2c 20 72 69 64 29 3b 0a 20 20 20 20 20 20 64 62  , rid);.      db
0ef0: 5f 73 74 65 70 28 26 69 6e 73 29 3b 0a 20 20 20  _step(&ins);.   
0f00: 20 20 20 64 62 5f 72 65 73 65 74 28 26 69 6e 73     db_reset(&ins
0f10: 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 64  );.    }.  }.  d
0f20: 62 5f 66 69 6e 61 6c 69 7a 65 28 26 69 6e 73 29  b_finalize(&ins)
0f30: 3b 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28  ;.  db_finalize(
0f40: 26 69 73 42 72 29 3b 0a 20 20 64 62 5f 66 69 6e  &isBr);.  db_fin
0f50: 61 6c 69 7a 65 28 26 71 31 29 3b 0a 20 20 62 61  alize(&q1);.  ba
0f60: 67 5f 63 6c 65 61 72 28 26 70 65 6e 64 69 6e 67  g_clear(&pending
0f70: 29 3b 0a 20 20 62 61 67 5f 63 6c 65 61 72 28 26  );.  bag_clear(&
0f80: 73 65 65 6e 29 3b 0a 20 20 69 66 28 20 63 6c 6f  seen);.  if( clo
0f90: 73 65 4d 6f 64 65 3d 3d 31 20 29 7b 0a 20 20 20  seMode==1 ){.   
0fa0: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a   db_multi_exec(.
0fb0: 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 46 52        "DELETE FR
0fc0: 4f 4d 20 6c 65 61 76 65 73 20 57 48 45 52 45 20  OM leaves WHERE 
0fd0: 72 69 64 20 49 4e 22 0a 20 20 20 20 20 20 22 20  rid IN".      " 
0fe0: 20 28 53 45 4c 45 43 54 20 6c 65 61 76 65 73 2e   (SELECT leaves.
0ff0: 72 69 64 20 46 52 4f 4d 20 6c 65 61 76 65 73 2c  rid FROM leaves,
1000: 20 74 61 67 78 72 65 66 22 0a 20 20 20 20 20 20   tagxref".      
1010: 22 20 20 20 20 57 48 45 52 45 20 74 61 67 78 72  "    WHERE tagxr
1020: 65 66 2e 72 69 64 3d 6c 65 61 76 65 73 2e 72 69  ef.rid=leaves.ri
1030: 64 20 22 0a 20 20 20 20 20 20 22 20 20 20 20 20  d ".      "     
1040: 20 41 4e 44 20 74 61 67 78 72 65 66 2e 74 61 67   AND tagxref.tag
1050: 69 64 3d 25 64 22 0a 20 20 20 20 20 20 22 20 20  id=%d".      "  
1060: 20 20 20 20 41 4e 44 20 74 61 67 78 72 65 66 2e      AND tagxref.
1070: 74 61 67 74 79 70 65 3e 30 29 22 2c 0a 20 20 20  tagtype>0)",.   
1080: 20 20 20 54 41 47 5f 43 4c 4f 53 45 44 0a 20 20     TAG_CLOSED.  
1090: 20 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28    );.  }else if(
10a0: 20 63 6c 6f 73 65 4d 6f 64 65 3d 3d 32 20 29 7b   closeMode==2 ){
10b0: 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78  .    db_multi_ex
10c0: 65 63 28 0a 20 20 20 20 20 20 22 44 45 4c 45 54  ec(.      "DELET
10d0: 45 20 46 52 4f 4d 20 6c 65 61 76 65 73 20 57 48  E FROM leaves WH
10e0: 45 52 45 20 72 69 64 20 4e 4f 54 20 49 4e 22 0a  ERE rid NOT IN".
10f0: 20 20 20 20 20 20 22 20 20 28 53 45 4c 45 43 54        "  (SELECT
1100: 20 6c 65 61 76 65 73 2e 72 69 64 20 46 52 4f 4d   leaves.rid FROM
1110: 20 6c 65 61 76 65 73 2c 20 74 61 67 78 72 65 66   leaves, tagxref
1120: 22 0a 20 20 20 20 20 20 22 20 20 20 20 57 48 45  ".      "    WHE
1130: 52 45 20 74 61 67 78 72 65 66 2e 72 69 64 3d 6c  RE tagxref.rid=l
1140: 65 61 76 65 73 2e 72 69 64 20 22 0a 20 20 20 20  eaves.rid ".    
1150: 20 20 22 20 20 20 20 20 20 41 4e 44 20 74 61 67    "      AND tag
1160: 78 72 65 66 2e 74 61 67 69 64 3d 25 64 22 0a 20  xref.tagid=%d". 
1170: 20 20 20 20 20 22 20 20 20 20 20 20 41 4e 44 20       "      AND 
1180: 74 61 67 78 72 65 66 2e 74 61 67 74 79 70 65 3e  tagxref.tagtype>
1190: 30 29 22 2c 0a 20 20 20 20 20 20 54 41 47 5f 43  0)",.      TAG_C
11a0: 4c 4f 53 45 44 0a 20 20 20 20 29 3b 0a 20 20 7d  LOSED.    );.  }
11b0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 61 64 20 74  .}../*.** Load t
11c0: 68 65 20 72 65 63 6f 72 64 20 49 44 20 72 69 64  he record ID rid
11d0: 20 61 6e 64 20 75 70 20 74 6f 20 4e 2d 31 20 63   and up to N-1 c
11e0: 6c 6f 73 65 73 74 20 61 6e 63 65 73 74 6f 72 73  losest ancestors
11f0: 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 22 6f 6b   into.** the "ok
1200: 22 20 74 61 62 6c 65 2e 0a 2a 2f 0a 76 6f 69 64  " table..*/.void
1210: 20 63 6f 6d 70 75 74 65 5f 61 6e 63 65 73 74 6f   compute_ancesto
1220: 72 73 28 69 6e 74 20 72 69 64 2c 20 69 6e 74 20  rs(int rid, int 
1230: 4e 29 7b 0a 20 20 42 61 67 20 73 65 65 6e 3b 0a  N){.  Bag seen;.
1240: 20 20 50 51 75 65 75 65 20 71 75 65 75 65 3b 0a    PQueue queue;.
1250: 20 20 62 61 67 5f 69 6e 69 74 28 26 73 65 65 6e    bag_init(&seen
1260: 29 3b 0a 20 20 70 71 75 65 75 65 5f 69 6e 69 74  );.  pqueue_init
1270: 28 26 71 75 65 75 65 29 3b 0a 20 20 62 61 67 5f  (&queue);.  bag_
1280: 69 6e 73 65 72 74 28 26 73 65 65 6e 2c 20 72 69  insert(&seen, ri
1290: 64 29 3b 0a 20 20 70 71 75 65 75 65 5f 69 6e 73  d);.  pqueue_ins
12a0: 65 72 74 28 26 71 75 65 75 65 2c 20 72 69 64 2c  ert(&queue, rid,
12b0: 20 30 2e 30 29 3b 0a 20 20 77 68 69 6c 65 28 20   0.0);.  while( 
12c0: 28 4e 2d 2d 29 3e 30 20 26 26 20 28 72 69 64 20  (N--)>0 && (rid 
12d0: 3d 20 70 71 75 65 75 65 5f 65 78 74 72 61 63 74  = pqueue_extract
12e0: 28 26 71 75 65 75 65 29 29 21 3d 30 20 29 7b 0a  (&queue))!=0 ){.
12f0: 20 20 20 20 53 74 6d 74 20 71 3b 0a 20 20 20 20      Stmt q;.    
1300: 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22 49  db_multi_exec("I
1310: 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20  NSERT OR IGNORE 
1320: 49 4e 54 4f 20 6f 6b 20 56 41 4c 55 45 53 28 25  INTO ok VALUES(%
1330: 64 29 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 64  d)", rid);.    d
1340: 62 5f 70 72 65 70 61 72 65 28 26 71 2c 0a 20 20  b_prepare(&q,.  
1350: 20 20 20 20 20 22 53 45 4c 45 43 54 20 61 2e 70       "SELECT a.p
1360: 69 64 2c 20 62 2e 6d 74 69 6d 65 20 46 52 4f 4d  id, b.mtime FROM
1370: 20 70 6c 69 6e 6b 20 61 20 4c 45 46 54 20 4a 4f   plink a LEFT JO
1380: 49 4e 20 70 6c 69 6e 6b 20 62 20 4f 4e 20 62 2e  IN plink b ON b.
1390: 63 69 64 3d 61 2e 70 69 64 22 0a 20 20 20 20 20  cid=a.pid".     
13a0: 20 20 22 20 57 48 45 52 45 20 61 2e 63 69 64 3d    " WHERE a.cid=
13b0: 25 64 22 2c 20 72 69 64 0a 20 20 20 20 29 3b 0a  %d", rid.    );.
13c0: 20 20 20 20 77 68 69 6c 65 28 20 64 62 5f 73 74      while( db_st
13d0: 65 70 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52  ep(&q)==SQLITE_R
13e0: 4f 57 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  OW ){.      int 
13f0: 70 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f  pid = db_column_
1400: 69 6e 74 28 26 71 2c 20 30 29 3b 0a 20 20 20 20  int(&q, 0);.    
1410: 20 20 64 6f 75 62 6c 65 20 6d 74 69 6d 65 20 3d    double mtime =
1420: 20 64 62 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c   db_column_doubl
1430: 65 28 26 71 2c 20 31 29 3b 0a 20 20 20 20 20 20  e(&q, 1);.      
1440: 69 66 28 20 62 61 67 5f 69 6e 73 65 72 74 28 26  if( bag_insert(&
1450: 73 65 65 6e 2c 20 70 69 64 29 20 29 7b 0a 20 20  seen, pid) ){.  
1460: 20 20 20 20 20 20 70 71 75 65 75 65 5f 69 6e 73        pqueue_ins
1470: 65 72 74 28 26 71 75 65 75 65 2c 20 70 69 64 2c  ert(&queue, pid,
1480: 20 2d 6d 74 69 6d 65 29 3b 0a 20 20 20 20 20 20   -mtime);.      
1490: 7d 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 5f 66  }.    }.    db_f
14a0: 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20 7d  inalize(&q);.  }
14b0: 0a 20 20 62 61 67 5f 63 6c 65 61 72 28 26 73 65  .  bag_clear(&se
14c0: 65 6e 29 3b 0a 20 20 70 71 75 65 75 65 5f 63 6c  en);.  pqueue_cl
14d0: 65 61 72 28 26 71 75 65 75 65 29 3b 0a 7d 0a 0a  ear(&queue);.}..
14e0: 2f 2a 0a 2a 2a 20 4c 6f 61 64 20 74 68 65 20 72  /*.** Load the r
14f0: 65 63 6f 72 64 20 49 44 20 72 69 64 20 61 6e 64  ecord ID rid and
1500: 20 75 70 20 74 6f 20 4e 2d 31 20 63 6c 6f 73 65   up to N-1 close
1510: 73 74 20 64 65 73 63 65 6e 64 61 6e 74 73 20 69  st descendants i
1520: 6e 74 6f 0a 2a 2a 20 74 68 65 20 22 6f 6b 22 20  nto.** the "ok" 
1530: 74 61 62 6c 65 2e 0a 2a 2f 0a 76 6f 69 64 20 63  table..*/.void c
1540: 6f 6d 70 75 74 65 5f 64 65 73 63 65 6e 64 61 6e  ompute_descendan
1550: 74 73 28 69 6e 74 20 72 69 64 2c 20 69 6e 74 20  ts(int rid, int 
1560: 4e 29 7b 0a 20 20 42 61 67 20 73 65 65 6e 3b 0a  N){.  Bag seen;.
1570: 20 20 50 51 75 65 75 65 20 71 75 65 75 65 3b 0a    PQueue queue;.
1580: 20 20 62 61 67 5f 69 6e 69 74 28 26 73 65 65 6e    bag_init(&seen
1590: 29 3b 0a 20 20 70 71 75 65 75 65 5f 69 6e 69 74  );.  pqueue_init
15a0: 28 26 71 75 65 75 65 29 3b 0a 20 20 62 61 67 5f  (&queue);.  bag_
15b0: 69 6e 73 65 72 74 28 26 73 65 65 6e 2c 20 72 69  insert(&seen, ri
15c0: 64 29 3b 0a 20 20 70 71 75 65 75 65 5f 69 6e 73  d);.  pqueue_ins
15d0: 65 72 74 28 26 71 75 65 75 65 2c 20 72 69 64 2c  ert(&queue, rid,
15e0: 20 30 2e 30 29 3b 0a 20 20 77 68 69 6c 65 28 20   0.0);.  while( 
15f0: 28 4e 2d 2d 29 3e 30 20 26 26 20 28 72 69 64 20  (N--)>0 && (rid 
1600: 3d 20 70 71 75 65 75 65 5f 65 78 74 72 61 63 74  = pqueue_extract
1610: 28 26 71 75 65 75 65 29 29 21 3d 30 20 29 7b 0a  (&queue))!=0 ){.
1620: 20 20 20 20 53 74 6d 74 20 71 3b 0a 20 20 20 20      Stmt q;.    
1630: 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22 49  db_multi_exec("I
1640: 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20  NSERT OR IGNORE 
1650: 49 4e 54 4f 20 6f 6b 20 56 41 4c 55 45 53 28 25  INTO ok VALUES(%
1660: 64 29 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 64  d)", rid);.    d
1670: 62 5f 70 72 65 70 61 72 65 28 26 71 2c 22 53 45  b_prepare(&q,"SE
1680: 4c 45 43 54 20 63 69 64 2c 20 6d 74 69 6d 65 20  LECT cid, mtime 
1690: 46 52 4f 4d 20 70 6c 69 6e 6b 20 57 48 45 52 45  FROM plink WHERE
16a0: 20 70 69 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a   pid=%d", rid);.
16b0: 20 20 20 20 77 68 69 6c 65 28 20 64 62 5f 73 74      while( db_st
16c0: 65 70 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52  ep(&q)==SQLITE_R
16d0: 4f 57 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  OW ){.      int 
16e0: 70 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f  pid = db_column_
16f0: 69 6e 74 28 26 71 2c 20 30 29 3b 0a 20 20 20 20  int(&q, 0);.    
1700: 20 20 64 6f 75 62 6c 65 20 6d 74 69 6d 65 20 3d    double mtime =
1710: 20 64 62 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c   db_column_doubl
1720: 65 28 26 71 2c 20 31 29 3b 0a 20 20 20 20 20 20  e(&q, 1);.      
1730: 69 66 28 20 62 61 67 5f 69 6e 73 65 72 74 28 26  if( bag_insert(&
1740: 73 65 65 6e 2c 20 70 69 64 29 20 29 7b 0a 20 20  seen, pid) ){.  
1750: 20 20 20 20 20 20 70 71 75 65 75 65 5f 69 6e 73        pqueue_ins
1760: 65 72 74 28 26 71 75 65 75 65 2c 20 70 69 64 2c  ert(&queue, pid,
1770: 20 6d 74 69 6d 65 29 3b 0a 20 20 20 20 20 20 7d   mtime);.      }
1780: 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 5f 66 69  .    }.    db_fi
1790: 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20 7d 0a  nalize(&q);.  }.
17a0: 20 20 62 61 67 5f 63 6c 65 61 72 28 26 73 65 65    bag_clear(&see
17b0: 6e 29 3b 0a 20 20 70 71 75 65 75 65 5f 63 6c 65  n);.  pqueue_cle
17c0: 61 72 28 26 71 75 65 75 65 29 3b 0a 7d 0a 0a 2f  ar(&queue);.}../
17d0: 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 20 64  *.** COMMAND:  d
17e0: 65 73 63 65 6e 64 61 6e 74 73 0a 2a 2a 0a 2a 2a  escendants.**.**
17f0: 20 55 73 61 67 65 3a 20 25 66 6f 73 73 69 6c 20   Usage: %fossil 
1800: 64 65 73 63 65 6e 64 61 6e 74 73 20 3f 42 41 53  descendants ?BAS
1810: 45 4c 49 4e 45 2d 49 44 3f 0a 2a 2a 0a 2a 2a 20  ELINE-ID?.**.** 
1820: 46 69 6e 64 20 61 6c 6c 20 6c 65 61 66 20 64 65  Find all leaf de
1830: 73 63 65 6e 64 61 6e 74 73 20 6f 66 20 74 68 65  scendants of the
1840: 20 62 61 73 65 6c 69 6e 65 20 73 70 65 63 69 66   baseline specif
1850: 69 65 64 20 6f 72 20 69 66 20 74 68 65 20 61 72  ied or if the ar
1860: 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 6f 6d 69  gument.** is omi
1870: 74 74 65 64 2c 20 6f 66 20 74 68 65 20 62 61 73  tted, of the bas
1880: 65 6c 69 6e 65 20 63 75 72 72 65 6e 74 6c 79 20  eline currently 
1890: 63 68 65 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a  checked out..*/.
18a0: 76 6f 69 64 20 64 65 73 63 65 6e 64 61 6e 74 73  void descendants
18b0: 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20 53 74  _cmd(void){.  St
18c0: 6d 74 20 71 3b 0a 20 20 69 6e 74 20 62 61 73 65  mt q;.  int base
18d0: 3b 0a 0a 20 20 64 62 5f 6d 75 73 74 5f 62 65 5f  ;..  db_must_be_
18e0: 77 69 74 68 69 6e 5f 74 72 65 65 28 29 3b 0a 20  within_tree();. 
18f0: 20 69 66 28 20 67 2e 61 72 67 63 3d 3d 32 20 29   if( g.argc==2 )
1900: 7b 0a 20 20 20 20 62 61 73 65 20 3d 20 64 62 5f  {.    base = db_
1910: 6c 67 65 74 5f 69 6e 74 28 22 63 68 65 63 6b 6f  lget_int("checko
1920: 75 74 22 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65  ut", 0);.  }else
1930: 7b 0a 20 20 20 20 62 61 73 65 20 3d 20 6e 61 6d  {.    base = nam
1940: 65 5f 74 6f 5f 72 69 64 28 67 2e 61 72 67 76 5b  e_to_rid(g.argv[
1950: 32 5d 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 62  2]);.  }.  if( b
1960: 61 73 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ase==0 ) return;
1970: 0a 20 20 63 6f 6d 70 75 74 65 5f 6c 65 61 76 65  .  compute_leave
1980: 73 28 62 61 73 65 2c 20 30 29 3b 0a 20 20 64 62  s(base, 0);.  db
1990: 5f 70 72 65 70 61 72 65 28 26 71 2c 0a 20 20 20  _prepare(&q,.   
19a0: 20 22 25 73 22 0a 20 20 20 20 22 20 20 20 41 4e   "%s".    "   AN
19b0: 44 20 65 76 65 6e 74 2e 6f 62 6a 69 64 20 49 4e  D event.objid IN
19c0: 20 28 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f   (SELECT rid FRO
19d0: 4d 20 6c 65 61 76 65 73 29 22 0a 20 20 20 20 22  M leaves)".    "
19e0: 20 4f 52 44 45 52 20 42 59 20 65 76 65 6e 74 2e   ORDER BY event.
19f0: 6d 74 69 6d 65 20 44 45 53 43 22 2c 0a 20 20 20  mtime DESC",.   
1a00: 20 74 69 6d 65 6c 69 6e 65 5f 71 75 65 72 79 5f   timeline_query_
1a10: 66 6f 72 5f 74 74 79 28 29 0a 20 20 29 3b 0a 20  for_tty().  );. 
1a20: 20 70 72 69 6e 74 5f 74 69 6d 65 6c 69 6e 65 28   print_timeline(
1a30: 26 71 2c 20 32 30 29 3b 0a 20 20 64 62 5f 66 69  &q, 20);.  db_fi
1a40: 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 7d 0a 0a 2f  nalize(&q);.}../
1a50: 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 20 6c  *.** COMMAND:  l
1a60: 65 61 76 65 73 0a 2a 2a 0a 2a 2a 20 55 73 61 67  eaves.**.** Usag
1a70: 65 3a 20 25 66 6f 73 73 69 6c 20 6c 65 61 76 65  e: %fossil leave
1a80: 73 20 3f 2d 2d 61 6c 6c 3f 20 3f 2d 2d 63 6c 6f  s ?--all? ?--clo
1a90: 73 65 64 3f 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 20  sed?.**.** Find 
1aa0: 6c 65 61 76 65 73 20 6f 66 20 61 6c 6c 20 62 72  leaves of all br
1ab0: 61 6e 63 68 65 73 2e 20 20 42 79 20 64 65 66 61  anches.  By defa
1ac0: 75 6c 74 20 73 68 6f 77 20 6f 6e 6c 79 20 6f 70  ult show only op
1ad0: 65 6e 20 6c 65 61 76 65 73 2e 0a 2a 2a 20 54 68  en leaves..** Th
1ae0: 65 20 2d 2d 61 6c 6c 20 66 6c 61 67 20 63 61 75  e --all flag cau
1af0: 73 65 73 20 61 6c 6c 20 6c 65 61 76 65 73 20 28  ses all leaves (
1b00: 63 6c 6f 73 65 64 20 61 6e 64 20 6f 70 65 6e 29  closed and open)
1b10: 20 74 6f 20 62 65 20 73 68 6f 77 6e 2e 0a 2a 2a   to be shown..**
1b20: 20 54 68 65 20 2d 2d 63 6c 6f 73 65 64 20 66 6c   The --closed fl
1b30: 61 67 20 73 68 6f 77 73 20 6f 6e 6c 79 20 63 6c  ag shows only cl
1b40: 6f 73 65 64 20 6c 65 61 76 65 73 2e 0a 2a 2f 0a  osed leaves..*/.
1b50: 76 6f 69 64 20 6c 65 61 76 65 73 5f 63 6d 64 28  void leaves_cmd(
1b60: 76 6f 69 64 29 7b 0a 20 20 53 74 6d 74 20 71 3b  void){.  Stmt q;
1b70: 0a 20 20 69 6e 74 20 73 68 6f 77 41 6c 6c 20 3d  .  int showAll =
1b80: 20 66 69 6e 64 5f 6f 70 74 69 6f 6e 28 22 61 6c   find_option("al
1b90: 6c 22 2c 20 30 2c 20 30 29 21 3d 30 3b 0a 20 20  l", 0, 0)!=0;.  
1ba0: 69 6e 74 20 73 68 6f 77 43 6c 6f 73 65 64 20 3d  int showClosed =
1bb0: 20 66 69 6e 64 5f 6f 70 74 69 6f 6e 28 22 63 6c   find_option("cl
1bc0: 6f 73 65 64 22 2c 20 30 2c 20 30 29 21 3d 30 3b  osed", 0, 0)!=0;
1bd0: 0a 0a 20 20 64 62 5f 6d 75 73 74 5f 62 65 5f 77  ..  db_must_be_w
1be0: 69 74 68 69 6e 5f 74 72 65 65 28 29 3b 0a 20 20  ithin_tree();.  
1bf0: 63 6f 6d 70 75 74 65 5f 6c 65 61 76 65 73 28 30  compute_leaves(0
1c00: 2c 20 73 68 6f 77 41 6c 6c 20 3f 20 30 20 3a 20  , showAll ? 0 : 
1c10: 73 68 6f 77 43 6c 6f 73 65 64 20 3f 20 32 20 3a  showClosed ? 2 :
1c20: 20 31 29 3b 0a 20 20 64 62 5f 70 72 65 70 61 72   1);.  db_prepar
1c30: 65 28 26 71 2c 0a 20 20 20 20 22 25 73 22 0a 20  e(&q,.    "%s". 
1c40: 20 20 20 22 20 20 20 41 4e 44 20 62 6c 6f 62 2e     "   AND blob.
1c50: 72 69 64 20 49 4e 20 6c 65 61 76 65 73 22 0a 20  rid IN leaves". 
1c60: 20 20 20 22 20 4f 52 44 45 52 20 42 59 20 65 76     " ORDER BY ev
1c70: 65 6e 74 2e 6d 74 69 6d 65 20 44 45 53 43 22 2c  ent.mtime DESC",
1c80: 0a 20 20 20 20 74 69 6d 65 6c 69 6e 65 5f 71 75  .    timeline_qu
1c90: 65 72 79 5f 66 6f 72 5f 74 74 79 28 29 0a 20 20  ery_for_tty().  
1ca0: 29 3b 0a 20 20 70 72 69 6e 74 5f 74 69 6d 65 6c  );.  print_timel
1cb0: 69 6e 65 28 26 71 2c 20 32 30 30 30 29 3b 0a 20  ine(&q, 2000);. 
1cc0: 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29   db_finalize(&q)
1cd0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
1ce0: 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
1cf0: 64 20 77 68 69 6c 65 20 66 6f 72 20 65 61 63 68  d while for each
1d00: 20 63 68 65 63 6b 2d 69 6e 20 74 68 61 74 20 69   check-in that i
1d10: 73 20 72 65 6e 64 65 72 65 64 20 62 79 0a 2a 2a  s rendered by.**
1d20: 20 74 68 65 20 22 6c 65 61 76 65 73 22 20 70 61   the "leaves" pa
1d30: 67 65 2e 20 20 41 64 64 20 73 6f 6d 65 20 61 64  ge.  Add some ad
1d40: 64 69 74 69 6f 6e 61 6c 20 68 79 70 65 72 6c 69  ditional hyperli
1d50: 6e 6b 20 74 6f 20 73 68 6f 77 20 74 68 65 20 0a  nk to show the .
1d60: 2a 2a 20 61 6e 63 65 73 74 6f 72 73 20 6f 66 20  ** ancestors of 
1d70: 74 68 65 20 6c 65 61 66 2e 0a 2a 2f 0a 73 74 61  the leaf..*/.sta
1d80: 74 69 63 20 76 6f 69 64 20 6c 65 61 76 65 73 5f  tic void leaves_
1d90: 65 78 74 72 61 28 69 6e 74 20 72 69 64 29 7b 0a  extra(int rid){.
1da0: 20 20 40 20 3c 61 20 68 72 65 66 3d 22 25 73 28    @ <a href="%s(
1db0: 67 2e 7a 42 61 73 65 55 52 4c 29 2f 74 69 6d 65  g.zBaseURL)/time
1dc0: 6c 69 6e 65 3f 70 3d 25 64 28 72 69 64 29 22 3e  line?p=%d(rid)">
1dd0: 5b 74 69 6d 65 6c 69 6e 65 5d 3c 2f 61 3e 0a 7d  [timeline]</a>.}
1de0: 0a 0a 2f 2a 0a 2a 2a 20 57 45 42 50 41 47 45 3a  ../*.** WEBPAGE:
1df0: 20 20 6c 65 61 76 65 73 0a 2a 2a 0a 2a 2a 20 46    leaves.**.** F
1e00: 69 6e 64 20 6c 65 61 76 65 73 20 6f 66 20 61 6c  ind leaves of al
1e10: 6c 20 62 72 61 6e 63 68 65 73 2e 0a 2a 2f 0a 76  l branches..*/.v
1e20: 6f 69 64 20 6c 65 61 76 65 73 5f 70 61 67 65 28  oid leaves_page(
1e30: 76 6f 69 64 29 7b 0a 20 20 53 74 6d 74 20 71 3b  void){.  Stmt q;
1e40: 0a 20 20 69 6e 74 20 73 68 6f 77 41 6c 6c 20 3d  .  int showAll =
1e50: 20 50 28 22 61 6c 6c 22 29 21 3d 30 3b 0a 20 20   P("all")!=0;.  
1e60: 69 6e 74 20 73 68 6f 77 43 6c 6f 73 65 64 20 3d  int showClosed =
1e70: 20 50 28 22 63 6c 6f 73 65 64 22 29 21 3d 30 3b   P("closed")!=0;
1e80: 0a 0a 20 20 6c 6f 67 69 6e 5f 63 68 65 63 6b 5f  ..  login_check_
1e90: 63 72 65 64 65 6e 74 69 61 6c 73 28 29 3b 0a 20  credentials();. 
1ea0: 20 69 66 28 20 21 67 2e 6f 6b 52 65 61 64 20 29   if( !g.okRead )
1eb0: 7b 20 6c 6f 67 69 6e 5f 6e 65 65 64 65 64 28 29  { login_needed()
1ec0: 3b 20 72 65 74 75 72 6e 3b 20 7d 0a 0a 20 20 69  ; return; }..  i
1ed0: 66 28 20 21 73 68 6f 77 41 6c 6c 20 29 7b 0a 20  f( !showAll ){. 
1ee0: 20 20 20 73 74 79 6c 65 5f 73 75 62 6d 65 6e 75     style_submenu
1ef0: 5f 65 6c 65 6d 65 6e 74 28 22 41 6c 6c 22 2c 20  _element("All", 
1f00: 22 41 6c 6c 22 2c 20 22 6c 65 61 76 65 73 3f 61  "All", "leaves?a
1f10: 6c 6c 22 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  ll");.  }.  if( 
1f20: 21 73 68 6f 77 43 6c 6f 73 65 64 20 29 7b 0a 20  !showClosed ){. 
1f30: 20 20 20 73 74 79 6c 65 5f 73 75 62 6d 65 6e 75     style_submenu
1f40: 5f 65 6c 65 6d 65 6e 74 28 22 43 6c 6f 73 65 64  _element("Closed
1f50: 22 2c 20 22 43 6c 6f 73 65 64 22 2c 20 22 6c 65  ", "Closed", "le
1f60: 61 76 65 73 3f 63 6c 6f 73 65 64 22 29 3b 0a 20  aves?closed");. 
1f70: 20 7d 0a 20 20 69 66 28 20 73 68 6f 77 43 6c 6f   }.  if( showClo
1f80: 73 65 64 20 7c 7c 20 73 68 6f 77 41 6c 6c 20 29  sed || showAll )
1f90: 7b 0a 20 20 20 20 73 74 79 6c 65 5f 73 75 62 6d  {.    style_subm
1fa0: 65 6e 75 5f 65 6c 65 6d 65 6e 74 28 22 4f 70 65  enu_element("Ope
1fb0: 6e 22 2c 20 22 4f 70 65 6e 22 2c 20 22 6c 65 61  n", "Open", "lea
1fc0: 76 65 73 22 29 3b 0a 20 20 7d 0a 20 20 73 74 79  ves");.  }.  sty
1fd0: 6c 65 5f 68 65 61 64 65 72 28 22 4c 65 61 76 65  le_header("Leave
1fe0: 73 22 29 3b 0a 20 20 6c 6f 67 69 6e 5f 61 6e 6f  s");.  login_ano
1ff0: 6e 79 6d 6f 75 73 5f 61 76 61 69 6c 61 62 6c 65  nymous_available
2000: 28 29 3b 0a 20 20 63 6f 6d 70 75 74 65 5f 6c 65  ();.  compute_le
2010: 61 76 65 73 28 30 2c 20 73 68 6f 77 41 6c 6c 20  aves(0, showAll 
2020: 3f 20 30 20 3a 20 73 68 6f 77 43 6c 6f 73 65 64  ? 0 : showClosed
2030: 20 3f 20 32 20 3a 20 31 29 3b 0a 20 20 73 74 79   ? 2 : 1);.  sty
2040: 6c 65 5f 73 69 64 65 62 6f 78 5f 62 65 67 69 6e  le_sidebox_begin
2050: 28 22 4e 6f 6d 65 6e 63 6c 61 74 75 72 65 3a 22  ("Nomenclature:"
2060: 2c 20 22 33 33 25 22 29 3b 0a 20 20 40 20 3c 6f  , "33%");.  @ <o
2070: 6c 3e 0a 20 20 40 20 3c 6c 69 3e 20 41 20 3c 62  l>.  @ <li> A <b
2080: 3e 6c 65 61 66 3c 2f 62 3e 20 69 73 20 61 20 63  >leaf</b> is a c
2090: 68 65 63 6b 2d 69 6e 20 77 69 74 68 20 6e 6f 20  heck-in with no 
20a0: 64 65 73 63 65 6e 64 61 6e 74 73 2e 3c 2f 6c 69  descendants.</li
20b0: 3e 0a 20 20 40 20 3c 6c 69 3e 20 41 6e 20 3c 62  >.  @ <li> An <b
20c0: 3e 6f 70 65 6e 20 6c 65 61 66 3c 2f 62 3e 20 69  >open leaf</b> i
20d0: 73 20 61 20 6c 65 61 66 20 74 68 61 74 20 64 6f  s a leaf that do
20e0: 65 73 20 6e 6f 74 20 68 61 76 65 20 61 20 22 63  es not have a "c
20f0: 6c 6f 73 65 64 22 20 74 61 67 0a 20 20 40 20 61  losed" tag.  @ a
2100: 6e 64 20 69 73 20 74 68 75 73 20 61 73 73 75 6d  nd is thus assum
2110: 65 64 20 74 6f 20 73 74 69 6c 6c 20 62 65 20 69  ed to still be i
2120: 6e 20 75 73 65 2e 3c 2f 6c 69 3e 0a 20 20 40 20  n use.</li>.  @ 
2130: 3c 6c 69 3e 20 41 20 3c 62 3e 63 6c 6f 73 65 64  <li> A <b>closed
2140: 20 6c 65 61 66 3c 2f 62 3e 20 68 61 73 20 61 20   leaf</b> has a 
2150: 22 63 6c 6f 73 65 64 22 20 74 61 67 20 61 6e 64  "closed" tag and
2160: 20 69 73 20 74 68 75 73 20 61 73 73 75 6d 65 64   is thus assumed
2170: 20 74 6f 0a 20 20 40 20 62 65 20 68 69 73 74 6f   to.  @ be histo
2180: 72 69 63 61 6c 20 61 6e 64 20 6e 6f 20 6c 6f 6e  rical and no lon
2190: 67 65 72 20 69 6e 20 61 63 74 69 76 65 20 75 73  ger in active us
21a0: 65 2e 3c 2f 6c 69 3e 0a 20 20 40 20 3c 2f 6f 6c  e.</li>.  @ </ol
21b0: 3e 0a 20 20 73 74 79 6c 65 5f 73 69 64 65 62 6f  >.  style_sidebo
21c0: 78 5f 65 6e 64 28 29 3b 0a 0a 20 20 69 66 28 20  x_end();..  if( 
21d0: 73 68 6f 77 41 6c 6c 20 29 7b 0a 20 20 20 20 40  showAll ){.    @
21e0: 20 3c 68 31 3e 41 6c 6c 20 6c 65 61 76 65 73 2c   <h1>All leaves,
21f0: 20 62 6f 74 68 20 6f 70 65 6e 20 61 6e 64 20 63   both open and c
2200: 6c 6f 73 65 64 3a 3c 2f 68 31 3e 0a 20 20 7d 65  losed:</h1>.  }e
2210: 6c 73 65 20 69 66 28 20 73 68 6f 77 43 6c 6f 73  lse if( showClos
2220: 65 64 20 29 7b 0a 20 20 20 20 40 20 3c 68 31 3e  ed ){.    @ <h1>
2230: 43 6c 6f 73 65 64 20 6c 65 61 76 65 73 3a 3c 2f  Closed leaves:</
2240: 68 31 3e 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  h1>.  }else{.   
2250: 20 40 20 3c 68 31 3e 4f 70 65 6e 20 6c 65 61 76   @ <h1>Open leav
2260: 65 73 3a 3c 2f 68 31 3e 0a 20 20 7d 0a 20 20 64  es:</h1>.  }.  d
2270: 62 5f 70 72 65 70 61 72 65 28 26 71 2c 0a 20 20  b_prepare(&q,.  
2280: 20 20 22 25 73 22 0a 20 20 20 20 22 20 20 20 41    "%s".    "   A
2290: 4e 44 20 62 6c 6f 62 2e 72 69 64 20 49 4e 20 6c  ND blob.rid IN l
22a0: 65 61 76 65 73 22 0a 20 20 20 20 22 20 4f 52 44  eaves".    " ORD
22b0: 45 52 20 42 59 20 65 76 65 6e 74 2e 6d 74 69 6d  ER BY event.mtim
22c0: 65 20 44 45 53 43 22 2c 0a 20 20 20 20 74 69 6d  e DESC",.    tim
22d0: 65 6c 69 6e 65 5f 71 75 65 72 79 5f 66 6f 72 5f  eline_query_for_
22e0: 77 77 77 28 29 0a 20 20 29 3b 0a 20 20 77 77 77  www().  );.  www
22f0: 5f 70 72 69 6e 74 5f 74 69 6d 65 6c 69 6e 65 28  _print_timeline(
2300: 26 71 2c 20 54 49 4d 45 4c 49 4e 45 5f 4c 45 41  &q, TIMELINE_LEA
2310: 46 4f 4e 4c 59 2c 20 6c 65 61 76 65 73 5f 65 78  FONLY, leaves_ex
2320: 74 72 61 29 3b 0a 20 20 64 62 5f 66 69 6e 61 6c  tra);.  db_final
2330: 69 7a 65 28 26 71 29 3b 0a 20 20 40 20 3c 62 72  ize(&q);.  @ <br
2340: 20 63 6c 65 61 72 3d 22 62 6f 74 68 22 3e 0a 20   clear="both">. 
2350: 20 40 20 3c 73 63 72 69 70 74 3e 0a 20 20 40 20   @ <script>.  @ 
2360: 66 75 6e 63 74 69 6f 6e 20 78 69 6e 28 69 64 29  function xin(id)
2370: 7b 0a 20 20 40 20 7d 0a 20 20 40 20 66 75 6e 63  {.  @ }.  @ func
2380: 74 69 6f 6e 20 78 6f 75 74 28 69 64 29 7b 0a 20  tion xout(id){. 
2390: 20 40 20 7d 0a 20 20 40 20 3c 2f 73 63 72 69 70   @ }.  @ </scrip
23a0: 74 3e 0a 20 20 73 74 79 6c 65 5f 66 6f 6f 74 65  t>.  style_foote
23b0: 72 28 29 3b 0a 7d 0a                             r();.}.