1

related to previous stackoverflow

I followed the steps where I prefixed the authorized key with a run to a ruby script that creates the repo. The script successfully creates a repo, but the git-receive-pack sending of code fails to unload properly. the repo is owned by the same user doing the push.

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

Also as another symptom my ssh session automatically closes with a command prefix in place. As soon as I remove the command, it pushes without issue.

For our build system this would be a nice feature to have, so looking for any insight on the behavior.

script for example

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')
jcarapet
  • 15
  • 6

1 Answers1

0

My issues lied with how i was calling a shell to resume normal operation. modified the script from original post to better meet my needs.

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
jcarapet
  • 15
  • 6