Class: LgPodPlugin::GitRepository

Inherits:
Object
  • Object
show all
Defined in:
lib/lg_pod_plugin/git/git_clone.rb

Instance Method Summary collapse

Constructor Details

#initialize(checkout_options = {}) ⇒ GitRepository

Returns a new instance of GitRepository.



12
13
14
15
16
17
18
19
20
21
# File 'lib/lg_pod_plugin/git/git_clone.rb', line 12

def initialize(checkout_options = {})
  self.git = checkout_options[:git]
  self.tag = checkout_options[:tag]
  self.name = checkout_options[:name]
  self.path = checkout_options[:path]
  self.config = checkout_options[:config]
  self.commit = checkout_options[:commit]
  self.branch = checkout_options[:branch]
  @checkout_options = checkout_options
end

Instance Method Details

#downloadObject



23
24
25
26
27
28
29
30
31
# File 'lib/lg_pod_plugin/git/git_clone.rb', line 23

def download
  if self.git && self.tag
    self.git_clone_by_tag(self.path, "lg_temp_pod")
  elsif self.git && self.branch
    self.git_clone_by_branch self.path, "lg_temp_pod", self.branch
  elsif self.git && self.commit
    self.git_clone_by_commit self.path, "lg_temp_pod"
  end
end

#git_clone_by_branch(path, temp_name, branch = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lg_pod_plugin/git/git_clone.rb', line 33

def git_clone_by_branch(path, temp_name, branch = nil)
  new_branch = branch ? branch : nil
  download_temp_path = path.join(temp_name)
  if self.git && new_branch
    git_download_command(temp_name, self.git, new_branch, nil)
  else
    git_download_command(temp_name, self.git, nil, nil)
    if File.exist?(temp_name)
      system("git -C #{download_temp_path.to_path} rev-parse HEAD")
    end
  end
  download_temp_path
end

#git_clone_by_commit(path, temp_name) ⇒ Object

git clone commit



53
54
55
56
57
58
59
60
61
# File 'lib/lg_pod_plugin/git/git_clone.rb', line 53

def git_clone_by_commit(path, temp_name)
  Git.init(temp_name)
  FileUtils.chdir(temp_name)
  LgPodPlugin.log_blue "git clone #{self.git}"
  system("git remote add origin #{self.git}")
  system("git fetch origin #{self.commit}")
  system("git reset --hard FETCH_HEAD")
  path.join(temp_name)
end

#git_clone_by_tag(path, temp_name) ⇒ Object



47
48
49
50
# File 'lib/lg_pod_plugin/git/git_clone.rb', line 47

def git_clone_by_tag(path, temp_name)
  git_download_command(temp_name, self.git, nil, self.tag)
  path.join(temp_name)
end

#git_download_command(temp_name, git, branch, tag) ⇒ Object

封装 git clone命令



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/lg_pod_plugin/git/git_clone.rb', line 64

def git_download_command(temp_name, git, branch, tag)
  cmds = ['git']
  cmds << "clone"
  cmds << "#{git}"
  cmds << "#{temp_name} "
  cmds << "--template="
  cmds << "--single-branch --depth 1"
  if branch
    cmds << "--branch"
    cmds << branch
  elsif tag
    cmds << "--branch"
    cmds << tag
  end
  cmds_to_s = cmds.join(" ")
  LgPodPlugin.log_blue cmds_to_s
  system(cmds_to_s)
end