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
14
DEFAULT_IGNORE.append('./.bzr-shelf*')
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")
25
class QuitException(Exception):
30
self.bzr_root = run_bzr('root')[0].strip()
32
def shelf_suffix(self, index):
42
yield self.shelf_suffix(i)
45
stem = os.path.join(self.bzr_root, '.bzr-shelf')
46
for end in name_sequence():
48
if not os.path.exists(name):
52
stem = os.path.join(self.bzr_root, '.bzr-shelf')
53
shelves = glob.glob(stem)
54
shelves.extend(glob.glob(stem + '-*'))
55
def shelf_index(name):
58
return int(name[len(stem)+1:])
59
shelvenums = [shelf_index(f) for f in shelves]
62
if len(shelvenums) == 0:
64
return stem + self.shelf_suffix(shelvenums[-1])
66
def get_shelf_message(self, shelf):
68
if not shelf.startswith(prefix):
70
return shelf[len(prefix):shelf.index('\n')]
73
shelf = self.last_shelf()
76
raise Exception("No shelf found in '%s'" % self.bzr_root)
78
patch = open(shelf, 'r').read()
80
print >>sys.stderr, "Reapplying shelved patches",
81
message = self.get_shelf_message(patch)
82
if message is not None:
83
print >>sys.stderr, ' "%s"' % message
85
print >>sys.stderr, ""
86
pipe = os.popen('patch -d %s -s -p0' % self.bzr_root, 'w')
90
if pipe.close() is not None:
91
raise Exception("Failed running patch!")
94
print 'Diff status is now:'
95
os.system('bzr diff | diffstat')
99
def shelve(self, message=None, revision=None, file_list=None):
101
if revision is not None:
102
cmd.extend(['--revision', str(revision[0])])
103
if file_list is not None:
104
cmd.extend(file_list)
105
patches = parse_patches(run_bzr(cmd))
107
patches = HunkSelector(patches).select()
108
except QuitException:
111
if len(patches) == 0:
112
print >>sys.stderr, 'Nothing to shelve'
115
shelf = self.next_shelf()
116
print >>sys.stderr, "Saving shelved patches to", shelf
117
shelf = open(shelf, 'a')
118
if message is not None:
119
assert '\n' not in message
120
shelf.write("# shelf: %s\n" % message)
121
for patch in patches:
122
shelf.write(str(patch))
125
os.fsync(shelf.fileno())
128
print >>sys.stderr, "Reverting shelved patches"
129
pipe = os.popen('patch -d %s -sR -p0' % self.bzr_root, 'w')
130
for patch in patches:
131
pipe.write(str(patch))
134
if pipe.close() is not None:
135
raise Exception("Failed running patch!")
137
print 'Diff status is now:'
138
os.system('bzr diff | diffstat')