57
57
def delete(self, patch):
58
58
path = self.__path_from_user(patch)
59
os.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
path = self.__path_from_user(patch)
63
66
sys.stdout.write(open(path).read())
78
81
def __path_from_user(self, patch_id):
80
83
patch_index = int(patch_id)
84
except (TypeError, ValueError):
82
85
raise CommandError("Invalid patch name '%s'" % patch_id)
84
87
path = self.__path(patch_index)
131
134
return patch[len(self.MESSAGE_PREFIX):patch.index('\n')]
133
def unshelve(self, patch_source, all_hunks=False, force=False):
136
def unshelve(self, patch_source, patch_name=None, all=False, force=False):
134
137
self._check_upgrade()
136
patch_name = self.last_patch()
138
139
if patch_name is None:
140
patch_path = self.last_patch()
142
patch_path = self.__path_from_user(patch_name)
144
if patch_path is None:
139
145
raise CommandError("No patch found on shelf %s" % self.name)
141
hunks = FilePatchSource(patch_name).readhunks()
147
patches = FilePatchSource(patch_path).readpatches()
149
to_unshelve = patches
146
to_unshelve, to_remain = UnshelveHunkSelector(hunks).select()
152
to_unshelve, to_remain = UnshelveHunkSelector(patches).select()
148
154
if len(to_unshelve) == 0:
149
155
raise CommandError('Nothing to unshelve')
151
message = self.get_patch_message(patch_name)
157
message = self.get_patch_message(patch_path)
152
158
if message is None:
153
159
message = "No message saved with patch."
154
160
self.log('Unshelving from %s/%s: "%s"\n' % \
155
(self.name, os.path.basename(patch_name), message))
161
(self.name, os.path.basename(patch_path), message))
158
164
self._run_patch(to_unshelve, dry_run=True)
159
165
self._run_patch(to_unshelve)
160
166
except PatchFailed:
162
self._run_patch(to_unshelve, strip=0, dry_run=True)
163
self._run_patch(to_unshelve, strip=0)
168
self._run_patch(to_unshelve, strip=1, dry_run=True)
169
self._run_patch(to_unshelve, strip=1)
164
170
except PatchFailed:
166
172
self.log('Warning: Unshelving failed, forcing as ' \
174
180
"longer applies cleanly to the working tree!")
176
182
# Backup the shelved patch
177
os.rename(patch_name, '%s~' % patch_name)
183
os.rename(patch_path, '%s~' % patch_path)
179
185
if len(to_remain) > 0:
180
f = open(patch_name, 'w')
181
for hunk in to_remain:
186
f = open(patch_path, 'w')
187
for patch in to_remain:
185
def shelve(self, patch_source, all_hunks=False, message=None):
191
def shelve(self, patch_source, all=False, message=None):
186
192
self._check_upgrade()
188
hunks = patch_source.readhunks()
194
patches = patch_source.readpatches()
193
to_shelve = ShelveHunkSelector(hunks).select()[0]
199
to_shelve = ShelveHunkSelector(patches).select()[0]
195
201
if len(to_shelve) == 0:
196
202
raise CommandError('Nothing to shelve')
199
205
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
200
206
message = "Changes shelved on %s" % timestamp
202
patch_name = self.next_patch()
208
patch_path = self.next_patch()
203
209
self.log('Shelving to %s/%s: "%s"\n' % \
204
(self.name, os.path.basename(patch_name), message))
210
(self.name, os.path.basename(patch_path), message))
206
patch = open(patch_name, 'a')
212
f = open(patch_path, 'a')
208
214
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())
215
f.write("%s%s\n" % (self.MESSAGE_PREFIX, message))
217
for patch in to_shelve:
219
225
self._run_patch(to_shelve, reverse=True, dry_run=True)
220
226
self._run_patch(to_shelve, reverse=True)
221
227
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)
229
self._run_patch(to_shelve, reverse=True, strip=1, dry_run=True)
230
self._run_patch(to_shelve, reverse=True, strip=1)
225
231
except PatchFailed:
226
232
raise CommandError("Failed removing shelved changes from the"
229
def _run_patch(self, patches, strip=1, reverse=False, dry_run=False):
235
def _run_patch(self, patches, strip=0, reverse=False, dry_run=False):
230
236
args = ['patch', '-d', self.base, '-s', '-p%d' % strip, '-f']
232
238
args.append('-R')