Method: GitHandler::Session#execute

Defined in:
lib/git_handler/session.rb

#execute(args, env, run_git = true) ⇒ Object

Execute session

Parameters:

  • args (Array)

    session arguments

  • env (Hash)

    hash with environment variables, use ENV.to_hash.dup

  • run_git (Boolean) (defaults to: true)

    execute git command if set to true

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/git_handler/session.rb', line 36

def execute(args, env, run_git=true)
  @args = args
  @env  = env

  raise SessionError, "Invalid environment" unless valid_environment?
  raise SessionError, "Invalid git request" unless valid_request?

  command   = parse_command(env['SSH_ORIGINAL_COMMAND'])
  repo_path = File.join(config.repos_path, command[:repo])
  request   = GitHandler::Request.new(
    :remote_ip => env['SSH_CLIENT'].split(' ').first,
    :args      => args,
    :env       => env,
    :repo      => command[:repo],
    :repo_path => repo_path,
    :command   => [command[:action], "'#{repo_path}'"].join(' '),
    :read      => command[:read],
    :write     => command[:write]
  )

  if config.log == true
    log_request(request) 
  end

  if config.raise_errors == true
    unless File.exist?(request.repo_path)
      raise SessionError, "Repository #{request.repo} does not exist!"
    end
  end

  if block_given?
    # Pass all request information for custom processing
    # if no block is defined it will execute git-shell
    # with parameters provided
    yield request
  else
    if run_git == true
      exec("git-shell", "-c", request.command)
    end
  end

  # Interesting part, inspired by github write-up
  # if we need to pass this to another server
  # the process should replace itself with another ssh call:
  # exec("ssh", "git@TARGET", "#{args.join(' ')}")
end