Class: ChefDK::CookbookProfiler::Git

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#chefdk_home, #err, #git_bin_dir, #git_windows_bin_dir, #msg, #omnibus_apps_dir, #omnibus_bin_dir, #omnibus_chefdk_location, #omnibus_embedded_bin_dir, #omnibus_env, #omnibus_install?, #omnibus_root, #stderr, #stdout, #system_command, #usr_bin_path, #usr_bin_prefix

Constructor Details

#initialize(cookbook_path) ⇒ Git

Returns a new instance of Git.



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

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

Instance Attribute Details

#cookbook_pathObject (readonly)

Returns the value of attribute cookbook_path.



26
27
28
# File 'lib/chef-dk/cookbook_profiler/git.rb', line 26

def cookbook_path
  @cookbook_path
end

Instance Method Details

#clean?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/chef-dk/cookbook_profiler/git.rb', line 63

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

#current_branchObject



101
102
103
# File 'lib/chef-dk/cookbook_profiler/git.rb', line 101

def current_branch
  @current_branch ||= detect_current_branch
end

#have_remote?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/chef-dk/cookbook_profiler/git.rb', line 97

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

#profile_dataHash

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

Returns:

  • (Hash)

    Hashed used for pinning cookbook versions within a Policfile.lock



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chef-dk/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 'master'
    # git config --get branch.master.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



84
85
86
87
88
89
90
91
# File 'lib/chef-dk/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_nameObject



93
94
95
# File 'lib/chef-dk/cookbook_profiler/git.rb', line 93

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

#revisionObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/chef-dk/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_remotesObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/chef-dk/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?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/chef-dk/cookbook_profiler/git.rb', line 67

def unpublished_commits?
  synchronized_remotes.empty?
end