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

guess: add progress/status bar

My attempts to pass the data via DataWrapper objects was met with
SIGSEGV at random times, and bizarre tracebacks from signal handler
functions about QObject instances not having 'data' members.

Not sure what to make of it, but this impl seems to be solid. Bits
of this (ProgUi) should eventually be moved to common code.

Changeset ff6e7a012b30

Parent c6e5151eacec

by Steve Borho

Changes to one file · Browse files at ff6e7a012b30 Showing diff from parent c6e5151eacec Diff from another changeset...

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
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
 # guess.py - TortoiseHg's dialogs for detecting copies and renames  #  # 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 os    from mercurial import hg, ui, mdiff, cmdutil, util, error, similar    from tortoisehg.util import hglib, shlib, paths    from tortoisehg.hgqt.i18n import _ -from tortoisehg.hgqt import qtlib, htmlui +from tortoisehg.hgqt import qtlib, htmlui, cmdui    from PyQt4.QtCore import *  from PyQt4.QtGui import *    # Technical Debt -# Add a progress/status bar, connect to thread errors -# Give simularity routines a repo.ui that catches progress reports  # Disable buttons when lists are empty    class DetectRenameDialog(QDialog):   'Detect renames after they occur'     matchAccepted = pyqtSignal()     def __init__(self, parent=None, root=None, *pats):   QDialog.__init__(self, parent)     repo = hg.repository(ui.ui(), path=paths.find_root(root))   self.repo = repo   self.pats = pats   self.thread = None     reponame = hglib.get_reponame(repo)   self.setWindowTitle(_('Detect Copies/Renames in %s') % reponame)   s = QSettings()   self.restoreGeometry(s.value('guess/geom').toByteArray())     layout = QVBoxLayout()   self.setLayout(layout)     # vsplit for top & diff   vsplit = QSplitter(Qt.Horizontal)   vsplit.restoreState(s.value('guess/vsplit-state').toByteArray())   utframe = QFrame(vsplit)   matchframe = QFrame(vsplit)     utvbox = QVBoxLayout()   utframe.setLayout(utvbox)   matchvbox = QVBoxLayout()   matchframe.setLayout(matchvbox)     hsplit = QSplitter(Qt.Vertical)   hsplit.restoreState(s.value('guess/hsplit-state').toByteArray())   layout.addWidget(hsplit)   hsplit.addWidget(vsplit)     utlbl = QLabel(_('<b>Unrevisioned Files</b>'))   utvbox.addWidget(utlbl)   self.unrevlist = QListWidget()   self.unrevlist.setSelectionMode(QAbstractItemView.ExtendedSelection)   utvbox.addWidget(self.unrevlist)     simhbox = QHBoxLayout()   utvbox.addLayout(simhbox)   lbl = QLabel()   slider = QSlider(Qt.Horizontal)   slider.setRange(0, 100)   slider.setTickInterval(10)   slider.setPageStep(10)   slider.setTickPosition(QSlider.TicksBelow)   slider.changefunc = lambda v: lbl.setText(   _('Min Simularity: %d%%') % v)   slider.valueChanged.connect(slider.changefunc)   slider.setValue(s.value('guess/simslider').toInt()[0])   self.simslider = slider   lbl.setBuddy(slider)   simhbox.addWidget(lbl)   simhbox.addWidget(slider, 1)     buthbox = QHBoxLayout()   utvbox.addLayout(buthbox)   copycheck = QCheckBox(_('Only consider deleted files'))   copycheck.setToolTip(_('Uncheck to consider all revisioned files'   ' for copy sources'))   copycheck.setChecked(True)   findrenames = QPushButton(_('Find Rename'))   findrenames.setToolTip(_('Find copy and/or rename sources'))   findrenames.clicked.connect(self.findRenames)   buthbox.addWidget(copycheck)   buthbox.addStretch(1)   buthbox.addWidget(findrenames)   self.findbtn, self.copycheck = findrenames, copycheck     matchlbl = QLabel(_('<b>Candidate Matches</b>'))   matchvbox.addWidget(matchlbl)   self.matchlv = QTreeView()   self.matchlv.setItemsExpandable(False)   self.matchlv.setRootIsDecorated(False) - self.matchlv.setModel(MatchModel()) + self.model = MatchModel() + self.matchlv.setModel(self.model)   self.matchlv.clicked.connect(self.showDiff)   buthbox = QHBoxLayout()   matchbtn = QPushButton(_('Accept Selected Matches'))   matchbtn.clicked.connect(self.acceptMatch)   matchbtn.setEnabled(False)   self.matchbtn = matchbtn   buthbox.addStretch(1)   buthbox.addWidget(matchbtn)   matchvbox.addWidget(self.matchlv)   matchvbox.addLayout(buthbox)     diffframe = QFrame(hsplit)   diffvbox = QVBoxLayout()   diffframe.setLayout(diffvbox)     difflabel = QLabel(_('<b>Differences from Source to Dest</b>'))   diffvbox.addWidget(difflabel)   difftb = QTextBrowser()   difftb.document().setDefaultStyleSheet(qtlib.thgstylesheet)   diffvbox.addWidget(difftb)   self.difftb = difftb   - BB = QDialogButtonBox - bb = QDialogButtonBox(BB.Close) - self.connect(bb, SIGNAL("accepted()"), self, SLOT("accept()")) - self.connect(bb, SIGNAL("rejected()"), self, SLOT("reject()")) - layout.addWidget(bb) - self.bb = bb + self.pmon = cmdui.ProgressMonitor() + self.pmon.hide() + layout.addWidget(self.pmon)     self.vsplit, self.hsplit = vsplit, hsplit   QTimer.singleShot(0, self.refresh)     def refresh(self):   hglib.invalidaterepo(self.repo)   wctx = self.repo[None]   wctx.status(unknown=True)   self.unrevlist.clear()   dests = []   for u in wctx.unknown():   dests.append(u)   for a in wctx.added():   if not wctx[a].renamed():   dests.append(a)   for x in dests:   item = QListWidgetItem(hglib.tounicode(x))   item.orig = x   self.unrevlist.addItem(item)   self.unrevlist.setItemSelected(item, x in self.pats)   self.difftb.clear()   self.pats = [] + self.pmon.clear_progress()     def findRenames(self):   'User pressed "find renames" button'   if self.thread and self.thread.isRunning():   QMessageBox.information(self, _('Search already in progress'),   _('Cannot start a new search'))   return   ulist = []   for item in self.unrevlist.selectedItems():   ulist.append(item.orig)   if not ulist:   QMessageBox.information(self, _('No rows selected'),   _('Select one or more rows for search'))   return     pct = self.simslider.value() / 100.0   copies = not self.copycheck.isChecked() - model = self.matchlv.model() - model.clear()   self.findbtn.setEnabled(False)   self.matchbtn.setEnabled(False) + self.errorstr = None   + self.matchlv.model().clear()   self.thread = RenameSearchThread(self.repo, ulist, pct, copies) - self.thread.match.connect(model.appendRow) - #self.thread.error.connect(print) - #self.thread.progress.connect(print) + self.connect(self.thread, SIGNAL('match'), self.rowReceived) + self.connect(self.thread, SIGNAL('progress'), self.progressReceived) + self.connect(self.thread, SIGNAL('error'), self.errorReceived)   self.thread.searchComplete.connect(self.finished)   self.thread.start()     def finished(self): + self.pmon.fillup_progress() + if self.errorstr: + self.pmon.set_text(self.errorstr) + else: + self.pmon.hide()   for col in xrange(3):   self.matchlv.resizeColumnToContents(col)   self.findbtn.setEnabled(True) - self.matchbtn.setDisabled(model.isEmpty()) + self.matchbtn.setDisabled(self.matchlv.model().isEmpty()) + + def rowReceived(self, args): + self.matchlv.model().appendRow(*args) + + def errorReceived(self, qstr): + self.errorstr = qstr + self.pmon.set_text(qstr) + + def progressReceived(self, data): + if self.thread.isFinished(): + return + self.pmon.show() + counting = False + topic, item, pos, total, unit = data + if pos is None: + self.pmon.clear_progress() + return + if total is None: + count = '%d' % pos + counting = True + else: + self.pmon.pbar.setMaximum(total) + self.pmon.pbar.setValue(pos) + count = '%d / %d' % (pos, total) + if unit: + count += ' ' + unit + self.pmon.prog_label.setText(hglib.tounicode(count)) + if item: + status = '%s: %s' % (topic, item) + else: + status = _('Status: %s') % topic + self.pmon.status_label.setText(hglib.tounicode(status)) + self.pmon.inprogress = True + + if not self.pmon.inprogress or counting: + # use indeterminate mode + self.pmon.pbar.setMinimum(0)     def acceptMatch(self):   'User pressed "accept match" button'   hglib.invalidaterepo(self.repo) - sel = self.matchlv.selectionModel() - for index in sel.selectedIndexes(): + for index in self.matchlv.selectedIndexes():   src, dest, percent = self.matchlv.model().getRow(index)   if not os.path.exists(self.repo.wjoin(src)):   # Mark missing rename source as removed   self.repo.remove([src])   self.repo.copy(src, dest)   shlib.shell_notify([self.repo.wjoin(src), self.repo.wjoin(dest)])   # Mark all rows with this target file as non-sensitive   #for row in self.matchlv.model().getRows():   # if row[1] == dest:   # row[5] = False   self.matchAccepted.emit()   self.refresh()     def showDiff(self, index):   'User selected a row in the candidate tree'   hglib.invalidaterepo(self.repo)   ctx = self.repo['.']   hu = htmlui.htmlui()   row = self.matchlv.model().getRow(index)   src, dest, percent = self.matchlv.model().getRow(index)   aa = self.repo.wread(dest)   rr = ctx.filectx(src).data()   opts = mdiff.defaultopts   difftext = mdiff.unidiff(rr, '', aa, '', src,   dest, None, opts=opts)   if not difftext:   t = _('%s and %s have identical contents\n\n') % (src, dest)   hu.write(t, label='ui.error')   else:   for t, l in qtlib.difflabel(difftext.splitlines, True):   hu.write(t, label=l)   self.difftb.setHtml(hu.getdata()[0])     def accept(self):   s = QSettings()   s.setValue('guess/geom', self.saveGeometry())   s.setValue('guess/vsplit-state', self.vsplit.saveState())   s.setValue('guess/hsplit-state', self.hsplit.saveState())   s.setValue('guess/simslider', self.simslider.value())   QDialog.accept(self)     def reject(self):   if self.thread and self.thread.isRunning():   self.thread.terminate()   # This can lockup, so stop waiting after 2sec   self.thread.wait( 2000 )   self.finished()   self.thread = None   else:   s = QSettings()   s.setValue('guess/geom', self.saveGeometry())   s.setValue('guess/vsplit-state', self.vsplit.saveState())   s.setValue('guess/hsplit-state', self.hsplit.saveState())   s.setValue('guess/simslider', self.simslider.value())   QDialog.reject(self)      class MatchModel(QAbstractTableModel):   def __init__(self, parent=None):   QAbstractTableModel.__init__(self, parent)   self.rows = []   self.headers = (_('Source'), _('Dest'), _('% Match'))     def rowCount(self, parent):   return len(self.rows)     def columnCount(self, parent):   return len(self.headers)     def data(self, index, role):   if not index.isValid():   return QVariant()   if role == Qt.DisplayRole:   s = self.rows[index.row()][index.column()]   return QVariant(hglib.tounicode(s))   '''   elif role == Qt.TextColorRole:   src, dst, pct = self.rows[index.row()]   if pct == 1.0:   return QColor('green')   else:   return QColor('black')   elif role == Qt.ToolTipRole:   # explain what row means?   '''   return QVariant()     def headerData(self, col, orientation, role):   if role != Qt.DisplayRole or orientation != Qt.Horizontal:   return QVariant()   else:   return QVariant(self.headers[col])     def flags(self, index):   return Qt.ItemIsSelectable | Qt.ItemIsEnabled     # Custom methods     def getRow(self, index):   assert index.isValid()   return self.rows[index.row()]     def appendRow(self, *args):   self.beginInsertRows(QModelIndex(), len(self.rows), len(self.rows)) - vals = [str(a) for a in args] # PyQt is upgrading to QString - self.rows.append(vals) + self.rows.append(args)   self.endInsertRows()   self.emit(SIGNAL("dataChanged()"))     def clear(self):   self.beginRemoveRows(QModelIndex(), 0, len(self.rows)-1)   self.rows = []   self.endRemoveRows()   self.emit(SIGNAL("dataChanged()"))     def sort(self, col, order):   self.emit(SIGNAL("layoutAboutToBeChanged()"))   if col == COL_PATH:   c = self.checked   self.rows.sort(lambda x, y: cmp(c[x[col]], c[y[col]]))   else:   self.rows.sort(lambda x, y: cmp(x[col], y[col]))   if order == Qt.DescendingOrder:   self.rows.reverse()   self.emit(SIGNAL("layoutChanged()"))   self.reset()     def isEmpty(self):   return not bool(self.rows)    class RenameSearchThread(QThread):   '''Background thread for searching repository history''' - match = pyqtSignal(str, str, str) - error = pyqtSignal(QString) - progress = pyqtSignal()   searchComplete = pyqtSignal()     def __init__(self, repo, ufiles, minpct, copies):   super(RenameSearchThread, self).__init__()   self.repo = repo   self.ufiles = ufiles   self.minpct = minpct   self.copies = copies     def run(self): + class ProgUi(ui.ui): + def __init__(self, src=None): + super(ProgUi, self).__init__(src) + self.setconfig('ui', 'interactive', 'off') + self.setconfig('progress', 'disable', 'True') + os.environ['TERM'] = 'dumb' + if src: + self.sig = src.sig + else: + self.sig = QObject() # dummy object to emit signals + def progress(self, topic, pos, item='', unit='', total=None): + self.sig.emit(SIGNAL('progress'), + [topic, item, pos, total, unit]) + progui = ProgUi() + self.connect(progui.sig, SIGNAL('progress'), self.progress) + storeui = self.repo.ui + self.progui = progui + self.repo.ui = progui   try:   self.search(self.repo)   except Exception, e: - self.error.emit(hglib.tounicode(str(e))) - print e + self.emit(SIGNAL('error'), hglib.tounicode(str(e))) + self.repo.ui = storeui   self.searchComplete.emit()   + def progress(self, wr): + self.emit(SIGNAL('progress'), wr) +   def search(self, repo):   hglib.invalidaterepo(repo)   wctx = repo[None]   pctx = repo['.']   if self.copies:   wctx.status(clean=True)   srcs = wctx.removed() + wctx.deleted()   srcs += wctx.modified() + wctx.clean()   else:   srcs = wctx.removed() + wctx.deleted()   added = [wctx[a] for a in self.ufiles]   removed = [pctx[a] for a in srcs if a in pctx]   # do not consider files of zero length   added = sorted([fctx for fctx in added if fctx.size() > 0])   removed = sorted([fctx for fctx in removed if fctx.size() > 0])   exacts = [] - for o, n in similar._findexactmatches(repo, added, removed): + gen = similar._findexactmatches(repo, added, removed) + for o, n in gen:   old, new = o.path(), n.path()   exacts.append(old) - self.match.emit(old, new, '100%') + self.emit(SIGNAL('match'), [old, new, '100%']) + if self.minpct == 1.0: + return   removed = [r for r in removed if r.path() not in exacts] - if self.minpct < 1.0: - for o, n, s in similar._findsimilarmatches(repo, added, removed, - self.minpct): - old, new = o.path(), n.path() - self.match.emit(old, new, '%d%%' % (s*100)) + gen = similar._findsimilarmatches(repo, added, removed, self.minpct) + for o, n, s in gen: + old, new, sim = o.path(), n.path(), '%d%%' % (s*100) + self.emit(SIGNAL('match'), [old, new, sim])    def run(ui, *pats, **opts):   return DetectRenameDialog(None, None, *pats)