summaryrefslogtreecommitdiff
path: root/tortoisehg/hgqt/revert.py
blob: 14754176cd7ca677ec5f0f233fdaa19055f6c918 (plain)
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
# revert.py - File revert dialog for TortoiseHg
#
# 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.

from __future__ import absolute_import

from .qtcore import (
    Qt,
    pyqtSlot,
)
from .qtgui import (
    QCheckBox,
    QComboBox,
    QDialog,
    QDialogButtonBox,
    QLabel,
    QVBoxLayout,
)

from mercurial.node import nullid

from ..util import hglib
from ..util.i18n import _
from . import (
    cmdcore,
    cmdui,
    qtlib,
)

class RevertDialog(QDialog):
    def __init__(self, repoagent, wfiles, rev, parent):
        super(RevertDialog, self).__init__(parent)

        self._repoagent = repoagent
        self._cmdsession = cmdcore.nullCmdSession()
        self.setWindowTitle(_('Revert - %s') % repoagent.displayName())

        f = self.windowFlags()
        self.setWindowFlags(f & ~Qt.WindowContextHelpButtonHint)
        repo = repoagent.rawRepo()
        self.wfiles = [repo.wjoin(wfile) for wfile in wfiles]

        self.setLayout(QVBoxLayout())

        if len(wfile) == 1:
            lblText = _('<b>Revert %s to its contents'
                        ' at the following revision?</b>') % (
                      hglib.tounicode(wfiles[0]))
        else:
            lblText = _('<b>Revert %d files to their contents'
                        ' at the following revision?</b>') % (
                      len(wfiles))
        lbl = QLabel(lblText)
        self.layout().addWidget(lbl)

        self._addRevertTargetCombo(rev)

        self.allchk = QCheckBox(_('Revert all files to this revision'))
        self.layout().addWidget(self.allchk)

        BB = QDialogButtonBox
        bbox = QDialogButtonBox(BB.Ok|BB.Cancel)
        bbox.accepted.connect(self.accept)
        bbox.rejected.connect(self.reject)
        self.layout().addWidget(bbox)
        self.bbox = bbox

    def _addRevertTargetCombo(self, rev):
        if rev is None:
            raise ValueError('Cannot revert to working directory')
        self.revcombo = QComboBox()
        revnames = ['revision %d' % rev]
        repo = self._repoagent.rawRepo()
        ctx = repo[rev]
        parents = ctx.parents()[:2]
        if len(parents) == 1:
            parentdesctemplate = ("revision %d's parent (i.e. revision %d)",)
        else:
            parentdesctemplate = (
                _("revision %d's first parent (i.e. revision %d)"),
                _("revision %d's second parent (i.e. revision %d)"),
            )
        for n, pctx in enumerate(parents):
            if pctx.node() == nullid:
                revdesc = _('null revision (i.e. remove file(s))')
            else:
                revdesc = parentdesctemplate[n] % (rev, pctx.rev())
            revnames.append(revdesc)
        self.revcombo.addItems(revnames)
        reverttargets = [ctx] + parents
        for n, ctx in enumerate(reverttargets):
            self.revcombo.setItemData(n, ctx.hex())
        self.layout().addWidget(self.revcombo)

    def accept(self):
        rev = self.revcombo.itemData(self.revcombo.currentIndex())
        if self.allchk.isChecked():
            if not qtlib.QuestionMsgBox(_('Confirm Revert'),
                     _('Reverting all files will discard changes and '
                       'leave affected files in a modified state.<br>'
                       '<br>Are you sure you want to use revert?<br><br>'
                       '(use update to checkout another revision)'),
                       parent=self):
                return
            cmdline = hglib.buildcmdargs('revert', all=True, rev=rev)
        else:
            files = map(hglib.tounicode, self.wfiles)
            cmdline = hglib.buildcmdargs('revert', rev=rev, *files)
        self.bbox.button(QDialogButtonBox.Ok).setEnabled(False)
        self._cmdsession = sess = self._repoagent.runCommand(cmdline, self)
        sess.commandFinished.connect(self._onCommandFinished)

    @pyqtSlot(int)
    def _onCommandFinished(self, ret):
        if ret == 0:
            self.reject()
        else:
            cmdui.errorMessageBox(self._cmdsession, self)