Mercurial > hg
comparison mercurial/txnutil.py @ 31050:206532700213
txnutil: factor out the logic to read file in according to HG_PENDING
This patch adds new file txnutil.py, because:
- transaction.py is too large to import small utility logic
- scmutil.py or so causes cyclic importing in phases.py
mayhavepending() is defined separately for convenience in subsequent
patch.
author | FUJIWARA Katsunori <foozy@lares.dti.ne.jp> |
---|---|
date | Tue, 21 Feb 2017 01:20:59 +0900 |
parents | |
children | 2372284d9457 |
comparison
equal
deleted
inserted
replaced
31049:20027be9f23d | 31050:206532700213 |
---|---|
1 # txnutil.py - transaction related utilities | |
2 # | |
3 # Copyright FUJIWARA Katsunori <foozy@lares.dti.ne.jp> and others | |
4 # | |
5 # This software may be used and distributed according to the terms of the | |
6 # GNU General Public License version 2 or any later version. | |
7 | |
8 from __future__ import absolute_import | |
9 | |
10 import errno | |
11 | |
12 from . import ( | |
13 encoding, | |
14 ) | |
15 | |
16 def mayhavepending(root): | |
17 '''return whether 'root' may have pending changes, which are | |
18 visible to this process. | |
19 ''' | |
20 return root == encoding.environ.get('HG_PENDING') | |
21 | |
22 def trypending(root, vfs, filename, **kwargs): | |
23 '''Open file to be read according to HG_PENDING environment variable | |
24 | |
25 This opens '.pending' of specified 'filename' only when HG_PENDING | |
26 is equal to 'root'. | |
27 | |
28 This returns '(fp, is_pending_opened)' tuple. | |
29 ''' | |
30 if mayhavepending(root): | |
31 try: | |
32 return (vfs('%s.pending' % filename, **kwargs), True) | |
33 except IOError as inst: | |
34 if inst.errno != errno.ENOENT: | |
35 raise | |
36 return (vfs(filename, **kwargs), False) |