Class: OctocatalogDiff::CatalogUtil::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/octocatalog-diff/catalog-util/git.rb

Overview

Class to perform a git checkout (via ‘git archive’) of a branch from the base git directory into another targeted directory.

Class Method Summary collapse

Class Method Details

.branch_sha(options = {}) ⇒ Object

Determine the SHA of origin/master (or any other branch really) in the git repo

Parameters:

  • options (Hash) (defaults to: {})

    Options hash:

    • :branch => Branch name to determine SHA of

    • :basedir => Where to check out from (must exist as a directory)



61
62
63
64
65
66
67
68
69
# File 'lib/octocatalog-diff/catalog-util/git.rb', line 61

def self.branch_sha(options = {})
  branch = options.fetch(:branch)
  dir = options.fetch(:basedir)
  if dir.nil? || !File.directory?(dir)
    raise Errno::ENOENT, "Git directory #{dir.inspect} does not exist"
  end
  repo = Rugged::Repository.new(dir)
  repo.branches[branch].target_id
end

.check_out_git_archive(options = {}) ⇒ Object

Check out a branch via ‘git archive’ from one directory into another.

Parameters:

  • options (Hash) (defaults to: {})

    Options hash:

    • :branch => Branch name to check out

    • :path => Where to check out to (must exist as a directory)

    • :basedir => Where to check out from (must exist as a directory)

    • :logger => Logger object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/octocatalog-diff/catalog-util/git.rb', line 19

def self.check_out_git_archive(options = {})
  branch = options.fetch(:branch)
  path = options.fetch(:path)
  dir = options.fetch(:basedir)
  logger = options.fetch(:logger)
  override_script_path = options.fetch(:override_script_path, nil)

  # Validate parameters
  if dir.nil? || !File.directory?(dir)
    raise OctocatalogDiff::Errors::GitCheckoutError, "Source directory #{dir.inspect} does not exist"
  end
  if path.nil? || !File.directory?(path)
    raise OctocatalogDiff::Errors::GitCheckoutError, "Target directory #{path.inspect} does not exist"
  end

  # Create and execute checkout script
  sr_opts = {
    logger: logger,
    default_script: 'git-extract/git-extract.sh',
    override_script_path: override_script_path
  }
  script = OctocatalogDiff::Util::ScriptRunner.new(sr_opts)

  sr_run_opts = {
    :working_dir             => dir,
    :pass_env_vars           => options[:pass_env_vars],
    'OCD_GIT_EXTRACT_BRANCH' => branch,
    'OCD_GIT_EXTRACT_TARGET' => path
  }

  begin
    script.run(sr_run_opts)
    logger.debug("Success git archive #{dir}:#{branch}")
  rescue OctocatalogDiff::Util::ScriptRunner::ScriptException
    raise OctocatalogDiff::Errors::GitCheckoutError, "Git archive #{branch}->#{path} failed: #{script.output}"
  end
end