summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorShawn O. Pearce <sop@google.com>2011-09-19 10:04:23 -0700
committerShawn O. Pearce <sop@google.com>2011-09-19 14:52:32 -0700
commitbd0312a4841e9488cf43ae4afb3b58d44eebbbb1 (patch)
treebb0a60c3068ceb640d9618cb8d7afa716b9609c0
parent334851e4b6390f4c78e463b977003f1d967c88ed (diff)
downloadgit-repo-bd0312a4841e9488cf43ae4afb3b58d44eebbbb1.tar.gz
Support ~/.netrc for HTTP Basic authentication
If repo tries to access a URL over HTTP and the user needs to authenticate, offer a match from ~/.netrc. This matches behavior with the Git command line client. Change-Id: I803f3c5d562177ea0330941350cff3cc1e1bef08 Signed-off-by: Shawn O. Pearce <sop@google.com>
-rwxr-xr-xmain.py12
1 files changed, 12 insertions, 0 deletions
diff --git a/main.py b/main.py
index 9c545b31..f80bd45e 100755
--- a/main.py
+++ b/main.py
@@ -22,6 +22,7 @@ if __name__ == '__main__':
22 del sys.argv[-1] 22 del sys.argv[-1]
23del magic 23del magic
24 24
25import netrc
25import optparse 26import optparse
26import os 27import os
27import re 28import re
@@ -254,6 +255,17 @@ class _UserAgentHandler(urllib2.BaseHandler):
254def init_http(): 255def init_http():
255 handlers = [_UserAgentHandler()] 256 handlers = [_UserAgentHandler()]
256 257
258 mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
259 try:
260 n = netrc.netrc()
261 for host in n.hosts:
262 p = n.hosts[host]
263 mgr.add_password(None, 'http://%s/' % host, p[0], p[2])
264 mgr.add_password(None, 'https://%s/' % host, p[0], p[2])
265 except netrc.NetrcParseError:
266 pass
267 handlers.append(urllib2.HTTPBasicAuthHandler(mgr))
268
257 if 'http_proxy' in os.environ: 269 if 'http_proxy' in os.environ:
258 url = os.environ['http_proxy'] 270 url = os.environ['http_proxy']
259 handlers.append(urllib2.ProxyHandler({'http': url, 'https': url})) 271 handlers.append(urllib2.ProxyHandler({'http': url, 'https': url}))