require "KCommercialPipeline/core/git" require 'time' require 'git' require 'digest/sha1' module KCommercial module KCPipeline class GitAuthor attr_accessor :author attr_accessor :mail def initialize(author_info) init_author(author_info) end def init_author(author_info) data = author_info.split(" ") @author = data[0] @mail = data[1] || "" end end class KCCommit attr_accessor :commit_id; attr_accessor :ori_commit_id; attr_accessor :sha attr_accessor :message attr_accessor :parent attr_accessor :author attr_accessor :author_info attr_accessor :date def initialize(data) data_c = data.clone data_c.delete("sha") data_c.delete("message") @ori_commit_id = data["sha"].chomp @commit_id = Digest::SHA1.hexdigest(data_c.to_s) @sha = data["sha"].chomp @message = data["message"].chomp @parent = data["parent"] @author_info = data["Author:"].chomp @author = GitAuthor.new(data["Author:"].chomp) @date = Time.parse(data["Date:"]).to_s.chomp end end class Git::Lib #获取两分支间差异 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 end class KCBranchDiffs attr_accessor :source_branch attr_accessor :target_branch def initialize(source_branch,target_branch = "develop", type = "branch") if type == "branch" @source_branch = source_branch.include?("origin/") ? source_branch : "origin/#{source_branch}" @target_branch = target_branch.include?("origin/") ? target_branch : "origin/#{target_branch}" else @source_branch = source_branch @target_branch = target_branch end end def git @git ||= KCGit.git end def diffs #源分支差异 source_branch_diff = git.lib.diff_log_between_source_branch_target_branch(@source_branch,@target_branch).map { |c| KCCommit.new(c) } #目标分支差异 target_branch_diff = git.lib.diff_log_between_source_branch_target_branch(@target_branch,@source_branch).map { |c| KCCommit.new(c) } target_branch_diff_map = {} #目标分支差异转成hash target_branch_diff.each do |c| target_branch_diff_map[c.commit_id] = c end #获取不在目标分支的所有commit diffs = source_branch_diff.find_all{|c| target_branch_diff_map[c.commit_id].nil? } end end end end