annotate mercurial/config.py @ 45892:06b64fabf91c

copies: cache the ancestor checking call when tracing copy A good share of the time spent in this function is spent doing ancestors checking. To avoid spending time in duplicated call, we cache the result of calls. In the slower case, this provide a quite significant performance boost. Below are the result for a set of selected pairs (many of them pathological): (And further down is another table that summarize the current state of filelog based vs changeset base copy tracing) The benchmark have been configured to be killed after 6 minutes of runtime, which mean that any detect slower than 2 minutes will be marked as "killed". This drop some useful information about how much slower these case are? but also prevent 99% of the benchmark time to be spent on case that can be labelled "very slow" anyway. Repo Case Source-Rev Dest-Rev Old-Time New-Time Difference Factor ------------------------------------------------------------------------------------------------------------------------------------ mercurial x_revs_x_added_0_copies ad6b123de1c7 39cfcef4f463 : 0.000044 s, 0.000044 s, +0.000000 s, ? 1.0000 mercurial x_revs_x_added_x_copies 2b1c78674230 0c1d10351869 : 0.000138 s, 0.000138 s, +0.000000 s, ? 1.0000 mercurial x000_revs_x000_added_x_copies 81f8ff2a9bf2 dd3267698d84 : 0.005067 s, 0.005052 s, -0.000015 s, ? 0.9970 pypy x_revs_x_added_0_copies aed021ee8ae8 099ed31b181b : 0.000218 s, 0.000219 s, +0.000001 s, ? 1.0046 pypy x_revs_x000_added_0_copies 4aa4e1f8e19a 359343b9ac0e : 0.000053 s, 0.000055 s, +0.000002 s, ? 1.0377 pypy x_revs_x_added_x_copies ac52eb7bbbb0 72e022663155 : 0.000125 s, 0.000128 s, +0.000003 s, ? 1.0240 pypy x_revs_x00_added_x_copies c3b14617fbd7 ace7255d9a26 : 0.001098 s, 0.001089 s, -0.000009 s, ? 0.9918 pypy x_revs_x000_added_x000_copies df6f7a526b60 a83dc6a2d56f : 0.017546 s, 0.017407 s, -0.000139 s, ? 0.9921 pypy x000_revs_xx00_added_0_copies 89a76aede314 2f22446ff07e : 0.096723 s, 0.094175 s, -0.002548 s, ? 0.9737 pypy x000_revs_x000_added_x_copies 8a3b5bfd266e 2c68e87c3efe : 0.271796 s, 0.238009 s, -0.033787 s, ? 0.8757 pypy x000_revs_x000_added_x000_copies 89a76aede314 7b3dda341c84 : 0.128602 s, 0.125876 s, -0.002726 s, ? 0.9788 pypy x0000_revs_x_added_0_copies d1defd0dc478 c9cb1334cc78 : 7.086742 s, 3.581556 s, -3.505186 s, ? 0.5054 pypy x0000_revs_xx000_added_0_copies bf2c629d0071 4ffed77c095c : 0.016634 s, 0.016721 s, +0.000087 s, ? 1.0052 pypy x0000_revs_xx000_added_x000_copies 08ea3258278e d9fa043f30c0 : 0.254225 s, 0.242367 s, -0.011858 s, ? 0.9534 netbeans x_revs_x_added_0_copies fb0955ffcbcd a01e9239f9e7 : 0.000166 s, 0.000165 s, -0.000001 s, ? 0.9940 netbeans x_revs_x000_added_0_copies 6f360122949f 20eb231cc7d0 : 0.000118 s, 0.000114 s, -0.000004 s, ? 0.9661 netbeans x_revs_x_added_x_copies 1ada3faf6fb6 5a39d12eecf4 : 0.000296 s, 0.000296 s, +0.000000 s, ? 1.0000 netbeans x_revs_x00_added_x_copies 35be93ba1e2c 9eec5e90c05f : 0.001137 s, 0.001124 s, -0.000013 s, ? 0.9886 netbeans x000_revs_xx00_added_0_copies eac3045b4fdd 51d4ae7f1290 : 0.014133 s, 0.013060 s, -0.001073 s, ? 0.9241 netbeans x000_revs_x000_added_x_copies e2063d266acd 6081d72689dc : 0.016988 s, 0.017112 s, +0.000124 s, ? 1.0073 netbeans x000_revs_x000_added_x000_copies ff453e9fee32 411350406ec2 : 0.676361 s, 0.660350 s, -0.016011 s, ? 0.9763 netbeans x0000_revs_xx000_added_x000_copies 588c2d1ced70 1aad62e59ddd : 12.515149 s, 10.032499 s, -2.482650 s, ? 0.8016 mozilla-central x_revs_x_added_0_copies 3697f962bb7b 7015fcdd43a2 : 0.000186 s, 0.000189 s, +0.000003 s, ? 1.0161 mozilla-central x_revs_x000_added_0_copies dd390860c6c9 40d0c5bed75d : 0.000459 s, 0.000462 s, +0.000003 s, ? 1.0065 mozilla-central x_revs_x_added_x_copies 8d198483ae3b 14207ffc2b2f : 0.000273 s, 0.000270 s, -0.000003 s, ? 0.9890 mozilla-central x_revs_x00_added_x_copies 98cbc58cc6bc 446a150332c3 : 0.001503 s, 0.001474 s, -0.000029 s, ? 0.9807 mozilla-central x_revs_x000_added_x000_copies 3c684b4b8f68 0a5e72d1b479 : 0.004862 s, 0.004806 s, -0.000056 s, ? 0.9885 mozilla-central x_revs_x0000_added_x0000_copies effb563bb7e5 c07a39dc4e80 : 0.088291 s, 0.085150 s, -0.003141 s, ? 0.9644 mozilla-central x000_revs_xx00_added_0_copies 6100d773079a 04a55431795e : 0.007113 s, 0.007064 s, -0.000049 s, ? 0.9931 mozilla-central x000_revs_x000_added_x_copies 9f17a6fc04f9 2d37b966abed : 0.004687 s, 0.004741 s, +0.000054 s, ? 1.0115 mozilla-central x000_revs_x000_added_x000_copies 7c97034feb78 4407bd0c6330 : 0.198710 s, 0.190133 s, -0.008577 s, ? 0.9568 mozilla-central x0000_revs_xx000_added_0_copies 9eec5917337d 67118cc6dcad : 0.036068 s, 0.035651 s, -0.000417 s, ? 0.9884 mozilla-central x0000_revs_xx000_added_x000_copies f78c615a656c 96a38b690156 : 0.465362 s, 0.440694 s, -0.024668 s, ? 0.9470 mozilla-central x00000_revs_x0000_added_x0000_copies 6832ae71433c 4c222a1d9a00 : 24.519684 s, 18.454163 s, -6.065521 s, ? 0.7526 mozilla-central x00000_revs_x00000_added_x000_copies 76caed42cf7c 1daa622bbe42 : 42.711897 s, 31.562719 s, -11.149178 s, ? 0.7390 mozilla-try x_revs_x_added_0_copies aaf6dde0deb8 9790f499805a : 0.001201 s, 0.001189 s, -0.000012 s, ? 0.9900 mozilla-try x_revs_x000_added_0_copies d8d0222927b4 5bb8ce8c7450 : 0.001216 s, 0.001204 s, -0.000012 s, ? 0.9901 mozilla-try x_revs_x_added_x_copies 092fcca11bdb 936255a0384a : 0.000595 s, 0.000586 s, -0.000009 s, ? 0.9849 mozilla-try x_revs_x00_added_x_copies b53d2fadbdb5 017afae788ec : 0.001856 s, 0.001845 s, -0.000011 s, ? 0.9941 mozilla-try x_revs_x000_added_x000_copies 20408ad61ce5 6f0ee96e21ad : 0.064936 s, 0.063822 s, -0.001114 s, ? 0.9828 mozilla-try x_revs_x0000_added_x0000_copies effb563bb7e5 c07a39dc4e80 : 0.090601 s, 0.088038 s, -0.002563 s, ? 0.9717 mozilla-try x000_revs_xx00_added_0_copies 6100d773079a 04a55431795e : 0.007510 s, 0.007389 s, -0.000121 s, ? 0.9839 mozilla-try x000_revs_x000_added_x_copies 9f17a6fc04f9 2d37b966abed : 0.004911 s, 0.004868 s, -0.000043 s, ? 0.9912 mozilla-try x000_revs_x000_added_x000_copies 1346fd0130e4 4c65cbdabc1f : 0.233231 s, 0.222450 s, -0.010781 s, ? 0.9538 mozilla-try x0000_revs_x_added_0_copies 63519bfd42ee a36a2a865d92 : 0.419989 s, 0.370675 s, -0.049314 s, ? 0.8826 mozilla-try x0000_revs_x_added_x_copies 9fe69ff0762d bcabf2a78927 : 0.401521 s, 0.358020 s, -0.043501 s, ? 0.8917 mozilla-try x0000_revs_xx000_added_x_copies 156f6e2674f2 4d0f2c178e66 : 0.179555 s, 0.145235 s, -0.034320 s, ? 0.8089 mozilla-try x0000_revs_xx000_added_0_copies 9eec5917337d 67118cc6dcad : 0.038004 s, 0.037606 s, -0.000398 s, ? 0.9895 mozilla-try x0000_revs_xx000_added_x000_copies 89294cd501d9 7ccb2fc7ccb5 : 52.838482 s, 7.382439 s, -45.456043 s, ? 0.1397 mozilla-try x0000_revs_x0000_added_x0000_copies e928c65095ed e951f4ad123a : 8.705874 s, 7.273506 s, -1.432368 s, ? 0.8355 mozilla-try x00000_revs_x00000_added_0_copies dc8a3ca7010e d16fde900c9c : 1.126708 s, 1.074593 s, -0.052115 s, ? 0.9537 mozilla-try x00000_revs_x0000_added_x0000_copies 8d3fafa80d4b eb884023b810 : 83.854020 s, 27.746195 s, -56.107825 s, ? 0.3309 Below is a table comparing the runtime of the current "filelog centric" algorithm, with the "changeset centric" one, we just modified. The changeset centric algorithm is a significant win in many scenario, but they are still various cases where it is quite slower. When many revision has to be considered the cost of retrieving the copy information, creating new dictionaries, merging dictionaries and checking if revision are ancestors of each other can slow things down. The rest of this series, will introduce a rust version of the copy tracing code to deal with most of theses issues. Repo Case Source-Rev Dest-Rev filelog sidedata Difference Factor --------------------------------------------------------------------------------------------------------------------------------------- mercurial x_revs_x_added_0_copies ad6b123de1c7 39cfcef4f463 : 0.000914 s, 0.000044 s, - 0.000870 s, ? 0.048140 mercurial x_revs_x_added_x_copies 2b1c78674230 0c1d10351869 : 0.001812 s, 0.000138 s, - 0.001674 s, ? 0.076159 mercurial x000_revs_x000_added_x_copies 81f8ff2a9bf2 dd3267698d84 : 0.017954 s, 0.005052 s, - 0.012902 s, ? 0.281386 pypy x_revs_x_added_0_copies aed021ee8ae8 099ed31b181b : 0.001509 s, 0.000219 s, - 0.001290 s, ? 0.145129 pypy x_revs_x000_added_0_copies 4aa4e1f8e19a 359343b9ac0e : 0.206881 s, 0.000055 s, - 0.206826 s, ? 0.000266 pypy x_revs_x_added_x_copies ac52eb7bbbb0 72e022663155 : 0.016951 s, 0.000128 s, - 0.016823 s, ? 0.007551 pypy x_revs_x00_added_x_copies c3b14617fbd7 ace7255d9a26 : 0.019096 s, 0.001089 s, - 0.018007 s, ? 0.057028 pypy x_revs_x000_added_x000_copies df6f7a526b60 a83dc6a2d56f : 0.762506 s, 0.017407 s, - 0.745099 s, ? 0.022829 pypy x000_revs_xx00_added_0_copies 89a76aede314 2f22446ff07e : 1.179211 s, 0.094175 s, - 1.085036 s, ? 0.079863 pypy x000_revs_x000_added_x_copies 8a3b5bfd266e 2c68e87c3efe : 1.249058 s, 0.238009 s, - 1.011049 s, ? 0.190551 pypy x000_revs_x000_added_x000_copies 89a76aede314 7b3dda341c84 : 1.614107 s, 0.125876 s, - 1.488231 s, ? 0.077985 pypy x0000_revs_x_added_0_copies d1defd0dc478 c9cb1334cc78 : 0.001064 s, 3.581556 s, + 3.580492 s, ? 3366.124060 pypy x0000_revs_xx000_added_0_copies bf2c629d0071 4ffed77c095c : 1.061275 s, 0.016721 s, - 1.044554 s, ? 0.015756 pypy x0000_revs_xx000_added_x000_copies 08ea3258278e d9fa043f30c0 : 1.341119 s, 0.242367 s, - 1.098752 s, ? 0.180720 netbeans x_revs_x_added_0_copies fb0955ffcbcd a01e9239f9e7 : 0.027803 s, 0.000165 s, - 0.027638 s, ? 0.005935 netbeans x_revs_x000_added_0_copies 6f360122949f 20eb231cc7d0 : 0.130014 s, 0.000114 s, - 0.129900 s, ? 0.000877 netbeans x_revs_x_added_x_copies 1ada3faf6fb6 5a39d12eecf4 : 0.024990 s, 0.000296 s, - 0.024694 s, ? 0.011845 netbeans x_revs_x00_added_x_copies 35be93ba1e2c 9eec5e90c05f : 0.052201 s, 0.001124 s, - 0.051077 s, ? 0.021532 netbeans x000_revs_xx00_added_0_copies eac3045b4fdd 51d4ae7f1290 : 0.037642 s, 0.013060 s, - 0.024582 s, ? 0.346953 netbeans x000_revs_x000_added_x_copies e2063d266acd 6081d72689dc : 0.197086 s, 0.017112 s, - 0.179974 s, ? 0.086825 netbeans x000_revs_x000_added_x000_copies ff453e9fee32 411350406ec2 : 0.935148 s, 0.660350 s, - 0.274798 s, ? 0.706145 netbeans x0000_revs_xx000_added_x000_copies 588c2d1ced70 1aad62e59ddd : 3.920674 s, 10.032499 s, + 6.111825 s, ? 2.558871 mozilla-central x_revs_x_added_0_copies 3697f962bb7b 7015fcdd43a2 : 0.024232 s, 0.000189 s, - 0.024043 s, ? 0.007800 mozilla-central x_revs_x000_added_0_copies dd390860c6c9 40d0c5bed75d : 0.141483 s, 0.000462 s, - 0.141021 s, ? 0.003265 mozilla-central x_revs_x_added_x_copies 8d198483ae3b 14207ffc2b2f : 0.025775 s, 0.000270 s, - 0.025505 s, ? 0.010475 mozilla-central x_revs_x00_added_x_copies 98cbc58cc6bc 446a150332c3 : 0.084922 s, 0.001474 s, - 0.083448 s, ? 0.017357 mozilla-central x_revs_x000_added_x000_copies 3c684b4b8f68 0a5e72d1b479 : 0.194784 s, 0.004806 s, - 0.189978 s, ? 0.024673 mozilla-central x_revs_x0000_added_x0000_copies effb563bb7e5 c07a39dc4e80 : 2.161103 s, 0.085150 s, - 2.075953 s, ? 0.039401 mozilla-central x000_revs_xx00_added_0_copies 6100d773079a 04a55431795e : 0.089347 s, 0.007064 s, - 0.082283 s, ? 0.079063 mozilla-central x000_revs_x000_added_x_copies 9f17a6fc04f9 2d37b966abed : 0.732171 s, 0.004741 s, - 0.727430 s, ? 0.006475 mozilla-central x000_revs_x000_added_x000_copies 7c97034feb78 4407bd0c6330 : 1.157287 s, 0.190133 s, - 0.967154 s, ? 0.164292 mozilla-central x0000_revs_xx000_added_0_copies 9eec5917337d 67118cc6dcad : 6.726568 s, 0.035651 s, - 6.690917 s, ? 0.005300 mozilla-central x0000_revs_xx000_added_x000_copies f78c615a656c 96a38b690156 : 3.266229 s, 0.440694 s, - 2.825535 s, ? 0.134924 mozilla-central x00000_revs_x0000_added_x0000_copies 6832ae71433c 4c222a1d9a00 : 15.860534 s, 18.454163 s, + 2.593629 s, ? 1.163527 mozilla-central x00000_revs_x00000_added_x000_copies 76caed42cf7c 1daa622bbe42 : 20.450475 s, 31.562719 s, +11.112244 s, ? 1.543373 mozilla-try x_revs_x_added_0_copies aaf6dde0deb8 9790f499805a : 0.080442 s, 0.001189 s, - 0.079253 s, ? 0.014781 mozilla-try x_revs_x000_added_0_copies d8d0222927b4 5bb8ce8c7450 : 0.497672 s, 0.001204 s, - 0.496468 s, ? 0.002419 mozilla-try x_revs_x_added_x_copies 092fcca11bdb 936255a0384a : 0.021183 s, 0.000586 s, - 0.020597 s, ? 0.027664 mozilla-try x_revs_x00_added_x_copies b53d2fadbdb5 017afae788ec : 0.230991 s, 0.001845 s, - 0.229146 s, ? 0.007987 mozilla-try x_revs_x000_added_x000_copies 20408ad61ce5 6f0ee96e21ad : 1.118461 s, 0.063822 s, - 1.054639 s, ? 0.057062 mozilla-try x_revs_x0000_added_x0000_copies effb563bb7e5 c07a39dc4e80 : 2.206083 s, 0.088038 s, - 2.118045 s, ? 0.039907 mozilla-try x000_revs_xx00_added_0_copies 6100d773079a 04a55431795e : 0.089404 s, 0.007389 s, - 0.082015 s, ? 0.082647 mozilla-try x000_revs_x000_added_x_copies 9f17a6fc04f9 2d37b966abed : 0.733043 s, 0.004868 s, - 0.728175 s, ? 0.006641 mozilla-try x000_revs_x000_added_x000_copies 1346fd0130e4 4c65cbdabc1f : 1.163367 s, 0.222450 s, - 0.940917 s, ? 0.191212 mozilla-try x0000_revs_x_added_0_copies 63519bfd42ee a36a2a865d92 : 0.085456 s, 0.370675 s, + 0.285219 s, ? 4.337612 mozilla-try x0000_revs_x_added_x_copies 9fe69ff0762d bcabf2a78927 : 0.083601 s, 0.358020 s, + 0.274419 s, ? 4.282485 mozilla-try x0000_revs_xx000_added_x_copies 156f6e2674f2 4d0f2c178e66 : 7.366614 s, 0.145235 s, - 7.221379 s, ? 0.019715 mozilla-try x0000_revs_xx000_added_0_copies 9eec5917337d 67118cc6dcad : 6.664464 s, 0.037606 s, - 6.626858 s, ? 0.005643 mozilla-try x0000_revs_xx000_added_x000_copies 89294cd501d9 7ccb2fc7ccb5 : 7.467836 s, 7.382439 s, - 0.085397 s, ? 0.988565 mozilla-try x0000_revs_x0000_added_x0000_copies e928c65095ed e951f4ad123a : 9.801294 s, 7.273506 s, - 2.527788 s, ? 0.742097 mozilla-try x00000_revs_x_added_0_copies 6a320851d377 1ebb79acd503 : 0.091886 s, killed mozilla-try x00000_revs_x00000_added_0_copies dc8a3ca7010e d16fde900c9c : 26.491140 s, 1.074593 s, -25.416547 s, ? 0.040564 mozilla-try x00000_revs_x_added_x_copies 5173c4b6f97c 95d83ee7242d : 0.092863 s, killed mozilla-try x00000_revs_x000_added_x_copies 9126823d0e9c ca82787bb23c : 0.226823 s, killed mozilla-try x00000_revs_x0000_added_x0000_copies 8d3fafa80d4b eb884023b810 : 18.914630 s, 27.746195 s, + 8.831565 s, ? 1.466917 mozilla-try x00000_revs_x00000_added_x0000_copies 1b661134e2ca 1ae03d022d6d : 21.198903 s, killed mozilla-try x00000_revs_x00000_added_x000_copies 9b2a99adc05e 8e29777b48e6 : 24.952268 s, killed Differential Revision: https://phab.mercurial-scm.org/D9296
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Mon, 02 Nov 2020 11:03:56 +0100
parents 0883413e09bc
children 9dc1351d0b5f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8229
ddf3d6656e7c config: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 8222
diff changeset
1 # config.py - configuration parsing for Mercurial
ddf3d6656e7c config: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 8222
diff changeset
2 #
ddf3d6656e7c config: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 8222
diff changeset
3 # Copyright 2009 Matt Mackall <mpm@selenic.com> and others
ddf3d6656e7c config: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 8222
diff changeset
4 #
ddf3d6656e7c config: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 8222
diff changeset
5 # This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 10042
diff changeset
6 # GNU General Public License version 2 or any later version.
8229
ddf3d6656e7c config: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 8222
diff changeset
7
25923
a027a0813b44 config: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
8 from __future__ import absolute_import
a027a0813b44 config: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
9
a027a0813b44 config: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
10 import errno
a027a0813b44 config: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
11 import os
a027a0813b44 config: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
12
a027a0813b44 config: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
13 from .i18n import _
43089
c59eb1560c44 py3: manually import getattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43077
diff changeset
14 from .pycompat import getattr
25923
a027a0813b44 config: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
15 from . import (
43276
d201a637c971 py3: encode underlying error message during parse error of %include
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43106
diff changeset
16 encoding,
25923
a027a0813b44 config: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
17 error,
31306
c920efa9d34b config: guard against setconfig specifying unicode values on py3
Augie Fackler <raf@durin42.com>
parents: 31176
diff changeset
18 pycompat,
25923
a027a0813b44 config: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
19 util,
a027a0813b44 config: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
20 )
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
21
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
22
8186
6a0018cdb2fe config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents: 8185
diff changeset
23 class config(object):
45254
3f54242781e9 config: remove now-unused support for "includepaths"
Martin von Zweigbergk <martinvonz@google.com>
parents: 45208
diff changeset
24 def __init__(self, data=None):
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
25 self._data = {}
19087
7d82ad4b3727 config: discard "%unset" values defined in the other files read in previously
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17537
diff changeset
26 self._unset = []
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
27 if data:
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
28 for k in data._data:
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
29 self._data[k] = data[k].copy()
8185
dc10a7a3f1d4 config: split source data out into separate map
Matt Mackall <mpm@selenic.com>
parents: 8184
diff changeset
30 self._source = data._source.copy()
34357
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
31 else:
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
32 self._source = util.cowdict()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
33
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
34 def copy(self):
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
35 return config(self)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
36
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
37 def __contains__(self, section):
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
38 return section in self._data
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
39
27696
e70c97cc9243 config: add hasconfig method and supporting plumbing
Bryan O'Sullivan <bos@serpentine.com>
parents: 25923
diff changeset
40 def hasitem(self, section, item):
e70c97cc9243 config: add hasconfig method and supporting plumbing
Bryan O'Sullivan <bos@serpentine.com>
parents: 25923
diff changeset
41 return item in self._data.get(section, {})
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
42
8186
6a0018cdb2fe config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents: 8185
diff changeset
43 def __getitem__(self, section):
6a0018cdb2fe config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents: 8185
diff changeset
44 return self._data.get(section, {})
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
45
8186
6a0018cdb2fe config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents: 8185
diff changeset
46 def __iter__(self):
6a0018cdb2fe config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents: 8185
diff changeset
47 for d in self.sections():
6a0018cdb2fe config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents: 8185
diff changeset
48 yield d
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
49
8193
94246e90081e config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents: 8192
diff changeset
50 def update(self, src):
34357
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
51 self._source = self._source.preparewrite()
19087
7d82ad4b3727 config: discard "%unset" values defined in the other files read in previously
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17537
diff changeset
52 for s, n in src._unset:
34357
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
53 ds = self._data.get(s, None)
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
54 if ds is not None and n in ds:
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
55 self._data[s] = ds.preparewrite()
19087
7d82ad4b3727 config: discard "%unset" values defined in the other files read in previously
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17537
diff changeset
56 del self._data[s][n]
7d82ad4b3727 config: discard "%unset" values defined in the other files read in previously
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17537
diff changeset
57 del self._source[(s, n)]
8193
94246e90081e config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents: 8192
diff changeset
58 for s in src:
34357
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
59 ds = self._data.get(s, None)
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
60 if ds:
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
61 self._data[s] = ds.preparewrite()
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
62 else:
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
63 self._data[s] = util.cowsortdict()
8193
94246e90081e config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents: 8192
diff changeset
64 self._data[s].update(src._data[s])
94246e90081e config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents: 8192
diff changeset
65 self._source.update(src._source)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
66
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
67 def get(self, section, item, default=None):
8185
dc10a7a3f1d4 config: split source data out into separate map
Matt Mackall <mpm@selenic.com>
parents: 8184
diff changeset
68 return self._data.get(section, {}).get(item, default)
15919
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
69
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
70 def backup(self, section, item):
17527
6e11d5cf8e39 spelling: value
timeless@mozdev.org
parents: 16944
diff changeset
71 """return a tuple allowing restore to reinstall a previous value
15919
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
72
17530
0dff04ffa15d grammar: it-handles
timeless@mozdev.org
parents: 17527
diff changeset
73 The main reason we need it is because it handles the "no data" case.
15919
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
74 """
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
75 try:
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
76 value = self._data[section][item]
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
77 source = self.source(section, item)
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
78 return (section, item, value, source)
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
79 except KeyError:
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
80 return (section, item)
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
81
8198
cf9accffd0b3 config: getsource -> source
Matt Mackall <mpm@selenic.com>
parents: 8193
diff changeset
82 def source(self, section, item):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
83 return self._source.get((section, item), b"")
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
84
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
85 def sections(self):
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
86 return sorted(self._data.keys())
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
87
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
88 def items(self, section):
43106
d783f945a701 py3: finish porting iteritems() to pycompat and remove source transformer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43089
diff changeset
89 return list(pycompat.iteritems(self._data.get(section, {})))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
90
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
91 def set(self, section, item, value, source=b""):
31306
c920efa9d34b config: guard against setconfig specifying unicode values on py3
Augie Fackler <raf@durin42.com>
parents: 31176
diff changeset
92 if pycompat.ispy3:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
93 assert not isinstance(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
94 section, str
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
95 ), b'config section may not be unicode strings on Python 3'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
96 assert not isinstance(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
97 item, str
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
98 ), b'config item may not be unicode strings on Python 3'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
99 assert not isinstance(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
100 value, str
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
101 ), b'config values may not be unicode strings on Python 3'
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
102 if section not in self:
34357
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
103 self._data[section] = util.cowsortdict()
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
104 else:
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
105 self._data[section] = self._data[section].preparewrite()
8185
dc10a7a3f1d4 config: split source data out into separate map
Matt Mackall <mpm@selenic.com>
parents: 8184
diff changeset
106 self._data[section][item] = value
20789
d19c9bdbbf35 config: don't set source when no source is specified - don't overwrite with ''
Mads Kiilerich <madski@unity3d.com>
parents: 19087
diff changeset
107 if source:
34357
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
108 self._source = self._source.preparewrite()
20789
d19c9bdbbf35 config: don't set source when no source is specified - don't overwrite with ''
Mads Kiilerich <madski@unity3d.com>
parents: 19087
diff changeset
109 self._source[(section, item)] = source
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
110
15919
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
111 def restore(self, data):
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
112 """restore data returned by self.backup"""
34357
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
113 self._source = self._source.preparewrite()
15919
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
114 if len(data) == 4:
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
115 # restore old data
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
116 section, item, value, source = data
34357
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
117 self._data[section] = self._data[section].preparewrite()
15919
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
118 self._data[section][item] = value
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
119 self._source[(section, item)] = source
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
120 else:
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
121 # no data before, remove everything
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
122 section, item = data
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
123 if section in self._data:
22037
8665c647da6e config: fix restoreconfig of non existing config
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21910
diff changeset
124 self._data[section].pop(item, None)
15919
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
125 self._source.pop((section, item), None)
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
126
8265
52c5be55af82 config: add parse interface
Matt Mackall <mpm@selenic.com>
parents: 8263
diff changeset
127 def parse(self, src, data, sections=None, remap=None, include=None):
30349
954002426f78 config: mark parser regexes as bytes explicitly
Augie Fackler <augie@google.com>
parents: 27696
diff changeset
128 sectionre = util.re.compile(br'\[([^\[]+)\]')
954002426f78 config: mark parser regexes as bytes explicitly
Augie Fackler <augie@google.com>
parents: 27696
diff changeset
129 itemre = util.re.compile(br'([^=\s][^=]*?)\s*=\s*(.*\S|)')
954002426f78 config: mark parser regexes as bytes explicitly
Augie Fackler <augie@google.com>
parents: 27696
diff changeset
130 contre = util.re.compile(br'\s+(\S|\S.*\S)\s*$')
954002426f78 config: mark parser regexes as bytes explicitly
Augie Fackler <augie@google.com>
parents: 27696
diff changeset
131 emptyre = util.re.compile(br'(;|#|\s*$)')
954002426f78 config: mark parser regexes as bytes explicitly
Augie Fackler <augie@google.com>
parents: 27696
diff changeset
132 commentre = util.re.compile(br'(;|#)')
954002426f78 config: mark parser regexes as bytes explicitly
Augie Fackler <augie@google.com>
parents: 27696
diff changeset
133 unsetre = util.re.compile(br'%unset\s+(\S+)')
954002426f78 config: mark parser regexes as bytes explicitly
Augie Fackler <augie@google.com>
parents: 27696
diff changeset
134 includere = util.re.compile(br'%include\s+(\S|\S.*\S)\s*$')
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
135 section = b""
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
136 item = None
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
137 line = 0
9339
9be91129c96e config: improve code readability
Andrey <py4fun@gmail.com>
parents: 9136
diff changeset
138 cont = False
8180
6fc30fe7f3e7 hgweb: use config.config
Matt Mackall <mpm@selenic.com>
parents: 8144
diff changeset
139
34713
e5a2cfc524d4 config: allow remapping the default section
Yuya Nishihara <yuya@tcha.org>
parents: 34454
diff changeset
140 if remap:
e5a2cfc524d4 config: allow remapping the default section
Yuya Nishihara <yuya@tcha.org>
parents: 34454
diff changeset
141 section = remap.get(section, section)
e5a2cfc524d4 config: allow remapping the default section
Yuya Nishihara <yuya@tcha.org>
parents: 34454
diff changeset
142
9136
31177742f54a for calls expecting bool args, pass bool instead of int
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8312
diff changeset
143 for l in data.splitlines(True):
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
144 line += 1
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
145 if line == 1 and l.startswith(b'\xef\xbb\xbf'):
16348
f350021ee32e config: discard UTF-8 BOM if found
Matt Mackall <mpm@selenic.com>
parents: 15919
diff changeset
146 # Someone set us up the BOM
f350021ee32e config: discard UTF-8 BOM if found
Matt Mackall <mpm@selenic.com>
parents: 15919
diff changeset
147 l = l[3:]
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
148 if cont:
14642
fdcdb221a922 config: handle comment lines in continuations (issue2854)
Matt Mackall <mpm@selenic.com>
parents: 14486
diff changeset
149 if commentre.match(l):
fdcdb221a922 config: handle comment lines in continuations (issue2854)
Matt Mackall <mpm@selenic.com>
parents: 14486
diff changeset
150 continue
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
151 m = contre.match(l)
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
152 if m:
8193
94246e90081e config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents: 8192
diff changeset
153 if sections and section not in sections:
94246e90081e config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents: 8192
diff changeset
154 continue
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
155 v = self.get(section, item) + b"\n" + m.group(1)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
156 self.set(section, item, v, b"%s:%d" % (src, line))
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
157 continue
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
158 item = None
9469
7f0f882af23d config: abort on indented non-continuation lines (issue1829)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8312
diff changeset
159 cont = False
8183
2858ab754995 config: allow including other config files
Matt Mackall <mpm@selenic.com>
parents: 8180
diff changeset
160 m = includere.match(l)
25095
3182965b3971 config: give it an includepaths option for looking for config files
Jordi Guti?rrez Hermoso <jordigh@octave.org>
parents: 22419
diff changeset
161
3182965b3971 config: give it an includepaths option for looking for config files
Jordi Guti?rrez Hermoso <jordigh@octave.org>
parents: 22419
diff changeset
162 if m and include:
3182965b3971 config: give it an includepaths option for looking for config files
Jordi Guti?rrez Hermoso <jordigh@octave.org>
parents: 22419
diff changeset
163 expanded = util.expandpath(m.group(1))
45254
3f54242781e9 config: remove now-unused support for "includepaths"
Martin von Zweigbergk <martinvonz@google.com>
parents: 45208
diff changeset
164 try:
45257
668af67bfd18 config: remove now-unused `abs` argument from `include` callback
Martin von Zweigbergk <martinvonz@google.com>
parents: 45256
diff changeset
165 include(expanded, remap=remap, sections=sections)
45254
3f54242781e9 config: remove now-unused support for "includepaths"
Martin von Zweigbergk <martinvonz@google.com>
parents: 45208
diff changeset
166 except IOError as inst:
3f54242781e9 config: remove now-unused support for "includepaths"
Martin von Zweigbergk <martinvonz@google.com>
parents: 45208
diff changeset
167 if inst.errno != errno.ENOENT:
3f54242781e9 config: remove now-unused support for "includepaths"
Martin von Zweigbergk <martinvonz@google.com>
parents: 45208
diff changeset
168 raise error.ParseError(
3f54242781e9 config: remove now-unused support for "includepaths"
Martin von Zweigbergk <martinvonz@google.com>
parents: 45208
diff changeset
169 _(b"cannot include %s (%s)")
45257
668af67bfd18 config: remove now-unused `abs` argument from `include` callback
Martin von Zweigbergk <martinvonz@google.com>
parents: 45256
diff changeset
170 % (expanded, encoding.strtolocal(inst.strerror)),
45254
3f54242781e9 config: remove now-unused support for "includepaths"
Martin von Zweigbergk <martinvonz@google.com>
parents: 45208
diff changeset
171 b"%s:%d" % (src, line),
3f54242781e9 config: remove now-unused support for "includepaths"
Martin von Zweigbergk <martinvonz@google.com>
parents: 45208
diff changeset
172 )
8183
2858ab754995 config: allow including other config files
Matt Mackall <mpm@selenic.com>
parents: 8180
diff changeset
173 continue
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
174 if emptyre.match(l):
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
175 continue
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
176 m = sectionre.match(l)
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
177 if m:
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
178 section = m.group(1)
8298
9542f4c3fa1b config: make remap actually work
Matt Mackall <mpm@selenic.com>
parents: 8265
diff changeset
179 if remap:
9542f4c3fa1b config: make remap actually work
Matt Mackall <mpm@selenic.com>
parents: 8265
diff changeset
180 section = remap.get(section, section)
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
181 if section not in self:
34357
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
182 self._data[section] = util.cowsortdict()
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
183 continue
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
184 m = itemre.match(l)
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
185 if m:
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
186 item = m.group(1)
9339
9be91129c96e config: improve code readability
Andrey <py4fun@gmail.com>
parents: 9136
diff changeset
187 cont = True
8193
94246e90081e config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents: 8192
diff changeset
188 if sections and section not in sections:
94246e90081e config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents: 8192
diff changeset
189 continue
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
190 self.set(section, item, m.group(2), b"%s:%d" % (src, line))
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
191 continue
8184
9189afe1eba3 config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents: 8183
diff changeset
192 m = unsetre.match(l)
9189afe1eba3 config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents: 8183
diff changeset
193 if m:
9189afe1eba3 config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents: 8183
diff changeset
194 name = m.group(1)
8193
94246e90081e config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents: 8192
diff changeset
195 if sections and section not in sections:
94246e90081e config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents: 8192
diff changeset
196 continue
13031
3da456d0c885 code style: prefer 'is' and 'is not' tests with singletons
Martin Geisler <mg@aragost.com>
parents: 11292
diff changeset
197 if self.get(section, name) is not None:
34454
0efdfb57b05c config: add a missing preparewrite() call
Jun Wu <quark@fb.com>
parents: 34357
diff changeset
198 self._data[section] = self._data[section].preparewrite()
8184
9189afe1eba3 config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents: 8183
diff changeset
199 del self._data[section][name]
19087
7d82ad4b3727 config: discard "%unset" values defined in the other files read in previously
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17537
diff changeset
200 self._unset.append((section, name))
8184
9189afe1eba3 config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents: 8183
diff changeset
201 continue
9189afe1eba3 config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents: 8183
diff changeset
202
45777
0883413e09bc config: move message about leading spaces in config to config.py
Martin von Zweigbergk <martinvonz@google.com>
parents: 45257
diff changeset
203 message = l.rstrip()
0883413e09bc config: move message about leading spaces in config to config.py
Martin von Zweigbergk <martinvonz@google.com>
parents: 45257
diff changeset
204 if l.startswith(b' '):
0883413e09bc config: move message about leading spaces in config to config.py
Martin von Zweigbergk <martinvonz@google.com>
parents: 45257
diff changeset
205 message = b"unexpected leading whitespace: %s" % message
0883413e09bc config: move message about leading spaces in config to config.py
Martin von Zweigbergk <martinvonz@google.com>
parents: 45257
diff changeset
206 raise error.ParseError(message, (b"%s:%d" % (src, line)))
8265
52c5be55af82 config: add parse interface
Matt Mackall <mpm@selenic.com>
parents: 8263
diff changeset
207
52c5be55af82 config: add parse interface
Matt Mackall <mpm@selenic.com>
parents: 8263
diff changeset
208 def read(self, path, fp=None, sections=None, remap=None):
52c5be55af82 config: add parse interface
Matt Mackall <mpm@selenic.com>
parents: 8263
diff changeset
209 if not fp:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
210 fp = util.posixfile(path, b'rb')
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43346
diff changeset
211 assert getattr(fp, 'mode', 'rb') == 'rb', (
43346
6ada8a274b9c formatting: run black version 19.10b0 on the codebase
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43276
diff changeset
212 b'config files must be opened in binary mode, got fp=%r mode=%r'
6ada8a274b9c formatting: run black version 19.10b0 on the codebase
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43276
diff changeset
213 % (fp, fp.mode,)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
214 )
45208
f7f142d74df3 config: pass both relative and absolute paths to `include` callback
Martin von Zweigbergk <martinvonz@google.com>
parents: 43506
diff changeset
215
45256
83ca8d6f3206 config: re-calculate absolute %include path in `include` callback
Martin von Zweigbergk <martinvonz@google.com>
parents: 45254
diff changeset
216 dir = os.path.dirname(path)
83ca8d6f3206 config: re-calculate absolute %include path in `include` callback
Martin von Zweigbergk <martinvonz@google.com>
parents: 45254
diff changeset
217
45257
668af67bfd18 config: remove now-unused `abs` argument from `include` callback
Martin von Zweigbergk <martinvonz@google.com>
parents: 45256
diff changeset
218 def include(rel, remap, sections):
45256
83ca8d6f3206 config: re-calculate absolute %include path in `include` callback
Martin von Zweigbergk <martinvonz@google.com>
parents: 45254
diff changeset
219 abs = os.path.normpath(os.path.join(dir, rel))
45208
f7f142d74df3 config: pass both relative and absolute paths to `include` callback
Martin von Zweigbergk <martinvonz@google.com>
parents: 43506
diff changeset
220 self.read(abs, remap=remap, sections=sections)
f7f142d74df3 config: pass both relative and absolute paths to `include` callback
Martin von Zweigbergk <martinvonz@google.com>
parents: 43506
diff changeset
221
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
222 self.parse(
45208
f7f142d74df3 config: pass both relative and absolute paths to `include` callback
Martin von Zweigbergk <martinvonz@google.com>
parents: 43506
diff changeset
223 path, fp.read(), sections=sections, remap=remap, include=include
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
224 )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
225
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
226
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
227 def parselist(value):
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
228 """parse a configuration value as a list of comma/space separated strings
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
229
34131
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 32318
diff changeset
230 >>> parselist(b'this,is "a small" ,test')
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
231 ['this', 'is', 'a small', 'test']
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
232 """
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
233
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
234 def _parse_plain(parts, s, offset):
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
235 whitespace = False
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
236 while offset < len(s) and (
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
237 s[offset : offset + 1].isspace() or s[offset : offset + 1] == b','
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
238 ):
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
239 whitespace = True
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
240 offset += 1
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
241 if offset >= len(s):
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
242 return None, parts, offset
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
243 if whitespace:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
244 parts.append(b'')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
245 if s[offset : offset + 1] == b'"' and not parts[-1]:
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
246 return _parse_quote, parts, offset + 1
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
247 elif s[offset : offset + 1] == b'"' and parts[-1][-1:] == b'\\':
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
248 parts[-1] = parts[-1][:-1] + s[offset : offset + 1]
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
249 return _parse_plain, parts, offset + 1
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
250 parts[-1] += s[offset : offset + 1]
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
251 return _parse_plain, parts, offset + 1
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
252
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
253 def _parse_quote(parts, s, offset):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
254 if offset < len(s) and s[offset : offset + 1] == b'"': # ""
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
255 parts.append(b'')
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
256 offset += 1
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
257 while offset < len(s) and (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
258 s[offset : offset + 1].isspace()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
259 or s[offset : offset + 1] == b','
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
260 ):
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
261 offset += 1
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
262 return _parse_plain, parts, offset
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
263
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
264 while offset < len(s) and s[offset : offset + 1] != b'"':
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
265 if (
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
266 s[offset : offset + 1] == b'\\'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
267 and offset + 1 < len(s)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
268 and s[offset + 1 : offset + 2] == b'"'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
269 ):
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
270 offset += 1
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
271 parts[-1] += b'"'
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
272 else:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
273 parts[-1] += s[offset : offset + 1]
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
274 offset += 1
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
275
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
276 if offset >= len(s):
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
277 real_parts = _configlist(parts[-1])
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
278 if not real_parts:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
279 parts[-1] = b'"'
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
280 else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
281 real_parts[0] = b'"' + real_parts[0]
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
282 parts = parts[:-1]
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
283 parts.extend(real_parts)
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
284 return None, parts, offset
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
285
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
286 offset += 1
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
287 while offset < len(s) and s[offset : offset + 1] in [b' ', b',']:
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
288 offset += 1
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
289
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
290 if offset < len(s):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
291 if offset + 1 == len(s) and s[offset : offset + 1] == b'"':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
292 parts[-1] += b'"'
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
293 offset += 1
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
294 else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
295 parts.append(b'')
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
296 else:
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
297 return None, parts, offset
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
298
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
299 return _parse_plain, parts, offset
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
300
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
301 def _configlist(s):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
302 s = s.rstrip(b' ,')
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
303 if not s:
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
304 return []
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
305 parser, parts, offset = _parse_plain, [b''], 0
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
306 while parser:
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
307 parser, parts, offset = parser(parts, s, offset)
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
308 return parts
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
309
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
310 if value is not None and isinstance(value, bytes):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
311 result = _configlist(value.lstrip(b' ,\n'))
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
312 else:
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
313 result = value
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
314 return result or []