hgforest / forest.py

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
# Forest, an extension to work on a set of nested Mercurial trees.
#
# Copyright (C) 2006 by Robin Farine <robin.farine@terminus.org>
#
# This software may be used and distributed according to the terms
# of the GNU General Public License, incorporated herein by reference.

# Repository path representation
#
# Repository paths stored in the filesystem representation are stored
# in variables named 'rpath'. Repository roots in the mercurial
# representation, stored in variables named 'root', are used in
# snapshot files and in command output.

"""Operations on trees with nested Mercurial repositories.

This extension provides commands that apply to a composite tree called
a forest. Some commands simply wrap standard Mercurial commands, such
as 'clone' or 'status', and others involve a snapshot file.

A snapshot file represents the state of a forest at a given time. It
has the format of a ConfigParser file and lists the trees in a forest,
each tree with the following attributes:

  root          path relative to the top-level tree
  revision      the revision the working directory is based on
  paths         a list of (alias, location) pairs

The 'fsnap' command generates or updates such a file based on a forest
in the file system. Other commands use this information to populate a
forest or to pull/push changes.


Configuration

This extension recognizes the following item in the forest
configuration section:

walkhg = (0|no|false|1|yes|true)

  Whether repositories directly under a .hg directory should be
  skipped (0|no|false) or not (1|yes|true). The default value is true.
  Some commands accept the --walkhg command-line option to override
  the behavior selected by this item.

partial = (0|no|false|1|yes|true)

  Whether fpull should default to partial. The default value is 0.

"""

import errno
import os
import re
import shutil

from mercurial import cmdutil, commands, error, hg, hgweb, node, util
from mercurial import localrepo, sshrepo, sshserver, httprepo, statichttprepo
from mercurial.i18n import gettext as _

# For backwards compatibility, we need the following function definition.
# If we didn't want that, we'd have just written:
#     from mercurial.commands import 
def findcmd(ui, cmd, table, strict=True):
    """Find and execute mercurial.*.findcmd([ui,] cmd[, table, strict])."""
    try:
        return findcmd.findcmd(cmd=cmd, table=table, strict=strict)
    except TypeError:
        try:
            return findcmd.findcmd(ui=ui, cmd=cmd, table=table)
        except TypeError:
            return findcmd.findcmd(ui, cmd)
try:
    findcmd.findcmd = cmdutil.findcmd
    findcmd.__doc__ = cmdutil.findcmd.__doc__
except AttributeError:
    findcmd.findcmd = commands.findcmd
    findcmd.__doc__ = commands.findcmd.__doc__
for m in (error, cmdutil, commands):
    if hasattr(m, "UnknownCommand"):
        UnknownCommand = m.UnknownCommand
        break
try:
    # Assign the exceptions explicitely to avoid demandload issues
    import mercurial.repo
    import mercurial.cmdutil
    RepoError = mercurial.repo.RepoError
    ParseError = mercurial.dispatch.ParseError
except AttributeError:
    import mercurial.error
    RepoError = mercurial.error.RepoError
    ParseError = mercurial.error.ParseError

# For backwards compatibility, find the parseurl() function that splits
# urls and revisions.  Mercurial 0.9.3 doesn't have this, so we need
# to provide a stub.
try:
    parseurl = cmdutil.parseurl
except:
    try:
        parseurl = hg.parseurl
    except:
        def parseurl(url, revs):
            """Mercurial <= 0.9.3 doesn't have this feature."""
            return url, (revs or None)

# For backwards compatibility, find the HTTP protocol.
if not hasattr(hgweb, 'protocol'):
    hgweb.protocol = hgweb.hgweb_mod.hgweb

# There are no issues with backward compatibility and ConfigParser.
# But since it was replaced in mercurial >= 1.3, the module is not
# longer shipped by Windows binary packages. In this case, use
# mercurial.config instead.
try:
    from mercurial import config
    ConfigError = error.ConfigError

    def readconfig(path):
        cfg = config.config()
        try:
            cfg.read(path)
            return cfg
        except IOError:
            return None

except (ImportError, AttributeError):
    import ConfigParser
    ConfigError = ConfigParser.Error    

    def readconfig(path):
        cfg = ConfigParser.RawConfigParser()
        if not cfg.read([path]):
            return None
        return cfg

class SnapshotError(ConfigError):
    pass

def cmd_options(ui, cmd, remove=None, table=commands.table):
    aliases, spec = findcmd(ui, cmd, table)
    res = list(spec[1])
    if remove is not None:
        res = [opt for opt in res
               if opt[0] not in remove and opt[1] not in remove]
    return res

def walkhgenabled(ui, walkhg):
    if not walkhg:
        walkhg = ui.config('forest', 'walkhg', 'true')
    try:
        res = { '0' : False, 'false' : False, 'no' : False,
                '1' : True, 'true' : True, 'yes' : True }[walkhg.lower()]
    except KeyError:
        raise util.Abort(_("invalid value for 'walkhg': %s" % walkhg))
    return res

def partialenabled(ui, partial):
    if partial:
        return partial
    else:
        partial = ui.config('forest', 'partial', 'false')
    try:
        res = { '0' : False, 'false' : False, 'no' : False,
                '1' : True, 'true' : True, 'yes' : True }[partial.lower()]
    except KeyError:
        raise util.Abort(_("invalid value for 'partial': %s" % partial))
    return res

def _localrepo_forests(self, walkhg):
    """Shim this function into mercurial.localrepo.localrepository so
    that it gives you the list of subforests.

    Return a list of roots in filesystem representation relative to
    the self repository.  This list is lexigraphically sorted.
    """

    def errhandler(err):
        if err.filename == self.root:
            raise err

    def normpath(path):
        if path:
            return util.normpath(path)
        else:
            return '.'

    res = {}
    paths = [self.root]
    while paths:
        path = paths.pop()
        if os.path.realpath(path) in res:
            continue
        for root, dirs, files in os.walk(path, onerror=errhandler):
            hgdirs = dirs[:]  # Shallow-copy to protect d from dirs.remove() 
            for d in hgdirs:
                if d == '.hg':
                    res[os.path.realpath(root)] = root
                    if not walkhg:
                        dirs.remove(d)
                else:
                    p = os.path.join(root, d)
                    if os.path.islink(p) and os.path.abspath(p) not in res:
                        paths.append(p)
    res = res.values()
    res.sort()
    # Turn things into relative paths
    pfx = len(self.root) + 1
    return [normpath(r[pfx:]) for r in res]

localrepo.localrepository.forests = _localrepo_forests


def _sshrepo_forests(self, walkhg):
    """Shim this function into mercurial.sshrepo.sshrepository so
    that it gives you the list of subforests.

    Return a list of roots as ssh:// URLs.
    """

    if 'forests' not in self.capabilities:
        raise util.Abort(_("Remote forests cannot be cloned because the "
                           "other repository doesn't support the forest "
                           "extension."))
    if hasattr(self, 'call'):
        # hg < 1.7
        func = self.call
    else:
        # hg >= 1.7
        func = self._call
    data = func("forests", walkhg=("", "True")[walkhg])
    return data.splitlines()

sshrepo.sshrepository.forests = _sshrepo_forests


def _sshserver_do_hello(self):
    '''the hello command returns a set of lines describing various
    interesting things about the server, in an RFC822-like format.
    Currently the only one defined is "capabilities", which
    consists of a line in the form:
    
    capabilities: space separated list of tokens
    '''

    caps = ['unbundle', 'lookup', 'changegroupsubset', 'forests']
    if self.ui.configbool('server', 'uncompressed'):
        if hasattr(self.repo, "revlogversion"):
            version = self.repo.revlogversion
        else:
            version = self.repo.changelog.version
        caps.append('stream=%d' % version)
    self.respond("capabilities: %s\n" % (' '.join(caps),))

sshserver.sshserver.do_hello = _sshserver_do_hello


def _sshserver_do_forests(self):
    """Shim this function into the sshserver so that it responds to
    the forests command.  It gives a list of roots relative to the
    self.repo repository, sorted lexigraphically.
    """
    
    key, walkhg = self.getarg()
    forests = self.repo.forests(bool(walkhg))
    self.respond("\n".join(forests))

sshserver.sshserver.do_forests = _sshserver_do_forests



def _httprepo_forests(self, walkhg):
    """Shim this function into mercurial.httprepo.httprepository so
    that it gives you the list of subforests.

    Return a list of roots as http:// URLs.
    """

    if 'forests' not in self.capabilities:
        raise util.Abort(_("Remote forests cannot be cloned because the "
                           "other repository doesn't support the forest "
                           "extension."))
    if hasattr(self, 'do_read'):
        # < 1.7
        func = self.do_read
    else:
        # >= 1.7
        func = self._call
    data = func("forests", walkhg=("", "True")[walkhg])
    return data.splitlines()

httprepo.httprepository.forests = _httprepo_forests


def _httpserver_do_capabilities(self, req):
    caps = ['lookup', 'changegroupsubset', 'forests']
    if self.configbool('server', 'uncompressed'):
        if hasattr(self.repo, "revlogversion"):
            version = self.repo.revlogversion
        else:
            version = self.repo.changelog.version
        caps.append('stream=%d' % version)
    # XXX: make configurable and/or share code with do_unbundle:
    unbundleversions = ['HG10GZ', 'HG10BZ', 'HG10UN']
    if unbundleversions:
        caps.append('unbundle=%s' % ','.join(unbundleversions))
    resp = ' '.join(caps)
    req.httphdr("application/mercurial-0.1", length=len(resp))
    req.write(resp)

hgweb.protocol.do_capabilities = _httpserver_do_capabilities


def _httpserver_do_forests(self, req):
    """Shim this function into the httpserver so that it responds to
    the forests command.  It gives a list of roots relative to the
    self.repo repository, sorted lexigraphically.
    """

    resp = ""
    if req.form.has_key('walkhg'):
        forests = self.repo.forests(bool(req.form['walkhg'][0]))
        resp = "\n".join(forests)
    req.httphdr("application/mercurial-0.1", length=len(resp))
    req.write(resp)


hgweb.protocol.do_forests = _httpserver_do_forests


def _statichttprepo_forests(self, walkhg):
    """Shim this function into
    mercurial.statichttprepo.statichttprepository so that it gives you
    the list of subforests.

    It depends on the fact that most directory indices have directory
    names followed by a slash.  There is no reliable way of telling
    whether a link leads into a subdirectory.

    Return a list of roots in filesystem representation relative to
    the self repository.  This list is lexigraphically sorted.
    """

    import HTMLParser
    import string
    import urllib
    import urllib2
    import urlparse

    class HtmlIndexParser(HTMLParser.HTMLParser):
        def __init__(self, ui, paths, walkhg):
            self._paths = paths
            self._ui = ui
            self._walkhg = walkhg
            self.current = None
        def handle_starttag(self, tag, attrs):
            if string.lower(tag) == "a":
                for attr in attrs:
                    if (string.lower(attr[0]) == "href" and
                        attr[1].endswith('/')):
                        link = urlparse.urlsplit(attr[1])
                        if (not self._walkhg and
                            link[2].rstrip('/').split('/')[-1] == '.hg'):
                            break
                        if not link[0] and not link[2].startswith('/'):
                            self._ui.debug(_("matched on '%s'") % attr[1])
                            self._paths.append(urlparse.urljoin(self.current,
                                                                attr[1]))

    if self._url.endswith('/'):
        url = self._url
    else:
        url = self._url + '/'

    res = []
    paths = [url]
    seen = {}

    parser = HtmlIndexParser(self.ui, paths, walkhg)
    while paths:
        path = paths.pop()
        if not seen.has_key(path):
            seen[path] = True
            parser.current = path
            index = None
            try:
                self.ui.debug(_("retrieving '%s'\n") % path)
                index = urllib2.urlopen(path)
                parser.reset()
                parser.feed(index.read())
                parser.close()
                hg_path = urlparse.urljoin(path, '.hg')
                self.ui.debug(_("retrieving '%s'\n") % hg_path)
                hg = urllib2.urlopen(hg_path)
                res.append(path)
            except urllib2.HTTPError, inst:
                pass
                #raise IOError(None, inst)
            except urllib2.URLError, inst:
                pass
                #raise IOError(None, inst.reason[1])

    res.sort()
    # Turn things into relative paths
    return [root[len(url):].rstrip('/') or "." for root in res]

statichttprepo.statichttprepository.forests = _statichttprepo_forests


def die_on_numeric_revs(revs):
    """Check to ensure that the revs passed in are not numeric.

    Numeric revisions make no sense when searching a forest.  You want
    only named branches and tags.  The only special exception is
    revision -1, which occurs before the first checkin.
    """
    if revs is None:
        return
    if not hasattr(revs, '__iter__'):
        revs = [revs]
    for strrev in revs:
        try:
            intrev = int(strrev)
        except:
            continue                    # String-based revision
        if intrev == 0 and strrev.startswith("00"):
            continue                    # Revision -1
        raise util.Abort(_("numeric revision '%s'") % strrev)


def relpath(root, pathname):
    """Returns the relative path of a local pathname from a local root."""
    root = os.path.abspath(root)
    pathname = os.path.abspath(pathname)
    if root == pathname or pathname.startswith(root + os.sep):
        pathname = os.path.normpath(pathname[len(root)+1:])
    return pathname


def urltopath(url):
    if url and hg.islocal(url):
        if url.startswith("file://"):
            url = url[7:]
        elif url.startswith("file:"):
            url = url[5:]
    return url


class Forest(object):
    """Describes the state of the forest within the current repository.

    This data structure describes the Forest contained within the
    current repository.  It contains a list of Trees that describe
    each sub-repository.
    """    

    class Tree(object):
        """Describe a local sub-repository within a forest."""

        class Skip(Warning):
            """Exception that signals this tree should be skipped."""
            pass

        __slots__ = ('_repo', '_root', 'revs', 'paths')

        def __init__(self, repo=None, root=None, revs=[], paths={}):
            """Create a Tree object.

            repo may be any mercurial.localrepo object
            root is the absolute path of this repo object
            rev is the desired revision for this repository, None meaning the tip
            paths is a dictionary of path aliases to real paths
            """
            self._repo = repo
            self.revs = revs
            if repo:
                self.paths = {}
                self.setrepo(repo)
                self.paths.update(paths)
            else:
                self.setroot(root)
                self.paths = paths

        def die_on_mq(self, rootpath=None):
            """Raises a util.Abort exception if self has mq patches applied."""
            if self.mq_applied():
                rpath = self.root
                if rootpath:
                    if not isinstance(rootpath, str):
                        rootpath = rootpath.root
                    rpath = relpath(rootpath, rpath)
                raise util.Abort(_("'%s' has mq patches applied") % rpath)

        def mq_applied(self):
            rpath = urltopath(self.root)
            if not hg.islocal(rpath):
                raise util.Abort(_("'%s' is not a local repository") % rpath)
            rpath = util.localpath(rpath)
            rpath = os.path.join(rpath, ".hg")
            if not os.path.isdir(rpath):
                return False
            for entry in os.listdir(rpath):
                path = os.path.join(rpath, entry)
                if (os.path.isdir(path) and
                    os.path.isfile(os.path.join(path, 'series'))):
                    try:
                        s = os.stat(os.path.join(path, "status"))
                        if s.st_size > 0:
                            return path
                    except OSError, err:
                        if err.errno != errno.ENOENT:
                            raise
            return False

        def getpath(self, paths):
            assert(type(paths) != str)
            if paths is None:
                return None
            for path in paths:
                if not hg.islocal(path):
                    return path
                result = urltopath(path)
                if os.path.isdir(result):
                    return result
                result = urltopath(self.paths.get(path, None))
                if result is not None:
                    return result
            return None

        def getrepo(self, ui=False):
            if not self._repo and self._root:
                if ui is False:
                    raise AttributeError("getrepo() requires 'ui' parameter")
                self._repo = hg.repository(ui, self._root)
            return self._repo

        def setrepo(self, repo):
            self._root = None
            self._repo = repo
            if repo.ui:
                self.paths.update(dict(repo.ui.configitems('paths')))

        def getroot(self):
            if self._repo:
                return self._repo.root
            else:
                return self._root

        def setroot(self, root):
            self._repo = None
            self._root = root

        @staticmethod
        def skip(function):
            """Decorator that turns any exception into a Forest.Tree.Skip"""
            def skipme(*args, **keywords):
                try:
                    function(*args, **keywords)
                except Exception, err:
                    raise Forest.Tree.Skip(err)
            return skipme

        @staticmethod
        def warn(function):
            """Decorator that turns any exception into a Warning"""
            def warnme(*args, **keywords):
                try:
                    function(*args, **keywords)
                except Exception, err:
                    raise Warning(err)
            return warnme

        def working_revs(self):
            """Returns the revision of the working copy."""
            try:
                ctx = self.repo[None]
            except TypeError:
                ctx = self.repo.workingctx()
            parents = ctx.parents()
            return [node.hex(parents[0].node())]

        def rollback(self):
            if self._repo:
                try:
                    self._repo.transaction('forest').__del__()
                except TypeError, err:
                    # Wrong number of arguments for hg < 1.6
                    if '2 given' not in str(err):
                        raise
                    self._repo.transaction().__del__()

        def __repr__(self):
            return ("<forest.Tree object "
                    "- repo: %s "
                    "- revs: %s "
                    "- root: %s "
                    "- paths: %s>") % (self.repo, self.revs,
                                       self.root, self.paths)

        repo = property(getrepo, setrepo, None, None)
        root = property(getroot, setroot, None, None)
        
    __slots__ = ('trees', 'snapfile')

    def __init__(self, error=None, top=None, snapfile=None, walkhg=True):
        """Create a Forest object.

        top is the mercurial.localrepo object at the top of the forest.
        snapfile is the filename of the snapshot file.
        walkhg controls if we descend into .hg directories.

        If you provide no snapfile, the top repo will be searched for
        sub-repositories.

        If you do provide a snapfile, then the snapfile will be read
        for sub-repositories and no searching of the filesystem will
        be done.  The top repository is queried for the root of all
        relative paths, but if it's missing, then the current
        directory will be assumed.
        """
        if error:
            raise AttributeError("__init__() takes only named arguments")
        self.trees = []
        self.snapfile = None
        if snapfile:
            self.snapfile = snapfile
            if top is None:
                toppath = ""
            else:
                toppath = top.root
            self.read(snapfile, toppath)
        elif top:
            self.trees.append(Forest.Tree(repo=top))
            if top.ui:
                top.ui.note(_("searching for repos in %s\n") % top.root)
            self.scan(walkhg)

    def collate_files(self, pats):
        """Returns a dictionary of absolute file paths, keyed Tree.

        This lets us iterate over repositories with only the files
        that belong to them.
        """
        result = {}
        files = [os.path.abspath(path) for path in list(pats)]
        if files:
            files.sort(reverse=True)
            trees = self.trees[:]
            trees.sort(reverse=True, key=(lambda tree: tree.root))
            for tree in trees:
                paths = []
                for path in files[:]:
                    if not os.path.exists(path):
                        raise util.Abort(_("%s not under root") % path)
                    if path.startswith(tree.root):
                        paths.append(path)
                        files.remove(path)
                if paths:
                    result[tree] = paths
        return result


    def apply(self, ui, function, paths, opts, prehooks=[]):
        """Apply function(repo, targetpath, opts) to the entire forest.

        path is a path provided on the command line.
        function is a function that should be called for every repository.
        opts is a list of options provided to the function
        prehooks is a list of hook(tree) that are run before function()

        Useful for the vast majority of commands that scan a local
        forest and perform some command on each sub-repository.
        
        Skips a sub-repository skipping it isn't actually a repository
        or if it has mq patches applied.

        In function(), targetpath will be /-separated.  You may have
        to util.localpath() it.
        """
        opts['force'] = None                # Acting on unrelated repos is BAD
        if paths:
            # Extract revisions from # syntax in path.
            paths[0], revs = parseurl(paths[0], opts['rev'])[0:2]
            if isinstance(revs, tuple):
                # hg >= 1.6
                revs = revs[1]
        elif 'rev' in opts:
            revs = opts['rev']
        else:
            revs = None
        die_on_numeric_revs(revs)
        for tree in self.trees:
            rpath = relpath(self.top().root, tree.root)
            ui.status("[%s]\n" % rpath)
            try:
                for hook in prehooks:
                    try:
                        hook(tree)
                    except Forest.Tree.Skip:
                        raise
                    except Warning, message:
                        ui.warn(_("warning: %s\n") % message)
            except Forest.Tree.Skip, message:
                ui.warn(_("skipped: %s\n") % message)
                ui.status("\n")
                continue
            except util.Abort:
                raise
            if revs:
                opts['rev'] = revs
            else:
                opts['rev'] = tree.revs
            targetpath = paths or None
            if paths:
                targetpath = tree.getpath(paths)
                if targetpath:
                    if targetpath == paths[0] and rpath != os.curdir:
                        targetpath = '/'.join((targetpath, util.pconvert(rpath)))
            function(tree, targetpath, opts)
            ui.status("\n")

    def read(self, snapfile, toppath="."):
        """Loads the information in snapfile into this forest.

        snapfile is the filename of a snapshot file
        toppath is the path of the top of this forest
        """
        if not toppath:
            toppath = "."
        cfg = readconfig(snapfile)
        if not cfg:
            raise util.Abort("%s: %s" % (snapfile, os.strerror(errno.ENOENT)))
        seen_root = False
        sections = {}
        for section in cfg.sections():
            if section.endswith('.paths'):
                # Compatibility with old Forest snapshot files
                paths = dict(cfg.items(section))
                section = section[:-6]
                if section in sections:
                    sections[section].paths.update(paths)
                else:
                    sections[section] = Forest.Tree(paths=paths)
            else:
                root = cfg.get(section, 'root')
                if root == '.':
                    seen_root = True
                    root = toppath
                else:
                    root = os.path.join(toppath, util.localpath(root))
                root = os.path.normpath(root)
                rev = cfg.get(section, 'revision')
                if not rev:
                    rev = []
                paths = dict([(k[5:], v)
                              for k, v in cfg.items(section)
                              if k.startswith('path')])
                if section in sections:
                    sections[section].root = root
                    sections[section].revs = [rev]
                    sections[section].paths.update(paths)
                else:
                    sections[section] = Forest.Tree(root=root,
                                                    revs=[rev],
                                                    paths=paths)
        if not seen_root:
            raise SnapshotError("Could not find 'root = .' in '%s'" %
                                snapfile)
        self.trees = sections.values()
        self.trees.sort(key=(lambda tree: tree.root))

    def scan(self, walkhg):
        """Scans for sub-repositories within this forest.

        This method modifies this forest in-place.  It searches within the
        forest's directories and enumerates all the repositories it finds.
        """
        trees = []
        top = self.top()
        ui = top.repo.ui
        for relpath in top.repo.forests(walkhg):
            if relpath != '.':
                abspath = os.path.join(top.root, util.localpath(relpath))
                trees.append(Forest.Tree(hg.repository(ui, abspath)))
        trees.sort(key=(lambda tree: tree.root))
        trees.insert(0, Forest.Tree(hg.repository(ui, top.root)))
        self.trees = trees

    def top(self):
        """Returns the top Forest.Tree in this forest."""
        if len(self.trees):
            return self.trees[0]
        else:
            return None

    def update(self, ui=None):
        """Gets the most recent information about repos."""
        try:
            if not ui:
                ui = self.top().repo.ui
        except:
            pass
        for tree in self.trees:
            try:
                repo = hg.repository(ui, tree.root)
            except RepoError:
                repo = None
            tree.repo = repo

    def write(self, fd, oldstyle=False):
        """Writes a snapshot file to a file descriptor."""
        counter = 1
        for tree in self.trees:
            fd.write("[tree%s]\n" % counter)
            root = relpath(self.top().root, tree.root)
            if root == os.curdir:
                root = '.'
            root = util.normpath(root)
            fd.write("root = %s\n" % root)
            if tree.revs:
                fd.write("revision = %s\n" % tree.revs[0])
            else:
                fd.write("revision = None\n")
            if not oldstyle:
                for name, path in tree.paths.items():
                    fd.write("path.%s = %s\n" % (name, path))
            else:
                fd.write("\n[tree%s.paths]\n" % counter)
                for name, path in tree.paths.items():
                    fd.write("%s = %s\n" % (name, path))
            fd.write("\n")
            counter += 1


    def __repr__(self):
        return ("<forest.Forest object - trees: %s> ") % self.trees


def qclone(ui, source, sroot, dest, rpath, opts):
    """Helper function to clone from a remote repository.

    source is the URL of the source of this repository
    dest is the directory of the destination
    rpath is the relative path of the destination
    opts are a list of options to be passed into the clone
    """
    ui.status("[%s]\n" % rpath)
    assert(dest is not None)
    destpfx = os.path.normpath(os.path.dirname(dest))
    if not os.path.exists(destpfx):
        os.makedirs(destpfx)
    repo = hg.repository(ui, source)
    mqdir = None
    assert(source is not None)
    if hg.islocal(source):
        Forest.Tree(repo=repo).die_on_mq(sroot)
    url = urltopath(repo.url())
    ui.note(_("cloning %s to %s\n") % (url, dest))
    commands.clone(ui, url, dest, **opts)
    repo = None


def clone(ui, source, dest=None, **opts):
    """make a clone of an existing forest of repositories

    Create a clone of an existing forest in a new directory.

    Look at the help text for the clone command for more information.
    """
    die_on_numeric_revs(opts['rev'])
    source = ui.expandpath(source) or source
    islocalsrc = hg.islocal(source)
    if islocalsrc:
        source = os.path.abspath(urltopath(source))
    if dest:
        if hg.islocal(dest):
            dest = os.path.normpath(dest)
        else:
            pass
    else:
        dest = hg.defaultdest(source)
    toprepo = hg.repository(ui, source)
    forests = toprepo.forests(walkhgenabled(ui, opts['walkhg']))
    for rpath in forests:
        if rpath == '.':
            rpath = ''
        if islocalsrc:
            srcpath = source
            srcpath = os.path.join(source, util.localpath(rpath))
        else:
            srcpath = '/'.join((source, rpath))
        if rpath:
            destpath = os.path.join(dest, util.localpath(rpath))
        else:
            destpath = dest
        try:
            qclone(ui=ui,
                   source=srcpath, sroot=source,
                   dest=destpath, rpath=os.path.normpath(rpath),
                   opts=opts)
        except util.Abort, err:
            ui.warn(_("skipped: %s\n") % err)
        ui.status("\n")


def fetch(ui, top, source="default", **opts):
    """pull changes from a remote forest, merge new changes if needed.

    This finds all changes from the forest at the specified path or
    URL and adds them to the local forest.

    Look at the help text for the fetch command for more information.
    """

    snapfile = opts['snapfile']
    forest = Forest(top=top, snapfile=snapfile,
                    walkhg=walkhgenabled(ui, opts['walkhg']))
    source = [source]
    try:
        import hgext.fetch as fetch
    except ImportError:
        raise util.Abort(_("could not import fetch module\n"))

    def function(tree, srcpath, opts):
        if not srcpath:
            srcpath = forest.top().getpath(source)
            if srcpath:
                rpath = util.pconvert(relpath(forest.top().root, tree.root))
                srcpath = '/'.join((srcpath, rpath))
            else:
                ui.warn(_("skipped: %s\n") %
                        _("repository %s not found") % source[0])
                return
        try:
            fetch.fetch(ui, tree.getrepo(ui), srcpath, **opts)
        except Exception, err:
            ui.warn(_("skipped: %s\n") % err)
            tree.rollback()

    @Forest.Tree.skip
    def check_mq(tree):
        tree.die_on_mq(top.root)

    forest.apply(ui, function, source, opts,
                 prehooks=[lambda tree: check_mq(tree)])


def incoming(ui, top, source="default", **opts):
    """show new changesets found in source forest

    Show new changesets found in the specified path/URL or the default
    pull location for each repository in the source forest.

    Look at the help text for the incoming command for more information.
    """
    die_on_numeric_revs(opts['rev'])
    forest = Forest(top=top, snapfile=opts['snapfile'],
                    walkhg=walkhgenabled(ui, opts['walkhg']))
    source = [source]
    opts["bundle"] = ""

    def function(tree, srcpath, opts):
        if not srcpath:
            srcpath = forest.top().getpath(source)
            if srcpath:
                rpath = util.pconvert(relpath(forest.top().root, tree.root))
                srcpath = '/'.join((srcpath, rpath))
            else:
                ui.warn(_("skipped: %s\n") %
                        _("repository %s not found") % source[0])
                return
        try:
            commands.incoming(ui, tree.repo, srcpath, **opts)
        except Exception, err:
            ui.warn(_("skipped: %s\n") % err)

    @Forest.Tree.warn
    def check_mq(tree):
        tree.die_on_mq(top.root)

    forest.apply(ui, function, source, opts,
                 prehooks=[lambda tree: check_mq(tree)])


def outgoing(ui, top, dest=None, **opts):
    """show changesets not found in destination forest

    Show changesets not found in the specified destination forest or
    the default push location.

    Look at the help text for the outgoing command for more information.
    """
    die_on_numeric_revs(opts['rev'])
    forest = Forest(top=top, snapfile=opts['snapfile'],
                    walkhg=walkhgenabled(ui, opts['walkhg']))
    if dest == None:
        dest = ["default-push", "default"]
    else:
        dest = [dest]

    def function(tree, destpath, opts):
        if not destpath:
            destpath = forest.top().getpath(dest)
            if destpath:
                rpath = util.pconvert(relpath(forest.top().root, tree.root))
                destpath = '/'.join((destpath, rpath))
            else:
                ui.warn(_("skipped: %s\n") %
                        _("repository %s not found") % dest[0])
                return
        try:
            commands.outgoing(ui, tree.repo, destpath, **opts)
        except Exception, err:
            ui.warn(_("skipped: %s\n") % err)

    @Forest.Tree.warn
    def check_mq(tree):
        tree.die_on_mq(top.root)

    forest.apply(ui, function, dest, opts,
                 prehooks=[lambda tree: check_mq(tree)])


def pull(ui, top, source="default", pathalias=None, **opts):
    """pull changes from the specified forest

    Pull changes from a remote forest to a local one.

    You may specify a snapshot file, which is generated by the fsnap
    command.  For each tree in this file, pull the specified revision
    from the specified source path.

    By default, pull new remote repositories that it discovers.  If
    you use the -p option, pull only the repositories available locally.

    Look at the help text for the pull command for more information.
    """

    die_on_numeric_revs(opts['rev'])
    if pathalias:
        # Compatibility with old 'hg fpull SNAPFILE PATH-ALIAS' syntax
        snapfile = source
        source = pathalias
    else:
        snapfile = opts['snapfile']
    source = [source]
    walkhg = walkhgenabled(ui, opts['walkhg'])
    forest = Forest(top=top, snapfile=snapfile, walkhg=walkhg)
    toproot = forest.top().root
    if not snapfile:
        # Look for new remote paths from source
        srcpath = forest.top().getpath(source) or ""
        srcrepo = hg.repository(ui, srcpath)
        srcforests = None
        try:
            srcforests = srcrepo.forests(walkhg)
        except util.Abort, err:
            ui.note(_("skipped new forests: %s\n") % err)
        if srcforests:
            ui.note(_("looking for new forests\n"))
            newrepos = [util.localpath(root) for root in srcforests]
            for tree in forest.trees:
                try:
                    newrepos.remove(relpath(toproot, tree.root))
                except Exception, err:
                    pass
            ui.note(_("found new forests: %s\n") % newrepos)
            forest.trees.extend([Forest.Tree(root=os.path.join(toproot, new))
                                 for new in newrepos])
            forest.trees.sort(key=(lambda tree: tree.root))
    opts['pull'] = True
    opts['uncompressed'] = None
    opts['noupdate'] = not opts['update']
    partial = partialenabled(ui, opts['partial'])

    def function(tree, srcpath, opts):
        if snapfile:
            opts['rev'] = tree.revs
        else:
            destpath = relpath(os.path.abspath(os.curdir), tree.root)
            rpath = util.pconvert(relpath(toproot, tree.root))
            if not srcpath:
                srcpath = forest.top().getpath(source)
                if srcpath:
                    srcpath = '/'.join((srcpath, rpath))
                else:
                    ui.warn(_("warning: %s\n") %
                            _("repository %s not found") % source[0])
                    return
            try:
                tree.getrepo(ui)
            except RepoError:
                if partial:
                    ui.warn(_("skipped: new remote repository\n"))
                else:
                    # Need to clone
                    quiet = ui.quiet
                    try:
                        ui.quiet = True # Hack to shut up qclone's ui.status()
                        qclone(ui=ui,
                               source=srcpath, sroot=source,
                               dest=destpath, rpath=rpath,
                               opts=opts)
                    except util.Abort, err:
                        ui.warn(_("skipped: %s\n") % err)
                    ui.quiet = quiet
                return
        try:
            commands.pull(ui, tree.getrepo(ui), srcpath, **opts)
        except Exception, err:
            ui.warn(_("skipped: %s\n") % err)
            tree.rollback()

    @Forest.Tree.skip
    def check_mq(tree):
        tree.die_on_mq(top.root)

    forest.apply(ui, function, source, opts,
                 prehooks=[lambda tree: check_mq(tree)])

def push(ui, top, dest=None, pathalias=None, **opts):
    """push changes to the specified forest.

    Push changes from the local forest to the given destination.

    You may specify a snapshot file, which is generated by the fsnap
    command.  For each tree in this file, push the specified revision
    to the specified destination path.

    Look at the help text for the push command for more information.
    """

    if pathalias:
        # Compatibility with old 'hg fpush SNAPFILE PATH-ALIAS' syntax
        snapfile = dest
        dest = [pathalias]
        opts['rev'] = ['tip']           # Force a push from tip
    else:
        snapfile = opts['snapfile']
        if dest:
            dest = [dest]
        else:
            dest = ["default-push", "default"]
    forest = Forest(top=top, snapfile=snapfile,
                    walkhg=walkhgenabled(ui, opts['walkhg']))

    def function(tree, destpath, opts):
        try:
            commands.push(ui, tree.getrepo(ui), destpath, **opts)
        except Exception, err:
            ui.warn(_("skipped: %s\n") % err)
            tree.rollback()

    @Forest.Tree.skip
    def check_mq(tree):
        tree.die_on_mq(top.root)

    forest.apply(ui, function, dest, opts,
                 prehooks=[lambda tree: check_mq(tree)])


def seed(ui, snapshot=None, source='default', **opts):
    """populate a forest according to a snapshot file.

    Populate an empty local forest according to a snapshot file.

    Given a snapshot file, clone any non-existant directory from the
    provided path-alias.  This defaults to cloning from the 'default'
    path.

    Unless the --tip option is set, this command will clone the
    revision specified in the snapshot file.

    Look at the help text for the clone command for more information.
    """

    snapfile = snapshot or opts['snapfile']
    if not snapfile:
        raise ParseError("fseed", _("invalid arguments"))
    forest = Forest(snapfile=snapfile)
    tip = opts['tip']
    dest = opts['root']
    if not dest:
        dest = os.curdir
        forest.trees.remove(forest.top())
    dest = os.path.normpath(dest)
    for tree in forest.trees:
        srcpath = tree.getpath([source])
        if not srcpath:
            ui.status("[%s]\n" % util.pconvert(tree.root))
            ui.warn(_("skipped: path alias %s not defined\n") % source)
            ui.status("\n")
            continue
        srcpath = urltopath(srcpath)
        if tree.root == ".":
            destpath = dest
        else:
            destpath = os.path.join(dest, tree.root)
        opts['rev'] = tree.revs
        try:
            qclone(ui=ui,
                   source=srcpath, sroot=None,
                   dest=destpath, rpath=util.pconvert(tree.root),
                   opts=opts)
        except util.Abort, err:
            ui.warn(_("skipped: %s\n") % err)
        ui.status("\n")


def snap(ui, top, snapshot=None, **opts):
    """take a snapshot of the forest and show it

    Shows the current state of the forest.

    You can use the output of this command as with the --snapfile
    option of other forest commands.

    When you provide a snapshot file, only the trees mentioned in that
    file will be shown.
    """

    snapfile = snapshot or opts['snapfile']
    tip = opts['tip']
    forest = Forest(top=top, snapfile=snapfile,
                    walkhg=walkhgenabled(ui, opts['walkhg']))
    if snapfile:
        forest.update(ui)
    for tree in forest.trees:
        tree.die_on_mq(top.root)
        if not tip:
            tree.revs = tree.working_revs()
    forest.write(ui, opts['compatible'])


def status(ui, top, *pats, **opts):
    """show changed files in the working forest

    Show status of files in this forest's repositories.

    Look at the help text for the status command for more information.
    """
    forest = Forest(top=top, walkhg=walkhgenabled(ui, opts['walkhg']))
    die_on_numeric_revs(opts['rev'])
    # Figure out which paths are relative to which roots
    files = forest.collate_files(pats)
    if files:
        # Trim which trees we're going to look at
        forest.trees = files.keys()

    class munge_ui(object):
        """This wrapper class allows us to munge the mercurial.ui.write() """
        def __init__(self, transform, ui):
            self._transform = transform
            self._ui = ui
        def write(self, output):
            self._ui.write(self._transform(output))
        def __getattr__(self, attrname):
            return getattr(self._ui, attrname)

    def function(tree, path, opts):
        path = util.localpath(path)
        if files:
            pats = files[tree]
        else:
            pats = ()
            if path == top.root:
                path = ''
            else:
                path = relpath(top.root, path)
            def prefix(output):
                """This function shims the root in before the filename."""
                if opts['no_status']:
                    return os.path.join(path, output)
                else:
                    prefix, filename = output.split(' ', 1)
                    return ' '.join((prefix, os.path.join(path, filename)))
            localui = munge_ui(prefix, ui)
        try:
            commands.status(localui, tree.repo, *pats, **opts)
        except RepoError, err:
            ui.warn(_("skipped: %s\n") % err)

    @Forest.Tree.warn
    def check_mq(tree):
        tree.die_on_mq(top.root)

    forest.apply(ui, function, [top.root], opts,
                 prehooks=[lambda tree: check_mq(tree)])


def tag(ui, top, name, revision=None, **opts):
    """add a tag for the current or given revision in the working forest

    Name a particular revision using <name>.

    Tags are used to name particular revisions of the repository and are
    very useful to compare different revision, to go back to significant
    earlier versions or to mark branch points as releases, etc.

    If no revision is given, the parent of the working directory is used,
    or tip if no revision is checked out.

    To facilitate version control, distribution, and merging of tags,
    they are stored as a file named ".hgtags" which is managed
    similarly to other project files and can be hand-edited if
    necessary.  The file '.hg/localtags' is used for local tags (not
    shared among repositories).
    """
    if revision is not None:
        ui.warn(_("use of 'hg ftag NAME [REV]' is deprecated, "
                  "please use 'hg ftag [-r REV] NAME' instead\n"))
        if opts['rev']:
            raise util.Abort(_("use only one form to specify the revision"))
        opts['rev'] = revision
    forest = Forest(top=top, snapfile=None,
                    walkhg=walkhgenabled(ui, opts['walkhg']))

    def function(tree, ignore, opts):
        try:
            commands.tag(ui, tree.getrepo(ui), name, rev_=None, **opts)
        except Exception, err:
            ui.warn(_("skipped: %s\n") % err)
            tree.rollback()

    @Forest.Tree.skip
    def check_mq(tree):
        tree.die_on_mq(top.root)

    forest.apply(ui, function, None, opts,
                 prehooks=[lambda tree: check_mq(tree)])


def trees(ui, top, **opts):
    """show the roots of the repositories

    Show the roots of the trees in the forest.

    By default, show the absolute path of each repository.  With
    --convert, show the portable Mercurial path.
    """

    forest = Forest(top=top,
                    walkhg=walkhgenabled(ui, opts['walkhg']))
    convert = opts['convert']
    for tree in forest.trees:
        if convert:
            ui.write("%s\n" % relpath(top.root, tree.root))
        else:
            ui.write("%s\n" % util.localpath(tree.root))


def update(ui, top, revision=None, **opts):
    """update working forest

    Update the working forest to the specified revision, or the
    tip of the current branch if none is specified.

    You may specify a snapshot file, which is generated by the fsnap
    command.  For each tree in this file, update to the revision
    recorded for that tree.

    Look at the help text for the update command for more information.
    """

    snapfile = None
    if revision:
        try:
            if readconfig(revision):
                # Compatibility with old 'hg fupdate SNAPFILE' syntax
                snapfile = revision
        except ConfigError, err:
            ui.warn(_("warning: %s\n") % err)
    if snapfile is None:
        snapfile = opts['snapfile']
        opts['rev'] = revision
    forest = Forest(top=top, snapfile=snapfile,
                    walkhg=walkhgenabled(ui, opts['walkhg']))

    def function(tree, ignore, opts):
        if 'rev' in opts:
            rev = opts['rev'] or None
        else:
            rev = None
        if hasattr(rev, '__iter__'):
            rev = rev[-1]
        try:
            if rev is not None:
                commands.update(ui, tree.getrepo(ui),
                                rev=rev, clean=opts['clean'], date=opts['date'])
            else:
                commands.update(ui, tree.getrepo(ui),
                                clean=opts['clean'], date=opts['date'])
        except Exception, err:
            ui.warn(_("skipped: %s\n") % err)
            tree.rollback()

    @Forest.Tree.skip
    def check_mq(tree):
        tree.die_on_mq(top.root)

    forest.apply(ui, function, None, opts,
                 prehooks=[lambda tree: check_mq(tree)])


cmdtable = None

def uisetup(ui):
    global cmdtable
    walkhgopts = ('', 'walkhg', '',
                  _("walk repositories under '.hg' (yes/no)"))
    snapfileopts = ('', 'snapfile', '',
                    _("snapshot file generated by fsnap"))
    cmdtable = {
        "^fclone" :
            (clone,
             [walkhgopts] + cmd_options(ui, 'clone'),
             _('hg fclone [OPTION]... SOURCE [DEST]')),
        "fincoming|fin" :
            (incoming,
             [walkhgopts, snapfileopts]
             + cmd_options(ui, 'incoming', remove=('f', 'bundle')),
             _('hg fincoming [OPTION]... [SOURCE]')),
        "foutgoing|fout" :
            (outgoing,
             [walkhgopts, snapfileopts]
             + cmd_options(ui, 'outgoing', remove=('f',)),
             _('hg foutgoing [OPTION]... [DEST]')),
        "^fpull" :
            (pull,
             [('p', 'partial', False,
               _("do not pull new remote repositories")),
              walkhgopts, snapfileopts] + cmd_options(ui, 'pull', remove=('f',)),
             _('hg fpull [OPTION]... [SOURCE]')),
        "^fpush" :
            (push,
             [walkhgopts, snapfileopts] + cmd_options(ui, 'push', remove=('f',)),
             _('hg fpush [OPTION]... [DEST]')),
        "fseed" :
            (seed,
             [('', 'root', '',
               _("create root as well as children under <root>")),
              snapfileopts,
              ('t', 'tip', False,
               _("use tip instead of revisions stored in the snapshot file"))]
             + cmd_options(ui, 'clone', remove=('r',)),
             _('hg fseed [OPTION]... SNAPSHOT-FILE [PATH-ALIAS]')),
        "fsnap" :
            (snap,
             [('', 'compatible', False,
               _("write snapshot file compatible with older forest versions")),
              snapfileopts,
              ('t', 'tip', False,
               _("record tip instead of actual child revisions")),
              walkhgopts],
             _('hg fsnap [OPTION]... [SNAPSHOT-FILE]')),
        "^fstatus|fst" :
            (status,
             [walkhgopts] + cmd_options(ui, 'status'),
             _('hg fstatus [OPTION]... [FILE]...')),
        "ftag":
            (tag,
             [walkhgopts] + cmd_options(ui, 'tag'),
             _('hg ftag [-l] [-m TEXT] [-d DATE] [-u USER] [-r REV] NAME')),
        "ftrees" :
            (trees,
             [('c', 'convert', False,
               _("convert paths to mercurial representation")),
              walkhgopts],
             _('hg ftrees [OPTIONS]')),
        "^fupdate|fup|fcheckout|fco" :
            (update,
             [snapfileopts,
              ('', 'tip', False,
               _("use tip instead of revisions stored in the snapshot file")),
              walkhgopts]
             + cmd_options(ui, 'update'),
             _('hg fupdate [OPTION]...'))
        }

    try:
        import hgext.fetch
    except ImportError:
        return
    try:
        cmdtable.update({"ffetch": (fetch,
                                    [walkhgopts, snapfileopts]
                                    + cmd_options(ui, 'fetch',
                                                  remove=('bundle',),
                                                  table=hgext.fetch.cmdtable),
                                    _('hg ffetch [OPTION]... [SOURCE]'))})
    except UnknownCommand:
        return

commands.norepo += " fclone fseed"
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.