您可以在遠程編寫一個包裝腳本,並在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