Kiln » TortoiseHg » TortoiseHg
Clone URL:  
chunkselect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# chunkselect.py - Change chunk selection dialog # # Copyright 2010 Steve Borho <steve@borho.org> # # This software may be used and distributed according to the terms of the # GNU General Public License version 2, incorporated herein by reference. import sys import cStringIO from PyQt4.QtCore import * from PyQt4.QtGui import * from mercurial import hg, commands, util, cmdutil, mdiff, error, patch from tortoisehg.util import hgshelve, hglib from tortoisehg.util.i18n import _ from tortoisehg.hgqt.htmllistview import HtmlListView from tortoisehg.hgqt import htmlui def check_max_diff(ctx, wfile): lines = [] try: fctx = ctx.filectx(wfile) except error.LookupError: fctx = None if fctx and fctx.size() > hglib.getmaxdiffsize(ctx._repo.ui): # Fake patch that displays size warning lines = ['diff --git a/%s b/%s\n' % (wfile, wfile)] lines.append(_('File is larger than the specified max size.\n')) lines.append(_('Hunk selection is disabled for this file.\n')) lines.append('--- a/%s\n' % wfile) lines.append('+++ b/%s\n' % wfile) elif '\0' in fctx.data(): # Fake patch that displays binary file warning lines = ['diff --git a/%s b/%s\n' % (wfile, wfile)] lines.append(_('File is binary.\n')) lines.append(_('Hunk selection is disabled for this file.\n')) lines.append('--- a/%s\n' % wfile) lines.append('+++ b/%s\n' % wfile) return lines class ChunkModel(QAbstractListModel): def __init__(self, chunks): QAbstractTableModel.__init__(self) self.chunks = chunks def rowCount(self, parent): if not parent.isValid(): return len(self.chunks) return 0 def columnCount(self): return 1 def data(self, index, role): if not index.isValid() or role != Qt.DisplayRole: return QVariant() if index.row() < 0 or index.row() >= len(self.chunks): return QVariant() return QVariant(self.chunks[index.row()]) def headerData(self, col, orientation, role): if col != 0 or role != Qt.DisplayRole or orientation != Qt.Horizontal: return QVariant() else: return QVariant("Change chunks") def flags(self, index): return Qt.ItemIsSelectable | Qt.ItemIsEnabled def run(ui, *pats, **opts): repo = hg.repository(ui) fp = cStringIO.StringIO() matcher = cmdutil.matchall(repo) diffopts = mdiff.diffopts(git=True, nodates=True) try: node2, node1 = repo['tip'].node(), repo['tip'].parents()[0].node() for s in patch.diff(repo, node1, node2, match=matcher, opts=diffopts): fp.writelines(s.splitlines(True)) except (IOError, error.RepoError, error.LookupError, util.Abort), e: self.stat.stbar.set_text(str(e)) fp.seek(0) hu = htmlui.htmlui() items = [] for chunk in hgshelve.parsepatch(fp): fp = cStringIO.StringIO() chunk.write(fp) fp.seek(0) for a, l in patch.difflabel(fp.readlines): if a: hu.write(a, label=l) o, e = hu.getdata() items.append(o) cm = ChunkModel(items) lv = HtmlListView(cm) lv.show() return lv