在git push上创建回购失败并出现错误

时间:2018-06-21 22:34:06

标签: git ssh authorized-keys

previous stackoverflow

有关

我按照以下步骤进行操作,在该步骤中,将授权密钥加上运行到创建回购协议的ruby脚本的前缀。该脚本成功创建了一个存储库,但是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')

1 个答案:

答案 0 :(得分: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