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 HunkSelector
13
from diffstat import DiffStat
15
DEFAULT_IGNORE.append('./.bzr-shelf*')
17
class QuitException(Exception):
21
def __init__(self, location):
22
self.branch = Branch.open_containing(location)[0]
24
def shelf_suffix(self, index):
34
yield self.shelf_suffix(i)
37
stem = os.path.join(self.branch.base, '.bzr-shelf')
38
for end in name_sequence():
40
if not os.path.exists(name):
44
stem = os.path.join(self.branch.base, '.bzr-shelf')
45
shelves = glob.glob(stem)
46
shelves.extend(glob.glob(stem + '-*'))
47
def shelf_index(name):
50
return int(name[len(stem)+1:])
51
shelvenums = [shelf_index(f) for f in shelves]
54
if len(shelvenums) == 0:
56
return stem + self.shelf_suffix(shelvenums[-1])
58
def get_shelf_message(self, shelf):
60
if not shelf.startswith(prefix):
62
return shelf[len(prefix):shelf.index('\n')]
65
shelf = self.last_shelf()
68
raise Exception("No shelf found in '%s'" % self.branch.base)
70
patch = open(shelf, 'r').read()
72
print >>sys.stderr, "Reapplying shelved patches",
73
message = self.get_shelf_message(patch)
74
if message is not None:
75
print >>sys.stderr, ' "%s"' % message
77
print >>sys.stderr, ""
78
pipe = os.popen('patch -d %s -s -p0' % self.branch.base, 'w')
82
if pipe.close() is not None:
83
raise Exception("Failed running patch!")
87
diff_stat = DiffStat(self.get_patches(None, None))
88
print 'Diff status is now:\n', diff_stat
92
def get_patches(self, revision, file_list):
93
from StringIO import StringIO
94
from bzrlib.diff import show_diff
96
show_diff(self.branch, revision, specific_files=file_list, output=out)
98
return out.readlines()
100
def shelve(self, all_hunks=False, message=None, revision=None,
102
patches = parse_patches(self.get_patches(revision, file_list))
106
patches = HunkSelector(patches).select()
107
except QuitException:
110
if len(patches) == 0:
111
print >>sys.stderr, 'Nothing to shelve'
114
shelf = self.next_shelf()
115
print >>sys.stderr, "Saving shelved patches to", shelf
116
shelf = open(shelf, 'a')
117
if message is not None:
118
assert '\n' not in message
119
shelf.write("# shelf: %s\n" % message)
120
for patch in patches:
121
shelf.write(str(patch))
124
os.fsync(shelf.fileno())
127
print >>sys.stderr, "Reverting shelved patches"
128
pipe = os.popen('patch -d %s -sR -p0' % self.branch.base, 'w')
129
for patch in patches:
130
pipe.write(str(patch))
133
if pipe.close() is not None:
134
raise Exception("Failed running patch!")
136
diff_stat = DiffStat(self.get_patches(None, None))
137
print 'Diff status is now:\n', diff_stat