Module: RHC::GitHelpers

Included in:
Commands::App, Commands::GitClone, ContextHelpers, Wizard
Defined in:
lib/rhc/git_helpers.rb

Instance Method Summary collapse

Instance Method Details

#git_clone_application(app) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rhc/git_helpers.rb', line 33

def git_clone_application(app)
  repo_dir = options.repo || app.name

  debug "Pulling new repo down"
  dir = git_clone_repo(app.git_url, repo_dir)

  debug "Configuring git repo"
  Dir.chdir(repo_dir) do
    git_config_set "rhc.app-id", app.id
    git_config_set "rhc.app-name", app.name
    git_config_set "rhc.domain-name", app.domain_id

    git_remote_add("upstream", app.initial_git_url) if app.initial_git_url.present?
  end

  git_clone_deploy_hooks(repo_dir)

  dir
end

#git_clone_deploy_hooks(repo_dir) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/rhc/git_helpers.rb', line 24

def git_clone_deploy_hooks(repo_dir)
  debug "Deploy default hooks"
  Dir.chdir(repo_dir) do |dir|
    Dir.glob(".openshift/git_hooks/*") do |hook|
      FileUtils.cp(hook, ".git/hooks/")
    end
  end
end

#git_clone_repo(git_url, repo_dir) ⇒ Object

:nocov:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rhc/git_helpers.rb', line 85

def git_clone_repo(git_url, repo_dir)
  # quote the repo to avoid input injection risk
  destination = (repo_dir ? " \"#{repo_dir}\"" : "")
  cmd = "#{git_cmd} clone #{git_url}#{destination}"
  debug "Running #{cmd}"

  status, stdout, stderr = run_with_tee(cmd)

  if status != 0
    case stderr
    when /fatal: destination path '[^']*' already exists and is not an empty directory./
      raise RHC::GitDirectoryExists, "The directory you are cloning into already exists."
    when /^Permission denied \(.*?publickey.*?\).$/
      raise RHC::GitPermissionDenied, "You don't have permission to access this repository.  Check that your SSH public keys are correct."
    else
      raise RHC::GitException, "Unable to clone your repository. Called Git with: #{cmd}"
    end
  end
  File.expand_path(repo_dir)
end

#git_cmdObject



6
7
8
# File 'lib/rhc/git_helpers.rb', line 6

def git_cmd
  "git"
end

#git_config_get(key) ⇒ Object

:nocov: These all call external binaries so test them in cucumber



61
62
63
64
65
66
67
68
69
70
# File 'lib/rhc/git_helpers.rb', line 61

def git_config_get(key)
  return nil unless has_git?
  
  config_get_cmd = "#{git_cmd} config --get #{key}"
  value = %x[#{config_get_cmd}].strip
  debug "Git config '#{config_get_cmd}' returned '#{value}'"
  value = nil if $?.exitstatus != 0 or value.empty?

  value
end

#git_config_set(key, value) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rhc/git_helpers.rb', line 72

def git_config_set(key, value)
  unset_cmd = "#{git_cmd} config --unset-all #{key}"
  config_cmd = "#{git_cmd} config --add #{key} #{value}"
  debug "Adding #{key} = #{value} to git config"
  commands = [unset_cmd, config_cmd]
  commands.each do |cmd|
    debug "Running #{cmd} 2>&1"
    output = %x[#{cmd} 2>&1]
    raise RHC::GitException, "Error while adding config values to git - #{output}" unless output.empty?
  end
end

#git_remote_add(remote_name, remote_url) ⇒ Object

Raises:



53
54
55
56
57
58
# File 'lib/rhc/git_helpers.rb', line 53

def git_remote_add(remote_name, remote_url)
  cmd = "#{git_cmd} remote add upstream \"#{remote_url}\""
  debug "Running #{cmd} 2>&1"
  output = %x[#{cmd} 2>&1]
  raise RHC::GitException, "Error while adding upstream remote - #{output}" unless output.empty?
end

#git_versionObject



10
11
12
# File 'lib/rhc/git_helpers.rb', line 10

def git_version
  @git_version ||= `#{git_cmd} --version 2>&1`.strip #:nocov:
end

#has_git?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
# File 'lib/rhc/git_helpers.rb', line 14

def has_git?
  @has_git ||= begin
      @git_version = nil
      git_version
      $?.success?
    rescue
      false
    end
end