~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
# 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 os
import shutil
import re
import tempfile

import util
import pybaz
import arch_core
import native
import errors
from arch_core import chattermatch
import misc
import patches

__docformat__ = "restructuredtext"
__doc__ = "Compound functionality built on lower-level commands"

def iter_any_ancestry(tree, startrev):
    return _iter_ancestry(tree, startrev)

def _iter_ancestry(tree, startrev):
    """Iterate over ancestry according to tree's patchlogs.

    Generator that yields `arch.Patchlog` or `arch.Revision` instances
    according to `tree` ancestry. Each item returned is the direct ancestor of
    the previous `arch.Revision`. If `startrev` is supplied, it is the first
    item yielded. The iteration stops after the first ancestor named in a
    patchlog that does not, itself, have a patchlog in `tree`, or at the
    beginning.

    :param tree: tree to determine the ancestry of
    :type tree: `arch.ArchSourceTree`
    :param startrev: revision to start at. Must be in the ancestry of `tree`.
    :type startrev: `arch.Revision`
    :rtype: iterator of `arch.Patchlog` or `arch.Revision`
    """
    log = None
    while startrev is not None:
        nextrev = None
        skip = True
        for log in tree.iter_logs(startrev.version, reverse=True):
            if skip and log.revision == startrev:
                skip = False
            if skip:
                continue
            yield log
            if log.continuation_of:
                nextrev = log.continuation_of # sure it should not be startrev?
# pretty sure.  if we did that, startrev would never be None
                break
        startrev = nextrev
    if log and log.continuation_of:
        yield log.continuation_of


def iter_ancestry(tree, startrev=None):
    """Iterate over ancestry according to tree's patchlogs.

    Generator that yields `arch.Revision` instances according to `tree`
    ancestry.  Each `arch.Revision` returned is the direct ancestor of the
    previous `arch.Revision`.  If `startrev` is supplied, it is the first item
    yielded. The iteration stops after the first ancestor named in a patchlog
    that does not, itself, have a patchlog in `tree`, or at the beginning.

    :param tree: tree to determine the ancestry of.
    :type tree: `arch.ArchSourceTree`
    :param startrev: optional revision to start at. Must be in the ancestry of
        `tree`. The latest revision is used if not supplied.
    :type startrev: `arch.Revision`
    :rtype: iterator of `arch.Revision`
    """
    if startrev is None:
        startrev = tree.tree_revision
    for r in _iter_ancestry(tree, startrev):
        if not isinstance(r, pybaz.Revision):
            r = r.revision
        yield r


def iter_ancestry_logs(tree, startrev=None):
    """Iterate over ancestry according to tree's patchlogs.

    Generator that yields `arch.Patchlog` instances according to `tree`
    ancestry.  Each `arch.Patchlog` returned is from the direct ancestor of the
    revision of the previous `arch.Patchlog`.  If `startrev` is supplied, it is
    the first item yielded. The iteration stops after the first ancestor named
    in a patchlog that does not, itself, have a patchlog in `tree`, or at the
    beginning.

    :param tree: tree to determine the ancestry of.
    :type tree: `arch.ArchSourceTree`
    :param startrev: optional revision to start at. Must be in the ancestry of
        `tree`. The latest revision is used if not supplied.
    :type startrev: `arch.Revision`
    :rtype: iterator of `arch.Patchlog`
    """
    if startrev is None:
        startrev = tree.tree_revision
    for r in _iter_ancestry(tree, startrev):
        if not isinstance(r, pybaz.Patchlog):
            r = pybaz.Patchlog(r)
        yield r


def get_entry_name(entry):
    """Returns a filename for the entry, preferring the MOD name

    :param entry: The entry to get a filename for
    :type entry: `ChangesetEntry`
    :return: The found file name
    :rtype: str
    """
    if entry.mod_name is not None:
        return entry.mod_name
    else:
        return entry.orig_name

def iter_changes(changeset_munger):
    entries = changeset_munger.get_entries()
    sorted_entries = list(entries.itervalues())
    sorted_entries.sort(util.cmp_func(get_entry_name))

    for entry in sorted_entries:
        if entry.orig_name and not entry.mod_name:
            if entry.orig_type == "dir":
                suf="/ "
            else:
                suf="  "
            yield pybaz.FileDeletion("D%s%s" % (suf, entry.orig_name[2:]), ' ')

    for entry in sorted_entries:
        if entry.mod_name and not entry.orig_name:
            if entry.mod_type == "dir":
                suf="/ "
            else:
                suf="  "
            yield pybaz.FileAddition("A%s%s" % (suf, entry.mod_name[2:]), ' ')

    for entry in sorted_entries:
        if entry.orig_name != entry.mod_name and entry.orig_name and \
            entry.mod_name:
            yield pybaz.FileRename("=> %s\t%s" % (entry.orig_name[2:], 
                                                 entry.mod_name[2:]), ' ')
        if entry.diff:
            yield pybaz.FileModification("M  %s" % entry.mod_name[2:], ' ')

        if entry.original or entry.modified:
            yield pybaz.FileModification("Mb %s" % entry.mod_name[2:], ' ')

        if entry.orig_perms or entry.mod_perms:
            if entry.mod_type == "file":
                yield pybaz.FilePermissionsChange("-- %s" % entry.mod_name[2:],
                                                 ' ')
            elif entry.mod_type == "dir":
                yield pybaz.FilePermissionsChange("-/ %s" % entry.mod_name[2:],
                                                 ' ')

def diff_iter2(changeset, 
               exclude_str='(^\./{arch\}|/(\.arch-ids|\.arch-inventory))'):
    """Native diff iterator

    :param changeset: The changeset to iterate through the diffs of
    :type changeset: str
    """
    munger = native.ChangesetMunger(changeset)
    munger.read_indices()
    sorted_entries = list(munger.get_entries().itervalues())
    sorted_entries.sort(util.cmp_func(get_entry_name))
    if exclude_str is not None:
        exclude = re.compile(exclude_str)
    else:
        exclude = None
    for entry in sorted_entries:
        if exclude is not None and munger.entry_matches_pattern(entry, exclude):
            continue
        if entry.diff is not None:
            lines = [f.rstrip('\n') for f in open(entry.diff)]
        elif entry.mod_name is None or entry.orig_name is None:
            if entry.removed_file is None and entry.new_file is None:
                continue
            try:
                diff = native.invoke_diff(changeset+"/removed-files-archive",
                                          entry.orig_name,
                                          changeset+"/new-files-archive",
                                          entry.mod_name)
                lines = diff.split('\n')
            except native.BinaryFiles:
                continue
        else:
            continue
        for line in native.diff_classifier(lines):
            yield line


def replay(tree, revision, linehandler=None, reverse=False, munge_opts=None):
    """Applies the changeset for a revision to the tree

    :param tree: The tree to apply changes for
    :type tree: `arch.WorkingTree`
    :param revision: The revision to get the changes for
    :type revision: `arch.Revision`
    :param reverse: Apply the changeset in reverse
    :type reverse: bool
    :param munge_opts: options for munging the changeset before applying
    :type munge_opts: `MungeOpts`
    """
    #revision = NativeRevision(str(revision))
    my_dir = util.tmpdir(os.getcwd())
    try:
        changeset = revision.get_patch(my_dir)
        munger = native.ChangesetMunger(changeset)
        munger.read_indices()
        if munge_opts is not None:
            munger.munge(munge_opts)
#        entry_handler = PatchEntryHandler(tree)
        entry_handler = None
        iterator = iter_custom_apply_changeset(tree, munger, entry_handler, reverse=reverse)
        for line in iterator:
            if linehandler is not None:
                linehandler(line)
    finally:
        if os.access(my_dir, os.R_OK):
            shutil.rmtree(my_dir)
    try:
        if iterator.conflicting:
            return 1
        else:
            return 0
    except:
        return 2


def revert(tree, revision, munge_opts=None, keep=True):
    """Apply the difference between two things to tree.

    :param tree: The tree to apply changes to
    :type tree: `arch.ArchSourceTree`
    :param revision: The revision to apply the changes to
    :type revision: `arch.Revision`
    :param munge_opts: If supplied, a set of parameters for munging the changeset
    :type munge_opts: `MungeOpts`
    :param keep: If true, keep the generated changeset
    :type keep: bool
    :rtype: Iterator of changeset application output 
    """
    changeset = pybaz.util.new_numbered_name(tree, ',,undo-')
    delt=pybaz.iter_delta(revision, tree, changeset)
    for line in delt:
        if not isinstance(line, pybaz.TreeChange) and not chattermatch(line, "changeset:"):
            yield line
    if munge_opts is not None:
        yield pybaz.Chatter("* munging changeset")
        munger = native.ChangesetMunger(delt.changeset)
        entries = munger.munge(munge_opts)
        if entries == 0 and munge_opts.keep_pattern is not None:
            print "No entries matched pattern %s" % munge_opts.keep_pattern
    yield pybaz.Chatter("* applying changeset")
    for line in delt.changeset.iter_apply(tree, reverse=True):
        yield line
    if not keep:
        shutil.rmtree(changeset)


def iter_added_log(logs, revision):
    """Generate an iterator of revisions that added a certain revision log.

    :param logs: An iterator of patchlogs to filter
    :type logs: iter of `arch.Patchlog`
    :param revision: The revision of the log to look for
    :type revision: `arch.Revision`
    :rtype: iter of `arch.Revision`
    """
    for log in logs:
        if revision in log.new_patches:
            yield log


def merge_ancestor2(mine, other, other_revision):
    """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_revision: Override for broken library revisions
    :type other_revision: `arch.Revision`
    :return: Log of the merged revision
    :rtype: `arch.Patchlog`
    """
    # find latest ancestor of opposite tree in each tree
    my_revision=tree_latest(mine)
    my_merged=last_merge_of(other, iter_ancestry(mine, my_revision))
    other_merged=last_merge_of(mine, iter_ancestry(other, other_revision))
    if my_merged is None:
        if other_merged is None:
            raise errors.UnrelatedTrees(mine, other)
        return other_merged
    if other_merged is None:
        return my_merged

    # determine which merge happend later in sequence for each tree
    other_latest = latest_merge(other, other_revision, other_merged, my_merged)
    my_latest = latest_merge(mine, my_revision, my_merged, other_merged)
    if other_latest != my_latest:
        #probably paralel merge
        raise errors.NoLatestRevision(my_latest, other_latest)

    return my_latest


def last_merge_of(into, ancestry):
    """Determine last log from the ancestry of "merged" in "into".

    :param into: The tree that a log was merged into
    :type into: `arch.ArchSourceTree`
    :param ancestry: An interator of the tree's ancestry
    :type ancestry: iter of `arch.Patchlog`
    :return: The last merged log, or None if none found
    :rtype: `arch.Revision`
    """
    log_list = None
    log_version = None

    for ancestor in ancestry:
        if ancestor.version != log_version:
            log_version = ancestor.version
            log_list = list([f.revision for f in into.iter_logs(log_version)])
        if ancestor in log_list:
            return ancestor
    return None


def latest_merge(tree, tree_revision, tree_merged, other_merged):
    """Determines which of two revisions was merged most recently into tree
    :param tree: The tree the revisions were merged into
    :type tree: `arch.ArchSourceTree`
    :param tree_merged: The revision from tree that was merged
    :type tree_merged: `arch.Revision`
    :param other_merged: The revision from OTHER that was merged
    :type other_merged: `arch.Revision`
    :return: The revision merged most recently, according to this tree
    :rtype: `arch.Revision`
    """

    for log in iter_ancestry_logs(tree, tree_revision):
        if log.revision == tree_merged:
            return tree_merged
        # skip logs that are continuations, because of bogus new-patches
        if log.continuation_of:
            continue
        if other_merged in log.new_patches:
            return other_merged
    return None


def iter_vchange(iterator):
    """First revision of each sequence from a different package-version.

    Generator that yields only those revisions in different
    package-versions than the previous revision.

    :type iterator: iterator of `arch.Revision`
    :rtype: iterator of `arch.Revision`
    """
    oldversion = None
    for item in iterator:
        if oldversion is not None and item.version != oldversion:
            yield item
        oldversion = item.version


def tag_source(tree):
    """Return the revision that the current tree thinks it was tagged from.

    :type tree: `arch.ArchSourceTree`
    :rtype: `arch.Revision`
    """
    try:
        return iter_vchange(iter_ancestry(tree, None)).next()
    except StopIteration:
        return None


def ensure_revision_exists(revision):
    pass


def ensure_archive_registered(archive, location=None):
    """Throw an exception if the archive isn't registered.
    This function is intended to be overridden.

    :param archive: The archive to check
    :type archive: `arch.Archive`
    :param location: A possible location of the archive
    :type location: str
    """
    if archive.is_registered():
        return
    else:
        raise pybaz.errors.ArchiveNotRegistered(archive)


def tree_file_path(tree, path):
    """Convert a relative or absolute path into a tree-relative path

    :param tree: The tree the path falls in
    :param path: The path the tree falls in
    """
    dir, file = os.path.split(path)
    if dir == "":
        dir = "."
    dir = os.path.realpath(dir)
    treedir = os.path.realpath(tree)
    if not dir.startswith(treedir):
        raise errors.TreeNotContain(tree, path)
    dir = dir[len(treedir)+1:]
    return os.path.join(dir, file)

def tree_cwd(tree):
    """Get the current directory expressed as a tree path.
    :param tree: The tree that the current directory is in
    :type tree: ArchSourceTree
    :return: The tree path
    :raise TreeNotContain: The path does not fall in the tree
    """
    cwd = os.getcwd()
    if not cwd.startswith(str(tree)):
        raise errors.TreeNotContain(tree, cwd)
    treepath = cwd[len(str(tree)):]
    treepath = treepath.lstrip('/')
    return treepath


def iter_changedfile(tree, revision, filename, yield_file=False):
    """Lists logs of ancestor revisions that changed a particular file.  
    Revisions that do not have patchlogs in the tree are ignored.

    :param tree: The tree to get logs from
    :type tree: `arch.ArchSourceTree`
    :param revision: The revision to trace ancestry from
    :type revision: `arch.Revision`
    :param filename: The filename to look for changes to
    :type filename: str
    :rtype: iterator of `arch.Patchlog`
    """
    cwd = tree_cwd(tree)
    if cwd != "":
        filename = cwd + "/" + filename
    for log in _iter_ancestry(tree, revision):
        if isinstance(log, pybaz.Patchlog):
            if filename in log.modified_files or filename in log.new_files or filename in log.removed_files:
                if yield_file:
                    yield (filename, log)
                else:
                    yield log
            if log.renamed_files:
                for (key, value) in log.renamed_files.iteritems():
                    if value == filename:
                        filename = key
                        break

def iter_changed_file_line(tree, revision, filename, line_num):
    """Lists logs of ancestor revisions that changed a particular file.  
    Revisions that do not have patchlogs in the tree are ignored.

    :param tree: The tree to get logs from
    :type tree: `arch.ArchSourceTree`
    :param revision: The revision to trace ancestry from
    :type revision: `arch.Revision`
    :param filename: The filename to look for changes to
    :type filename: str
    :rtype: iterator of `arch.Patchlog`
    """
    cwd = tree_cwd(tree)
    if cwd != "":
        cwd += ("/")

    patches = []
    for (filename, log) in iter_changedfile(tree, revision, filename, True):
        path = filename
        if log.revision.patchlevel == "base-0" and log.continuation_of is None:
            continue
        if filename in log.new_files:
            continue
        if filename in log.removed_files:
            continue
        patch = parse_changeset_patches(log.revision,[path])[0]
        if patch is None:
            continue
        new_iter = patch.iter_inserted()
        for (num, line) in new_iter:
            old_num = num

            for cur_patch in patches:
                num = cur_patch.pos_in_mod(num)
                if num == None: 
                    break
            if num == line_num - 1:
                yield log
        patches=[patch]+patches


class AnnotateLine:
    """A line associated with the log that produced it"""
    def __init__(self, text, log=None):
        self.text = text
        self.log = log


def annotate_file(tree, revision, filename):
    """Lists logs of ancestor revisions that changed a particular file.  
    Revisions that do not have patchlogs in the tree are ignored.

    :param tree: The tree to get logs from
    :type tree: `arch.ArchSourceTree`
    :param revision: The revision to trace ancestry from
    :type revision: `arch.Revision`
    :param filename: The filename to look for changes to
    :type filename: str
    :rtype: iterator of `arch.Patchlog`
    """
    lines = [AnnotateLine(f) for f in open(filename)]

    patches = []
    for (filename, log) in iter_changedfile(tree, revision, filename, True):
        patch = None
        path = filename
        if log.revision.patchlevel == "base-0" and log.continuation_of is None:
            continue
        if filename in log.new_files:
            new_iter = iter_new_file(log.revision, path)
        else:
            patch = parse_changeset_patches(log.revision,[path])[0]
            if patch is None: 
                continue
            new_iter = patch.iter_inserted()
        for (num, line) in new_iter:
            old_num = num

            for cur_patch in patches:
                num = cur_patch.pos_in_mod(num)
                if num == None: 
                    break

            if num is not None and lines[num].log is None:
                lines[num].log = log
        if patch is None:
            break
        patches=[patch]+patches
    return lines


def print_annotated(lines):
    """Unprettily-prints a set of annotated lines from annotate_file"""
    last = "NotNoneOrPatchlog"
    for line in lines:
        if line.log != last:
            if line.log == None:
                print "UNASSIGNED"
            else:
                print line.log.revision
            last=line.log
        print("  " + line.text.rstrip("\n"))

def iter_changed_file_line_orig(tree, revision, filename, line_num):
    """Lists logs of revisions that originally added a line to a particular
    file.  Revisions that do not have patchlogs in the tree are ignored.

    :param tree: The tree to get logs from
    :type tree: `arch.ArchSourceTree`
    :param revision: The revision to trace ancestry from
    :type revision: `arch.Revision`
    :param filename: The filename to look for changes to
    :type filename: str
    :rtype: iterator of `arch.Patchlog`
    """
    path = tree_cwd(tree)+"/"+filename
    my_file = open(filename)
    try:
        for i in range(line_num):
            line = my_file.next()
    except StopIteration:
        raise ValueError("line number out of range")
    for log in iter_changed_file_line(tree, revision, filename, line_num):
        yield merge_blame(log, filename, path, line)


def merge_blame(log, filename, path, line, diffs=None):
    """Return the log of the revision that added a line.

    :param log: The log of the revision that added a line
    :type log: `arch.Patchlog`
    :param filename: The name of the file to look for
    :type filename: str
    :param path: The path of the file to look for
    :type path: str
    :param line: The added line
    :type line: str
    """
    for revision in log.new_patches:
        if revision == log.revision:
            continue
        if filename in log.modified_files or filename in log.new_files:
            if patch_added_line(log.revision, filename, line, diffs):
                return merge_blame(revision.patchlog, filename, path, line,
                                   diffs)
    return log

def get_diff(revision, path, diffs):
    """Returns the diff for a given path, memoizing it in diffs.
    :param revision: The revision to find the diff for
    :type revision: `arch.Revision`
    :param path: The path of the diff to get
    :type path: str
    :param diffs: The diff will be placed in diffs if it's not already there
    :type diffs: map of str => str
    :return: lines of the diff
    """
    if diffs is not None:
        diff = diffs.get(str(revision))
    else:
        diff = None

    if diff is not None:
        return diff
    
    tree = util.tmpdir()
    try:
        ensure_archive_registered(revision.archive)
        changeset = revision.get_patch(tree+"/changeset")
        patch_path = "%s/patches/%s.patch" % (changeset, path)
        if not os.access(patch_path, os.R_OK):
            return None
        diff = list(open(patch_path))
        if diffs is not None:
            diffs[str(revision)] = diff
    finally:
        shutil.rmtree(tree)
    return diff

def patch_added_line(revision, path, line, diffs=None):
    """Determine whether a revision's changeset added a line.

    :param revision: The log of the revision that may have added a line
    :type revision: `arch.Revision`
    :param path: The path of the file to look for
    :type path: str
    :param line: The added line
    :type line: str
    """
    diff = get_diff(revision, path, diffs)
    if diff is None:
        return False
    for pline in diff:
        match = (pline.startswith('+') and pline[1:] == line)
        if match:
            break
    return match


def parse_changeset_patches(revision, paths):
    """Retrieves the requested patches from a revision changeset.

    :param revision: The revision of the changeset
    :type revision: `arch.Revision`
    :param paths: The path of the file to get patches for
    :type path: str
    :return: list of patches, in order of request
    :rtype: list of `patches.Patch`
    """
    tree = util.tmpdir()
    ensure_archive_registered(revision.archive)
    changeset = revision.get_patch(tree+"/changeset")
    patchen = []
    for path in paths:
        patch_path = "%s/patches/%s.patch" % (changeset, path)
        if os.path.exists(patch_path):
            patchen.append(patches.parse_patch(open(patch_path)))
        else:
            patchen.append(None)
    shutil.rmtree(tree)
    return patchen 


def iter_new_file(revision, path):
    """Retrieves the requested patches from a revision changeset.

    :param revision: The revision of the changeset
    :type revision: `arch.Revision`
    :param paths: The path of the file to get patches for
    :type path: str
    :return: list of patches, in order of request
    :rtype: list of `patches.Patch`
    """
    tree = util.tmpdir()
    ensure_archive_registered(revision.archive)
    changeset = revision.get_patch(tree+"/changeset")
    file_path = "%s/new-files-archive/%s" % (changeset, path)
    i = 0
    lines = []
    for line in open(file_path):
        lines.append((i, line))
        i += 1
    shutil.rmtree(tree)
    for line in lines:
        yield line


def iter_log_match(iter, key, value):
    for log in iter:
        if isinstance(log, pybaz.Revision):
            log = pybaz.Patchlog(log)
        if log[key] == value:
            yield log


def tree_latest(tree, version=None):
    """Return the latest Revision in the tree

    :param tree: The tree to return the revision for
    :type tree: `arch.ArchSourceTree`
    :param version: The version of the revision to find
    :type version: `arch.Version`
    """
    if version is None:
        version = tree.tree_version
    try:
        log = tree.iter_logs(version = version, reverse = True).next()
        return log.revision
    except StopIteration:
        raise errors.NoVersionRevisions(tree, version)

def iter_all_library_revisions():
    """Generates an iterator of all revisions in the library
    :return: iterator of all revisions in the revision library
    :rtype: iter of `arch.Revision`
    """
    for archive in pybaz.iter_library_archives():
        for category in archive.iter_library_categories():
            for branch in category.iter_library_branches():
                for version in branch.iter_library_versions():
                    for revision in version.iter_library_revisions():
                        yield revision


def is_library_dir(directory):
    directory = os.path.realpath(directory)
    for revlib in arch_core.iter_revision_libraries():
        if dir == revlib:
            return True
    return False


def iter_greedy_libraries():
    """Generate an iterator of greedy libraries.
    :rtype: iter of str
    """
    for path in arch_core.iter_revision_libraries():
        if os.access("%s/=greedy" % path, os.R_OK):
            yield path


def find_or_make_local_revision(revision):
    """Finds or makes a library copy of a revision.
    :param revision: The revision to look for
    :type revision: `arch.Revision`
    :return: The library copy
    :rtype: `arch.LibraryTree`
    """
    try:
        return revision.library_find()
    except:
        ensure_revision_exists(revision)
        for lib in iter_greedy_libraries():
            list(arch_core.iter_library_add(revision, lib))
            return revision.library_find()
    raise errors.NoGreedy(revision)

def list_mod_names(tree):
    tmpdir = util.tmpdir()+"/changeset"
    modified = []
    try:
        for line in pybaz.iter_delta(tree_latest(tree), tree, tmpdir):
            if isinstance(line, pybaz.TreeChange):
                modified.append(line.name[1:])
    finally:
        shutil.rmtree(tmpdir)
    return modified

def list_mod(tree, revision=None):
    tmpdir = util.tmpdir()+"/changeset"
    modified = []
    if revision is None:
        revision = tree_latest(tree)
    try:
        for line in pybaz.iter_delta(revision, tree, tmpdir):
            if isinstance(line, pybaz.TreeChange):
                if line.name[0]==" ":
                    line.name = line.name[1:]
                modified.append(line)
    finally:
        shutil.rmtree(tmpdir)
    return modified

def valid_tree_root(tree):
    if tree is None:
        raise errors.TreeRootNone()


def iter_no_conflicts(iter_rev, test_dir, good_dir, reverse=False):
    """Generate an iterator of revisions/changesets that don't conflict.
    revision,changeset tuples are returned, but the changeset is None if
    applying it would produce conflicts.

    :param iter_rev: the iterator of revisions to go through
    :type iter_rev: iterator of `arch.Revision`
    :param test_dir: The scratch directory to use
    :type test_dir: str
    :param good_dir: The good directory to copy from
    :type good_dir: str
    :param reverse: Apply revisions in reverse?
    :type reverse: bool
    :rtype: iter of (revision, changeset) 
    """
    for revision in iter_rev:
        if revision == None:
            continue
        if isinstance(revision, pybaz.Patchlog):
            revision = revision.revision
        directory = util.tmpdir()
        tree = pybaz.tree_root(test_dir)
        changeset = revision.get_patch(directory+"/changeset")
        result = arch_core.apply_changeset(tree, changeset, 
                                           reverse=reverse)
        if result:
            changeset = None
            shutil.rmtree(test_dir)
            util.linktree(good_dir, test_dir)

        yield (revision, changeset)
        shutil.rmtree(directory)

def iter_no_conflicts_rev(iter_rev, test_dir, good_dir, reverse=False):
    """Generate an iterator of revisions/changesets that don't conflict.
    
    :param iter_rev: the iterator of revisions to go through
    :type iter_rev: iterator of `arch.Revision`
    :param test_dir: The scratch directory to use
    :type test_dir: str
    :param good_dir: The good directory to copy from
    :type good_dir: str
    :param reverse: Apply revisions in reverse?
    :type reverse: bool
    :rtype: iter of revision 
    """
    for (revision, changeset) in \
        iter_no_conflicts(iter_rev, test_dir, good_dir, reverse):
        if changeset is not None:
            shutil.rmtree(changeset)
            yield revision


def iter_skip_replay_conflicts(tree, version):
    """Generate an iterator of revisions/changesets that won't conflict if
    replayed.  Assumes that all revisions returned are applied.
    
    :param tree: the tree that revisions might conflict with
    :type tree: `arch.WorkingTree`
    :param version: 
    :param reverse: Apply revisions in reverse?
    """
    iter_miss = arch_core.iter_missing(tree, version, False)
    test_dir = util.tmpdir(tree)
    try:
        util.linktree(tree, test_dir, top_exists = True)
        iter_no_conf = iter_no_conflicts_rev(iter_miss, test_dir, tree)
        return util.iter_delete_wrapper(iter_no_conf, test_dir)
    except:
        shutil.rmtree(test_dir)
        raise


class iter_depends:
    def __init__(self, anc_iter, nondeps=True):
        self.anc_iter = anc_iter
        self.nondeps=nondeps
        try:
            revision = anc_iter.next()
            if isinstance(revision, pybaz.Patchlog):
                revision = revision.revision
            self.treedir = util.tmpdir(os.getcwd())
            self.good = self.treedir+"/good"
            self.test = self.treedir+"/test"
            list(arch_core.iter_get(revision, self.good))
            util.linktree(self.good, self.test)
        except StopIteration:
            self.treedir = None

    def __del__(self):
        if self.treedir is not None:
            shutil.rmtree(self.treedir)

    def __iter__(self):
        if self.treedir is None:
            return
        revision = None
        for newrevision in self.anc_iter:
            if revision == None:
                revision = newrevision
                continue
            if isinstance(revision, pybaz.Patchlog):
                revision = revision.revision
            directory = util.tmpdir()
            try:
                changeset = revision.get_patch(directory+"/changeset")
                result = arch_core.apply_changeset(pybaz.tree_root(self.test),
                                                   changeset, reverse=True)
                if result:
                    arch_core.apply_changeset(pybaz.tree_root(self.good),
                                              changeset, reverse=True)
            except:
                shutil.rmtree(directory)
                raise
            
            shutil.rmtree(directory)
            if (result == 0) == self.nondeps:
                yield revision
            if result != 0:
                shutil.rmtree(self.test)
                shutil.copytree(self.good, self.test)
            revision = newrevision


class LogMemo:
    """Memoized access to tree logs"""
    def __init__(self, tree):
        self.tree = tree
        self.logs = {}

    def get_log(self, revision):
        """From tree, get the log for this revision.
        
        :param revision: The revision to get
        :type revision: `arch.Revision`
        """
        if not self.logs.has_key(revision.version):
            self.logs[str(revision.version)] = \
                list(self.tree.iter_logs(revision.version))
        for log in self.logs[str(revision.version)]:
            if log.revision == revision:
                return log
        return None


def iter_to_present(iter, tree):
    """Returns all logs until it encounters new-patch present in the tree

    :param iter: The iterator that produces logs/revisions
    :type iter: iter of `arch.Patchlog` or `arch.Revision`
    """
    log_memo = LogMemo(tree)
    for log in iter:
        if isinstance(log, pybaz.Revision):
            log = log.patchlog
        for revision in log.new_patches:
            if log_memo.get_log(revision) is not None:
                return
        yield log


def changelog_for_merge(logs, merges):
    """Produces a new-format log-for-merge that includes merged log bodies

    :param logs: The list or iter of logs to use.  Direct merges recommended.
    :type logs: iter of `arch.Patchlog`
    :return: A string containing log-for-merge text
    :rtype: str
    """
    changelog = ""
    for log in logs:
        if changelog != "":
            changelog += "\n"
        changelog += " * %s\n" % log.revision
        changelog += " * %s\n" % log.summary
        if (log.description != ""):
            has_bogus_merges = False
            for merge in log.new_patches:
                if merges is not None and not merge in [x.revision for x in 
                                                        merges]:
                    has_bogus_merges = True
                    break
            if not has_bogus_merges:
                changelog += log.description
    return changelog


class EntryHandler:
    def __init__(self, tree):
        self.tree = tree
        self.id_map = arch_core.get_id_filename_map(tree)

class PatchEntryHandler(EntryHandler):
    def __init__(self, tree):
        EntryHandler.__init__(self, tree)
        self.removed_dir = None

    def __call__(self, entry, reverse):
        if not entry.diff:
            return None
        status = self.invoke_patch(entry, reverse)
        if status == 0:
            result = pybaz.FileModification("M  %s" % entry.mod_name[2:], ' ')
        elif status == 1:
            result = pybaz.PatchConflict("C   %s" % entry.mod_name[2:])
        else:
            raise Exception("unexpected status %s" % str(status))
        os.unlink(entry.diff)
        return result

    def get_removed_dir(self):
        if self.removed_dir is None:
            self.removed_dir = tempfile.mkdtemp("", "+removed-conflict-files", 
                                        self.tree)
        return self.removed_dir

    def remove_conflict_files(self, file):
        rej = file+".rej"
        if not os.path.exists(rej):
            rej = None
        orig = file+".orig"
        if not os.path.exists(orig):
            orig = None
        if orig is not None or rej is not None:
            removed_dir = self.get_removed_dir()
            new_parent_dir = os.path.dirname(os.path.join(removed_dir,
                                             file[len(self.tree):]))
            if not os.path.exists(new_parent_dir):
                os.makedirs(new_parent_dir)

            if orig is not None:
                os.rename(orig, os.path.join(new_parent_dir, 
                          os.path.basename(orig)))
            if rej is not None:
                os.rename(rej, os.path.join(new_parent_dir, 
                          os.path.basename(rej)))

    def invoke_patch(self, entry, reverse=False):
        file = os.path.join(self.tree, self.id_map[entry.id])
        self.remove_conflict_files(file)
        new_version = util.NewFileVersion(file)
        out_file = new_version.temp_filename
        status = native.invoke_patch(file, entry.diff, out_file, reverse)
        if status == 1:
            os.link(file, file+".orig")
            os.rename(out_file+".rej", file+".rej")
        new_version.commit()
        return status


class Diff3Handler(PatchEntryHandler):
    def __init__(self, tree, older_tree, yours_tree):
        PatchEntryHandler.__init__(self, tree)
        self.older_tree = older_tree
        self.yours_tree = yours_tree
        self.older_map = arch_core.get_id_filename_map(self.older_tree)
        self.yours_map = arch_core.get_id_filename_map(self.yours_tree)

    def invoke_patch(self, entry, reverse=False):
        mine_file = os.path.join(self.tree, self.id_map[entry.id])
        older_path = os.path.join(self.older_tree, self.older_map[entry.id])
        yours_path = os.path.join(self.yours_tree, self.yours_map[entry.id])
        self.remove_conflict_files(mine_file)
        new_version = util.NewFileVersion(mine_file)
        out_file = new_version.temp_filename
        if reverse:
            status = native.invoke_diff3(new_version, mine_file, yours_path, 
                                         older_path)
        else:
            status = native.invoke_diff3(new_version, mine_file, older_path,
                                         yours_path)

        new_version.commit()
        return status


class iter_custom_apply_changeset:
    def __init__(self, dir, munger, entry_handler, reverse=False):
        self.dir = dir
        self.munger = munger
        self.entry_handler = entry_handler
        self.reverse = reverse
        self.conflicting = False
        self.iterator = self.make_iterator()

    def __iter__(self):
        """Destructively applies a changeset
        """
        return self.iterator


    def next(self):
        return self.iterator.next()


    def make_iterator(self):
        if self.entry_handler is not None:
            for entry in self.munger.get_entries().itervalues():
                result = self.entry_handler(entry, self.reverse)
                if result is not None:
                    if isinstance(result, pybaz.PatchConflict):
                        self.conflicting = True
                    yield result
        changeset = pybaz.Changeset(self.munger.changeset)
        ch_iter = changeset.iter_apply(self.dir, self.reverse)
        for line in ch_iter:
            yield line
        if ch_iter.conflicting:
            self.conflicting = True

def apply_delta_custom(a_spec, b_spec, tree, munge_opts=None, apply_type=None, 
        reverse=False):
    def spec_resolve(spec):
        if isinstance(spec, pybaz.Revision):
            return find_or_make_local_revision(spec)
        else:
            return spec

    from_path = spec_resolve(a_spec)
    to_path = spec_resolve(b_spec)
    
    if apply_type == PatchEntryHandler:
        entry_handler = PatchEntryHandler(tree, reverse=reverse)
    elif apply_type == Diff3Handler:
        entry_handler = Diff3Handler(tree, from_path, to_path)
    else:
        entry_handler = None

    tmp=util.tmpdir(tree)
    changeset=tmp+"/changeset"
    delt=pybaz.iter_delta(from_path, to_path, changeset)
    for line in delt:
        yield line
    munger = native.ChangesetMunger(delt.changeset)
    munger.read_indices()
    if munge_opts is not None:
        yield pybaz.Chatter("* munging changeset")
        munger.munge(munge_opts)
    yield pybaz.Chatter("* applying changeset")
    for line in iter_custom_apply_changeset(tree, munger, entry_handler, reverse=reverse):
        yield line
    shutil.rmtree(tmp)


def revision(name):
    try:
        return NativeRevision(name)
    except errors.UnsupportedScheme, e:
        return pybaz.Revision(name)


class NativeRevision(pybaz.Revision):
    def __init__ (self, name):
        parsed = pybaz.NameParser(name)
        self.arch_name = parsed.get_archive()
        params = native.ArchParams()
        if not params['=locations'].exists(self.arch_name):
            raise pybaz.errors.ArchiveNotRegistered()
        self.location = params['=locations'][self.arch_name].strip()
        native.get_pfs_type(self.location)
        pybaz.Revision.__init__(self, name)
        
    def narchive(self):
        return native.get_pfs_archive(self.location, self.arch_name)

    def exists(self):
        return self.narchive().exists(self)

    def get_patch(self, dir):
        tempdir = tempfile.mkdtemp("", ",", 
                                   os.path.dirname(os.path.realpath(dir)))
        try:
            patchfile = os.path.join(tempdir + "delta.tar.gz")
            patch = native.cache_get_revision(self, "delta.tar.gz")
            if patch is None:
                patch = self.narchive().get_patch(self)
            file(patchfile, "wb").write(patch.read())
            parent_dir = util.untar_parent(patchfile, tempdir)
            os.rename(os.path.join(tempdir, parent_dir), dir)

        finally:
            shutil.rmtree(tempdir)
        return pybaz.Changeset(dir)

# arch-tag: c73c601a-0050-4e6c-ac63-819d7a00921f