Mercurial and Git clients can push and pull from this alias URL to interact with this repository. You can change to which repository an alias points by going to the Aliases link on the project page.
# chunks.py - TortoiseHg patch/diff browser and editor## Copyright 2010 Steve Borho <steve@borho.org>## This software may be used and distributed according to the terms# of the GNU General Public License, incorporated herein by reference.importcStringIOimport os
from mercurial import hg, util, patch
+from mercurial import match as matchmodfrom hgext import record
from tortoisehg.util import hglib
fromtortoisehg.util.patchctximportpatchctxfromtortoisehg.hgqt.i18nimport_fromtortoisehg.hgqtimportqtlib,thgrepo,qscilib,lexers,wctxactionsfromtortoisehg.hgqtimportfilelistmodel,filelistview,fileviewfromPyQt4.QtCoreimport*fromPyQt4.QtGuiimport*fromPyQt4importQsciqsci=Qsci.QsciScintillaclassChunksWidget(QWidget):linkActivated=pyqtSignal(QString)showMessage=pyqtSignal(QString)chunksSelected=pyqtSignal(bool)fileSelected=pyqtSignal(bool)fileModelEmpty=pyqtSignal(bool)fileModified=pyqtSignal()def__init__(self,repo,parent):QWidget.__init__(self,parent)self.repo=repoself.currentFile=Nonelayout=QVBoxLayout(self)layout.setSpacing(0)layout.setMargin(0)layout.setContentsMargins(2,2,2,2)self.setLayout(layout)self.splitter=QSplitter(self)self.splitter.setOrientation(Qt.Vertical)self.splitter.setChildrenCollapsible(False)self.layout().addWidget(self.splitter)self.filelist=filelistview.HgFileListView(self)self.fileListFrame=QFrame(self.splitter)self.fileListFrame.setFrameShape(QFrame.NoFrame)vbox=QVBoxLayout()vbox.setSpacing(0)vbox.setMargin(0)vbox.addWidget(self.filelist)self.fileListFrame.setLayout(vbox)self.diffbrowse=DiffBrowser(self.splitter)self.diffbrowse.setFont(qtlib.getfont('fontdiff').font())self.diffbrowse.showMessage.connect(self.showMessage)self.diffbrowse.linkActivated.connect(self.linkActivated)self.diffbrowse.chunksSelected.connect(self.chunksSelected)self.splitter.setStretchFactor(0,0)self.splitter.setStretchFactor(1,3)self.timerevent=self.startTimer(500)deftimerEvent(self,event):'Periodic poll of currently displayed patch or working file'ctx=self.filelistmodel._ctxifctxisNone:returnifisinstance(ctx,patchctx):path=ctx._pathmtime=ctx._mtimeelifself.currentFile:path=self.repo.wjoin(self.currentFile)mtime=self.mtimeelse:returnifos.path.exists(path):newmtime=os.path.getmtime(path)ifmtime!=newmtime:self.mtime=newmtimeself.refresh()defeditCurrentFile(self):ctx=self.filelistmodel._ctxifisinstance(ctx,patchctx):path=ctx._pathelse:path=self.repo.wjoin(self.currentFile)wctxactions.edit(self,self.repo.ui,self.repo,[path])defdeleteSelectedChunks(self):'delete currently selected chunks'repo=self.repochunks=self.diffbrowse.curchunksdchunks=[cforcinchunks[1:]ifc.selected]ifnotdchunks:self.showMessage.emit(_('No deletable chunks'))returnkchunks=[cforcinchunks[1:]ifnotc.selected]revertall=Falseifnotkchunksandqtlib.QuestionMsgBox(_('No chunks remain'),_('Remove all file changes?')): 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')
forwfileinctx._fileorder:ifwfile==self.currentFile:ifrevertall:continuechunks[0].write(fp)forchunkinkchunks:chunk.write(fp)ifnotchunks[-1].selected:fp.write('\n')else:forchunkinctx._files[wfile]:chunk.write(fp)fp.rename()finally:delfpself.fileModified.emit()else:path=repo.wjoin(self.currentFile)ifnotos.path.exists(path):self.showMessage.emit(_('file hsa been deleted, refresh'))returnifself.mtime!=os.path.getmtime(path): 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):
iffile:self.currentFile=filepath=self.repo.wjoin(file)ifos.path.exists(path):self.mtime=os.path.getmtime(path)self.diffbrowse.displayFile(file,status)self.fileSelected.emit(True)else:self.currentFile=Noneself.diffbrowse.clearDisplay()self.fileSelected.emit(False)defsetContext(self,ctx):ifself.filelist.model()isnotNone:f=self.filelist.currentFile()else:f=Noneself.fileSelected.emit(False)self.filelistmodel=filelistmodel.HgFileListModel(self.repo,self)self.filelist.setModel(self.filelistmodel)self.filelist.fileRevSelected.connect(self.displayFile)self.filelist.clearDisplay.connect(self.diffbrowse.clearDisplay)self.diffbrowse.setContext(ctx)self.filelistmodel.setContext(ctx)self.fileModelEmpty.emit(len(ctx.files())==0)iffandfinctx:self.filelist.selectFile(f)defrefresh(self):f=self.filelist.currentFile()ctx=self.filelistmodel._ctxifisinstance(ctx,patchctx):# if patch mtime has not changed, it could return the same ctxctx=self.repo.changectx(ctx._path)else:self.repo.thginvalidate()ctx=self.repo.changectx(ctx.node())self.filelistmodel.setContext(ctx)iffinctx:self.filelist.selectFile(f)classDiffBrowser(QFrame):"""diff browser"""linkActivated=pyqtSignal(QString)showMessage=pyqtSignal(QString)chunksSelected=pyqtSignal(bool)def__init__(self,parent):QFrame.__init__(self,parent)self.curchunks=[]self.countselected=0self._ctx=Nonevbox=QVBoxLayout()vbox.setContentsMargins(0,0,0,0)vbox.setSpacing(0)self.setLayout(vbox)self.labelhbox=hbox=QHBoxLayout()hbox.setContentsMargins(0,0,0,0)hbox.setSpacing(2)self.layout().addLayout(hbox)self.filenamelabel=w=QLabel()hbox.addWidget(w)w.setWordWrap(True)f=w.textInteractionFlags()w.setTextInteractionFlags(f|Qt.TextSelectableByMouse)w.linkActivated.connect(self.linkActivated)self.sumlabel=QLabel()self.allbutton=QToolButton()self.allbutton.setText(_('All'))self.allbutton.setShortcut(QKeySequence.SelectAll)self.allbutton.clicked.connect(self.selectAll)self.nonebutton=QToolButton()self.nonebutton.setText(_('None'))self.nonebutton.setShortcut(QKeySequence.New)self.nonebutton.clicked.connect(self.selectNone)hbox.addStretch(1)hbox.addWidget(self.sumlabel)hbox.addWidget(self.allbutton)hbox.addWidget(self.nonebutton)self.extralabel=w=QLabel()w.setWordWrap(True)w.linkActivated.connect(self.linkActivated)self.layout().addWidget(w)w.hide()self.sci=qscilib.Scintilla(self)self.sci.setFrameStyle(0)self.sci.setReadOnly(True)self.sci.setUtf8(True)self.sci.setWrapMode(qsci.WrapCharacter)i=qscilib.KeyPressInterceptor(self,None,[QKeySequence.SelectAll,QKeySequence.New])self.sci.installEventFilter(i)self.sci.setCaretLineVisible(False)self.sci.setMarginType(1,qsci.SymbolMargin)self.sci.setMarginLineNumbers(1,False)self.sci.setMarginWidth(1,QFontMetrics(self.font()).width('XX'))self.sci.setMarginSensitivity(1,True)self.sci.marginClicked.connect(self.marginClicked)self.selected=self.sci.markerDefine(qsci.Plus,-1)self.unselected=self.sci.markerDefine(qsci.Minus,-1)self.vertical=self.sci.markerDefine(qsci.VerticalLine,-1)self.selcolor=self.sci.markerDefine(qsci.Background,-1)self.sci.setMarkerBackgroundColor(QColor('#BBFFFF'),self.selcolor)mask=(1<<self.selected)|(1<<self.unselected)| \
(1<<self.vertical)|(1<<self.selcolor)self.sci.setMarginMarkerMask(1,mask)self.layout().addWidget(self.sci,1)lexer=lexers.get_diff_lexer(self)self.sci.setLexer(lexer)self.clearDisplay()defupdateSummary(self):self.sumlabel.setText(_('Chunks selected: %d / %d')%(self.countselected,len(self.curchunks[1:])))self.chunksSelected.emit(self.countselected>0)@pyqtSlot()defselectAll(self):forchunkinself.curchunks[1:]:ifnotchunk.selected:self.sci.markerDelete(chunk.mline,-1)self.sci.markerAdd(chunk.mline,self.selected)chunk.selected=Trueself.countselected+=1foriinxrange(*chunk.lrange):self.sci.markerAdd(i,self.selcolor)self.updateSummary()@pyqtSlot()defselectNone(self):forchunkinself.curchunks[1:]:ifchunk.selected:self.sci.markerDelete(chunk.mline,-1)self.sci.markerAdd(chunk.mline,self.unselected)chunk.selected=Falseself.countselected-=1foriinxrange(*chunk.lrange):self.sci.markerDelete(i,self.selcolor)self.updateSummary()@pyqtSlot(int,int,Qt.KeyboardModifiers)defmarginClicked(self,margin,line,modifiers):forchunkinself.curchunks[1:]:ifline>=chunk.lrange[0]andline<chunk.lrange[1]:self.sci.markerDelete(chunk.mline,-1)ifchunk.selected:self.sci.markerAdd(chunk.mline,self.unselected)chunk.selected=Falseself.countselected-=1foriinxrange(*chunk.lrange):self.sci.markerDelete(i,self.selcolor)else:self.sci.markerAdd(chunk.mline,self.selected)chunk.selected=Trueself.countselected+=1foriinxrange(*chunk.lrange):self.sci.markerAdd(i,self.selcolor)self.updateSummary()returndefsetContext(self,ctx):self._ctx=ctxself.sci.setTabWidth(ctx._repo.tabwidth)ifctx._repo.wsvisible=='Visible':self.sci.setWhitespaceVisibility(qsci.WsVisible)elifctx._repo.wsvisible=='VisibleAfterIndent':self.sci.setWhitespaceVisibility(qsci.WsVisibleAfterIndent)else:self.sci.setWhitespaceVisibility(qsci.WsInvisible)defclearDisplay(self):self.sci.clear()self.filenamelabel.setText(' ')self.extralabel.hide()self.curchunks=[]self.countselected=0self.updateSummary()defdisplayFile(self,filename,status):self.clearDisplay()fd=fileview.FileData(self._ctx,None,filename,status)iffd.elabel:self.extralabel.setText(fd.elabel)self.extralabel.show()else:self.extralabel.hide()self.filenamelabel.setText(fd.flabel)ifnotfd.isValid()ornotfd.diff:self.sci.setText(fd.erroror'')returneliftype(self._ctx.rev())isstr:chunks=self._ctx._files[filename]else:buf=cStringIO.StringIO()buf.write('diff -r aaaaaaaaaaaa -r bbbbbbbbbbb %s\n'%filename)buf.write('\n'.join(fd.diff))buf.seek(0)chunks=record.parsepatch(buf)utext=[]forchunkinchunks[1:]:buf=cStringIO.StringIO()chunk.selected=Falsechunk.write(buf)chunk.lines=buf.getvalue().splitlines()utext+=[hglib.tounicode(l)forlinchunk.lines]self.sci.setText(u'\n'.join(utext))start=0self.sci.markerDeleteAll(-1)forchunkinchunks[1:]:chunk.lrange=(start,start+len(chunk.lines))chunk.mline=start+len(chunk.lines)/2foriinxrange(1,len(chunk.lines)-1):ifstart+i==chunk.mline:self.sci.markerAdd(chunk.mline,self.unselected)else:self.sci.markerAdd(start+i,self.vertical)start+=len(chunk.lines)self.origcontents=fd.olddataself.curchunks=chunksself.countselected=0self.updateSummary()defrun(ui,*pats,**opts):'for testing purposes only'fromtortoisehg.utilimportpathsrepo=thgrepo.repository(ui,path=paths.find_root())dlg=ChunksWidget(repo,None)desktopgeom=qApp.desktop().availableGeometry()dlg.resize(desktopgeom.size()*0.8)dlg.setContext(repo.changectx(None))returndlg
Attach a Trello Card
Add a tag
Your session has expired
You are no longer logged in. Please log in and try your request again.
Filter RSS Feed
This RSS feed URL allows you to see the contents of your current filter using any feed reader.
This link includes a special authentication token. If you share the URL with anyone else, they can see this RSS feed's activity. You can disable these tokens when needed.
Your current filter is unsaved; changing it won't affect this RSS feed.