Class: Git::Lib

Inherits:
Object
  • Object
show all
Defined in:
lib/KCommercialPipeline/core/branch_diffs.rb,
lib/KCommercialPipeline/core/git.rb

Instance Method Summary collapse

Instance Method Details

#command_lines_pub(cmd, *option) ⇒ Object



44
45
46
# File 'lib/KCommercialPipeline/core/git.rb', line 44

def command_lines_pub(cmd,*option)
  ks_command(cmd,*option)
end

#diff_log_between_source_branch_target_branch(source_branch, target_branch) ⇒ Object

获取两分支间差异



50
51
52
53
54
55
56
57
58
59
# File 'lib/KCommercialPipeline/core/branch_diffs.rb', line 50

def diff_log_between_source_branch_target_branch(source_branch,target_branch)
   begin
      logs = command_lines("log", [source_branch,"^#{target_branch}"])
      diff_commits_maps = process_commit_log_data(logs)
      diff_commits_maps
   rescue
      KCommercial::UI.error "分支对比失败"
      exit! 1
   end
end

#ks_command(cmd, *opts, &block) ⇒ Object



48
49
50
51
52
53
54
55
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
# File 'lib/KCommercialPipeline/core/git.rb', line 48

def ks_command(cmd, *opts, &block)
  command_opts = { chomp: true, redirect: '' }
  if opts.last.is_a?(Hash)
    command_opts.merge!(opts.pop)
  end
  command_opts.keys.each do |k|
    raise ArgumentError.new("Unsupported option: #{k}") unless [:chomp, :redirect].include?(k)
  end

  global_opts = []
  global_opts << "--git-dir=#{@git_dir}" if !@git_dir.nil?
  global_opts << "--work-tree=#{@git_work_dir}" if !@git_work_dir.nil?
  global_opts << ["-c", "color.ui=false"]

  opts = [opts].flatten.map {|s| escape(s) }.join(' ')

  global_opts = global_opts.flatten.map {|s| escape(s) }.join(' ')

  git_cmd = "#{Git::Base.config.binary_path} #{global_opts} #{cmd} #{opts} #{command_opts[:redirect]} 2>&1"

  output = nil

  exitstatus = nil

  with_custom_env_variables do
    command_thread = Thread.new do
      KCommercial::UI.info "开始执行git命令: #{git_cmd}"
      output = run_command(git_cmd, &block)
      KCommercial::UI.info "执行git命令结果: #{output}"
      exitstatus = $?.exitstatus
    end
    command_thread.join
  end

  if exitstatus > 1 || (exitstatus == 1 && output != '')
    KCommercial::UI.error "自动cherry-pick失败 #{output}"
  end
  msgs = output.chomp! if output && command_opts[:chomp] && !block_given?
  KCommercial::KCPipeline::KCGitCommandResult.new(exitstatus,msgs)
end

#ks_command_lines(cmd, *opts) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/KCommercialPipeline/core/git.rb', line 89

def ks_command_lines(cmd, *opts)
  rs  = ks_command(cmd, *opts)
  cmd_op = rs[:output] || ""
  if cmd_op.encoding.name != "UTF-8"
    op = cmd_op.encode("UTF-8", "binary", :invalid => :replace, :undef => :replace)
  else
    op = cmd_op
  end
  {"output":op,"exitstatus":rs[:exitstatus]}
end

#parse_output(output) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/KCommercialPipeline/core/git.rb', line 100

def parse_output(output)
  if output.encoding.name != "UTF-8"
    op = output.encode("UTF-8", "binary", :invalid => :replace, :undef => :replace)
  else
    op = output
  end
  op.split('\n')
end