我的问题在于我如何调用 shell 来恢复正常操作。修改了原始帖子的脚本以更好地满足我的需求。
require 'fileutils'
require 'logger'
require 'git'
LOG = Logger.new('/path/to/gitlog/create_on_push.log')
SSH_COMMAND = ENV['SSH_ORIGINAL_COMMAND']
USER_SHELL = '/bin/sh'
PATH_PREFIX = '/path/to/gitdir'
#########################################################################
# create missing path and repo
#
# @param path [string] path to the repository to create
#
def initialize_repo(repo_path)
path = File.join(PATH_PREFIX,repo_path)
unless File.exist?(path)
LOG.info("initializing repository #{path}")
FileUtils.mkdir_p(File.dirname(path))
Git.init(path, bare: true)
end
end
#########################################################################
# run ssh command if no arguments
#
def noargs
LOG.info('running shell with no arguments')
exec USER_SHELL
end
#########################################################################
# run supplied ssh command
#
def shell_with_command
LOG.info("shell with command \"#{SSH_COMMAND}\"")
exec USER_SHELL, '-c', SSH_COMMAND
end
#########################################################################
# initialize repo and allow for push of content to repo
#
# @param command [string] command to run
# @param path [string] path to the git repo
#
def git_shell_args(command, path)
repo_path = path.gsub('\'','')
initialize_repo(repo_path)
exec 'git-shell', '-c', "#{command} '#{repo_path}'"
end
# default action if no ssh commands were supplied
noargs if SSH_COMMAND.nil?
# words is empty or have 2 words
command, path = SSH_COMMAND.split(' ')
case command
when 'git-receive-pack'
git_shell_args(command, path)
when 'git-upload-pack'
git_shell_args(command, path)
else
shell_with_command
end