diff options
| author | Mike Frysinger <vapier@google.com> | 2019-07-10 17:10:07 -0400 |
|---|---|---|
| committer | Mike Frysinger <vapier@google.com> | 2019-10-01 05:39:27 +0000 |
| commit | 369814b4a77adcc78b2549ad728e0d69175f08e8 (patch) | |
| tree | eccd05989cd69c424dce146b2ea76d0a7069b423 /git_command.py | |
| parent | e37aa5f331aa39776d5db1a1f816b66496f60e0c (diff) | |
| download | git-repo-369814b4a77adcc78b2549ad728e0d69175f08e8.tar.gz | |
move UserAgent to git_command for wider user
We can't import the main module, so move the UserAgent helper out of
it and into the git_command module so it can be used in more places.
Bug: https://crbug.com/gerrit/11144
Change-Id: I8093c8a20bd1dc7d612d0e2a85180341817c0d86
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/231057
Tested-by: Mike Frysinger <vapier@google.com>
Reviewed-by: David Pursehouse <dpursehouse@collab.net>
Diffstat (limited to 'git_command.py')
| -rw-r--r-- | git_command.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/git_command.py b/git_command.py index 67423035..32dcde09 100644 --- a/git_command.py +++ b/git_command.py | |||
| @@ -98,6 +98,52 @@ class _GitCall(object): | |||
| 98 | return fun | 98 | return fun |
| 99 | git = _GitCall() | 99 | git = _GitCall() |
| 100 | 100 | ||
| 101 | |||
| 102 | _user_agent = None | ||
| 103 | |||
| 104 | def RepoUserAgent(): | ||
| 105 | """Return a User-Agent string suitable for HTTP-like services. | ||
| 106 | |||
| 107 | We follow the style as documented here: | ||
| 108 | https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent | ||
| 109 | """ | ||
| 110 | global _user_agent | ||
| 111 | |||
| 112 | if _user_agent is None: | ||
| 113 | py_version = sys.version_info | ||
| 114 | |||
| 115 | os_name = sys.platform | ||
| 116 | if os_name == 'linux2': | ||
| 117 | os_name = 'Linux' | ||
| 118 | elif os_name == 'win32': | ||
| 119 | os_name = 'Win32' | ||
| 120 | elif os_name == 'cygwin': | ||
| 121 | os_name = 'Cygwin' | ||
| 122 | elif os_name == 'darwin': | ||
| 123 | os_name = 'Darwin' | ||
| 124 | |||
| 125 | p = GitCommand( | ||
| 126 | None, ['describe', 'HEAD'], | ||
| 127 | cwd=os.path.dirname(__file__), | ||
| 128 | capture_stdout=True) | ||
| 129 | if p.Wait() == 0: | ||
| 130 | repo_version = p.stdout | ||
| 131 | if repo_version and repo_version[-1] == '\n': | ||
| 132 | repo_version = repo_version[0:-1] | ||
| 133 | if repo_version and repo_version[0] == 'v': | ||
| 134 | repo_version = repo_version[1:] | ||
| 135 | else: | ||
| 136 | repo_version = 'unknown' | ||
| 137 | |||
| 138 | _user_agent = 'git-repo/%s (%s) git/%s Python/%d.%d.%d' % ( | ||
| 139 | repo_version, | ||
| 140 | os_name, | ||
| 141 | git.version_tuple().full, | ||
| 142 | py_version.major, py_version.minor, py_version.micro) | ||
| 143 | |||
| 144 | return _user_agent | ||
| 145 | |||
| 146 | |||
| 101 | def git_require(min_version, fail=False, msg=''): | 147 | def git_require(min_version, fail=False, msg=''): |
| 102 | git_version = git.version_tuple() | 148 | git_version = git.version_tuple() |
| 103 | if min_version <= git_version: | 149 | if min_version <= git_version: |
