PageRenderTime 43ms CodeModel.GetById 2ms app.highlight 35ms RepoModel.GetById 2ms app.codeStats 0ms

/tortoisehg/hgqt/hginit.py

https://bitbucket.org/tortoisehg/hgtk/
Python | 226 lines | 184 code | 23 blank | 19 comment | 17 complexity | 6f8f69c03ea8142190882351502b4110 MD5 | raw file
  1# hginit.py - TortoiseHg dialog to initialize a repo
  2#
  3# Copyright 2008 Steve Borho <steve@borho.org>
  4# Copyright 2010 Johan Samyn <johan.samyn@gmail.com>
  5#
  6# This software may be used and distributed according to the terms of the
  7# GNU General Public License version 2, incorporated herein by reference.
  8
  9import os
 10
 11from mercurial import hg, ui, error, util
 12
 13from tortoisehg.hgqt.i18n import _
 14from tortoisehg.hgqt import qtlib
 15from tortoisehg.util import hglib, shlib
 16
 17from PyQt4.QtCore import *
 18from PyQt4.QtGui import *
 19
 20class InitDialog(QDialog):
 21    """TortoiseHg init dialog"""
 22
 23    def __init__(self, destdir=[], opts={}, parent=None):
 24        super(InitDialog, self).__init__(parent)
 25
 26        # main layout
 27        self.vbox = QVBoxLayout()
 28        self.vbox.setSpacing(6)
 29        self.grid = QGridLayout()
 30        self.grid.setSpacing(6)
 31        self.vbox.addLayout(self.grid)
 32
 33        # dest widgets
 34        self.dest_lbl = QLabel(_('Destination path:'))
 35        self.dest_edit = QLineEdit()
 36        self.dest_edit.setMinimumWidth(300)
 37        self.dest_btn = QPushButton(_('Browse...'))
 38        self.dest_btn.setAutoDefault(False)
 39        self.grid.addWidget(self.dest_lbl, 0, 0)
 40        self.grid.addWidget(self.dest_edit, 0, 1)
 41        self.grid.addWidget(self.dest_btn, 0, 2)
 42
 43        # options checkboxes
 44        self.add_files_chk = QCheckBox(
 45                _('Add special files (.hgignore, ...)'))
 46        self.make_pre_1_7_chk = QCheckBox(
 47                _('Make repo compatible with Mercurial <1.7'))
 48        self.run_wb_chk = QCheckBox(
 49                _('Show in Workbench after init'))
 50        self.grid.addWidget(self.add_files_chk, 1, 1)
 51        self.grid.addWidget(self.make_pre_1_7_chk, 2, 1)
 52        if not self.parent():
 53            self.grid.addWidget(self.run_wb_chk, 3, 1)
 54
 55        # buttons
 56        self.init_btn = QPushButton(_('Create'))
 57        self.init_btn.setDefault(True)
 58        self.close_btn = QPushButton(_('&Close'))
 59        self.close_btn.setAutoDefault(False)
 60        self.hbox = QHBoxLayout()
 61        self.hbox.addStretch(0)
 62        self.hbox.addWidget(self.init_btn)
 63        self.hbox.addWidget(self.close_btn)
 64        self.vbox.addLayout(self.hbox)
 65
 66        # some extras
 67        self.hgcmd_lbl = QLabel(_('Hg command:'))
 68        self.hgcmd_lbl.setAlignment(Qt.AlignRight)
 69        self.hgcmd_txt = QLineEdit()
 70        self.hgcmd_txt.setReadOnly(True)
 71        self.grid.addWidget(self.hgcmd_lbl, 4, 0)
 72        self.grid.addWidget(self.hgcmd_txt, 4, 1)
 73
 74        # init defaults
 75        self.cwd = os.getcwd()
 76        path = os.path.abspath(destdir and destdir[0] or self.cwd)
 77        if os.path.isfile(path):
 78            path = os.path.dirname(path)
 79        self.dest_edit.setText(hglib.tounicode(path))
 80        self.add_files_chk.setChecked(True)
 81        self.make_pre_1_7_chk.setChecked(False)
 82        self.compose_command()
 83
 84        # dialog settings
 85        self.setWindowTitle(_('Init'))
 86        self.setWindowIcon(qtlib.geticon('init'))
 87        self.setWindowFlags(
 88                self.windowFlags() & ~Qt.WindowContextHelpButtonHint)
 89        self.setLayout(self.vbox)
 90        self.layout().setSizeConstraint(QLayout.SetFixedSize)
 91        self.dest_edit.setFocus()
 92
 93        # connecting slots
 94        self.dest_edit.textChanged.connect(self.compose_command)
 95        self.dest_btn.clicked.connect(self.browse_clicked)
 96        self.init_btn.clicked.connect(self.init)
 97        self.close_btn.clicked.connect(self.close)
 98        self.make_pre_1_7_chk.toggled.connect(self.compose_command)
 99
100    def browse_clicked(self):
101        """Select the destination directory"""
102        dest = hglib.fromunicode(self.dest_edit.text())
103        if not os.path.exists(dest):
104            dest = os.path.dirname(dest)
105        FD = QFileDialog
106        caption = _('Select Destination Folder')
107        path = FD.getExistingDirectory(parent=self, caption=caption,
108                options=FD.ShowDirsOnly | FD.ReadOnly)
109        response = str(path)
110        if response:
111            self.dest_edit.setText(response)
112
113    def compose_command(self):
114        # just a stub for extension with extra options (--mq, --ssh, ...)
115        cmd = ['hg', 'init']
116        if self.make_pre_1_7_chk.isChecked():
117            cmd.append('--config format.dotencode=False')
118        cmd.append(self.getPath())
119        self.hgcmd_txt.setText(hglib.tounicode(' '.join(cmd)))
120
121    def getPath(self):
122        return hglib.fromunicode(self.dest_edit.text()).strip()
123
124    def init(self):
125        dest = self.getPath()
126
127        if dest == '':
128            qtlib.ErrorMsgBox(_('Error executing init'),
129                    _('Destination path is empty'),
130                    _('Please enter the directory path'))
131            self.dest_edit.setFocus()
132            return False
133
134        dest = os.path.normpath(dest)
135        self.dest_edit.setText(hglib.tounicode(dest))
136        udest = self.dest_edit.text()
137
138        if not os.path.exists(dest):
139            p = dest
140            l = 0
141            while not os.path.exists(p):
142                l += 1
143                p, t = os.path.split(p)
144                if not t:
145                    break  # already root path
146            if l > 1:
147                res = qtlib.QuestionMsgBox(_('Init'),
148                        _('Are you sure about adding the new repository'
149                          ' %d extra levels deep?') % l,
150                        _('Path exists up to:\n%s\nand you asked for:\n%s')
151                            % (p, udest),
152                        defaultbutton=QMessageBox.No)
153                if not res:
154                    self.dest_edit.setFocus()
155                    return
156            try:
157                # create the folder, just like Hg would
158                os.makedirs(dest)
159            except:
160                qtlib.ErrorMsgBox(_('Error executing init'),
161                        _('Cannot create folder %s' % udest))
162                return False
163
164        _ui = ui.ui()
165
166        # dotencode is the new default repo format in Mercurial 1.7
167        if self.make_pre_1_7_chk.isChecked():
168            _ui.setconfig('format', 'dotencode', 'False')
169
170        try:
171            # create the new repo
172            hg.repository(_ui, dest, create=1)
173        except error.RepoError, inst:
174            qtlib.ErrorMsgBox(_('Error executing init'),
175                    _('Unable to create new repository'),
176                    hglib.tounicode(str(inst)))
177            return False
178        except util.Abort, inst:
179            if e.hint:
180                err = _('%s (hint: %s)') % (hglib.tounicode(str(e)),
181                                            hglib.tounicode(e.hint))
182            else:
183                err = hglib.tounicode(str(e))
184            qtlib.ErrorMsgBox(_('Error executing init'),
185                    _('Error when creating repository'), err)
186            return False
187        except:
188            import traceback
189            qtlib.ErrorMsgBox(_('Error executing init'),
190                    _('Error when creating repository'),
191                    traceback.format_exc())
192            return False
193
194        # Create the .hg* file, mainly to workaround
195        # Explorer's problem in creating files with a name
196        # beginning with a dot.
197        if (self.add_files_chk.isChecked() and
198                os.path.exists(os.path.sep.join([dest, '.hg']))):
199            hgignore = os.path.join(dest, '.hgignore')
200            if not os.path.exists(hgignore):
201                try:
202                    open(hgignore, 'wb')
203                except:
204                    pass
205
206        if self.run_wb_chk.isChecked():
207            from tortoisehg.hgqt import run
208            try:
209                run.log(ui.ui(), root=dest)
210            except Exception, e:
211                qtlib.WarningMsgBox(_('Init'),
212                  _('<p>Repository successfully created at</p><p>%s</p>') % dest,
213                  _('<p>But could not run Workbench for it.</p><p>%s</p>')
214                    % hglib.tounicode(str(e)))
215        else:
216            if not self.parent():
217                qtlib.InfoMsgBox(_('Init'),
218                _('<p>Repository successfully created at</p><p>%s</p>') % udest)
219
220        self.accept()
221
222    def reject(self):
223        super(InitDialog, self).reject()
224
225def run(ui, *pats, **opts):
226    return InitDialog(pats, opts)