Commits

Anonymous committed c6b34d8

fileview: don't create an empty subrepo unintentionally

When a subrepo is missing from the working directory, viewing the revision
history of any revision where said repository had been modified (initialized,
changed or removed) would create an empty subrepo in the working directory.

The problem was due to the fact that calling ctx.sub(wfile) always creates an
empty repository.

Many of the places where we were calling ctx.sub() were unneeded, since we only
needed to call it to get the current status of the repository, which does not
make sense if the repository is missing or if we are not viewing the working
directory status.

This patch could be further improved as follows:

- Avoid calling ctx.sub() for non hg subrepos, which could be done by
checking: ctx.substate.get(wfile, subrepo.nullstate)[2] != 'hg'
- Let the user create (clone?) missing subrepos.

Comments (0)

Files changed (1)

tortoisehg/hgqt/fileview.py

             try:
                 from mercurial import subrepo, commands
 
-                def genSubrepoRevChangedDescription(sfrom, sto):
+                def genSubrepoRevChangedDescription(subrelpath, sfrom, sto):
                     """Generate a subrepository revision change description"""
                     out = []
                     opts = {'date':None, 'user':None, 'rev':[sfrom]}
-                    if not sfrom:
+                    subabspath = os.path.join(repo.root, subrelpath)
+                    missingsub = not os.path.isdir(subabspath)
+                    def isinitialrevision(rev):
+                        return all([el == '0' for el in rev])
+                    if isinitialrevision(sfrom):
+                        sfrom = ''
+                    if isinitialrevision(sto):
+                        sto = ''
+                    if not sfrom and not sto:
+                        sstatedesc = 'new'
+                        out.append(_('Subrepo created and set to initial revision.') + u'\n\n')
+                        return out, sstatedesc
+                    elif not sfrom:
                         sstatedesc = 'new'
                         out.append(_('Subrepo initialized to revision:') + u'\n\n')
                     elif not sto:
                         return out, sstatedesc
                     else:
                         sstatedesc = 'changed'
+
                         out.append(_('Revision has changed from:') + u'\n\n')
+                        if missingsub:
+                            out.append(hglib.tounicode(_('changeset: %s') % sfrom + '\n'))
+                        else:
+                            _ui.pushbuffer()
+                            commands.log(_ui, srepo, **opts)
+                            out.append(hglib.tounicode(_ui.popbuffer()))
+
+                        out.append(_('To:') + u'\n')
+                    if missingsub:
+                        stolog = _('changeset: %s') % sto + '\n\n'
+                        stolog += _('Subrepository not found in working directory.') + '\n'
+                        stolog += _('Further subrepository revision information cannot be retrieved.') + '\n'
+                    else:
+                        opts['rev'] = [sto]
                         _ui.pushbuffer()
                         commands.log(_ui, srepo, **opts)
-                        out.append(hglib.tounicode(_ui.popbuffer()))
-                        out.append(_('To:') + u'\n')
-                    opts['rev'] = [sto]
-                    _ui.pushbuffer()
-                    commands.log(_ui, srepo, **opts)
-                    stolog = _ui.popbuffer()
+                        stolog = _ui.popbuffer()
+
                     if not stolog:
                         stolog = _('Initial revision')
                     out.append(hglib.tounicode(stolog))
+
                     return out, sstatedesc
 
                 srev = ctx.substate.get(wfile, subrepo.nullstate)[1]
+                srepo = None
                 try:
-                    sub = ctx.sub(wfile)
-                    if isinstance(sub, subrepo.hgsubrepo):
-                        srepo = sub._repo
-                        sactual = srepo['.'].hex()
+                    subabspath = os.path.join(repo.root, wfile)
+                    if not os.path.isdir(subabspath):
+                        sactual = ''
                     else:
-                        self.error = _('Not a Mercurial subrepo, not previewable')
-                        return
+                        sub = ctx.sub(wfile)
+                        if isinstance(sub, subrepo.hgsubrepo):
+                            srepo = sub._repo
+                            sactual = srepo['.'].hex()
+                        else:
+                            self.error = _('Not a Mercurial subrepo, not previewable')
+                            return
                 except (util.Abort), e:
-                    sub = ctx.p1().sub(wfile)
-                    srepo = sub._repo
                     sactual = ''
+
                 out = []
                 _ui = uimod.ui()
-                _ui.pushbuffer()
-                commands.status(_ui, srepo)
-                data = _ui.popbuffer()
-                if data:
-                    out.append(_('File Status:') + u'\n')
-                    out.append(hglib.tounicode(data))
-                    out.append(u'\n')
+
+                if srepo is None or ctx.rev() is not None:
+                    data = []
+                else:
+                    _ui.pushbuffer()
+                    commands.status(_ui, srepo)
+                    data = _ui.popbuffer()
+                    if data:
+                        out.append(_('File Status:') + u'\n')
+                        out.append(hglib.tounicode(data))
+                        out.append(u'\n')
+
                 sstatedesc = 'changed'
                 if ctx.rev() is not None:
                     sparent = ctx.p1().substate.get(wfile, subrepo.nullstate)[1]
-                    subrepochange, sstatedesc = genSubrepoRevChangedDescription(sparent, srev)
+                    subrepochange, sstatedesc = genSubrepoRevChangedDescription(wfile, sparent, srev)
                     out += subrepochange
                 else:
                     sstatedesc = 'dirty'
                     if srev != sactual:
                         subrepochange, sstatedesc = \
-                            genSubrepoRevChangedDescription(srev, sactual)
+                            genSubrepoRevChangedDescription(wfile, srev, sactual)
                         out += subrepochange
                         if data:
                             sstatedesc += ' and dirty'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.