diff options
| author | Mike Frysinger <vapier@google.com> | 2020-02-11 02:10:28 -0500 |
|---|---|---|
| committer | Mike Frysinger <vapier@google.com> | 2020-02-12 19:57:42 +0000 |
| commit | 8409410aa294cad68bf93679726e0a4465e1fe2b (patch) | |
| tree | dc7bbeef44441043ecb43c6ca4c114ac8dc3c2f3 | |
| parent | dc63181fcd3c34340e3acb5c72ae80539b5c7282 (diff) | |
| download | git-repo-8409410aa294cad68bf93679726e0a4465e1fe2b.tar.gz | |
repo: export GIT_TRACE2_PARENT_SID
This helps with people tracing repo/git execution. We use a similar
format to git, but a little simpler since we always initialize the
env var setting, and we want to avoid too much overhead.
Bug: https://crbug.com/gerrit/12314
Change-Id: I75675b6cc4c6f7c4f5e09f54128eba9456364d04
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/254331
Reviewed-by: Josh Steadmon <steadmon@google.com>
Reviewed-by: Mike Frysinger <vapier@google.com>
Tested-by: Mike Frysinger <vapier@google.com>
| -rwxr-xr-x | repo | 47 | ||||
| -rw-r--r-- | tests/test_wrapper.py | 50 |
2 files changed, 84 insertions, 13 deletions
| @@ -10,6 +10,7 @@ copy of repo in the checkout. | |||
| 10 | 10 | ||
| 11 | from __future__ import print_function | 11 | from __future__ import print_function |
| 12 | 12 | ||
| 13 | import datetime | ||
| 13 | import os | 14 | import os |
| 14 | import platform | 15 | import platform |
| 15 | import subprocess | 16 | import subprocess |
| @@ -478,6 +479,39 @@ def _CheckGitVersion(): | |||
| 478 | raise CloneFailure() | 479 | raise CloneFailure() |
| 479 | 480 | ||
| 480 | 481 | ||
| 482 | def SetGitTrace2ParentSid(env=None): | ||
| 483 | """Set up GIT_TRACE2_PARENT_SID for git tracing.""" | ||
| 484 | # We roughly follow the format git itself uses in trace2/tr2_sid.c. | ||
| 485 | # (1) Be unique (2) be valid filename (3) be fixed length. | ||
| 486 | # | ||
| 487 | # Since we always export this variable, we try to avoid more expensive calls. | ||
| 488 | # e.g. We don't attempt hostname lookups or hashing the results. | ||
| 489 | if env is None: | ||
| 490 | env = os.environ | ||
| 491 | |||
| 492 | KEY = 'GIT_TRACE2_PARENT_SID' | ||
| 493 | |||
| 494 | now = datetime.datetime.utcnow() | ||
| 495 | value = 'repo-%s-P%08x' % (now.strftime('%Y%m%dT%H%M%SZ'), os.getpid()) | ||
| 496 | |||
| 497 | # If it's already set, then append ourselves. | ||
| 498 | if KEY in env: | ||
| 499 | value = env[KEY] + '/' + value | ||
| 500 | |||
| 501 | _setenv(KEY, value, env=env) | ||
| 502 | |||
| 503 | |||
| 504 | def _setenv(key, value, env=None): | ||
| 505 | """Set |key| in the OS environment |env| to |value|.""" | ||
| 506 | if env is None: | ||
| 507 | env = os.environ | ||
| 508 | # Environment handling across systems is messy. | ||
| 509 | try: | ||
| 510 | env[key] = value | ||
| 511 | except UnicodeEncodeError: | ||
| 512 | env[key] = value.encode() | ||
| 513 | |||
| 514 | |||
| 481 | def NeedSetupGnuPG(): | 515 | def NeedSetupGnuPG(): |
| 482 | if not os.path.isdir(home_dot_repo): | 516 | if not os.path.isdir(home_dot_repo): |
| 483 | return True | 517 | return True |
| @@ -514,10 +548,7 @@ def SetupGnuPG(quiet): | |||
| 514 | sys.exit(1) | 548 | sys.exit(1) |
| 515 | 549 | ||
| 516 | env = os.environ.copy() | 550 | env = os.environ.copy() |
| 517 | try: | 551 | _setenv('GNUPGHOME', gpg_dir, env) |
| 518 | env['GNUPGHOME'] = gpg_dir | ||
| 519 | except UnicodeEncodeError: | ||
| 520 | env['GNUPGHOME'] = gpg_dir.encode() | ||
| 521 | 552 | ||
| 522 | cmd = ['gpg', '--import'] | 553 | cmd = ['gpg', '--import'] |
| 523 | try: | 554 | try: |
| @@ -723,10 +754,7 @@ def _Verify(cwd, branch, quiet): | |||
| 723 | print(file=sys.stderr) | 754 | print(file=sys.stderr) |
| 724 | 755 | ||
| 725 | env = os.environ.copy() | 756 | env = os.environ.copy() |
| 726 | try: | 757 | _setenv('GNUPGHOME', gpg_dir, env) |
| 727 | env['GNUPGHOME'] = gpg_dir | ||
| 728 | except UnicodeEncodeError: | ||
| 729 | env['GNUPGHOME'] = gpg_dir.encode() | ||
| 730 | 758 | ||
| 731 | cmd = [GIT, 'tag', '-v', cur] | 759 | cmd = [GIT, 'tag', '-v', cur] |
| 732 | proc = subprocess.Popen(cmd, | 760 | proc = subprocess.Popen(cmd, |
| @@ -901,6 +929,9 @@ def _SetDefaultsTo(gitdir): | |||
| 901 | def main(orig_args): | 929 | def main(orig_args): |
| 902 | cmd, opt, args = _ParseArguments(orig_args) | 930 | cmd, opt, args = _ParseArguments(orig_args) |
| 903 | 931 | ||
| 932 | # We run this early as we run some git commands ourselves. | ||
| 933 | SetGitTrace2ParentSid() | ||
| 934 | |||
| 904 | repo_main, rel_repo_dir = None, None | 935 | repo_main, rel_repo_dir = None, None |
| 905 | # Don't use the local repo copy, make sure to switch to the gitc client first. | 936 | # Don't use the local repo copy, make sure to switch to the gitc client first. |
| 906 | if cmd != 'gitc-init': | 937 | if cmd != 'gitc-init': |
diff --git a/tests/test_wrapper.py b/tests/test_wrapper.py index 38def512..e574946b 100644 --- a/tests/test_wrapper.py +++ b/tests/test_wrapper.py | |||
| @@ -19,8 +19,10 @@ | |||
| 19 | from __future__ import print_function | 19 | from __future__ import print_function |
| 20 | 20 | ||
| 21 | import os | 21 | import os |
| 22 | import re | ||
| 22 | import unittest | 23 | import unittest |
| 23 | 24 | ||
| 25 | from pyversion import is_python3 | ||
| 24 | import wrapper | 26 | import wrapper |
| 25 | 27 | ||
| 26 | 28 | ||
| @@ -30,16 +32,22 @@ def fixture(*paths): | |||
| 30 | return os.path.join(os.path.dirname(__file__), 'fixtures', *paths) | 32 | return os.path.join(os.path.dirname(__file__), 'fixtures', *paths) |
| 31 | 33 | ||
| 32 | 34 | ||
| 33 | class RepoWrapperUnitTest(unittest.TestCase): | 35 | class RepoWrapperTestCase(unittest.TestCase): |
| 34 | """Tests helper functions in the repo wrapper | 36 | """TestCase for the wrapper module.""" |
| 35 | """ | ||
| 36 | 37 | ||
| 37 | def setUp(self): | 38 | def setUp(self): |
| 38 | """Load the wrapper module every time | 39 | """Load the wrapper module every time.""" |
| 39 | """ | ||
| 40 | wrapper._wrapper_module = None | 40 | wrapper._wrapper_module = None |
| 41 | self.wrapper = wrapper.Wrapper() | 41 | self.wrapper = wrapper.Wrapper() |
| 42 | 42 | ||
| 43 | if not is_python3(): | ||
| 44 | self.assertRegex = self.assertRegexpMatches | ||
| 45 | |||
| 46 | |||
| 47 | class RepoWrapperUnitTest(RepoWrapperTestCase): | ||
| 48 | """Tests helper functions in the repo wrapper | ||
| 49 | """ | ||
| 50 | |||
| 43 | def test_get_gitc_manifest_dir_no_gitc(self): | 51 | def test_get_gitc_manifest_dir_no_gitc(self): |
| 44 | """ | 52 | """ |
| 45 | Test reading a missing gitc config file | 53 | Test reading a missing gitc config file |
| @@ -80,5 +88,37 @@ class RepoWrapperUnitTest(unittest.TestCase): | |||
| 80 | self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/'), None) | 88 | self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/'), None) |
| 81 | 89 | ||
| 82 | 90 | ||
| 91 | class SetGitTrace2ParentSid(RepoWrapperTestCase): | ||
| 92 | """Check SetGitTrace2ParentSid behavior.""" | ||
| 93 | |||
| 94 | KEY = 'GIT_TRACE2_PARENT_SID' | ||
| 95 | VALID_FORMAT = re.compile(r'^repo-[0-9]{8}T[0-9]{6}Z-P[0-9a-f]{8}$') | ||
| 96 | |||
| 97 | def test_first_set(self): | ||
| 98 | """Test env var not yet set.""" | ||
| 99 | env = {} | ||
| 100 | self.wrapper.SetGitTrace2ParentSid(env) | ||
| 101 | self.assertIn(self.KEY, env) | ||
| 102 | value = env[self.KEY] | ||
| 103 | self.assertRegex(value, self.VALID_FORMAT) | ||
| 104 | |||
| 105 | def test_append(self): | ||
| 106 | """Test env var is appended.""" | ||
| 107 | env = {self.KEY: 'pfx'} | ||
| 108 | self.wrapper.SetGitTrace2ParentSid(env) | ||
| 109 | self.assertIn(self.KEY, env) | ||
| 110 | value = env[self.KEY] | ||
| 111 | self.assertTrue(value.startswith('pfx/')) | ||
| 112 | self.assertRegex(value[4:], self.VALID_FORMAT) | ||
| 113 | |||
| 114 | def test_global_context(self): | ||
| 115 | """Check os.environ gets updated by default.""" | ||
| 116 | os.environ.pop(self.KEY, None) | ||
| 117 | self.wrapper.SetGitTrace2ParentSid() | ||
| 118 | self.assertIn(self.KEY, os.environ) | ||
| 119 | value = os.environ[self.KEY] | ||
| 120 | self.assertRegex(value, self.VALID_FORMAT) | ||
| 121 | |||
| 122 | |||
| 83 | if __name__ == '__main__': | 123 | if __name__ == '__main__': |
| 84 | unittest.main() | 124 | unittest.main() |
