~jelmer/brz-debian/debmutate

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
#    import_dsc.py -- Import a series of .dsc files.
#    Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
#              (C) 2008 Canonical Ltd.
#
#    Code is also taken from bzrtools, which is
#             (C) 2005, 2006, 2007 Aaron Bentley <aaron.bentley@utoronto.ca>
#             (C) 2005, 2006 Canonical Limited.
#             (C) 2006 Michael Ellerman.
#    and distributed under the GPL, version 2 or later.
#
#    This file is part of bzr-builddeb.
#
#    bzr-builddeb 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.
#
#    bzr-builddeb 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 bzr-builddeb; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

from __future__ import absolute_import

import calendar
from contextlib import contextmanager
import os
import shutil
import stat
import tempfile

from debian import deb822
from debian.changelog import Version, Changelog, VersionError

from ... import (
    controldir,
    osutils,
    )
from ...export import (
    export,
    )
from ...config import ConfigObj
from ...errors import (
    BzrError,
    AlreadyBranchError,
    NotBranchError,
    NoRoundtrippingSupport,
    NoWorkingTree,
    UnrelatedBranches,
    )
from ...revision import NULL_REVISION
from ...trace import warning, mutter
from ...transport import (
    get_transport,
    )

from .bzrtools_import import import_dir
from .errors import (
    MultipleUpstreamTarballsNotSupported,
    PackageVersionNotPresent,
    UpstreamAlreadyImported,
    UpstreamBranchAlreadyMerged,
    )
from .extract import extract
from .util import (
    extract_orig_tarballs,
    get_commit_info_from_changelog,
    md5sum_filename,
    open_file_via_transport,
    open_transport,
    safe_decode,
    )
from .upstream.pristinetar import (
    PristineTarSource,
    )


class PreviousVersionTagMissing(BzrError):

    _fmt = ("Unable to find the tag for the "
            "previous upstream version (%(version)s) in the branch: "
            "%(tag_name)s")

    def __init__(self, version, tag_name):
        super(PreviousVersionTagMissing, self).__init__(
            version=version, tag_name=tag_name)


class DscCache(object):

    def __init__(self, transport=None):
        self.cache = {}
        self.transport_cache = {}
        self.transport = transport

    def get_dsc(self, name):

        if name in self.cache:
            dsc1 = self.cache[name]
        else:
            # Obtain the dsc file, following any redirects as needed.
            filename, transport = open_transport(name)
            with open_file_via_transport(filename, transport) as f1:
                dsc1 = deb822.Dsc(f1)
            self.cache[name] = dsc1
            self.transport_cache[name] = transport

        return dsc1

    def get_transport(self, name):
        return self.transport_cache[name]


class DscComp(object):

    def __init__(self, cache):
        self.cache = cache

    def key(self, dscname):
        dsc = self.cache.get_dsc(dscname)
        return Version(dsc['Version'])

    def cmp(self, dscname1, dscname2):
        dsc1 = self.cache.get_dsc(dscname1)
        dsc2 = self.cache.get_dsc(dscname2)
        v1 = Version(dsc1['Version'])
        v2 = Version(dsc2['Version'])
        if v1 == v2:
            return 0
        if v1 > v2:
            return 1
        return -1


class DistributionBranchSet(object):
    """A collection of DistributionBranches with an ordering.

    A DistributionBranchSet collects a group of DistributionBranches
    and an order, and then can provide the branches with information
    about their place in the relationship with other branches.
    """

    def __init__(self):
        """Create a DistributionBranchSet."""
        self._branch_list = []

    def add_branch(self, branch):
        """Adds a DistributionBranch to the end of the list.

        Appends the passed distribution branch to the end of the list
        that this DistributionBranchSet represents. It also provides
        the distribution branch with a way to get the branches that
        are before and after it in the list.

        It will call branch.set_get_lesser_branches_callback() and
        branch.set_get_greater_branches_callback(), passing it methods
        that the DistributionBranch can call to get the list of branches
        before it in the list and after it in the list respectively.
        The passed methods take no arguments and return a list (possibly
        empty) of the desired branches.

        :param branch: the DistributionBranch to add.
        """
        self._branch_list.append(branch)
        lesser_callback = self._make_lesser_callback(branch)
        branch.set_get_lesser_branches_callback(lesser_callback)
        greater_callback = self._make_greater_callback(branch)
        branch.set_get_greater_branches_callback(greater_callback)

    def _make_lesser_callback(self, branch):
        return lambda: self.get_lesser_branches(branch)

    def _make_greater_callback(self, branch):
        return lambda: self.get_greater_branches(branch)

    def get_lesser_branches(self, branch):
        """Return the list of branches less than the argument.

        :param branch: The branch that all branches returned must be less
            than.
        :return: a (possibly empty) list of all the branches that are
            less than the argument. The list is sorted starting with the
            least element.
        """
        index = self._branch_list.index(branch)
        return self._branch_list[:index]

    def get_greater_branches(self, branch):
        """Return the list of branches greater than the argument.

        :param branch: The branch that all branches returned must be greater
            than.
        :return: a (possibly empty) list of all the branches that are
            greater than the argument. The list is sorted starting with the
            least element.
        """
        index = self._branch_list.index(branch)
        return self._branch_list[index+1:]


def checkout_upstream_version(tree, package, version, revisions):
    """Checkout an upstream version from the pristine tar source.

    """
    tree.update(revision=revisions[None])
    parent_ids = []
    for component in sorted(revisions.keys()):
        revid = revisions[component]
        if component is not None:
            component_tree = tree.branch.repository.revision_tree(revid)
            export(component_tree, os.path.join(tree.basedir, component),
                   format='dir')
        parent_ids.append(revid)
    tree.set_parent_ids(parent_ids)


class DistributionBranch(object):
    """A DistributionBranch is a representation of one line of development.

    It is a branch that is linked to a line of development, such as Debian
    unstable. It also has associated branches, some of which are "lesser"
    and some are "greater". A lesser branch is one that this branch
    derives from. A greater branch is one that derives from this. For
    instance Debian experimental would have unstable as a lesser branch,
    and vice-versa. It is assumed that a group of DistributionBranches will
    have a total ordering with respect to these relationships.
    """

    def __init__(self, branch, pristine_upstream_branch, tree=None,
                 pristine_upstream_tree=None):
        """Create a distribution branch.

        You can only import packages on to the DistributionBranch
        if both tree and pristine_upstream_tree are provided.

        :param branch: the Branch for the packaging part.
        :param pristine_upstream_branch: the Branch for the pristine tar part,
            if any.
        :param tree: an optional tree for the branch.
        :param pristine_upstream_tree: an optional tree for the
            pristine_upstream_branch.
        """
        self.branch = branch
        self.tree = tree
        self.pristine_upstream_branch = pristine_upstream_branch
        self.pristine_upstream_tree = pristine_upstream_tree
        if pristine_upstream_branch is not None:
            self.pristine_upstream_source = PristineTarSource.from_tree(
                branch=pristine_upstream_branch, tree=tree)
        else:
            self.pristine_upstream_source = None
        self.get_lesser_branches = None
        self.get_greater_branches = None

    def set_get_lesser_branches_callback(self, callback):
        """Set the callback to get the branches "lesser" than this.

        The function passed to this method will be used to get the
        list of branches that are "lesser" than this one. It is
        expected to require no arguments, and to return the desired
        (possibly empty) list of branches. The returned list should
        be sorted starting with the least element.

        :param callback: a function that is called to get the desired list
            of branches.
        """
        self.get_lesser_branches = callback

    def set_get_greater_branches_callback(self, callback):
        """Set the callback to get the branches "greater" than this.

        The function passed to this method will be used to get the
        list of branches that are "greater" than this one. It is
        expected to require no arguments, and to return the desired
        (possibly empty) list of branches. The returned list should
        be sorted starting with the least element.

        :param callback: a function that is called to get the desired list
            of branches.
        """
        self.get_greater_branches = callback

    def get_other_branches(self):
        """Return all the other branches in this set.

        The returned list will be ordered, and will not contain this
        branch.

        :return: a list of all the other branches in this set (if any).
        """
        return self.get_lesser_branches() + self.get_greater_branches()

    def tag_name(self, version, vendor=None):
        """Gets the name of the tag that is used for the version.

        :param version: the Version object that the tag should refer to.
        :return: a String with the name of the tag.
        """
        if vendor is not None:
            return '%s/%s' % (vendor, version)
        else:
            return str(version)

    def _has_version(self, branch, tag_name, md5=None):
        if not branch.tags.has_tag(tag_name):
            return False
        revid = branch.tags.lookup_tag(tag_name)
        with branch.lock_read():
            graph = branch.repository.get_graph()
            if not graph.is_ancestor(revid, branch.last_revision()):
                return False
        if md5 is None:
            return True
        rev = branch.repository.get_revision(revid)
        # TODO(jelmer): Check the commit message for git repositories
        try:
            return rev.properties['deb-md5'] == md5
        except KeyError:
            warning(
                "tag %s present in branch, but there is no "
                "associated 'deb-md5' property" % tag_name)
            return False

    def has_version(self, version, md5=None, vendor=None):
        """Whether this branch contains the package version specified.

        The version must be judged present by having the appropriate tag
        in the branch. If the md5 argument is not None then the string
        passed must the the md5sum that is associated with the revision
        pointed to by the tag.

        :param version: a Version object to look for in this branch.
        :param md5: a string with the md5sum that if not None must be
            associated with the revision.
        :return: True if this branch contains the specified version of the
            package. False otherwise.
        """
        if self._has_version(self.branch, str(version), md5=md5):
            return True
        # TODO(jelmer): There is some overlap here with revid_of_version
        for debian_tag_name in ["debian-%s" % version, "debian/%s" % version]:
            if self._has_version(self.branch, debian_tag_name, md5=md5):
                return True
        for ubuntu_tag_name in ["ubuntu-%s" % version, "ubuntu/%s" % version]:
            if self._has_version(self.branch, ubuntu_tag_name, md5=md5):
                return True
        return False

    def contained_versions(self, versions):
        """Splits a list of versions depending on presence in the branch.

        Partitions the input list of versions depending on whether they
        are present in the branch or not.

        The two output lists will be sorted in the same order as the input
        list.

        :param versions: a list of Version objects to look for in the
            branch. May be an empty list.
        :return: A tuple of two lists. The first list is the list of those
            items from the input list that are present in the branch. The
            second list is the list of those items from the input list that
            are not present in the branch. The two lists will be disjoint
            and cover the input list. Either list may be empty, or both if
            the input list is empty.
        """
        # FIXME: should probably do an ancestory check to find all
        # merged revisions. This will avoid adding an extra parent when say
        # experimental 1-1~rc1
        # unstable 1-1 1-1~rc1
        # Ubuntu 1-1ubuntu1 1-1 1-1~rc1
        # where only the first in each list is actually uploaded.
        contained = []
        not_contained = []
        for version in versions:
            if self.has_version(version):
                contained.append(version)
            else:
                not_contained.append(version)
        return contained, not_contained

    def missing_versions(self, versions):
        """Returns the versions from the list that the branch does not have.

        Looks at all the versions specified and returns a list of the ones
        that are earlier in the list that the last version that is
        contained in this branch.

        :param versions: a list of Version objects to look for in the branch.
            May be an empty list.
        :return: The subset of versions from the list that are not present
            in this branch. May be an empty list.
        """
        last_contained = self.last_contained_version(versions)
        if last_contained is None:
            return versions
        index = versions.index(last_contained)
        return versions[:index]

    def last_contained_version(self, versions):
        """Returns the highest version from the list present in this branch.

        It assumes that the input list of versions is sorted with the
        highest version first.

        :param versions: a list of Version objects to look for in the branch.
            Must be sorted with the highest version first. May be an empty
            list.
        :return: the highest version that is contained in this branch, or
            None if none of the versions are contained within the branch.
        """
        for version in versions:
            if self.has_version(version):
                return version
        return None

    def revid_of_version(self, version):
        """Returns the revision id corresponding to that version.

        :param version: the Version object that you wish to retrieve the
            revision id of. The Version must be present in the branch.
        :return: the revision id corresponding to that version
        """
        if self._has_version(self.branch, str(version)):
            return self.branch.tags.lookup_tag(str(version))
        for debian_tag_name in ["debian-%s" % version, "debian/%s" % version]:
            if self._has_version(self.branch, debian_tag_name):
                return self.branch.tags.lookup_tag(debian_tag_name)
        for ubuntu_tag_name in ["ubuntu-%s" % version, "ubuntu/%s" % version]:
            if self._has_version(self.branch, ubuntu_tag_name):
                return self.branch.tags.lookup_tag(ubuntu_tag_name)
        return self.branch.tags.lookup_tag(str(version))

    def tag_version(self, version, revid=None, vendor=None):
        """Tags the branch's last revision with the given version.

        Sets a tag on the last revision of the branch with a tag that refers
        to the version provided.

        :param version: the Version object to derive the tag name from.
        :param revid: the revid to associate the tag with, or None for the
            tip of self.branch.
        :return: Name of the tag set
        """
        tag_name = self.tag_name(version, vendor)
        if revid is None:
            revid = self.branch.last_revision()
        self.branch.tags.set_tag(tag_name, revid)
        return tag_name

    def _default_config_for_tree(self, tree):
        # FIXME: shouldn't go to configobj directly
        for path in ('debian/bzr-builddeb.conf', '.bzr-builddeb/default.conf'):
            fileid = tree.path2id(path)
            if fileid is not None:
                break
        else:
            return None, None, None
        with tree.lock_read():
            config = ConfigObj(tree.get_file(path))
            try:
                config['BUILDDEB']
            except KeyError:
                config['BUILDDEB'] = {}
        return fileid, path, config

    def _is_tree_native(self, config):
        if config is not None:
            try:
                current_value = config['BUILDDEB']['native']
            except KeyError:
                current_value = False
            return current_value == "True"
        return False

    def is_version_native(self, version):
        """Determines whether the given version is native.

        :param version: the Version object to test. Must be present in
            the branch.
        :return: True if the version is was recorded as native when
            imported, False otherwise.
        """
        revid = self.revid_of_version(version)
        rev_tree = self.branch.repository.revision_tree(revid)
        (config_fileid, config_path,
         current_config) = self._default_config_for_tree(rev_tree)
        if self._is_tree_native(current_config):
            return True
        rev = self.branch.repository.get_revision(revid)
        try:
            prop = rev.properties["deb-native"]
            return prop == "True"
        except KeyError:
            return False

    def can_pull_from_branch(self, branch, version, md5):
        if not branch.has_version(version, md5=md5):
            return False

        # Check that they haven't diverged
        with branch.branch.lock_read():
            graph = branch.branch.repository.get_graph(
                    self.branch.repository)
            return graph.is_ancestor(
                self.branch.last_revision(), branch.revid_of_version(version))

    def branch_to_pull_version_from(self, version, md5):
        """Checks whether this upload is a pull from a lesser branch.

        Looks in all the lesser branches for the given version/md5 pair
        in a branch that has not diverged from this.

        If it is present in another branch that has not diverged this
        method will return the greatest branch that it is present in,
        otherwise it will return None. If it returns a branch then it
        indicates that a pull should be done from that branch, rather
        than importing the version as a new revision in this branch.

        :param version: the Version object to look for in the lesser
            branches.
        :param md5: a String containing the md5 associateed with the
            version.
        :return: a DistributionBranch object to pull from if that is
            what should be done, otherwise None.
        """
        assert md5 is not None, \
            ("It's not a good idea to use branch_to_pull_version_from with "
             "md5 == None, as you may pull the wrong revision.")
        with self.branch.lock_read():
            for branch in reversed(self.get_lesser_branches()):
                if self.can_pull_from_branch(branch, version, md5):
                    return branch
            for branch in self.get_greater_branches():
                if self.can_pull_from_branch(branch, version, md5):
                    return branch
            return None

    def can_pull_upstream_from_branch(self, branch, package, version,
                                      upstream_tarballs=None):
        """Check if a version can be pulled from another branch into this one.

        :param branch: Branch with upstream version
        :param package: Package name
        :param version: Package version
        :param upstream_tarballs: Required upstream tarballs (optional)
        """
        if not branch.pristine_upstream_source.has_version(
                package, version, tarballs=upstream_tarballs):
            return False

        up_branch = self.pristine_upstream_branch
        with up_branch.lock_read():
            # Check that they haven't diverged
            other_up_branch = branch.pristine_upstream_branch
            with other_up_branch.lock_read():
                graph = other_up_branch.repository.get_graph(
                        up_branch.repository)
                pristine_upstream_revids = branch.pristine_upstream_source\
                    .version_as_revisions(
                        package, version, tarballs=upstream_tarballs)
                for (component, pristine_upstream_revid) in pristine_upstream_revids.items():
                    if not graph.is_ancestor(
                           up_branch.last_revision(), pristine_upstream_revid):
                        return False
                return True

    def branch_to_pull_upstream_from(self, package, version,
                                     upstream_tarballs):
        """Checks whether this upstream is a pull from a lesser branch.

        Looks in all the other upstream branches for the given
        version/md5 pair in a branch that has not diverged from this.
        If it is present in a lower branch this method will return the
        greatest branch that it is present in that has not diverged,
        otherwise it will return None. If it returns a branch then it
        indicates that a pull should be done from that branch, rather
        than importing the upstream as a new revision in this branch.

        :param version: the upstream version to use when searching in the
            lesser branches.
        :return: a DistributionBranch object to pull the upstream from
            if that is what should be done, otherwise None.
        """
        assert isinstance(version, str)
        for branch in reversed(self.get_lesser_branches()):
            if self.can_pull_upstream_from_branch(
                    branch, package, version, upstream_tarballs):
                return branch
        for branch in self.get_greater_branches():
            if self.can_pull_upstream_from_branch(
                    branch, package, version, upstream_tarballs):
                return branch
        return None

    def get_parents(self, versions):
        """Return the list of parents for a specific version.

        This method returns the list of revision ids that should be parents
        for importing a specific package version. The specific package version
        is the first element of the list of versions passed.

        The parents are determined by looking at the other versions in the
        passed list and examining which of the branches (if any) they are
        already present in.

        You should probably use get_parents_with_upstream rather than
        this method.

        :param versions: a list of Version objects, the first item of
            which is the version of the package that is currently being
            imported.
        :return: a list of tuples of (DistributionBranch, version,
            revision id). The revision ids should all be parents of the
            revision that imports the specified version of the package.
            The versions are the versions that correspond to that revision
            id. The DistributionBranch is the branch that contains that
            version.
        """
        assert len(versions) > 0, "Need a version to import"
        mutter("Getting parents of %s" % str(versions))
        missing_versions = self.missing_versions(versions)
        mutter("Versions we don't have are %s" % str(missing_versions))
        last_contained_version = self.last_contained_version(versions)
        parents = []
        if last_contained_version is not None:
            assert last_contained_version != versions[0], \
                "Reupload of a version?"
            mutter("The last versions we do have is %s",
                   str(last_contained_version))
            parents = [
                (self, last_contained_version,
                 self.revid_of_version(last_contained_version))]
        else:
            mutter("We don't have any of those versions")
        for branch in list(reversed(self.get_lesser_branches())) + self.get_greater_branches():
            merged, missing_versions = \
                branch.contained_versions(missing_versions)
            if merged:
                revid = branch.revid_of_version(merged[0])
                parents.append((branch, merged[0], revid))
                mutter("Adding merge from related branch of %s for version %s",
                       revid, str(merged[0]))
                # FIXME: should this really be here?
                self._fetch_from_branch(branch, revid)
        return parents

    def pull_upstream_from_branch(self, pull_branch, package, version):
        """Pulls an upstream version from a branch.

        Given a DistributionBranch and a version number this method
        will pull the upstream part of the given version from the
        branch in to this. The upstream version must be present
        in the DistributionBranch, and it is assumed that the md5
        matches.

        It sets the necessary tags so that the pulled version is
        recognised as being part of this branch.

        :param pull_branch: the DistributionBranch to pull from.
        :param version: the upstream version string
        """
        assert isinstance(version, str)
        pull_revisions = pull_branch.pristine_upstream_source.version_as_revisions(
            package, version)
        for (component, pull_revision) in pull_revisions.items():
            mutter("Fetching upstream part %s of %s from revision %s",
                   component, version, pull_revision)
            assert self.pristine_upstream_tree is not None, \
                "Can't pull upstream with no tree"
            self.pristine_upstream_branch.pull(
                pull_branch.pristine_upstream_branch,
                stop_revision=pull_revision)
            self.pristine_upstream_source.tag_version(version, pull_revision)
            self.branch.fetch(self.pristine_upstream_branch, pull_revision)
            self.pristine_upstream_branch.tags.merge_to(self.branch.tags)
        checkout_upstream_version(
            self.pristine_upstream_tree, package, version, pull_revisions)

    def pull_version_from_branch(self, pull_branch, package, version,
                                 native=False):
        """Pull a version from a particular branch.

        Given a DistributionBranch and a version number this method
        will pull the given version from the branch in to this. The
        version must be present in the DistributionBranch, and it
        is assumed that the md5 matches.

        It will also pull in any upstream part that is needed to
        the upstream branch. It is assumed that the md5 matches
        here as well. If the upstream version must be present in
        at least one of the upstream branches.

        It sets the necessary tags on the revisions so they are
        recongnised in this branch as well.

        :param pull_branch: the DistributionBranch to pull from.
        :param version: the Version to pull.
        :param native: whether it is a native version that is being
            imported.
        """
        pull_revision = pull_branch.revid_of_version(version)
        mutter("already has version %s so pulling from revision %s",
               str(version), pull_revision)
        assert self.tree is not None, "Can't pull branch with no tree"
        self.tree.pull(pull_branch.branch, stop_revision=pull_revision)
        tag_name = self.tag_version(version, revid=pull_revision)
        if not native and not self.pristine_upstream_source.has_version(
                package, version.upstream_version):
            if pull_branch.pristine_upstream_source.has_version(
                    package, version.upstream_version):
                self.pull_upstream_from_branch(
                    pull_branch, package, version.upstream_version)
            else:
                raise AssertionError(
                    "Can't find the needed upstream part for version %s" %
                    version)
        if (native
                and self.pristine_upstream_branch.last_revision() == NULL_REVISION
                and pull_branch.pristine_upstream_branch.last_revision() != NULL_REVISION):
            # in case the package wasn't native before then we pull
            # the upstream. These checks may be a bit restrictive.
            self.pristine_upstream_tree.pull(pull_branch.pristine_upstream_branch)
            pull_branch.pristine_upstream_branch.tags.merge_to(
                    self.pristine_upstream_branch.tags)
        elif native:
            mutter("Not checking for upstream as it is a native package")
        else:
            mutter("Not importing the upstream part as it is already "
                   "present in the upstream branch")
        return tag_name

    def get_parents_with_upstream(self, package, version, versions,
                                  tarballs, force_upstream_parent=False):
        """Get the list of parents including any upstream parents.

        Further to get_parents this method includes any upstream parents
        that are needed. An upstream parent is needed if none of
        the other parents include the upstream version. The needed
        upstream must already present in the upstream branch before
        calling this method.

        If force_upstream_parent is True then the upstream parent will
        be included, even if another parent is already using that
        upstream. This is for use in cases where the .orig.tar.gz
        is different in two distributions.

        :param version: the Version that we are currently importing.
        :param versions: the list of Versions that are ancestors of
            version, including version itself. Sorted with the latest
            versions first, so version must be the first entry.
        :param force_upstream_parent: if True then an upstream parent
            will be added as the first parent, regardless of what the
            other parents are.
        :return: a list of revision ids that should be the parents when
            importing the specified revision.
        """
        assert version == versions[0], \
            "version is not the first entry of versions"
        parents = self.get_parents(versions)
        need_upstream_parent = True
        if not force_upstream_parent:
            for parent_pair in parents:
                if parent_pair[1].upstream_version == version.upstream_version:
                    need_upstream_parent = False
                    break
        real_parents = [p[2] for p in parents]
        if need_upstream_parent:
            upstream_revids = self.pristine_upstream_source\
                    .version_as_revisions(
                            package, version.upstream_version, tarballs)

            def key(a):
                if a is None:
                    return None
                return a
            for component in sorted(upstream_revids.keys(), key=key):
                if len(real_parents) > 0:
                    real_parents.insert(1, upstream_revids[component])
                else:
                    real_parents = [upstream_revids[component]]
        return real_parents

    def _fetch_upstream_to_branch(self, imported_revids):
        """Fetch the revision from the upstream branch in to the packaging one.
        """
        # Make sure we see any revisions added by the upstream branch
        # since self.tree was locked.
        self.branch.repository.refresh_data()
        for (component, tag, revid) in imported_revids:
            self.branch.fetch(self.pristine_upstream_branch, revid)
        self.pristine_upstream_branch.tags.merge_to(self.branch.tags)

    def import_upstream(self, upstream_part, package, version,
                        upstream_parents, upstream_tarballs,
                        upstream_branch=None, upstream_revisions=None,
                        timestamp=None, author=None, file_ids_from=None,
                        force_pristine_tar=False, committer=None,
                        files_excluded=None):
        """Import an upstream part on to the upstream branch.

        This imports the upstream part of the code and places it on to
        the upstream branch, setting the necessary tags.

        :param upstream_part: the path of a directory containing the
            unpacked upstream part of the source package.
        :param version: upstream version that is being imported
        :param upstream_parents: the parents to give the upstream revision
        :param timestamp: a tuple of (timestamp, timezone) to use for
            the commit, or None to use the current time.
        :return: list with (component, tag, revid) tuples
        """
        # Should we just dump the upstream part on whatever is currently
        # there, or try and pull all of the other upstream versions
        # from lesser branches first? For now we'll just dump it on.
        # TODO: this method needs a lot of work for when we will make
        # the branches writeable by others.
        mutter("Importing upstream version %s from %s with parents %r",
               version, upstream_part, upstream_parents)
        assert self.pristine_upstream_tree is not None, \
            "Can't import upstream with no tree"
        other_branches = self.get_other_branches()
        ret = []
        for (tarball, component, md5) in upstream_tarballs:
            parents = upstream_parents.get(component, [])
            if upstream_revisions is not None:
                revid = upstream_revisions[component]
            else:
                revid = None
            upstream_trees = [
                o.pristine_upstream_branch.basis_tree()
                for o in other_branches]
            target_tree = None
            if upstream_branch is not None:
                if revid is None:
                    # FIXME: This is wrong for component tarballs
                    revid = upstream_branch.last_revision()
                try:
                    self.pristine_upstream_branch.fetch(
                            upstream_branch, revid)
                except NoRoundtrippingSupport:
                    fetch_result = self.pristine_upstream_branch.fetch(
                        upstream_branch, revid, lossy=True)
                    revid = fetch_result.revidmap[revid]
                upstream_branch.tags.merge_to(
                    self.pristine_upstream_branch.tags)
                parents.append(revid)
                target_tree = self.pristine_upstream_branch.repository.revision_tree(
                        revid)
            if file_ids_from is not None:
                upstream_trees = file_ids_from + upstream_trees
            if self.tree:
                self_tree = self.tree
                self_tree.lock_write()  # might also be upstream tree for dh_make
            else:
                self_tree = self.branch.basis_tree()
                self_tree.lock_read()
            if len(parents) > 0:
                parent_revid = parents[0]
            else:
                parent_revid = NULL_REVISION
            self.pristine_upstream_tree.pull(
                self.pristine_upstream_tree.branch,
                overwrite=True, stop_revision=parent_revid)
            if component is None:
                path = upstream_part
            else:
                path = os.path.join(upstream_part, component)
            try:
                import_dir(
                    self.pristine_upstream_tree, path,
                    file_ids_from=[self_tree] + upstream_trees,
                    target_tree=target_tree)
            finally:
                self_tree.unlock()
            if component is None:
                exclude = [
                    tb[1] for tb in upstream_tarballs if tb[1] is not None]
            else:
                exclude = []
            (tag, revid) = self.pristine_upstream_source.import_component_tarball(
                package, version, self.pristine_upstream_tree, parents,
                component, md5, tarball, author=author, timestamp=timestamp,
                exclude=exclude, force_pristine_tar=force_pristine_tar,
                committer=committer, files_excluded=files_excluded,
                reuse_existing=True)
            self.pristine_upstream_branch.generate_revision_history(revid)
            ret.append((component, tag, revid))
            self.branch.fetch(self.pristine_upstream_branch)
            self.branch.tags.set_tag(tag, revid)
        return ret

    def import_upstream_tarballs(self, tarballs, package, version, parents,
                                 upstream_branch=None, upstream_revisions=None,
                                 force_pristine_tar=False):
        """Import an upstream part to the upstream branch.

        :param tarballs: List of tarballs / components to extract
        :param version: The upstream version to import.
        :param parents: The tarball-branch parents to use for the import.
            If an upstream branch is supplied, its automatically added to
            parents.
        :param upstream_branch: An upstream branch to associate with the
            tarball.
        :param upstream_revisions: Upstream revision ids dictionary
        :param md5sum: hex digest of the md5sum of the tarball, if known.
        :return: list with (component, tag, revid) tuples
        """
        with _extract_tarballs_to_tempdir(tarballs) as tarball_dir:
            return self.import_upstream(
                tarball_dir, package, version, parents, tarballs,
                upstream_branch=upstream_branch,
                upstream_revisions=upstream_revisions,
                force_pristine_tar=force_pristine_tar)

    def _mark_native_config(self, native):
        poss_native_tree = self.branch.basis_tree()
        (config_fileid, config_relpath,
         current_config) = self._default_config_for_tree(poss_native_tree)
        current_native = self._is_tree_native(current_config)
        if current_config is not None:
            # Add that back to the current tree
            current_config.filename = self.tree.abspath(config_relpath)
            dir_path = osutils.dirname(current_config.filename)
            if not os.path.exists(dir_path):
                os.mkdir(dir_path)
            current_config.write()
            dirname = osutils.dirname(config_relpath)
            dir_id = poss_native_tree.path2id(dirname)
            if self.tree.supports_setting_file_ids():
                self.tree.add([dirname, config_relpath],
                              ids=[dir_id, config_fileid])
            else:
                self.tree.add([dirname, config_relpath])
        if native != current_native:
            if current_config is None:
                needs_add = True
                if native:
                    current_config = ConfigObj()
                    current_config['BUILDDEB'] = {}
            if current_config is not None:
                if native:
                    current_config['BUILDDEB']['native'] = True
                else:
                    del current_config['BUILDDEB']['native']
                    if len(current_config['BUILDDEB']) == 0:
                        del current_config['BUILDDEB']
                if len(current_config) == 0:
                    self.tree.remove([
                        '.bzr-builddeb',
                        '.bzr-builddeb/default.conf',
                        'debian/bzr-builddeb.conf'],
                        keep_files=False)
                else:
                    current_config.filename = os.path.join(
                        self.tree.basedir, 'debian', 'bzr-builddeb.conf')
                    current_config.write()
                    if needs_add:
                        self.tree.add(['debian', 'debian/bzr-builddeb.conf'])

    def import_debian(self, debian_part, version, parents, md5,
                      native=False, timestamp=None, file_ids_from=None):
        """Import the debian part of a source package.

        :param debian_part: the path of a directory containing the unpacked
            source package.
        :param version: the Version of the source package.
        :param parents: a list of revision ids that should be the
            parents of the imported revision.
        :param md5: the md5 sum reported by the .dsc for
            the .diff.gz part of this source package.
        :param native: whether the package is native.
        :param timestamp: a tuple of (timestamp, timezone) to use for
            the commit, or None to use the current values.
        """
        mutter("Importing debian part for version %s from %s, with parents "
               "%s", str(version), debian_part, str(parents))
        assert self.tree is not None, "Can't import with no tree"
        # First we move the branch to the first parent
        if parents:
            if self.branch.last_revision() == NULL_REVISION:
                parent_revid = parents[0]
                self.tree.pull(
                    self.tree.branch, overwrite=True,
                    stop_revision=parent_revid)
            elif parents[0] != self.branch.last_revision():
                mutter("Adding current tip as parent: %s",
                       self.branch.last_revision())
                parents.insert(0, self.branch.last_revision())
        elif self.branch.last_revision() != NULL_REVISION:
            # We were told to import with no parents. That's not
            # right, so import with the current parent. Should
            # perhaps be fixed in the methods to determine the parents.
            mutter("Told to import with no parents. Adding current tip "
                   "as the single parent")
            parents = [self.branch.last_revision()]
        other_branches = self.get_other_branches()
        debian_trees = [o.branch.basis_tree() for o in other_branches]
        parent_trees = []
        if file_ids_from is not None:
            parent_trees = file_ids_from[:]
        for parent in parents:
            parent_trees.append(self.branch.repository.revision_tree(
                        parent))
        import_dir(
            self.tree, debian_part,
            file_ids_from=parent_trees + debian_trees)
        rules_path = os.path.join(self.tree.basedir, 'debian', 'rules')
        if os.path.isfile(rules_path):
            os.chmod(rules_path,
                     (stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP |
                      stat.S_IROTH | stat.S_IXOTH))
        self.tree.set_parent_ids(parents)
        changelog_path = os.path.join(
            self.tree.basedir, 'debian', 'changelog')
        if os.path.exists(changelog_path):
            changelog = self.get_changelog_from_source(
                self.tree.basedir, max_blocks=1)
        message, authors, thanks, bugs = \
            get_commit_info_from_changelog(changelog, self.branch)
        if message is None:
            message = 'Import packaging changes for version %s' % \
                        (str(version),)
        supports_revprops = (
            self.tree.branch.repository._format.
            supports_custom_revision_properties)
        revprops = {}
        if supports_revprops:
            revprops["deb-md5"] = md5
        if native and supports_revprops:
            revprops['deb-native'] = "True"
        if authors:
            revprops['authors'] = "\n".join(authors)
        if thanks and supports_revprops:
            revprops['deb-thanks'] = "\n".join(thanks)
        if bugs and supports_revprops:
            revprops['bugs'] = "\n".join(bugs)
        timezone = None
        if timestamp is not None:
            timezone = timestamp[1]
            timestamp = timestamp[0]
        self._mark_native_config(native)
        revid = self.tree.commit(
            message, revprops=revprops, timestamp=timestamp, timezone=timezone)
        return self.tag_version(version, revid=revid)

    def upstream_parents(self, package, versions, version):
        """Get the parents for importing a new upstream.

        The upstream parents will be the last upstream version,
        except for some cases when the last version was native.

        :return: the list of revision ids to use as parents when
            importing the specified upstream version.
        """
        parents = []
        first_parent = self.pristine_upstream_branch.last_revision()
        if first_parent != NULL_REVISION:
            parents = [first_parent]
        last_contained_version = self.last_contained_version(versions)
        if last_contained_version is not None:
            # If the last version was native, and was not from the same
            # upstream as a non-native version (i.e. it wasn't a mistaken
            # native -2 version), then we want to add an extra parent.
            if (self.is_version_native(last_contained_version)
                and not self.pristine_upstream_source.has_version(
                    package, last_contained_version.upstream_version)):
                revid = self.revid_of_version(last_contained_version)
                parents.append(revid)
                self.pristine_upstream_branch.fetch(self.branch, revid)
        pull_parents = self.get_parents(versions)
        if ((first_parent == NULL_REVISION and len(pull_parents) > 0)
                or len(pull_parents) > 1):
            if first_parent == NULL_REVISION:
                pull_branch = pull_parents[0][0]
                pull_version = pull_parents[0][1]
            else:
                pull_branch = pull_parents[1][0]
                pull_version = pull_parents[1][1]
            if not pull_branch.is_version_native(pull_version):
                pull_revids = pull_branch.pristine_upstream_source.version_as_revisions(
                    package, pull_version.upstream_version)
                if list(pull_revids.keys()) != [None]:
                    raise MultipleUpstreamTarballsNotSupported()
                pull_revid = pull_revids[None]
                mutter("Initialising upstream from %s, version %s",
                       str(pull_branch), str(pull_version))
                parents.append(pull_revid)
                self.pristine_upstream_branch.fetch(
                        pull_branch.pristine_upstream_branch,
                        pull_revid)
                pull_branch.pristine_upstream_branch.tags.merge_to(
                        self.pristine_upstream_branch.tags)
        # FIXME: What about other versions ?
        return {None: parents}

    def get_changelog_from_source(self, dir, max_blocks=None):
        cl_filename = os.path.join(dir, "debian", "changelog")
        with open(cl_filename, 'rb') as f:
            content = f.read()
        # Older versions of python-debian were accepting various encodings in
        # the changelog. This is not true with 0.1.20ubuntu2 at least which
        # force an 'utf-8' encoding. This leads to failures when trying to
        # parse old changelogs. Using safe_decode() below will fallback to
        # iso-8859-1 (latin_1) in this case.
        content = safe_decode(content)
        cl = Changelog()
        cl.parse_changelog(content, strict=False, max_blocks=max_blocks)
        return cl

    def _fetch_from_branch(self, branch, revid):
        branch.branch.tags.merge_to(self.branch.tags)
        self.branch.fetch(branch.branch, revid)
        if self.pristine_upstream_branch.last_revision() == NULL_REVISION:
            self.pristine_upstream_tree.pull(branch.pristine_upstream_branch)
            branch.pristine_upstream_branch.tags.merge_to(
                self.pristine_upstream_branch.tags)

    def _import_normal_package(
            self, package, version, versions, debian_part, md5,
            upstream_part, upstream_tarballs, timestamp=None, author=None,
            file_ids_from=None, pull_debian=True, force_pristine_tar=False):
        """Import a source package.

        :param package: Package name
        :param version: Full Debian version
        :param versions: Safe versions from changelog
        :param debian_part: Path to extracted directory with Debian changes
        :param unextracted_debian_md5: MD5 sum of unextracted Debian
            diff/tarball
        :param upstream_part: Extracted upstream directory
        :param upstream_tarballs:
            List of tuples with (upstream tarfile, md5sum)
        :param timestamp: Version timestamp according to changelog
        :param author: Author according to changelog
        :param file_ids_from: Sequence of trees to take file ids from
        :param pull_debian: Whether to pull from the Debian branch
        """
        pull_branch = None
        if pull_debian:
            pull_branch = self.branch_to_pull_version_from(version, md5)
        if pull_branch is not None:
            if self.branch_to_pull_upstream_from(
                    package, version.upstream_version,
                    upstream_tarballs) is None:
                pull_branch = None
        if pull_branch is not None:
            self.pull_version_from_branch(pull_branch, package, version)
        else:
            # We need to import at least the diff, possibly upstream.
            # Work out if we need the upstream part first.
            imported_upstream = False
            if not self.pristine_upstream_source.has_version(
                    package, version.upstream_version):
                up_pull_branch = \
                    self.branch_to_pull_upstream_from(
                        package, version.upstream_version, upstream_tarballs)
                if up_pull_branch is not None:
                    self.pull_upstream_from_branch(
                        up_pull_branch, package, version.upstream_version)
                else:
                    imported_upstream = True
                    # Check whether we should pull first if this initialises
                    # from another branch:
                    upstream_parents = self.upstream_parents(
                        package, versions, version.upstream_version)
                    imported_revids = self.import_upstream(
                        upstream_part, package, version.upstream_version,
                        upstream_parents, upstream_tarballs=upstream_tarballs,
                        timestamp=timestamp, author=author,
                        file_ids_from=file_ids_from,
                        force_pristine_tar=force_pristine_tar)
                    self._fetch_upstream_to_branch(imported_revids)
            else:
                mutter("We already have the needed upstream part")
            parents = self.get_parents_with_upstream(
                package, version, versions, upstream_tarballs,
                force_upstream_parent=imported_upstream)
            # Now we have the list of parents we need to import the .diff.gz
            return self.import_debian(
                debian_part, version, parents, md5, timestamp=timestamp,
                file_ids_from=file_ids_from)

    def get_native_parents(self, version, versions):
        last_contained_version = self.last_contained_version(versions)
        if last_contained_version is None:
            parents = []
        else:
            parents = [self.revid_of_version(last_contained_version)]
        missing_versions = self.missing_versions(versions)
        for branch in list(reversed(self.get_lesser_branches())) + self.get_greater_branches():
            merged, missing_versions = \
                branch.contained_versions(missing_versions)
            if merged:
                revid = branch.revid_of_version(merged[0])
                parents.append(revid)
                # FIXME: should this really be here?
                self._fetch_from_branch(branch, revid)
        if (self.branch.last_revision() != NULL_REVISION
                and not self.branch.last_revision() in parents):
            parents.insert(0, self.branch.last_revision())
        return parents

    def _import_native_package(self, package, version, versions, debian_part,
                               md5, timestamp=None, file_ids_from=None,
                               pull_debian=True):
        pull_branch = None
        if pull_debian:
            pull_branch = self.branch_to_pull_version_from(version, md5)
        if pull_branch is not None:
            return self.pull_version_from_branch(
                pull_branch, package, version, native=True)
        else:
            parents = self.get_native_parents(version, versions)
            return self.import_debian(
                debian_part, version, parents, md5, native=True,
                timestamp=timestamp, file_ids_from=file_ids_from)

    def _get_safe_versions_from_changelog(self, cl):
        versions = []
        for block in cl._blocks:
            try:
                versions.append(block.version)
            except VersionError:
                break
        return versions

    def import_package(self, dsc_filename, use_time_from_changelog=True,
                       file_ids_from=None, pull_debian=True,
                       force_pristine_tar=False):
        """Import a source package.

        :param dsc_filename: a path to a .dsc file for the version
            to be imported.
        :param use_time_from_changelog: whether to use the current time or
            the one from the last changelog entry.
        """
        with open(dsc_filename) as f:
            dsc = deb822.Dsc(f.read())
        version = Version(dsc['Version'])
        with extract(dsc_filename, dsc) as extractor:
            cl = self.get_changelog_from_source(extractor.extracted_debianised)
            timestamp = None
            author = None
            if use_time_from_changelog and len(cl._blocks) > 0:
                raw_timestamp = cl.date
                import email.utils
                time_tuple = email.utils.parsedate_tz(raw_timestamp)
                if time_tuple is not None:
                    timestamp = (calendar.timegm(
                                 time_tuple[:9]) - time_tuple[9],
                                 time_tuple[9])
                author = safe_decode(cl.author)
            versions = self._get_safe_versions_from_changelog(cl)
            if self.has_version(version):
                raise AssertionError(
                        "Trying to import version %s again" % str(version))
            # TODO: check that the versions list is correctly ordered,
            # as some methods assume that, and it's not clear what
            # should happen if it isn't.

            if extractor.extracted_upstream is not None:
                return self._import_normal_package(
                    dsc['Source'], version, versions,
                    extractor.extracted_debianised,
                    extractor.unextracted_debian_md5,
                    extractor.extracted_upstream, extractor.upstream_tarballs,
                    timestamp=timestamp, author=author,
                    file_ids_from=file_ids_from, pull_debian=pull_debian,
                    force_pristine_tar=force_pristine_tar)
            else:
                return self._import_native_package(
                    dsc['Source'], version, versions,
                    extractor.extracted_debianised,
                    extractor.unextracted_debian_md5, timestamp=timestamp,
                    file_ids_from=file_ids_from, pull_debian=pull_debian)

    def extract_upstream_tree(self, upstream_tips, basedir):
        """Extract upstream_tip to a tempdir as a working tree."""
        # TODO: should stack rather than trying to use the repository,
        # as that will be more efficient.
        to_location = os.path.join(basedir, "upstream")
        # Use upstream_branch if it has been set, otherwise self.branch.
        source_branch = self.pristine_upstream_branch or self.branch
        assert list(upstream_tips.keys()) == [None], \
            "Upstream tips: %r" % list(upstream_tips.keys())
        # TODO(jelmer): Use colocated branches rather than creating a copy.
        dir_to = source_branch.controldir.sprout(
            to_location, revision_id=upstream_tips[None],
            accelerator_tree=self.tree)
        try:
            self.pristine_upstream_tree = dir_to.open_workingtree()
        except NoWorkingTree:
            # Handle shared treeless repo's.
            self.pristine_upstream_tree = dir_to.create_workingtree()
        self.pristine_upstream_branch = self.pristine_upstream_tree.branch
        self.pristine_upstream_branch.get_config_stack().set(
            'branch.fetch_tags', True)

    def create_empty_upstream_tree(self, basedir):
        to_location = os.path.join(basedir, "upstream")
        to_transport = get_transport(to_location)
        to_transport.ensure_base()
        source_branch = self.pristine_upstream_branch or self.branch
        format = source_branch.controldir._format
        try:
            existing_controldir = controldir.ControlDir.open_from_transport(
                    to_transport)
        except NotBranchError:
            # really a NotControlDirError error...
            create_branch = controldir.ControlDir.create_branch_convenience
            branch = create_branch(
                to_transport.base, format=format,
                possible_transports=[to_transport])
        else:
            if existing_controldir.has_branch():
                raise AlreadyBranchError(to_location)
            else:
                branch = existing_controldir.create_branch()
                existing_controldir.create_workingtree()
        self.pristine_upstream_branch = branch
        self.pristine_upstream_tree = branch.controldir.open_workingtree()
        if self.pristine_upstream_tree.supports_setting_file_ids():
            if self.tree:
                root_id = self.tree.path2id('')
            else:
                tip = self.branch.basis_tree()
                with tip.lock_read():
                    root_id = tip.path2id('')
            if root_id:
                self.pristine_upstream_tree.set_root_id(root_id)

    def _export_previous_upstream_tree(
            self, package, previous_version, tempdir):
        try:
            upstream_tips = self.pristine_upstream_source.version_as_revisions(
                    package, previous_version)
        except PackageVersionNotPresent:
            raise PreviousVersionTagMissing(
                previous_version,
                self.pristine_upstream_source.tag_name(previous_version))
        self.extract_upstream_tree(upstream_tips, tempdir)

    def has_merged_upstream_revisions(
            self, this_revision, upstream_repository, upstream_revisions):
        graph = self.branch.repository.get_graph(
            other_repository=upstream_repository)
        return all(graph.is_ancestor(upstream_revision, this_revision)
                   for upstream_revision in upstream_revisions.values())

    def merge_upstream(self, tarball_filenames, package, version,
                       previous_version, upstream_branch=None,
                       upstream_revisions=None, merge_type=None, force=False,
                       force_pristine_tar=False, committer=None,
                       files_excluded=None):
        with tempfile.TemporaryDirectory(dir=os.path.join(self.tree.basedir, '..')) as tempdir:
            if previous_version is not None:
                self._export_previous_upstream_tree(
                    package, previous_version, tempdir)
            else:
                self.create_empty_upstream_tree(tempdir)
            if self.pristine_upstream_source.has_version(package, version):
                raise UpstreamAlreadyImported(version)
            if upstream_branch is not None:
                upstream_branch.lock_read()
            try:
                if upstream_branch is not None:
                    if upstream_revisions is None:
                        upstream_revisions = {None: upstream_branch.last_revision()}
                    if (not force and
                            self.has_merged_upstream_revisions(self.branch.last_revision(), upstream_branch.repository, upstream_revisions)):
                        raise UpstreamBranchAlreadyMerged
                upstream_tarballs = [
                    (os.path.abspath(fn), component, md5sum_filename(fn)) for
                    (fn, component) in
                    tarball_filenames]
                with _extract_tarballs_to_tempdir(upstream_tarballs) as tarball_dir:
                    # FIXME: should use upstream_parents()?
                    parents = {None: []}
                    if self.pristine_upstream_branch.last_revision() != NULL_REVISION:
                        parents = {None: [self.pristine_upstream_branch.last_revision()]}
                    imported_revids = self.import_upstream(
                        tarball_dir, package, version, parents,
                        upstream_tarballs=upstream_tarballs,
                        upstream_branch=upstream_branch,
                        upstream_revisions=upstream_revisions,
                        force_pristine_tar=force_pristine_tar,
                        committer=committer, files_excluded=files_excluded)
                    self._fetch_upstream_to_branch(imported_revids)
                if self.branch.last_revision() != NULL_REVISION:
                    try:
                        conflicts = self.tree.merge_from_branch(
                                self.pristine_upstream_branch,
                                merge_type=merge_type)
                    except UnrelatedBranches:
                        # Bug lp:515367 where the first upstream tarball is
                        # missing a proper history link and a criss-cross merge
                        # then recurses and finds no deeper ancestor.
                        # Use the previous upstream import as the from revision
                        if len(parents[None]) == 0:
                            from_revision = NULL_REVISION
                        else:
                            from_revision = parents[None][0]
                        conflicts = self.tree.merge_from_branch(
                            self.pristine_upstream_branch,
                            merge_type=merge_type, from_revision=from_revision)
                else:
                    # Pull so that merge-upstream allows you to start a branch
                    # from upstream tarball.
                    conflicts = 0
                    self.tree.pull(self.pristine_upstream_branch)
                self.pristine_upstream_branch.tags.merge_to(self.branch.tags)
                return conflicts
            finally:
                if upstream_branch is not None:
                    upstream_branch.unlock()


@contextmanager
def _extract_tarballs_to_tempdir(tarballs):
    with tempfile.TemporaryDirectory() as tempdir:
        extract_orig_tarballs(
            [(fn, component) for (fn, component, md5) in tarballs],
            tempdir)
        yield tempdir