[PATCH 1 of 5] reporegistry: simplify conditional logic in addSubrepo()

4 views
Skip to first unread message

Antonio Muci

unread,
Jun 9, 2023, 3:17:21 AM6/9/23
to thg...@googlegroups.com, a....@inwind.it
# 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'

Antonio Muci

unread,
Jun 9, 2023, 3:17:21 AM6/9/23
to thg...@googlegroups.com, a....@inwind.it
# HG changeset patch
# User Antonio Muci <a....@inwind.it>
# Date 1686294743 -7200
# Fri Jun 09 09:12:23 2023 +0200
# Node ID 4292d53b4a0473b6c61d657832ec18eec2c1b0b6
# Parent 8b07a8287edc0b702fe63ff904a67acce460db8e
reporegistry: remove nop "return" at the end of void function addSubrepo()

addSubrepo() is a void function that requires no return statement at the end.

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
@@ -734,7 +734,6 @@ class RepoRegistryView(QDockWidget):
_('Failed to add repository'),
_('Cannot update the .hgsub file in:<br><br>%s') \
% root, parent=self)
- return

Antonio Muci

unread,
Jun 9, 2023, 3:17:21 AM6/9/23
to thg...@googlegroups.com, a....@inwind.it
# HG changeset patch
# User Antonio Muci <a....@inwind.it>
# Date 1686294539 -7200
# Fri Jun 09 09:08:59 2023 +0200
# Node ID 8b07a8287edc0b702fe63ff904a67acce460db8e
# Parent a5b102cb0a988b3f9e2ea2a6c8855756b462c36e
reporegistry: removed unuseful "else" branch in addSubrepo()

After the previous transforms, it is evident that this "else" branch can be
indented in.

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
@@ -648,94 +648,93 @@ class RepoRegistryView(QDockWidget):
'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
-
- # Use forward slashes for relative subrepo root paths
- srelroot = util.pconvert(hglib.fromunicode(sroot[len(root)+1:]))
+ # The selected path is the root of a repository that is inside
+ # the selected repository

- # 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
+ # Use forward slashes for relative subrepo root paths
+ srelroot = util.pconvert(hglib.fromunicode(sroot[len(root)+1:]))

- 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
+ # 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:

Yuya Nishihara

unread,
Jun 9, 2023, 7:24:15 AM6/9/23
to 'Antonio Muci' via TortoiseHg Developers, a....@inwind.it
On Fri, 09 Jun 2023 09:17:17 +0200, 'Antonio Muci' via TortoiseHg Developers wrote:
> # 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()

Thanks for clean up. Queued.
Reply all
Reply to author
Forward
0 new messages