summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xrepo47
-rw-r--r--tests/test_wrapper.py50
2 files changed, 84 insertions, 13 deletions
diff --git a/repo b/repo
index 7bf48023..6ecf3921 100755
--- a/repo
+++ b/repo
@@ -10,6 +10,7 @@ copy of repo in the checkout.
10 10
11from __future__ import print_function 11from __future__ import print_function
12 12
13import datetime
13import os 14import os
14import platform 15import platform
15import subprocess 16import subprocess
@@ -478,6 +479,39 @@ def _CheckGitVersion():
478 raise CloneFailure() 479 raise CloneFailure()
479 480
480 481
482def 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
504def _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
481def NeedSetupGnuPG(): 515def 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):
901def main(orig_args): 929def 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 @@
19from __future__ import print_function 19from __future__ import print_function
20 20
21import os 21import os
22import re
22import unittest 23import unittest
23 24
25from pyversion import is_python3
24import wrapper 26import 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
33class RepoWrapperUnitTest(unittest.TestCase): 35class 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
47class 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
91class 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
83if __name__ == '__main__': 123if __name__ == '__main__':
84 unittest.main() 124 unittest.main()