class GitVcsClient::Git

Wrapper around the git commands rubocop:disable Metrics/ClassLength

Public Class Methods

new(logger) click to toggle source
# File lib/plugins/clients/git_vcs_client.rb, line 12
def initialize(logger)
  @logger = logger
end

Public Instance Methods

branch?(name) click to toggle source
# File lib/plugins/clients/git_vcs_client.rb, line 24
def branch?(name)
  res = git("branch", log_level: :silent)
  res.stdout.include?(name) || res.stdout.include?("* #{name}")
end
branch_locally(start_commit, branch_name) click to toggle source
# File lib/plugins/clients/git_vcs_client.rb, line 16
def branch_locally(start_commit, branch_name)
  git "branch #{branch_name} #{start_commit}"
end
checkout(target_branch) click to toggle source
# File lib/plugins/clients/git_vcs_client.rb, line 20
def checkout(target_branch)
  git "checkout #{target_branch}"
end
continue_rebase() click to toggle source
# File lib/plugins/clients/git_vcs_client.rb, line 67
def continue_rebase
  !git("rebase --continue").stderr.include? "needs merge"
end
current_branch() click to toggle source
# File lib/plugins/clients/git_vcs_client.rb, line 29
def current_branch
  git("rev-parse --abbrev-ref HEAD", log_level: :silent).stdout.first.strip
end
delete_local_branch(branch) click to toggle source
# File lib/plugins/clients/git_vcs_client.rb, line 88
def delete_local_branch(branch)
  git "branch -D #{branch}"
end
delete_remote_branch(remote, branch) click to toggle source
# File lib/plugins/clients/git_vcs_client.rb, line 83
def delete_remote_branch(remote, branch)
  delete_local_branch branch
  git "push #{remote} :#{branch}"
end
diverged?(base_branch, commit) click to toggle source
# File lib/plugins/clients/git_vcs_client.rb, line 50
def diverged?(base_branch, commit)
  diverged_count(base_branch, commit) > 0
end
diverged_count(base_branch = "master", commit = "HEAD") click to toggle source
# File lib/plugins/clients/git_vcs_client.rb, line 54
def diverged_count(base_branch = "master", commit = "HEAD")
  git("rev-list #{base_branch}..#{commit} --count", log_level: :silent).stdout.first.to_i
end
diverged_list(base_branch, commit) click to toggle source
# File lib/plugins/clients/git_vcs_client.rb, line 58
def diverged_list(base_branch, commit)
  git("log #{base_branch}..#{commit} --pretty=format:'- %h %s by %cN <%cE>'",
      log_level: :silent).stdout
end
fetch() click to toggle source
# File lib/plugins/clients/git_vcs_client.rb, line 33
def fetch
  git "fetch"
end
git(command, options = {}) click to toggle source
# File lib/plugins/clients/git_vcs_client.rb, line 110
def git(command, options = {})
  git_command = "git #{command}"

  case options[:log_level]
  when :silent
    run git_command
  else
    inf "Run: '#{git_command}'"
    run git_command do | stdout, stderr, _thread|
      inf "[git-out]: #{stdout}" if stdout
      err "[git-err]: #{stderr}" if stderr
    end
  end
end
launch_merge_conflict_tool() click to toggle source
# File lib/plugins/clients/git_vcs_client.rb, line 71
def launch_merge_conflict_tool
  Process.wait(spawn("git mergetool --no-prompt"))
end
merge_fast_forward_only(branch) click to toggle source
# File lib/plugins/clients/git_vcs_client.rb, line 96
def merge_fast_forward_only(branch)
  git "merge --ff-only #{branch}"
end
pull(remote, branch, remote_branch = nil) click to toggle source
# File lib/plugins/clients/git_vcs_client.rb, line 46
def pull(remote, branch, remote_branch = nil)
  git "pull #{remote} #{branch}:#{remote_branch || branch}"
end
push(remote, branch, remote_branch = nil) click to toggle source
# File lib/plugins/clients/git_vcs_client.rb, line 75
def push(remote, branch, remote_branch = nil)
  git "push #{remote} #{branch}:#{remote_branch || branch}"
end
push_force(remote, branch, remote_branch = nil) click to toggle source
# File lib/plugins/clients/git_vcs_client.rb, line 79
def push_force(remote, branch, remote_branch = nil)
  git "push #{remote} #{branch}:#{remote_branch || branch} --force"
end
rebase_onto(branch = "master") click to toggle source
# File lib/plugins/clients/git_vcs_client.rb, line 63
def rebase_onto(branch = "master")
  !(git("rebase #{branch}").stderr.include?("\nCONFLICT"))
end
reset_head(commit) click to toggle source
# File lib/plugins/clients/git_vcs_client.rb, line 92
def reset_head(commit)
  git "reset --keep #{commit}"
end
server_availability?(remote) click to toggle source
# File lib/plugins/clients/git_vcs_client.rb, line 105
def server_availability?(remote)
  # TODO: Check for positive case instead
  !git("ls-remote #{remote}", log_level: :silent).stderr.include? "fatal: unable to access"
end
squash_branch(number_of_commits, message) click to toggle source
# File lib/plugins/clients/git_vcs_client.rb, line 100
def squash_branch(number_of_commits, message)
  git "reset --soft HEAD~#{number_of_commits}"
  git "commit -m '#{message}'"
end
uncommitted_changes() click to toggle source
# File lib/plugins/clients/git_vcs_client.rb, line 37
def uncommitted_changes
  git("status --porcelain", log_level: :silent).stdout.map do |change|
    VcsFile.new(
      File.absolute_path(change.split(" ")[1]),
      parse_status(change.slice(0..1))
    )
  end
end

Private Instance Methods

parse_char(char) click to toggle source

rubocop:disable Metrics/CyclomaticComplexity

# File lib/plugins/clients/git_vcs_client.rb, line 132
def parse_char(char)
  case char
  when "?" then return :untracked
  when "M" then return :modified
  when "A" then return :added
  when "D" then return :deleted
  when "R" then return :renamed
  when "C" then return :copied
  when "U" then return :updated
  else return :none
  end
end
parse_status(status_str) click to toggle source
# File lib/plugins/clients/git_vcs_client.rb, line 127
def parse_status(status_str)
  { staged: parse_char(status_str[0]), unstaged: parse_char(status_str[1]) }
end