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