Class: Vx::Common::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/vx/common/git.rb

Constant Summary collapse

COMMIT_RE =
/^(.*) -:- (.*) \((.*)\) -:- (.*)$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src, sha, path, options = {}, &block) ⇒ Git

Returns a new instance of Git.



12
13
14
15
16
17
18
19
# File 'lib/vx/common/git.rb', line 12

def initialize(src, sha, path, options = {}, &block)
  @src             = src
  @sha             = sha
  @path            = path
  @branch          = options[:branch]
  @pull_request_id = options[:pull_request_id]
  @logger          = block
end

Instance Attribute Details

#branchObject (readonly)

Returns the value of attribute branch.



10
11
12
# File 'lib/vx/common/git.rb', line 10

def branch
  @branch
end

#git_sshObject (readonly)

Returns the value of attribute git_ssh.



10
11
12
# File 'lib/vx/common/git.rb', line 10

def git_ssh
  @git_ssh
end

#loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'lib/vx/common/git.rb', line 10

def logger
  @logger
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/vx/common/git.rb', line 10

def path
  @path
end

#pull_request_idObject (readonly)

Returns the value of attribute pull_request_id.



10
11
12
# File 'lib/vx/common/git.rb', line 10

def pull_request_id
  @pull_request_id
end

#shaObject (readonly)

Returns the value of attribute sha.



10
11
12
# File 'lib/vx/common/git.rb', line 10

def sha
  @sha
end

#srcObject (readonly)

Returns the value of attribute src.



10
11
12
# File 'lib/vx/common/git.rb', line 10

def src
  @src
end

Instance Method Details

#fetch_cmd(options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/vx/common/git.rb', line 31

def fetch_cmd(options = {})
  depth        = options.key?(:depth) ? options[:depth] : 50
  clone_branch = " --branch=#{branch}" if branch
  checkout_cmd = "git checkout -qf #{sha}"
  fetch_cmd    = nil

  if pull_request_id
    clone_branch = ""
    fetch_cmd = "git fetch origin +refs/pull/#{pull_request_id}/head"
    checkout_cmd = "git checkout -q FETCH_HEAD"
  end

  clone_cmd = "git clone --depth=#{depth}#{clone_branch} #{src} #{path}"

  cmd = []
  cmd << %{ echo "$ #{clone_cmd}" }
  cmd << clone_cmd
  if fetch_cmd
    cmd << %{ echo "$ #{fetch_cmd}" }
    cmd << %{ ( cd #{path} && #{fetch_cmd} ) }
  end
  cmd << %{ echo "$ #{checkout_cmd}" }
  cmd << %{ ( cd #{path} && #{checkout_cmd} ) }

  cmd.join("\n")
end

#git_ssh_content(key_location) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/vx/common/git.rb', line 21

def git_ssh_content(key_location)
  key = key_location ? "-i #{key_location}" : ""
  out = "#!/bin/sh\n"
  out << "exec /usr/bin/ssh"
  out << " -A -o LogLevel=quiet"
  out << " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
  out << " #{key} $@\n"
  out
end