Class: LgPodPlugin::LGitUtil

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ LGitUtil

Returns a new instance of LGitUtil.



18
19
20
21
22
23
24
25
# File 'lib/lg_pod_plugin/gitlab_download.rb', line 18

def initialize(name, options = {})
  self.name = name
  self.git = options[:git]
  self.tag = options[:tag]
  self.path = options[:path]
  self.branch = options[:branch]
  self.commit = options[:commit]
end

Class Method Details

.git_ls_remote_refs(name, git, branch, tag, commit) ⇒ Object

获取最新的一条 commit 信息



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/lg_pod_plugin/gitlab_download.rb', line 128

def self.git_ls_remote_refs(name,git, branch, tag, commit)
  ip = LRequest.shared.net_ping.ip
  network_ok = LRequest.shared.net_ping.network_ok
  return [nil, nil] unless (ip && network_ok)
  if branch
    LgPodPlugin.log_blue "git ls-remote --refs #{git} #{branch}"
    result = %x(timeout 5 git ls-remote --refs #{git} #{branch})
    unless result && result != ""
      id = LPodLatestRefs.get_pod_id(name, git, branch, tag)
      pod_info = LSqliteDb.shared.query_pod_refs(id)
      new_commit = pod_info.commit if pod_info
      return [branch, new_commit]
    end
    new_commit = LUtils.commit_from_ls_remote(result, branch)
    if new_commit
      LSqliteDb.shared.insert_pod_refs(name, git, branch, tag, new_commit)
    end
    return [branch, new_commit]
  elsif tag
    LgPodPlugin.log_blue "git ls-remote --tags #{git}"
    result = %x(timeout 5 git ls-remote --tags #{git})
    unless result && result != ""
      id = LPodLatestRefs.get_pod_id(name, git, branch, tag)
      pod_info = LSqliteDb.shared.query_pod_refs(id)
      new_commit = pod_info.commit if pod_info
      return [nil, new_commit]
    end
    new_commit = LUtils.commit_from_ls_remote(result, tag)
    if new_commit
      LSqliteDb.shared.insert_pod_refs(name, git, branch, tag, new_commit)
    end
    return [nil, new_commit]
  elsif commit
    return nil, commit
  else
    LgPodPlugin.log_blue "git ls-remote --refs #{git}"
    result = %x(timeout 5 git ls-remote -- #{git})
    unless result && result != ""
      id = LPodLatestRefs.get_pod_id(name, git, branch, tag)
      pod_info = LSqliteDb.shared.query_pod_refs(id)
      new_commit = pod_info.commit if pod_info
      return [nil, new_commit]
    end
    new_commit = LUtils.commit_from_ls_remote(result, "HEAD")
    if new_commit
      LSqliteDb.shared.insert_pod_refs(name, git, branch, tag, new_commit)
    end
    return nil, new_commit
  end
end

Instance Method Details

#git_clone_by_branch(path, temp_name) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/lg_pod_plugin/gitlab_download.rb', line 27

def git_clone_by_branch(path, temp_name)
  if self.git && self.branch
    LgPodPlugin.log_blue "git clone --template= --single-branch --depth 1 -b #{self.branch} #{self.git}"
    system("git clone --template= --single-branch --depth 1 -b #{self.branch} #{self.git} #{temp_name}")
  else
    LgPodPlugin.log_blue "git clone --template= --single-branch --depth 1 #{self.git}"
    system("git clone --template= --single-branch --depth 1 #{self.git} #{temp_name}")
  end
  path.join(temp_name)
end

#git_clone_by_commit(path, temp_name) ⇒ Object

git clone commit



45
46
47
48
49
50
51
52
53
# File 'lib/lg_pod_plugin/gitlab_download.rb', line 45

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



38
39
40
41
42
# File 'lib/lg_pod_plugin/gitlab_download.rb', line 38

def git_clone_by_tag(path, temp_name)
  LgPodPlugin.log_blue "git clone --template= --single-branch --depth 1 -b #{self.tag} #{self.git}"
  system("git clone --template= --single-branch --depth 1 -b #{self.tag} #{self.git} #{temp_name}")
  path.join(temp_name)
end

#git_clone_repository(path) ⇒ Object

clone 代码仓库



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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/lg_pod_plugin/gitlab_download.rb', line 56

def git_clone_repository(path)
  FileUtils.chdir(path)
  temp_name = "lg_temp_pod"
  git_archive = GitLabArchive.new(self.name, self.git, self.branch, self.tag, self.commit)
  if self.git && self.tag
    begin
      if LUtils.is_use_gitlab_archive_file(self.git)
        return git_archive.gitlab_download_tag_zip(path, temp_name)
      elsif self.git.include?("https://github.com")
        return git_archive.github_download_tag_zip path, temp_name
      else
        return self.git_clone_by_tag(path, temp_name)
      end
    rescue
      return self.git_clone_by_tag(path, temp_name)
    end
  elsif self.git && self.branch
    begin
      if LUtils.is_use_gitlab_archive_file(self.git)
        return git_archive.gitlab_download_branch_zip(path, temp_name)
      elsif self.git.include?("https://github.com")
        return git_archive.github_download_branch_zip path, temp_name
      else
        return self.git_clone_by_branch(path, temp_name)
      end
    rescue
      return self.git_clone_by_branch(path, temp_name)
    end
  elsif self.git && self.commit
    if LUtils.is_use_gitlab_archive_file(self.git)
      return git_archive.gitlab_download_commit_zip(path, temp_name)
    elsif self.git.include?("https://github.com")
      return git_archive.github_download_commit_zip path, temp_name
    else
      return self.git_clone_by_commit(path, temp_name)
    end
  elsif self.git
    if LUtils.is_use_gitlab_archive_file(self.git)
      return git_archive.gitlab_download_branch_zip(path, temp_name)
    elsif self.git.include?("https://github.com")
      return git_archive.github_download_branch_zip path, temp_name
    else
      return self.git_clone_by_branch(path, temp_name)
    end
  end

end

#pre_download_git_repositoryObject

git 预下载



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/lg_pod_plugin/gitlab_download.rb', line 105

def pre_download_git_repository
  temp_path = LFileManager.download_director.join("temp")
  if temp_path.exist?
    FileUtils.rm_rf(temp_path)
  end
  lg_pod_path = LRequest.shared.cache.cache_root
  unless lg_pod_path.exist?
    lg_pod_path.mkdir(0700)
  end
  get_temp_folder = git_clone_repository(lg_pod_path)
  #下载 git 仓库失败
  unless get_temp_folder && get_temp_folder.exist?
    return nil
  end
  if LRequest.shared.single_git
    LgPodPlugin::LCache.cache_pod(self.name, get_temp_folder, { :git => self.git })
  end
  LgPodPlugin::LCache.cache_pod(self.name, get_temp_folder, LRequest.shared.get_cache_key_params)
  FileUtils.chdir(LFileManager.download_director)
  FileUtils.rm_rf(lg_pod_path)
end