diff options
author | Mike Frysinger <vapier@google.com> | 2019-07-10 17:13:46 -0400 |
---|---|---|
committer | Mike Frysinger <vapier@google.com> | 2019-10-01 05:47:17 +0000 |
commit | 2f0951b216489ecbd0a28935ca589fb0067a8381 (patch) | |
tree | 8554a4538647a69d7a1a47d243d8a9c1a090f833 | |
parent | 72ab852ca503d1c8e32e493edc3fa3e1157e930b (diff) | |
download | git-repo-2f0951b216489ecbd0a28935ca589fb0067a8381.tar.gz |
git_command: set GIT_HTTP_USER_AGENT on all requests
We've been setting the User-Agent header when making connections
from repo itself, but not when running git (as the latter will set
up User-Agent itself). Our Gerrit/Git admins say it'll be helpful
if we pass through the repo version settings even when running git.
We currently set GIT_HTTP_USER_AGENT and not GIT_USER_AGENT as it's
unclear if the extended form works over all protocols. We can wait
for a user request.
Bug: https://crbug.com/gerrit/11144
Change-Id: I21d293f49534058dbc23225152451df26c5b7bef
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/239233
Tested-by: Mike Frysinger <vapier@google.com>
Reviewed-by: David Pursehouse <dpursehouse@collab.net>
-rw-r--r-- | git_command.py | 13 | ||||
-rw-r--r-- | tests/test_git_command.py | 8 |
2 files changed, 21 insertions, 0 deletions
diff --git a/git_command.py b/git_command.py index a4081f45..dc542c36 100644 --- a/git_command.py +++ b/git_command.py | |||
@@ -134,6 +134,7 @@ class UserAgent(object): | |||
134 | 134 | ||
135 | _os = None | 135 | _os = None |
136 | _repo_ua = None | 136 | _repo_ua = None |
137 | _git_ua = None | ||
137 | 138 | ||
138 | @property | 139 | @property |
139 | def os(self): | 140 | def os(self): |
@@ -165,6 +166,17 @@ class UserAgent(object): | |||
165 | 166 | ||
166 | return self._repo_ua | 167 | return self._repo_ua |
167 | 168 | ||
169 | @property | ||
170 | def git(self): | ||
171 | """The UA when running git.""" | ||
172 | if self._git_ua is None: | ||
173 | self._git_ua = 'git/%s (%s) git-repo/%s' % ( | ||
174 | git.version_tuple().full, | ||
175 | self.os, | ||
176 | RepoSourceVersion()) | ||
177 | |||
178 | return self._git_ua | ||
179 | |||
168 | user_agent = UserAgent() | 180 | user_agent = UserAgent() |
169 | 181 | ||
170 | def git_require(min_version, fail=False, msg=''): | 182 | def git_require(min_version, fail=False, msg=''): |
@@ -214,6 +226,7 @@ class GitCommand(object): | |||
214 | if 'GIT_ALLOW_PROTOCOL' not in env: | 226 | if 'GIT_ALLOW_PROTOCOL' not in env: |
215 | _setenv(env, 'GIT_ALLOW_PROTOCOL', | 227 | _setenv(env, 'GIT_ALLOW_PROTOCOL', |
216 | 'file:git:http:https:ssh:persistent-http:persistent-https:sso:rpc') | 228 | 'file:git:http:https:ssh:persistent-http:persistent-https:sso:rpc') |
229 | _setenv(env, 'GIT_HTTP_USER_AGENT', user_agent.git) | ||
217 | 230 | ||
218 | if project: | 231 | if project: |
219 | if not cwd: | 232 | if not cwd: |
diff --git a/tests/test_git_command.py b/tests/test_git_command.py index 5ceb0b33..51171a32 100644 --- a/tests/test_git_command.py +++ b/tests/test_git_command.py | |||
@@ -68,3 +68,11 @@ class UserAgentUnitTest(unittest.TestCase): | |||
68 | # the general form. | 68 | # the general form. |
69 | m = re.match(r'^git-repo/[^ ]+ ([^ ]+) git/[^ ]+ Python/[0-9.]+', ua) | 69 | m = re.match(r'^git-repo/[^ ]+ ([^ ]+) git/[^ ]+ Python/[0-9.]+', ua) |
70 | self.assertIsNotNone(m) | 70 | self.assertIsNotNone(m) |
71 | |||
72 | def test_smoke_git(self): | ||
73 | """Make sure git UA returns something useful.""" | ||
74 | ua = git_command.user_agent.git | ||
75 | # We can't dive too deep because of OS/tool differences, but we can check | ||
76 | # the general form. | ||
77 | m = re.match(r'^git/[^ ]+ ([^ ]+) git-repo/[^ ]+', ua) | ||
78 | self.assertIsNotNone(m) | ||