您可以在远程编写一个包装脚本,并在authorization_keys的行前加上command =“/ path/to/wrapper”。
command="/usr/local/bin/gitaccess" ssh-rsa ...
在这个包装中,您将检查SSH_ORIGINAL_COMMAND。混帐发出以下命令:
git receive-pack '/path/provided' # when pushing
git upload-pack '/path/provided' # when pulling
如果SSH_ORIGINAL_COMMAND不为空,并用其中的一个开始,你检查路径,如有必要,创建存储库,安装你需要它的任何配置,并执行命令。
如果SSH_ORIGINAL_COMMAND为空,并且您想为用户提供shell访问权限,那么您将调用一个shell。
如果SSH_ORIGINAL_COMMAND不为空,但不以git命令开始,如果要允许用户拥有shell访问权限,只需执行提供的命令即可。
这里有一些Ruby代码来演示。请注意,我没有测试它,还有改进的空间(例如,我们不应该硬编码/ bin/bash)。
#!/usr/bin/env ruby
orig_command = ENV['SSH_ORIGINAL_COMMAND']
if orig_command.nil?
# If you want to preserve shell access
exec '/bin/bash' # not tested, I hope it's interactive if executed this way
end
cmd_parts = orig_command.match /([a-z-]+) '([a-z0-9.\/]+)'/
if cmd_parts.nil?
# If you want to preserve shell access
exec '/bin/bash', '-c', orig_command
end
command = cmd_parts[1]
path = '/var/repositories/'+cmd_parts[2] # not secured (could contain '..')
if command == 'git-receive-pack' || command == 'git-upload-pack'
if not File.directory?(path)
`git init --bare #{path}` # not secured, I didn't escape path
# Do any configuration here
end
exec 'git-shell', '-c', "#{command} '#{path}'"
end
# If you want to preserve shell access
exec '/bin/bash', '-c', orig_command
你也可以传递参数给这个脚本在authorized_keys中来识别用户,并选择他们是否应该有shell访问。我也这样做来控制每个存储库的访问。如果你想将这个参数传递给git钩子,只需创建一个环境变量。
嗨。谢谢回复!尽管对于大多数观看这个问题的人来说这是一个有效的答案,但它并不是我正在寻找的那个。我需要能够推动我的本地__develop__分支,例如远程__staging__分支。但我不想创建一个新的分支来完成跟踪和所有。你知不知道这可能是可以实现的,而无需对本地存储库进行更改?对不起,如果问题有点不清楚,我想要做的可能不是很常见。 :) – 2010-11-05 15:55:49
@Michael:你的评论在这里听起来根本不像你的问题。在这里你正在谈论推送一个本地分支到不同的远程分支(可能存在也可能不存在)。然而,你的问题是关于推送到* repository *尚不存在的远程,并且必须创建。 – Cascabel 2010-11-05 17:49:11