diff options
Diffstat (limited to 'contrib/hg-ssh')
-rwxr-xr-x | contrib/hg-ssh | 64 |
1 files changed, 15 insertions, 49 deletions
diff --git a/contrib/hg-ssh b/contrib/hg-ssh index 5021958..abf7a7d 100755 --- a/contrib/hg-ssh +++ b/contrib/hg-ssh @@ -24,9 +24,6 @@ command="cd path/to/my/repositories && hg-ssh repo1 subdir/repo2" You can use pattern matching of your normal shell, e.g.: command="cd repos && hg-ssh user/thomas/* projects/{mercurial,foo}" - -You can also add a --read-only flag to allow read-only access to a key, e.g.: -command="hg-ssh --read-only repos/*" """ # enable importing on demand to reduce startup time @@ -34,53 +31,22 @@ from mercurial import demandimport; demandimport.enable() from mercurial import dispatch -import sys, os, shlex +import sys, os -def main(): - cwd = os.getcwd() - readonly = False - args = sys.argv[1:] - while len(args): - if args[0] == '--read-only': - readonly = True - args.pop(0) - else: - break - allowed_paths = [os.path.normpath(os.path.join(cwd, - os.path.expanduser(path))) - for path in args] - orig_cmd = os.getenv('SSH_ORIGINAL_COMMAND', '?') - try: - cmdargv = shlex.split(orig_cmd) - except ValueError, e: - sys.stderr.write('Illegal command "%s": %s\n' % (orig_cmd, e)) - sys.exit(255) +cwd = os.getcwd() +allowed_paths = [os.path.normpath(os.path.join(cwd, os.path.expanduser(path))) + for path in sys.argv[1:]] +orig_cmd = os.getenv('SSH_ORIGINAL_COMMAND', '?') - if cmdargv[:2] == ['hg', '-R'] and cmdargv[3:] == ['serve', '--stdio']: - path = cmdargv[2] - repo = os.path.normpath(os.path.join(cwd, os.path.expanduser(path))) - if repo in allowed_paths: - cmd = ['-R', repo, 'serve', '--stdio'] - if readonly: - cmd += [ - '--config', - 'hooks.prechangegroup.hg-ssh=python:__main__.rejectpush', - '--config', - 'hooks.prepushkey.hg-ssh=python:__main__.rejectpush' - ] - dispatch.dispatch(dispatch.request(cmd)) - else: - sys.stderr.write('Illegal repository "%s"\n' % repo) - sys.exit(255) +if orig_cmd.startswith('hg -R ') and orig_cmd.endswith(' serve --stdio'): + path = orig_cmd[6:-14] + repo = os.path.normpath(os.path.join(cwd, os.path.expanduser(path))) + if repo in allowed_paths: + dispatch.dispatch(dispatch.request(['-R', repo, 'serve', '--stdio'])) else: - sys.stderr.write('Illegal command "%s"\n' % orig_cmd) - sys.exit(255) - -def rejectpush(ui, **kwargs): - ui.warn("Permission denied\n") - # mercurial hooks use unix process conventions for hook return values - # so a truthy return means failure - return True + sys.stderr.write("Illegal repository %r\n" % repo) + sys.exit(-1) +else: + sys.stderr.write("Illegal command %r\n" % orig_cmd) + sys.exit(-1) -if __name__ == '__main__': - main() |