Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 0.9, 0.9.1, and 0.9.1.1

changeset: add the ability to show a patch

This feature can be used for "Import dialog" in the future.

Changeset cb4e28895de5

Parent 9b109409d761

by Yuki KODAMA

Changes to 3 files · Browse files at cb4e28895de5 Showing diff from parent 9b109409d761 Diff from another changeset...

 
129
130
131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
133
134
135
136
137
138
139
 
 
 
 
 
 
 
 
 
140
141
142
 
 
 
 
 
 
143
144
145
 
210
211
212
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
214
215
 
251
252
253
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254
255
256
 
129
130
131
132
133
134
135
136
137
138
139
140
141
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
172
173
 
 
 
 
174
175
176
177
178
179
180
181
182
183
 
 
184
185
186
187
188
189
190
191
192
 
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
290
291
292
293
294
295
296
297
298
299
300
301
302
 
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
@@ -129,17 +129,64 @@
  else:   self._filesel.select_path((0,))   + def load_patch_details(self, patchfile): + 'Load specified patch details into buffer and file list' + self._filelist.clear() + self._filelist.append(('*', _('[All Files]'), '')) + + # append files & collect hunks + self.currev = -1 + self.curphunks = {} + self.curpatch = patchfile + pf = open(self.curpatch) + try: + def get_path(a, b): + rawpath = b != '/dev/null' and b or a + return rawpath.split('/', 1)[-1] + hunks = [] + map = {'MODIFY': 'M', 'ADD': 'A', 'DELETE': 'R', + 'RENAME': '', 'COPY': ''} + for state, values in patch.iterhunks(self.ui, pf): + if state == 'git': + for m in values: + f = m.path + self._filelist.append((map[m.op], toutf(f), f)) + elif state == 'file': + path = get_path(values[0], values[1]) + self.curphunks[path] = hunks = ['diff', '', ''] + elif state == 'hunk': + hunks.extend([l.rstrip('\r\n') for l in values.hunk]) + else: + raise 'Unknown hunk type: %s' % state + finally: + pf.close() + + # select first file + if len(self._filelist) > 1: + self._filesel.select_path((1,)) + else: + self._filesel.select_path((0,)) +   def filelist_rowchanged(self, sel):   model, path = sel.get_selected()   if not path:   return - status, file_utf8, self.curfile = model[path] - self.generate_change_header() - if self.curfile: - self.append_diff(self.curfile) + status, file_utf8, file = model[path] + if self.currev != -1: + self.curfile = file + self.generate_change_header() + if self.curfile: + self.append_diff(self.curfile) + else: + for _, _, f in model: + self.append_diff(f)   else: - for _, _, f in model: - self.append_diff(f) + self.generate_patch_header() + if file: + self.append_patch_diff(file) + else: + for _, _, f in model: + self.append_patch_diff(f)     def generate_change_header(self):   buf, rev = self._buffer, self.currev @@ -210,6 +257,46 @@
  log = toutf(ctx.description())   buf.insert(eob, '\n' + log + '\n\n')   + def generate_patch_header(self): + buf = self._buffer + buf.set_text('') + eob = buf.get_end_iter() + + # copy from generate_change_header + def title_line(title, text, tag): + pad = ' ' * (12 - len(title)) + utext = toutf(title + pad + text) + buf.insert_with_tags_by_name(eob, utext, tag) + buf.insert(eob, '\n') + + pf = open(self.curpatch) + try: + data = patch.extract(self.ui, pf) + tmp, msg, user, date, branch, node, p1, p2 = data + try: + ud = toutf(user) + '\t' + displaytime(util.parsedate(date)) + msg = toutf(msg.rstrip('\r\n')) + patchname = os.path.basename(self.curpatch) + patchtitle = patchname + if node: + patchtitle += ' (%s)' % node[:12] + title_line(_('patch:'), patchtitle, 'changeset') + if branch: + title_line(_('branch:'), toutf(branch), 'greybg') + title_line(_('user/date:'), ud, 'changeset') + for pnode in (p1, p2): + if pnode is None: + continue + title = _('parent:') + title += ' ' * (12 - len(title)) + pnode[:12] + buf.insert_with_tags_by_name(eob, title, 'parent') + buf.insert(eob, '\n') + buf.insert(eob, '\n' + msg + '\n\n') + finally: + os.unlink(tmp) + finally: + pf.close() +   def append_diff(self, wfile):   if not wfile:   return @@ -251,6 +338,29 @@
  buf.apply_tag_by_name('mono', pos, eob)   return True   + def append_patch_diff(self, patchfile): + if not patchfile: + return + + # append diffs + buf = self._buffer + eob = buf.get_end_iter() + offset = eob.get_offset() + lines = self.curphunks[patchfile] + tags, lines = self.prepare_diff(lines, offset, patchfile) + for line in lines: + buf.insert(eob, line) + + # insert the tags + for name, p0, p1 in tags: + i0 = buf.get_iter_at_offset(p0) + i1 = buf.get_iter_at_offset(p1) + buf.apply_tag_by_name(name, i0, i1) + + sob, eob = buf.get_bounds() + pos = buf.get_iter_at_offset(offset) + buf.apply_tag_by_name('mono', pos, eob) +   def prepare_diff(self, difflines, offset, fname):   'Borrowed from hgview; parses changeset diffs'   def addtag( name, offset, length ):
Change 1 of 1 Show Entire File hggtk/​history.py Stacked
 
279
280
281
282
283
284
285
286
 
 
 
 
 
 
 
 
 
 
287
288
289
 
279
280
281
 
 
 
 
 
282
283
284
285
286
287
288
289
290
291
292
293
294
@@ -279,11 +279,16 @@
  self.reload_log()     def patch_selected(self, mqwidget, revid, patchname): - self.currevid = revid - if self.currevid != self.lastrevid: - self.lastrevid = self.currevid - self.changeview.opts['rev'] = [str(self.currevid)] - self.changeview.load_details(self.currevid) + if revid < 0: + patchfile = os.path.join(self.repo.root, '.hg', 'patches', patchname) + self.currevid = self.lastrevid = None + self.changeview.load_patch_details(patchfile) + else: + self.currevid = revid + if self.currevid != self.lastrevid: + self.lastrevid = self.currevid + self.changeview.opts['rev'] = [str(self.currevid)] + self.changeview.load_details(self.currevid)     def repo_invalidated(self, mqwidget):   self.reload_log()
Change 1 of 1 Show Entire File hggtk/​thgmq.py Stacked
 
633
634
635
636
 
637
638
 
 
639
640
641
 
633
634
635
 
636
637
 
638
639
640
641
642
@@ -633,9 +633,10 @@
  patchname = row[MQ_NAME]   try:   ctx = self.repo[patchname] - self.emit('patch-selected', ctx.rev(), patchname) + revid = ctx.rev()   except hglib.RepoError: - pass + revid = -1 + self.emit('patch-selected', revid, patchname)     def list_row_activated(self, list, path, column):   self.qgoto_by_row(self.model[path])