~abentley/bzrtools/bzrtools.dev

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
#!/usr/bin/python

import bzrlib.branch
from errors import CommandError
from bzrlib.option import Option
from patchsource import BzrPatchSource
from shelf import Shelf
from bzrlib import DEFAULT_IGNORE

DEFAULT_IGNORE.append('./.shelf')

class cmd_shelve(bzrlib.commands.Command):
    """Temporarily set aside some changes to the current working tree.

    Shelve allows you to temporarily put changes you've made "on the shelf",
    ie. out of the way, until a later time when you can bring them back from
    the shelf with the 'unshelve' command.

    You can put multiple items on the shelf, each time you run unshelve the
    most recently shelved changes will be reinstated.

    If filenames are specified, only the changes to those files will be
    shelved, other files will be left untouched.

    If a revision is specified, changes since that revision will be shelved.

    If you specifiy "--pick" you'll be prompted for each hunk of the diff as
    to whether you want to shelve it or not. Press "?" at the prompt for help.
    """
    takes_args = ['file*']
    takes_options = [Option('pick'), 'message', 'revision']
    def run(self, pick=False, file_list=None, message=None, revision=None):
        if revision is not None and revision:
            if len(revision) == 1:
                revision = revision[0]
            else:
                raise CommandError("shelve only accepts a single revision "
                                  "parameter.")

        source = BzrPatchSource(revision, file_list)
        s = Shelf(source.branch.base)
        s.shelve(source, pick, message)
        return 0

class cmd_shelf(bzrlib.commands.Command):
    """Perform various operations on your shelved patches.

    Subcommands:
        list   (ls)           List the patches on the current shelf.
        delete (del) <patch>  Delete a patch from the current shelf.
        switch       <shelf>  Switch to the named shelf, create it if necessary.
        show         <patch>  Show the contents of the specified patch.
    """
    takes_args = ['subcommand', 'args*']

    def run(self, subcommand, args_list):
        import sys

        source = BzrPatchSource()
        s = Shelf(source.branch.base)

        if subcommand == 'ls' or subcommand == 'list':
            self.__check_no_args(args_list, "shelf list takes no arguments!")
            s.list()
        elif subcommand == 'delete' or subcommand == 'del':
            self.__check_one_arg(args_list, "shelf delete takes one argument!")
            s.delete(args_list[0])
        elif subcommand == 'switch':
            self.__check_one_arg(args_list, "shelf switch takes one argument!")
            s = Shelf(source.branch.base, args_list[0])
            s.make_default()
        elif subcommand == 'show':
            self.__check_one_arg(args_list, "shelf show takes one argument!")
            s.display(args_list[0])
        else:
            print subcommand, args_list
            print >>sys.stderr, "Unknown shelf subcommand '%s'" % subcommand

    def __check_one_arg(self, args, msg):
        if args is None or len(args) != 1:
            raise BzrCommandError(msg)

    def __check_no_args(self, args, msg):
        if args is not None:
            raise BzrCommandError(msg)

class cmd_unshelve(bzrlib.commands.Command):
    """Reinstate the most recently shelved changes.
    See 'shelve' for more information.
    """
    takes_options = [Option('pick')]
    def run(self, pick=False):
        source = BzrPatchSource()
        s = Shelf(source.branch.base)
        s.unshelve(source, pick)
        return 0

bzrlib.commands.register_command(cmd_shelf)
bzrlib.commands.register_command(cmd_shelve)
bzrlib.commands.register_command(cmd_unshelve)

def test_suite():
    from bzrlib.tests.TestUtil import TestLoader
    import tests
    return TestLoader().loadTestsFromModule(tests)