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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
|
# manifestmodel.py - Model for TortoiseHg manifest view
#
# Copyright (C) 2009-2010 LOGILAB S.A. <http://www.logilab.fr/>
# Copyright (C) 2010 Yuya Nishihara <yuya@tcha.org>
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
from __future__ import absolute_import
import os
import re
from .qtcore import (
QAbstractItemModel,
QMimeData,
QModelIndex,
QUrl,
Qt,
pyqtSignal,
pyqtSlot,
)
from .qtgui import (
QCompleter,
QFileIconProvider,
QIcon,
)
from mercurial import (
error,
match as matchmod,
)
from ..util import hglib
from . import (
filedata,
qtlib,
status,
visdiff,
)
_subrepoType2IcoMap = {
'hg': 'hg',
'hgsubversion': 'thg-svn-subrepo',
'git': 'thg-git-subrepo',
'svn': 'thg-svn-subrepo',
}
_subrepoStatus2IcoMap = {
'A': 'thg-added-subrepo',
'R': 'thg-removed-subrepo',
}
class ManifestModel(QAbstractItemModel):
"""Status of files between two revisions or patch"""
# emitted when all files of the revision has been loaded successfully
revLoaded = pyqtSignal(object)
StatusRole = Qt.UserRole + 1
"""Role for file change status"""
# -1 and None are valid revision number
FirstParent = -2
SecondParent = -3
def __init__(self, repoagent, parent=None, rev=None, namefilter=None,
statusfilter='MASC', flat=False):
QAbstractItemModel.__init__(self, parent)
self._fileiconprovider = QFileIconProvider()
self._iconcache = {} # (path, status, subkind): icon
self._repoagent = repoagent
self._namefilter = unicode(namefilter or '')
assert all(c in 'MARSC' for c in statusfilter)
self._statusfilter = statusfilter
self._changedfilesonly = False
self._nodeop = _nodeopmap[bool(flat)]
self._rootentry = self._newRevNode(rev)
self._populate = _populaterepo
self._rootpopulated = False
def data(self, index, role=Qt.DisplayRole):
if not index.isValid():
return
if role == Qt.DecorationRole:
return self.fileIcon(index)
if role == self.StatusRole:
return self.fileStatus(index)
e = index.internalPointer()
if role in (Qt.DisplayRole, Qt.EditRole):
return e.name
def filePath(self, index):
"""Return path at the given index [unicode]"""
if not index.isValid():
return ''
return index.internalPointer().path
def fileData(self, index):
"""Returns the displayable file data at the given index"""
repo = self._repoagent.rawRepo()
if not index.isValid():
return filedata.createNullData(repo)
f = index.internalPointer()
e = f.parent
while e and e.ctx is None:
e = e.parent
assert e, 'root entry must have ctx'
wfile = hglib.fromunicode(f.path[len(e.path):].lstrip('/'))
rpath = hglib.fromunicode(e.path)
if f.subkind:
# TODO: use subrepo ctxs and status resolved by this model
return filedata.createSubrepoData(e.ctx, e.pctx, wfile, f.status,
rpath, f.subkind)
if f.isdir:
return filedata.createDirData(e.ctx, e.pctx, wfile, rpath)
return filedata.createFileData(e.ctx, e.pctx, wfile, f.status, rpath)
def subrepoType(self, index):
"""Return the subrepo type the specified index"""
if not index.isValid():
return
e = index.internalPointer()
return e.subkind
def fileIcon(self, index):
if not index.isValid():
return QIcon()
e = index.internalPointer()
k = (e.path, e.status, e.subkind)
try:
return self._iconcache[k]
except KeyError:
self._iconcache[k] = ic = self._makeFileIcon(e)
return ic
def _makeFileIcon(self, e):
if e.subkind in _subrepoType2IcoMap:
ic = qtlib.geticon(_subrepoType2IcoMap[e.subkind])
# use fine-tuned status overlay if any
n = _subrepoStatus2IcoMap.get(e.status)
if n:
return qtlib.getoverlaidicon(ic, qtlib.geticon(n))
ic = qtlib.getoverlaidicon(ic, qtlib.geticon('thg-subrepo'))
elif e.isdir:
ic = self._fileiconprovider.icon(QFileIconProvider.Folder)
else:
# do not use fileiconprovier.icon(fileinfo), which may return icon
# with shell (i.e. status of working directory) overlay.
# default file icon looks ugly with status overlay on Windows
ic = qtlib.geticon('text-x-generic')
if not e.status:
return ic
st = status.statusTypes[e.status]
if st.icon:
icOverlay = qtlib.geticon(st.icon)
ic = qtlib.getoverlaidicon(ic, icOverlay)
return ic
def fileStatus(self, index):
"""Return the change status of the specified file"""
if not index.isValid():
return
e = index.internalPointer()
# TODO: 'S' should not be a status
if e.subkind:
return 'S'
return e.status
# TODO: this should be merged to fileStatus()
def subrepoStatus(self, index):
"""Return the change status of the specified subrepo"""
if not index.isValid():
return
e = index.internalPointer()
if not e.subkind:
return
return e.status
def isDir(self, index):
if not index.isValid():
return True # root entry must be a directory
e = index.internalPointer()
return e.isdir
def mimeData(self, indexes):
files = [self.filePath(i) for i in indexes if i.isValid()]
ctx = self._rootentry.ctx
if ctx.rev() is not None:
repo = self._repoagent.rawRepo()
lfiles = map(hglib.fromunicode, files)
lbase, _fns = visdiff.snapshot(repo, lfiles, ctx)
base = hglib.tounicode(lbase)
else:
# working copy
base = self._repoagent.rootPath()
m = QMimeData()
m.setUrls([QUrl.fromLocalFile(os.path.join(base, e)) for e in files])
return m
def mimeTypes(self):
return ['text/uri-list']
def flags(self, index):
f = super(ManifestModel, self).flags(index)
if not index.isValid():
return f
if not (self.isDir(index) or self.fileStatus(index) == 'R'
or self._populate is _populatepatch):
f |= Qt.ItemIsDragEnabled
return f
def index(self, row, column, parent=QModelIndex()):
if row < 0 or self.rowCount(parent) <= row or column != 0:
return QModelIndex()
return self.createIndex(row, column, self._parententry(parent).at(row))
def indexFromPath(self, path, column=0):
"""Return index for the specified path if found [unicode]
If not found, returns invalid index.
"""
if not path:
return QModelIndex()
try:
e = self._nodeop.findpath(self._rootentry, unicode(path))
except KeyError:
return QModelIndex()
return self.createIndex(e.parent.index(e.name), column, e)
def parent(self, index):
if not index.isValid():
return QModelIndex()
e = index.internalPointer()
if e.path:
return self.indexFromPath(e.parent.path, index.column())
else:
return QModelIndex()
def _parententry(self, parent):
if parent.isValid():
return parent.internalPointer()
else:
return self._rootentry
def rowCount(self, parent=QModelIndex()):
return len(self._parententry(parent))
def columnCount(self, parent=QModelIndex()):
return 1
def rev(self, parent=QModelIndex()):
"""Revision number of the current changectx"""
e = self._parententry(parent)
if e.ctx is None or not _isreporev(e.ctx.rev()):
return -1
return e.ctx.rev()
def baseRev(self, parent=QModelIndex()):
"""Revision of the base changectx where status is calculated from"""
e = self._parententry(parent)
if e.pctx is None or not _isreporev(e.pctx.rev()):
return -1
return e.pctx.rev()
def setRev(self, rev, prev=FirstParent):
"""Change to the specified repository revision; None for working-dir"""
roote = self._rootentry
newroote = self._newRevNode(rev, prev)
if (_samectx(newroote.ctx, roote.ctx)
and _samectx(newroote.pctx, roote.pctx)):
return
self._populate = _populaterepo
self._repopulateNodes(newroote=newroote)
if self._rootpopulated:
self.revLoaded.emit(self.rev())
def setRawContext(self, ctx):
"""Change to the specified changectx in place of repository revision"""
if _samectx(self._rootentry.ctx, ctx):
return
if _isreporev(ctx.rev()):
repo = self._repoagent.rawRepo()
try:
if ctx == repo[ctx.rev()]:
return self.setRev(ctx.rev())
except error.RepoLookupError:
pass
newroote = _Entry()
newroote.ctx = ctx
self._populate = _populatepatch
self._repopulateNodes(newroote=newroote)
if self._rootpopulated:
self.revLoaded.emit(self.rev())
def nameFilter(self):
"""Return the current name filter"""
return self._namefilter
@pyqtSlot(str)
def setNameFilter(self, pattern):
"""Filter file name by partial match of glob pattern"""
pattern = unicode(pattern)
if self._namefilter == pattern:
return
self._namefilter = pattern
self._repopulateNodes()
def statusFilter(self):
"""Return the current status filter"""
return self._statusfilter
# TODO: split or remove 'S' which causes several design flaws
@pyqtSlot(str)
def setStatusFilter(self, status):
"""Filter file tree by change status 'MARSC'"""
status = str(status)
assert all(c in 'MARSC' for c in status)
if self._statusfilter == status:
return # for performance reason
self._statusfilter = status
self._repopulateNodes()
def isChangedFilesOnly(self):
"""Whether or not to filter by ctx.files, i.e. to exclude files not
changed in the current revision.
If this filter is enabled, 'C' (clean) files are not listed. For
merge changeset, 'M' (modified) files in one side are also excluded.
"""
return self._changedfilesonly
def setChangedFilesOnly(self, changedonly):
if self._changedfilesonly == bool(changedonly):
return
self._changedfilesonly = bool(changedonly)
self._repopulateNodes()
def isFlat(self):
"""Whether all entries are listed in the same level or per directory"""
return self._nodeop is _listnodeop
def setFlat(self, flat):
if self.isFlat() == bool(flat):
return
# self._nodeop must be changed after layoutAboutToBeChanged; otherwise
# client code may obtain invalid indexes in its slot
self._repopulateNodes(newnodeop=_nodeopmap[bool(flat)])
def canFetchMore(self, parent):
if parent.isValid():
return False
return not self._rootpopulated
def fetchMore(self, parent):
if parent.isValid() or self._rootpopulated:
return
assert len(self._rootentry) == 0
newroote = self._rootentry.copyskel()
self._populateNodes(newroote)
last = len(newroote) - 1
if last >= 0:
self.beginInsertRows(parent, 0, last)
self._rootentry = newroote
self._rootpopulated = True
if last >= 0:
self.endInsertRows()
self.revLoaded.emit(self.rev())
def _repopulateNodes(self, newnodeop=None, newroote=None):
"""Recreate populated nodes if any"""
if not self._rootpopulated:
# no stale nodes
if newnodeop:
self._nodeop = newnodeop
if newroote:
self._rootentry = newroote
return
self.layoutAboutToBeChanged.emit()
try:
oldindexmap = [(i, self.filePath(i))
for i in self.persistentIndexList()]
if newnodeop:
self._nodeop = newnodeop
if not newroote:
newroote = self._rootentry.copyskel()
self._populateNodes(newroote)
self._rootentry = newroote
for oi, path in oldindexmap:
self.changePersistentIndex(oi, self.indexFromPath(path))
finally:
self.layoutChanged.emit()
def _newRevNode(self, rev, prev=FirstParent):
"""Create empty root node for the specified revision"""
if not _isreporev(rev):
raise ValueError('unacceptable revision number: %r' % rev)
if not _isreporev(prev):
raise ValueError('unacceptable parent revision number: %r' % prev)
repo = self._repoagent.rawRepo()
roote = _Entry()
roote.ctx = repo[rev]
if prev == ManifestModel.FirstParent:
roote.pctx = roote.ctx.p1()
elif prev == ManifestModel.SecondParent:
roote.pctx = roote.ctx.p2()
else:
roote.pctx = repo[prev]
return roote
def _populateNodes(self, roote):
repo = self._repoagent.rawRepo()
lpat = hglib.fromunicode(self._namefilter)
match = _makematcher(repo, roote.ctx, lpat, self._changedfilesonly)
self._populate(roote, repo, self._nodeop, self._statusfilter, match)
roote.sort()
class _Entry(object):
"""Each file or directory"""
__slots__ = ('_name', '_parent', 'status', 'ctx', 'pctx', 'subkind',
'_child', '_nameindex')
def __init__(self, name='', parent=None):
self._name = name
self._parent = parent
self.status = None
self.ctx = None
self.pctx = None
self.subkind = None
self._child = {}
self._nameindex = []
def copyskel(self):
"""Create unpopulated copy of this entry"""
e = self.__class__()
e.status = self.status
e.ctx = self.ctx
e.pctx = self.pctx
e.subkind = self.subkind
return e
@property
def parent(self):
return self._parent
@property
def path(self):
if self.parent is None or not self.parent.name:
return self.name
else:
return self.parent.path + '/' + self.name
@property
def name(self):
return self._name
@property
def isdir(self):
return bool(self.subkind or self._child)
def __len__(self):
return len(self._child)
def __nonzero__(self):
# leaf node should not be False because of len(node) == 0
return True
def __getitem__(self, name):
return self._child[name]
def makechild(self, name):
if name not in self._child:
self._nameindex.append(name)
self._child[name] = e = self.__class__(name, parent=self)
return e
def putchild(self, name, e):
assert not e.name and not e.parent
e._name = name
e._parent = self
if name not in self._child:
self._nameindex.append(name)
self._child[name] = e
def __contains__(self, item):
return item in self._child
def at(self, index):
return self._child[self._nameindex[index]]
def index(self, name):
return self._nameindex.index(name)
def sort(self, reverse=False):
"""Sort the entries recursively; directories first"""
for e in self._child.itervalues():
e.sort(reverse=reverse)
self._nameindex.sort(
key=lambda s: (not self[s].isdir, os.path.normcase(s)),
reverse=reverse)
def _isreporev(rev):
# patchctx.rev() returns str, which isn't a valid repository revision
return rev is None or isinstance(rev, int)
def _samectx(ctx1, ctx2):
# no fast way to detect changes in uncommitted ctx, just assumes different
if ctx1.rev() is None or not _isreporev(ctx1.rev()):
return False
# compare hash in case it was stripped and recreated (e.g. by qrefresh)
return ctx1 == ctx2 and ctx1.node() == ctx2.node()
# TODO: visual feedback to denote query type and error as in repofilter
def _makematcher(repo, ctx, pat, changedonly):
cwd = '' # always relative to repo root
patterns = []
if pat and ':' not in pat and '*' not in pat:
# mimic case-insensitive partial string match
patterns.append('relre:(?i)' + re.escape(pat))
elif pat:
patterns.append(pat)
include = []
if changedonly:
include.extend('path:%s' % p for p in ctx.files())
if not include:
# no match
return matchmod.exact(repo.root, cwd, [])
try:
return matchmod.match(repo.root, cwd, patterns, include=include,
default='relglob', auditor=repo.auditor, ctx=ctx)
except (error.Abort, error.ParseError):
# no match
return matchmod.exact(repo.root, cwd, [])
class _listnodeop(object):
subreporecursive = False
@staticmethod
def findpath(e, path):
return e[path]
@staticmethod
def makepath(e, path):
return e.makechild(path)
@staticmethod
def putpath(e, path, c):
e.putchild(path, c)
class _treenodeop(object):
subreporecursive = True
@staticmethod
def findpath(e, path):
for p in path.split('/'):
e = e[p]
return e
@staticmethod
def makepath(e, path):
for p in path.split('/'):
if p not in e:
e.makechild(p)
e = e[p]
return e
@staticmethod
def putpath(e, path, c):
rp = path.rfind('/')
if rp >= 0:
e = _treenodeop.makepath(e, path[:rp])
e.putchild(path[rp + 1:], c)
_nodeopmap = {
False: _treenodeop,
True: _listnodeop,
}
def _populaterepo(roote, repo, nodeop, statusfilter, match):
if 'S' in statusfilter:
_populatesubrepos(roote, repo, nodeop, statusfilter, match)
ctx = roote.ctx
pctx = roote.pctx
repo.lfstatus = True
try:
stat = repo.status(pctx, ctx, match, clean='C' in statusfilter)
finally:
repo.lfstatus = False
for st, files in zip('MAR!?IC', stat):
if st not in statusfilter:
continue
for path in files:
e = nodeop.makepath(roote, hglib.tounicode(path))
e.status = st
def _comparesubstate(state1, state2):
if state1 == state2:
return 'C'
elif state1 == hglib.nullsubrepostate:
return 'A'
elif state2 == hglib.nullsubrepostate:
return 'R'
else:
return 'M'
def _populatesubrepos(roote, repo, nodeop, statusfilter, match):
ctx = roote.ctx
pctx = roote.pctx
subpaths = set(pctx.substate)
subpaths.update(ctx.substate)
for path in subpaths:
substate = ctx.substate.get(path, hglib.nullsubrepostate)
psubstate = pctx.substate.get(path, hglib.nullsubrepostate)
e = _Entry()
e.status = _comparesubstate(psubstate, substate)
if e.status == 'R':
# denotes the original subrepo has been removed
e.subkind = psubstate[2]
else:
e.subkind = substate[2]
# do not call ctx.sub() unnecessarily, which may raise Abort or OSError
# if git or svn executable not found
if (nodeop.subreporecursive and e.subkind == 'hg' and e.status != 'R'
and os.path.isdir(repo.wjoin(path))):
smatch = matchmod.subdirmatcher(path, match)
try:
srepo = ctx.sub(path)._repo
e.ctx = srepo[substate[1]]
e.pctx = srepo[psubstate[1] or 'null']
_populaterepo(e, srepo, nodeop, statusfilter, smatch)
except (error.RepoError, EnvironmentError):
pass
# subrepo is filtered out only if the node and its children do not
# match the specified condition at all
if len(e) > 0 or (e.status in statusfilter and match(path)):
nodeop.putpath(roote, hglib.tounicode(path), e)
def _populatepatch(roote, repo, nodeop, statusfilter, match):
ctx = roote.ctx
stat = ctx.changesToParent(0)
for st, files in zip('MAR', stat):
if st not in statusfilter:
continue
for path in files:
if not match(path):
continue
e = nodeop.makepath(roote, hglib.tounicode(path))
e.status = st
class ManifestCompleter(QCompleter):
"""QCompleter for ManifestModel"""
def splitPath(self, path):
"""
>>> c = ManifestCompleter()
>>> c.splitPath(u'foo/bar')
[u'foo', u'bar']
trailing slash appends extra '', so that QCompleter can descend to
next level:
>>> c.splitPath(u'foo/')
[u'foo', u'']
"""
return unicode(path).split('/')
def pathFromIndex(self, index):
if not index.isValid():
return ''
m = self.model()
if not m:
return ''
return m.filePath(index)
|