6
4
from datetime import datetime
7
from errors import CommandError, PatchFailed
5
from errors import CommandError, PatchFailed, PatchInvokeError
8
6
from hunk_selector import ShelveHunkSelector, UnshelveHunkSelector
7
from patch import run_patch
9
8
from patchsource import PatchSource, FilePatchSource
9
from bzrlib.osutils import rename
11
11
class Shelf(object):
12
12
MESSAGE_PREFIX = "# Shelved patch: "
57
57
def delete(self, patch):
58
58
path = self.__path_from_user(patch)
59
rename(path, '%s~' % path)
61
def display(self, patch):
62
path = self.__path_from_user(patch)
61
def display(self, patch=None):
63
path = self.last_patch()
65
raise CommandError("No patches on shelf.")
67
path = self.__path_from_user(patch)
63
68
sys.stdout.write(open(path).read())
131
136
return patch[len(self.MESSAGE_PREFIX):patch.index('\n')]
133
def unshelve(self, patch_source, all_hunks=False, force=False):
138
def unshelve(self, patch_source, patch_name=None, all=False, force=False,
134
140
self._check_upgrade()
136
patch_name = self.last_patch()
142
if no_color is False:
138
146
if patch_name is None:
147
patch_path = self.last_patch()
149
patch_path = self.__path_from_user(patch_name)
151
if patch_path is None:
139
152
raise CommandError("No patch found on shelf %s" % self.name)
141
hunks = FilePatchSource(patch_name).readhunks()
154
patches = FilePatchSource(patch_path).readpatches()
156
to_unshelve = patches
146
to_unshelve, to_remain = UnshelveHunkSelector(hunks).select()
159
hs = UnshelveHunkSelector(patches, color)
160
to_unshelve, to_remain = hs.select()
148
162
if len(to_unshelve) == 0:
149
163
raise CommandError('Nothing to unshelve')
151
message = self.get_patch_message(patch_name)
165
message = self.get_patch_message(patch_path)
152
166
if message is None:
153
167
message = "No message saved with patch."
154
168
self.log('Unshelving from %s/%s: "%s"\n' % \
155
(self.name, os.path.basename(patch_name), message))
169
(self.name, os.path.basename(patch_path), message))
158
172
self._run_patch(to_unshelve, dry_run=True)
159
173
self._run_patch(to_unshelve)
160
174
except PatchFailed:
162
self._run_patch(to_unshelve, strip=0, dry_run=True)
163
self._run_patch(to_unshelve, strip=0)
176
self._run_patch(to_unshelve, strip=1, dry_run=True)
177
self._run_patch(to_unshelve, strip=1)
164
178
except PatchFailed:
166
180
self.log('Warning: Unshelving failed, forcing as ' \
174
188
"longer applies cleanly to the working tree!")
176
190
# Backup the shelved patch
177
os.rename(patch_name, '%s~' % patch_name)
191
rename(patch_path, '%s~' % patch_path)
179
193
if len(to_remain) > 0:
180
f = open(patch_name, 'w')
181
for hunk in to_remain:
194
f = open(patch_path, 'w')
195
for patch in to_remain:
185
def shelve(self, patch_source, all_hunks=False, message=None):
199
def shelve(self, patch_source, all=False, message=None, no_color=False):
186
200
self._check_upgrade()
188
hunks = patch_source.readhunks()
193
to_shelve = ShelveHunkSelector(hunks).select()[0]
201
if no_color is False:
206
patches = patch_source.readpatches()
211
to_shelve = ShelveHunkSelector(patches, color).select()[0]
195
213
if len(to_shelve) == 0:
196
214
raise CommandError('Nothing to shelve')
199
217
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
200
218
message = "Changes shelved on %s" % timestamp
202
patch_name = self.next_patch()
220
patch_path = self.next_patch()
203
221
self.log('Shelving to %s/%s: "%s"\n' % \
204
(self.name, os.path.basename(patch_name), message))
222
(self.name, os.path.basename(patch_path), message))
206
patch = open(patch_name, 'a')
224
f = open(patch_path, 'a')
208
226
assert '\n' not in message
209
patch.write("%s%s\n" % (self.MESSAGE_PREFIX, message))
211
for hunk in to_shelve:
212
patch.write(str(hunk))
215
os.fsync(patch.fileno())
227
f.write("%s%s\n" % (self.MESSAGE_PREFIX, message))
229
for patch in to_shelve:
219
237
self._run_patch(to_shelve, reverse=True, dry_run=True)
220
238
self._run_patch(to_shelve, reverse=True)
221
239
except PatchFailed:
223
self._run_patch(to_shelve, reverse=True, strip=0, dry_run=True)
224
self._run_patch(to_shelve, reverse=True, strip=0)
241
self._run_patch(to_shelve, reverse=True, strip=1, dry_run=True)
242
self._run_patch(to_shelve, reverse=True, strip=1)
225
243
except PatchFailed:
226
244
raise CommandError("Failed removing shelved changes from the"
229
def _run_patch(self, patches, strip=1, reverse=False, dry_run=False):
230
args = ['patch', '-d', self.base, '-s', '-p%d' % strip, '-f']
234
args.append('--dry-run')
235
stdout = stderr = subprocess.PIPE
237
stdout = stderr = None
239
process = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=stdout,
241
for patch in patches:
242
process.stdin.write(str(patch))
244
process.communicate()
246
result = process.wait()
247
def _run_patch(self, patches, strip=0, reverse=False, dry_run=False):
248
run_patch(self.base, patches, strip, reverse, dry_run)
252
250
def _check_upgrade(self):
253
251
if len(self._list_old_shelves()) > 0: