# HG changeset patch
# User Antonio Muci <
a....@inwind.it>
# Date 1686294338 -7200
# Fri Jun 09 09:05:38 2023 +0200
# Node ID 06b166e559767ca682d376b3869fd90dfa0296a1
# Parent f67084ebec7dfb8d73efe5f949e35ced4e93ac36
reporegistry: simplify conditional logic in addSubrepo()
The whole function was behind an "if path" which guarded for the user having
pressed "cancel". It can be simplified returning early.
The diff of this commit is very big. However, apart for the initial inversion
of the "if" condition, only consists of whitespace changes.
No functional changes.
diff --git a/tortoisehg/hgqt/reporegistry.py b/tortoisehg/hgqt/reporegistry.py
--- a/tortoisehg/hgqt/reporegistry.py
+++ b/tortoisehg/hgqt/reporegistry.py
@@ -617,123 +617,125 @@ class RepoRegistryView(QDockWidget):
FD = QFileDialog
path = FD.getExistingDirectory(caption=caption,
directory=root, options=QFileDialog.Option.ShowDirsOnly | QFileDialog.Option.ReadOnly)
- if path:
- path = os.path.normpath(path)
- sroot = paths.find_root(path)
+ if not path:
+ # the used pressed "cancel"
+ return
+ path = os.path.normpath(path)
+ sroot = paths.find_root(path)
+
+ root = os.path.normcase(os.path.normpath(root))
- root = os.path.normcase(os.path.normpath(root))
+ if not sroot:
+ qtlib.WarningMsgBox(_('Cannot add subrepository'),
+ _('%s is not a valid repository') % path,
+ parent=self)
+ return
+ elif not os.path.isdir(sroot):
+ qtlib.WarningMsgBox(_('Cannot add subrepository'),
+ _('"%s" is not a folder') % sroot,
+ parent=self)
+ return
+ elif os.path.normcase(sroot) == root:
+ qtlib.WarningMsgBox(_('Cannot add subrepository'),
+ _('A repository cannot be added as a subrepo of itself'),
+ parent=self)
+ return
+ elif root != paths.find_root(os.path.dirname(os.path.normcase(path))):
+ qtlib.WarningMsgBox(_('Cannot add subrepository'),
+ _('The selected folder:<br><br>%s<br><br>'
+ 'is not inside the target repository.<br><br>'
+ 'This may be allowed but is greatly discouraged.<br>'
+ 'If you want to add a non trivial subrepository mapping '
+ 'you must manually edit the <i>.hgsub</i> file') % root, parent=self)
+ return
+ else:
+ # The selected path is the root of a repository that is inside
+ # the selected repository
- if not sroot:
- qtlib.WarningMsgBox(_('Cannot add subrepository'),
- _('%s is not a valid repository') % path,
- parent=self)
+ # Use forward slashes for relative subrepo root paths
+ srelroot = util.pconvert(hglib.fromunicode(sroot[len(root)+1:]))
+
+ # Is is already on the selected repository substate list?
+ try:
+ repo = hg.repository(hglib.loadui(),
+ hglib.fromunicode(root))
+ except:
+ qtlib.WarningMsgBox(_('Cannot open repository'),
+ _('The selected repository:<br><br>%s<br><br>'
+ 'cannot be open!') % root, parent=self)
return
- elif not os.path.isdir(sroot):
- qtlib.WarningMsgBox(_('Cannot add subrepository'),
- _('"%s" is not a folder') % sroot,
- parent=self)
- return
- elif os.path.normcase(sroot) == root:
- qtlib.WarningMsgBox(_('Cannot add subrepository'),
- _('A repository cannot be added as a subrepo of itself'),
- parent=self)
- return
- elif root != paths.find_root(os.path.dirname(os.path.normcase(path))):
- qtlib.WarningMsgBox(_('Cannot add subrepository'),
- _('The selected folder:<br><br>%s<br><br>'
- 'is not inside the target repository.<br><br>'
- 'This may be allowed but is greatly discouraged.<br>'
- 'If you want to add a non trivial subrepository mapping '
- 'you must manually edit the <i>.hgsub</i> file') % root, parent=self)
+
+ if srelroot in repo[b'.'].substate:
+ qtlib.WarningMsgBox(_('Subrepository already exists'),
+ _('The selected repository:<br><br>%s<br><br>'
+ 'is already a subrepository of:<br><br>%s<br><br>'
+ 'as: "%s"') % (sroot, root, hglib.tounicode(srelroot)),
+ parent=self)
return
else:
- # The selected path is the root of a repository that is inside
- # the selected repository
-
- # Use forward slashes for relative subrepo root paths
- srelroot = util.pconvert(hglib.fromunicode(sroot[len(root)+1:]))
-
- # Is is already on the selected repository substate list?
- try:
- repo = hg.repository(hglib.loadui(),
- hglib.fromunicode(root))
- except:
- qtlib.WarningMsgBox(_('Cannot open repository'),
- _('The selected repository:<br><br>%s<br><br>'
- 'cannot be open!') % root, parent=self)
- return
-
- if srelroot in repo[b'.'].substate:
- qtlib.WarningMsgBox(_('Subrepository already exists'),
- _('The selected repository:<br><br>%s<br><br>'
- 'is already a subrepository of:<br><br>%s<br><br>'
- 'as: "%s"') % (sroot, root, hglib.tounicode(srelroot)),
- parent=self)
- return
- else:
- # Read the current .hgsub file contents
- lines = []
- hasHgsub = os.path.exists(repo.wjoin(b'.hgsub'))
- if hasHgsub:
- try:
- with repo.wvfs(b'.hgsub', b'r') as fsub:
- lines = fsub.readlines()
- except:
- qtlib.WarningMsgBox(
- _('Failed to add subrepository'),
- _('Cannot open the .hgsub file in:<br><br>%s') \
- % root, parent=self)
- return
-
- # Make sure that the selected subrepo (or one of its
- # subrepos!) is not already on the .hgsub file
- linesep = b''
- # On Windows case is unimportant, while on posix it is
- srelrootnormcase = os.path.normcase(srelroot)
- for line in lines:
- spath = line.split(b"=")[0].strip()
- if not spath:
- continue
- uline = hglib.tounicode(line)
- if not linesep:
- linesep = hglib.fromunicode(hglib.getLineSeparator(uline))
- spath = util.pconvert(spath)
- if os.path.normcase(spath) == srelrootnormcase:
- qtlib.WarningMsgBox(
- _('Failed to add repository'),
- _('The .hgsub file already contains the '
- 'line:<br><br>%s') % uline, parent=self)
- return
- if not linesep:
- linesep = pycompat.oslinesep
-
- # Append the new subrepo to the end of the .hgsub file
- lines.append(b'%s = %s' % (srelroot, srelroot))
- lines = [line.strip(linesep) for line in lines]
-
- # and update the .hgsub file
+ # Read the current .hgsub file contents
+ lines = []
+ hasHgsub = os.path.exists(repo.wjoin(b'.hgsub'))
+ if hasHgsub:
try:
- with repo.wvfs(b'.hgsub', b'w') as fsub:
- fsub.write(linesep.join(lines) + linesep)
-
- if not hasHgsub:
- commands.add(hglib.loadui(),
- repo, repo.wjoin(b'.hgsub'))
- qtlib.InfoMsgBox(
- _('Subrepo added to .hgsub file'),
- _('The selected subrepo:<br><br><i>%s</i><br><br>'
- 'has been added to the .hgsub file of the repository:<br><br><i>%s</i><br><br>'
- 'Remember that in order to finish adding the '
- 'subrepo <i>you must still <u>commit</u></i> the '
- 'changes to the .hgsub file in order to confirm '
- 'the addition of the subrepo.') \
- % (hglib.tounicode(srelroot), root), parent=self)
+ with repo.wvfs(b'.hgsub', b'r') as fsub:
+ lines = fsub.readlines()
except:
qtlib.WarningMsgBox(
+ _('Failed to add subrepository'),
+ _('Cannot open the .hgsub file in:<br><br>%s') \
+ % root, parent=self)
+ return
+
+ # Make sure that the selected subrepo (or one of its
+ # subrepos!) is not already on the .hgsub file
+ linesep = b''
+ # On Windows case is unimportant, while on posix it is
+ srelrootnormcase = os.path.normcase(srelroot)
+ for line in lines:
+ spath = line.split(b"=")[0].strip()
+ if not spath:
+ continue
+ uline = hglib.tounicode(line)
+ if not linesep:
+ linesep = hglib.fromunicode(hglib.getLineSeparator(uline))
+ spath = util.pconvert(spath)
+ if os.path.normcase(spath) == srelrootnormcase:
+ qtlib.WarningMsgBox(
_('Failed to add repository'),
- _('Cannot update the .hgsub file in:<br><br>%s') \
- % root, parent=self)
- return
+ _('The .hgsub file already contains the '
+ 'line:<br><br>%s') % uline, parent=self)
+ return
+ if not linesep:
+ linesep = pycompat.oslinesep
+
+ # Append the new subrepo to the end of the .hgsub file
+ lines.append(b'%s = %s' % (srelroot, srelroot))
+ lines = [line.strip(linesep) for line in lines]
+
+ # and update the .hgsub file
+ try:
+ with repo.wvfs(b'.hgsub', b'w') as fsub:
+ fsub.write(linesep.join(lines) + linesep)
+
+ if not hasHgsub:
+ commands.add(hglib.loadui(),
+ repo, repo.wjoin(b'.hgsub'))
+ qtlib.InfoMsgBox(
+ _('Subrepo added to .hgsub file'),
+ _('The selected subrepo:<br><br><i>%s</i><br><br>'
+ 'has been added to the .hgsub file of the repository:<br><br><i>%s</i><br><br>'
+ 'Remember that in order to finish adding the '
+ 'subrepo <i>you must still <u>commit</u></i> the '
+ 'changes to the .hgsub file in order to confirm '
+ 'the addition of the subrepo.') \
+ % (hglib.tounicode(srelroot), root), parent=self)
+ except:
+ qtlib.WarningMsgBox(
+ _('Failed to add repository'),
+ _('Cannot update the .hgsub file in:<br><br>%s') \
+ % root, parent=self)
+ return
def removeSubrepo(self):
'menu action handler for removing an existing subrepository'