~abentley/bzrtools/bzrtools.dev

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
1495
1496
1497
1498
1499
1500
# Copyright (C) 2004 Aaron Bentley
# <aaron.bentley@utoronto.ca>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import options
import os
import sys
import pybaz as arch

import pylon
from pylon.errors import *
from pylon import errors
from pylon import paths
from pylon import util

import shutil
import datetime
import dateutil.parser
import optparse 
import re
from optparse import OptionGroup
import add_tagline
from add_tagline import AlreadyTagged, NoCommentSyntax
import terminal

__docformat__ = "restructuredtext"
__doc__ = "Utility functions to be used by commands"
    

def alias_endpoint(spectype, spec):
    """Determines the endpoint for iteration.  Assumes input is of the form \
    "tprev" or "tprev:5".

    :param spectype: The leading portion of the specification, e.g. "tprev"
    :type spectype: string
    :param spec: The actual spec
    :type spec: string
    :return: The endpoint to use
    :rtype: integer
    """
    if len(spec)>len(spectype+":"):
        try:
            endpoint=int(spec[len(spectype+":"):])
        except ValueError:
            raise errors.CantDetermineRevision(spec, 
                                               "\""+spec[len(spectype+":"):]\
                                               +"\" is not an integer.")
        if endpoint < 0:
            raise errors.CantDetermineRevision(spec, "\""+str(endpoint)+\
                                               "\" is negative.")

    else:
        endpoint=1
    return endpoint


def determine_version_arch(arg, tree):
    """
    Returns an `arch.Version`, using the default archive if necessary.

    :param arg: version name
    :type arg: str
    :rtype: `arch.Version`
    """
    name=arch.NameParser(expand_alias(arg, tree))
    if not name.is_version():
        raise CantDetermineVersion(arg, "\"%s\" is not a version" % name)
    if not name.has_archive():
        return arch.Version(
            str(arch.default_archive())+"/"+ name.get_package_version())
    else:
        return arch.Version(name)

def determine_version_tree(arg, tree):
    """
    Returns an `arch.Version`, using the tree-version archive if necessary.

    :param arg: version name
    :type arg: str
    :param tree: The tree to use for getting the archive (if needed).
    :type tree: `arch.ArchSourceTree`
    :rtype: `arch.Version`
    """
    if arg is None:
        return tree.tree_version 
    name=arch.NameParser(expand_alias(arg, tree))
    if not name.is_version():
        raise CantDetermineVersion(arg, "\"%s\" is not a version" % name)
    if not name.has_archive():
        return arch.Version(
            str(tree.tree_version.archive)+"/"+ 
                name.get_package_version())
    else:
        return arch.Version(name)


def determine_version_or_revision_tree(tree, spec):
    name=arch.NameParser(expand_alias(arg.tree))
    if name.is_version():
        return determine_version_tree(name, tree)
    else:
        return determine_revision_tree(tree, name)
    

def expand_prefix_alias(args, tree, prefix="^"):
    """
    Treats all arguments that have the prefix as aliases, and expands them.

    :param args: The list of arguments.
    :type args: List of str
    :param tree: The current tree (or None, if there is no current tree)
    :type tree: `arch.ArchSourceTree`
    :param prefix: The prefix to use for aliases
    :type prefix: str
    :rtype: list of str
    """
    expanded=[]
    for segment in args:
        if segment.startswith(prefix):
            cropped=segment[len(prefix):]
            expansion=expand_alias(cropped, tree)
            if expansion != cropped:
                expanded.append(str(expansion))
        else:
            expanded.append(segment)
    return expanded


def tag_cur(tree):
    """Determine the latest revision of the version of the tag source.

    :param tree: The tree to determine the current revision of
    :type tree: `arch.WorkingTree`
    :return: The archive-latest revision of the tag source version
    :rtype: `arch.Revision`
    """
    if tree==None:
        raise CantDetermineRevision("tagcur", "No source tree available.")
    spec = pylon.tag_source(tree)
    if spec is None:
        raise CantDetermineRevision("tagcur", 
            "This revision has no ancestor version.")
    ensure_archive_registered(spec.version.archive)
    spec = spec.version.iter_revisions(reverse=True).next()
    return spec


def expand_alias(spec, tree):
    """
    Attempts to perform alias expansion on a given spec.  Will expand both \
    automatic aliases and user-specified aliases

    :param spec: The specification to expand.
    :type spec: string
    :param tree: The current working tree
    :type tree: `arch.ArchSourceTree`
    :return: The expanded result, or spec if no expansion was done
    :rtype: string or Revision

    """
    name = arch.NameParser(spec)
    if name.is_version() or name.has_patchlevel():
        return spec
    if spec.startswith("^"):
        spec=spec[1:]
    if pylon.paths.is_url_path(spec):
        try:
            spec, dummy=pylon.paths.full_path_decode(spec)
        except pylon.paths.CantDecode, e:
            pass
        
    elif spec.startswith("tprev"):
        if tree==None:
            raise CantDetermineRevision(spec, "No source tree available.")

        iter = tree.iter_logs(reverse=True)
        endpoint=alias_endpoint("tprev", spec)
        try:
            for q in range(endpoint):
                iter.next()
            spec = iter.next().revision
        except StopIteration:
            raise CantDetermineRevision(spec, 
                "Value \"%d\" is out of range." % endpoint)
    elif spec.startswith("tcur"):
        if tree==None:
            raise CantDetermineRevision(spec, "No source tree available.")
        version = None
        try:
            if len(spec)>4:
                version=determine_version_tree(expand_alias(spec[5:], tree),
                    tree)
            spec = tree.iter_logs(version, reverse=True).next().revision
        except StopIteration:
            if version is None:
                version = "default version (%s)" % str(tree.tree_version)
            raise CantDetermineRevision(spec, 
                "Tree contains no logs for %s." % str(version))
    elif spec == "ttag":
        if tree==None:
            raise CantDetermineRevision(spec, "No source tree available.")
        spec = pylon.tag_source(tree)
        if spec is None:
            raise CantDetermineRevision("ttag", 
                "This revision has no ancestor version.")

    elif spec == "tagcur":
        spec = tag_cur(tree)

    elif spec.startswith("tanc"):
        iter = pylon.iter_ancestry(tree)
        endpoint=alias_endpoint("tanc", spec)
        try:
            for q in range(endpoint):
                iter.next()
            spec = iter.next()
        except StopIteration:
            raise CantDetermineRevision(spec, 
                "Value \"%d\" is out of range." % endpoint)

    elif spec.startswith("tmod:"):
        try:
            return modified_iter(spec[5:], tree).next().revision
        except StopIteration, e:
            raise CantDetermineRevision(spec, 
                "Can't find a revision that changed \"%s\"." % spec[5:])


    elif spec.startswith("tdate:"):
        default=datetime.datetime.now().replace(hour=23, minute=59, second=59, 
                                                microsecond=999999)
        try:
            date = dateutil.parser.parse(spec[6:], default=default).timetuple()
        except ValueError, e:
            raise CantDetermineRevision(spec, str(e))
        if date > default.timetuple():
            raise CantDetermineRevision(spec, "Date is in the future.")
        revision = None
        for log in pylon.iter_any_ancestry(tree, tree.tree_revision):
            if not isinstance(log, arch.Patchlog):
                break
            if log.date <= date:
                revision = log.revision
                break
        if revision is None:
            raise CantDetermineRevision(spec, "No logs found for date \"%s\"."\
                                        % spec[6:])
        spec = revision        

    elif spec.startswith("acur"):
        if len(spec)>4:
            version=determine_version_arch(expand_alias(spec[5:], tree), tree)
        elif tree:
            version=tree.tree_version
        else:
            raise CantDetermineRevision(spec, "No source tree available.")
        ensure_archive_registered(version.archive)
        spec=version.iter_revisions(True).next()

    elif spec.startswith("mergeanc"):
        if tree is None:
            raise CantDetermineRevision(spec, "No source tree available.")

        if spec == "mergeanc":
            try:
                other_version = pylon.iter_partners(tree).next()
                other_revision = other_version.iter_revisions(True).next() 
            except StopIteration, e:
                other_revision = tag_cur(tree)
        else:
            other_revision = determine_revision_arch(tree, 
                spec[len("mergeanc:"):])
        other_tree = pylon.find_or_make_local_revision(other_revision)
        merge_anc = pylon.merge_ancestor2(tree, other_tree, 
                                                  other_revision)
        if merge_anc is None:
            raise CantDetermineRevision(spec, "Can't find a common ancestor.")
        return merge_anc

    elif spec.startswith("micro"):
        specsection = spec.split(':')
        if len(specsection) > 1:
            version = determine_version_arch(specsection[1], tree)
        else:
            version = tree.tree_version
        try:
            return pylon.iter_micro(tree, version).next().revision
        except StopIteration, e:
            raise CantDetermineRevision(spec, "No next microbranch revision")
        
    else:
        expansion=None
        if '@' in spec:
            return spec
        for parts in pylon.iter_all_alias(tree):
            if spec.startswith(parts[0]):
                expansion=parts[1]+spec[len(parts[0]):]
        if expansion != None:
            spec = expansion
    return spec


def determine_revision_arch(tree, spec=None, check_existence=True, allow_package=False):
    """
    Determines a revision from a revision, patchlevel, package-version, or package.
    Uses the archive to supply missing elements.

    :param tree: The working tree, used to interpret patchlevels
    :type tree: `arch.WorkingTree`
    :param spec: The name specification
    :type spec: string
    """
    name = None
    if spec!=None:
        spec=expand_alias(spec, tree)
        name = arch.NameParser(spec)
    elif tree:
        name = arch.NameParser(tree.tree_version)
    else:
        raise CantDetermineRevision("", "Not using a source tree.")
    if name.has_patchlevel() or name.is_version() or (allow_package and name.is_package()):
        if not name.has_archive():
            archive=arch.default_archive()
            if archive is None:
                raise CantDetermineRevision(spec, "No default archive is set")
            name=arch.NameParser(str(archive)+"/"+str(name))
        if name.has_patchlevel():
            return arch.Revision(name)

        if name.is_version():
            version = arch.Version(name)
            ensure_archive_registered(version.archive)
            try:
                return get_latest_revision_anywhere(version)
            except StopIteration:
                if not arch.Version(name).exists():
                    raise CantDetermineRevision(spec, "The version \""+str(name)+"\" does not exist")
                else:
                    raise CantDetermineRevision(spec, "The version \""+str(name)+"\" has no revisions")

            except Exception, e:
                print e
                raise CantDetermineRevision(spec, 
                    "Error listing revisions for \""+str(name)+"\".")

        elif (allow_package and name.is_package()):
            branch = arch.Branch(name)
            ensure_archive_registered(branch.archive)
            try:
                return get_latest_revision_anywhere(branch)
            except StopIteration:
                if not arch.Branch(name).exists():
                    raise CantDetermineRevision(spec, "The package \""+str(name)+"\" does not exist")
                else:
                    raise CantDetermineRevision(spec, "The package \""+str(name)+"\" has no revisions")

            except Exception, e:
                print e
                raise CantDetermineRevision(spec, 
                    "Error listing revisions for \""+str(name)+"\".")

    else:
        if tree is None:
            raise CantDetermineRevision(spec, "Not in a project tree")
        name=arch.NameParser(str(tree.tree_version)+"--"+spec)
        if not name.has_patchlevel():
            raise CantDetermineRevision(spec,
                "Not a revision, package-version, or patchlevel.")
        return arch.Revision(name)

def determine_revision_tree(tree, spec=None):
    """
    Determines a revision from a revision, patchlevel, or package-version.

    Uses the tree to supply missing elements

    :param tree: The tree to use by default
    :type tree: `arch.ArchSourceTree`
    :param spec: The revision specification
    :type spec: string
    :rtype: `arch.Revision`
    """
    name = None
    if spec!=None:
        spec=expand_alias(spec, tree)
        name = arch.NameParser(spec)
    else:
        name = arch.NameParser(str(tree.tree_version))

    revision = None
    if name.is_version():
        try:
            version = name.get_package_version()
            if name.has_archive():
                version = name.get_archive()+"/"+version
            else:
                version = arch.Version(str(tree.tree_version.archive)
                                       +"/"+version)
            revision = tree.iter_logs(version, True).next().revision
        except StopIteration:
            raise CantDetermineRevision(spec, 
                                      "No revisions present for this version.")
    elif name !=None:
        if not name.has_patchlevel():
            name = arch.NameParser (str(tree.tree_version) +"--"+ name)
        if name.has_patchlevel():
            revision=name.get_nonarch()
            if not name.has_archive():
                name = arch.NameParser (str(tree.tree_version))
            revision = name.get_archive()+"/"+revision
    if revision == None:
        raise CantDetermineRevision(spec, 
                                "Not a revision, package-version, or patchlevel.")
    return arch.Revision(revision)

def determine_version_revision_tree(spec, tree):
    """Get a version or revision from tree and input.  Version preferred.

    :param spec: A revision, version, patchlevel or alias
    :type spec: str
    :param tree: The tree to use for determining revision/version info
    :type tree: `arch.ArchSourceTree`
    :return: The version or revision specified
    :rtype: `arch.Version` or `arch.Revision`
    """
    try:
        return determine_version_tree(spec, tree)
    except errors.CantDetermineVersion, e:
        return determine_revision_tree(tree, spec)


def show_diffs(changeset):
    """
    Print diff output for a changeset.

    :param changeset: the name of the changeset
    :type changeset: string
    """

    for line in pylon.diff_iter2(changeset):
        colorize(line)


def show_custom_diffs(changeset, diffargs, orig, mod):
    """
    Generates and displays alternative diffs for a changeset.
    :param changeset: The (initialized) ChangesetMunger to generate diffs for
    :type changeset: `pylon.ChangesetMunger`
    :param diffargs: The arguments to pass to diff
    :type diffargs: list of str
    :param orig: The path to the ORIG directory
    :type orig: str
    :param mod: The path to the MOD directory
    :type mod: str
    """
    try:
        for entry in changeset.get_entries().itervalues():
            if entry.diff is None:
                continue
            try:
                print pylon.invoke_diff(orig, entry.orig_name, mod,
                                              entry.mod_name,
                                              diffargs).rstrip("\n")
            except pylon.BinaryFiles:
                pass
    except arch.util.ExecProblem, e: 
        print e.proc.error


def colorize (item, suppress_chatter=False):
    """
    Colorizes and prints tla-style output, according to class type.

    Colorizing can be disabled by setting options.colorize to false.

    :param item: The item to display
    :type item: Any tla output object, string
    :param suppress_chatter: If true, no chatter output is displayed.
    :type suppress_chatter: bool
    """
    if options.colorize not in ('yes', 'no', 'maybe'): raise BadOption
    colorize = options.colorize
    if colorize == 'maybe':
        if sys.stdout.isatty() and terminal.has_ansi_colors():
            colorize = 'yes'
        else:
            colorize = 'no'

    if colorize == 'no':
        print item
        return
    if isinstance(item, arch.Chatter):
        if not suppress_chatter:
            print terminal.colorstring(str(item), 'yellow')
            #sys.stdout.write(term_title("Fai: %s" % item.text))
    elif isinstance(item, arch.PatchConflict):
        print terminal.colorstring(str(item), 'white', True, 'red')
    elif isinstance(item, arch.FileAddition):
        print terminal.colorstring(str(item), 'green')  
    elif isinstance(item, arch.FileDeletion):
        print terminal.colorstring(str(item), 'red', True)
    elif isinstance(item, arch.FileModification):
        print terminal.colorstring(str(item), 'magenta', True)
    elif isinstance(item, arch.FilePermissionsChange):
        print terminal.colorstring(str(item), 'magenta')
    elif isinstance(item, arch.FileRename):
        print "=> "+terminal.colorstring(item.oldname, 'red', True)+"\t"+terminal.colorstring(item.name, 'green')
    elif isinstance(item, pylon.DiffFilenames):
        print terminal.colorstring("---"+item.orig, 'red', True)
        print terminal.colorstring("+++"+item.mod, 'green')
    elif isinstance(item, pylon.DiffHunk):
        print terminal.colorstring(str(item), 'blue')  
    elif isinstance(item, pylon.DiffAddLine):
        print terminal.colorstring(str(item), 'green')  
    elif isinstance(item, pylon.DiffRemoveLine):
        print terminal.colorstring(str(item), 'red', True)
    elif isinstance(item, pylon.DiffLine):
        print item
    else:
        print item
    sys.stdout.flush()


def confirm(question, default=False):
    """
    Prompts a user to confirm or deny an action.

    :param question: The question to ask
    :type question: string
    :param default: The default choice
    :type default: boolean
    """

    while True:
        sys.stdout.write(question)
        if default:
            sys.stdout.write(" [Y/n] ")
        else:
            sys.stdout.write(" [y/N] ")
        answer=sys.stdin.readline().strip().lower()
        if answer=="y":
            return True
        elif answer=="n":
            return False
        elif answer=="":
            return default


def prompt(type):
    """
    Prompts the user to decide whether to perform an action

    Prompts can be overridden by setting their option to "always" or "never"
    :param type: the prompt type, as defined in options
    :type type: string
    """
    if options.prompts[type]=="always":
        return True
    elif options.prompts[type]=="never":
        return False
    elif options.prompts[type]=="occasionally":
        return confirm(options.promptquestions[type], False)
    elif options.prompts[type]=="often":
        return confirm(options.promptquestions[type], True)
    else:
        raise BadOption

def get_mirror_source(archive):
    """
    Returns the source of a mirror, if available.  If not available, or
    if the archive is not a mirror, returns None.  We check whether it's a
    mirror second, to reduce network use.

    :param archive: The archive to find the source for
    :type archive: `arch.Archive`
    """

    sourcename=str(archive)+"-SOURCE"
    for arch_entry in arch.iter_archives():
        if str(arch_entry) == sourcename and archive.is_mirror:
            return sourcename 
    return None

def get_best_branch_or_version(branch_version):
    """
    Returns the most authentic Branch or Version available.  If there is a mirror source,
    returns the version for that.  Otherwise, returns the input.

    :param branch_version: The `arch.Version` to retrieve the version of
    :type branch_version: `arch.Version` or `arch.Branch`

    """
    maybe_source = arch.Archive(str(branch_version.archive)+"-SOURCE")
    if not (maybe_source.is_registered() and prompt("Source latest revision")):
        return branch_version
        
    source=get_mirror_source(branch_version.archive)
    if source is None:
        return branch_version
    elif arch.NameParser(branch_version).is_package():
        return arch.Branch(source+"/"+branch_version.nonarch)
    else:
        return arch.Version(source+"/"+branch_version.nonarch)

def get_latest_revision_anywhere(branch_version):
    """
    Returns the latest revision of the branch or version.  If a mirror source is
    available, uses that.  Otherwise, uses the normal archive.  Revisions
    are returned in terms of their official name, not -SOURCE

    :param branch_version: The version to find the latest revision for
    :type branch_version: `arch.Version` or `arch.Branch`
    :rtype: `arch.Revision`
    """
    ensure_archive_registered(branch_version.archive)
    source_branch_version = get_best_branch_or_version(branch_version)
    if not source_branch_version.exists():
        raise StopIteration
    source_revision = source_branch_version.iter_revisions(True).next()
    revision = arch.Revision(str(branch_version.archive) + "/"+ 
        source_revision.nonarch)
    return revision


def update(revision, tree, patch_forward=False):
    """
    Puts the given revision in a tree, preserving uncommitted changes

    :param revision: The revision to get
    :param tree: The tree to update
    """
    command=['update', '--dir', str(tree)]
    if patch_forward:
        command.append('--forward')
    command.append(str(revision))
    return arch.util.exec_safe_iter_stdout('tla', command, expected=(0, 1, 2))


def apply_delta(a_spec, b_spec, tree, ignore_present=False):
    """Apply the difference between two things to tree.

    :param a_spec: The first thing to compare
    :type a_spec: `arch.Revision`, `arch.ArchSourceTree`
    :param b_spec: The first thing to compare
    :type b_spec: `arch.Revision`, `arch.ArchSourceTree`
    :param tree: The tree to apply changes to
    :type tree: `arch.ArchSourceTree`
    :rtype: Iterator of changeset application output 
    """
    tmp=pylon.util.tmpdir(tree)
    changeset=tmp+"/changeset"
    delt=arch.iter_delta(a_spec, b_spec, changeset)
    for line in delt:
        yield line
    yield arch.Chatter("* applying changeset")
    for line in delt.changeset.iter_apply(tree, ignore_present):
        yield line
    shutil.rmtree(tmp)


def iter_apply_delta_filter(iter):
    show_file_changes=False
    for line in iter:
        if pylon.chattermatch(line, "applying changeset"):
            show_file_changes=True
        if pylon.chattermatch(line, "changeset:"):
            pass
        elif not show_file_changes and isinstance(line, arch.MergeOutcome):
            pass
        else:
            yield line


def find_editor():
    """
    Finds an editor to use.  Uses the EDITOR environment variable.

    :rtype: string
    """
    if os.environ.has_key("EDITOR") and os.environ["EDITOR"] != "":
        return os.environ["EDITOR"]
    else:
        raise NoEditorSpecified


def invoke_editor(filename):
    """
    Invokes user-specified editor.

    :param filename: The file to edit
    :type filename: string
    """
    os.system("%s %s" % (find_editor(), filename))

def iter_reverse(iter):
    """Produce brute-force reverse iterator.

    :param iter: The iterator to run through in reverse
    :type iter: Iterator of any
    :rtype: reversed list of any
    """
    reversed = list(iter)
    reversed.reverse()
    return reversed

def iter_cacherevs(version, reverse=False):
    """Iterates through cached revisions as revisions, not patchlevels"""
    iter =  version.iter_cachedrevs()
    if not reverse:
        return iter
    else:
        return iter_reverse(iter)

def iter_present(tree, version, reverse=False):
    """The inverse of skip-present.  Iterates through revisions that are
    missing, but merge revisions already present in the tree.
    :param tree: The tree to look for patchlogs in
    :type tree: `arch.ArchSourceTree`
    :param version: The version of patchlogs to look for
    :type version: `arch.Version`
    :param reverse: If true, go in reverse
    :type reverse: bool
    """
    miss = pylon.iter_missing(tree, version, reverse)
    miss_skip = pylon.iter_missing(tree, version, reverse, skip_present=True)
    for skip_revision in miss_skip:
        for miss_revision in miss:
            if miss_revision == skip_revision:
                break
            yield miss_revision
    for miss_revision in miss:
        yield miss_revision


def iter_partner_missing(tree, reverse=False, skip_present=False):
    """Generate an iterator of all missing partner revisions.

    :param tree: The tree that may be missing revisions from partners
    :type tree: `arch.ArchSourceTree`
    :param reverse: If true, perform iteration in reverse
    :type reverse: bool
    :return: Iterator of missing partner revisions
    :rtype: Iterator of `arch.Revision`
    """
    pylon.valid_tree_root(tree)
    for version in pylon.iter_partners(tree, tree.tree_version):
        ensure_archive_registered(version.archive)
        for revision in pylon.iter_missing(tree, version, reverse, 
                                           skip_present):
            yield revision

def iter_skip(iter, skips=0):
    """Ajusts an iteration list by skipping items at beginning or end.  May
    modify original iterator.

    :param iter: The base iterator to use.
    :type iter: Iterator of anything
    :param skips: Positive numbers skip from the beginning.  Negative numbers
        skip from the end.
    :type skips: int
    :return: an iterator that starts or stops at a different place
    :rtype: iterator of iter's iter type
    """
    if skips == 0:
        return iter;
    if skips > 0:
        for q in range(skips):
            iter.next()
        return iter
    if skips < 0:
        return iter_skip_end(iter, -skips)

def iter_skip_end(iter, skips):
    """Generates an iterator based on iter that skips the last skips items

    :param skips: number of items to skip from the end.
    :type skips: int
    :return: an iterator that stops at a different place
    :rtype: iterator of iter's iteration type
    """
    iter = iter_skip(iter, -skips + 1)
    item = None
    for it in iter:
        if item is not None:
            yield item
        item = it

def modified_iter(modified, tree, originator=False):
    sections = modified.split(':')
    revision = tree.tree_revision
    if len(sections) == 1:
        return pylon.iter_changedfile(tree, revision, sections[0])
    elif len(sections) == 2 and originator:
        return pylon.iter_changed_file_line_orig(tree, revision, 
                                                         sections[0], 
                                                         int(sections[1]))
    elif len(sections) == 2:
        return pylon.iter_changed_file_line(tree, revision, 
                                                    sections[0], 
                                                    int(sections[1]))


def iter_combine(iterators):
    """Generate an iterator that iterates through the values in several.
    :param iterators: The iterators to get values from
    :type iterators: List of iterator
    :rtype: iterator of input iterators' value type
    """
    for iterator in iterators:
        for value in iterator:
            yield value


def is_tla_command(command):
    """Determines whether the specified string is a tla command.
    
    :param command: The string to check
    :type command: str
    :rtype: bool"""
    for line in pylon.iter_tla_commands():
        if (line == command):
            return True
    return False



class BadCommandOption:
    def __init__(self, message):
        self.text = message

    def __str__(self):
        return self.text

def raise_get_help(option, opt, value, parser):
    raise GetHelp


class CmdOptionParser(optparse.OptionParser):
    def __init__(self, usage):
        optparse.OptionParser.__init__(self, usage)
        self.remove_option("-h")
        self.add_option("-h", "--help", action="callback", 
                        callback=raise_get_help, help="Print a help message")

    def error(self, message):
        raise BadCommandOption(message)

    def iter_options(self):
        return iter_combine([self._short_opt.iterkeys(), 
                            self._long_opt.iterkeys()])


def add_tagline_or_explicit_id(file, tltl=False, implicit=False):
    """This is basically a wrapper for add-tagline

    :param file: The file to add an id for
    :type file: str
    :param tltl: Use lord-style tagline
    :type tltl: bool
    """
    opt = optparse.Values()
    opt.__dict__["tltl"] = tltl
    opt.__dict__["id"] = None
    opt.__dict__["implicit"] = implicit

    if not os.path.isfile(file) or os.path.islink(file):
        pylon.add_id([file])
        return 
    try:
        add_tagline.tagFile(file, opt)
    except add_tagline.NoCommentSyntax:
        print "Can't use tagline for \"%s\" because no comment sytax known." % \
            file
        if prompt("Fallthrough to explicit"):
            pylon.add_id([file])
        else:
            raise

browse_memo = {}

def iter_browse_memo(search):
    """Memoized version of iter_browse.
    :param search: The string to find entries for
    :type search: str
    :return: Any categories, branches, versions that matched
    :rtype: iterator of `arch.Category`, `arch.Branch`, `arch.Version`, \
 `arch.Revision`
    """
    global browse_memo
    if arch.NameParser.is_archive_name(search):
        archive = search
    else:
        name = arch.NameParser(search)
        archive = name.get_archive()
    if not browse_memo.has_key(str(archive)):
        browse_memo[str(archive)] = list(pylon.iter_browse(archive))

    for entry in browse_memo[str(archive)]:
        if str(entry).startswith(search):
            yield entry


def iter_revision_completions(arg, tree):
    if arg.count('/') == 0:
        for completion in arch.iter_archives():
            yield str(completion)+"/"
        for completion in pylon.iter_all_alias(tree):
            yield completion[0]

        default_archive = str(arch.default_archive())
        if default_archive is not None:
            default_archive_arg = default_archive+'/'+arg
            for completion in iter_browse_memo(default_archive_arg):
                completion = str(completion)
                if completion.startswith(default_archive_arg) and \
                    completion != default_archive_arg:
                    yield completion[len(default_archive)+1:]

    else:
        if arg.endswith('/'):
            arg = arg[:-1]
        try:
            expansion = expand_alias(arg, tree)
            for completion in iter_browse_memo(expansion):
                completion = str(completion)
                if completion.startswith(expansion) and completion != expansion:
                    yield arg + completion[len(expansion):]
        except Exception, e:
            print e


def ensure_archive_registered(archive, location=None):
    if archive.is_registered():
        return
    user_supplied = False
    if location is None:
        colorize (arch.Chatter("* Looking up archive %s" % archive))
        home = None
        mirror = None
        
        try:
            colorize (arch.Chatter("* Checking bonehunter.rulez.org"))
            home = pylon.bh_lookup(archive)
        except ArchiveLookupError, e:
            print e

        if home is not None:
            print "Found location %s" % home
            if prompt("Use lookup location"):
                location = home
        if location is None:
            try:
                colorize (arch.Chatter("* Checking sourcecontrol.net"))
                (home, mirror) = pylon.sc_lookup(archive)
                if home is not None:
                    print "Found location %s" % home
                    if prompt("Use lookup location"):
                        location = home
                if location is None and mirror is not None:
                    print "Found mirror location %s" % mirror 
                    if prompt("Use lookup mirror location"):
                        location = mirror
            except ArchiveLookupError, e:
                print e
        if location is None:
            print "If you know the location for %s, please enter it here." % \
                archive
            location = raw_input("Fai> ")
            if location == "":
                location = None
            else:
                user_supplied = True
                
        if location is None:
            raise arch.errors.ArchiveNotRegistered(archive)

    print "Registering archive \""+str(archive)+"\""
    if location_cacheable(location) and prompt("Register cached"):
        cached_location="cached:"+location
        ar = register_archive(str(archive), cached_location, str(archive))
    elif prompt ("Make local mirror"):
        ar = arch.Archive(str(archive)+"-SOURCE")
        if not ar.is_registered():
            ar = register_archive(str(archive)+"-SOURCE", location, str(archive))
        ar.make_mirror(str(archive), 
            os.path.expanduser(options.mirror_path)+"/"+str(archive))

    else:
        ar = register_archive(str(archive), location, str(archive))
    if user_supplied and prompt("Submit user-supplied"):
        pylon.bh_submit(archive, location)

pylon.ensure_archive_registered = ensure_archive_registered

def location_cacheable(location):
    return not location.startswith('/') and not location.startswith('cached:')\
        and pylon.ArchParams().exists('=arch-cache')    

def ensure_revision_exists(revision):
    ensure_archive_registered(revision.archive)
    if revision.exists():
        return
    source = get_mirror_source(revision.archive)
    if source is not None:
        if arch.Revision(str(source)+"/"+revision.nonarch).exists():
            cmd = pylon.mirror_archive(str(source), str(revision.archive),
                                 str(revision.version.nonarch))
            for line in arch.classify_chatter(cmd):
                colorize(line)
            return
    raise NoSuchRevision(revision)

pylon.ensure_revision_exists = ensure_revision_exists


def merge_ancestor(mine, other, other_start):
    """Determines an ancestor suitable for merging, according to the tree logs.

    :param mine: Tree to find an ancestor for (usually the working tree)
    :type mine: `arch.WorkingTree`
    :param other: Tree that merged or was merged by mine
    :type other: `arch.ArchSourceTree`
    :param other_start: The start revision to use for the other tree
    :type other_start: `arch.Revision`
    :return: Log of the merged revision
    :rtype: `arch.Patchlog`
    """
    my_ancestors = list (pylon.iter_ancestry_logs(mine))
    other_ancestors = list (pylon.iter_ancestry_logs(other, 
                                                             other_start))
    
    other_ancestor = None
    mine_ancestor = None
    
    for log in my_ancestors:
        for oth_log in other_ancestors:
            if log.continuation_of:
                if oth_log.revision == log.continuation_of:
                    mine_ancestor = oth_log
                    break
            elif oth_log.revision in log.new_patches:
                mine_ancestor = oth_log
                break
        if mine_ancestor is not None:
            break        

    for log in other_ancestors:
        if log == mine_ancestor:
            break
        for my_log in my_ancestors:
            if log.continuation_of:
                if my_log.revision == my_log:
                    other_ancestor = my_log
                    break
            elif my_log.revision in log.new_patches:
                other_ancestor = my_log 
                break
        if other_ancestor is not None:
            break

    if other_ancestor is not None:
        return other_ancestor
    else:
        return mine_ancestor




def iter_supported_switches(cmd):
    for line in pylon.iter_tla_cmd_help(cmd):
        if not line.startswith("  -"):
            continue
        chunks = line[2:].split()
        if chunks[0][-1] == ",":
            yield chunks[0][:-1]
            yield chunks[1]
        else:
            yield chunks[0]

def supports_switch(cmd, switch):
    return switch in list(iter_supported_switches(cmd))

        
def register_archive(name, location, expected_name=None):
    """Wrapper that can require a certain official_name for the location
    :param name: The name of the archive to register
    :param location: The location where a version is stored
    :param expected_name: If supplied, the location's expected official_name
    """
    ar = arch.register_archive(name, location)
    if expected_name is not None:
        arname = ar.official_name
        if arname!=expected_name:
            print "The archive at this location is wrong: %s" %\
                arname
            if not prompt("Register wrong name"):
                ar.unregister()
                raise WrongArchName(location, expected_name, arname)
    return ar

       
def direct_merges(merges):
    """Get a list of direct merges, from a list of direct and indirect
    
    :param merges: Iterator of merge patchlogs
    :type merges: iter of `arch.Patchlog`
    :return: The direct merges
    :rtype: list of `arch.Patchlog`
    """
    indirect = []
    direct = []
    logs = list(merges)
    if not logs:
        return []
    for log in logs:
        indirect.extend([f for f in log.new_patches if f != log.revision])
        if log.continuation_of is not None:
            # continuations list everything in new_patches
            continue
        try:
            # ask baz if we can
            ancestor = log.revision.ancestor
        except pybaz.errors.ExecProblem:
            # baz does not know
            # guess
            ancestor = namespace_previous(log.revision)
        if ancestor is not None:
            indirect.append(ancestor)
    return [log for log in logs if not log.revision in indirect]

def namespace_previous(revision):
    if revision.patchlevel == 'base-0':
        return None
    if revision.patchlevel == 'patch-1':
        return revision.version['base-0']
    if revision.patchlevel.startswith('patch'):
        level = int(revision.patchlevel[len('patch-'):]) -1
        return revision.version['patch-%d' % level]
    if revision.patchlevel == 'version-0':
        raise RuntimeError("cannot determine prior namespace level for a "
                           "version-0 patch")
    if revision.patchlevel == 'version-1':
        return revision.version['version-0']
    if revision.patchlevel.startswith('version'):
        level = int(revision.patchlevel[len('version-'):]) -1
        return revision.version['version-%d' % level]
    raise NotImplementedError

def require_version_exists(version, spec):
    if not version.exists():
        raise errors.CantDetermineVersion(spec, 
                                          "The version %s does not exist." \
                                          % version)


def revision_iterator(tree, type, args, reverse, modified, shallow):
    """Produce an iterator of revisions

    :param tree: The project tree or None
    :type tree: `arch.ArchSourceTree`
    :param type: The type of iterator to produce
    :type type: str
    :param args: The commandline positional arguments
    :type args: list of str
    :param reverse: If true, iterate in reverse
    :type reverse: bool
    :param modified: if non-null, use to create a file modification iterator
    :type modified: str
    """
    if len(args) > 0:
        spec = args[0]
    else:
        spec = None
    if modified is not None:
        iter = modified_iter(modified, tree, shallow != True)
        if reverse:
            return iter
        else:
            return iter_reverse(iter)
    elif type == "archive":
        if spec is None:
            if tree is None:
                raise CantDetermineRevision("", "Not in a project tree")
            version = determine_version_tree(spec, tree)
        else:
            version = determine_version_arch(spec, tree)
            ensure_archive_registered(version.archive)
            require_version_exists(version, spec)
        return version.iter_revisions(reverse)
    elif type == "cacherevs":
        if spec is None:
            if tree is None:
                raise CantDetermineRevision("", "Not in a project tree")
            version = determine_version_tree(spec, tree)
        else:
            version = determine_version_arch(spec, tree)
            ensure_archive_registered(version.archive)
            require_version_exists(version, spec)
        return iter_cacherevs(version, reverse)
    elif type == "library":
        if spec is None:
            if tree is None:
                raise CantDetermineRevision("", 
                                                    "Not in a project tree")
            version = determine_version_tree(spec, tree)
        else:
            version = determine_version_arch(spec, tree)
        return version.iter_library_revisions(reverse)
    elif type == "logs":
        if tree is None:
            raise CantDetermineRevision("", "Not in a project tree")
        return tree.iter_logs(determine_version_tree(spec, \
                              tree), reverse)
    elif type == "missing" or type == "skip-present":
        if tree is None:
            raise CantDetermineRevision("", "Not in a project tree")
        skip = (type == "skip-present")
        version = determine_version_tree(spec, tree)
        ensure_archive_registered(version.archive)
        require_version_exists(version, spec)
        return pylon.iter_missing(tree, version, reverse, skip_present=skip)

    elif type == "present":
        if tree is None:
            raise CantDetermineRevision("", "Not in a project tree")
        version = determine_version_tree(spec, tree)
        ensure_archive_registered(version.archive)
        require_version_exists(version, spec)
        return iter_present(tree, version, reverse)

    elif type == "new-merges" or type == "direct-merges":
        if tree is None:
            raise CantDetermineRevision("", "Not in a project tree")
        version = determine_version_tree(spec, tree)
        ensure_archive_registered(version.archive)
        require_version_exists(version, spec)
        iter = pylon.iter_new_merges(tree, version, reverse)
        if type == "new-merges":
            return iter
        elif type == "direct-merges":
            return pylon.direct_merges(iter)

    elif type == "missing-from":
        if tree is None:
            raise CantDetermineRevision("", "Not in a project tree")
        revision = determine_revision_tree(tree, spec)
        libtree = pylon.find_or_make_local_revision(revision)
        return pylon.iter_missing(libtree, tree.tree_version, reverse)

    elif type == "partner-missing":
        return iter_partner_missing(tree, reverse)

    elif type == "ancestry":
        revision = determine_revision_tree(tree, spec)
        iter = pylon.iter_any_ancestry(tree, revision)
        if reverse:
            return iter
        else:
            return iter_reverse(iter)

    elif type == "archive-ancestry":
        revision = determine_revision_tree(tree, spec)
        iter = revision.iter_ancestors(metoo=True)
        if reverse:
            return iter
        else:
            return iter_reverse(iter)

    elif type == "dependencies" or type == "non-dependencies" or \
        type == "with-deps":
        nondeps = (type == "non-dependencies")
        revision = determine_revision_tree(tree, spec)
        anc_tree = pylon.find_or_make_local_revision(revision)
        anc_iter = pylon.iter_any_ancestry(anc_tree, revision)
        iter = pylon.iter_depends(anc_iter, nondeps)
        if type == "with-deps":
            iter = iter_combine(((revision,),
                                 pylon.iter_to_present(iter, tree)))
        if reverse:
            return iter
        else:
            return iter_reverse(iter)

    elif type == "micro":
        return pylon.iter_micro(tree)

    elif type == "added-log":
        revision = determine_revision_tree(tree, spec)
        iter = pylon.iter_ancestry_logs(tree)
        return pylon.iter_added_log(iter, revision)

    elif type == "skip-conflicts":
        #this only works for replay
        if reverse:
            raise Exception("Skip-conflicts doesn't work in reverse.")
        version = determine_version_tree(spec, tree)
        return pylon.iter_skip_replay_conflicts(tree, version)

def add_revision_iter_options(select):
        select.add_option("", "--archive", action="store_const", 
                          const="archive", dest="type", default="default",
                          help="List all revisions in the archive")
        select.add_option("", "--cacherevs", action="store_const", 
                          const="cacherevs", dest="type",
                          help="List all revisions stored in the archive as "
                          "complete copies")
        select.add_option("", "--logs", action="store_const", 
                          const="logs", dest="type",
                          help="List revisions that have a patchlog in the "
                          "tree")
        select.add_option("", "--missing", action="store_const", 
                          const="missing", dest="type",
                          help="List revisions from the specified version that"
                          " have no patchlog in the tree")
        select.add_option("", "--skip-present", action="store_const", 
                          const="skip-present", dest="type",
                          help="List revisions from the specified version that"
                          " have no patchlogs at all in the tree")
        select.add_option("", "--present", action="store_const", 
                          const="present", dest="type",
                          help="List revisions from the specified version that"
                          " have no patchlog in the tree, but can't be merged")
        select.add_option("", "--missing-from", action="store_const", 
                          const="missing-from", dest="type",
                          help="List revisions from the specified revision "
                          "that have no patchlog for the tree version")
        select.add_option("", "--partner-missing", action="store_const", 
                          const="partner-missing", dest="type",
                          help="List revisions in partner versions that are"
                          " missing")
        select.add_option("", "--new-merges", action="store_const", 
                          const="new-merges", dest="type",
                          help="List revisions that have had patchlogs added"
                          " to the tree since the last commit")
        select.add_option("", "--direct-merges", action="store_const", 
                          const="direct-merges", dest="type",
                          help="List revisions that have been directly added"
                          " to tree since the last commit ")
        select.add_option("", "--library", action="store_const", 
                          const="library", dest="type",
                          help="List revisions in the revision library")
        select.add_option("", "--archive-ancestry", action="store_const", 
                          const="archive-ancestry", dest="type",
                          help="List revisions that are ancestors of the "
                          "current tree version")
        select.add_option("", "--ancestry", action="store_const", 
                          const="ancestry", dest="type",
                          help="List revisions that are ancestors of the "
                          "current tree version")

        select.add_option("", "--dependencies", action="store_const", 
                          const="dependencies", dest="type",
                          help="List revisions that the given revision "
                          "depends on")

        select.add_option("", "--non-dependencies", action="store_const", 
                          const="non-dependencies", dest="type",
                          help="List revisions that the given revision "
                          "does not depend on")

        select.add_option("", "--with-deps", action="store_const", 
                          const="with-deps", dest="type",
                          help="The specified revision, plus its dependencies"
                          "since the last present dependency")

        select.add_option("--micro", action="store_const", 
                          const="micro", dest="type",
                          help="List partner revisions aimed for this "
                          "micro-branch")

        select.add_option("", "--modified", dest="modified", 
                          help="List tree ancestor revisions that modified a "
                          "given file", metavar="FILE[:LINE]")

        select.add_option("", "--shallow", dest="shallow", action="store_true",
                          help="Don't scan merges when looking for revisions"
                          " that modified a line")
        select.add_option("--added-log", action="store_const", 
                          const="added-log", dest="type",
                          help="List revisions that added this log")

def log_for_merge2(tree, version):
    merges = list(pylon.iter_new_merges(tree, version))
    direct = direct_merges (merges)
    out = "Patches applied:\n\n"
    for log in direct:
        out +=merge_log_recurse(log, merges, tree)
    return out

def merge_log_recurse(log, interesting_merges, tree, indent = None):
    """A variation on log-for-merge that scaled extremely badly"""
    if indent is None:
        indent = ""
    out = "%s * %s\n%s   %s\n\n" % (indent, log.revision, indent, log.summary)
    new_patch_logs = [f for f in interesting_merges if (f.revision in
                      log.new_patches and f != log) ]
    direct = direct_merges(new_patch_logs)
    for merge in direct:
        out+=merge_log_recurse(merge, new_patch_logs, tree, indent+"  ")
    return out

def chatter(str):
    colorize(arch.Chatter("* "+str))


def user_hunk_confirm(hunk):
    """Asks user to confirm each hunk
    
    :param hunk: a diff hunk
    :type hunk: str
    :return: The user's choice
    :rtype: bool
    """
    for line in pylon.diff_classifier(hunk.split('\n')):
        colorize(line)
    return confirm("Revert this hunk?", False)

def merge_completions(tree, arg, index):
    completions = list(pylon.iter_partners(tree, tree.tree_version))
    if len(completions) == 0:
        completions = list(tree.iter_log_versions())

    aliases = []
    try:
        for completion in completions:
            alias = pylon.compact_alias(str(completion), tree)
            if alias:
                aliases.extend(alias)

        for completion in completions:
            if completion.archive == tree.tree_version.archive:
                aliases.append(completion.nonarch)

    except Exception, e:
        print e
        
    completions.extend(aliases)
    return completions


def iter_file_completions(arg, only_dirs = False):
    """Generate an iterator that iterates through filename completions.

    :param arg: The filename fragment to match
    :type arg: str
    :param only_dirs: If true, match only directories
    :type only_dirs: bool
    """
    cwd = os.getcwd()
    if cwd != "/":
        extras = [".", ".."]
    else:
        extras = []
    (dir, file) = os.path.split(arg)
    if dir != "":
        listingdir = os.path.expanduser(dir)
    else:
        listingdir = cwd
    for file in iter_combine([os.listdir(listingdir), extras]):
        if dir != "":
            userfile = dir+'/'+file
        else:
            userfile = file
        if userfile.startswith(arg):
            if os.path.isdir(listingdir+'/'+file):
                userfile+='/'
                yield userfile
            elif not only_dirs:
                yield userfile


def iter_dir_completions(arg):
    """Generate an iterator that iterates through directory name completions.

    :param arg: The directory name fragment to match
    :type arg: str
    """
    return iter_file_completions(arg, True)


def iter_munged_completions(iter, arg, text):
    for completion in iter:
        completion = str(completion)
        if completion.startswith(arg):
            yield completion[len(arg)-len(text):]

# arch-tag: 7d28710a-0b02-4dcb-86a7-e6b3b5486da4