# HG changeset patch # User David Schleimer # Date 2012-05-21 23:19:30 # Node ID 67bfe7f64e57fffa33b134c731c66e4493ceef5c # Parent 2ac08d8b21aa7b6e0a062afed5a3f357ccef67f9 hg-ssh: refactor to have main() method Refactor hg-ssh to have a main() function instead of a bunch of top-level statements. diff --git a/contrib/hg-ssh b/contrib/hg-ssh --- a/contrib/hg-ssh +++ b/contrib/hg-ssh @@ -33,25 +33,31 @@ from mercurial import dispatch import sys, os, shlex -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', '?') -try: - cmdargv = shlex.split(orig_cmd) -except ValueError, e: - sys.stderr.write('Illegal command "%s": %s\n' % (orig_cmd, e)) - sys.exit(255) +def main(): + 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', '?') + try: + cmdargv = shlex.split(orig_cmd) + except ValueError, e: + sys.stderr.write('Illegal command "%s": %s\n' % (orig_cmd, e)) + sys.exit(255) -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: - dispatch.dispatch(dispatch.request(['-R', repo, 'serve', '--stdio'])) + 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: + dispatch.dispatch(dispatch.request(['-R', repo, + 'serve', + '--stdio'])) + else: + sys.stderr.write('Illegal repository "%s"\n' % repo) + sys.exit(255) else: - sys.stderr.write('Illegal repository "%s"\n' % repo) + sys.stderr.write('Illegal command "%s"\n' % orig_cmd) sys.exit(255) -else: - sys.stderr.write('Illegal command "%s"\n' % orig_cmd) - sys.exit(255) +if __name__ == '__main__': + main()