3
from patches import parse_patches
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
15
DEFAULT_IGNORE.append('./.bzr-shelf*')
17
class QuitException(Exception):
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)
26
# FIXME surely there's an easier way to do this?
27
t = self.branch._transport
28
for dir in [base, self.shelf_dir]:
32
def __path(self, idx):
33
return os.path.join(self.shelf_dir, '%.2d' % idx)
38
name = self.__path(index)
39
if not os.path.exists(name):
44
shelves = os.listdir(self.shelf_dir)
45
indexes = [int(f) for f in shelves]
51
return self.__path(indexes[-1])
53
def get_shelf_message(self, shelf):
55
if not shelf.startswith(prefix):
57
return shelf[len(prefix):shelf.index('\n')]
59
def unshelve(self, pick_hunks=False):
60
shelf = self.last_shelf()
63
raise Exception("No shelf found in '%s'" % self.branch.base)
65
patches = parse_patches(open(shelf, 'r').readlines())
68
patches = UnshelveHunkSelector(patches).select()
73
print >>sys.stderr, 'Nothing to unshelve'
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
81
print >>sys.stderr, ""
82
pipe = os.popen('patch -d %s -s -p0' % self.branch.base, 'w')
84
pipe.write(str(patch))
87
if pipe.close() is not None:
88
raise Exception("Failed running patch!")
92
diff_stat = DiffStat(self.get_patches(None, None))
93
print 'Diff status is now:\n', diff_stat
97
def get_patches(self, revision, file_list):
98
from StringIO import StringIO
99
from bzrlib.diff import show_diff
101
show_diff(self.branch, revision, specific_files=file_list, output=out)
103
return out.readlines()
105
def shelve(self, pick_hunks=False, message=None, revision=None,
107
patches = parse_patches(self.get_patches(revision, file_list))
111
patches = ShelveHunkSelector(patches).select()
112
except QuitException:
115
if len(patches) == 0:
116
print >>sys.stderr, 'Nothing to shelve'
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))
129
os.fsync(shelf.fileno())
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))
138
if pipe.close() is not None:
139
raise Exception("Failed running patch!")
141
diff_stat = DiffStat(self.get_patches(None, None))
142
print 'Diff status is now:\n', diff_stat