Module: Pindo::Githelper

Extended by:
Executable
Included in:
Command, PgyerHelper
Defined in:
lib/pindo/base/githelper.rb

Instance Method Summary collapse

Methods included from Executable

capture_command, executable, execute_command, which, which!

Instance Method Details

#add_branch(local_repo_dir: nil, branch: nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pindo/base/githelper.rb', line 22

def add_branch(local_repo_dir: nil, branch: nil)

    current=Dir.pwd
    result = false

    if !local_branch_exists?(local_repo_dir: local_repo_dir, branch: branch)
      git!(%W(-C #{local_repo_dir} checkout -b #{branch}))
      result = true
    end

    git!(%W(-C #{local_repo_dir} push origin #{branch}:#{branch}))

    Dir.chdir(current)
    return result
end

#add_tag(local_repo_dir: nil, tag_name: nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/pindo/base/githelper.rb', line 96

def add_tag(local_repo_dir: nil, tag_name: nil)

    current=Dir.pwd
    result = false
    if File.exist?(local_repo_dir)
      Dir.chdir(local_repo_dir)
      if !local_tag_exists?(local_repo_dir: local_repo_dir, tag_name: tag_name)
          git!(%W(-C #{local_repo_dir} tag #{tag_name}))
          result = true
      end
      git!(%W(-C #{local_repo_dir} push origin #{tag_name})) 
    else
      result = false
    end
    Dir.chdir(current)
    return result
end

#add_tag_with_check(local_repo_dir: nil, tag_name: nil) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/pindo/base/githelper.rb', line 114

def add_tag_with_check(local_repo_dir:nil, tag_name:nil)

    if remote_tag_exists?(local_repo_dir: local_repo_dir, tag_name: tag_name)
        puts
        puts "路径: #{local_repo_dir}"
        puts "Tag: #{tag_name}"
        puts 
        puts "仓库已经存在同样tag:#{tag_name} !!!"
        
        answer = agree("确定删除远程仓库的tag, 重新添加tag:#{tag_name}吗(Y/n)?:")
        unless answer
            raise Informative, "添加tag异常!!!"
        end
        if local_tag_exists?(local_repo_dir: local_repo_dir, tag_name: tag_name)
            git!(%W(-C #{local_repo_dir} tag -d #{tag_name}))
        end
        git!(%W(-C #{local_repo_dir} push origin :#{tag_name}))
        add_tag(local_repo_dir:local_repo_dir, tag_name:tag_name)
    else
        if local_tag_exists?(local_repo_dir: local_repo_dir, tag_name: tag_name)
            git!(%W(-C #{local_repo_dir} tag -d #{tag_name}))
        end
        add_tag(local_repo_dir:local_repo_dir, tag_name:tag_name)
    end
end

#clone_clang_repoObject



379
380
381
382
383
384
385
386
387
388
389
# File 'lib/pindo/base/githelper.rb', line 379

def clone_clang_repo           
  pindo_dir = File::expand_path(Pindoconfig.instance.pindo_dir)
  if !File.exist?(pindo_dir)
    FileUtils.mkdir(pindo_dir) 
  end

  getcode_to_dir(reponame:"deployclang", remote_url:Pindoconfig.instance.deploy_clang_giturl, path:pindo_dir)

  config_repo_dir = File.join(pindo_dir, "deployclang")
  return config_repo_dir
end

#clone_devclang_repoObject



367
368
369
370
371
372
373
374
375
376
377
# File 'lib/pindo/base/githelper.rb', line 367

def clone_devclang_repo           
  pindo_dir = File::expand_path(Pindoconfig.instance.pindo_dir)
  if !File.exist?(pindo_dir)
    FileUtils.mkdir(pindo_dir) 
  end

  getcode_to_dir(reponame:"confuseclang", remote_url:Pindoconfig.instance.dev_clang_giturl, path:pindo_dir)

  config_repo_dir = File.join(pindo_dir, "confuseclang")
  return config_repo_dir
end

#clone_pindo_common_config_repo(force_delete: false) ⇒ Object



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/pindo/base/githelper.rb', line 332

def clone_pindo_common_config_repo(force_delete:false)
  pindo_dir = File::expand_path(Pindoconfig.instance.pindo_dir)
  if !File.exist?(pindo_dir)
    FileUtils.mkdir(pindo_dir) 
  end

  common_config_dir_basename = get_repo_base_name(repo_url:Pindoconfig.instance.pindo_common_config_giturl)
  config_repo_dir = File.join(pindo_dir, common_config_dir_basename)
  if force_delete
    if File.exist?(config_repo_dir)
      FileUtils.rm_rf(config_repo_dir)
    end
  end
  getcode_to_dir(reponame:common_config_dir_basename, remote_url:Pindoconfig.instance.pindo_common_config_giturl, path:pindo_dir)
  
  return config_repo_dir
end

#clone_pindo_env_config_repo(force_delete: false) ⇒ Object



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/pindo/base/githelper.rb', line 350

def clone_pindo_env_config_repo(force_delete:false)          
  pindo_dir = File::expand_path(Pindoconfig.instance.pindo_dir)
  if !File.exist?(pindo_dir)
    FileUtils.mkdir(pindo_dir) 
  end

  env_config_dir_basename = get_repo_base_name(repo_url:Pindoconfig.instance.pindo_env_config_giturl)
  config_repo_dir = File.join(pindo_dir, env_config_dir_basename)
  if force_delete
    if File.exist?(config_repo_dir)
      FileUtils.rm_rf(config_repo_dir)
    end
  end
  getcode_to_dir(reponame:env_config_dir_basename, remote_url:Pindoconfig.instance.pindo_env_config_giturl, path:pindo_dir)
  return config_repo_dir
end

#clong_buildconfig_repo(repo_name: nil) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/pindo/base/githelper.rb', line 307

def clong_buildconfig_repo(repo_name:nil)      
  

  pindo_dir = File::expand_path(Pindoconfig.instance.pindo_dir)
  if !File.exist?(pindo_dir)
    FileUtils.mkdir(pindo_dir) 
  end

  repo_org_name = Pindoconfig.instance.build_deploy_org
  
  git_repo_file = File.join(Pindoconfig.instance.pindo_env_configdir,  "git_base_url.json")
  git_repo_json = JSON.parse(File.read(git_repo_file))
  if repo_name && git_repo_json && git_repo_json[repo_name]
    repo_org_name = git_repo_json[repo_name]
  end

  url = File.join("https://gitee.com", repo_org_name, repo_name + ".git")
  getcode_to_dir(reponame:repo_name, remote_url: url, path:pindo_dir)
  config_repo_dir = File.join(pindo_dir, repo_name)
  


  return config_repo_dir
end

#get_repo_base_name(repo_url: nil) ⇒ Object



299
300
301
302
303
304
305
# File 'lib/pindo/base/githelper.rb', line 299

def get_repo_base_name(repo_url:nil)  
  temp_url = String.new(repo_url)
  index = temp_url.rindex('/') 
  temp_url = temp_url.slice(index + 1, temp_url.length) 
  base_name = temp_url.gsub!(/\.git/, "")
  return base_name 
end

#getcode_to_dir(reponame: nil, remote_url: nil, path: nil, new_branch: "master", new_tag: nil) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/pindo/base/githelper.rb', line 223

def getcode_to_dir(reponame:nil, remote_url:nil, path: nil, new_branch:"master", new_tag: nil)

  current=Dir.pwd
  local_repo_dir = File::join(path, reponame)

  Funlog.instance.fancyinfo_update("开始更新仓库:#{local_repo_dir}...")

  begin
    

    if !reponame.empty? && !remote_url.empty? && !path.empty?

        if File.exist?(local_repo_dir)
          Dir.chdir(local_repo_dir)

          remote_info = git!(%W(remote -v))
          if remote_info.include?("git.coding.net")
            current_branch = git!(%W(-C #{local_repo_dir} rev-parse --abbrev-ref HEAD)).strip
            git!(%W(remote remove origin))
            git!(%W(remote add origin #{remote_url}))
            args = %W(-C #{local_repo_dir} fetch origin)
            args.push('--progress')
            git!(args)
            git!(%W(-C #{local_repo_dir} branch --set-upstream-to=origin/#{current_branch}  #{current_branch}))  
          end

          git!(%W(-C #{local_repo_dir} fetch origin --progress))
        else
          git! ['clone', remote_url, local_repo_dir]
        end

        Dir.chdir(local_repo_dir)
        current_branch = git!(%W(-C #{local_repo_dir} rev-parse --abbrev-ref HEAD)).strip
        if current_branch.eql?(new_branch)
            git!(%W(-C #{local_repo_dir} reset --hard origin/#{new_branch}))      
            git!(%W(-C #{local_repo_dir} branch --set-upstream-to=origin/#{new_branch} #{new_branch}))       
            git!(%W(-C #{local_repo_dir} pull))
            
        else
          git!(%W(-C #{local_repo_dir} clean -fd))
          if local_branch_exists?(local_repo_dir:local_repo_dir, branch:new_branch)
            git!(%W(-C #{local_repo_dir} checkout #{new_branch})) 
          else
            if !remote_branch_exists?(local_repo_dir:local_repo_dir, branch:new_branch)
                git!(%W(-C #{local_repo_dir} checkout -b #{new_branch})) 
            else
                raise Informative, "仓库中的#{new_branch}分支不存在 !!!"
            end
          end
          git!(%W(-C #{local_repo_dir} branch --set-upstream-to=origin/#{new_branch} #{new_branch})) 
          git!(%W(-C #{local_repo_dir} pull))
        end

        if !new_tag.nil? && new_tag != "" 
          if !new_tag.nil? && new_tag != "" && local_tag_exists?(local_repo_dir:local_repo_dir, tag_name:new_tag)
            puts "tag : #{new_tag} is exist !!!"
            git!(%W(-C #{local_repo_dir} checkout #{new_tag}))
          else
            raise Informative, "tag #{new_tag} is not exist !!!"
          end
        end

    end
    $stdin.flush
    Funlog.instance.fancyinfo_success("仓库#{local_repo_dir}更新完成!")
  rescue StandardError => e

    Funlog.instance.fancyinfo_error("仓库#{local_repo_dir}更新失败!")
    
    raise Informative, e.to_s
  end

  Dir.chdir(current)
  return local_repo_dir
end

#git_addpush_repo(path: nil, message: "res", commit_file_params: nil) ⇒ Object



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/pindo/base/githelper.rb', line 391

def git_addpush_repo(path:nil, message:"res", commit_file_params:nil)           
  current=Dir.pwd
  Dir.chdir(path)
  begin
      if !commit_file_params.nil? && !commit_file_params.empty?
        files_list = git! ['ls-files', '--other', '--modified', '--exclude-standard'] || []

        need_commit = false
        commit_file_params.each do |file_name|
          if !files_list.nil? && files_list.size > 0 && files_list.include?(file_name)
              need_commit = true
              git! ['add', file_name]  
          end
        end
        if need_commit
          git! ['commit', '-m ' + "#{message}"]  
          git! ['push']
        else
          # puts "\n#{path}\n!!!仓库中文件未发生变化,无需提交!!!\n"
        end
      else
        files_list = git! ['ls-files', '--other', '--modified', '--exclude-standard'] || []
        # puts "提交如下内容:"
        # puts files_list
        if !files_list.nil? && files_list.size > 0
            git! ['add', '-A']
            git! ['commit', '-m ' + "#{message}"]
            git! ['push']
        else
          puts "\n#{path}\n!!!仓库中文件未发生变化,无需提交!!!\n"
        end
      end

  rescue => error
      # puts(error.to_s)
      raise Informative, "\n#{path}\n 仓库失败 !!!"
  end


  Dir.chdir(current)
end

#git_latest_commit_id(local_repo_dir: nil) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/pindo/base/githelper.rb', line 198

def git_latest_commit_id(local_repo_dir:nil)

  current=Dir.pwd

  unless File.exist?(File::join(local_repo_dir, ".git"))
    return nil
  end
  
  commit_id = nil

  if File.exist?(local_repo_dir)
    Dir.chdir(local_repo_dir)
    current_branch = git!(%W(-C #{local_repo_dir} rev-parse --abbrev-ref HEAD)).strip
    # puts "current_branch: #{current_branch}"

    # git log -n 1 --pretty=format:"commit %H"
    # format_str = "commit %H"
    # latest_log_info = git!(%W(-C #{current} log -n 1 --pretty=#{format_str} #{current_branch})).strip
    commit_id = git!(%W(-C #{local_repo_dir} rev-parse #{current_branch})).strip
  end
  Dir.chdir(current)

  return commit_id
end

#local_branch_exists?(local_repo_dir: nil, branch: nil) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/pindo/base/githelper.rb', line 62

def local_branch_exists?(local_repo_dir: nil, branch: nil)
    current=Dir.pwd
    result = false

    if File.exist?(local_repo_dir)
      Dir.chdir(local_repo_dir)

      res_data = Executable.capture_command('git', %W(rev-parse --verify #{branch}), :capture => :out)
      # puts "=====1"
      # puts res_data
      # res_data = git!(%W(-C #{local_repo_dir} --no-pager branch --list origin/#{branch} --no-color -r))
      result = !res_data.nil? && !res_data.empty?
    else
      result = false
    end
    Dir.chdir(current)
    return result
end

#local_tag_exists?(local_repo_dir: nil, tag_name: nil) ⇒ Boolean

Returns:

  • (Boolean)


182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/pindo/base/githelper.rb', line 182

def local_tag_exists?(local_repo_dir: nil, tag_name: nil)
    # tag_ref = "refs/tags/#{tag_name}"
    result = false
    current=Dir.pwd

    if File.exist? (local_repo_dir)
      Dir.chdir(local_repo_dir)
      res_data = git!(%W(-C #{local_repo_dir} tag --list #{tag_name}))
      result = !res_data.nil? && !res_data.empty?
    else
      result = false
    end
    Dir.chdir(current)
    return result
end

#prepare_gitenvObject



12
13
14
15
16
17
18
19
20
# File 'lib/pindo/base/githelper.rb', line 12

def prepare_gitenv()

    usrname = Etc.getlogin
    command = "git config --global user.email #{usrname}@example.com"
    system command
    command = "git config --global user.name #{usrname}"
    system command

end

#process_need_add_files(project_dir: nil) ⇒ Object



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/pindo/base/githelper.rb', line 435

def process_need_add_files(project_dir:nil)

        current_project_dir = project_dir
        origin_path=Dir.pwd
        Dir.chdir(current_project_dir)
        coding_branch = git!(%W(-C #{current_project_dir} rev-parse --abbrev-ref HEAD)).strip
        git!(%W(-C #{current_project_dir} restore --staged #{current_project_dir}))

        files_list = git!(%W(-C #{current_project_dir} ls-files --other --modified --exclude-standard)) || []
        if !files_list.nil? && files_list.size > 0

            process_types = ["全部提交", "全部删除", "退出"]
            puts 
            puts "当前所在分支:#{coding_branch}有下面这些文件没有提交:"
            puts 
            puts files_list
            puts 


            cli = HighLine.new
            menu_choice="None"
            cli.choose do |menu|                                  # you can also use constants like :blue
                menu.header = "对以上未提交的文件, 有下列处理方式"
                menu.prompt = "请选择如何处理(1/2/3...):"
                if !process_types.nil? && process_types.length > 0
                  for index_item in process_types do
                    menu.choice(index_item) do |details|
                      # puts index_item
                      menu_choice="#{details}"
                    end
                  end
                end
            end

            puts "选择的处理方式为: #{menu_choice}"
            if menu_choice.eql?("全部提交")
                message = ask('请输入提交的备注信息: ') || "release code"
                git! ['add', '-A']
                git! ['commit', '-m ' + "#{message}"]
                git!(%W(-C #{current_project_dir} fetch origin))
                if remote_branch_exists?(local_repo_dir: current_project_dir, branch:coding_branch)
                  git!(%W(-C #{current_project_dir} branch --set-upstream-to=origin/#{coding_branch} #{coding_branch}))   
                  git! ['pull']
                  git! ['push']
                else
                  git!(%W(-C #{current_project_dir} push --set-upstream origin #{coding_branch}))   
                end
            elsif menu_choice.eql?("全部删除")  

                answer = agree("有未提交的文件,确认删除?(Y/n):")
                unless answer
                  raise Informative, "请手动处理未提交的文件!!!"
                end 
                git!(%W(-C #{current_project_dir} reset --hard))
                git!(%W(-C #{current_project_dir} clean -dfx))
                git!(%W(-C #{current_project_dir} branch --set-upstream-to=origin/#{coding_branch} #{coding_branch})) 
                git!(%W(-C #{current_project_dir} pull))
            else
                raise Informative, "请手动处理未提交的文件!!!"
            end
        else
            puts "当前所在分支没有需要提交的文件!!"
            puts
        end

        Dir.chdir(origin_path)
end

#remote_branch_exists?(local_repo_dir: nil, branch: nil) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pindo/base/githelper.rb', line 81

def remote_branch_exists?(local_repo_dir: nil, branch: nil)
    current=Dir.pwd
    result = false
    if File.exist?(local_repo_dir)
      Dir.chdir(local_repo_dir)
      res_data = git!(%W(-C #{local_repo_dir} ls-remote --heads origin refs/heads/#{branch}))
      result = !res_data.nil? && !res_data.empty?
    else
      result = false
    end
    Dir.chdir(current)
    return result
end

#remote_tag_exists?(local_repo_dir: nil, tag_name: nil) ⇒ Boolean

Returns:

  • (Boolean)


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/pindo/base/githelper.rb', line 163

def remote_tag_exists?(local_repo_dir: nil, tag_name: nil)

    result = false
    current=Dir.pwd

    if File.exist?(local_repo_dir)
      Dir.chdir(local_repo_dir)
      tag_ref = "refs/tags/#{tag_name}"            
      res_data = git!(%W(-C #{local_repo_dir} ls-remote --tags origin #{tag_ref})) 
      result =  !res_data.nil? && !res_data.empty?
    else
      result = false
    end
    Dir.chdir(current)
    return result


end

#remove_branch(local_repo_dir: nil, branch: nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pindo/base/githelper.rb', line 38

def remove_branch(local_repo_dir: nil, branch: nil)
    current=Dir.pwd

    result = false
    if File.exist?(local_repo_dir)
      current_branch = git!(%W(-C #{local_repo_dir} rev-parse --abbrev-ref HEAD)).strip
      if local_branch_exists?(local_repo_dir: local_repo_dir, branch: branch)
          git!(%W(-C #{local_repo_dir} branch -D #{branch})) 
          result = true
      end
      
      if remote_branch_exists?(local_repo_dir: local_repo_dir, branch: branch)
          git!(%W(-C #{local_repo_dir} push origin :#{branch})) 
          result = true
      end

    else
      result = false
    end
    Dir.chdir(current)
    return result
end

#remove_tag(local_repo_dir: nil, tag_name: nil) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/pindo/base/githelper.rb', line 140

def remove_tag(local_repo_dir: nil, tag_name: nil)

    result = false
    current=Dir.pwd
    if File.exist?(local_repo_dir)
      Dir.chdir(local_repo_dir)
      if local_tag_exists?(local_repo_dir: local_repo_dir, tag_name: tag_name)
          git!(%W(-C #{local_repo_dir} tag -d #{tag_name})) 
          result = true
      end
      if remote_tag_exists?(local_repo_dir: local_repo_dir, tag_name: tag_name)
          git!(%W(-C #{local_repo_dir} push origin :#{tag_name})) 
          result = true
      end
    else
      result = false
    end
    Dir.chdir(current)
    return result

end