changeset 5798:86f5d8f608b7

fetch: hide authentication details
author Bryan O'Sullivan <bos@serpentine.com>
date Fri, 04 Jan 2008 11:58:27 -0800
parents 7b7f03c7dfa5
children d852151fb8d4
files hgext/fetch.py mercurial/util.py tests/test-fetch tests/test-fetch.out
diffstat 4 files changed, 53 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/fetch.py	Fri Jan 04 11:52:24 2008 -0800
+++ b/hgext/fetch.py	Fri Jan 04 11:58:27 2008 -0800
@@ -43,7 +43,8 @@
         if not err:
             mod, add, rem = repo.status()[:3]
             message = (cmdutil.logmessage(opts) or
-                       (_('Automated merge with %s') % other.url()))
+                       (_('Automated merge with %s') %
+                        util.removeauth(other.url())))
             n = repo.commit(mod + add + rem, message,
                             opts['user'], opts['date'],
                             force_editor=opts.get('force_editor'))
@@ -54,7 +55,8 @@
         cmdutil.setremoteconfig(ui, opts)
 
         other = hg.repository(ui, ui.expandpath(source))
-        ui.status(_('pulling from %s\n') % ui.expandpath(source))
+        ui.status(_('pulling from %s\n') %
+                  util.hidepassword(ui.expandpath(source)))
         revs = None
         if opts['rev'] and not other.local():
             raise util.Abort(_("fetch -r doesn't work for remote repositories yet"))
--- a/mercurial/util.py	Fri Jan 04 11:52:24 2008 -0800
+++ b/mercurial/util.py	Fri Jan 04 11:58:27 2008 -0800
@@ -1707,32 +1707,14 @@
     # Avoid double backslash in Windows path repr()
     return repr(s).replace('\\\\', '\\')
 
-def hidepassword(url):
-    '''replaces the password in the url string by three asterisks (***)
+def hidepassword(url, user=True, password=True):
+    '''hide user credential in a url string'''
+    scheme, netloc, path, params, query, fragment = urlparse.urlparse(url)
+    netloc = re.sub('([^:]*):([^@]*)@(.*)', r'\1:***@\3', netloc)
+    return urlparse.urlunparse((scheme, netloc, path, params, query, fragment))
 
-    >>> hidepassword('http://www.example.com/some/path#fragment')
-    'http://www.example.com/some/path#fragment'
-    >>> hidepassword('http://me@www.example.com/some/path#fragment')
-    'http://me@www.example.com/some/path#fragment'
-    >>> hidepassword('http://me:simplepw@www.example.com/path#frag')
-    'http://me:***@www.example.com/path#frag'
-    >>> hidepassword('http://me:complex:pw@www.example.com/path#frag')
-    'http://me:***@www.example.com/path#frag'
-    >>> hidepassword('/path/to/repo')
-    '/path/to/repo'
-    >>> hidepassword('relative/path/to/repo')
-    'relative/path/to/repo'
-    >>> hidepassword('c:\\\\path\\\\to\\\\repo')
-    'c:\\\\path\\\\to\\\\repo'
-    >>> hidepassword('c:/path/to/repo')
-    'c:/path/to/repo'
-    >>> hidepassword('bundle://path/to/bundle')
-    'bundle://path/to/bundle'
-    '''
-    url_parts = list(urlparse.urlparse(url))
-    host_with_pw_pattern = re.compile('^([^:]*):([^@]*)@(.*)$')
-    if host_with_pw_pattern.match(url_parts[1]):
-        url_parts[1] = re.sub(host_with_pw_pattern, r'\1:***@\3',
-            url_parts[1])
-    return urlparse.urlunparse(url_parts)
-
+def removeauth(url):
+    '''remove all authentication information from a url string'''
+    scheme, netloc, path, params, query, fragment = urlparse.urlparse(url)
+    netloc = netloc[netloc.find('@')+1:]
+    return urlparse.urlunparse((scheme, netloc, path, params, query, fragment))
--- a/tests/test-fetch	Fri Jan 04 11:52:24 2008 -0800
+++ b/tests/test-fetch	Fri Jan 04 11:58:27 2008 -0800
@@ -20,5 +20,20 @@
 
 echo c > c/c
 hg --cwd c commit -d '3 0' -Amc
+
+hg clone c d
+hg clone c e
+
 hg --cwd c fetch -d '4 0' -m 'automated merge' ../a
 ls c
+
+hg --cwd a serve -a localhost -p $HGPORT -d --pid-file=hg.pid
+cat a/hg.pid >> "$DAEMON_PIDS"
+
+echo '% fetch over http, no auth'
+hg --cwd d fetch -d '5 0' http://localhost:$HGPORT/
+hg --cwd d tip --template '{desc}\n'
+
+echo '% fetch over http with auth (should be hidden in desc)'
+hg --cwd e fetch -d '5 0' http://user:password@localhost:$HGPORT/
+hg --cwd e tip --template '{desc}\n'
--- a/tests/test-fetch.out	Fri Jan 04 11:52:24 2008 -0800
+++ b/tests/test-fetch.out	Fri Jan 04 11:58:27 2008 -0800
@@ -13,6 +13,8 @@
 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
 1:97d72e5f12c7
 adding c
+2 files updated, 0 files merged, 0 files removed, 0 files unresolved
+2 files updated, 0 files merged, 0 files removed, 0 files unresolved
 pulling from ../a
 searching for changes
 adding changesets
@@ -25,3 +27,25 @@
 a
 b
 c
+% fetch over http, no auth
+pulling from http://localhost:20059/
+searching for changes
+adding changesets
+adding manifests
+adding file changes
+added 1 changesets with 1 changes to 1 files (+1 heads)
+merging with new head 2:97d72e5f12c7
+1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+new changeset 3:0b6439e938f9 merges remote changes with local
+Automated merge with http://localhost:20059/
+% fetch over http with auth (should be hidden in desc)
+pulling from http://user:***@localhost:20059/
+searching for changes
+adding changesets
+adding manifests
+adding file changes
+added 1 changesets with 1 changes to 1 files (+1 heads)
+merging with new head 2:97d72e5f12c7
+1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+new changeset 3:0b6439e938f9 merges remote changes with local
+Automated merge with http://localhost:20059/