~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shelf.py

  • Committer: Aaron Bentley
  • Date: 2005-06-07 18:52:04 UTC
  • Revision ID: abentley@panoramicfeedback.com-20050607185204-5fc1f0e3d393b909
Added NEWS, obsoleted bzr-pull

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
 
3
 
from patches import parse_patches
4
 
import os
5
 
import sys
6
 
import string
7
 
import glob
8
 
import bzrlib
9
 
from bzrlib.commands import Command
10
 
from bzrlib.branch import Branch
11
 
from bzrlib import DEFAULT_IGNORE
12
 
from hunk_selector import ShelveHunkSelector, UnshelveHunkSelector
13
 
from diffstat import DiffStat
14
 
 
15
 
DEFAULT_IGNORE.append('./.bzr-shelf*')
16
 
 
17
 
class QuitException(Exception):
18
 
    pass
19
 
 
20
 
class Shelf(object):
21
 
    def __init__(self, location, name='default'):
22
 
        self.branch = Branch.open_containing(location)[0]
23
 
        base = self.branch.controlfilename('x-shelf')
24
 
        self.shelf_dir = os.path.join(base, name)
25
 
 
26
 
        # FIXME surely there's an easier way to do this?
27
 
        t = self.branch._transport
28
 
        for dir in [base, self.shelf_dir]:
29
 
            if not t.has(dir):
30
 
                t.mkdir(dir)
31
 
 
32
 
    def __path(self, idx):
33
 
        return os.path.join(self.shelf_dir, '%.2d' % idx)
34
 
 
35
 
    def next_shelf(self):
36
 
        index = 0
37
 
        while True:
38
 
            name = self.__path(index)
39
 
            if not os.path.exists(name):
40
 
                return name
41
 
            index += 1
42
 
 
43
 
    def last_shelf(self):
44
 
        shelves = os.listdir(self.shelf_dir)
45
 
        indexes = [int(f) for f in shelves]
46
 
        indexes.sort()
47
 
 
48
 
        if len(indexes) == 0:
49
 
            return None
50
 
 
51
 
        return self.__path(indexes[-1])
52
 
 
53
 
    def get_shelf_message(self, shelf):
54
 
        prefix = "# shelf: "
55
 
        if not shelf.startswith(prefix):
56
 
            return None
57
 
        return shelf[len(prefix):shelf.index('\n')]
58
 
 
59
 
    def unshelve(self, pick_hunks=False):
60
 
        shelf = self.last_shelf()
61
 
 
62
 
        if shelf is None:
63
 
            raise Exception("No shelf found in '%s'" % self.branch.base)
64
 
 
65
 
        patches = parse_patches(open(shelf, 'r').readlines())
66
 
        if pick_hunks:
67
 
            try:
68
 
                patches = UnshelveHunkSelector(patches).select()
69
 
            except QuitException:
70
 
                return False
71
 
 
72
 
        if len(patches) == 0:
73
 
            print >>sys.stderr, 'Nothing to unshelve'
74
 
            return True
75
 
 
76
 
        print >>sys.stderr, "Reapplying shelved patches",
77
 
        message = self.get_shelf_message(open(shelf, 'r').read())
78
 
        if message is not None:
79
 
            print >>sys.stderr, ' "%s"' % message
80
 
        else:
81
 
            print >>sys.stderr, ""
82
 
        pipe = os.popen('patch -d %s -s -p0' % self.branch.base, 'w')
83
 
        for patch in patches:
84
 
            pipe.write(str(patch))
85
 
        pipe.flush()
86
 
 
87
 
        if pipe.close() is not None:
88
 
            raise Exception("Failed running patch!")
89
 
 
90
 
        os.remove(shelf)
91
 
 
92
 
        diff_stat = DiffStat(self.get_patches(None, None))
93
 
        print 'Diff status is now:\n', diff_stat
94
 
 
95
 
        return True
96
 
 
97
 
    def get_patches(self, revision, file_list):
98
 
        from StringIO import StringIO
99
 
        from bzrlib.diff import show_diff
100
 
        out = StringIO()
101
 
        show_diff(self.branch, revision, specific_files=file_list, output=out)
102
 
        out.seek(0)
103
 
        return out.readlines()
104
 
 
105
 
    def shelve(self, pick_hunks=False, message=None, revision=None,
106
 
             file_list=None):
107
 
        patches = parse_patches(self.get_patches(revision, file_list))
108
 
 
109
 
        if pick_hunks:
110
 
            try:
111
 
                patches = ShelveHunkSelector(patches).select()
112
 
            except QuitException:
113
 
                return False
114
 
 
115
 
        if len(patches) == 0:
116
 
            print >>sys.stderr, 'Nothing to shelve'
117
 
            return True
118
 
 
119
 
        shelf = self.next_shelf()
120
 
        print >>sys.stderr, "Saving shelved patches to", shelf
121
 
        shelf = open(shelf, 'a')
122
 
        if message is not None:
123
 
            assert '\n' not in message
124
 
            shelf.write("# shelf: %s\n" % message)
125
 
        for patch in patches:
126
 
            shelf.write(str(patch))
127
 
 
128
 
        shelf.flush()
129
 
        os.fsync(shelf.fileno())
130
 
        shelf.close()
131
 
 
132
 
        print >>sys.stderr, "Reverting shelved patches"
133
 
        pipe = os.popen('patch -d %s -sR -p0' % self.branch.base, 'w')
134
 
        for patch in patches:
135
 
            pipe.write(str(patch))
136
 
        pipe.flush()
137
 
 
138
 
        if pipe.close() is not None:
139
 
            raise Exception("Failed running patch!")
140
 
 
141
 
        diff_stat = DiffStat(self.get_patches(None, None))
142
 
        print 'Diff status is now:\n', diff_stat
143
 
 
144
 
        return True
145