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>
diff --git a/main.py b/main.py
index 9c545b3..f80bd45 100755
--- a/main.py
+++ b/main.py
@@ -22,6 +22,7 @@
del sys.argv[-1]
del magic
+import netrc
import optparse
import os
import re
@@ -254,6 +255,17 @@
def init_http():
handlers = [_UserAgentHandler()]
+ mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
+ try:
+ n = netrc.netrc()
+ for host in n.hosts:
+ p = n.hosts[host]
+ mgr.add_password(None, 'http://%s/' % host, p[0], p[2])
+ mgr.add_password(None, 'https://%s/' % host, p[0], p[2])
+ except netrc.NetrcParseError:
+ pass
+ handlers.append(urllib2.HTTPBasicAuthHandler(mgr))
+
if 'http_proxy' in os.environ:
url = os.environ['http_proxy']
handlers.append(urllib2.ProxyHandler({'http': url, 'https': url}))