class BigKeeper::GitOperator

Operator for got

Public Class Methods

remote_local_name(path) click to toggle source
# File lib/big_keeper/util/git_operator.rb, line 200
def self.remote_local_name(path)
  Dir.chdir(path) do
    IO.popen("git remote") do |io|
      io.each do |line|
        Logger.error("Check git remote setting.") if line.length == 0
        return line.chomp
      end
    end
  end
end

Public Instance Methods

check_diff(path, branch, compare_branch) click to toggle source

TODO: 需要改造,util方法不应该有业务逻辑

# File lib/big_keeper/util/git_operator.rb, line 212
def check_diff(path, branch, compare_branch)
  compare_branch_commits = Array.new
  IO.popen("cd '#{path}'; git log --left-right #{branch}...#{compare_branch} --pretty=oneline") do |io|
    io.each do |line|
      compare_branch_commits.push(line) if (line.include? '>') && (line.include? "Merge branch #{branch} into #{compare_branch}")
    end
  end
  if compare_branch_commits.size > 0
    compare_branch_commits.map { |item|
        Logger.default(item)
    }
    Logger.highlight("#{compare_branch} branch has commit doesn't committed in #{branch}, please check")
    return false
  else
    Logger.highlight("#{compare_branch} branch doesn't have commit before #{branch}")
    return true
  end
end
check_merge(path, condition) click to toggle source
# File lib/big_keeper/util/git_operator.rb, line 170
def check_merge(path, condition)
  unmerged_branch = Array.new
  IO.popen("cd '#{path}'; git branch --no-merged") do |io|
    io.each do |line|
      unmerged_branch.push(line) if line.include? "#{condition}"
    end
  end
  if (unmerged_branch.size > 0)
    unmerged_branch.map { |item|
        Logger.default(item)
    }
    Logger.error("Still has unmerged feature branch, please check")
  end
end
check_push_success(path, branch, compare_branch) click to toggle source
# File lib/big_keeper/util/git_operator.rb, line 247
def check_push_success(path, branch, compare_branch)
  compare_branch_commits = Array.new
  IO.popen("cd '#{path}'; git log --left-right #{branch}...#{compare_branch} --pretty=oneline") do |io|
    io.each do |line|
      compare_branch_commits.push(line) if (line.include? '>') || (line.include? 'fatal')
    end
  end
  if compare_branch_commits.size > 0
    compare_branch_commits.map { |item|
        Logger.default(item)
    }
    Logger.error("#{branch} branch push unsuccess, please check")
  else
    Logger.highlight("#{branch} branch push success")
  end
end
check_remote_branch_diff(path, branch, compare_branch) click to toggle source
# File lib/big_keeper/util/git_operator.rb, line 185
def check_remote_branch_diff(path, branch, compare_branch)
  fetch(path)
  compare_branch_commits = Array.new
  IO.popen("cd '#{path}';git log --left-right #{branch}...#{GitOperator.remote_local_name(path)}/#{compare_branch} --pretty=oneline") do |io|
    io.each do |line|
      compare_branch_commits.push(line) unless (line.include? '>') && (line.include? "Merge branch \'#{branch}\'")
    end
  end
  if compare_branch_commits.size > 0
    return true
  else
    return false
  end
end
checkout(path, branch_name) click to toggle source
# File lib/big_keeper/util/git_operator.rb, line 42
def checkout(path, branch_name)
  Dir.chdir(path) do
    IO.popen("git checkout #{branch_name}") do |io|
      io.each do |line|
        Logger.error("Checkout #{branch_name} failed.") if line.include? 'error'
      end
    end
  end
end
clone(path, git_base) click to toggle source
# File lib/big_keeper/util/git_operator.rb, line 64
def clone(path, git_base)
  Dir.chdir(path) do
    `git clone #{git_base}`
  end
end
commit(path, message) click to toggle source
# File lib/big_keeper/util/git_operator.rb, line 70
def commit(path, message)
  Dir.chdir(path) do
    `git add .`
    `git commit -m "#{Logger.formatter_output(message)}"`
  end
end
current_branch(path) click to toggle source
# File lib/big_keeper/util/git_operator.rb, line 6
def current_branch(path)
  Dir.chdir(path) do
    `git rev-parse --abbrev-ref HEAD`.chop
  end
end
del_local(path, branch_name) click to toggle source
# File lib/big_keeper/util/git_operator.rb, line 118
def del_local(path, branch_name)
  Dir.chdir(path) do
    `git branch -D #{branch_name}`
  end
end
del_remote(path, branch_name) click to toggle source
# File lib/big_keeper/util/git_operator.rb, line 124
def del_remote(path, branch_name)
  Dir.chdir(path) do
    `git push #{GitOperator.remote_local_name(path)} --delete #{branch_name}`
  end
end
discard(path) click to toggle source
# File lib/big_keeper/util/git_operator.rb, line 112
def discard(path)
  Dir.chdir(path) do
    `git checkout . && git clean -df`
  end
end
fetch(path) click to toggle source
# File lib/big_keeper/util/git_operator.rb, line 52
def fetch(path)
  Dir.chdir(path) do
    `git fetch #{GitOperator.remote_local_name(path)}`
  end
end
has_branch(path, branch_name) click to toggle source
# File lib/big_keeper/util/git_operator.rb, line 32
def has_branch(path, branch_name)
  has_branch = false
  IO.popen("cd '#{path}'; git branch -a") do |io|
    io.each do |line|
      has_branch = true if line.include? branch_name
    end
  end
  has_branch
end
has_changes(path) click to toggle source
# File lib/big_keeper/util/git_operator.rb, line 101
def has_changes(path)
  has_changes = true
  clear_flag = 'nothing to commit, working tree clean'
  IO.popen("cd '#{path}'; git status") do |io|
    io.each do |line|
      has_changes = false if line.include? clear_flag
    end
  end
  has_changes
end
has_commits(path, branch_name) click to toggle source
# File lib/big_keeper/util/git_operator.rb, line 91
def has_commits(path, branch_name)
  has_commits = false
  IO.popen("cd '#{path}'; git log --branches --not --remotes") do |io|
    io.each do |line|
      has_commits = true if line.include? "(#{branch_name})"
    end
  end
  has_commits
end
has_local_branch(path, branch_name) click to toggle source
# File lib/big_keeper/util/git_operator.rb, line 22
def has_local_branch(path, branch_name)
  has_branch = false
  IO.popen("cd '#{path}'; git branch") do |io|
    io.each do |line|
      has_branch = true if line.include? branch_name
    end
  end
  has_branch
end
has_remote_branch(path, branch_name) click to toggle source
# File lib/big_keeper/util/git_operator.rb, line 12
def has_remote_branch(path, branch_name)
  has_branch = false
  IO.popen("cd '#{path}'; git branch -r") do |io|
    io.each do |line|
      has_branch = true if line.include? branch_name
    end
  end
  has_branch
end
merge(path, branch_name) click to toggle source
# File lib/big_keeper/util/git_operator.rb, line 231
def merge(path, branch_name)
  IO.popen("cd '#{path}'; git merge #{branch_name}") do |io|
    io.each do |line|
      Logger.error("Merge conflict in #{line}") if line.include? 'Merge conflict'
    end
  end
end
merge_no_ff(path, branch_name) click to toggle source
# File lib/big_keeper/util/git_operator.rb, line 239
def merge_no_ff(path, branch_name)
  IO.popen("cd '#{path}'; git merge #{branch_name} --no-ff") do |io|
    io.each do |line|
      Logger.error("Merge conflict in #{line}") if line.include? 'Merge conflict'
    end
  end
end
pull(path) click to toggle source
# File lib/big_keeper/util/git_operator.rb, line 84
def pull(path)
  Dir.chdir(path) do
    #  git pull <remote> <branch>
    `git pull #{GitOperator.remote_local_name(path)} #{GitOperator.new.current_branch(path)}`
  end
end
push_to_remote(path, branch_name) click to toggle source
# File lib/big_keeper/util/git_operator.rb, line 77
def push_to_remote(path, branch_name)
  Dir.chdir(path) do
    `git push -u #{GitOperator.remote_local_name(path)} #{branch_name}`
  end
  GitOperator.new.check_push_success(path, branch_name, "#{GitOperator.remote_local_name(path)}/#{branch_name}")
end
rebase(path, branch_name) click to toggle source
# File lib/big_keeper/util/git_operator.rb, line 58
def rebase(path, branch_name)
  Dir.chdir(path) do
    `git rebase #{GitOperator.remote_local_name(path)}/#{branch_name}`
  end
end
tag(path, version) click to toggle source
# File lib/big_keeper/util/git_operator.rb, line 141
def tag(path, version)
  tags = Array.new
  IO.popen("cd '#{path}'; git tag") do |io|
    io.each do |line|
      tags << line
    end
  end
  unless tags.include? "#{version}\n"
    Dir.chdir(path) do
      `git tag -a #{version} -m "release: V #{version}" master;`
      `git push --tags`
    end
    return
  end
  Logger.highlight("tag already exists in the remote, skip this step")
end
tag_list(path) click to toggle source
# File lib/big_keeper/util/git_operator.rb, line 158
def tag_list(path)
  tag_list = Array.new
  IO.popen("cd '#{path}'; git tag -l") do |io|
    io.each do |line|
      unless line=~(/[a-zA-Z]/)
        tag_list << line
      end
    end
  end
  tag_list
end
user() click to toggle source
# File lib/big_keeper/util/git_operator.rb, line 130
def user
  name = `git config user.name`.chop
  cn_reg = /[\u4e00-\u9fa5]{1}/
  cn_arr = name.scan(cn_reg)
  if cn_arr.count > 0
    Logger.error("git config user.name has Chinese character")
  else
    name.gsub(/[^0-9A-Za-z]/, '').downcase
  end
end