-
Notifications
You must be signed in to change notification settings - Fork 0
/
branch.py
1072 lines (894 loc) · 40.3 KB
/
branch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Loom, a plugin for bzr to assist in developing focused patches.
# Copyright (C) 2006 - 2008 Canonical Limited.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation.
#
# 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
"""The Loom Branch format.
A Loom branch extends the behaviour of various methods to manage and propogate
the Loom specific data. In the future it would be nice to have this data
registered with a normal bzr branch. That said, the branch format should still
be specific to loom, to ensure people have the loom plugin when working on a
loom branch.
"""
from __future__ import absolute_import
from io import BytesIO
import breezy.branch
from breezy import (
commit as _mod_commit,
controldir,
errors,
symbol_versioning,
trace,
ui,
urlutils,
)
from breezy.bzr import (
branch as _mod_bzrbranch,
bzrdir,
fetch as _mod_fetch,
inventory as _mod_inventory,
inventorytree as _mod_inventorytree,
remote,
)
from breezy.revision import is_null, NULL_REVISION
from breezy.plugins.loom import (
loom_io,
loom_state,
require_loom_branch,
NotALoom,
)
EMPTY_REVISION = b'empty:'
from breezy.bzr.fullhistory import BzrBranch5, BzrBranchFormat5
def create_thread(loom, thread_name):
"""Create a thread in the branch loom called thread."""
require_loom_branch(loom)
with loom.lock_write():
loom.new_thread(thread_name, loom.nick)
loom._set_nick(thread_name)
class AlreadyLoom(errors.BzrError):
_fmt = """Loom %(loom)s is already a loom."""
def __init__(self, loom):
errors.BzrError.__init__(self)
self.loom = loom
def loomify(branch):
"""Convert branch to a loom.
If branch is a BzrBranch5 branch, it will become a LoomBranch.
"""
with branch.lock_write():
try:
require_loom_branch(branch)
except NotALoom:
pass
else:
raise AlreadyLoom(branch)
try:
format = {
BzrBranchFormat5: BzrBranchLoomFormat1,
_mod_bzrbranch.BzrBranchFormat6: BzrBranchLoomFormat6,
_mod_bzrbranch.BzrBranchFormat7: BzrBranchLoomFormat7,
}[branch._format.__class__]()
except KeyError:
raise UnsupportedBranchFormat(branch._format)
format.take_over(branch)
class LoomThreadError(errors.BzrError):
"""Base class for Loom-Thread errors."""
def __init__(self, branch, thread):
errors.BzrError.__init__(self)
self.branch = branch
self.thread = thread
class UnrecordedRevision(errors.BzrError):
_fmt = """The revision %(revision_id)s is not recorded in the loom %(branch)s."""
def __init__(self, branch, revision_id):
errors.BzrError.__init__(self)
self.branch = branch
self.revision_id = revision_id
class UnsupportedBranchFormat(errors.BzrError):
_fmt = """The branch format %(format)s is not supported by loomify."""
def __init__(self, format):
self.format = format
class DuplicateThreadName(LoomThreadError):
_fmt = """The thread %(thread)s already exists in branch %(branch)s."""
class UnchangedThreadRevision(LoomThreadError):
_fmt = """No new commits to record on thread %(thread)s."""
class NoSuchThread(LoomThreadError):
_fmt = """No such thread '%(thread)s'."""
class NoLowerThread(errors.BzrError):
_fmt = """No lower thread exists."""
class CannotCombineOnLastThread(NoLowerThread):
_fmt = """Cannot combine threads on the bottom thread."""
class LoomMetaTree(_mod_inventorytree.InventoryTree):
"""A 'tree' object that is used to commit the loom meta branch."""
def __init__(self, loom_meta_ie, loom_stream, loom_sha1):
"""Create a Loom Meta Tree.
:param loom_content_lines: the unicode content to be used for the loom.
"""
self._inventory = _mod_inventory.Inventory()
self._inventory.add(loom_meta_ie)
self._loom_stream = loom_stream
self._loom_sha1 = loom_sha1
def get_file(self, path, file_id=None):
"""Get the content of file_id from this tree.
As usual this must be for the single existing file 'loom'.
"""
return self._loom_stream
def get_file_with_stat(self, path, file_id=None):
return (self.get_file(path, file_id), None)
def get_file_sha1(self, path, file_id=None, stat_value=None):
"""Get the sha1 for a file.
This tree only has one file, so it MUST be present!
"""
assert path == 'loom'
assert file_id is None or file_id == b'loom_meta_tree'
return self._loom_sha1
def is_executable(self, path, file_id=None):
"""get the executable status for file_id.
Nothing in a LoomMetaTree is executable.
"""
return False
def _comparison_data(self, entry, path):
if entry is None:
return None, False, None
return entry.kind, entry.executable, None
class LoomSupport(object):
"""Loom specific logic called into from Branch."""
def _adjust_nick_after_changing_threads(self, threads, current_index):
"""Adjust the branch nick when we may have removed a current thread.
:param threads: The current threads.
:param position: The position in the old threads self.nick had.
"""
threads_dict = dict(thread[0:2] for thread in threads)
if self.nick not in threads_dict:
if not len(threads):
# all threads gone
# revert to being a normal branch: revert to an empty revision
# history.
self.generate_revision_history(breezy.revision.NULL_REVISION)
return
# TODO, calculate the offset of removed threads.
# i.e. if there are ten threads removed, and current_index is 5,
# if 4 of the ten removed were 2,3,4,5, then the new index should
# be 2.
if len(threads) <= current_index:
# removed the end
# take the new end thread
self._set_nick(threads[-1][0])
new_rev = threads[-1][1]
if new_rev == EMPTY_REVISION:
new_rev = breezy.revision.NULL_REVISION
self.generate_revision_history(new_rev)
return
# non-end thread removed.
self._set_nick(threads[current_index][0])
new_rev = threads[current_index][1]
if new_rev == EMPTY_REVISION:
new_rev = breezy.revision.NULL_REVISION
self.generate_revision_history(new_rev)
elif self.last_revision() != threads_dict[self.nick]:
new_rev = threads_dict[self.nick]
if new_rev == EMPTY_REVISION:
new_rev = breezy.revision.NULL_REVISION
self.generate_revision_history(new_rev)
def bind(self, other):
"""Bind the local branch the other branch.
:param other: The branch to bind to
:type other: Branch
"""
# Looms are not currently bindable.
raise errors.UpgradeRequired(self.base)
def clone(self, to_controldir, revision_id=None, repository_policy=None, name=None,
tag_selector=None):
"""Clone the branch into to_controldir.
This differs from the base clone by cloning the loom, setting the
current nick to the top of the loom, not honouring any branch format
selection on the target controldir, and ensuring that the format of
the created branch is stacking compatible.
"""
# If the target is a stackable repository, force-upgrade the
# output loom format
if (isinstance(to_controldir, bzrdir.BzrDirMeta1) and
to_controldir._format.repository_format.supports_external_lookups):
format = BzrBranchLoomFormat7()
else:
format = self._format
result = format.initialize(to_controldir, name=name)
if repository_policy is not None:
repository_policy.configure_branch(result)
with self.lock_read():
breezy.branch.InterBranch.get(self, result).copy_content_into(
revision_id=revision_id, tag_selector=tag_selector)
return result
def _get_checkout_format(self, lightweight=False):
"""Checking out a Loom gets a regular branch for now.
This is a short term measure to get to an all-tests passing status.
"""
format = self.repository.controldir.checkout_metadir()
format.set_branch_format(breezy.branch.BzrBranchFormat6())
return format
def get_loom_state(self):
"""Get the current loom state object."""
# TODO: cache the loom state during the transaction lifetime.
current_content = self._transport.get('last-loom')
reader = loom_io.LoomStateReader(current_content)
state = loom_state.LoomState(reader)
return state
def get_old_bound_location(self):
"""Return the URL of the branch we used to be bound to."""
# No binding for looms yet.
raise errors.UpgradeRequired(self.base)
def get_threads(self, rev_id):
"""Return the threads from a loom revision.
:param rev_id: A specific loom revision to retrieve.
:return: a list of threads. e.g. [('threadname', 'last_revision')]
"""
if rev_id is None:
symbol_versioning.warn('NULL_REVISION should be used for the null'
' revision instead of None, as of bzr 0.90.',
DeprecationWarning, stacklevel=2)
if is_null(rev_id):
return []
content = self._loom_content(rev_id)
return self._parse_loom(content)
def export_threads(self, root_transport):
"""Export the threads in this loom as branches.
:param root_transport: Transport for the directory to place branches
under. Defaults to branch root transport.
"""
threads = self.get_loom_state().get_threads()
for thread_name, thread_revision, _parents in threads:
thread_transport = root_transport.clone(thread_name)
user_location = urlutils.unescape_for_display(
thread_transport.base, 'utf-8')
try:
control_dir = controldir.ControlDir.open(thread_transport.base,
possible_transports=[thread_transport])
tree, branch = control_dir._get_tree_branch()
except errors.NotBranchError:
trace.note('Creating branch at %s' % user_location)
branch = controldir.ControlDir.create_branch_convenience(
thread_transport.base,
possible_transports=[thread_transport])
tree, branch = branch.controldir.open_tree_or_branch(
thread_transport.base)
else:
if thread_revision == branch.last_revision():
trace.note('Skipping up-to-date branch at %s'
% user_location)
continue
else:
trace.note('Updating branch at %s' % user_location)
if tree is not None:
tree.pull(self, stop_revision=thread_revision)
else:
branch.pull(self, stop_revision=thread_revision)
def _loom_content(self, rev_id):
"""Return the raw formatted content of a loom as a series of lines.
:param rev_id: A specific loom revision to retrieve.
Currently the disk format is:
----
Loom meta 1
revisionid threadname_in_utf8
----
if revisionid is empty:, this is a new, empty branch.
"""
tree = self.repository.revision_tree(rev_id)
with tree.get_file('loom') as f:
lines = f.read().split(b'\n')
assert lines[0] == b'Loom meta 1'
return lines[1:-1]
def loom_parents(self):
"""Return the current parents to use in the next commit."""
return self.get_loom_state().get_parents()
def new_thread(self, thread_name, after_thread=None):
"""Add a new thread to this branch called 'thread_name'."""
state = self.get_loom_state()
threads = state.get_threads()
if thread_name in state.get_threads_dict():
raise DuplicateThreadName(self, thread_name)
assert after_thread is None or after_thread in state.get_threads_dict()
if after_thread is None:
insertion_point = len(threads)
else:
insertion_point = state.thread_index(after_thread) + 1
if insertion_point == 0:
revision_for_thread = self.last_revision()
else:
revision_for_thread = threads[insertion_point - 1][1]
if is_null(revision_for_thread):
revision_for_thread = EMPTY_REVISION
threads.insert(
insertion_point,
(thread_name,
revision_for_thread,
[None] * len(state.get_parents())
)
)
state.set_threads(threads)
self._set_last_loom(state)
def _parse_loom(self, content):
"""Parse the body of a loom file."""
result = []
for line in content:
rev_id, name = line.split(b' ', 1)
result.append((name.decode('utf-8'), rev_id))
return result
def _loom_get_nick(self):
return self._get_nick(local=True)
def _rename_thread(self, nick):
"""Rename the current thread to nick."""
state = self.get_loom_state()
threads = state.get_threads()
if not len(threads):
# No threads at all - probably a default initialised loom in the
# test suite.
return self._set_nick(nick)
current_index = state.thread_index(self.nick)
threads[current_index] = (nick,) + threads[current_index][1:]
state.set_threads(threads)
self._set_last_loom(state)
# Preserve default behavior: set the branch nick
self._set_nick(nick)
nick = property(_loom_get_nick, _rename_thread)
def heads_to_fetch(self):
"""See Branch.heads_to_fetch."""
# The base implementation returns ([tip], tags)
must_fetch, should_fetch = super(LoomSupport, self).heads_to_fetch()
# Add each thread's content to must_fetch
must_fetch.update(
thread_rev for thread_name, thread_rev, thread_parents in
self.get_loom_state().get_threads())
must_fetch.discard(EMPTY_REVISION)
must_fetch.discard(breezy.revision.NULL_REVISION)
return must_fetch, should_fetch
def push(self, target, overwrite=False, stop_revision=None, lossy=False,
_override_hook_source_branch=None):
# Not ideal, but see the issues raised on bazaar@lists.canonical.com
# about the push api needing work.
with self.lock_read():
if not isinstance(target, LoomSupport):
return super(LoomSupport, self).push(target, overwrite,
stop_revision, lossy=lossy, _override_hook_source_branch=None)
if lossy:
raise errors.LossyPushToSameVCS(self, target)
return _Pusher(self, target).transfer(overwrite, stop_revision,
run_hooks=True)
def record_loom(self, commit_message):
"""Perform a 'commit' to the loom branch.
:param commit_message: The commit message to use when committing.
"""
with self.lock_write():
state = self.get_loom_state()
parents = state.get_parents()
old_threads = self.get_threads(state.get_basis_revision_id())
threads = state.get_threads()
# check the semantic value, not the serialised value for equality.
if old_threads == threads:
raise _mod_commit.PointlessCommit
builder = self.get_commit_builder(parents)
loom_ie = _mod_inventory.make_entry(
'file', 'loom', _mod_inventory.ROOT_ID, b'loom_meta_tree')
writer = loom_io.LoomWriter()
loom_stream = BytesIO()
new_threads = [thread[0:2] for thread in threads]
loom_sha1 = writer.write_threads(new_threads, loom_stream)
loom_stream.seek(0)
loom_tree = LoomMetaTree(loom_ie, loom_stream, loom_sha1)
try:
basis_revid = parents[0]
except IndexError:
basis_revid = breezy.revision.NULL_REVISION
for unused in builder.record_iter_changes(
loom_tree, basis_revid,
loom_tree.iter_changes(self.repository.revision_tree(basis_revid))):
pass
builder.finish_inventory()
rev_id = builder.commit(commit_message)
state.set_parents([rev_id])
state.set_threads((thread + ([thread[1]],) for thread in new_threads))
self._set_last_loom(state)
return rev_id
def record_thread(self, thread_name, revision_id):
"""Record an updated version of an existing thread.
:param thread_name: the thread to record.
:param revision_id: the revision it is now at. This should be a child
of the next lower thread.
"""
with self.lock_write():
state = self.get_loom_state()
threads = state.get_threads()
assert thread_name in state.get_threads_dict()
if is_null(revision_id):
revision_id = EMPTY_REVISION
for position, (name, rev, parents) in enumerate(threads):
if name == thread_name:
if revision_id == rev:
raise UnchangedThreadRevision(self, thread_name)
threads[position] = (name, revision_id, parents)
state.set_threads(threads)
self._set_last_loom(state)
def remove_thread(self, thread_name):
"""Remove thread from the current loom.
:param thread_name: The thread to remove.
"""
with self.lock_write():
state = self.get_loom_state()
threads = state.get_threads()
current_index = state.thread_index(thread_name)
del threads[current_index]
state.set_threads(threads)
self._set_last_loom(state)
def revert_loom(self):
"""Revert the loom to be the same as the basis loom."""
with self.lock_write():
state = self.get_loom_state()
# get the current position
position = state.thread_index(self.nick)
# reset the current threads
basis_threads = self.get_threads(state.get_basis_revision_id())
state.set_threads(
(thread + ([thread[1]],) for thread in basis_threads)
)
basis_rev_id = state.get_basis_revision_id()
# reset the parents list to just the basis.
if basis_rev_id is not None:
state.set_parents([basis_rev_id])
self._adjust_nick_after_changing_threads(state.get_threads(), position)
self._set_last_loom(state)
def revert_thread(self, thread):
"""Revert a single thread.
:param thread: the thread to restore to its state in
the basis. If it was not present in the basis it
will be removed from the current loom.
"""
with self.lock_write():
state = self.get_loom_state()
parents = state.get_parents()
threads = state.get_threads()
position = state.thread_index(thread)
basis_threads = self.get_threads(state.get_basis_revision_id())
if thread in dict(basis_threads):
basis_rev = dict(basis_threads)[thread]
threads[position] = (thread, basis_rev, threads[position][2])
else:
del threads[position]
state.set_threads(threads)
self._set_last_loom(state)
# adjust the nickname to be valid
self._adjust_nick_after_changing_threads(threads, position)
def _set_last_loom(self, state):
"""Record state to the last-loom control file."""
stream = BytesIO()
writer = loom_io.LoomStateWriter(state)
writer.write(stream)
stream.seek(0)
self._transport.put_file('last-loom', stream)
def unlock(self):
"""Unlock the loom after a lock.
If at the end of the lock, the current revision in the branch is not
recorded correctly in the loom, an automatic record is attempted.
"""
if (self.control_files._lock_count==1 and
self.control_files._lock_mode=='w'):
# about to release the lock
state = self.get_loom_state()
threads = state.get_threads()
if len(threads):
# looms are enabled:
lastrev = self.last_revision()
if is_null(lastrev):
lastrev = EMPTY_REVISION
if dict(state.get_threads_dict())[self.nick][0] != lastrev:
self.record_thread(self.nick, lastrev)
super(LoomSupport, self).unlock()
class _Puller(object):
# XXX: Move into InterLoomBranch.
def __init__(self, source, target):
self.target = target
self.source = source
# If _Puller has been created, we need real branch objects.
self.real_target = self.unwrap_branch(target)
self.real_source = self.unwrap_branch(source)
def unwrap_branch(self, branch):
if isinstance(branch, remote.RemoteBranch):
branch._ensure_real()
return branch._real_branch
return branch
def prepare_result(self, _override_hook_target):
result = self.make_result()
result.source_branch = self.source
result.target_branch = _override_hook_target
if result.target_branch is None:
result.target_branch = self.target
# cannot bind currently
result.local_branch = None
result.master_branch = self.target
result.old_revno, result.old_revid = self.target.last_revision_info()
return result
def finish_result(self, result):
result.new_revno, result.new_revid = self.target.last_revision_info()
def do_hooks(self, result, run_hooks):
self.finish_result(result)
# get the final result object details
if run_hooks:
for hook in self.post_hooks():
hook(result)
return result
@staticmethod
def make_result():
return breezy.branch.PullResult()
@staticmethod
def post_hooks():
return breezy.branch.Branch.hooks['post_pull']
def plain_transfer(self, result, run_hooks, stop_revision, overwrite):
# no thread commits ever
# just pull the main branch.
new_rev = stop_revision
if new_rev is None:
new_rev = self.source.last_revision()
if new_rev == EMPTY_REVISION:
new_rev = breezy.revision.NULL_REVISION
fetch_spec = self.build_fetch_spec(stop_revision)
self.target.repository.fetch(self.source.repository,
fetch_spec=fetch_spec)
self.target.generate_revision_history(new_rev, self.target.last_revision(),
self.source)
tag_ret = self.source.tags.merge_to(self.target.tags)
if isinstance(tag_ret, tuple):
result.tag_updates, result.tag_conflicts = tag_ret
else:
result.tag_conflicts = tag_ret
# get the final result object details
self.do_hooks(result, run_hooks)
return result
def build_fetch_spec(self, stop_revision):
factory = _mod_fetch.FetchSpecFactory()
factory.source_branch = self.source
factory.source_repo = self.source.repository
factory.source_branch_stop_revision_id = stop_revision
factory.target_repo = self.target.repository
factory.target_repo_kind = _mod_fetch.TargetRepoKinds.PREEXISTING
return factory.make_fetch_spec()
def transfer(self, overwrite, stop_revision, run_hooks=True,
possible_transports=None, _override_hook_target=None, local=False):
"""Implementation of push and pull"""
if local:
raise errors.LocalRequiresBoundBranch()
# pull the loom, and position our
pb = ui.ui_factory.nested_progress_bar()
try:
result = self.prepare_result(_override_hook_target)
with self.target.lock_write(), self.source.lock_read():
source_state = self.real_source.get_loom_state()
source_parents = source_state.get_parents()
if not source_parents:
return self.plain_transfer(result, run_hooks,
stop_revision, overwrite)
# pulling a loom
# the first parent is the 'tip' revision.
my_state = self.target.get_loom_state()
source_loom_rev = source_state.get_parents()[0]
if not overwrite:
# is the loom compatible?
if len(my_state.get_parents()) > 0:
graph = self.source.repository.get_graph()
if not graph.is_ancestor(my_state.get_parents()[0],
source_loom_rev):
raise errors.DivergedBranches(
self.target, self.source)
# fetch the loom content
self.target.repository.fetch(self.source.repository,
revision_id=source_loom_rev)
# get the threads for the new basis
threads = self.target.get_threads(
source_state.get_basis_revision_id())
# stopping at from our repository.
revisions = [rev for name,rev in threads]
# fetch content for all threads and tags.
fetch_spec = self.build_fetch_spec(stop_revision)
self.target.repository.fetch(self.source.repository,
fetch_spec=fetch_spec)
# set our work threads to match (this is where we lose data if
# there are local mods)
my_state.set_threads(
(thread + ([thread[1]],) for thread in threads)
)
# and the new parent data
my_state.set_parents([source_loom_rev])
# and save the state.
self.target._set_last_loom(my_state)
# set the branch nick.
self.target._set_nick(threads[-1][0])
# and position the branch on the top loom
new_rev = threads[-1][1]
if new_rev == EMPTY_REVISION:
new_rev = breezy.revision.NULL_REVISION
self.target.generate_revision_history(new_rev)
# merge tags
tag_ret = self.source.tags.merge_to(self.target.tags)
if isinstance(tag_ret, tuple):
result.tag_updates, tag_conflicts = tag_ret
else:
result.tag_conflicts = tag_ret
self.do_hooks(result, run_hooks)
return result
finally:
pb.finished()
class _Pusher(_Puller):
@staticmethod
def make_result():
return breezy.branch.BranchPushResult()
@staticmethod
def post_hooks():
return breezy.branch.Branch.hooks['post_push']
class LoomBranch(LoomSupport, BzrBranch5):
"""The Loom branch.
A mixin is used as the easiest migration path to support branch6. A
delegated object may well be cleaner.
"""
class LoomBranch6(LoomSupport, _mod_bzrbranch.BzrBranch6):
"""Branch6 Loom branch.
A mixin is used as the easiest migration path to support branch6. A
delegated object may well be cleaner.
"""
class LoomBranch7(LoomSupport, _mod_bzrbranch.BzrBranch7):
"""Branch6 Loom branch.
A mixin is used as the easiest migration path to support branch7.
A rewrite would be preferable, but a stackable loom format is needed
quickly.
"""
class LoomFormatMixin(object):
"""Support code for Loom formats."""
# A mixin is not ideal because it is tricky to test, but it seems to be the
# best solution for now.
def initialize(self, a_controldir, name=None, repository=None,
append_revisions_only=None):
"""Create a branch of this format in a_controldir."""
super(LoomFormatMixin, self).initialize(a_controldir, name=name,
repository=repository,
append_revisions_only=append_revisions_only)
branch_transport = a_controldir.get_branch_transport(self)
files = []
state = loom_state.LoomState()
writer = loom_io.LoomStateWriter(state)
state_stream = BytesIO()
writer.write(state_stream)
state_stream.seek(0)
files.append(('last-loom', state_stream))
control_files = breezy.lockable_files.LockableFiles(
branch_transport, 'lock', breezy.lockdir.LockDir)
control_files.lock_write()
try:
for filename, stream in files:
branch_transport.put_file(filename, stream)
finally:
control_files.unlock()
return self.open(a_controldir, _found=True, name=name)
def open(self, a_controldir, name=None, _found=False, ignore_fallbacks=False,
found_repository=None, possible_transports=None):
"""Return the branch object for a_controldir
_found is a private parameter, do not use it. It is used to indicate
if format probing has already be done.
:param name: The 'colocated branches' name for the branch to open.
"""
if name is None:
name = a_controldir._get_selected_branch()
if not _found:
format = breezy.branch.BranchFormat.find_format(a_controldir, name=name)
assert format.__class__ == self.__class__
transport = a_controldir.get_branch_transport(None, name=name)
control_files = breezy.lockable_files.LockableFiles(
transport, 'lock', breezy.lockdir.LockDir)
if found_repository is None:
found_repository = a_controldir.find_repository()
return self._branch_class()(_format=self,
_control_files=control_files,
a_controldir=a_controldir,
_repository=found_repository,
ignore_fallbacks=ignore_fallbacks,
name=name)
def take_over(self, branch):
"""Take an existing breezy branch over into Loom format.
This currently cannot convert branches to Loom format unless they are
in Branch 5 format.
The conversion takes effect when the branch is next opened.
"""
assert branch._format.__class__ is self._parent_classs
branch._transport.put_bytes('format', self.get_format_string())
state = loom_state.LoomState()
writer = loom_io.LoomStateWriter(state)
state_stream = BytesIO()
writer.write(state_stream)
state_stream.seek(0)
branch._transport.put_file('last-loom', state_stream)
class BzrBranchLoomFormat1(LoomFormatMixin, BzrBranchFormat5):
"""Loom's first format.
This format is an extension to BzrBranchFormat5 with the following changes:
- a loom-revision file.
The loom-revision file has a revision id in it which points into the loom
data branch in the repository.
This format is new in the loom plugin.
"""
def _branch_class(self):
return LoomBranch
_parent_classs = BzrBranchFormat5
@classmethod
def get_format_string(cls):
"""See BranchFormat.get_format_string()."""
return b"Bazaar-NG Loom branch format 1\n"
def get_format_description(self):
"""See BranchFormat.get_format_description()."""
return "Loom branch format 1"
def __str__(self):
return "Bazaar-NG Loom format 1"
class BzrBranchLoomFormat6(LoomFormatMixin, _mod_bzrbranch.BzrBranchFormat6):
"""Loom's second edition - based on bzr's Branch6.
This format is an extension to BzrBranchFormat6 with the following changes:
- a last-loom file.
The last-loom file has a revision id in it which points into the loom
data branch in the repository.
This format is new in the loom plugin.
"""
def _branch_class(self):
return LoomBranch6
_parent_classs = _mod_bzrbranch.BzrBranchFormat6
@classmethod
def get_format_string(cls):
"""See BranchFormat.get_format_string()."""
return b"Bazaar-NG Loom branch format 6\n"
def get_format_description(self):
"""See BranchFormat.get_format_description()."""
return "Loom branch format 6"
def __str__(self):
return "bzr loom format 6 (based on bzr branch format 6)\n"
class BzrBranchLoomFormat7(LoomFormatMixin, _mod_bzrbranch.BzrBranchFormat7):
"""Loom's second edition - based on bzr's Branch7.
This format is an extension to BzrBranchFormat7 with the following changes:
- a last-loom file.
The last-loom file has a revision id in it which points into the loom
data branch in the repository.
This format is new in the loom plugin.
"""
def _branch_class(self):
return LoomBranch7
_parent_classs = _mod_bzrbranch.BzrBranchFormat7
@classmethod
def get_format_string(cls):
"""See BranchFormat.get_format_string()."""
return b"Bazaar-NG Loom branch format 7\n"
def get_format_description(self):
"""See BranchFormat.get_format_description()."""
return "Loom branch format 7"
def __str__(self):
return "bzr loom format 7 (based on bzr branch format 7)\n"
# Handle the smart server:
class InterLoomBranch(breezy.branch.GenericInterBranch):
@classmethod
def _get_branch_formats_to_test(klass):
default_format = breezy.branch.format_registry.get_default()
return [
(default_format, BzrBranchLoomFormat7()),
(BzrBranchLoomFormat7(), default_format),
(BzrBranchLoomFormat7(), BzrBranchLoomFormat7()),
]
def unwrap_branch(self, branch):
if isinstance(branch, remote.RemoteBranch):
branch._ensure_real()
return branch._real_branch
return branch
@classmethod
def is_compatible(klass, source, target):
# 1st cut: special case and handle all *->Loom and Loom->*
return klass.branch_is_loom(source) or klass.branch_is_loom(target)
def get_loom_state(self, branch):
branch = self.unwrap_branch(branch)
return branch.get_loom_state()
def get_threads(self, branch, revision_id):
branch = self.unwrap_branch(branch)
return branch.get_threads(revision_id)
@classmethod
def branch_is_loom(klass, branch):
format = klass.unwrap_format(branch._format)
return isinstance(format, LoomFormatMixin)
def copy_content_into(self, revision_id=None, tag_selector=None):
with self.lock_write():
if not self.__class__.branch_is_loom(self.source):
# target is loom, but the generic code path works Just Fine for
# regular to loom copy_content_into.
return super(InterLoomBranch, self).copy_content_into(
revision_id=revision_id, tag_selector=tag_selector)
# XXX: hint for breezy - break this into two routines, one for
# copying the last-rev pointer, one for copying parent etc.
state = self.get_loom_state(self.source)
parents = state.get_parents()
if parents:
loom_tip = parents[0]
else:
loom_tip = None
threads = self.get_threads(self.source, state.get_basis_revision_id())
if revision_id not in (None, NULL_REVISION):
if threads:
# revision_id should be in the loom, or its an error
found_threads = [
thread for thread, rev in threads
if rev == revision_id]
if not found_threads:
# the thread we have been asked to set in the remote
# side has not been recorded yet, so its data is not
# present at this point.