Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 1.9.2, 1.9.3, and 2.0

chunks: initial implementation of chunk movement functions

I expect these will need a good amount more tweaking and error handling, but
the basic functionality is there.

Changeset 274a19501200

Parent f8db13c42758

by Steve Borho

Changes to one file · Browse files at 274a19501200 Showing diff from parent f8db13c42758 Diff from another changeset...

 
9
10
11
 
12
13
14
 
111
112
113
 
 
114
115
116
117
118
119
 
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
160
161
162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
164
165
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
167
168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
170
171
 
9
10
11
12
13
14
15
 
112
113
114
115
116
117
 
 
118
119
120
 
143
144
145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
 
 
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
 
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
 
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
@@ -9,6 +9,7 @@
 import os    from mercurial import hg, util, patch +from mercurial import match as matchmod  from hgext import record    from tortoisehg.util import hglib @@ -111,9 +112,9 @@
  revertall = True   ctx = self.filelistmodel._ctx   if isinstance(ctx, patchctx): + repo.thgbackup(ctx._path) + fp = util.atomictempfile(ctx._path, 'wb')   try: - repo.thgbackup(ctx._path) - fp = util.atomictempfile(ctx._path, 'wb')   if ctx._ph.comments:   fp.write('\n'.join(ctx._ph.comments))   fp.write('\n\n') @@ -142,30 +143,147 @@
  self.showMessage.emit(_('file hsa been modified, refresh'))   return   repo.thgbackup(repo.wjoin(self.currentFile)) - if revertall: - hg.revert(repo, repo.dirstate.parents()[0], - lambda a: a == self.currentFile) - else: - repo.wopener(self.currentFile, 'wb').write( - self.diffbrowse.origcontents) - fp = cStringIO.StringIO() - chunks[0].write(fp) - for c in kchunks: - c.write(fp) - fp.seek(0) - pfiles = {} - patch.internalpatch(fp, repo.ui, 1, repo.root, files=pfiles, - eolmode=None) + wlock = repo.wlock() + try: + if revertall: + hg.revert(repo, repo.dirstate.parents()[0], + lambda a: a == self.currentFile) + else: + repo.wopener(self.currentFile, 'wb').write( + self.diffbrowse.origcontents) + fp = cStringIO.StringIO() + chunks[0].write(fp) + for c in kchunks: + c.write(fp) + fp.seek(0) + pfiles = {} + patch.internalpatch(fp, repo.ui, 1, repo.root, files=pfiles, + eolmode=None) + finally: + wlock.release()   self.fileModified.emit()   - def addFile(self, wfile, chunks): - pass + def mergeChunks(self, wfile, chunks): + def isAorR(header): + for line in header: + if line.startswith('--- /dev/null'): + return True + if line.startswith('+++ /dev/null'): + return True + return False + repo = self.repo + ctx = self.filelistmodel._ctx + if isinstance(ctx, patchctx): + if wfile in ctx._files: + patchchunks = ctx._files[wfile] + if isAorR(chunks[0].header) or isAorR(patchchunks[0].header): + qtlib.InfoMsgBox(_('Unable to merge chunks'), + _('Add or remove patches must be merged' + ' in the working directory')) + return False + # merge new chunks into existing chunks, sorting on start line + newchunks = chunks[0] + pidx = nidx = 1 + while pidx < len(patchchunks) and nidx < len(chunks): + if pidx == len(patchchunks): + newchunks.append(chunks[nidx]) + nidx += 1 + elif nidx == len(chunks): + newchunks.append(patchchunks[pidx]) + pidx += 1 + elif chunks[nidx].toline < patchchunks[pidx].toline: + newchunks.append(chunks[nidx]) + nidx += 1 + else: + newchunks.append(patchchunks[pidx]) + pidx += 1 + ctx._files[wfile] = newchunks + else: + # add file to patch + ctx._files[wfile] = chunks + ctx._fileorder.append(wfile) + repo.thgbackup(ctx._path) + fp = util.atomictempfile(ctx._path, 'wb') + try: + if ctx._ph.comments: + fp.write('\n'.join(ctx._ph.comments)) + fp.write('\n\n') + for file in ctx._fileorder: + for chunk in ctx._files[wfile]: + chunk.write(fp) + fp.rename() + self.fileModified.emit() + return True + finally: + del fp + return False + else: + # Apply chunks to wfile + repo.thgbackup(repo.wjoin(wfile)) + fp = cStringIO.StringIO() + for c in chunks: + c.write(fp) + fp.seek(0) + wlock = repo.wlock() + try: + try: + pfiles = {} + patch.internalpatch(fp, repo.ui, 1, repo.root, files=pfiles, + eolmode=None) + hglib.updatedir(repo.ui, repo, pfiles) + # TODO: detect patch rejects, offer to open editor + return True + except patch.PatchError, err: + self.showMessage.emit(hglib.tounicode(str(err))) + finally: + wlock.release() + return False     def removeFile(self, wfile): - pass + repo = self.repo + ctx = self.filelistmodel._ctx + if isinstance(ctx, patchctx): + repo.thgbackup(ctx._path) + fp = util.atomictempfile(ctx._path, 'wb') + try: + if ctx._ph.comments: + fp.write('\n'.join(ctx._ph.comments)) + fp.write('\n\n') + for file in ctx._fileorder: + if file == wfile: + continue + for chunk in ctx._files[wfile]: + chunk.write(fp) + fp.rename() + finally: + del fp + else: + repo.thgbackup(repo.wjoin(wfile)) + try: + wlock = repo.wlock() + hg.revert(repo, repo.dirstate.parents()[0], + lambda a: a == wfile) + finally: + wlock.release() + self.fileModified.emit()     def getChunksForFile(self, wfile): - pass + repo = self.repo + ctx = self.filelistmodel._ctx + if isinstance(ctx, patchctx): + if wfile in ctx._files: + return ctx._files[wfile] + else: + return [] + else: + buf = cStringIO.StringIO() + diffopts = patch.diffopts(repo.ui, {'git':True}) + m = matchmod.exact(repo.root, repo.root, [wfile]) + for p in patch.diff(repo, ctx.p1().node(), None, match=m, + opts=diffopts): + buf.write(p) + buf.seek(0) + return record.parsepatch(buf)     @pyqtSlot(object, object, object)   def displayFile(self, file, rev, status):