class ChefCLI::CookbookProfiler::Git

Attributes

cookbook_path[R]

Public Class Methods

new(cookbook_path) click to toggle source
# File lib/chef-cli/cookbook_profiler/git.rb, line 28
def initialize(cookbook_path)
  @cookbook_path = cookbook_path
  @unborn_branch = nil
  @unborn_branch_ref = nil
end

Public Instance Methods

clean?() click to toggle source
# File lib/chef-cli/cookbook_profiler/git.rb, line 63
def clean?
  git!("diff-files --quiet", returns: [0, 1]).exitstatus == 0
end
current_branch() click to toggle source
# File lib/chef-cli/cookbook_profiler/git.rb, line 101
def current_branch
  @current_branch ||= detect_current_branch
end
have_remote?() click to toggle source
# File lib/chef-cli/cookbook_profiler/git.rb, line 97
def have_remote?
  !remote_name.empty? && remote_name != "."
end
profile_data() click to toggle source

@return [Hash] Hashed used for pinning cookbook versions within a Policyfile.lock

# File lib/chef-cli/cookbook_profiler/git.rb, line 35
def profile_data
  {
    "scm" => "git",
    # To get this info, you need to do something like:
    # figure out branch or assume 'main'
    # git config --get branch.main.remote
    # git config --get remote.opscode.url
    "remote" => remote,
    "revision" => revision,
    "working_tree_clean" => clean?,
    "published" => !unpublished_commits?,
    "synchronized_remote_branches" => synchronized_remotes,
  }
end
remote() click to toggle source
# File lib/chef-cli/cookbook_profiler/git.rb, line 84
def remote
  @remote_url ||=
    if have_remote?
      git!("config --get remote.#{remote_name}.url").stdout.strip
    else
      nil
    end
end
remote_name() click to toggle source
# File lib/chef-cli/cookbook_profiler/git.rb, line 93
def remote_name
  @remote_name ||= git!("config --get branch.#{current_branch}.remote", returns: [0, 1]).stdout.strip
end
revision() click to toggle source
# File lib/chef-cli/cookbook_profiler/git.rb, line 50
def revision
  git!("rev-parse HEAD").stdout.strip
rescue Mixlib::ShellOut::ShellCommandFailed => e
  # We may have an "unborn" branch, i.e. one with no commits.
  if unborn_branch_ref
    nil
  else
    # if we got here, but verify_ref_cmd didn't error, we don't know why
    # the original git command failed, so re-raise.
    raise e
  end
end
synchronized_remotes() click to toggle source
# File lib/chef-cli/cookbook_profiler/git.rb, line 71
def synchronized_remotes
  @synchronized_remotes ||= git!("branch -r --contains #{revision}").stdout.lines.map(&:strip)
rescue Mixlib::ShellOut::ShellCommandFailed => e
  # We may have an "unborn" branch, i.e. one with no commits.
  if unborn_branch_ref
    []
  else
    # if we got here, but verify_ref_cmd didn't error, we don't know why
    # the original git command failed, so re-raise.
    raise e
  end
end
unpublished_commits?() click to toggle source
# File lib/chef-cli/cookbook_profiler/git.rb, line 67
def unpublished_commits?
  synchronized_remotes.empty?
end

Private Instance Methods

detect_current_branch() click to toggle source
# File lib/chef-cli/cookbook_profiler/git.rb, line 118
def detect_current_branch
  branch = git!("rev-parse --abbrev-ref HEAD").stdout.strip
  @unborn_branch = false
  branch
rescue Mixlib::ShellOut::ShellCommandFailed => e
  # "unborn" branch, i.e. one with no commits or
  # verify_ref_cmd didn't error, we don't know why
  # the original git command failed, so re-raise.
  unborn_branch_ref || raise(e)
end
git(subcommand, options = {}) click to toggle source
# File lib/chef-cli/cookbook_profiler/git.rb, line 113
def git(subcommand, options = {})
  options = { cwd: cookbook_path }.merge(options)
  system_command("git #{subcommand}", options)
end
git!(subcommand, options = {}) click to toggle source
# File lib/chef-cli/cookbook_profiler/git.rb, line 107
def git!(subcommand, options = {})
  cmd = git(subcommand, options)
  cmd.error!
  cmd
end
unborn_branch_ref() click to toggle source
# File lib/chef-cli/cookbook_profiler/git.rb, line 129
def unborn_branch_ref
  @unborn_branch_ref ||=
    begin
      strict_branch_ref = git!("symbolic-ref -q HEAD").stdout.strip
      verify_ref_cmd = git("show-ref --verify #{strict_branch_ref}")
      if verify_ref_cmd.error?
        @unborn_branch = true
        strict_branch_ref
      else
        # if we got here, but verify_ref_cmd didn't error, then `git
        # rev-parse` is probably failing for a reason we haven't anticipated.
        # Calling code should detect this and re-raise.
        nil
      end
    end
end