~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to command_classes.py

  • Committer: Aaron Bentley
  • Date: 2008-11-11 15:55:32 UTC
  • Revision ID: aaron@aaronbentley.com-20081111155532-pjxa3kib17yggl9x
Update shelf tests to new command names

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006, 2007 Aaron Bentley <aaron@aaronbentley.com>
 
2
# Copyright (C) 2005, 2006 Canonical Limited.
 
3
# Copyright (C) 2006 Michael Ellerman.
 
4
#
 
5
#    This program is free software; you can redistribute it and/or modify
 
6
#    it under the terms of the GNU General Public License as published by
 
7
#    the Free Software Foundation; either version 2 of the License, or
 
8
#    (at your option) any later version.
 
9
#
 
10
#    This program is distributed in the hope that it will be useful,
 
11
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
#    GNU General Public License for more details.
 
14
#
 
15
#    You should have received a copy of the GNU General Public License
 
16
#    along with this program; if not, write to the Free Software
 
17
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 
 
19
import bzrlib
 
20
 
 
21
from bzrlib.lazy_import import lazy_import
 
22
lazy_import(globals(), """
 
23
from bzrlib import help, urlutils
 
24
import shelf
 
25
""")
 
26
 
 
27
from command import BzrToolsCommand
 
28
from errors import CommandError
 
29
from patchsource import BzrPatchSource
 
30
import sys
 
31
import os.path
 
32
 
 
33
import bzrlib.builtins
 
34
import bzrlib.commands
 
35
from bzrlib.branch import Branch
 
36
from bzrlib.bzrdir import BzrDir
 
37
from bzrlib.commands import get_cmd_object
 
38
from bzrlib.errors import BzrCommandError
 
39
import bzrlib.ignores
 
40
from bzrlib.trace import note
 
41
from bzrlib.option import Option
 
42
 
 
43
from command import BzrToolsCommand
 
44
 
 
45
 
 
46
class cmd_clean_tree(BzrToolsCommand):
 
47
    """Remove unwanted files from working tree.
 
48
 
 
49
    By default, only unknown files, not ignored files, are deleted.  Versioned
 
50
    files are never deleted.
 
51
 
 
52
    Another class is 'detritus', which includes files emitted by bzr during
 
53
    normal operations and selftests.  (The value of these files decreases with
 
54
    time.)
 
55
 
 
56
    If no options are specified, unknown files are deleted.  Otherwise, option
 
57
    flags are respected, and may be combined.
 
58
 
 
59
    To check what clean-tree will do, use --dry-run.
 
60
    """
 
61
    takes_options = [Option('ignored', help='Delete all ignored files.'),
 
62
                     Option('detritus', help='Delete conflict files, merge'
 
63
                            ' backups, and failed selftest dirs.'),
 
64
                     Option('unknown',
 
65
                            help='Delete files unknown to bzr (default).'),
 
66
                     Option('dry-run', help='Show files to delete instead of'
 
67
                            ' deleting them.'),
 
68
                     Option('force', help='Do not prompt before deleting.')]
 
69
    def run(self, unknown=False, ignored=False, detritus=False, dry_run=False,
 
70
            force=False):
 
71
        from clean_tree import clean_tree
 
72
        if not (unknown or ignored or detritus):
 
73
            unknown = True
 
74
        if dry_run:
 
75
            force = True
 
76
        clean_tree('.', unknown=unknown, ignored=ignored, detritus=detritus, 
 
77
                   dry_run=dry_run, no_prompt=force)
 
78
 
 
79
 
 
80
class cmd_graph_ancestry(BzrToolsCommand):
 
81
    """Produce ancestry graphs using dot.
 
82
    
 
83
    Output format is detected according to file extension.  Some of the more
 
84
    common output formats are html, png, gif, svg, ps.  An extension of '.dot'
 
85
    will cause a dot graph file to be produced.  HTML output has mouseovers
 
86
    that show the commit message.
 
87
 
 
88
    Branches are labeled r?, where ? is the revno.  If they have no revno,
 
89
    with the last 5 characters of their revision identifier are used instead.
 
90
 
 
91
    The value starting with d is "(maximum) distance from the null revision".
 
92
 
 
93
    If --merge-branch is specified, the two branches are compared and a merge
 
94
    base is selected.
 
95
 
 
96
    Legend:
 
97
    white    normal revision
 
98
    yellow   THIS  history
 
99
    red      OTHER history
 
100
    orange   COMMON history
 
101
    blue     COMMON non-history ancestor
 
102
    green    Merge base (COMMON ancestor farthest from the null revision)
 
103
    dotted   Ghost revision (missing from branch storage)
 
104
 
 
105
    Ancestry is usually collapsed by skipping revisions with a single parent
 
106
    and descendant.  The number of skipped revisions is shown on the arrow.
 
107
    This feature can be disabled with --no-collapse.
 
108
 
 
109
    By default, revisions are ordered by distance from root, but they can be
 
110
    clustered instead using --cluster.
 
111
 
 
112
    If available, rsvg is used to antialias PNG and JPEG output, but this can
 
113
    be disabled with --no-antialias.
 
114
    """
 
115
    takes_args = ['file', 'merge_branch?']
 
116
    takes_options = [Option('no-collapse', help="Do not skip simple nodes."),
 
117
                     Option('no-antialias',
 
118
                     help="Do not use rsvg to produce antialiased output."),
 
119
                     Option('merge-branch', type=str,
 
120
                     help="Use this branch to calcuate a merge base."),
 
121
                     Option('cluster', help="Use clustered output."),
 
122
                     Option('max-distance',
 
123
                            help="Show no nodes farther than this.", type=int),
 
124
                     Option('directory',
 
125
                            help='Source branch to use (default is current'
 
126
                            ' directory).',
 
127
                            short_name='d',
 
128
                            type=unicode),
 
129
                    ]
 
130
    def run(self, file, merge_branch=None, no_collapse=False,
 
131
            no_antialias=False, cluster=False, max_distance=100,
 
132
            directory='.'):
 
133
        if max_distance == -1:
 
134
            max_distance = None
 
135
        import graph
 
136
        if cluster:
 
137
            ranking = "cluster"
 
138
        else:
 
139
            ranking = "forced"
 
140
        graph.write_ancestry_file(directory, file, not no_collapse,
 
141
                                  not no_antialias, merge_branch, ranking,
 
142
                                  max_distance=max_distance)
 
143
 
 
144
 
 
145
class cmd_fetch_ghosts(BzrToolsCommand):
 
146
    """Attempt to retrieve ghosts from another branch.
 
147
    If the other branch is not supplied, the last-pulled branch is used.
 
148
    """
 
149
    aliases = ['fetch-missing']
 
150
    takes_args = ['branch?']
 
151
    takes_options = [Option('no-fix', help="Skip additional synchonization.")]
 
152
    def run(self, branch=None, no_fix=False):
 
153
        from fetch_ghosts import fetch_ghosts
 
154
        fetch_ghosts(branch, no_fix)
 
155
 
 
156
strip_help="""Strip the smallest prefix containing num leading slashes  from \
 
157
each file name found in the patch file."""
 
158
 
 
159
 
 
160
class cmd_patch(BzrToolsCommand):
 
161
    """Apply a named patch to the current tree.
 
162
    """
 
163
    takes_args = ['filename?']
 
164
    takes_options = [Option('strip', type=int, help=strip_help),
 
165
                     Option('silent', help='Suppress chatter.')]
 
166
    def run(self, filename=None, strip=None, silent=False):
 
167
        from patch import patch
 
168
        from bzrlib.workingtree import WorkingTree
 
169
        wt = WorkingTree.open_containing('.')[0]
 
170
        if strip is None:
 
171
            strip = 0
 
172
        return patch(wt, filename, strip, silent)
 
173
 
 
174
 
 
175
class cmd_shelve1(BzrToolsCommand):
 
176
    """Temporarily set aside some changes from the current tree.
 
177
 
 
178
    Shelve allows you to temporarily put changes you've made "on the shelf",
 
179
    ie. out of the way, until a later time when you can bring them back from
 
180
    the shelf with the 'unshelve1' command.
 
181
 
 
182
    Shelve is intended to help separate several sets of text changes that have
 
183
    been inappropriately mingled.  If you just want to get rid of all changes
 
184
    (text and otherwise) and you don't need to restore them later, use revert.
 
185
    If you want to shelve all text changes at once, use shelve1 --all.
 
186
 
 
187
    By default shelve1 asks you what you want to shelve, press '?' at the
 
188
    prompt to get help. To shelve everything run shelve1 --all.
 
189
 
 
190
    If filenames are specified, only the changes to those files will be
 
191
    shelved, other files will be left untouched.
 
192
 
 
193
    If a revision is specified, changes since that revision will be shelved.
 
194
 
 
195
    You can put multiple items on the shelf. Normally each time you run
 
196
    unshelve1 the most recently shelved changes will be reinstated. However,
 
197
    you can also unshelve changes in a different order by explicitly
 
198
    specifiying which changes to unshelve1. This works best when the changes
 
199
    don't depend on each other.
 
200
 
 
201
    While you have patches on the shelf you can view and manipulate them with
 
202
    the 'shelf' command. Run 'bzr shelf -h' for more info.
 
203
    """
 
204
 
 
205
    aliases = ['shelve']
 
206
    takes_args = ['file*']
 
207
    takes_options = [Option('message',
 
208
            help='A message to associate with the shelved changes.',
 
209
            short_name='m', type=unicode),
 
210
            'revision',
 
211
            Option('all', help='Shelve all changes without prompting.'),
 
212
            Option('no-color', help='Never display changes in color.')]
 
213
 
 
214
    def run(self, all=False, file_list=None, message=None, revision=None,
 
215
            no_color=False):
 
216
        if revision is not None and revision:
 
217
            if len(revision) == 1:
 
218
                revision = revision[0]
 
219
            else:
 
220
                raise CommandError("shelve only accepts a single revision "
 
221
                                  "parameter.")
 
222
 
 
223
        source = BzrPatchSource(revision, file_list)
 
224
        s = shelf.Shelf(source.base)
 
225
        s.shelve(source, all, message, no_color)
 
226
        return 0
 
227
 
 
228
 
 
229
# The following classes are only used as subcommands for 'shelf', they're
 
230
# not to be registered directly with bzr.
 
231
 
 
232
class cmd_shelf_list(bzrlib.commands.Command):
 
233
    """List the patches on the current shelf."""
 
234
    aliases = ['list', 'ls']
 
235
    def run(self):
 
236
        self.shelf.list()
 
237
 
 
238
 
 
239
class cmd_shelf_delete(bzrlib.commands.Command):
 
240
    """Delete the patch from the current shelf."""
 
241
    aliases = ['delete', 'del']
 
242
    takes_args = ['patch']
 
243
    def run(self, patch):
 
244
        self.shelf.delete(patch)
 
245
 
 
246
 
 
247
class cmd_shelf_switch(bzrlib.commands.Command):
 
248
    """Switch to the other shelf, create it if necessary."""
 
249
    aliases = ['switch']
 
250
    takes_args = ['othershelf']
 
251
    def run(self, othershelf):
 
252
        s = shelf.Shelf(self.shelf.base, othershelf)
 
253
        s.make_default()
 
254
 
 
255
 
 
256
class cmd_shelf_show(bzrlib.commands.Command):
 
257
    """Show the contents of the specified or topmost patch."""
 
258
    aliases = ['show', 'cat', 'display']
 
259
    takes_args = ['patch?']
 
260
    def run(self, patch=None):
 
261
        self.shelf.display(patch)
 
262
 
 
263
 
 
264
class cmd_shelf_upgrade(bzrlib.commands.Command):
 
265
    """Upgrade old format shelves."""
 
266
    aliases = ['upgrade']
 
267
    def run(self):
 
268
        self.shelf.upgrade()
 
269
 
 
270
 
 
271
class cmd_shelf(BzrToolsCommand):
 
272
    """Perform various operations on your shelved patches. See also shelve1."""
 
273
    takes_args = ['subcommand', 'args*']
 
274
 
 
275
    subcommands = [cmd_shelf_list, cmd_shelf_delete, cmd_shelf_switch,
 
276
        cmd_shelf_show, cmd_shelf_upgrade]
 
277
 
 
278
    def run(self, subcommand, args_list):
 
279
        import sys
 
280
 
 
281
        if args_list is None:
 
282
            args_list = []
 
283
        cmd = self._get_cmd_object(subcommand)
 
284
        source = BzrPatchSource()
 
285
        s = shelf.Shelf(source.base)
 
286
        cmd.shelf = s
 
287
 
 
288
        if args_list is None:
 
289
            args_list = []
 
290
        return cmd.run_argv_aliases(args_list)
 
291
 
 
292
    def _get_cmd_object(self, cmd_name):
 
293
        for cmd_class in self.subcommands:
 
294
            for alias in cmd_class.aliases:
 
295
                if alias == cmd_name:
 
296
                    return cmd_class()
 
297
        raise CommandError("Unknown shelf subcommand '%s'" % cmd_name)
 
298
 
 
299
    def help(self):
 
300
        text = ["%s\n\nSubcommands:\n" % self.__doc__]
 
301
 
 
302
        for cmd_class in self.subcommands:
 
303
            text.extend(self.sub_help(cmd_class) + ['\n'])
 
304
 
 
305
        return ''.join(text)
 
306
 
 
307
    def sub_help(self, cmd_class):
 
308
        text = []
 
309
        cmd_obj = cmd_class()
 
310
        indent = 2 * ' '
 
311
 
 
312
        usage = cmd_obj._usage()
 
313
        usage = usage.replace('bzr shelf-', '')
 
314
        text.append('%s%s\n' % (indent, usage))
 
315
 
 
316
        text.append('%s%s\n' % (2 * indent, cmd_class.__doc__))
 
317
 
 
318
        # Somewhat copied from bzrlib.help.help_on_command_options
 
319
        option_help = []
 
320
        for option_name, option in sorted(cmd_obj.options().items()):
 
321
            if option_name == 'help':
 
322
                continue
 
323
            option_help.append('%s--%s' % (3 * indent, option_name))
 
324
            if option.type is not None:
 
325
                option_help.append(' %s' % option.argname.upper())
 
326
            if option.short_name():
 
327
                option_help.append(', -%s' % option.short_name())
 
328
            option_help.append('%s%s\n' % (2 * indent, option.help))
 
329
 
 
330
        if len(option_help) > 0:
 
331
            text.append('%soptions:\n' % (2 * indent))
 
332
            text.extend(option_help)
 
333
 
 
334
        return text
 
335
 
 
336
 
 
337
class cmd_unshelve1(BzrToolsCommand):
 
338
    """Restore shelved changes.
 
339
 
 
340
    By default the most recently shelved changes are restored. However if you
 
341
    specify a patch by name those changes will be restored instead.
 
342
 
 
343
    See 'shelve1' for more information.
 
344
    """
 
345
    aliases = ['unshelve']
 
346
    takes_options = [
 
347
            Option('all', help='Unshelve all changes without prompting.'),
 
348
            Option('force', help='Force unshelving even if errors occur.'),
 
349
            Option('no-color', help='Never display changes in color.')
 
350
        ]
 
351
    takes_args = ['patch?']
 
352
    def run(self, patch=None, all=False, force=False, no_color=False):
 
353
        source = BzrPatchSource()
 
354
        s = shelf.Shelf(source.base)
 
355
        s.unshelve(source, patch, all, force, no_color)
 
356
        return 0
 
357
 
 
358
 
 
359
class cmd_shell(BzrToolsCommand):
 
360
    """Begin an interactive shell tailored for bzr.
 
361
    Bzr commands can be used without typing bzr first, and will be run natively
 
362
    when possible.  Tab completion is tailored for bzr.  The shell prompt shows
 
363
    the branch nick, revno, and path.
 
364
 
 
365
    If it encounters any moderately complicated shell command, it will punt to
 
366
    the system shell.
 
367
 
 
368
    Example:
 
369
    $ bzr shell
 
370
    bzr bzrtools:287/> status
 
371
    modified:
 
372
      __init__.py
 
373
    bzr bzrtools:287/> status --[TAB][TAB]
 
374
    --all        --help       --revision   --show-ids
 
375
    bzr bzrtools:287/> status --
 
376
    """
 
377
    def run(self):
 
378
        import shell
 
379
        return shell.run_shell()
 
380
 
 
381
 
 
382
class cmd_branch_history(BzrToolsCommand):
 
383
    """\
 
384
    Display the development history of a branch.
 
385
 
 
386
    Each different committer or branch nick is considered a different line of
 
387
    development.  Committers are treated as the same if they have the same
 
388
    name, or if they have the same email address.
 
389
    """
 
390
    takes_args = ["branch?"]
 
391
    def run(self, branch=None):
 
392
        from branchhistory import branch_history
 
393
        return branch_history(branch)
 
394
 
 
395
 
 
396
class cmd_zap(BzrToolsCommand):
 
397
    """\
 
398
    Remove a lightweight checkout, if it can be done safely.
 
399
 
 
400
    This command will remove a lightweight checkout without losing data.  That
 
401
    means it only removes lightweight checkouts, and only if they have no
 
402
    uncommitted changes.
 
403
 
 
404
    If --branch is specified, the branch will be deleted too, but only if the
 
405
    the branch has no new commits (relative to its parent).
 
406
    """
 
407
    takes_options = [Option("branch", help="Remove associated branch from"
 
408
                                           " repository."),
 
409
                     Option('force', help='Delete tree even if contents are'
 
410
                     ' modified.')]
 
411
    takes_args = ["checkout"]
 
412
    def run(self, checkout, branch=False, force=False):
 
413
        from zap import zap
 
414
        return zap(checkout, remove_branch=branch, allow_modified=force)
 
415
 
 
416
 
 
417
class cmd_cbranch(BzrToolsCommand):
 
418
    """
 
419
    Create a new checkout, associated with a new repository branch.
 
420
 
 
421
    When you cbranch, bzr looks up a target location in locations.conf, and
 
422
    creates the branch there.
 
423
 
 
424
    In your locations.conf, add the following lines:
 
425
    [/working_directory_root]
 
426
    cbranch_target = /branch_root
 
427
    cbranch_target:policy = appendpath
 
428
 
 
429
    This will mean that if you run "bzr cbranch foo/bar foo/baz" in the
 
430
    working directory root, the branch will be created in
 
431
    "/branch_root/foo/baz"
 
432
 
 
433
    NOTE: cbranch also supports "cbranch_root", but that behaviour is
 
434
    deprecated.
 
435
    """
 
436
    takes_options = [Option("lightweight",
 
437
                            help="Create a lightweight checkout."), 'revision',
 
438
                     Option('files-from', type=unicode,
 
439
                            help='Accelerate checkout using files from this'
 
440
                                 ' tree.'),
 
441
                     Option('hardlink',
 
442
                            help='Hard-link files from source/files-from tree'
 
443
                            ' where posible.')]
 
444
    takes_args = ["source", "target?"]
 
445
    def run(self, source, target=None, lightweight=False, revision=None,
 
446
            files_from=None, hardlink=False):
 
447
        from cbranch import cbranch
 
448
        return cbranch(source, target, lightweight=lightweight,
 
449
                       revision=revision, files_from=files_from,
 
450
                       hardlink=hardlink)
 
451
 
 
452
 
 
453
class cmd_branches(BzrToolsCommand):
 
454
    """Scan a location for branches"""
 
455
    takes_args = ["location?"]
 
456
    def run(self, location=None):
 
457
        from branches import branches
 
458
        return branches(location)
 
459
 
 
460
class cmd_trees(BzrToolsCommand):
 
461
    """Scan a location for trees"""
 
462
    takes_args = ['location?']
 
463
    def run(self, location='.'):
 
464
        from bzrlib.workingtree import WorkingTree
 
465
        from bzrlib.transport import get_transport
 
466
        t = get_transport(location)
 
467
        for tree in WorkingTree.find_trees(location):
 
468
            self.outf.write('%s\n' % t.relpath(
 
469
                tree.bzrdir.root_transport.base))
 
470
 
 
471
class cmd_multi_pull(BzrToolsCommand):
 
472
    """Pull all the branches under a location, e.g. a repository.
 
473
 
 
474
    Both branches present in the directory and the branches of checkouts are
 
475
    pulled.
 
476
    """
 
477
    takes_args = ["location?"]
 
478
    def run(self, location=None):
 
479
        from bzrlib.transport import get_transport
 
480
        from bzrtools import iter_branch_tree
 
481
        if location is None:
 
482
            location = '.'
 
483
        t = get_transport(location)
 
484
        possible_transports = []
 
485
        if not t.listable():
 
486
            print "Can't list this type of location."
 
487
            return 3
 
488
        for branch, wt in iter_branch_tree(t):
 
489
            if wt is None:
 
490
                pullable = branch
 
491
            else:
 
492
                pullable = wt
 
493
            parent = branch.get_parent()
 
494
            if parent is None:
 
495
                continue
 
496
            if wt is not None:
 
497
                base = wt.basedir
 
498
            else:
 
499
                base = branch.base
 
500
            if base.startswith(t.base):
 
501
                relpath = base[len(t.base):].rstrip('/')
 
502
            else:
 
503
                relpath = base
 
504
            print "Pulling %s from %s" % (relpath, parent)
 
505
            try:
 
506
                branch_t = get_transport(parent, possible_transports)
 
507
                pullable.pull(Branch.open_from_transport(branch_t))
 
508
            except Exception, e:
 
509
                print e
 
510
 
 
511
 
 
512
 
 
513
class cmd_import(BzrToolsCommand):
 
514
    """Import sources from a directory, tarball or zip file
 
515
 
 
516
    This command will import a directory, tarball or zip file into a bzr
 
517
    tree, replacing any versioned files already present.  If a directory is
 
518
    specified, it is used as the target.  If the directory does not exist, or
 
519
    is not versioned, it is created.
 
520
 
 
521
    Tarballs may be gzip or bzip2 compressed.  This is autodetected.
 
522
 
 
523
    If the tarball or zip has a single root directory, that directory is
 
524
    stripped when extracting the tarball.  This is not done for directories.
 
525
    """
 
526
 
 
527
    takes_args = ['source', 'tree?']
 
528
    def run(self, source, tree=None):
 
529
        from upstream_import import do_import
 
530
        do_import(source, tree)
 
531
 
 
532
 
 
533
class cmd_cdiff(BzrToolsCommand):
 
534
    """A color version of bzr's diff"""
 
535
    takes_args = property(lambda x: get_cmd_object('diff').takes_args)
 
536
    takes_options = list(get_cmd_object('diff').takes_options) + [
 
537
        Option('check-style',
 
538
            help='Warn if trailing whitespace or spurious changes have been'
 
539
                 ' added.')]
 
540
 
 
541
    def run(self, check_style=False, *args, **kwargs):
 
542
        from colordiff import colordiff
 
543
        colordiff(check_style, *args, **kwargs)
 
544
 
 
545
 
 
546
class cmd_rspush(BzrToolsCommand):
 
547
    """Upload this branch to another location using rsync.
 
548
 
 
549
    If no location is specified, the last-used location will be used.  To
 
550
    prevent dirty trees from being uploaded, rspush will error out if there are
 
551
    unknown files or local changes.  It will also error out if the upstream
 
552
    directory is non-empty and not an earlier version of the branch.
 
553
    """
 
554
    takes_args = ['location?']
 
555
    takes_options = [Option('overwrite', help='Ignore differences between'
 
556
                            ' branches and overwrite unconditionally.'),
 
557
                     Option('no-tree', help='Do not push the working tree,'
 
558
                            ' just the .bzr.')]
 
559
 
 
560
    def run(self, location=None, overwrite=False, no_tree=False):
 
561
        from bzrlib import workingtree
 
562
        import bzrtools
 
563
        cur_branch = workingtree.WorkingTree.open_containing(".")[0]
 
564
        bzrtools.rspush(cur_branch, location, overwrite=overwrite,
 
565
                      working_tree=not no_tree)
 
566
 
 
567
 
 
568
class cmd_link_tree(BzrToolsCommand):
 
569
    """Hardlink matching files to another tree.
 
570
 
 
571
    Only files with identical content and execute bit will be linked.
 
572
    """
 
573
    takes_args = ['location']
 
574
 
 
575
    def run(self, location):
 
576
        from bzrlib import workingtree
 
577
        from bzrlib.plugins.bzrtools.link_tree import link_tree
 
578
        target_tree = workingtree.WorkingTree.open_containing(".")[0]
 
579
        source_tree = workingtree.WorkingTree.open(location)
 
580
        target_tree.lock_write()
 
581
        try:
 
582
            source_tree.lock_read()
 
583
            try:
 
584
                link_tree(target_tree, source_tree)
 
585
            finally:
 
586
                source_tree.unlock()
 
587
        finally:
 
588
            target_tree.unlock()