[PATCH] Hide HTTP password on console

Brad Ediger brad at bradediger.com
Tue Oct 2 14:12:43 CDT 2007


This patch hides the password on the console when dealing with HTTP repo
URIs like http://user:pass@hg.example.com/. All command-line output is
now in the form http://user@hg.example.com/.

I posted the patch a few weeks ago, but made a few changes since based
on some testing and input (changed incoming / outgoing to use the
printable path, and unhid the username).
-------------- next part --------------
# HG changeset patch
# User Brad Ediger <brad.ediger at madriska.com>
# Date 1191352044 18000
# Node ID 46102a6ad00244c39c518be0ecdb9c39b33879d6
# Parent  d0c48891dd4a7334548e54cfb33d39e26a7b24d7
[mq]: hide-http-password-on-console

diff -r d0c48891dd4a -r 46102a6ad002 mercurial/bundlerepo.py
--- a/mercurial/bundlerepo.py	Thu Sep 27 23:59:18 2007 -0500
+++ b/mercurial/bundlerepo.py	Tue Oct 02 14:07:24 2007 -0500
@@ -151,9 +151,10 @@ class bundlefilelog(bundlerevlog, filelo
                               linkmapper)
 
 class bundlerepository(localrepo.localrepository):
-    def __init__(self, ui, path, bundlename):
+    def __init__(self, ui, path, bundlename, origpath=None):
         localrepo.localrepository.__init__(self, ui, path)
-
+        
+        self.origpath = origpath
         self._url = 'bundle:' + bundlename
         if path: self._url += '+' + path
 
@@ -217,6 +218,9 @@ class bundlerepository(localrepo.localre
 
     def url(self):
         return self._url
+        
+    def printable_path(self):
+        return self.origpath
 
     def dev(self):
         return -1
@@ -256,6 +260,7 @@ def instance(ui, path, create):
 def instance(ui, path, create):
     if create:
         raise util.Abort(_('cannot create new bundle repository'))
+    origpath = path
     path = util.drop_scheme('file', path)
     if path.startswith('bundle:'):
         path = util.drop_scheme('bundle', path)
@@ -266,4 +271,4 @@ def instance(ui, path, create):
             repopath, bundlename = s
     else:
         repopath, bundlename = '', path
-    return bundlerepository(ui, repopath, bundlename)
+    return bundlerepository(ui, repopath, bundlename, origpath)
diff -r d0c48891dd4a -r 46102a6ad002 mercurial/commands.py
--- a/mercurial/commands.py	Thu Sep 27 23:59:18 2007 -0500
+++ b/mercurial/commands.py	Tue Oct 02 14:07:24 2007 -0500
@@ -1651,7 +1651,7 @@ def incoming(ui, repo, source="default",
     cmdutil.setremoteconfig(ui, opts)
 
     other = hg.repository(ui, source)
-    ui.status(_('comparing with %s\n') % source)
+    ui.status(_('comparing with %s\n') % other.printable_path())
     if revs:
         revs = [other.lookup(rev) for rev in revs]
     incoming = repo.findincoming(other, heads=revs, force=opts["force"])
@@ -1959,7 +1959,7 @@ def outgoing(ui, repo, dest=None, **opts
         revs = [repo.lookup(rev) for rev in revs]
 
     other = hg.repository(ui, dest)
-    ui.status(_('comparing with %s\n') % dest)
+    ui.status(_('comparing with %s\n') % other.printable_path())
     o = repo.findoutgoing(other, force=opts['force'])
     if not o:
         ui.status(_("no changes found\n"))
@@ -2092,7 +2092,7 @@ def pull(ui, repo, source="default", **o
     cmdutil.setremoteconfig(ui, opts)
 
     other = hg.repository(ui, source)
-    ui.status(_('pulling from %s\n') % (source))
+    ui.status(_('pulling from %s\n') % (other.printable_path()))
     if revs:
         try:
             revs = [other.lookup(rev) for rev in revs]
@@ -2139,7 +2139,7 @@ def push(ui, repo, dest=None, **opts):
     cmdutil.setremoteconfig(ui, opts)
 
     other = hg.repository(ui, dest)
-    ui.status('pushing to %s\n' % (dest))
+    ui.status('pushing to %s\n' % (other.printable_path()))
     if revs:
         revs = [repo.lookup(rev) for rev in revs]
     r = repo.push(other, opts['force'], revs=revs)
diff -r d0c48891dd4a -r 46102a6ad002 mercurial/httprepo.py
--- a/mercurial/httprepo.py	Thu Sep 27 23:59:18 2007 -0500
+++ b/mercurial/httprepo.py	Tue Oct 02 14:07:24 2007 -0500
@@ -198,6 +198,10 @@ class httprepository(remoterepository):
         # urllib cannot handle URLs with embedded user or passwd
         self._url = urlparse.urlunsplit((scheme, netlocunsplit(host, port),
                                          urlpath, '', ''))
+        
+        # printable path includes username but not password
+        self._printable_path = urlparse.urlunsplit((scheme, netlocunsplit(host, port, user),
+                                                    urlpath, '', ''))
         self.ui = ui
         self.ui.debug(_('using %s\n') % self._url)
 
@@ -270,6 +274,9 @@ class httprepository(remoterepository):
 
     def url(self):
         return self.path
+    
+    def printable_path(self):
+        return self._printable_path
 
     # look up capabilities only when needed
 
diff -r d0c48891dd4a -r 46102a6ad002 mercurial/localrepo.py
--- a/mercurial/localrepo.py	Thu Sep 27 23:59:18 2007 -0500
+++ b/mercurial/localrepo.py	Tue Oct 02 14:07:24 2007 -0500
@@ -101,6 +101,9 @@ class localrepository(repo.repository):
 
     def url(self):
         return 'file:' + self.root
+    
+    def printable_path(self):
+        return self.origroot
 
     def hook(self, name, throw=False, **args):
         return hook.hook(self.ui, self, name, throw, **args)
diff -r d0c48891dd4a -r 46102a6ad002 mercurial/repo.py
--- a/mercurial/repo.py	Thu Sep 27 23:59:18 2007 -0500
+++ b/mercurial/repo.py	Tue Oct 02 14:07:24 2007 -0500
@@ -26,6 +26,8 @@ class repository(object):
                 return cap[len(name_eq):]
         return False
 
+    def printable_path(self):
+        return self.path
     def requirecap(self, name, purpose):
         '''raise an exception if the given capability is not present'''
         if not self.capable(name):
diff -r d0c48891dd4a -r 46102a6ad002 mercurial/sshrepo.py
--- a/mercurial/sshrepo.py	Thu Sep 27 23:59:18 2007 -0500
+++ b/mercurial/sshrepo.py	Tue Oct 02 14:07:24 2007 -0500
@@ -42,6 +42,9 @@ class sshrepository(remoterepository):
         self.validate_repo(ui, sshcmd, args, remotecmd)
 
     def url(self):
+        return self._url
+        
+    def printable_path(self):
         return self._url
 
     def validate_repo(self, ui, sshcmd, args, remotecmd):
diff -r d0c48891dd4a -r 46102a6ad002 mercurial/statichttprepo.py
--- a/mercurial/statichttprepo.py	Thu Sep 27 23:59:18 2007 -0500
+++ b/mercurial/statichttprepo.py	Tue Oct 02 14:07:24 2007 -0500
@@ -32,6 +32,7 @@ class statichttprepository(localrepo.loc
     def __init__(self, ui, path):
         self._url = path
         self.ui = ui
+        self.origroot = 'static-' + path
 
         self.path = path.rstrip('/') + "/.hg"
         self.opener = opener(self.path)
diff -r d0c48891dd4a -r 46102a6ad002 tests/test-http-hide-password
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-http-hide-password	Tue Oct 02 14:07:24 2007 -0500
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+hg init test
+hg clone test test2
+
+cd test
+echo '[web]'>>.hg/hgrc
+echo 'allow_push = *'>>.hg/hgrc
+echo 'push_ssl = False'>>.hg/hgrc
+echo foo>foo
+hg add foo
+hg ci -m "added foo"
+hg serve -p 20059 -d --pid-file=../hg.pid
+cd ..
+cat hg.pid >> $DAEMON_PIDS
+
+echo % pull with password
+cd test2
+hg pull -u http://user:pass@localhost:20059/
+cd ..
+hg verify -R test2
+
+echo % push with password
+cd test2
+echo foo>foo2
+hg add foo2
+hg ci -m "added foo2"
+hg push http://user:pass@localhost:20059/
+cd ..
+hg verify -R test
\ No newline at end of file
diff -r d0c48891dd4a -r 46102a6ad002 tests/test-http-hide-password.out
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-http-hide-password.out	Tue Oct 02 14:07:24 2007 -0500
@@ -0,0 +1,26 @@
+0 files updated, 0 files merged, 0 files removed, 0 files unresolved
+% pull with password
+pulling from http://user@localhost:20059/
+requesting all changes
+adding changesets
+adding manifests
+adding file changes
+added 1 changesets with 1 changes to 1 files
+1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+checking changesets
+checking manifests
+crosschecking files in changesets and manifests
+checking files
+1 files, 1 changesets, 1 total revisions
+% push with password
+pushing to http://user@localhost:20059/
+searching for changes
+adding changesets
+adding manifests
+adding file changes
+added 1 changesets with 1 changes to 1 files
+checking changesets
+checking manifests
+crosschecking files in changesets and manifests
+checking files
+2 files, 2 changesets, 2 total revisions


More information about the Mercurial-devel mailing list