view contrib/hg-ssh @ 30080:33e8a5a00007

zsh_completion: update some option usage flags ('+', '=' and ':') Here are the relevant symbol descriptions from [0] optspec:... The colon indicates handling for one or more arguments to the option; if it is not present, the option is assumed to take no arguments. -optname+ The first argument may appear immediately after optname in the same word, or may appear as a separate word after the option. For example, ‘-foo+:...’ specifies that the completed option and argument will look like either ‘-fooarg’ or ‘-foo arg’. -optname= The argument may appear as the next word, or in same word as the option name provided that it is separated from it by an equals sign, for example ‘-foo=arg’ or ‘-foo arg’. There are 3 types of changes in this patch: - addition of '=': means that '--repository ~/foo' can now also be spelled as '--reporitory=~/foo' - addition of '+': means '-r 0' can now also be spelled as '-r0' - removal of '+', '=' and ':': means that '-u|--untrusted' doesn't take any arguments, so '-uq' is definitely '-u -q' and not '--untrusted=q' Occasionally, ':' had to be added together with '=' and '+'. This patch is mostly just making zsh_completion file look more like zsh's own hg completion file so that we can more easily take improvements from them or send patches to them. Some context for this patch from zsh project: [2] [3]. [0]: http://zsh.sourceforge.net/Doc/Release/Completion-System.html [1]: https://sourceforge.net/p/zsh/code/ci/master/tree/Completion/Unix/Command/_hg [2]: https://sourceforge.net/p/zsh/code/ci/92584634d3d39e9ca64475ae5af8010e2ccebe24/ [3]: http://www.zsh.org/mla/workers/2015/msg02551.html
author Anton Shestakov <av6@dwimlabs.net>
date Tue, 04 Oct 2016 20:49:59 +0800
parents 863075fd4cd0
children 77eaf9539499
line wrap: on
line source

#!/usr/bin/env python
#
# Copyright 2005-2007 by Intevation GmbH <intevation@intevation.de>
#
# Author(s):
# Thomas Arendsen Hein <thomas@intevation.de>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

"""
hg-ssh - a wrapper for ssh access to a limited set of mercurial repos

To be used in ~/.ssh/authorized_keys with the "command" option, see sshd(8):
command="hg-ssh path/to/repo1 /path/to/repo2 ~/repo3 ~user/repo4" ssh-dss ...
(probably together with these other useful options:
 no-port-forwarding,no-X11-forwarding,no-agent-forwarding)

This allows pull/push over ssh from/to the repositories given as arguments.

If all your repositories are subdirectories of a common directory, you can
allow shorter paths with:
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
from mercurial import demandimport; demandimport.enable()

from mercurial import dispatch

import sys, os, shlex

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 as 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:
            cmd = ['-R', repo, 'serve', '--stdio']
            if readonly:
                cmd += [
                    '--config',
                    'hooks.pretxnopen.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)
    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

if __name__ == '__main__':
    main()