Class: Vx::SCM::Git

Inherits:
Object
  • Object
show all
Includes:
Common::Helper::Shell
Defined in:
lib/vx/scm/git.rb,
lib/vx/scm/git/git_ssh.rb

Defined Under Namespace

Classes: GitSSH

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Git.



15
16
17
18
19
20
21
22
23
# File 'lib/vx/scm/git.rb', line 15

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

Instance Attribute Details

#branchObject (readonly)

Returns the value of attribute branch.



13
14
15
# File 'lib/vx/scm/git.rb', line 13

def branch
  @branch
end

#git_sshObject (readonly)

Returns the value of attribute git_ssh.



13
14
15
# File 'lib/vx/scm/git.rb', line 13

def git_ssh
  @git_ssh
end

#loggerObject (readonly)

Returns the value of attribute logger.



13
14
15
# File 'lib/vx/scm/git.rb', line 13

def logger
  @logger
end

#pathObject (readonly)

Returns the value of attribute path.



13
14
15
# File 'lib/vx/scm/git.rb', line 13

def path
  @path
end

#pull_request_idObject (readonly)

Returns the value of attribute pull_request_id.



13
14
15
# File 'lib/vx/scm/git.rb', line 13

def pull_request_id
  @pull_request_id
end

#shaObject (readonly)

Returns the value of attribute sha.



13
14
15
# File 'lib/vx/scm/git.rb', line 13

def sha
  @sha
end

#srcObject (readonly)

Returns the value of attribute src.



13
14
15
# File 'lib/vx/scm/git.rb', line 13

def src
  @src
end

Class Method Details

.make_export_command(from, to) ⇒ Object



37
38
39
# File 'lib/vx/scm/git.rb', line 37

def self.make_export_command(from, to)
  %{ (cd '#{from}' && git checkout-index -a -f --prefix='#{to}/') }.strip
end

Instance Method Details

#commit_infoObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/vx/scm/git.rb', line 65

def commit_info
  rs = {}
  if str = commit_info_string
    if m = str.match(COMMIT_RE)
      rs.merge!(
        sha:     m[1],
        author:  m[2],
        email:   m[3],
        message: m[4]
      )
    end
  end
  OpenStruct.new rs
end

#fetchObject



31
32
33
34
35
# File 'lib/vx/scm/git.rb', line 31

def fetch
  open do
    run_git make_fetch_command
  end
end

#make_fetch_command(options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/vx/scm/git.rb', line 41

def make_fetch_command(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}/merge:"
    checkout_cmd = "git checkout -q FETCH_HEAD"
  end

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

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

  cmd = cmd.join(" && ").gsub("\n", ' ').gsub(/\ +/, ' ').strip
  cmd
end

#openObject



25
26
27
28
29
# File 'lib/vx/scm/git.rb', line 25

def open
  git_ssh.open do
    yield if block_given?
  end
end