Class: ChefCLI::CookbookProfiler::Git

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/chef-cli/cookbook_profiler/git.rb

Constant Summary collapse

@@git_memo =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#err, #git_bin_dir, #git_windows_bin_dir, #msg, #omnibus_bin_dir, #omnibus_embedded_bin_dir, #omnibus_env, #omnibus_expand_path, #omnibus_install?, #omnibus_root, #package_home, #stderr, #stdout, #system_command, #usr_bin_path, #usr_bin_prefix

Constructor Details

#initialize(cookbook_path) ⇒ Git

Returns a new instance of Git.



34
35
36
37
38
39
# File 'lib/chef-cli/cookbook_profiler/git.rb', line 34

def initialize(cookbook_path)
  @cookbook_path = cookbook_path
  @repo_path = nil
  @unborn_branch = nil
  @unborn_branch_ref = nil
end

Instance Attribute Details

#cookbook_pathObject (readonly)

Returns the value of attribute cookbook_path.



28
29
30
# File 'lib/chef-cli/cookbook_profiler/git.rb', line 28

def cookbook_path
  @cookbook_path
end

Class Method Details

.uncacheObject



30
31
32
# File 'lib/chef-cli/cookbook_profiler/git.rb', line 30

def self.uncache
  @@git_memo = {}
end

Instance Method Details

#clean?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/chef-cli/cookbook_profiler/git.rb', line 70

def clean?
  git!("diff-files --quiet", returns: [0, 1]).exitstatus == 0
end

#current_branchObject



108
109
110
# File 'lib/chef-cli/cookbook_profiler/git.rb', line 108

def current_branch
  @current_branch ||= detect_current_branch
end

#have_remote?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/chef-cli/cookbook_profiler/git.rb', line 104

def have_remote?
  !remote_name.empty? && remote_name != "."
end

#profile_dataHash

Returns Hashed used for pinning cookbook versions within a Policyfile.lock.

Returns:

  • (Hash)

    Hashed used for pinning cookbook versions within a Policyfile.lock



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/chef-cli/cookbook_profiler/git.rb', line 42

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

#remoteObject



91
92
93
94
95
96
97
98
# File 'lib/chef-cli/cookbook_profiler/git.rb', line 91

def remote
  @remote_url ||=
    if have_remote?
      git!("config --get remote.#{remote_name}.url").stdout.strip
    else
      nil
    end
end

#remote_nameObject



100
101
102
# File 'lib/chef-cli/cookbook_profiler/git.rb', line 100

def remote_name
  @remote_name ||= git!("config --get branch.#{current_branch}.remote", returns: [0, 1]).stdout.strip
end

#revisionObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/chef-cli/cookbook_profiler/git.rb', line 57

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_remotesObject



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/chef-cli/cookbook_profiler/git.rb', line 78

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?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/chef-cli/cookbook_profiler/git.rb', line 74

def unpublished_commits?
  synchronized_remotes.empty?
end