~abentley/bzrtools/bzrtools.dev

0.1.22 by Michael Ellerman
Add __init__.py, put cmd_shelve/unshelve in there.
1
#!/usr/bin/python
246 by Aaron Bentley
Merged shelf_v2
2
"""\
3
Various useful plugins for working with bzr.
4
"""
0.1.22 by Michael Ellerman
Add __init__.py, put cmd_shelve/unshelve in there.
5
import bzrlib.commands
246 by Aaron Bentley
Merged shelf_v2
6
import push
7
from shelf import Shelf
8
import sys
9
import os.path
10
from bzrlib.option import Option
0.1.22 by Michael Ellerman
Add __init__.py, put cmd_shelve/unshelve in there.
11
import bzrlib.branch
0.1.39 by Michael Ellerman
Fix shelve and unshelve to pass location to Shelf().
12
from bzrlib.errors import BzrCommandError
277 by Aaron Bentley
Renamed force-reweave-inventory to fix
13
from reweave_inventory import cmd_fix
147.1.41 by Aaron Bentley
Merge from mainline
14
sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), 
15
						 "external")))
246 by Aaron Bentley
Merged shelf_v2
16
17
Option.OPTIONS['ignored'] = Option('ignored',
18
        help='delete all ignored files.')
265 by Aaron Bentley
Fixed spelling of detritus
19
Option.OPTIONS['detritus'] = Option('detritus',
246 by Aaron Bentley
Merged shelf_v2
20
        help='delete conflict files merge backups, and failed selftest dirs.' +
21
              '(*.THIS, *.BASE, *.OTHER, *~, *.tmp')
22
Option.OPTIONS['dry-run'] = Option('dry-run',
23
        help='show files to delete instead of deleting them.')
24
25
class cmd_clean_tree(bzrlib.commands.Command):
26
    """Remove unwanted files from working tree.
27
    Normally, ignored files are left alone.
28
    """
265 by Aaron Bentley
Fixed spelling of detritus
29
    takes_options = ['ignored', 'detritus', 'dry-run']
30
    def run(self, ignored=False, detritus=False, dry_run=False):
246 by Aaron Bentley
Merged shelf_v2
31
        from clean_tree import clean_tree
265 by Aaron Bentley
Fixed spelling of detritus
32
        clean_tree('.', ignored=ignored, detritus=detritus, dry_run=dry_run)
246 by Aaron Bentley
Merged shelf_v2
33
34
Option.OPTIONS['merge-branch'] = Option('merge-branch',type=str)
35
36
class cmd_graph_ancestry(bzrlib.commands.Command):
37
    """Produce ancestry graphs using dot.
38
    
39
    Output format is detected according to file extension.  Some of the more
296 by Aaron Bentley
Updated graph-ancestry help
40
    common output formats are html, png, gif, svg, ps.  An extension of '.dot'
41
    will cause a dot graph file to be produced.  HTML output has mouseovers
42
    that show the commit message.
246 by Aaron Bentley
Merged shelf_v2
43
44
    Branches are labeled r?, where ? is the revno.  If they have no revno,
45
    with the last 5 characters of their revision identifier are used instead.
296 by Aaron Bentley
Updated graph-ancestry help
46
47
    The value starting with d is "(maximum) distance from the null revision".
246 by Aaron Bentley
Merged shelf_v2
48
    
49
    If --merge-branch is specified, the two branches are compared and a merge
50
    base is selected.
51
    
52
    Legend:
53
    white    normal revision
54
    yellow   THIS  history
55
    red      OTHER history
56
    orange   COMMON history
57
    blue     COMMON non-history ancestor
296 by Aaron Bentley
Updated graph-ancestry help
58
    green    Merge base (COMMON ancestor farthest from the null revision)
59
    dotted   Ghost revision (missing from branch storage)
246 by Aaron Bentley
Merged shelf_v2
60
296 by Aaron Bentley
Updated graph-ancestry help
61
    Ancestry is usually collapsed by skipping revisions with a single parent
246 by Aaron Bentley
Merged shelf_v2
62
    and descendant.  The number of skipped revisions is shown on the arrow.
63
    This feature can be disabled with --no-collapse.
64
65
    By default, revisions are ordered by distance from root, but they can be
66
    clustered instead using --cluster.
67
68
    If available, rsvg is used to antialias PNG and JPEG output, but this can
69
    be disabled with --no-antialias.
70
    """
71
    takes_args = ['branch', 'file']
296 by Aaron Bentley
Updated graph-ancestry help
72
    takes_options = [Option('no-collapse', help="Do not skip simple nodes"), 
73
                     Option('no-antialias',
74
                     help="Do not use rsvg to produce antialiased output"), 
75
                     Option('merge-branch', type=str, 
76
                     help="Use this branch to calcuate a merge base"), 
77
                     Option('cluster', help="Use clustered output.")]
246 by Aaron Bentley
Merged shelf_v2
78
    def run(self, branch, file, no_collapse=False, no_antialias=False,
79
        merge_branch=None, cluster=False):
80
        import graph
81
        if cluster:
82
            ranking = "cluster"
83
        else:
84
            ranking = "forced"
85
        graph.write_ancestry_file(branch, file, not no_collapse, 
86
                                  not no_antialias, merge_branch, ranking)
87
88
class cmd_fetch_ghosts(bzrlib.commands.Command):
89
    """Attempt to retrieve ghosts from another branch.
90
    If the other branch is not supplied, the last-pulled branch is used.
91
    """
92
    aliases = ['fetch-missing']
93
    takes_args = ['branch?']
275.1.3 by Daniel Silverstone
Fix up fetch_ghosts to lock the branches, and to invoke bzr fix if it fetches any ghosts into the tree
94
    takes_options = [Option('no-fix')]
95
    def run(self, branch=None, no_fix=False):
246 by Aaron Bentley
Merged shelf_v2
96
        from fetch_ghosts import fetch_ghosts
275.1.3 by Daniel Silverstone
Fix up fetch_ghosts to lock the branches, and to invoke bzr fix if it fetches any ghosts into the tree
97
        fetch_ghosts(branch, no_fix)
246 by Aaron Bentley
Merged shelf_v2
98
99
strip_help="""Strip the smallest prefix containing num leading slashes  from \
100
each file name found in the patch file."""
101
Option.OPTIONS['strip'] = Option('strip', type=int, help=strip_help)
102
class cmd_patch(bzrlib.commands.Command):
103
    """Apply a named patch to the current tree.
104
    """
105
    takes_args = ['filename?']
106
    takes_options = ['strip']
320 by Aaron Bentley
Updated to match new bzr diff behaviour
107
    def run(self, filename=None, strip=1):
246 by Aaron Bentley
Merged shelf_v2
108
        from patch import patch
109
        from bzrlib.branch import Branch
110
        b = Branch.open_containing('.')[0]
111
        return patch(b, filename, strip)
112
0.1.22 by Michael Ellerman
Add __init__.py, put cmd_shelve/unshelve in there.
113
114
class cmd_shelve(bzrlib.commands.Command):
289 by Aaron Bentley
Updated shelf help
115
    """Temporarily remove some text changes from the current tree.
0.1.22 by Michael Ellerman
Add __init__.py, put cmd_shelve/unshelve in there.
116
    Use 'unshelve' to restore these changes.
117
289 by Aaron Bentley
Updated shelf help
118
    Shelve is intended to help separate several sets of text changes that have
119
    been inappropriately mingled.  If you just want to get rid of all changes
120
    (text and otherwise) and you don't need to restore them later, use revert.
121
    If you want to shelve all text changes at once, use shelve --all.
122
123
    If filenames are specified, only changes to those files will be shelved.
0.1.22 by Michael Ellerman
Add __init__.py, put cmd_shelve/unshelve in there.
124
    If a revision is specified, all changes since that revision will may be
289 by Aaron Bentley
Updated shelf help
125
    shelved.
0.1.22 by Michael Ellerman
Add __init__.py, put cmd_shelve/unshelve in there.
126
    """
127
    takes_args = ['file*']
289 by Aaron Bentley
Updated shelf help
128
    takes_options = [Option('all', 
129
                            help='Shelve all changes without prompting'), 
130
                     'message', 'revision']
0.1.28 by Michael Ellerman
Implement "bzr shelve --all".
131
    def run(self, all=False, file_list=None, message=None, revision=None):
0.1.39 by Michael Ellerman
Fix shelve and unshelve to pass location to Shelf().
132
        if file_list is not None and len(file_list) > 0:
133
            branchdir = file_list[0]
134
        else:
135
            branchdir = '.'
136
0.1.22 by Michael Ellerman
Add __init__.py, put cmd_shelve/unshelve in there.
137
        if revision is not None and revision:
0.1.39 by Michael Ellerman
Fix shelve and unshelve to pass location to Shelf().
138
            if len(revision) == 1:
139
                revision = revision[0]
0.1.22 by Michael Ellerman
Add __init__.py, put cmd_shelve/unshelve in there.
140
            else:
0.1.39 by Michael Ellerman
Fix shelve and unshelve to pass location to Shelf().
141
                raise BzrCommandError("shelve only accepts a single revision "
142
                                  "parameter.")
0.1.22 by Michael Ellerman
Add __init__.py, put cmd_shelve/unshelve in there.
143
0.1.39 by Michael Ellerman
Fix shelve and unshelve to pass location to Shelf().
144
        s = Shelf(branchdir)
145
        return s.shelve(all, message, revision, file_list)
0.1.22 by Michael Ellerman
Add __init__.py, put cmd_shelve/unshelve in there.
146
246 by Aaron Bentley
Merged shelf_v2
147
0.1.22 by Michael Ellerman
Add __init__.py, put cmd_shelve/unshelve in there.
148
class cmd_unshelve(bzrlib.commands.Command):
149
    """Restore previously-shelved changes to the current tree.
150
    See also 'shelve'.
151
    """
152
    def run(self):
0.1.39 by Michael Ellerman
Fix shelve and unshelve to pass location to Shelf().
153
        s = Shelf('.')
282 by Aaron Bentley
Fixed unshelve return code
154
        return s.unshelve()
0.1.22 by Michael Ellerman
Add __init__.py, put cmd_shelve/unshelve in there.
155
249 by Aaron Bentley
Got the shell basics working properly
156
class cmd_shell(bzrlib.commands.Command):
287 by Aaron Bentley
Added shell docstring
157
    """Begin an interactive shell tailored for bzr.
158
    Bzr commands can be used without typing bzr first, and will be run natively
159
    when possible.  Tab completion is tailored for bzr.  The shell prompt shows
160
    the branch nick, revno, and path.
161
162
    If it encounters any moderately complicated shell command, it will punt to
163
    the system shell.
164
165
    Example:
166
    $ bzr shell
167
    bzr bzrtools:287/> status
168
    modified:
169
      __init__.py
170
    bzr bzrtools:287/> status --[TAB][TAB]
171
    --all        --help       --revision   --show-ids
172
    bzr bzrtools:287/> status --
173
    """
249 by Aaron Bentley
Got the shell basics working properly
174
    def run(self):
175
        import shell
281 by Aaron Bentley
Handled whitespace branch names better
176
        return shell.run_shell()
246 by Aaron Bentley
Merged shelf_v2
177
292 by Aaron Bentley
Introduced branch-history command
178
class cmd_branch_history(bzrlib.commands.Command):
179
    """\
180
    Display the revision history with reference to lines of development.
181
293 by Aaron Bentley
Updated help
182
    Each different committer or branch nick is considered a different line of
183
    development.  Committers are treated as the same if they have the same
184
    name, or if they have the same email address.
292 by Aaron Bentley
Introduced branch-history command
185
    """
186
    takes_args = ["branch?"]
187
    def run(self, branch=None):
188
        from branchhistory import branch_history 
189
        return branch_history(branch)
190
246 by Aaron Bentley
Merged shelf_v2
191
commands = [cmd_shelve, cmd_unshelve, cmd_clean_tree, cmd_graph_ancestry,
292 by Aaron Bentley
Introduced branch-history command
192
            cmd_fetch_ghosts, cmd_patch, cmd_shell, cmd_fix, cmd_branch_history]
246 by Aaron Bentley
Merged shelf_v2
193
271 by Aaron Bentley
Cherry-picked Robert's diff and push fixes
194
command_decorators = []
195
147.4.16 by Robert Collins
Decorate the built in push command, allowing both rsync push and native push to be used.
196
command_decorators = []
197
246 by Aaron Bentley
Merged shelf_v2
198
import bzrlib.builtins
199
if not hasattr(bzrlib.builtins, "cmd_push"):
200
    commands.append(push.cmd_push)
271 by Aaron Bentley
Cherry-picked Robert's diff and push fixes
201
else:
202
    command_decorators.append(push.cmd_push)
246 by Aaron Bentley
Merged shelf_v2
203
204
from errors import NoPyBaz
205
try:
206
    import baz_import
147.1.41 by Aaron Bentley
Merge from mainline
207
    commands.append(baz_import.cmd_baz_import_branch)
246 by Aaron Bentley
Merged shelf_v2
208
    commands.append(baz_import.cmd_baz_import)
209
210
except NoPyBaz:
147.1.72 by Aaron Bentley
Handle missing pybaz properly
211
    class cmd_baz_import_branch(bzrlib.commands.Command):
212
        """Disabled. (Requires PyBaz)"""
213
        takes_args = ['to_location?', 'from_branch?', 'reuse_history*']
214
        takes_options = ['verbose', Option('max-count', type=int)]
215
        def run(self, to_location=None, from_branch=None, fast=False, 
216
                max_count=None, verbose=False, dry_run=False,
217
                reuse_history_list=[]):
218
            print "This command is disabled.  Please install PyBaz."
219
220
246 by Aaron Bentley
Merged shelf_v2
221
    class cmd_baz_import(bzrlib.commands.Command):
222
        """Disabled. (Requires PyBaz)"""
147.1.72 by Aaron Bentley
Handle missing pybaz properly
223
        takes_args = ['to_root_dir?', 'from_archive?', 'reuse_history*']
224
        takes_options = ['verbose', Option('prefixes', type=str,
225
                         help="Prefixes of branches to import")]
226
        def run(self, to_root_dir=None, from_archive=None, verbose=False,
227
                reuse_history_list=[], prefixes=None):
228
                print "This command is disabled.  Please install PyBaz."
229
    commands.extend((cmd_baz_import_branch, cmd_baz_import))
246 by Aaron Bentley
Merged shelf_v2
230
271 by Aaron Bentley
Cherry-picked Robert's diff and push fixes
231
246 by Aaron Bentley
Merged shelf_v2
232
if hasattr(bzrlib.commands, 'register_command'):
233
    for command in commands:
234
        bzrlib.commands.register_command(command)
271 by Aaron Bentley
Cherry-picked Robert's diff and push fixes
235
    for command in command_decorators:
236
        command._original_command = bzrlib.commands.register_command(
237
            command, True)
238
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
239
240
def test_suite():
147.1.41 by Aaron Bentley
Merge from mainline
241
    import baz_import
242
    import tests
321.1.3 by Aaron Bentley
Fixed up Robert's test changes
243
    from doctest import DocTestSuite, ELLIPSIS
246 by Aaron Bentley
Merged shelf_v2
244
    from unittest import TestSuite, TestLoader
245
    import clean_tree
246
    import blackbox
247 by Aaron Bentley
Renamed tests.py to shelf_tests.py
247
    import shelf_tests
246 by Aaron Bentley
Merged shelf_v2
248
    result = TestSuite()
321.1.3 by Aaron Bentley
Fixed up Robert's test changes
249
    result.addTest(DocTestSuite(bzrtools, optionflags=ELLIPSIS))
246 by Aaron Bentley
Merged shelf_v2
250
    result.addTest(clean_tree.test_suite())
147.1.41 by Aaron Bentley
Merge from mainline
251
    result.addTest(DocTestSuite(baz_import))
252
    result.addTest(tests.test_suite())
247 by Aaron Bentley
Renamed tests.py to shelf_tests.py
253
    result.addTest(TestLoader().loadTestsFromModule(shelf_tests))
246 by Aaron Bentley
Merged shelf_v2
254
    result.addTest(blackbox.test_suite())
255
    return result