-
Notifications
You must be signed in to change notification settings - Fork 24
/
git.rb
161 lines (139 loc) · 4.62 KB
/
git.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#
# Copyright:: Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require_relative "../helpers"
module ChefCLI
module CookbookProfiler
class Git
include Helpers
@@git_memo = {}
attr_reader :cookbook_path
def self.uncache
@@git_memo = {}
end
def initialize(cookbook_path)
@cookbook_path = cookbook_path
@unborn_branch = nil
@unborn_branch_ref = nil
end
# @return [Hash] Hashed used for pinning cookbook versions within a Policyfile.lock
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
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
def clean?
git!("diff-files --quiet", returns: [0, 1]).exitstatus == 0
end
def unpublished_commits?
synchronized_remotes.empty?
end
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
def remote
@remote_url ||=
if have_remote?
git!("config --get remote.#{remote_name}.url").stdout.strip
else
nil
end
end
def remote_name
@remote_name ||= git!("config --get branch.#{current_branch}.remote", returns: [0, 1]).stdout.strip
end
def have_remote?
!remote_name.empty? && remote_name != "."
end
def current_branch
@current_branch ||= detect_current_branch
end
private
def git!(subcommand, options = {})
cmd = git(subcommand, options)
cmd.error!
cmd
end
def git(subcommand, options = {})
memo_key = [subcommand, options]
if @@git_memo.has_key?(memo_key)
rv = @@git_memo[memo_key]
else
options = { cwd: cookbook_path }.merge(options)
rv = system_command("git #{subcommand}", options)
@@git_memo[memo_key] = rv
end
rv
end
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
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
end
end
end