contrib/hg-ssh
changeset 43659 99e231afc29c
parent 38109 666d90ace0bb
child 45830 c102b704edb5
equal deleted inserted replaced
43658:0796e266d26b 43659:99e231afc29c
    33 import os
    33 import os
    34 import shlex
    34 import shlex
    35 import sys
    35 import sys
    36 
    36 
    37 # enable importing on demand to reduce startup time
    37 # enable importing on demand to reduce startup time
    38 import hgdemandimport ; hgdemandimport.enable()
    38 import hgdemandimport
       
    39 
       
    40 hgdemandimport.enable()
    39 
    41 
    40 from mercurial import (
    42 from mercurial import (
    41     dispatch,
    43     dispatch,
    42     pycompat,
    44     pycompat,
    43     ui as uimod,
    45     ui as uimod,
    44 )
    46 )
       
    47 
    45 
    48 
    46 def main():
    49 def main():
    47     # Prevent insertion/deletion of CRs
    50     # Prevent insertion/deletion of CRs
    48     dispatch.initstdio()
    51     dispatch.initstdio()
    49 
    52 
    54         if args[0] == '--read-only':
    57         if args[0] == '--read-only':
    55             readonly = True
    58             readonly = True
    56             args.pop(0)
    59             args.pop(0)
    57         else:
    60         else:
    58             break
    61             break
    59     allowed_paths = [os.path.normpath(os.path.join(cwd,
    62     allowed_paths = [
    60                                                    os.path.expanduser(path)))
    63         os.path.normpath(os.path.join(cwd, os.path.expanduser(path)))
    61                      for path in args]
    64         for path in args
       
    65     ]
    62     orig_cmd = os.getenv('SSH_ORIGINAL_COMMAND', '?')
    66     orig_cmd = os.getenv('SSH_ORIGINAL_COMMAND', '?')
    63     try:
    67     try:
    64         cmdargv = shlex.split(orig_cmd)
    68         cmdargv = shlex.split(orig_cmd)
    65     except ValueError as e:
    69     except ValueError as e:
    66         sys.stderr.write('Illegal command "%s": %s\n' % (orig_cmd, e))
    70         sys.stderr.write('Illegal command "%s": %s\n' % (orig_cmd, e))
    73             cmd = [b'-R', pycompat.fsencode(repo), b'serve', b'--stdio']
    77             cmd = [b'-R', pycompat.fsencode(repo), b'serve', b'--stdio']
    74             req = dispatch.request(cmd)
    78             req = dispatch.request(cmd)
    75             if readonly:
    79             if readonly:
    76                 if not req.ui:
    80                 if not req.ui:
    77                     req.ui = uimod.ui.load()
    81                     req.ui = uimod.ui.load()
    78                 req.ui.setconfig(b'hooks', b'pretxnopen.hg-ssh',
    82                 req.ui.setconfig(
    79                                  b'python:__main__.rejectpush', b'hg-ssh')
    83                     b'hooks',
    80                 req.ui.setconfig(b'hooks', b'prepushkey.hg-ssh',
    84                     b'pretxnopen.hg-ssh',
    81                                  b'python:__main__.rejectpush', b'hg-ssh')
    85                     b'python:__main__.rejectpush',
       
    86                     b'hg-ssh',
       
    87                 )
       
    88                 req.ui.setconfig(
       
    89                     b'hooks',
       
    90                     b'prepushkey.hg-ssh',
       
    91                     b'python:__main__.rejectpush',
       
    92                     b'hg-ssh',
       
    93                 )
    82             dispatch.dispatch(req)
    94             dispatch.dispatch(req)
    83         else:
    95         else:
    84             sys.stderr.write('Illegal repository "%s"\n' % repo)
    96             sys.stderr.write('Illegal repository "%s"\n' % repo)
    85             sys.exit(255)
    97             sys.exit(255)
    86     else:
    98     else:
    87         sys.stderr.write('Illegal command "%s"\n' % orig_cmd)
    99         sys.stderr.write('Illegal command "%s"\n' % orig_cmd)
    88         sys.exit(255)
   100         sys.exit(255)
    89 
   101 
       
   102 
    90 def rejectpush(ui, **kwargs):
   103 def rejectpush(ui, **kwargs):
    91     ui.warn((b"Permission denied\n"))
   104     ui.warn((b"Permission denied\n"))
    92     # mercurial hooks use unix process conventions for hook return values
   105     # mercurial hooks use unix process conventions for hook return values
    93     # so a truthy return means failure
   106     # so a truthy return means failure
    94     return True
   107     return True
    95 
   108 
       
   109 
    96 if __name__ == '__main__':
   110 if __name__ == '__main__':
    97     main()
   111     main()