Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/subrepo.py @ 21891:db8a27d92818
subrepo: ensure "close()" execution at the end of "_initrepo()"
Before this patch, "close()" for the file object opened in
"_initrepo()" may not be executed, if unexpected exception is raised,
because it isn't executed in "finally" clause.
This patch ensures "close()" execution at the end of "_initrepo()" by
moving it into "finally" clause.
This patch puts configuration lines into "lines" array and write them
out at once, to narrow the scope of "try"/"finally" for review-ability.
This patch doesn't use "vfs.write()", because:
- current "vfs.write()" implementation doesn't take "mode" argument
to open file in "text" mode
- writing hgrc file out in binary mode may break backward compatibility
author | FUJIWARA Katsunori <foozy@lares.dti.ne.jp> |
---|---|
date | Fri, 20 Jun 2014 00:42:35 +0900 |
parents | ee7e8dcffc92 |
children | d4c972b97fee |
comparison
equal
deleted
inserted
replaced
21890:0f916db7f297 | 21891:db8a27d92818 |
---|---|
607 def _initrepo(self, parentrepo, source, create): | 607 def _initrepo(self, parentrepo, source, create): |
608 self._repo._subparent = parentrepo | 608 self._repo._subparent = parentrepo |
609 self._repo._subsource = source | 609 self._repo._subsource = source |
610 | 610 |
611 if create: | 611 if create: |
612 fp = self._repo.opener("hgrc", "w", text=True) | 612 lines = ['[paths]\n'] |
613 fp.write('[paths]\n') | |
614 | 613 |
615 def addpathconfig(key, value): | 614 def addpathconfig(key, value): |
616 if value: | 615 if value: |
617 fp.write('%s = %s\n' % (key, value)) | 616 lines.append('%s = %s\n' % (key, value)) |
618 self._repo.ui.setconfig('paths', key, value, 'subrepo') | 617 self._repo.ui.setconfig('paths', key, value, 'subrepo') |
619 | 618 |
620 defpath = _abssource(self._repo, abort=False) | 619 defpath = _abssource(self._repo, abort=False) |
621 defpushpath = _abssource(self._repo, True, abort=False) | 620 defpushpath = _abssource(self._repo, True, abort=False) |
622 addpathconfig('default', defpath) | 621 addpathconfig('default', defpath) |
623 if defpath != defpushpath: | 622 if defpath != defpushpath: |
624 addpathconfig('default-push', defpushpath) | 623 addpathconfig('default-push', defpushpath) |
625 fp.close() | 624 |
625 fp = self._repo.opener("hgrc", "w", text=True) | |
626 try: | |
627 fp.write(''.join(lines)) | |
628 finally: | |
629 fp.close() | |
626 | 630 |
627 @annotatesubrepoerror | 631 @annotatesubrepoerror |
628 def add(self, ui, match, dryrun, listsubrepos, prefix, explicitonly): | 632 def add(self, ui, match, dryrun, listsubrepos, prefix, explicitonly): |
629 return cmdutil.add(ui, self._repo, match, dryrun, listsubrepos, | 633 return cmdutil.add(ui, self._repo, match, dryrun, listsubrepos, |
630 os.path.join(prefix, self._path), explicitonly) | 634 os.path.join(prefix, self._path), explicitonly) |