class DTK::Client::Operation::ClientModuleDir::GitRepo::Internal

All Internal methods do not have wrap_operation and can only be accessed by a method that wraps it

Constants

CHECKOUT_LOCK

Public Class Methods

add_remote(repo, remote_name, remote_url) click to toggle source
# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 45
def self.add_remote(repo, remote_name, remote_url)
  repo.add_remote(remote_name, remote_url)
  remote_name
end
add_remote_and_push(repo, repo_url, remote_branch, opts = {}) click to toggle source
# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 247
def self.add_remote_and_push(repo, repo_url, remote_branch, opts = {})
  remote = opts[:remote] || Dtk_Server::GIT_REMOTE
  repo.add_remote(remote, repo_url)
  repo.stage_and_commit
  repo.push(remote, remote_branch, { :force => true })
end
add_service_repo_file(args) click to toggle source
# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 254
def self.add_service_repo_file(args)
  branch           = args.required(:branch)
  service_instance = args.required(:service_instance)
  path             = args.required(:path)
  content          = args.required(:content)
  checkout_branch_on_service_instance(service_instance, branch) do
    File.open(qualified_path(service_instance, path), 'w') { |f| f.write(content) }
  end
end
all_branches(args) click to toggle source
# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 277
def self.all_branches(args)
  repo_url = args.required(:path)
  repo = git_repo.new(repo_url)
  repo.all_branches
end
clone(repo_url, target_repo_dir, branch) click to toggle source
# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 114
def self.clone(repo_url, target_repo_dir,  branch)
  begin
    git_repo.clone(repo_url, target_repo_dir,  branch)
  rescue => e
    FileUtils.rm_rf(target_repo_dir) if File.directory?(target_repo_dir)
    Logger.instance.error_pp(e.message, e.backtrace)

    raise Error::Usage, "Clone to directory '#{target_repo_dir}' failed"
  end

  target_repo_dir
end
clone_module_repo(args) click to toggle source
# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 127
def self.clone_module_repo(args)
  module_type     = args.required(:module_type)
  repo_url        = args.required(:repo_url)
  branch          = args.required(:branch)
  module_name     = args.required(:module_name)
  remove_existing = args[:remove_existing]
  repo_dir        = args[:repo_dir]

  target_repo_dir = create_module_dir(module_type, module_name, :remove_existing => remove_existing, :path => repo_dir)
  clone(repo_url, target_repo_dir, branch)
  target_repo_dir
end
commit_and_push_to_nested_module_repo(args) click to toggle source
# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 98
def self.commit_and_push_to_nested_module_repo(args)
  target_repo_dir = args.required(:target_repo_dir)
  repo            = git_repo.new(target_repo_dir)
  branch          = args[:branch] || repo.current_branch
  remote_branch   = args[:remote_branch] || branch

  if repo.changed?
    repo.stage_and_commit
    dtk_server_remote = 'origin'
    repo.push(dtk_server_remote, branch)
    return repo.head_commit_sha
  end

  nil
end
commit_and_push_to_service_repo(args) click to toggle source

returns head_sha

# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 82
def self.commit_and_push_to_service_repo(args)
  branch           = args.required(:branch)
  remote_branch    = args[:remote_branch] || branch
  service_instance = args.required(:service_instance)
  commit_message   = args[:commit_message]

  #repo_dir = ret_base_path(:service, service_instance)
  repo_dir = args[:service_instance_dir] || ret_base_path(:service, service_instance)
  repo = git_repo.new(repo_dir, :branch => branch)
  repo.stage_and_commit
  # TODO: want to switch over to using Dtk_Server::GIT_REMOTE rather than 'origin'
  dtk_server_remote = 'origin'
  repo.push(dtk_server_remote, remote_branch)
  repo.head_commit_sha
end
create_add_remote_and_push(repo_dir, repo_url, remote_branch) click to toggle source
# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 171
def self.create_add_remote_and_push(repo_dir, repo_url, remote_branch)
  repo = git_repo.new(repo_dir)
  add_remote_and_push(repo, repo_url, remote_branch)
  repo.head_commit_sha
end
create_empty_git_repo?(repo_dir, opts = {}) click to toggle source

opts can have keys

:branch

returns object of type DTK::Client::GitRepo

# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 35
def self.create_empty_git_repo?(repo_dir, opts = {})
  git_repo.new(repo_dir, :branch => opts[:branch])
end
create_repo_from_remote_and_push(repo_dir, repo_url, remote_branch) click to toggle source
# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 237
def self.create_repo_from_remote_and_push(repo_dir, repo_url, remote_branch)
  repo = create_repo_from_server_remote(repo_dir, repo_url, remote_branch)
  # repo = git_repo.new(repo_dir, :branch => Dtkn::LOCAL_BRANCH)
  # repo.checkout(Dtkn::LOCAL_BRANCH, :new_branch => true)
  # repo.add_remote(Dtk_Server::GIT_REMOTE, repo_url)
  repo.stage_and_commit
  repo.push(Dtk_Server::GIT_REMOTE, remote_branch, { :force => true })
  repo.head_commit_sha
end
create_repo_from_server_remote(repo_dir, repo_url, remote_branch) click to toggle source
# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 230
def self.create_repo_from_server_remote(repo_dir, repo_url, remote_branch)
  repo = git_repo.new(repo_dir, :branch => Dtkn::LOCAL_BRANCH)
  repo.checkout(Dtkn::LOCAL_BRANCH, :new_branch => true)
  repo.add_remote(Dtk_Server::GIT_REMOTE, repo_url)
  repo
end
current_branch(args) click to toggle source
# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 283
def self.current_branch(args)
  repo_url = args.required(:path)
  repo = git_repo.new(repo_url)
  repo.current_branch.name
end
empty_commit(repo, commit_msg = nil) click to toggle source

returns head_sha

# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 40
def self.empty_commit(repo, commit_msg = nil)
  repo.empty_commit(commit_msg)
  repo.head_commit_sha
end
fetch(repo, remote_name) click to toggle source
# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 50
def self.fetch(repo, remote_name)
  repo.fetch(remote_name)
end
fetch_merge_and_push(args) click to toggle source
# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 156
def self.fetch_merge_and_push(args)
  repo_dir      = args.required(:repo_dir)
  repo_url      = args.required(:repo_url)
  remote_branch = args.required(:branch)
  
  head_sha =
    if git_repo.is_git_repo?(repo_dir)
      init_and_push_from_existing_repo(repo_dir, repo_url, remote_branch)
    else
      create_repo_from_remote_and_push(repo_dir, repo_url, remote_branch)
    end
  
  head_sha 
end
get_service_repo_file_content(args) click to toggle source

returns content if file exists

# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 265
def self.get_service_repo_file_content(args)
  branch           = args.required(:branch)
  service_instance = args.required(:service_instance)
  path             = args.required(:path)
  checkout_branch_on_service_instance(service_instance, branch) do
    qualified_path = qualified_path(service_instance, path)
    if File.exists?(qualified_path)
      File.open(qualified_path, 'r').read
    end
  end
end
git_repo() click to toggle source
# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 289
def self.git_repo
  ::DTK::Client::GitRepo
end
init_and_push_from_existing_repo(repo_dir, repo_url, remote_branch, opts = {}) click to toggle source

TODO: DTK-2765: see what this does and subsume by create_add_remote_and_push For this and other methods in Internal that use Dtk_Server::GIT_REMOTE put a version in Internal taht takes remote_name as param and then have method with same name in Dtk, that calss this with appropriate remote name

# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 181
def self.init_and_push_from_existing_repo(repo_dir, repo_url, remote_branch, opts = {})
  repo = git_repo.new(repo_dir)
  remote = opts[:remote] || Dtk_Server::GIT_REMOTE
  
  if repo.is_there_remote?(remote)
    push_when_there_is_dtk_remote(repo, repo_dir, repo_url, remote_branch, opts)
  else
    add_remote_and_push(repo, repo_url, remote_branch, opts)
  end
  
  repo.head_commit_sha
end
local_ahead?(repo, merge_from_ref, opts = {}) click to toggle source
# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 64
def self.local_ahead?(repo, merge_from_ref, opts = {})
  base_sha = repo.head_commit_sha
  remote_branch = repo.all_branches.remote.find { |r| "#{r.remote}/#{r.name}" == merge_from_ref }
  remote_sha = remote_branch.gcommit.sha
  repo.local_ahead(base_sha, remote_sha)
end
merge(repo, merge_from_ref, opts = {}) click to toggle source

opts can have keys

:no_commit
# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 56
def self.merge(repo, merge_from_ref, opts = {})
  base_sha = repo.head_commit_sha
  repo.merge(merge_from_ref, :use_theirs => opts[:use_theirs])
  # the git gem does not take no_commit as merge argument; so doing it with soft reset
  repo.reset_soft(base_sha) if opts[:no_commit]
  repo.head_commit_sha
end
modified(args, opts = {}) click to toggle source

opts can have keys:

:with_diffs (Boolean)
# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 142
def self.modified(args, opts = {})
  repo_url = args.required(:path)
  branch   = args.required(:branch)
  repo     = git_repo.new(repo_url, :branch => branch)
  command  = args.required(:command)

  original_path = Dir.pwd
  Dir.chdir(args[:path])
  changed = repo.changed?
  repo.print_status(:with_diffs => opts[:with_diffs], :command => command) if changed
  Dir.chdir(original_path)
  changed
end
pull_from_remote(args) click to toggle source
# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 194
def self.pull_from_remote(args)
  repo_url       = args.required(:repo_url)
  remote_branch  = args.required(:branch)
  repo_dir       = args.required(:repo_dir)
  
  repo = git_repo.new(repo_dir, :branch => remote_branch)
  repo.pull(repo.remotes.first, remote_branch)
end
pull_from_service_repo(args) click to toggle source

returns the repo

# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 204
def self.pull_from_service_repo(args)
  repo_url         = args.required(:repo_url)
  remote_branch    = args.required(:branch)
  service_instance = args.required(:service_instance)

  #repo_dir = ret_base_path(:service, service_instance)
  repo_dir = args[:service_instance_dir] || ret_base_path(:service, service_instance)
  repo = git_repo.new(repo_dir, :branch => remote_branch)
  
  repo.pull(repo.remotes.first, remote_branch)
  repo
end
push_when_there_is_dtk_remote(repo, repo_dir, repo_url, remote_branch, opts = {}) click to toggle source
# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 217
def self.push_when_there_is_dtk_remote(repo, repo_dir, repo_url, remote_branch, opts = {})
  # if there is only one remote and it is dtk-server; remove .git and initialize and push as new repo to dtk-server remote
  # else if multiple remotes and dtk-server being one of them; remove dtk-server; add new dtk-server remote and push
  if repo.remotes.size == 1
    git_repo.unlink_local_clone?(repo_dir)
    create_repo_from_remote_and_push(repo_dir, repo_url, remote_branch)
  else
    remote = opts[:remote] || Dtk_Server::GIT_REMOTE
    repo.remove_remote(remote)
    add_remote_and_push(repo, repo_url, remote_branch, opts)
  end
end
reset_hard(repo, merge_from_ref) click to toggle source
# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 293
def self.reset_hard(repo, merge_from_ref)
  repo.reset_hard(merge_from_ref)
  repo.head_commit_sha
end
stage_and_commit(repo_dir,local_branch_type, opts = {}) click to toggle source

opts can have keys:

:commit_msg

returns head_sha

# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 74
def self.stage_and_commit(repo_dir,local_branch_type, opts = {})
  local_branch = branch_from_local_branch_type(local_branch_type)
  repo = create_empty_git_repo?(repo_dir, :branch => local_branch)
  repo.stage_and_commit(opts[:commit_msg])
  repo.head_commit_sha
end

Private Class Methods

branch_from_local_branch_type(local_branch_type) click to toggle source
# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 300
def self.branch_from_local_branch_type(local_branch_type)
 case local_branch_type
 when :dtkn then Dtkn.local_branch
 else
   raise Error, "Illegal local_branch_type '#{local_branch_type}'"
 end
end
checkout_branch(repo, branch_to_checkout, opts = {}) { || ... } click to toggle source

opts can have keys

:current_branch
# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 322
def self.checkout_branch(repo, branch_to_checkout, opts = {}, &block)
  ret = nil
  CHECKOUT_LOCK.synchronize do
    current_branch = opts[:current_branch] || repo.current_branch.name
    if current_branch == branch_to_checkout
      ret = reset_if_error(repo, branch_to_checkout) { yield }
    else
      #TODO: DTK-2922: temporary fix until can solve problem described in note below
      unless repo.repo_dir == Dir.pwd
        raise Error::Usage, "This command must be invoked from the base module directory '#{repo.repo_dir}'"
      end

      begin
        repo.checkout(branch_to_checkout) 
        ret = reset_if_error(repo, branch_to_checkout) { yield }
      ensure
        repo.checkout(current_branch)
      end
    end
  end
  ret
end
checkout_branch_on_service_instance(service_instance, branch, &body) click to toggle source
# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 314
def self.checkout_branch_on_service_instance(service_instance, branch, &body)
  repo = git_repo.new(ret_base_path(:service, service_instance), :branch => branch)
  checkout_branch(repo, branch, {}, &body)
end
qualified_path(service_instance, relative_path) click to toggle source

relative_path is relative to top-leel repo directory

# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 309
def self.qualified_path(service_instance, relative_path)
  repo_dir = ret_base_path(:service, service_instance)
  "#{repo_dir}/#{relative_path}"
end
reset_if_error(repo, branch) { || ... } click to toggle source

TODO: DTK-2922: tried below for above; it worked aside from fact that dircetory is empty and have to step down and step up

Dir.chdir is done to avoid case where current directory does not exist in branch checking out
Dir.chdir(repo.repo_dir) do
  begin
    repo.checkout(branch_to_checkout) 
    ret = reset_if_error(repo, branch_to_checkout) { yield }
  ensure
    repo.checkout(current_branch)
  end
end
# File lib/client/operation/client_module_dir/git_repo/internal.rb, line 356
def self.reset_if_error(repo, branch, &block)
  ret = nil
  begin
    sha_before_operations =  repo.revparse(branch)
    ret = yield
  rescue => e
    # reset to enable checkout of another branch
    repo.add_all
    repo.reset_hard(sha_before_operations)
    raise e
  end
  ret
end