1

之前的stackoverflow相关

我按照步骤在授权密钥前面加上了运行创建 repo 的 ruby​​ 脚本的步骤。脚本成功创建repo,但是git-receive-pack 发送代码无法正常卸载。回购由执行推送的同一用户拥有。

fatal: ''/path/to/my/repo'' does not appear to be a git repository

同样作为另一个症状,我的 ssh 会话会自动关闭,并带有一个命令前缀。一旦我删除命令,它就会毫无问题地推送。

对于我们的构建系统来说,这将是一个不错的功能,因此请寻找有关该行为的任何见解。

例如脚本

require 'git'
require 'fileutils'
require 'mixlib/shellout'

var = ENV['SSH_ORIGINAL_COMMAND']

def process_request(var)
  path = var.split[1].gsub("'", '')
  unless File.exist?(path)
    FileUtils.mkdir_p(File.dirname(path))
    Git.init(path, bare: true)
  end
  command = Mixlib::ShellOut.new(var)
  command.run_command
end

process_request(var) if var.to_s.include?('git-receive-pack')
4

1 回答 1

0

我的问题在于我如何调用 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
于 2018-06-22T18:51:07.953 回答