class DTK::Client::GitRepo::Adapter::GitGem

Attributes

git_repo[RW]

Public Class Methods

clone(repo_url, target_path, branch) click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 96
def self.clone(repo_url, target_path, branch)
  git_base = handle_git_error { ::Git.clone(repo_url, target_path) }
  begin
    git_base.checkout(branch)
  rescue => e
    # TODO: see if any other kind of error
    raise Error::Usage, "The branch or tag '#{branch}' does not exist on repo '#{repo_url}'"
  end
  git_base
end
new(repo_dir, opts = {}) click to toggle source

opts can have keys

:branch
# File lib/client/git_repo/adapter/git_gem.rb, line 88
def initialize(repo_dir, opts = {})
  @repo_dir = repo_dir
  @git_repo = ::Git.init(repo_dir)
  # If we want to log Git interaction
  # @git_repo = ::Git.init(repo_dir, :log => Logger.new(STDOUT))
  @local_branch_name = opts[:branch]
end

Public Instance Methods

add(*files) click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 224
def add(*files)
  @git_repo.add(files.flatten)
end
add_all() click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 228
def add_all
  # Cannot use '@git_repo.add(:all => true)' because this only works if pwd is base git repo
  fully_qualified_repo_dir = (@repo_dir =~ /^\// ? @repo_dir : File.join(Dir.pwd, @repo_dir))
  @git_repo.add(fully_qualified_repo_dir, :all => true )
end
add_remote(name, url) click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 119
def add_remote(name, url)
  @git_repo.remove_remote(name) if is_there_remote?(name)
  @git_repo.add_remote(name, url)
end
added() click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 161
def added
  status.is_a?(Hash) ? status.added().keys : status.added().collect { |file| file.first }
end
all_branches() click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 276
def all_branches
  @git_repo.branches
end
changed() click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 147
def changed
  status.is_a?(Hash) ? status.changed().keys : status.changed().collect { |file| file.first }
end
changed?() click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 262
def changed?
  (!(changed().empty? && untracked().empty? && deleted().empty?))
end
checkout(branch, opts = {}) click to toggle source

opts can have keys

:new_branch - Boolean
# File lib/client/git_repo/adapter/git_gem.rb, line 109
def checkout(branch, opts = {})
  ret = @git_repo.checkout(branch, opts)
  @local_branch_name = branch
  ret
end
commit(commit_msg = "", opts = {}) click to toggle source

opts can have keys

:allow_empty
# File lib/client/git_repo/adapter/git_gem.rb, line 220
def commit(commit_msg = "", opts = {})
  @git_repo.commit(commit_msg, :allow_empty => opts[:allow_empty])
end
current_branch() click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 238
def current_branch
  @git_repo.branches.local.find { |b| b.current }
end
deleted() click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 157
def deleted
  status.is_a?(Hash) ? status.deleted().keys : status.deleted().collect { |file| file.first }
end
diff() click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 254
def diff
  @git_repo.diff
end
diff_name_status(branch_or_sha_1, branch_or_sha_2, opts = {}) click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 258
def diff_name_status(branch_or_sha_1, branch_or_sha_2, opts = {})
  @git_repo.name_status(branch_or_sha_1, branch_or_sha_2, opts)
end
empty_commit(commit_msg = nil) click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 175
def empty_commit(commit_msg = nil)
  commit_msg ||= default_commit_message
  commit(commit_msg, :allow_empty => true)
end
fetch(remote = 'origin') click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 115
def fetch(remote = 'origin')
  @git_repo.fetch(remote)
end
head_commit_sha() click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 246
def head_commit_sha
  current_branch.gcommit.sha
end
is_there_remote?(remote_name) click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 234
def is_there_remote?(remote_name)
  @git_repo.remotes.find { |r| r.name == remote_name }
end
local_ahead(base_sha, remote_sha) click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 196
def local_ahead(base_sha, remote_sha)
  results = @git_repo.rev_list(base_sha)
  !results.split("\n").grep(remote_sha).empty?
end
merge(branch_to_merge_from, opts = {}) click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 139
def merge(branch_to_merge_from, opts = {})
  @git_repo.merge(branch_to_merge_from, 'merge', :allow_unrelated_histories => allow_unrelated_histories?, :use_theirs => opts[:use_theirs])
end
print_status(opts = {}) click to toggle source

opts can have keys:

:with_diffs (Boolean)
pull(remote, branch) click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 250
def pull(remote, branch)
  @git_repo.pull(remote, branch)
end
push(remote, branch, opts = {}) click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 128
def push(remote, branch, opts = {})
  branch_name = current_branch ? current_branch.name : 'master'
  branch_for_push = "#{branch_name}:refs/heads/#{branch || local_branch_name}"
  @git_repo.push(remote, branch_for_push, opts)
end
push_from_cached_branch(remote, branch, opts = {}) click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 134
def push_from_cached_branch(remote, branch, opts = {})
  branch_for_push = "HEAD:#{branch}"
  @git_repo.push(remote, branch_for_push, opts)
end
remotes() click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 242
def remotes
  @git_repo.remotes
end
remove_remote(name) click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 124
def remove_remote(name)
  @git_repo.remove_remote(name) if is_there_remote?(name)
end
reset_hard(sha) click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 184
def reset_hard(sha)
  @git_repo.reset_hard(sha)
end
reset_soft(sha) click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 180
def reset_soft(sha)
  @git_repo.reset(sha)
end
rev_list(base_sha) click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 192
def rev_list(base_sha)
  @git_repo.rev_list(base_sha)
end
revparse(sha_or_string) click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 188
def revparse(sha_or_string)
  @git_repo.revparse(sha_or_string)
end
stage_and_commit(commit_msg = nil) click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 165
def stage_and_commit(commit_msg = nil)
  commit_msg ||= default_commit_message
  add_all
  begin
    commit(commit_msg)
  rescue
    # do not raise if nothing to commit
  end
end
stage_changes() click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 201
def stage_changes()
  handle_git_error do
    @git_repo.add(untracked())
    @git_repo.add(added())
    @git_repo.add(changed())
  end
  deleted().each do |file|
    begin
      @git_repo.remove(file)
    rescue
      # ignore this error means file has already been staged
      # we cannot support status of file, in 1.8.7 so this is
      # solution for that
    end
  end
end
status() click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 143
def status
  @git_repo.status
end
untracked() click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 151
def untracked
  @git_repo.lib.command_lines('ls-files', ['--others', '--exclude-standard'])
  # Replaced below with above because did not take into account what was in .gitignore
  # status.is_a?(Hash) ? status.untracked().keys : status.untracked().collect { |file| file.first }
end

Private Instance Methods

allow_unrelated_histories?() click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 320
def allow_unrelated_histories?
  git_version_output = `git --version`
  git_version = git_version_output.split.last
  # --allow-unrelated_histories was introduced in git 2.9
  Gem::Version.new(git_version) >= Gem::Version.new('2.9.0')
end
default_commit_message() click to toggle source
# File lib/client/git_repo/adapter/git_gem.rb, line 316
def default_commit_message
  "DTK Commit from client"
end
print_status_simple() click to toggle source
print_status_with_diffs(opts) click to toggle source