1. Yuya Nishihara
  2. thg-work

Commits

Yuya Nishihara  committed 1619a66

clone: suggest new destination on source changed or existing dest selected

This prepares for the cleanup of runCommand().

Example use case:
1. open new dialog at "/tmp" and set source to "http://example.org/foo/";
=> "/tmp/foo"
2. change source to "http://example.org/bar/";
=> "/tmp/bar"
3. change source to "/tmp/baz"
=> "/tmp/baz-clone" (because "/tmp/baz" already exists)
4. select existing destination, "/tmp/existing", by file dialog
=> "/tmp/existing/baz"
5. open file dialog and create new destination, "/tmp/new"
=> "/tmp/new" (unchanged)

  • Participants
  • Parent commits 363608a
  • Branches default

Comments (0)

Files changed (1)

File tortoisehg/hgqt/clone.py

View file
 from PyQt4.QtCore import *
 from PyQt4.QtGui import *
 
-from mercurial import cmdutil, commands
+from mercurial import cmdutil, commands, hg
 
 from tortoisehg.util import hglib
 from tortoisehg.hgqt.i18n import _
     longopts = set(e[1] for e in entry[1])
     return 'startrev' in longopts
 
+def _suggesteddest(src, basedest):
+    if '://' in basedest:
+        return basedest
+    try:
+        if not os.listdir(basedest):
+            # premade empty directory, just use it
+            return basedest
+    except OSError:
+        # guess existing base assuming "{basedest}/{name}"
+        basedest = os.path.dirname(basedest)
+    name = hglib.tounicode(hg.defaultdest(hglib.fromunicode(src, 'replace')))
+    if not name or name == '.':
+        return basedest
+    newdest = os.path.join(basedest, name)
+    if os.path.exists(newdest):
+        newdest += '-clone'
+    return newdest
+
 
 class CloneWidget(cmdui.AbstractCmdWidget):
 
 
         # connect extra signals
         self.src_combo.editTextChanged.connect(self._onSourceChanged)
+        self.src_combo.currentIndexChanged.connect(self._suggestDestination)
+        t = QTimer(self, interval=200, singleShot=True)
+        t.timeout.connect(self._suggestDestination)
+        le = self.src_combo.lineEdit()
+        le.editingFinished.connect(t.stop)  # only while it has focus
+        le.textEdited.connect(t.start)
         self.dest_combo.editTextChanged.connect(self._composeCommand)
         self.rev_chk.toggled.connect(self._composeCommand)
         self.rev_text.textChanged.connect(self._composeCommand)
         self.dest_combo.setEditText(url)
 
     @pyqtSlot()
+    def _suggestDestination(self):
+        self.setDestination(_suggesteddest(self.source(), self.destination()))
+
+    @pyqtSlot()
     def _composeCommand(self):
         opts = {
             'noupdate': self.noupdate_chk.isChecked(),
             self.src_combo.currentText(), QFileDialog.ShowDirsOnly)
         if path:
             self.src_combo.setEditText(QDir.toNativeSeparators(path))
+            self._suggestDestination()
             self.dest_combo.setFocus()
 
     @pyqtSlot()
             self.dest_combo.currentText(), QFileDialog.ShowDirsOnly)
         if path:
             self.dest_combo.setEditText(QDir.toNativeSeparators(path))
+            self._suggestDestination()  # in case existing dir is selected
             self.dest_combo.setFocus()
 
     @pyqtSlot()