annotate tests/printenv.py @ 39548:7ce9dea3a14a

localrepo: move repo creation logic out of localrepository.__init__ (API) It has long bothered me that local repository creation is handled as part of localrepository.__init__. Upcoming changes I want to make around how repositories are initialized and instantiated will make the continued existence of repository creation code in localrepository.__init__ even more awkward. localrepository instances are almost never constructed directly: instead, callers are supposed to go through hg.repository() to obtain a handle on a repository. And hg.repository() calls localrepo.instance() to return a new repo instance. This commit teaches localrepo.instance() to handle the create=True logic. Most of the code for repo construction has been moved to a standalone function. This allows extensions to monkeypatch the function to further customize freshly-created repositories. A few calls to localrepo.localrepository.__init__ that were passing create=True were converted to call localrepo.instance(). .. api:: local repo creation moved out of constructor ``localrepo.localrepository.__init__`` no longer accepts a ``create`` argument to create a new repository. New repository creation is now performed as part of ``localrepo.instance()`` and the bulk of the work is performed by ``localrepo.createrepository()``. Differential Revision: https://phab.mercurial-scm.org/D4534
author Gregory Szorc <gregory.szorc@gmail.com>
date Tue, 11 Sep 2018 13:46:59 -0700
parents 42f3a277c8dc
children 3c5aaea9638f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
25477
a372f7b4463b tests: make printenv executable
Matt Mackall <mpm@selenic.com>
parents: 17018
diff changeset
1 #!/usr/bin/env python
a372f7b4463b tests: make printenv executable
Matt Mackall <mpm@selenic.com>
parents: 17018
diff changeset
2 #
4285
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
3 # simple script to be used in hooks
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
4 #
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
5 # put something like this in the repo .hg/hgrc:
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
6 #
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
7 # [hooks]
17018
e7fdfc702d9f tests: consistently use printenv.py the same MSYS/Windows-compatible way
Mads Kiilerich <mads@kiilerich.com>
parents: 16982
diff changeset
8 # changegroup = python "$TESTDIR/printenv.py" <hookname> [exit] [output]
4285
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
9 #
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
10 # - <hookname> is a mandatory argument (e.g. "changegroup")
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
11 # - [exit] is the exit code of the hook (default: 0)
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
12 # - [output] is the name of the output file (default: use sys.stdout)
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
13 # the file will be opened in append mode.
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
14 #
28944
036787c10b16 py3: use absolute_import in printenv.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 25477
diff changeset
15 from __future__ import absolute_import
4285
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
16 import os
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
17 import sys
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
18
7080
a6477aa893b8 tests: Windows compatibility fixes
Patrick Mezard <pmezard@gmail.com>
parents: 4659
diff changeset
19 try:
a6477aa893b8 tests: Windows compatibility fixes
Patrick Mezard <pmezard@gmail.com>
parents: 4659
diff changeset
20 import msvcrt
a6477aa893b8 tests: Windows compatibility fixes
Patrick Mezard <pmezard@gmail.com>
parents: 4659
diff changeset
21 msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
a6477aa893b8 tests: Windows compatibility fixes
Patrick Mezard <pmezard@gmail.com>
parents: 4659
diff changeset
22 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
7186
f77c8d8331ca clean up trailing spaces, leading spaces in C
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7080
diff changeset
23 msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
7080
a6477aa893b8 tests: Windows compatibility fixes
Patrick Mezard <pmezard@gmail.com>
parents: 4659
diff changeset
24 except ImportError:
a6477aa893b8 tests: Windows compatibility fixes
Patrick Mezard <pmezard@gmail.com>
parents: 4659
diff changeset
25 pass
a6477aa893b8 tests: Windows compatibility fixes
Patrick Mezard <pmezard@gmail.com>
parents: 4659
diff changeset
26
4285
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
27 exitcode = 0
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
28 out = sys.stdout
38296
42f3a277c8dc tests: fix printenv script on Python 3
Augie Fackler <augie@google.com>
parents: 38111
diff changeset
29 out = getattr(out, 'buffer', out)
4285
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
30
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
31 name = sys.argv[1]
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
32 if len(sys.argv) > 2:
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
33 exitcode = int(sys.argv[2])
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
34 if len(sys.argv) > 3:
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
35 out = open(sys.argv[3], "ab")
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
36
4643
a39cec1d5cb8 printenv: filter empty environment variables for portability.
Patrick Mezard <pmezard@gmail.com>
parents: 4285
diff changeset
37 # variables with empty values may not exist on all platforms, filter
a39cec1d5cb8 printenv: filter empty environment variables for portability.
Patrick Mezard <pmezard@gmail.com>
parents: 4285
diff changeset
38 # them now for portability sake.
36254
84a6e39bc723 printenv: port to python3
Augie Fackler <augie@google.com>
parents: 28944
diff changeset
39 env = [(k, v) for k, v in os.environ.items()
4643
a39cec1d5cb8 printenv: filter empty environment variables for portability.
Patrick Mezard <pmezard@gmail.com>
parents: 4285
diff changeset
40 if k.startswith("HG_") and v]
4285
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
41 env.sort()
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
42
38111
bacbe829c2bf py3: use bytes in tests/printenv.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36254
diff changeset
43 out.write(b"%s hook: " % name.encode('ascii'))
16982
9c892c830a72 tests/printenv.py: eliminate trailing spaces on output
Adrian Buehlmann <adrian@cadifra.com>
parents: 16963
diff changeset
44 if os.name == 'nt':
9c892c830a72 tests/printenv.py: eliminate trailing spaces on output
Adrian Buehlmann <adrian@cadifra.com>
parents: 16963
diff changeset
45 filter = lambda x: x.replace('\\', '/')
9c892c830a72 tests/printenv.py: eliminate trailing spaces on output
Adrian Buehlmann <adrian@cadifra.com>
parents: 16963
diff changeset
46 else:
9c892c830a72 tests/printenv.py: eliminate trailing spaces on output
Adrian Buehlmann <adrian@cadifra.com>
parents: 16963
diff changeset
47 filter = lambda x: x
38111
bacbe829c2bf py3: use bytes in tests/printenv.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36254
diff changeset
48 vars = [b"%s=%s" % (k.encode('ascii'), filter(v).encode('ascii'))
bacbe829c2bf py3: use bytes in tests/printenv.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36254
diff changeset
49 for k, v in env]
bacbe829c2bf py3: use bytes in tests/printenv.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36254
diff changeset
50 out.write(b" ".join(vars))
bacbe829c2bf py3: use bytes in tests/printenv.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36254
diff changeset
51 out.write(b"\n")
4285
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
52 out.close()
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
53
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
54 sys.exit(exitcode)