~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shelf.py

  • Committer: Michael Ellerman
  • Date: 2005-10-19 13:53:35 UTC
  • mto: (0.3.1 shelf-dev) (325.1.2 bzrtools)
  • mto: This revision was merged to the branch mainline in revision 246.
  • Revision ID: michael@ellerman.id.au-20051019135335-f8f9fa754da64acc
- Keep our branch around, and use it directly instead of bzr_root.
- Call show_diff() on our branch directly, rather than calling run_bzr()
- Which means we don't need run_bzr() anymore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
 
14
14
DEFAULT_IGNORE.append('./.bzr-shelf*')
15
15
 
16
 
def run_bzr(args):
17
 
    if type(args) is str:
18
 
        args = [ args ]
19
 
    pipe = os.popen('bzr %s' % string.join(args, ' '), 'r')
20
 
    lines = pipe.readlines()
21
 
    if pipe.close() is not None:
22
 
        raise Exception("Failed running bzr")
23
 
    return lines
24
 
 
25
16
class QuitException(Exception):
26
17
    pass
27
18
 
28
19
class Shelf(object):
29
20
    def __init__(self):
30
 
        b = Branch.open_containing('.')[0]
31
 
        self.bzr_root = b.base
 
21
        self.branch = Branch.open_containing('.')[0]
32
22
 
33
23
    def shelf_suffix(self, index):
34
24
        if index == 0:
43
33
                yield self.shelf_suffix(i)
44
34
                i = i + 1
45
35
 
46
 
        stem = os.path.join(self.bzr_root, '.bzr-shelf')
 
36
        stem = os.path.join(self.branch.base, '.bzr-shelf')
47
37
        for end in name_sequence():
48
38
            name = stem + end
49
39
            if not os.path.exists(name):
50
40
                return name
51
41
 
52
42
    def last_shelf(self):
53
 
        stem = os.path.join(self.bzr_root, '.bzr-shelf')
 
43
        stem = os.path.join(self.branch.base, '.bzr-shelf')
54
44
        shelves = glob.glob(stem)
55
45
        shelves.extend(glob.glob(stem + '-*'))
56
46
        def shelf_index(name):
74
64
        shelf = self.last_shelf()
75
65
 
76
66
        if shelf is None:
77
 
            raise Exception("No shelf found in '%s'" % self.bzr_root)
 
67
            raise Exception("No shelf found in '%s'" % self.branch.base)
78
68
 
79
69
        patch = open(shelf, 'r').read()
80
70
 
84
74
            print >>sys.stderr, ' "%s"' % message
85
75
        else:
86
76
            print >>sys.stderr, ""
87
 
        pipe = os.popen('patch -d %s -s -p0' % self.bzr_root, 'w')
 
77
        pipe = os.popen('patch -d %s -s -p0' % self.branch.base, 'w')
88
78
        pipe.write(patch)
89
79
        pipe.flush()
90
80
 
97
87
 
98
88
        return True
99
89
 
 
90
    def get_patches(self, revision, file_list):
 
91
        from StringIO import StringIO
 
92
        from bzrlib.diff import show_diff
 
93
        out = StringIO()
 
94
        show_diff(self.branch, revision, specific_files=file_list, output=out)
 
95
        out.seek(0)
 
96
        return out.readlines()
 
97
 
100
98
    def shelve(self, all_hunks=False, message=None, revision=None,
101
99
             file_list=None):
102
 
        cmd = ['diff']
103
 
        if revision is not None:
104
 
            cmd.extend(['--revision', str(revision[0])])
105
 
        if file_list is not None:
106
 
            cmd.extend(file_list)
107
 
        patches = parse_patches(run_bzr(cmd))
 
100
        patches = parse_patches(self.get_patches(revision, file_list))
 
101
 
108
102
        if not all_hunks:
109
103
            try:
110
104
                patches = HunkSelector(patches).select()
129
123
        shelf.close()
130
124
 
131
125
        print >>sys.stderr, "Reverting shelved patches"
132
 
        pipe = os.popen('patch -d %s -sR -p0' % self.bzr_root, 'w')
 
126
        pipe = os.popen('patch -d %s -sR -p0' % self.branch.base, 'w')
133
127
        for patch in patches:
134
128
            pipe.write(str(patch))
135
129
        pipe.flush()